X-Git-Url: https://rtime.felk.cvut.cz/gitweb/sojka/can-utils.git/blobdiff_plain/5655d53acb2d7427d5d80a29c49db47fcbac76a3..HEAD:/canbusload.c diff --git a/canbusload.c b/canbusload.c index 36f3423..a5fb553 100644 --- a/canbusload.c +++ b/canbusload.c @@ -1,7 +1,3 @@ -/* - * $Id$ - */ - /* * canbusload.c * @@ -41,7 +37,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * - * Send feedback to + * Send feedback to * */ @@ -88,6 +84,7 @@ static unsigned char redraw; static unsigned char timestamp; static unsigned char color; static unsigned char bargraph; +static unsigned char ignore_bitstuffing; static char *prg; void print_usage(char *prg) @@ -98,20 +95,22 @@ void print_usage(char *prg) fprintf(stderr, " -c (colorize lines)\n"); fprintf(stderr, " -b (show bargraph in %d%% resolution)\n", PERCENTRES); fprintf(stderr, " -r (redraw the terminal - similar to top)\n"); + fprintf(stderr, " -i (ignore bitstuffing estimation in bandwith calculation)\n"); fprintf(stderr, "\n"); fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXSOCK); fprintf(stderr, "commandline in the form: @\n\n"); fprintf(stderr, "The bitrate is mandatory as it is needed to know the CAN bus bitrate to\n"); fprintf(stderr, "calcultate the bus load percentage based on the received CAN frames.\n"); + fprintf(stderr, "Due to the bitstuffing estimation the calculated busload may exceed 100%%.\n"); fprintf(stderr, "For each given interface the data is presented in one line which contains:\n\n"); fprintf(stderr, "(interface) (received CAN frames) (used bits total) (used bits for payload)\n"); fprintf(stderr, "\nExample:\n"); fprintf(stderr, "\nuser$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c\n\n"); fprintf(stderr, "%s 2008-05-27 15:18:49\n", prg); - fprintf(stderr, " can0@100000 805 74491 36656 74%% |XXXXXXXXXXXXXX......|\n"); - fprintf(stderr, " can1@500000 796 75140 37728 15%% |XXX.................|\n"); - fprintf(stderr, " can2@500000 0 0 0 0%% |....................|\n"); - fprintf(stderr, " can3@500000 47 4633 2424 0%% |....................|\n"); + fprintf(stderr, " can0@100000 805 74491 36656 74%% |XXXXXXXXXXXXXX......|\n"); + fprintf(stderr, " can1@500000 796 75140 37728 15%% |XXX.................|\n"); + fprintf(stderr, " can2@500000 0 0 0 0%% |....................|\n"); + fprintf(stderr, " can3@500000 47 4633 2424 0%% |....................|\n"); fprintf(stderr, "\n"); } @@ -162,7 +161,7 @@ void printstats(int signo) else percent = 0; - printf(" %*s@%-*d %4d %6d %6d %3d%%", + printf(" %*s@%-*d %5d %7d %6d %3d%%", max_devname_len, stat[i].devname, max_bitrate_len, stat[i].bitrate, stat[i].recv_frames, @@ -172,7 +171,10 @@ void printstats(int signo) if (bargraph) { - printf(" |"); + printf(" |"); + + if (percent > 100) + percent = 100; for (j=0; j < NUMBAR; j++){ if (j < percent/PERCENTRES) @@ -195,6 +197,7 @@ void printstats(int signo) } printf("\n"); + fflush(stdout); alarm(1); } @@ -220,7 +223,7 @@ int main(int argc, char **argv) prg = basename(argv[0]); - while ((opt = getopt(argc, argv, "rtbch?")) != -1) { + while ((opt = getopt(argc, argv, "rtbcih?")) != -1) { switch (opt) { case 'r': redraw = 1; @@ -238,6 +241,10 @@ int main(int argc, char **argv) color = 1; break; + case 'i': + ignore_bitstuffing = 1; + break; + default: print_usage(prg); exit(1); @@ -365,11 +372,32 @@ int main(int argc, char **argv) stat[i].recv_frames++; stat[i].recv_bits_payload += frame.can_dlc*8; - stat[i].recv_bits_total += frame.can_dlc*8; - if (frame.can_id & CAN_EFF_FLAG) - stat[i].recv_bits_total += 67; - else - stat[i].recv_bits_total += 47; + + /* + * Following Ken Tindells *worst* case calculation for stuff-bits + * (see "Guaranteeing Message Latencies on Controller Area Network" 1st ICC'94) + * the needed bits on the wire can be calculated as: + * + * (34 + 8n)/5 + 47 + 8n for SFF frames (11 bit CAN-ID) => (269 + 48n)/5 + * (54 + 8n)/5 + 67 + 8n for EFF frames (29 bit CAN-ID) => (389 + 48n)/5 + * + * while 'n' is the data length code (number of payload bytes) + * + */ + + if (ignore_bitstuffing) { + /* calculation without bitstuffing */ + if (frame.can_id & CAN_EFF_FLAG) + stat[i].recv_bits_total += 67 + frame.can_dlc*8; + else + stat[i].recv_bits_total += 47 + frame.can_dlc*8; + } else { + /* needed bits including estimated worst case stuff bits */ + if (frame.can_id & CAN_EFF_FLAG) + stat[i].recv_bits_total += (389 + frame.can_dlc*48)/5; + else + stat[i].recv_bits_total += (269 + frame.can_dlc*48)/5; + } } } }