]> rtime.felk.cvut.cz Git - can-benchmark.git/blobdiff - latester/latester.c
Merge branch 'master' of rtime.felk.cvut.cz:/can-benchmark
[can-benchmark.git] / latester / latester.c
index a6873b938b50ffccdb64fceffb882c10db423f12..61b2b433f45afe5c20bba5bc5830ff82a99ca83a 100644 (file)
@@ -82,6 +82,8 @@ struct {
        unsigned overrun;
        unsigned lost;
        struct timespec tic, tac;
        unsigned overrun;
        unsigned lost;
        struct timespec tic, tac;
+       unsigned timeouts;
+       unsigned invalid_frame;
 } stats;
 
 int num_interfaces = 0;
 } stats;
 
 int num_interfaces = 0;
@@ -131,7 +133,7 @@ void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
                }
 }
 
                }
 }
 
-static inline struct msg_info *frame2info(struct can_frame *frame)
+static inline uint16_t frame_index(struct can_frame *frame)
 {
        uint16_t idx;
        if (frame->can_dlc >= 2) {
 {
        uint16_t idx;
        if (frame->can_dlc >= 2) {
@@ -142,7 +144,12 @@ static inline struct msg_info *frame2info(struct can_frame *frame)
 
                error(1, 0, "%s error", __FUNCTION__);
        }
 
                error(1, 0, "%s error", __FUNCTION__);
        }
-       return &msg_infos[idx];
+       return idx;
+}
+
+static inline struct msg_info *frame2info(struct can_frame *frame)
+{
+       return &msg_infos[frame_index(frame)];
 }
 
 static inline char *tstamp_str(const void *ctx, struct timespec *tstamp)
 }
 
 static inline char *tstamp_str(const void *ctx, struct timespec *tstamp)
@@ -333,11 +340,16 @@ static inline void trace_off(int ret)
                write(trace_fd, "0", 1);
 }
 
                write(trace_fd, "0", 1);
 }
 
-void msg_info_free(struct msg_info *mi)
+static inline void msg_info_free(struct msg_info *mi)
 {
        mi->id = -1;
 }
 
 {
        mi->id = -1;
 }
 
+static inline bool msg_info_used(struct msg_info *mi)
+{
+       return mi->id != -1;
+}
+
 int send_frame(int socket)
 {
        struct can_frame frame;
 int send_frame(int socket)
 {
        struct can_frame frame;
@@ -349,7 +361,7 @@ int send_frame(int socket)
 
        MEMSET_ZERO(frame);
        i = curr_msg+1;
 
        MEMSET_ZERO(frame);
        i = curr_msg+1;
-       while (msg_infos[i].id != -1 && i != curr_msg) {
+       while (msg_info_used(&msg_infos[i]) && i != curr_msg) {
                i++;
                if (i >= MAX_INFOS)
                        i = 0;
                i++;
                if (i >= MAX_INFOS)
                        i = 0;
@@ -478,8 +490,11 @@ void process_on_wire_rx(int s)
        struct msg_info *mi;
        receive(s, &frame, &ts_kern, &ts_user);
        mi = frame2info(&frame);
        struct msg_info *mi;
        receive(s, &frame, &ts_kern, &ts_user);
        mi = frame2info(&frame);
-       mi->ts_rx_onwire_kern = ts_kern;
-       mi->ts_rx_onwire = ts_user;
+       if (msg_info_used(mi)) {
+               mi->ts_rx_onwire_kern = ts_kern;
+               mi->ts_rx_onwire = ts_user;
+       } else
+               stats.invalid_frame++;
 }
 
 
 }
 
 
@@ -510,6 +525,7 @@ void *measure_thread(void *arg)
        struct timespec timeout;
        struct sockaddr_can addr;
        sigset_t set;
        struct timespec timeout;
        struct sockaddr_can addr;
        sigset_t set;
+       int consecutive_timeouts = 0;
 
        MEMSET_ZERO(pfd);
 
 
        MEMSET_ZERO(pfd);
 
@@ -576,7 +592,13 @@ void *measure_thread(void *arg)
                                        SEND();
                                }
                        } else {
                                        SEND();
                                }
                        } else {
-                               error(1, 0, "poll timeout");
+                               /* Lost message - send a new one */
+                               stats.timeouts++;
+                               consecutive_timeouts++;
+                               if (consecutive_timeouts < 10)
+                                       SEND();
+                               else /* Something is really broken */
+                                       finish_flag = 1;
                        }
                        break;
                default: // Event
                        }
                        break;
                default: // Event
@@ -589,13 +611,16 @@ void *measure_thread(void *arg)
                        }
                        pfd[0].revents = 0;
 
                        }
                        pfd[0].revents = 0;
 
-                       if (num_interfaces == 3 && pfd[1].revents != 0) {
+                       if (num_interfaces == 3 && pfd[1].revents & POLLIN) {
                                process_on_wire_rx(pfd[1].fd);
                                pfd[1].revents = 0;
                        }
                                process_on_wire_rx(pfd[1].fd);
                                pfd[1].revents = 0;
                        }
+                       if (num_interfaces == 3 && pfd[1].revents & ~POLLIN)
+                               error(1, 0, "Unexpected pfd[1].revents: 0x%04x\n", pfd[1].revents);
 
                        i = (num_interfaces == 2) ? 1 : 2;
 
                        i = (num_interfaces == 2) ? 1 : 2;
-                       if (pfd[i].revents != 0) {
+                       if (pfd[i].revents & POLLIN) {
+                               consecutive_timeouts = 0;
                                process_final_rx(pfd[i].fd);
                                msg_in_progress--;
                                pfd[i].revents = 0;
                                process_final_rx(pfd[i].fd);
                                msg_in_progress--;
                                pfd[i].revents = 0;
@@ -604,6 +629,8 @@ void *measure_thread(void *arg)
                                        SEND();
                                }
                        }
                                        SEND();
                                }
                        }
+                       if (pfd[i].revents & ~POLLIN)
+                               error(1, 0, "Unexpected pfd[%d].revents: 0x%04x\n", pfd[i].revents);
                }
        }
 
                }
        }
 
@@ -797,6 +824,12 @@ int main(int argc, const char *argv[])
        fprintf(opt.f_stat, "lost=%d\n", stats.lost);
        if (stats.lost && !opt.quiet)
                printf("lost=%d\n", stats.lost);
        fprintf(opt.f_stat, "lost=%d\n", stats.lost);
        if (stats.lost && !opt.quiet)
                printf("lost=%d\n", stats.lost);
+       fprintf(opt.f_stat, "timeouts=%d\n", stats.timeouts);
+       if (stats.timeouts && !opt.quiet)
+               printf("timeouts=%d\n", stats.timeouts);
+       fprintf(opt.f_stat, "invalid_frame=%d\n", stats.timeouts);
+       if (stats.timeouts && !opt.quiet)
+               printf("invalid_frame=%d\n", stats.timeouts);
 
        fclose(opt.f_stat);
 
 
        fclose(opt.f_stat);