1 /**************************************************************************/
2 /* CAN latency tester */
3 /* Copyright (C) 2010, 2011, 2012, 2013 Michal Sojka, DCE FEE CTU Prague */
5 /**************************************************************************/
16 #include <semaphore.h>
24 #include <sys/ioctl.h>
26 #include <sys/socket.h>
29 #include <sys/types.h>
33 #include <linux/can.h>
34 #include <linux/can/raw.h>
36 #include "histogram.h"
41 #define dbg(level, fmt, arg...) do {} while (0)
43 #define dbg(level, fmt, arg...) do { if (level <= DEBUG) { printf("candping: " fmt, ## arg); } } while (0)
46 #define INTERRUPTED_SYSCALL(errno) (errno == EINTR || errno == ERESTART)
48 #define MEMSET_ZERO(obj) memset(&(obj), 0, sizeof(obj))
50 /* Global variables */
51 volatile sig_atomic_t finish_flag = 0; /* Threads should terminate. */
52 sem_t finish_sem; /* Thread signals a termination */
54 /* Command line options */
67 /* Temporary variables */
74 struct options opt = {
85 struct timespec tic, tac;
87 unsigned invalid_frame;
90 int num_interfaces = 0;
91 int count = 0; /* Number of sent messages */
92 unsigned msg_in_progress = 0;
93 int completion_pipe[2];
98 struct timespec ts_sent, ts_sent_kern;
99 struct timespec ts_rx_onwire, ts_rx_onwire_kern;
100 struct timespec ts_rx_final, ts_rx_final_kern;
101 struct can_frame sent, received;
102 unsigned lat_measured_us, tx_time_us;
105 #define MAX_INFOS 10000
106 struct msg_info msg_infos[MAX_INFOS];
108 struct histogram histogram, histogram_gw;
110 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
111 /* documentation see lib.h */
114 int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
116 if (cf->can_id & CAN_ERR_FLAG) {
117 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
119 } else if (cf->can_id & CAN_EFF_FLAG) {
120 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
123 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
127 if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
128 sprintf(buf+offset, "R");
130 for (i = 0; i < dlc; i++) {
131 sprintf(buf+offset, "%02X", cf->data[i]);
133 if (sep && (i+1 < dlc))
134 sprintf(buf+offset++, ".");
138 static inline uint16_t frame_index(struct can_frame *frame)
141 if (frame->can_dlc >= 2) {
142 memcpy(&idx, frame->data, sizeof(idx));
143 if (idx >= MAX_INFOS)
144 error(1, 0, "%s idx too high", __FUNCTION__);
147 error(1, 0, "%s error", __FUNCTION__);
152 static inline struct msg_info *frame2info(struct can_frame *frame)
154 return &msg_infos[frame_index(frame)];
157 static inline char *tstamp_str(const void *ctx, struct timespec *tstamp)
159 return talloc_asprintf(ctx, "%ld.%06ld",
160 tstamp->tv_sec, tstamp->tv_nsec/1000);
163 /* Functions and types for CRC checks.
165 * Generated on Wed Sep 21 22:30:11 2011,
166 * by pycrc v0.7.8, http://www.tty1.net/pycrc/
167 * using the configuration:
174 * Algorithm = table-driven
175 *****************************************************************************/
176 typedef uint16_t crc_t;
178 static const crc_t crc_table[2] = {
182 crc_t crc_update(crc_t crc, uint32_t data, size_t bit_len)
184 unsigned int tbl_idx;
185 /* crc_t bc = crc; */
186 /* uint32_t bd = data; */
187 /* size_t bl = bit_len; */
190 tbl_idx = (crc >> 14) ^ (data >> 31);
191 crc = crc_table[tbl_idx & 0x01] ^ (crc << 1);
195 /* printf("crc: 0x%04x -> 0x%04x data: 0x%08x len: %d\n", */
196 /* bc, crc, bd, bl); */
200 uint32_t calc_bitmap_crc(uint32_t *bitmap, unsigned start, unsigned end)
203 crc = crc_update(crc, bitmap[0] << start, 32 - start);
204 crc = crc_update(crc, bitmap[1], 32);
205 crc = crc_update(crc, bitmap[2], end - 64 > 32 ? 32 : end - 64); /* FIXME: This seems to be broken for end < 64 */
206 crc = crc_update(crc, bitmap[3], end > 96 ? end - 96 : 0);
210 unsigned calc_stuff_bits(struct can_frame *frame) {
212 unsigned start = 0, end;
213 uint32_t mask, ones = 0, basemask = 0xf8000000;
214 unsigned stuffed = 0;
215 memset(bitmap, 0, sizeof(bitmap));
218 /* char received[64]; */
219 /* sprint_canframe(received, frame, true); */
220 /* printf("Frame: %s\n", received); */
222 if (frame->can_id & CAN_EFF_FLAG) {
224 // [0] |...........................sBBBB| s = SOF, B = Base ID (11 bits)
225 // [1] |BBBBBBBSIEEEEEEEEEEEEEEEEEER10DLC4| S = SRR, I = IDE, E = Extended ID (18 bits), DLC4 = DLC
226 // [2] |00000000111111112222222233333333| Data bytes, MSB first
227 // [3] |44444444555555556666666677777777|
229 ((frame->can_id & CAN_EFF_MASK) >> 25);
231 ((frame->can_id & CAN_EFF_MASK) >> 18) << 25 |
233 ((frame->can_id & CAN_EFF_MASK) & 0x3ffff) << 7 |
234 (!!(frame->can_id & CAN_RTR_FLAG)) << 6 |
236 frame->can_dlc & 0xf;
237 memcpy(&bitmap[2], &frame->data, frame->can_dlc);
239 end = 64 + 8*frame->can_dlc;
242 // [0] |.............sIIIIIIIIIIIRE0DLC4| s = SOF, I = ID (11 bits), R = RTR, E = IDE, DLC4 = DLC
243 // [1] |00000000111111112222222233333333| Data bytes, MSB first
244 // [2] |44444444555555556666666677777777|
246 (frame->can_id << 7) |
247 (!!(frame->can_id & CAN_RTR_FLAG)) << 6 |
249 frame->can_dlc & 0xf;
250 memcpy(&bitmap[1], &frame->data, frame->can_dlc);
252 end = 32 + 8*frame->can_dlc;
254 crc = calc_bitmap_crc(bitmap, start, end);
255 uint32_t crc_be = htons(crc << 17);
256 memcpy((char*)bitmap + end/8, &crc_be, 2); /* end is guaranteed to be divisible by 8 */
259 while (start < end) {
260 mask = basemask >> (start & 0x1f);
262 ones = ones ? mask : 0;
263 uint32_t chunk = (bitmap[start >> 5] & mask) ^ ones;
264 //printf("start=%d bitmap=0x%08x mask=0x%08x ones=0x%08x chunk=0x%08x\n", start, bitmap[start >> 5], mask, ones, chunk);
266 unsigned change = __builtin_clz(chunk);
267 start = start & ~0x1f | change;
268 basemask = 0xf8000000;
270 unsigned oldstart = start;
271 start += __builtin_popcount(mask);
272 mask = (oldstart & 0x1f) ? basemask << (-oldstart & 0x1f) : 0;
273 //printf("oldstart=%d shl=%d mask=0x%08x\n", oldstart, -oldstart & 0x1f, mask);
274 if (mask && start < end)
276 if (start <= end && !mask) {
278 basemask = 0xf0000000;
279 //printf("stuffed %d\n", !ones);
286 //printf ("STUFFED %d BITS\n", stuffed);
290 unsigned calc_frame_txtime_us(struct can_frame *frame) {
291 return calc_stuff_bits(frame) +
294 ((frame->can_id & CAN_EFF_FLAG) ?
306 3 + /* CRC del, ACK, ACK del */
310 void msg_info_print(FILE *f, struct msg_info *mi)
312 struct timespec diff;
313 void *local = talloc_new (NULL);
315 char sent[64], received[64];
320 sprint_canframe(sent, &mi->sent, true);
321 sprint_canframe(received, &mi->received, true);
323 #define S(ts) tstamp_str(local, &ts)
324 #define DIFF(a, b) (timespec_subtract(&diff, &b, &a), S(diff))
326 switch (num_interfaces) {
328 fprintf(f, "%ld: %s %s -> %s (%s) %s = %s (%s) %d\n",
329 num, S(mi->ts_sent), sent, S(mi->ts_rx_final_kern), S(mi->ts_rx_final), received,
330 DIFF(mi->ts_sent, mi->ts_rx_final_kern),
331 DIFF(mi->ts_sent, mi->ts_rx_final),
335 fprintf(f, "%ld: %s %s -> %s (%s) -> %s (%s) %s = %s (%s), %s (%s) %d\n",
336 num, S(mi->ts_sent), sent,
337 S(mi->ts_rx_onwire_kern), S(mi->ts_rx_onwire),
338 S(mi->ts_rx_final_kern), S(mi->ts_rx_final), received,
339 DIFF(mi->ts_sent, mi->ts_rx_onwire_kern),
340 DIFF(mi->ts_sent, mi->ts_rx_onwire),
341 DIFF(mi->ts_rx_onwire_kern, mi->ts_rx_final_kern),
342 DIFF(mi->ts_rx_onwire, mi->ts_rx_final),
352 /* Subtract the `struct timespec' values X and Y, storing the result in
353 RESULT. Return 1 if the difference is negative, otherwise 0. */
355 int timespec_subtract (struct timespec *result, struct timespec *x, struct timespec *yy)
357 struct timespec ylocal = *yy, *y = &ylocal;
358 /* Perform the carry for the later subtraction by updating Y. */
359 if (x->tv_nsec < y->tv_nsec) {
360 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
361 y->tv_nsec -= 1000000000 * nsec;
364 if (x->tv_nsec - y->tv_nsec > 1000000000) {
365 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
366 y->tv_nsec += 1000000000 * nsec;
370 /* Compute the time remaining to wait.
371 `tv_nsec' is certainly positive. */
372 result->tv_sec = x->tv_sec - y->tv_sec;
373 result->tv_nsec = x->tv_nsec - y->tv_nsec;
375 /* Return 1 if result is negative. */
376 return x->tv_sec < y->tv_sec;
379 void dbg_print_timespec(char *msg, struct timespec *tv)
382 printf("%s sec=%ld nsec=%ld\n", msg, tv->tv_sec, tv->tv_nsec);
385 static inline void calc_msg_latencies(struct msg_info *mi)
387 struct timespec diff;
388 switch (num_interfaces) {
391 timespec_subtract(&diff, &mi->ts_rx_final, &mi->ts_rx_onwire);
393 timespec_subtract(&diff, &mi->ts_rx_final_kern, &mi->ts_rx_onwire_kern);
397 timespec_subtract(&diff, &mi->ts_rx_final, &mi->ts_sent);
399 timespec_subtract(&diff, &mi->ts_rx_final_kern, &mi->ts_sent);
404 mi->lat_measured_us = diff.tv_sec * 1000000 + diff.tv_nsec/1000;
405 mi->tx_time_us = calc_frame_txtime_us(&mi->received);
408 void set_sched_policy_and_prio(int policy, int rtprio)
410 struct sched_param scheduling_parameters;
411 int maxprio=sched_get_priority_max(policy);
412 int minprio=sched_get_priority_min(policy);
414 if((rtprio < minprio) || (rtprio > maxprio))
415 error(1, 0, "The priority for requested policy is out of <%d, %d> range\n",
418 scheduling_parameters.sched_priority = rtprio;
420 if (0 != pthread_setschedparam(pthread_self(), policy, &scheduling_parameters))
421 error(1, errno, "pthread_setschedparam error");
424 void term_handler(int signum)
429 static inline int sock_get_if_index(int s, const char *if_name)
434 strcpy(ifr.ifr_name, if_name);
435 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
436 error(1, errno, "SIOCGIFINDEX '%s'", if_name);
437 return ifr.ifr_ifindex;
440 static inline get_tstamp(struct timespec *ts)
442 clock_gettime(CLOCK_REALTIME, ts);
456 debugfs = "/sys/kernel/debug";
458 strcpy(path, debugfs);
459 strcat(path,"/tracing/tracing_on");
460 trace_fd = open(path, O_WRONLY);
462 write(trace_fd, "1", 1);
464 strcpy(path, debugfs);
465 strcat(path,"/tracing/trace_marker");
466 marker_fd = open(path, O_WRONLY);
468 strcpy(path, debugfs);
469 strcat(path,"/tracing/set_ftrace_pid");
470 f = fopen(path, "w");
471 fprintf(f, "%d\n", getpid());
473 system("echo function_graph > /sys/kernel/debug/tracing/current_tracer");
474 system("echo can_send > /sys/kernel/debug/tracing/set_graph_function");
475 system("echo > /sys/kernel/debug/tracing/trace");
476 system("echo 1 > /sys/kernel/debug/tracing/tracing_enabled");
481 static inline void trace_on()
484 write(trace_fd, "1", 1);
487 static inline void trace_off(int ret)
489 if (marker_fd >= 0) {
491 sprintf(marker, "write returned %d\n", ret);
492 write(marker_fd, marker, strlen(marker));
495 write(trace_fd, "0", 1);
498 static inline void msg_info_free(struct msg_info *mi)
503 static inline bool msg_info_used(struct msg_info *mi)
508 int send_frame(int socket)
510 struct can_frame frame;
513 static int curr_msg = -1;
519 while (msg_info_used(&msg_infos[i]) && i != curr_msg) {
525 error(1, 0, "Msg info table is full! Probably, many packets were lost.");
529 frame.can_id = opt.id;
531 error(1, 0, "Length < 2 is not yet supported");
532 frame.can_dlc = opt.length;
534 memcpy(frame.data, &idx, sizeof(idx));
535 mi = frame2info(&frame);
537 mi->id = frame.can_id;
538 mi->length = frame.can_dlc;
539 get_tstamp(&mi->ts_sent);
543 ret = write(socket, &frame, sizeof(frame));
546 if (ret == -1 || num_interfaces == 1)
551 static inline send_and_check(int s)
555 if (ret != sizeof(struct can_frame)) {
556 /* if (ret == -1 && errno == ENOBUFS && opt.period_us == 0 && !opt.oneattime) { */
557 /* stats.enobufs++; */
558 /* /\* Ignore this error - pfifo_fast qeuue is full *\/ */
560 error(1, errno, "send_frame (line %d)", __LINE__);
567 static inline void get_next_timeout(struct timespec *timeout)
570 static struct timespec last = {-1, 0 };
572 clock_gettime(CLOCK_MONOTONIC, &now);
574 if (last.tv_sec == -1)
576 if (opt.period_us != 0) {
577 last.tv_sec += opt.period_us/1000000;
578 last.tv_nsec += (opt.period_us%1000000)*1000;
579 while (last.tv_nsec >= 1000000000) {
580 last.tv_nsec -= 1000000000;
583 if (timespec_subtract(timeout, &last, &now) /* is negative */) {
585 memset(timeout, 0, sizeof(*timeout));
587 } else if (opt.timeout_ms != 0) {
588 timeout->tv_sec = opt.timeout_ms/1000;
589 timeout->tv_nsec = (opt.timeout_ms%1000)*1000000;
591 error(1, 0, "Timeout and period cannot be both zero");
594 void receive(int s, struct can_frame *frame, struct timespec *ts_kern, struct timespec *ts_user)
596 char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
599 struct cmsghdr *cmsg;
600 struct sockaddr_can addr;
602 static uint64_t dropcnt = 0;
604 iov.iov_base = frame;
605 msg.msg_name = &addr;
608 msg.msg_control = &ctrlmsg;
610 /* these settings may be modified by recvmsg() */
611 iov.iov_len = sizeof(*frame);
612 msg.msg_namelen = sizeof(addr);
613 msg.msg_controllen = sizeof(ctrlmsg);
616 nbytes = recvmsg(s, &msg, 0);
618 error(1, errno, "recvmsg");
620 if (nbytes < sizeof(struct can_frame))
621 error(1, 0, "recvmsg: incomplete CAN frame\n");
624 MEMSET_ZERO(*ts_kern);
625 for (cmsg = CMSG_FIRSTHDR(&msg);
626 cmsg && (cmsg->cmsg_level == SOL_SOCKET);
627 cmsg = CMSG_NXTHDR(&msg,cmsg)) {
628 if (cmsg->cmsg_type == SO_TIMESTAMPNS)
629 *ts_kern = *(struct timespec *)CMSG_DATA(cmsg);
630 else if (cmsg->cmsg_type == SO_RXQ_OVFL)
631 dropcnt += *(__u32 *)CMSG_DATA(cmsg);
636 void process_tx(int s)
638 error(1, 0, "%s: not implemented", __FUNCTION__);
641 void process_on_wire_rx(int s)
643 struct timespec ts_kern, ts_user, ts_diff;
644 struct can_frame frame;
646 receive(s, &frame, &ts_kern, &ts_user);
647 mi = frame2info(&frame);
648 if (msg_info_used(mi)) {
649 mi->ts_rx_onwire_kern = ts_kern;
650 mi->ts_rx_onwire = ts_user;
652 stats.invalid_frame++;
656 void process_final_rx(int s)
658 struct timespec ts_kern, ts_user, ts_diff;
659 struct can_frame frame;
663 receive(s, &frame, &ts_kern, &ts_user);
664 mi = frame2info(&frame);
665 mi->ts_rx_final_kern = ts_kern;
666 mi->ts_rx_final = ts_user;
667 mi->received = frame;
669 calc_msg_latencies(mi);
671 histogram_add(&histogram, mi->lat_measured_us);
672 histogram_add(&histogram_gw, mi->lat_measured_us - mi->tx_time_us);
674 ret = write(completion_pipe[1], &mi, sizeof(mi));
676 error(1, errno, "completion_pipe write");
679 void *measure_thread(void *arg)
682 struct pollfd pfd[3];
683 struct timespec timeout;
684 struct sockaddr_can addr;
686 int consecutive_timeouts = 0;
690 for (i=0; i<num_interfaces; i++) {
691 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
692 error(1, errno, "socket");
694 addr.can_family = AF_CAN;
695 addr.can_ifindex = sock_get_if_index(s, opt.interface[i]);
697 if (i == 0) { /* TX socket */
698 /* disable default receive filter on this RAW socket */
699 /* This is obsolete as we do not read from the socket at all, but for */
700 /* this reason we can remove the receive list in the Kernel to save a */
701 /* little (really a very little!) CPU usage. */
702 if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0) == -1)
703 error(1, errno, "SOL_CAN_RAW");
706 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
707 error(1, errno, "bind");
709 const int timestamp_on = 1;
710 if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMPNS,
711 ×tamp_on, sizeof(timestamp_on)) < 0)
712 error(1, errno, "setsockopt SO_TIMESTAMP");
714 const int dropmonitor_on = 1;
715 if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL,
716 &dropmonitor_on, sizeof(dropmonitor_on)) < 0)
717 error(1, errno, "setsockopt SO_RXQ_OVFL not supported by your Linux Kernel");
721 pfd[i].events = POLLIN | POLLERR | ((opt.period_us == 0 && !opt.oneattime) ? POLLOUT : 0);
723 pfd[i].events = POLLIN;
726 set_sched_policy_and_prio(SCHED_FIFO, 40);
728 #define SEND() send_and_check(pfd[0].fd)
733 get_tstamp(&stats.tic);
735 while (!finish_flag &&
736 (opt.count == 0 || count < opt.count || msg_in_progress != 0)) {
738 get_next_timeout(&timeout);
739 //printf("ppoll"); fflush(stdout);
740 ret = ppoll(pfd, num_interfaces, &timeout, NULL);
741 //printf("=%d\n", ret);
744 if (!INTERRUPTED_SYSCALL(errno))
745 error(1, errno, "ppoll");
749 if (opt.count == 0 || count < opt.count) {
753 /* Lost message - send a new one */
755 consecutive_timeouts++;
756 if (consecutive_timeouts < 10)
758 else /* Something is really broken */
763 if (pfd[0].revents & (POLLIN|POLLERR)) {
764 process_tx(pfd[0].fd);
766 if (pfd[0].revents & POLLOUT) {
767 if (opt.count == 0 || count < opt.count)
772 if (num_interfaces == 3 && pfd[1].revents & POLLIN) {
773 process_on_wire_rx(pfd[1].fd);
776 if (num_interfaces == 3 && pfd[1].revents & ~POLLIN)
777 error(1, 0, "Unexpected pfd[1].revents: 0x%04x\n", pfd[1].revents);
779 i = (num_interfaces == 2) ? 1 : 2;
780 if (pfd[i].revents & POLLIN) {
781 consecutive_timeouts = 0;
782 process_final_rx(pfd[i].fd);
785 if ((opt.count == 0 || count < opt.count) &&
790 if (pfd[i].revents & ~POLLIN)
791 error(1, 0, "Unexpected pfd[%d].revents: 0x%04x\n", pfd[i].revents);
795 get_tstamp(&stats.tac);
797 for (i=0; i<num_interfaces; i++)
803 struct poptOption optionsTable[] = {
804 { "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" },
805 { "count", 'c', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.count, 0, "The count of messages to send, zero corresponds to infinity", "num"},
806 { "id", 'i', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.id, 0, "CAN ID of sent messages", "id"},
807 { "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"},
808 { "timeout",'t', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.timeout_ms,0, "Timeout when period is zero", "ms"},
809 { "oneattime",'o', POPT_ARG_NONE, &opt.oneattime,0, "Send the next message only when the previous was finally received"},
810 { "verbose",'v', POPT_ARG_NONE, NULL, 'v', "Send the next message only when the previous was finally received"},
811 { "name", 'n', POPT_ARG_STRING, &opt.name, 0, "Prefix of the generated files"},
812 { "length", 'l', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.length, 0, "The length of generated messages", "bytes"},
813 { "userhist", 'u', POPT_ARG_NONE, &opt.userhist, 0, "Generate histogram from userspace timestamps"},
814 { "quiet", 'q', POPT_ARG_NONE, &opt.quiet, 0, "Do not print progress and statistics"},
816 { NULL, 0, 0, NULL, 0 }
819 int parse_options(int argc, const char *argv[])
822 poptContext optCon; /* context for parsing command-line options */
823 void *local = talloc_new (NULL);
825 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
826 //poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>");
828 /* Now do options processing */
829 while ((c = poptGetNextOpt(optCon)) >= 0) {
837 error(1, 0, "%s: %s\n",
838 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
841 if (num_interfaces < 1 || num_interfaces > 3)
842 error(1, 0, "-d option must only be given one, two or three times");
844 if (opt.oneattime && opt.period_us)
845 error(1, 0, "oneattime and period cannot be specified at the same time");
848 char *f = talloc_asprintf(local, "%s-msgs.txt", opt.name);
849 opt.f_msgs = fopen(f, "w");
851 error(1, errno, "fopen: %s", f);
855 char *f = talloc_asprintf(local, "%s-hist-raw.txt", opt.name);
856 opt.f_hist = fopen(f, "w");
858 error(1, errno, "fopen: %s", f);
862 char *f = talloc_asprintf(local, "%s-hist.txt", opt.name);
863 opt.f_hist_gw = fopen(f, "w");
865 error(1, errno, "fopen: %s", f);
869 char *f = talloc_asprintf(local, "%s-stat.txt", opt.name);
870 opt.f_stat = fopen(f, "w");
872 error(1, errno, "fopen: %s", f);
875 poptFreeContext(optCon);
880 void print_progress()
883 if (num_interfaces > 1)
884 printf("\rSent %5d, in progress %5d", count, msg_in_progress);
886 printf("\rSent %5d", count);
891 int main(int argc, const char *argv[])
897 parse_options(argc, argv);
899 mlockall(MCL_CURRENT | MCL_FUTURE);
901 signal(SIGINT, term_handler);
902 signal(SIGTERM, term_handler);
904 for (i=0; i<MAX_INFOS; i++)
905 msg_infos[i].id = -1;
907 histogram_init(&histogram, 5000000, 1);
908 histogram_init(&histogram_gw, 5000000, 1);
910 ret = pipe(completion_pipe);
912 error(1, errno, "pipe");
913 ret = fcntl(completion_pipe[1], F_SETFL, O_NONBLOCK);
915 error(1, errno, "pipe fcntl");
918 if (getenv("LATESTER_CONTROL_HACKBENCH")) {
920 sprintf(cmd, "ssh -x -a -S $HOME/.ssh/cangw-connection root@192.168.2.3 'kill -CONT -%s'",
921 getenv("LATESTER_CONTROL_HACKBENCH"));
922 printf("Running: %s\n", cmd);
926 pthread_create(&thread, 0, measure_thread, NULL);
928 struct timespec next, now, diff, allsent = {0,0};
929 clock_gettime(CLOCK_MONOTONIC, &next);
931 while (!finish_flag && (opt.count == 0 || completed < opt.count)) {
932 struct pollfd pfd[1];
933 pfd[0].fd = completion_pipe[0];
934 pfd[0].events = POLLIN;
935 ret = poll(pfd, 1, 100);
936 if (ret == -1 && !INTERRUPTED_SYSCALL(errno))
937 error(1, errno, "poll main");
938 if (ret > 0 && (pfd[0].revents & POLLIN)) {
941 ret = read(completion_pipe[0], &mi, sizeof(mi));
942 if (ret < sizeof(mi))
943 error(1, errno, "read completion returned %d", ret);
944 msg_info_print(opt.f_msgs, mi);
949 clock_gettime(CLOCK_MONOTONIC, &now);
950 if (timespec_subtract(&diff, &next, &now)) {
952 next.tv_nsec += 100000000;
953 while (next.tv_nsec >= 1000000000) {
954 next.tv_nsec -= 1000000000;
958 if (opt.count != 0 && count >= opt.count) {
959 if (allsent.tv_sec == 0)
961 timespec_subtract(&diff, &now, &allsent);
962 if (diff.tv_sec >= 1)
970 stats.lost = msg_in_progress;
972 pthread_join(thread, NULL);
974 if (getenv("LATESTER_CONTROL_HACKBENCH")) {
976 sprintf(cmd, "ssh -x -a -S $HOME/.ssh/cangw-connection root@192.168.2.3 'kill -STOP -%s'",
977 getenv("LATESTER_CONTROL_HACKBENCH"));
978 printf("Running: %s\n", cmd);
982 close(completion_pipe[0]);
983 close(completion_pipe[1]);
985 histogram_fprint(&histogram, opt.f_hist);
986 histogram_fprint(&histogram_gw, opt.f_hist_gw);
990 fclose(opt.f_hist_gw);
995 fprintf(opt.f_stat, "cmdline='");
996 for (i=0; i<argc; i++)
997 fprintf(opt.f_stat, "%s%s", argv[i], i < argc-1 ? " " : "");
998 fprintf(opt.f_stat, "'\n");
1000 timespec_subtract(&diff, &stats.tac, &stats.tic);
1001 fprintf(opt.f_stat, "duration=%s # seconds\n", tstamp_str(NULL, &diff));
1003 fprintf(opt.f_stat, "sent=%d\n", count);
1004 fprintf(opt.f_stat, "overrun=%d\n", stats.overrun);
1005 if (stats.overrun && !opt.quiet)
1006 printf("overrun=%d\n", stats.overrun);
1007 fprintf(opt.f_stat, "enobufs=%d\n", stats.enobufs);
1008 if (stats.enobufs && !opt.quiet)
1009 printf("enobufs=%d\n", stats.enobufs);
1010 fprintf(opt.f_stat, "lost=%d\n", stats.lost);
1011 if (stats.lost && !opt.quiet)
1012 printf("lost=%d\n", stats.lost);
1013 fprintf(opt.f_stat, "timeouts=%d\n", stats.timeouts);
1014 if (stats.timeouts && !opt.quiet)
1015 printf("timeouts=%d\n", stats.timeouts);
1016 fprintf(opt.f_stat, "invalid_frame=%d\n", stats.timeouts);
1017 if (stats.timeouts && !opt.quiet)
1018 printf("invalid_frame=%d\n", stats.timeouts);