1 /*******************************************************************************/
2 /* CAN latency tester */
3 /* Copyright (C) 2010, 2011, 2012, 2013, 2014 Michal Sojka, DCE FEE CTU Prague */
5 /*******************************************************************************/
17 #include <semaphore.h>
25 #include <sys/ioctl.h>
27 #include <sys/socket.h>
30 #include <sys/types.h>
34 #include "canframelen.h"
35 #include <linux/can.h>
36 #include <linux/can/raw.h>
38 #include "histogram.h"
43 #define dbg(level, fmt, arg...) do {} while (0)
45 #define dbg(level, fmt, arg...) do { if (level <= DEBUG) { printf("candping: " fmt, ## arg); } } while (0)
48 #define INTERRUPTED_SYSCALL(errno) (errno == EINTR || errno == ERESTART)
50 #define MEMSET_ZERO(obj) memset(&(obj), 0, sizeof(obj))
52 /* Global variables */
53 volatile sig_atomic_t finish_flag = 0; /* Threads should terminate. */
54 sem_t finish_sem; /* Thread signals a termination */
56 /* Command line options */
69 /* Temporary variables */
76 struct options opt = {
87 struct timespec tic, tac;
89 unsigned invalid_frame;
92 int num_interfaces = 0;
93 int count = 0; /* Number of sent messages */
94 unsigned msg_in_progress = 0;
95 int completion_pipe[2];
100 struct timespec ts_sent, ts_sent_kern;
101 struct timespec ts_rx_onwire, ts_rx_onwire_kern;
102 struct timespec ts_rx_final, ts_rx_final_kern;
103 struct can_frame sent, received;
106 #define MAX_INFOS 10000
107 struct msg_info msg_infos[MAX_INFOS];
109 struct histogram histogram, histogram_gw;
111 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
112 /* documentation see lib.h */
115 int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
117 if (cf->can_id & CAN_ERR_FLAG) {
118 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
120 } else if (cf->can_id & CAN_EFF_FLAG) {
121 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
124 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
128 if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
129 sprintf(buf+offset, "R");
131 for (i = 0; i < dlc; i++) {
132 sprintf(buf+offset, "%02X", cf->data[i]);
134 if (sep && (i+1 < dlc))
135 sprintf(buf+offset++, ".");
139 static inline uint16_t frame_index(struct can_frame *frame)
142 if (frame->can_dlc >= 2) {
143 memcpy(&idx, frame->data, sizeof(idx));
144 if (idx >= MAX_INFOS)
145 error(1, 0, "%s idx too high", __FUNCTION__);
148 error(1, 0, "%s error", __FUNCTION__);
153 static inline struct msg_info *frame2info(struct can_frame *frame)
155 return &msg_infos[frame_index(frame)];
158 static inline char *tstamp_str(const void *ctx, struct timespec *tstamp)
160 return talloc_asprintf(ctx, "%ld.%06ld",
161 tstamp->tv_sec, tstamp->tv_nsec/1000);
164 int timespec_subtract (struct timespec *result, const struct timespec *x, const struct timespec *yy);
166 static inline unsigned get_timestamp_diff_us(struct msg_info *mi);
168 void msg_info_process(FILE *f, struct msg_info *mi)
170 struct timespec diff;
171 void *local = talloc_new (NULL);
173 char sent[64], received[64];
174 int diff_us = get_timestamp_diff_us(mi);
175 int tx_time_us = calc_frame_length(&mi->received);
177 /* Update histograms */
178 histogram_add(&histogram, diff_us);
179 histogram_add(&histogram_gw, diff_us - tx_time_us);
184 /* Print information to a file */
185 sprint_canframe(sent, &mi->sent, true);
186 sprint_canframe(received, &mi->received, true);
188 #define S(ts) tstamp_str(local, &ts)
189 #define DIFF(a, b) (timespec_subtract(&diff, &b, &a), S(diff))
191 switch (num_interfaces) {
193 fprintf(f, "%ld: %s %s -> %s (%s) %s = %s (%s) %d\n",
194 num, S(mi->ts_sent), sent, S(mi->ts_rx_final_kern), S(mi->ts_rx_final), received,
195 DIFF(mi->ts_sent, mi->ts_rx_final_kern),
196 DIFF(mi->ts_sent, mi->ts_rx_final),
200 fprintf(f, "%ld: %s %s -> %s (%s) -> %s (%s) %s = %s (%s), %s (%s) %d %.6f\n",
201 num, S(mi->ts_sent), sent,
202 S(mi->ts_rx_onwire_kern), S(mi->ts_rx_onwire),
203 S(mi->ts_rx_final_kern), S(mi->ts_rx_final), received,
204 DIFF(mi->ts_sent, mi->ts_rx_onwire_kern),
205 DIFF(mi->ts_sent, mi->ts_rx_onwire),
206 DIFF(mi->ts_rx_onwire_kern, mi->ts_rx_final_kern),
207 DIFF(mi->ts_rx_onwire, mi->ts_rx_final),
209 1e-6*(diff_us - tx_time_us));
218 /* Subtract the `struct timespec' values X and Y, storing the result in
219 RESULT. Return 1 if the difference is negative, otherwise 0. */
221 int timespec_subtract (struct timespec *result, const struct timespec *x, const struct timespec *yy)
223 struct timespec ylocal = *yy, *y = &ylocal;
224 /* Perform the carry for the later subtraction by updating Y. */
225 if (x->tv_nsec < y->tv_nsec) {
226 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
227 y->tv_nsec -= 1000000000 * nsec;
230 if (x->tv_nsec - y->tv_nsec > 1000000000) {
231 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
232 y->tv_nsec += 1000000000 * nsec;
236 /* Compute the time remaining to wait.
237 `tv_nsec' is certainly positive. */
238 result->tv_sec = x->tv_sec - y->tv_sec;
239 result->tv_nsec = x->tv_nsec - y->tv_nsec;
241 /* Return 1 if result is negative. */
242 return x->tv_sec < y->tv_sec;
245 void dbg_print_timespec(char *msg, struct timespec *tv)
248 printf("%s sec=%ld nsec=%ld\n", msg, tv->tv_sec, tv->tv_nsec);
251 static inline unsigned get_timestamp_diff_us(struct msg_info *mi)
253 struct timespec diff;
254 switch (num_interfaces) {
257 timespec_subtract(&diff, &mi->ts_rx_final, &mi->ts_rx_onwire);
259 timespec_subtract(&diff, &mi->ts_rx_final_kern, &mi->ts_rx_onwire_kern);
263 timespec_subtract(&diff, &mi->ts_rx_final, &mi->ts_sent);
265 timespec_subtract(&diff, &mi->ts_rx_final_kern, &mi->ts_sent);
270 unsigned us = diff.tv_sec * 1000000 + diff.tv_nsec/1000;
274 void set_sched_policy_and_prio(int policy, int rtprio)
276 struct sched_param scheduling_parameters;
277 int maxprio=sched_get_priority_max(policy);
278 int minprio=sched_get_priority_min(policy);
280 if((rtprio < minprio) || (rtprio > maxprio))
281 error(1, 0, "The priority for requested policy is out of <%d, %d> range\n",
284 scheduling_parameters.sched_priority = rtprio;
286 if (0 != pthread_setschedparam(pthread_self(), policy, &scheduling_parameters))
287 error(1, errno, "pthread_setschedparam error");
290 void term_handler(int signum)
295 static inline int sock_get_if_index(int s, const char *if_name)
300 strcpy(ifr.ifr_name, if_name);
301 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
302 error(1, errno, "SIOCGIFINDEX '%s'", if_name);
303 return ifr.ifr_ifindex;
306 static inline void get_tstamp(struct timespec *ts)
308 clock_gettime(CLOCK_REALTIME, ts);
322 debugfs = "/sys/kernel/debug";
324 strcpy(path, debugfs);
325 strcat(path,"/tracing/tracing_on");
326 trace_fd = open(path, O_WRONLY);
328 write(trace_fd, "1", 1);
330 strcpy(path, debugfs);
331 strcat(path,"/tracing/trace_marker");
332 marker_fd = open(path, O_WRONLY);
334 strcpy(path, debugfs);
335 strcat(path,"/tracing/set_ftrace_pid");
336 f = fopen(path, "w");
337 fprintf(f, "%d\n", getpid());
339 system("echo function_graph > /sys/kernel/debug/tracing/current_tracer");
340 system("echo can_send > /sys/kernel/debug/tracing/set_graph_function");
341 system("echo > /sys/kernel/debug/tracing/trace");
342 system("echo 1 > /sys/kernel/debug/tracing/tracing_enabled");
347 static inline void trace_on()
350 write(trace_fd, "1", 1);
353 static inline void trace_off(int ret)
355 if (marker_fd >= 0) {
357 sprintf(marker, "write returned %d\n", ret);
358 write(marker_fd, marker, strlen(marker));
361 write(trace_fd, "0", 1);
364 static inline void msg_info_free(struct msg_info *mi)
369 static inline bool msg_info_used(struct msg_info *mi)
374 int send_frame(int socket)
376 struct can_frame frame;
379 static int curr_msg = -1;
385 while (msg_info_used(&msg_infos[i]) && i != curr_msg) {
391 error(1, 0, "Msg info table is full! Probably, many packets were lost.");
395 frame.can_id = opt.id;
397 error(1, 0, "Length < 2 is not yet supported");
398 frame.can_dlc = opt.length;
400 memcpy(frame.data, &idx, sizeof(idx));
401 mi = frame2info(&frame);
403 mi->id = frame.can_id;
404 mi->length = frame.can_dlc;
405 get_tstamp(&mi->ts_sent);
409 ret = write(socket, &frame, sizeof(frame));
412 if (ret == -1 || num_interfaces == 1)
417 static inline void send_and_check(int s)
421 if (ret != sizeof(struct can_frame)) {
422 /* if (ret == -1 && errno == ENOBUFS && opt.period_us == 0 && !opt.oneattime) { */
423 /* stats.enobufs++; */
424 /* /\* Ignore this error - pfifo_fast qeuue is full *\/ */
426 error(1, errno, "send_frame (line %d)", __LINE__);
433 static inline void get_next_timeout(struct timespec *timeout, bool advance)
436 static struct timespec last = {-1, 0 };
438 clock_gettime(CLOCK_MONOTONIC, &now);
440 if (last.tv_sec == -1) {
442 last.tv_nsec = last.tv_nsec/1000000*1000000;
444 if (opt.period_us != 0) {
446 last.tv_sec += opt.period_us/1000000;
447 last.tv_nsec += (opt.period_us%1000000)*1000;
448 while (last.tv_nsec >= 1000000000) {
449 last.tv_nsec -= 1000000000;
453 if (timespec_subtract(timeout, &last, &now) /* is negative */) {
455 memset(timeout, 0, sizeof(*timeout));
457 /* printf("next %ld.%06ld now %ld.%06ld --> timeout %ld.%06ld\n", */
458 /* last.tv_sec, last.tv_nsec/1000, */
459 /* now.tv_sec, now.tv_nsec/1000, */
460 /* timeout->tv_sec, timeout->tv_nsec/1000); */
461 } else if (opt.timeout_ms != 0) {
462 timeout->tv_sec = opt.timeout_ms/1000;
463 timeout->tv_nsec = (opt.timeout_ms%1000)*1000000;
465 error(1, 0, "Timeout and period cannot be both zero");
468 void receive(int s, struct can_frame *frame, struct timespec *ts_kern, struct timespec *ts_user)
470 char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
473 struct cmsghdr *cmsg;
474 struct sockaddr_can addr;
476 static uint64_t dropcnt = 0;
478 iov.iov_base = frame;
479 msg.msg_name = &addr;
482 msg.msg_control = &ctrlmsg;
484 /* these settings may be modified by recvmsg() */
485 iov.iov_len = sizeof(*frame);
486 msg.msg_namelen = sizeof(addr);
487 msg.msg_controllen = sizeof(ctrlmsg);
490 nbytes = recvmsg(s, &msg, 0);
492 error(1, errno, "recvmsg");
494 if (nbytes < sizeof(struct can_frame))
495 error(1, 0, "recvmsg: incomplete CAN frame\n");
498 MEMSET_ZERO(*ts_kern);
499 for (cmsg = CMSG_FIRSTHDR(&msg);
500 cmsg && (cmsg->cmsg_level == SOL_SOCKET);
501 cmsg = CMSG_NXTHDR(&msg,cmsg)) {
502 if (cmsg->cmsg_type == SO_TIMESTAMPNS)
503 memcpy(ts_kern, CMSG_DATA(cmsg), sizeof(struct timespec));
504 else if (cmsg->cmsg_type == SO_RXQ_OVFL) {
506 memcpy(&ovfl, CMSG_DATA(cmsg), sizeof(ovfl));
513 void process_tx(int s)
515 error(1, 0, "%s: not implemented", __FUNCTION__);
518 void process_on_wire_rx(int s)
520 struct timespec ts_kern, ts_user;
521 struct can_frame frame;
523 receive(s, &frame, &ts_kern, &ts_user);
524 mi = frame2info(&frame);
525 if (msg_info_used(mi)) {
526 mi->ts_rx_onwire_kern = ts_kern;
527 mi->ts_rx_onwire = ts_user;
529 stats.invalid_frame++;
533 void process_final_rx(int s)
535 struct timespec ts_kern, ts_user;
536 struct can_frame frame;
540 receive(s, &frame, &ts_kern, &ts_user);
541 mi = frame2info(&frame);
542 mi->ts_rx_final_kern = ts_kern;
543 mi->ts_rx_final = ts_user;
544 mi->received = frame;
546 ret = write(completion_pipe[1], &mi, sizeof(mi));
548 error(1, errno, "completion_pipe write");
551 void *measure_thread(void *arg)
554 struct pollfd pfd[3];
555 struct timespec timeout;
556 struct sockaddr_can addr;
557 int consecutive_timeouts = 0;
561 for (i=0; i<num_interfaces; i++) {
562 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
563 error(1, errno, "socket");
565 addr.can_family = AF_CAN;
566 addr.can_ifindex = sock_get_if_index(s, opt.interface[i]);
568 if (i == 0) { /* TX socket */
569 /* disable default receive filter on this RAW socket */
570 /* This is obsolete as we do not read from the socket at all, but for */
571 /* this reason we can remove the receive list in the Kernel to save a */
572 /* little (really a very little!) CPU usage. */
573 if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0) == -1)
574 error(1, errno, "SOL_CAN_RAW");
577 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
578 error(1, errno, "bind");
580 const int timestamp_on = 1;
581 if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMPNS,
582 ×tamp_on, sizeof(timestamp_on)) < 0)
583 error(1, errno, "setsockopt SO_TIMESTAMP");
585 const int dropmonitor_on = 1;
586 if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL,
587 &dropmonitor_on, sizeof(dropmonitor_on)) < 0)
588 error(1, errno, "setsockopt SO_RXQ_OVFL not supported by your Linux Kernel");
592 pfd[i].events = POLLIN | POLLERR | ((opt.period_us == 0 && !opt.oneattime) ? POLLOUT : 0);
594 pfd[i].events = POLLIN;
597 set_sched_policy_and_prio(SCHED_FIFO, 40);
599 #define SEND() send_and_check(pfd[0].fd)
604 get_tstamp(&stats.tic);
606 while (!finish_flag &&
607 (opt.count == 0 || count < opt.count || msg_in_progress != 0)) {
609 get_next_timeout(&timeout, ret == 0);
610 //printf("timeout %ld.%06ld\n", timeout.tv_sec, timeout.tv_nsec/1000);
611 //printf("ppoll"); fflush(stdout);
612 ret = ppoll(pfd, num_interfaces, &timeout, NULL);
613 //printf("=%d\n", ret);
616 if (!INTERRUPTED_SYSCALL(errno))
617 error(1, errno, "ppoll");
621 if (opt.count == 0 || count < opt.count) {
625 /* Lost message - send a new one */
627 consecutive_timeouts++;
628 if (consecutive_timeouts < 10)
630 else /* Something is really broken */
635 if (pfd[0].revents & (POLLIN|POLLERR)) {
636 process_tx(pfd[0].fd);
638 if (pfd[0].revents & POLLOUT) {
639 if (opt.count == 0 || count < opt.count)
644 if (num_interfaces == 3 && pfd[1].revents & POLLIN) {
645 process_on_wire_rx(pfd[1].fd);
648 if (num_interfaces == 3 && pfd[1].revents & ~POLLIN)
649 error(1, 0, "Unexpected pfd[1].revents: 0x%04x\n", pfd[1].revents);
651 i = (num_interfaces == 2) ? 1 : 2;
652 if (pfd[i].revents & POLLIN) {
653 consecutive_timeouts = 0;
654 process_final_rx(pfd[i].fd);
657 if ((opt.count == 0 || count < opt.count) &&
662 if (pfd[i].revents & ~POLLIN)
663 error(1, 0, "Unexpected pfd[%d].revents: 0x%04x\n", i, pfd[i].revents);
667 get_tstamp(&stats.tac);
669 for (i=0; i<num_interfaces; i++)
675 struct poptOption optionsTable[] = {
676 { "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" },
677 { "count", 'c', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.count, 0, "The count of messages to send, zero corresponds to infinity", "num"},
678 { "id", 'i', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.id, 0, "CAN ID of sent messages", "id"},
679 { "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"},
680 { "timeout",'t', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.timeout_ms,0, "Timeout when period is zero", "ms"},
681 { "oneattime",'o', POPT_ARG_NONE, &opt.oneattime,0, "Send the next message only when the previous was finally received"},
682 { "verbose",'v', POPT_ARG_NONE, NULL, 'v', "Send the next message only when the previous was finally received"},
683 { "name", 'n', POPT_ARG_STRING, &opt.name, 0, "Prefix of the generated files"},
684 { "length", 'l', POPT_ARG_INT|POPT_ARGFLAG_SHOW_DEFAULT, &opt.length, 0, "The length of generated messages", "bytes"},
685 { "userhist", 'u', POPT_ARG_NONE, &opt.userhist, 0, "Generate histogram from userspace timestamps"},
686 { "quiet", 'q', POPT_ARG_NONE, &opt.quiet, 0, "Do not print progress and statistics"},
688 { NULL, 0, 0, NULL, 0 }
691 int parse_options(int argc, const char *argv[])
694 poptContext optCon; /* context for parsing command-line options */
695 void *local = talloc_new (NULL);
697 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
698 //poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>");
700 /* Now do options processing */
701 while ((c = poptGetNextOpt(optCon)) >= 0) {
709 error(1, 0, "%s: %s\n",
710 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
713 if (num_interfaces < 1 || num_interfaces > 3)
714 error(1, 0, "-d option must only be given one, two or three times");
716 if (opt.oneattime && opt.period_us)
717 error(1, 0, "oneattime and period cannot be specified at the same time");
720 char *f = talloc_asprintf(local, "%s-msgs.txt", opt.name);
721 opt.f_msgs = fopen(f, "w");
723 error(1, errno, "fopen: %s", f);
727 char *f = talloc_asprintf(local, "%s-hist-raw.txt", opt.name);
728 opt.f_hist = fopen(f, "w");
730 error(1, errno, "fopen: %s", f);
734 char *f = talloc_asprintf(local, "%s-hist.txt", opt.name);
735 opt.f_hist_gw = fopen(f, "w");
737 error(1, errno, "fopen: %s", f);
741 char *f = talloc_asprintf(local, "%s-stat.txt", opt.name);
742 opt.f_stat = fopen(f, "w");
744 error(1, errno, "fopen: %s", f);
747 poptFreeContext(optCon);
752 void print_progress()
755 if (num_interfaces > 1)
756 printf("\rSent %5d, in progress %5d", count, msg_in_progress);
758 printf("\rSent %5d", count);
763 int main(int argc, const char *argv[])
768 parse_options(argc, argv);
770 mlockall(MCL_CURRENT | MCL_FUTURE);
772 signal(SIGINT, term_handler);
773 signal(SIGTERM, term_handler);
775 for (i=0; i<MAX_INFOS; i++)
776 msg_infos[i].id = -1;
778 histogram_init(&histogram, 5000000, 1);
779 histogram_init(&histogram_gw, 5000000, 1);
781 ret = pipe(completion_pipe);
783 error(1, errno, "pipe");
784 ret = fcntl(completion_pipe[1], F_SETFL, O_NONBLOCK);
786 error(1, errno, "pipe fcntl");
789 if (getenv("LATESTER_CONTROL_HACKBENCH")) {
791 sprintf(cmd, "ssh -x -a -S $HOME/.ssh/cangw-connection root@192.168.2.3 'kill -CONT -%s'",
792 getenv("LATESTER_CONTROL_HACKBENCH"));
793 printf("Running: %s\n", cmd);
797 pthread_create(&thread, 0, measure_thread, NULL);
799 struct timespec next, now, diff, allsent = {0,0};
800 clock_gettime(CLOCK_MONOTONIC, &next);
802 while (!finish_flag && (opt.count == 0 || completed < opt.count)) {
803 struct pollfd pfd[1];
804 pfd[0].fd = completion_pipe[0];
805 pfd[0].events = POLLIN;
806 ret = poll(pfd, 1, 100);
807 if (ret == -1 && !INTERRUPTED_SYSCALL(errno))
808 error(1, errno, "poll main");
809 if (ret > 0 && (pfd[0].revents & POLLIN)) {
812 ret = read(completion_pipe[0], &mi, sizeof(mi));
813 if (ret < sizeof(mi))
814 error(1, errno, "read completion returned %d", ret);
816 msg_info_process(opt.f_msgs, mi);
821 clock_gettime(CLOCK_MONOTONIC, &now);
822 if (timespec_subtract(&diff, &next, &now)) {
824 next.tv_nsec += 100000000;
825 while (next.tv_nsec >= 1000000000) {
826 next.tv_nsec -= 1000000000;
830 if (opt.count != 0 && count >= opt.count) {
831 if (allsent.tv_sec == 0)
833 timespec_subtract(&diff, &now, &allsent);
834 if (diff.tv_sec >= 1)
842 stats.lost = msg_in_progress;
844 pthread_join(thread, NULL);
846 if (getenv("LATESTER_CONTROL_HACKBENCH")) {
848 sprintf(cmd, "ssh -x -a -S $HOME/.ssh/cangw-connection root@192.168.2.3 'kill -STOP -%s'",
849 getenv("LATESTER_CONTROL_HACKBENCH"));
850 printf("Running: %s\n", cmd);
854 close(completion_pipe[0]);
855 close(completion_pipe[1]);
857 histogram_fprint(&histogram, opt.f_hist);
858 histogram_fprint(&histogram_gw, opt.f_hist_gw);
862 fclose(opt.f_hist_gw);
867 struct histogram_stats hs;
868 fprintf(opt.f_stat, "cmdline='");
869 for (i=0; i<argc; i++)
870 fprintf(opt.f_stat, "%s%s", argv[i], i < argc-1 ? " " : "");
871 fprintf(opt.f_stat, "'\n");
873 timespec_subtract(&diff, &stats.tac, &stats.tic);
874 fprintf(opt.f_stat, "duration=%s # seconds\n", tstamp_str(NULL, &diff));
876 fprintf(opt.f_stat, "sent=%d\n", count);
877 fprintf(opt.f_stat, "overrun=%d\n", stats.overrun);
878 if (stats.overrun && !opt.quiet)
879 printf("overrun=%d\n", stats.overrun);
880 fprintf(opt.f_stat, "enobufs=%d\n", stats.enobufs);
881 if (stats.enobufs && !opt.quiet)
882 printf("enobufs=%d\n", stats.enobufs);
883 fprintf(opt.f_stat, "lost=%d\n", stats.lost);
884 if (stats.lost && !opt.quiet)
885 printf("lost=%d\n", stats.lost);
886 fprintf(opt.f_stat, "timeouts=%d\n", stats.timeouts);
887 if (stats.timeouts && !opt.quiet)
888 printf("timeouts=%d\n", stats.timeouts);
889 fprintf(opt.f_stat, "invalid_frame=%d\n", stats.timeouts);
890 if (stats.timeouts && !opt.quiet)
891 printf("invalid_frame=%d\n", stats.timeouts);
893 histogram_stats(&histogram_gw, &hs);
894 double avg = hs.count ? (double)hs.sum/hs.count : 0;
895 fprintf(opt.f_stat, "avg=%g\n", avg);
896 fprintf(opt.f_stat, "out_of_range_below=%d\n", hs.below);
897 fprintf(opt.f_stat, "out_of_range_above=%d\n", hs.above);
898 for (i = 0; i <= 20; i++)
899 fprintf(opt.f_stat, "percentile%d=%d\n", i*5, hs.percentile[i*5]);