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