1 /**************************************************************************/
2 /* CAN latency tester */
3 /* Copyright (C) 2010 Michal Sojka, DCE FEE CTU Prague */
5 /**************************************************************************/
14 #include <semaphore.h>
21 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 //#include <ul_list.h>
28 #include <sys/socket.h>
29 #include <linux/can.h>
30 #include <linux/can/raw.h>
33 #include "histogram.h"
36 #define dbg(level, fmt, arg...) do {} while (0)
38 #define dbg(level, fmt, arg...) do { if (level <= DEBUG) { printf("candping: " fmt, ## arg); } } while (0)
41 #define INTERRUPTED_SYSCALL(errno) (errno == EINTR || errno == ERESTART)
43 #define MEMSET_ZERO(obj) memset(&(obj), 0, sizeof(obj))
45 /* Global variables */
46 volatile sig_atomic_t finish_flag = 0; /* Threads should terminate. */
47 sem_t finish_sem; /* Thread signals a termination */
49 #define MAX_INTERFACES 4
51 /* Command line options */
52 char *option_interface[MAX_INTERFACES];
53 int num_interfaces = 0;
55 unsigned option_period_us = 100000;
60 struct timespec ts_sent, ts_sent_kern;
61 struct timespec ts_rx_onwire, ts_rx_onwire_kern;
62 struct timespec ts_rx_final, ts_rx_final_kern;
65 #define MAX_INFOS 10000
66 struct msg_info msg_infos[MAX_INFOS];
67 uint16_t curr_msg = 0;
69 static inline struct msg_info *frame2info(struct can_frame *frame)
72 if (frame->can_dlc == 2) {
73 memcpy(&idx, frame->data, sizeof(idx));
75 error(1, 0, "%s idx too high", __FUNCTION__);
77 error(1, 0, "%s error", __FUNCTION__);
78 return &msg_infos[idx];
81 static inline char *tstamp_str(char *buf, struct timespec *tstamp)
83 sprintf(buf, "%ld.%06ld", tstamp->tv_sec, tstamp->tv_nsec/1000);
86 void print_msg_info(struct msg_info *mi)
88 struct timespec ts_diff1, ts_diff2;
89 char str_sent[32], str_kern[32], str_user[32], str_diff1[32], str_diff2[32];
90 timespec_subtract(&ts_diff1, &mi->ts_rx_final_kern, &mi->ts_sent);
91 timespec_subtract(&ts_diff2, &mi->ts_rx_final, &mi->ts_sent);
92 tstamp_str(str_sent, &mi->ts_sent);
93 tstamp_str(str_kern, &mi->ts_rx_final_kern);
94 tstamp_str(str_user, &mi->ts_rx_final);
95 tstamp_str(str_diff1, &ts_diff1);
96 tstamp_str(str_diff2, &ts_diff2);
97 printf("%s -> %s (%s) = %s (%s)\n", str_sent, str_user, str_kern, str_diff1, str_diff2);
101 /* Subtract the `struct timespec' values X and Y, storing the result in
102 RESULT. Return 1 if the difference is negative, otherwise 0. */
104 int timespec_subtract (struct timespec *result, struct timespec *x, struct timespec *yy)
106 struct timespec ylocal = *yy, *y = &ylocal;
107 /* Perform the carry for the later subtraction by updating Y. */
108 if (x->tv_nsec < y->tv_nsec) {
109 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
110 y->tv_nsec -= 1000000000 * nsec;
113 if (x->tv_nsec - y->tv_nsec > 1000000000) {
114 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
115 y->tv_nsec += 1000000000 * nsec;
119 /* Compute the time remaining to wait.
120 `tv_nsec' is certainly positive. */
121 result->tv_sec = x->tv_sec - y->tv_sec;
122 result->tv_nsec = x->tv_nsec - y->tv_nsec;
124 /* Return 1 if result is negative. */
125 return x->tv_sec < y->tv_sec;
128 void dbg_print_timespec(char *msg, struct timespec *tv)
131 printf("%s sec=%ld nsec=%ld\n", msg, tv->tv_sec, tv->tv_nsec);
134 void set_sched_policy_and_prio(int policy, int rtprio)
136 struct sched_param scheduling_parameters;
137 int maxprio=sched_get_priority_max(policy);
138 int minprio=sched_get_priority_min(policy);
140 if((rtprio < minprio) || (rtprio > maxprio))
141 error(1, 0, "The priority for requested policy is out of <%d, %d> range\n",
144 scheduling_parameters.sched_priority = rtprio;
146 if (0 != pthread_setschedparam(pthread_self(), policy, &scheduling_parameters))
147 error(1, errno, "pthread_setschedparam error");
150 void term_handler(int signum)
155 static inline int sock_get_if_index(int s, const char *if_name)
160 strcpy(ifr.ifr_name, if_name);
161 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
162 error(1, errno, "SIOCGIFINDEX");
163 return ifr.ifr_ifindex;
166 static inline get_tstamp(struct timespec *ts)
168 clock_gettime(CLOCK_REALTIME, ts);
171 int send_frame(int socket)
173 struct can_frame frame;
177 frame.can_id = option_id;
179 memcpy(frame.data, &curr_msg, sizeof(curr_msg));
180 mi = frame2info(&frame);
182 get_tstamp(&mi->ts_sent);
183 ret = write(socket, &frame, sizeof(frame));
187 static inline void get_next_timeout(struct timespec *timeout)
190 static struct timespec last = {-1, 0 };
192 clock_gettime(CLOCK_MONOTONIC, &now);
194 if (last.tv_sec == -1)
196 last.tv_sec += option_period_us/1000000;
197 last.tv_nsec += (option_period_us%1000000)*1000;
198 while (last.tv_nsec >= 1000000000) {
199 last.tv_nsec -= 1000000000;
202 timespec_subtract(timeout, &last, &now);
205 void receive(int s, struct can_frame *frame, struct timespec *ts_kern, struct timespec *ts_user)
207 char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
210 struct cmsghdr *cmsg;
211 struct sockaddr_can addr;
213 static uint64_t dropcnt = 0;
215 iov.iov_base = frame;
216 msg.msg_name = &addr;
219 msg.msg_control = &ctrlmsg;
221 /* these settings may be modified by recvmsg() */
222 iov.iov_len = sizeof(*frame);
223 msg.msg_namelen = sizeof(addr);
224 msg.msg_controllen = sizeof(ctrlmsg);
227 nbytes = recvmsg(s, &msg, 0);
229 error(1, errno, "recvmsg");
231 if (nbytes < sizeof(struct can_frame))
232 error(1, 0, "recvmsg: incomplete CAN frame\n");
235 MEMSET_ZERO(*ts_kern);
236 for (cmsg = CMSG_FIRSTHDR(&msg);
237 cmsg && (cmsg->cmsg_level == SOL_SOCKET);
238 cmsg = CMSG_NXTHDR(&msg,cmsg)) {
239 if (cmsg->cmsg_type == SO_TIMESTAMPNS)
240 *ts_kern = *(struct timespec *)CMSG_DATA(cmsg);
241 else if (cmsg->cmsg_type == SO_RXQ_OVFL)
242 dropcnt += *(__u32 *)CMSG_DATA(cmsg);
247 void process_tx(int s)
249 error(1, 0, "%s: not implemented", __FUNCTION__);
252 void process_on_wire_rx(int s)
254 error(1, 0, "%s: not implemented", __FUNCTION__);
258 void process_final_rx(int s)
260 struct timespec ts_kern, ts_user, ts_diff;
261 struct can_frame frame;
263 receive(s, &frame, &ts_kern, &ts_user);
264 mi = frame2info(&frame);
265 mi->ts_rx_final_kern = ts_kern;
266 mi->ts_rx_final = ts_user;
271 void *measure_thread(void *arg)
274 struct pollfd pfd[MAX_INTERFACES];
275 struct timespec timeout;
276 struct sockaddr_can addr;
281 for (i=0; i<num_interfaces; i++) {
282 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
283 error(1, errno, "socket");
285 addr.can_family = AF_CAN;
286 addr.can_ifindex = sock_get_if_index(s, option_interface[i]);
288 if (i == 0) { /* TX socket */
289 /* disable default receive filter on this RAW socket */
290 /* This is obsolete as we do not read from the socket at all, but for */
291 /* this reason we can remove the receive list in the Kernel to save a */
292 /* little (really a very little!) CPU usage. */
293 if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0) == -1)
294 error(1, errno, "SOL_CAN_RAW");
297 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
298 error(1, errno, "bind");
300 const int timestamp_on = 1;
301 if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMPNS,
302 ×tamp_on, sizeof(timestamp_on)) < 0)
303 error(1, errno, "setsockopt SO_TIMESTAMP");
305 // const int dropmonitor_on = 1;
306 // if (setsockopt(s, SOL_SOCKET, SO_RXQ_OVFL,
307 // &dropmonitor_on, sizeof(dropmonitor_on)) < 0)
308 // error(1, errno, "setsockopt SO_RXQ_OVFL not supported by your Linux Kernel");
311 pfd[i].events = (i == 0) ? POLLERR : POLLIN;
314 set_sched_policy_and_prio(SCHED_FIFO, 99);
316 while (!finish_flag) {
317 get_next_timeout(&timeout);
318 ret = ppoll(pfd, num_interfaces, &timeout, NULL);
321 if (!INTERRUPTED_SYSCALL(errno))
322 error(1, errno, "ppoll");
325 ret = send_frame(pfd[0].fd);
326 if (ret != sizeof(struct can_frame))
327 error(1, errno, "send_frame");
330 if (pfd[0].revents != 0) {
331 process_tx(pfd[0].fd);
333 if (pfd[1].revents != 0) {
334 switch (num_interfaces) {
335 case 2: process_final_rx(pfd[1].fd); break;
336 case 3: process_on_wire_rx(pfd[1].fd); break;
339 if (pfd[2].revents != 0) {
340 process_final_rx(pfd[2].fd);
345 for (i=0; i<num_interfaces; i++)
351 void print_help(void)
353 printf("Usage: latester -d <interface> -d <interface> ... [other options]\n"
355 " -d <interface> Interface to use. Must be given two times (tx, rx) or three times (tx, rx1, rx2).\n"
359 int parse_options(int argc, char *argv[])
364 while ((c = getopt (argc, argv, "d:h")) != -1)
368 if (num_interfaces < MAX_INTERFACES) {
369 option_interface[num_interfaces] = optarg;
372 error(1, 0, "error: Too many -d options");
379 if (isprint (optopt))
380 error (1, 0, "Unknown option `-%c'.\n", optopt);
382 error (1, 0, "Unknown option character `\\x%x'.\n", optopt);
384 error(1, 0, "Unhandled parameter");
386 if (num_interfaces < 2 || num_interfaces > 3)
387 error(1, 0, "-d option must be given exactly 2 or 3 times");
391 int main(int argc, char *argv[])
397 parse_options(argc, argv);
399 mlockall(MCL_CURRENT | MCL_FUTURE);
401 signal(SIGINT, term_handler);
402 signal(SIGTERM, term_handler);
405 pthread_create(&thread, 0, measure_thread, NULL);
407 while (!finish_flag) {
411 pthread_join(thread, NULL);