4 * Copyright (c) 2002-2008 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
60 #include <linux/can.h>
61 #include <linux/can/raw.h>
64 #include "canframelen.h"
66 #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
68 #define PERCENTRES 5 /* resolution in percent for bargraph */
69 #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
71 extern int optind, opterr, optopt;
74 char devname[IFNAMSIZ+1];
76 unsigned int recv_frames;
77 unsigned int recv_bits_total;
78 unsigned int recv_bits_payload;
81 static int max_devname_len; /* to prevent frazzled device name output */
82 static int max_bitrate_len;
84 static unsigned char redraw;
85 static unsigned char timestamp;
86 static unsigned char color;
87 static unsigned char bargraph;
88 static enum cfl_mode mode = CFL_WORSTCASE;
91 void print_usage(char *prg)
93 fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
94 fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
95 fprintf(stderr, "Options: -t (show current time on the first line)\n");
96 fprintf(stderr, " -c (colorize lines)\n");
97 fprintf(stderr, " -b (show bargraph in %d%% resolution)\n", PERCENTRES);
98 fprintf(stderr, " -r (redraw the terminal - similar to top)\n");
99 fprintf(stderr, " -i (ignore bitstuffing in bandwidth calculation)\n");
100 fprintf(stderr, " -e (exact calculation of stuffed bits)\n");
101 fprintf(stderr, "\n");
102 fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXSOCK);
103 fprintf(stderr, "commandline in the form: <ifname>@<bitrate>\n\n");
104 fprintf(stderr, "The bitrate is mandatory as it is needed to know the CAN bus bitrate to\n");
105 fprintf(stderr, "calcultate the bus load percentage based on the received CAN frames.\n");
106 fprintf(stderr, "Due to the bitstuffing estimation the calculated busload may exceed 100%%.\n");
107 fprintf(stderr, "For each given interface the data is presented in one line which contains:\n\n");
108 fprintf(stderr, "(interface) (received CAN frames) (used bits total) (used bits for payload)\n");
109 fprintf(stderr, "\nExample:\n");
110 fprintf(stderr, "\nuser$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c\n\n");
111 fprintf(stderr, "%s 2014-02-01 21:13:16 (worst case bitstuffing)\n", prg);
112 fprintf(stderr, " can0@100000 805 74491 36656 74%% |XXXXXXXXXXXXXX......|\n");
113 fprintf(stderr, " can1@500000 796 75140 37728 15%% |XXX.................|\n");
114 fprintf(stderr, " can2@500000 0 0 0 0%% |....................|\n");
115 fprintf(stderr, " can3@500000 47 4633 2424 0%% |....................|\n");
116 fprintf(stderr, "\n");
119 void sigterm(int signo)
124 void printstats(int signo)
129 printf("%s", CSR_HOME);
135 if (time(&currtime) == (time_t)-1) {
140 localtime_r(&currtime, &now);
142 printf("%s %04d-%02d-%02d %02d:%02d:%02d ",
153 case CFL_NO_BITSTUFFING:
154 /* plain bit calculation without bitstuffing */
155 printf("(ignore bitstuffing)\n");
159 /* worst case estimation - see above */
160 printf("(worst case bitstuffing)\n");
164 /* exact calculation of stuffed bits based on frame content and CRC */
165 printf("(exact bitstuffing)\n");
169 printf("(unknown bitstuffing)\n");
174 for (i=0; i<currmax; i++) {
180 printf("%s", FGBLUE);
184 percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
188 printf(" %*s@%-*d %5d %7d %6d %3d%%",
189 max_devname_len, stat[i].devname,
190 max_bitrate_len, stat[i].bitrate,
192 stat[i].recv_bits_total,
193 stat[i].recv_bits_payload,
203 for (j=0; j < NUMBAR; j++){
204 if (j < percent/PERCENTRES)
214 printf("%s", ATTRESET);
218 stat[i].recv_frames = 0;
219 stat[i].recv_bits_total = 0;
220 stat[i].recv_bits_payload = 0;
229 int main(int argc, char **argv)
236 struct sockaddr_can addr;
237 struct can_frame frame;
240 sigset_t sigmask, savesigmask;
242 signal(SIGTERM, sigterm);
243 signal(SIGHUP, sigterm);
244 signal(SIGINT, sigterm);
246 signal(SIGALRM, printstats);
248 prg = basename(argv[0]);
250 while ((opt = getopt(argc, argv, "rtbcieh?")) != -1) {
269 mode = CFL_NO_BITSTUFFING;
283 if (optind == argc) {
288 currmax = argc - optind; /* find real number of CAN devices */
290 if (currmax > MAXSOCK) {
291 printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
295 for (i=0; i < currmax; i++) {
297 ptr = argv[optind+i];
299 nbytes = strlen(ptr);
300 if (nbytes >= IFNAMSIZ+sizeof("@1000000")+1) {
301 printf("name of CAN device '%s' is too long!\n", ptr);
306 printf("open %d '%s'.\n", i, ptr);
309 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
315 nptr = strchr(ptr, '@');
322 nbytes = nptr - ptr; /* interface name is up the first '@' */
324 if (nbytes >= IFNAMSIZ) {
325 printf("name of CAN device '%s' is too long!\n", ptr);
329 strncpy(stat[i].devname, ptr, nbytes);
330 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
331 strncpy(ifr.ifr_name, ptr, nbytes);
333 if (nbytes > max_devname_len)
334 max_devname_len = nbytes; /* for nice printing */
336 stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
338 if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
339 printf("invalid bitrate for CAN device '%s'!\n", ptr);
343 nbytes = strlen(nptr+1);
344 if (nbytes > max_bitrate_len)
345 max_bitrate_len = nbytes; /* for nice printing */
349 printf("using interface name '%s'.\n", ifr.ifr_name);
352 if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
353 perror("SIOCGIFINDEX");
357 addr.can_family = AF_CAN;
358 addr.can_ifindex = ifr.ifr_ifindex;
360 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
369 printf("%s", CLR_SCREEN);
374 for (i=0; i<currmax; i++)
377 savesigmask = sigmask;
379 if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
381 sigmask = savesigmask;
385 for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
387 if (FD_ISSET(s[i], &rdfs)) {
389 nbytes = read(s[i], &frame, sizeof(struct can_frame));
396 if (nbytes < sizeof(struct can_frame)) {
397 fprintf(stderr, "read: incomplete CAN frame\n");
401 stat[i].recv_frames++;
402 stat[i].recv_bits_payload += frame.can_dlc*8;
403 stat[i].recv_bits_total += can_frame_length((struct canfd_frame*)&frame,
404 mode, sizeof(frame));
409 for (i=0; i<currmax; i++)