]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - latester/latester.c
Merge branch 'master' of rtime.felk.cvut.cz:/can-benchmark
[can-benchmark.git] / latester / latester.c
1 /**************************************************************************/
2 /* CAN latency tester                                                     */
3 /* Copyright (C) 2010 Michal Sojka, DCE FEE CTU Prague                    */
4 /* License: GPLv2                                                         */
5 /**************************************************************************/
6
7 #include <ctype.h>
8 #include <errno.h>
9 #include <error.h>
10 #include <fcntl.h>
11 #include <math.h>
12 #include <net/if.h>
13 #include <poll.h>
14 #include <popt.h>
15 #include <pthread.h>
16 #include <semaphore.h>
17 #include <sched.h>
18 #include <signal.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/ioctl.h>
25 #include <sys/mman.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <talloc.h>
31 #include <unistd.h>
32
33 #include <linux/can.h>
34 #include <linux/can/raw.h>
35
36 #include "histogram.h"
37
38 #ifndef DEBUG
39 #define dbg(level, fmt, arg...) do {} while (0)
40 #else
41 #define dbg(level, fmt, arg...) do { if (level <= DEBUG) { printf("candping: " fmt, ## arg); } } while (0)
42 #endif
43
44 #define INTERRUPTED_SYSCALL(errno) (errno == EINTR || errno == ERESTART)
45
46 #define MEMSET_ZERO(obj) memset(&(obj), 0, sizeof(obj))
47
48 /* Global variables */
49 volatile sig_atomic_t finish_flag = 0;  /* Threads should terminate. */
50 sem_t finish_sem;               /* Thread signals a termination */
51
52 /* Command line options */
53 struct options {
54         char **interface;
55         canid_t id;
56         unsigned period_us;
57         unsigned timeout_ms;
58         unsigned count;
59         unsigned oneattime;
60         FILE *file;
61         FILE *histogram;
62         int length;
63         int userhist;
64 };
65
66 struct options opt = {
67         .id = 10,
68         .period_us = 0,
69         .timeout_ms = 1000,
70         .length = 2,
71 };
72
73 int num_interfaces = 0;
74 int count = 0;                  /* Number of sent messages */
75 int completion_pipe[2];
76
77 struct msg_info {
78         canid_t id;
79         uint8_t length;
80         struct timespec ts_sent, ts_sent_kern;
81         struct timespec ts_rx_onwire, ts_rx_onwire_kern;
82         struct timespec ts_rx_final, ts_rx_final_kern;
83         struct can_frame sent, received;
84 };
85
86 #define MAX_INFOS 10000
87 struct msg_info msg_infos[MAX_INFOS];
88
89 struct histogram histogram;
90
91 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
92         /* documentation see lib.h */
93
94         int i,offset;
95         int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
96
97         if (cf->can_id & CAN_ERR_FLAG) {
98                 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
99                 offset = 9;
100         } else if (cf->can_id & CAN_EFF_FLAG) {
101                 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
102                 offset = 9;
103         } else {
104                 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
105                 offset = 4;
106         }
107
108         if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
109                 sprintf(buf+offset, "R");
110         else
111                 for (i = 0; i < dlc; i++) {
112                         sprintf(buf+offset, "%02X", cf->data[i]);
113                         offset += 2;
114                         if (sep && (i+1 < dlc))
115                                 sprintf(buf+offset++, ".");
116                 }
117 }
118
119 static inline struct msg_info *frame2info(struct can_frame *frame)
120 {
121         uint16_t idx;
122         if (frame->can_dlc >= 2) {
123                 memcpy(&idx, frame->data, sizeof(idx));
124                 if (idx >= MAX_INFOS)
125                         error(1, 0, "%s idx too high", __FUNCTION__);
126         } else {
127                 
128                 error(1, 0, "%s error", __FUNCTION__);
129         }
130         return &msg_infos[idx];
131 }
132
133 static inline char *tstamp_str(const void *ctx, struct timespec *tstamp)
134 {
135         return talloc_asprintf(ctx, "%ld.%06ld",
136                                tstamp->tv_sec, tstamp->tv_nsec/1000);
137 }
138
139 void msg_info_print(FILE *f, struct msg_info *mi)
140 {
141         struct timespec diff;
142         void *local = talloc_new (NULL);
143         static long num = 0;
144         char sent[64], received[64];
145
146         sprint_canframe(sent, &mi->sent, true);
147         sprint_canframe(received, &mi->received, true);
148
149 #define S(ts) tstamp_str(local, &ts)
150 #define DIFF(a, b) (timespec_subtract(&diff, &b, &a), S(diff))
151
152         if (num_interfaces == 2)
153                 fprintf(f, "%ld: %s %s -> %s (%s) %s = %s (%s)\n",
154                         num, S(mi->ts_sent), sent, S(mi->ts_rx_final_kern), S(mi->ts_rx_final), received,
155                        DIFF(mi->ts_sent, mi->ts_rx_final_kern),
156                        DIFF(mi->ts_sent, mi->ts_rx_final));
157         else
158                 fprintf(f, "%ld: %s %s -> %s (%s) -> %s (%s) %s = %s (%s), %s (%s)\n",
159                         num, S(mi->ts_sent), sent,
160                         S(mi->ts_rx_onwire_kern), S(mi->ts_rx_onwire),
161                         S(mi->ts_rx_final_kern), S(mi->ts_rx_final), received,
162                         DIFF(mi->ts_sent, mi->ts_rx_onwire_kern),
163                         DIFF(mi->ts_sent, mi->ts_rx_onwire),
164                         DIFF(mi->ts_rx_onwire_kern, mi->ts_rx_final_kern),
165                         DIFF(mi->ts_rx_onwire, mi->ts_rx_final));
166 #undef S
167 #undef DIFF
168         num++;
169         talloc_free (local);
170 }
171
172 int msg_info_store(FILE *f, struct msg_info *mi)
173 {
174         struct timespec diff;
175         void *local = talloc_new (NULL);
176         static long num = 0;
177
178 #define S(ts) tstamp_str(local, &ts)
179 #define DIFF(a, b) (timespec_subtract(&diff, &b, &a), S(diff))
180
181         if (num_interfaces == 2)
182                 fprintf(f, "%ld %d %d %s\n",
183                         num, mi->id, mi->length,
184                         DIFF(mi->ts_sent, mi->ts_rx_final_kern));
185         else
186                 fprintf(f, "%ld %d %d %s\n",
187                         num, mi->id, mi->length,
188                         DIFF(mi->ts_rx_onwire_kern, mi->ts_rx_final_kern));
189 #undef S
190 #undef DIFF
191         talloc_free (local);
192 }
193
194
195 /* Subtract the `struct timespec' values X and Y, storing the result in
196    RESULT.  Return 1 if the difference is negative, otherwise 0.  */
197
198 int timespec_subtract (struct timespec *result, struct timespec *x, struct timespec *yy)
199 {
200         struct timespec ylocal = *yy, *y = &ylocal;
201         /* Perform the carry for the later subtraction by updating Y. */
202         if (x->tv_nsec < y->tv_nsec) {
203                 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
204                 y->tv_nsec -= 1000000000 * nsec;
205                 y->tv_sec += nsec;
206         }
207         if (x->tv_nsec - y->tv_nsec > 1000000000) {
208                 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
209                 y->tv_nsec += 1000000000 * nsec;
210                 y->tv_sec -= nsec;
211         }
212
213         /* Compute the time remaining to wait.
214            `tv_nsec' is certainly positive. */
215         result->tv_sec = x->tv_sec - y->tv_sec;
216         result->tv_nsec = x->tv_nsec - y->tv_nsec;
217
218         /* Return 1 if result is negative. */
219         return x->tv_sec < y->tv_sec;
220 }
221
222 void dbg_print_timespec(char *msg, struct timespec *tv)
223 {
224
225         printf("%s sec=%ld nsec=%ld\n", msg, tv->tv_sec, tv->tv_nsec);
226 }
227
228 static inline unsigned get_msg_latency_us(struct msg_info *mi)
229 {
230         struct timespec diff;
231         if (num_interfaces == 3)
232                 if (opt.userhist)
233                         timespec_subtract(&diff, &mi->ts_rx_final_kern, &mi->ts_rx_onwire_kern);
234                 else
235                         timespec_subtract(&diff, &mi->ts_rx_final, &mi->ts_rx_onwire);
236         else
237                 if (opt.userhist)
238                         timespec_subtract(&diff, &mi->ts_rx_final, &mi->ts_sent);
239                 else
240                         timespec_subtract(&diff, &mi->ts_rx_final_kern, &mi->ts_sent);
241         return diff.tv_sec * 1000000 + diff.tv_nsec/1000;
242 }
243
244 void set_sched_policy_and_prio(int policy, int rtprio)
245 {
246         struct sched_param scheduling_parameters;
247         int maxprio=sched_get_priority_max(policy);
248         int minprio=sched_get_priority_min(policy);
249
250         if((rtprio < minprio) || (rtprio > maxprio))
251                 error(1, 0, "The priority for requested policy is out of <%d, %d> range\n",
252                       minprio, maxprio);
253
254         scheduling_parameters.sched_priority = rtprio;
255
256         if (0 != pthread_setschedparam(pthread_self(), policy, &scheduling_parameters))
257                 error(1, errno, "pthread_setschedparam error");
258 }
259
260 void term_handler(int signum)
261 {
262         finish_flag = 1;
263 }
264
265 static inline int sock_get_if_index(int s, const char *if_name)
266 {
267         struct ifreq ifr;
268         MEMSET_ZERO(ifr);
269
270         strcpy(ifr.ifr_name, if_name);
271         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
272                 error(1, errno, "SIOCGIFINDEX '%s'", if_name);
273         return ifr.ifr_ifindex;
274 }
275
276 static inline get_tstamp(struct timespec *ts)
277 {
278         clock_gettime(CLOCK_REALTIME, ts);
279 }
280
281 int send_frame(int socket)
282 {
283         struct can_frame frame;
284         struct msg_info *mi;
285         int ret;
286         static int curr_msg = -1;
287         int i;
288         uint16_t idx;
289
290         MEMSET_ZERO(frame);
291         i = curr_msg+1;
292         while (msg_infos[i].id != -1 && i != curr_msg) {
293                 i++;
294                 if (i >= MAX_INFOS)
295                         i = 0;
296         }
297         if (i == curr_msg)
298                 error(1, 0, "Msg info table is full! Probably, many packets were lost.");
299         else
300                 curr_msg = i;
301
302         frame.can_id = opt.id;
303         if (opt.length < 2)
304                 error(1, 0, "Length < 2 is not yet supported");
305         frame.can_dlc = opt.length;
306         idx = curr_msg;
307         memcpy(frame.data, &idx, sizeof(idx));
308         mi = frame2info(&frame);
309
310         mi->id = frame.can_id;
311         mi->length = frame.can_dlc;
312         get_tstamp(&mi->ts_sent);
313         mi->sent = frame;
314         ret = write(socket, &frame, sizeof(frame));
315         return ret;
316 }
317
318 void msg_info_free(struct msg_info *mi)
319 {
320         mi->id = -1;
321 }
322
323 static inline void get_next_timeout(struct timespec *timeout)
324 {
325         struct timespec now;
326         static struct timespec last = {-1, 0 };
327
328         clock_gettime(CLOCK_MONOTONIC, &now);
329
330         if (last.tv_sec == -1)
331                 last = now;
332         if (opt.period_us != 0) {
333                 last.tv_sec += opt.period_us/1000000;
334                 last.tv_nsec += (opt.period_us%1000000)*1000;
335                 while (last.tv_nsec >= 1000000000) {
336                         last.tv_nsec -= 1000000000;
337                         last.tv_sec++;
338                 }
339                 timespec_subtract(timeout, &last, &now);
340         } else if (opt.timeout_ms != 0) {
341                 timeout->tv_sec = opt.timeout_ms/1000;
342                 timeout->tv_nsec = (opt.timeout_ms%1000)*1000000;
343         } else
344                 error(1, 0, "Timeout and period cannot be both zero");
345 }
346
347 void receive(int s, struct can_frame *frame, struct timespec *ts_kern, struct timespec *ts_user)
348 {
349         char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
350         struct iovec iov;
351         struct msghdr msg;
352         struct cmsghdr *cmsg;
353         struct sockaddr_can addr;
354         int nbytes;
355         static uint64_t dropcnt = 0;
356
357         iov.iov_base = frame;
358         msg.msg_name = &addr;
359         msg.msg_iov = &iov;
360         msg.msg_iovlen = 1;
361         msg.msg_control = &ctrlmsg;
362
363         /* these settings may be modified by recvmsg() */
364         iov.iov_len = sizeof(*frame);
365         msg.msg_namelen = sizeof(addr);
366         msg.msg_controllen = sizeof(ctrlmsg);
367         msg.msg_flags = 0;
368
369         nbytes = recvmsg(s, &msg, 0);
370         if (nbytes < 0)
371                 error(1, errno, "recvmsg");
372
373         if (nbytes < sizeof(struct can_frame))
374                 error(1, 0, "recvmsg: incomplete CAN frame\n");
375
376         get_tstamp(ts_user);
377         MEMSET_ZERO(*ts_kern);
378         for (cmsg = CMSG_FIRSTHDR(&msg);
379              cmsg && (cmsg->cmsg_level == SOL_SOCKET);
380              cmsg = CMSG_NXTHDR(&msg,cmsg)) {
381                 if (cmsg->cmsg_type == SO_TIMESTAMPNS)
382                         *ts_kern = *(struct timespec *)CMSG_DATA(cmsg);
383                 else if (cmsg->cmsg_type == SO_RXQ_OVFL)
384                         dropcnt += *(__u32 *)CMSG_DATA(cmsg);
385         }
386
387 }
388
389 void process_tx(int s)
390 {
391         error(1, 0, "%s: not implemented", __FUNCTION__);
392 }
393
394 void process_on_wire_rx(int s)
395 {
396         struct timespec ts_kern, ts_user, ts_diff;
397         struct can_frame frame;
398         struct msg_info *mi;
399         receive(s, &frame, &ts_kern, &ts_user);
400         mi = frame2info(&frame);
401         mi->ts_rx_onwire_kern = ts_kern;
402         mi->ts_rx_onwire = ts_user;
403 }
404
405
406 void process_final_rx(int s)
407 {
408         struct timespec ts_kern, ts_user, ts_diff;
409         struct can_frame frame;
410         struct msg_info *mi;
411         int ret;
412         
413         receive(s, &frame, &ts_kern, &ts_user);
414         mi = frame2info(&frame);
415         mi->ts_rx_final_kern = ts_kern;
416         mi->ts_rx_final = ts_user;
417         mi->received = frame;
418
419         if (opt.histogram)
420                 histogram_add(&histogram, get_msg_latency_us(mi));
421
422         ret = write(completion_pipe[1], &mi, sizeof(mi));
423         if (ret == -1)
424                 error(1, errno, "completion_pipe write");
425 }
426
427 void *measure_thread(void *arg)
428 {
429         int s, i, ret;
430         struct pollfd pfd[3];
431         struct timespec timeout;
432         struct sockaddr_can addr;
433         sigset_t set;
434         unsigned msg_in_progress = 0;
435
436         MEMSET_ZERO(pfd);
437
438         for (i=0; i<num_interfaces; i++) {
439                 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
440                         error(1, errno, "socket");
441
442                 addr.can_family = AF_CAN;
443                 addr.can_ifindex = sock_get_if_index(s, opt.interface[i]);
444
445                 if (i == 0) {   /* TX socket */
446                         /* disable default receive filter on this RAW socket */
447                         /* This is obsolete as we do not read from the socket at all, but for */
448                         /* this reason we can remove the receive list in the Kernel to save a */
449                         /* little (really a very little!) CPU usage.                          */
450                         if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0) == -1)
451                                 error(1, errno, "SOL_CAN_RAW");
452                 }
453
454                 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
455                         error(1, errno, "bind");
456
457                 const int timestamp_on = 1;
458                 if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMPNS,
459                                &timestamp_on, sizeof(timestamp_on)) < 0)
460                         error(1, errno, "setsockopt SO_TIMESTAMP");
461
462                 const int dropmonitor_on = 1;
463                 if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL,
464                                &dropmonitor_on, sizeof(dropmonitor_on)) < 0)
465                         error(1, errno, "setsockopt SO_RXQ_OVFL not supported by your Linux Kernel");
466
467                 pfd[i].fd = s;
468                 if (i == 0)
469                         pfd[i].events = POLLIN | POLLERR | ((opt.period_us == 0 && !opt.oneattime) ? POLLOUT : 0);
470                 else
471                         pfd[i].events = POLLIN;
472         }
473
474         set_sched_policy_and_prio(SCHED_FIFO, 99);
475
476 #define SEND()                                          \
477         do {                                            \
478                 ret = send_frame(pfd[0].fd);            \
479                 if (ret != sizeof(struct can_frame))    \
480                         error(1, errno, "send_frame (line %d)", __LINE__); \
481                 count++;                                \
482                 msg_in_progress++;                      \
483         } while (0)
484
485         if (opt.oneattime) {
486                 SEND();
487                 count = 1;
488         }
489
490         while (!finish_flag &&
491                (opt.count == 0 || count < opt.count || msg_in_progress != 0)) {
492
493                 get_next_timeout(&timeout);
494                 //printf("ppoll"); fflush(stdout);
495                 ret = ppoll(pfd, num_interfaces, &timeout, NULL);
496                 //printf("=%d\n", ret);
497                 switch (ret) {
498                 case -1: // Error
499                         if (!INTERRUPTED_SYSCALL(errno))
500                                 error(1, errno, "ppoll");
501                         break;
502                 case 0: // Timeout
503                         if (opt.period_us) {
504                                 if (opt.count == 0 || count < opt.count) {
505                                         SEND();
506                                 }
507                         } else {
508                                 error(1, 0, "poll timeout");
509                         }
510                         break;
511                 default: // Event
512                         if (pfd[0].revents & (POLLIN|POLLERR)) {
513                                 process_tx(pfd[0].fd);
514                         }
515                         if (pfd[0].revents & POLLOUT) {
516                                 if (opt.count == 0 || count < opt.count)
517                                         SEND();
518                         }
519                         pfd[0].revents = 0;
520
521                         if (num_interfaces == 3 && pfd[1].revents != 0) {
522                                 process_on_wire_rx(pfd[1].fd);
523                                 pfd[1].revents = 0;
524                         }
525
526                         i = (num_interfaces == 2) ? 1 : 2;
527                         if (pfd[i].revents != 0) {
528                                 process_final_rx(pfd[i].fd);
529                                 msg_in_progress--;
530                                 pfd[i].revents = 0;
531                                 if ((opt.count == 0 || count < opt.count) &&
532                                     opt.oneattime) {
533                                         SEND();
534                                 }
535                         }
536                 }
537         }
538
539         for (i=0; i<num_interfaces; i++)
540                 close(pfd[i].fd);
541
542         return NULL;
543 }
544
545 struct poptOption optionsTable[] = {
546         { "device", 'd', POPT_ARG_ARGV, &opt.interface, 'd', "Interface to use. Must be given two times (tx, rx) or three times (tx, rx1, rx2)", "interface" },
547         { "count",  'c', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT,  &opt.count,   0,   "The count of messages to send, zero corresponds to infinity", "num"},
548         { "id",     'i', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT,  &opt.id,      0,   "CAN ID of sent messages", "id"},
549         { "period", 'p', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT,  &opt.period_us, 0, "Period for sending messages or zero (default) to send as fast as possible", "us"},
550         { "timeout",'t', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT,  &opt.timeout_ms,0, "Timeout when period is zero", "ms"},
551         { "oneattime",'o', POPT_ARG_NONE,                         &opt.oneattime,0,  "Send the next message only when the previous was finally received"},
552         { "verbose",'v', POPT_ARG_NONE,                           NULL, 'v',         "Send the next message only when the previous was finally received"},
553         { "file",   'f', POPT_ARG_STRING,                         NULL, 'f',         "File where to store results", "filename"},
554         { "histogram", 'h', POPT_ARG_STRING,                      NULL, 'h',         "Store histogram in file", "filename"},
555         { "length", 'l', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT,  &opt.length, 0,    "The length of generated messages", "bytes"},
556         { "userhist", 'u', POPT_ARG_NONE,                         &opt.userhist, 0,  "Generate histogram from userspace timestamps"},
557         POPT_AUTOHELP
558         { NULL, 0, 0, NULL, 0 }
559 };
560
561 int parse_options(int argc, const char *argv[])
562 {
563         int c;
564         poptContext optCon;   /* context for parsing command-line options */
565
566         optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
567         //poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>");
568
569         /* Now do options processing */
570         while ((c = poptGetNextOpt(optCon)) >= 0) {
571                 switch (c) {
572                 case 'd':
573                         num_interfaces++;
574                         break;
575                 case 'f':
576                         opt.file = fopen(poptGetOptArg(optCon), "w");
577                         if (!opt.file)
578                                 error(1, errno, "fopen: %s", poptGetOptArg(optCon));
579                         break;
580                 case 'h':
581                         opt.histogram = fopen(poptGetOptArg(optCon), "w");
582                         if (!opt.histogram)
583                                 error(1, errno, "fopen: %s", poptGetOptArg(optCon));
584                         break;
585                 }
586         }
587         if (c < -1)
588                 error(1, 0, "%s: %s\n",
589                       poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
590                       poptStrerror(c));
591
592         if (num_interfaces < 2 || num_interfaces > 3)
593                 error(1, 0, "-d option must be given exactly 2 or 3 times");
594
595         if (opt.oneattime && opt.period_us)
596                 error(1, 0, "oneattime and period cannot be specified at the same time");
597
598         poptFreeContext(optCon);
599
600         return 0;
601 }
602
603
604 int main(int argc, const char *argv[])
605 {
606         pthread_t thread;
607         sigset_t set;
608         int ret, i;
609
610         parse_options(argc, argv);
611
612         mlockall(MCL_CURRENT | MCL_FUTURE);
613
614         signal(SIGINT, term_handler);
615         signal(SIGTERM, term_handler);
616
617         for (i=0; i<MAX_INFOS; i++)
618                 msg_infos[i].id = -1;
619
620         if (opt.histogram) {
621                 histogram_init(&histogram, 5000000, 1);
622         }
623
624         ret = pipe(completion_pipe);
625         if (ret == -1)
626                 error(1, errno, "pipe");
627         ret = fcntl(completion_pipe[1], F_SETFL, O_NONBLOCK);
628         if (ret == -1)
629                 error(1, errno, "pipe fcntl");
630         
631         pthread_create(&thread, 0, measure_thread, NULL);
632
633         struct timespec next, now, diff;
634         clock_gettime(CLOCK_MONOTONIC, &next);
635         int completed = 0;
636         while (!finish_flag && (opt.count == 0 || completed < opt.count)) {
637                 struct pollfd pfd[1];
638                 pfd[0].fd = completion_pipe[0];
639                 pfd[0].events = POLLIN;
640                 ret = poll(pfd, 1, 100);
641                 if (ret == -1 && !INTERRUPTED_SYSCALL(errno))
642                         error(1, errno, "poll main");
643                 if (ret > 0 && (pfd[0].revents & POLLIN)) {
644                         struct msg_info *mi;
645                         int ret;
646                         ret = read(completion_pipe[0], &mi, sizeof(mi));
647                         if (ret < sizeof(mi))
648                                 error(1, errno, "read completion returned %d", ret);
649                         if (opt.file)
650                                 msg_info_print(opt.file, mi);
651                         msg_info_free(mi);
652                         completed++;
653                 }
654
655                 clock_gettime(CLOCK_MONOTONIC, &now);
656                 if (timespec_subtract(&diff, &next, &now)) {
657                         printf("\rMessage %d", count);
658                         fflush(stdout);
659                         next.tv_nsec += 100000000;
660                         while (next.tv_nsec >= 1000000000) {
661                                 next.tv_nsec -= 1000000000;
662                                 next.tv_sec++;
663                         }
664                 }
665         }
666         printf("\rMessage %d\n", count);
667
668         pthread_join(thread, NULL);
669
670         close(completion_pipe[0]);
671         close(completion_pipe[1]);
672
673         if (opt.histogram) {
674                 histogram_fprint(&histogram, opt.histogram);
675                 fclose(opt.histogram);
676         }
677         if (opt.file)
678                 fclose(opt.file);
679
680         return 0;
681 }