canbusload.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * canbusload.c
  3. *
  4. * Copyright (c) 2002-2008 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  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.
  18. *
  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.
  23. *
  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.
  26. *
  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
  38. * DAMAGE.
  39. *
  40. * Send feedback to <linux-can@vger.kernel.org>
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <unistd.h>
  46. #include <string.h>
  47. #include <signal.h>
  48. #include <ctype.h>
  49. #include <libgen.h>
  50. #include <time.h>
  51. #include <sys/time.h>
  52. #include <sys/types.h>
  53. #include <sys/socket.h>
  54. #include <sys/ioctl.h>
  55. #include <sys/uio.h>
  56. #include <net/if.h>
  57. #include <linux/can.h>
  58. #include <linux/can/raw.h>
  59. #include "terminal.h"
  60. #include "canframelen.h"
  61. #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
  62. #define PERCENTRES 5 /* resolution in percent for bargraph */
  63. #define NUMBAR (100/PERCENTRES) /* number of bargraph elements */
  64. extern int optind, opterr, optopt;
  65. static struct {
  66. char devname[IFNAMSIZ+1];
  67. unsigned int bitrate;
  68. unsigned int recv_frames;
  69. unsigned int recv_bits_total;
  70. unsigned int recv_bits_payload;
  71. } stat[MAXSOCK+1];
  72. static int max_devname_len; /* to prevent frazzled device name output */
  73. static int max_bitrate_len;
  74. static int currmax;
  75. static unsigned char redraw;
  76. static unsigned char timestamp;
  77. static unsigned char color;
  78. static unsigned char bargraph;
  79. static enum cfl_mode mode = CFL_WORSTCASE;
  80. static char *prg;
  81. void print_usage(char *prg)
  82. {
  83. fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
  84. fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
  85. fprintf(stderr, "Options: -t (show current time on the first line)\n");
  86. fprintf(stderr, " -c (colorize lines)\n");
  87. fprintf(stderr, " -b (show bargraph in %d%% resolution)\n", PERCENTRES);
  88. fprintf(stderr, " -r (redraw the terminal - similar to top)\n");
  89. fprintf(stderr, " -i (ignore bitstuffing in bandwidth calculation)\n");
  90. fprintf(stderr, " -e (exact calculation of stuffed bits)\n");
  91. fprintf(stderr, "\n");
  92. fprintf(stderr, "Up to %d CAN interfaces with mandatory bitrate can be specified on the \n", MAXSOCK);
  93. fprintf(stderr, "commandline in the form: <ifname>@<bitrate>\n\n");
  94. fprintf(stderr, "The bitrate is mandatory as it is needed to know the CAN bus bitrate to\n");
  95. fprintf(stderr, "calcultate the bus load percentage based on the received CAN frames.\n");
  96. fprintf(stderr, "Due to the bitstuffing estimation the calculated busload may exceed 100%%.\n");
  97. fprintf(stderr, "For each given interface the data is presented in one line which contains:\n\n");
  98. fprintf(stderr, "(interface) (received CAN frames) (used bits total) (used bits for payload)\n");
  99. fprintf(stderr, "\nExample:\n");
  100. fprintf(stderr, "\nuser$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c\n\n");
  101. fprintf(stderr, "%s 2014-02-01 21:13:16 (worst case bitstuffing)\n", prg);
  102. fprintf(stderr, " can0@100000 805 74491 36656 74%% |XXXXXXXXXXXXXX......|\n");
  103. fprintf(stderr, " can1@500000 796 75140 37728 15%% |XXX.................|\n");
  104. fprintf(stderr, " can2@500000 0 0 0 0%% |....................|\n");
  105. fprintf(stderr, " can3@500000 47 4633 2424 0%% |....................|\n");
  106. fprintf(stderr, "\n");
  107. }
  108. void sigterm(int signo)
  109. {
  110. exit(0);
  111. }
  112. void printstats(int signo)
  113. {
  114. int i, j, percent;
  115. if (redraw)
  116. printf("%s", CSR_HOME);
  117. if (timestamp) {
  118. time_t currtime;
  119. struct tm now;
  120. if (time(&currtime) == (time_t)-1) {
  121. perror("time");
  122. exit(1);
  123. }
  124. localtime_r(&currtime, &now);
  125. printf("%s %04d-%02d-%02d %02d:%02d:%02d ",
  126. prg,
  127. now.tm_year + 1900,
  128. now.tm_mon + 1,
  129. now.tm_mday,
  130. now.tm_hour,
  131. now.tm_min,
  132. now.tm_sec);
  133. switch (mode) {
  134. case CFL_NO_BITSTUFFING:
  135. /* plain bit calculation without bitstuffing */
  136. printf("(ignore bitstuffing)\n");
  137. break;
  138. case CFL_WORSTCASE:
  139. /* worst case estimation - see above */
  140. printf("(worst case bitstuffing)\n");
  141. break;
  142. case CFL_EXACT:
  143. /* exact calculation of stuffed bits based on frame content and CRC */
  144. printf("(exact bitstuffing)\n");
  145. break;
  146. default:
  147. printf("(unknown bitstuffing)\n");
  148. break;
  149. }
  150. }
  151. for (i=0; i<currmax; i++) {
  152. if (color) {
  153. if (i%2)
  154. printf("%s", FGRED);
  155. else
  156. printf("%s", FGBLUE);
  157. }
  158. if (stat[i].bitrate)
  159. percent = (stat[i].recv_bits_total*100)/stat[i].bitrate;
  160. else
  161. percent = 0;
  162. printf(" %*s@%-*d %5d %7d %6d %3d%%",
  163. max_devname_len, stat[i].devname,
  164. max_bitrate_len, stat[i].bitrate,
  165. stat[i].recv_frames,
  166. stat[i].recv_bits_total,
  167. stat[i].recv_bits_payload,
  168. percent);
  169. if (bargraph) {
  170. printf(" |");
  171. if (percent > 100)
  172. percent = 100;
  173. for (j=0; j < NUMBAR; j++){
  174. if (j < percent/PERCENTRES)
  175. printf("X");
  176. else
  177. printf(".");
  178. }
  179. printf("|");
  180. }
  181. if (color)
  182. printf("%s", ATTRESET);
  183. printf("\n");
  184. stat[i].recv_frames = 0;
  185. stat[i].recv_bits_total = 0;
  186. stat[i].recv_bits_payload = 0;
  187. }
  188. printf("\n");
  189. fflush(stdout);
  190. alarm(1);
  191. }
  192. int main(int argc, char **argv)
  193. {
  194. fd_set rdfs;
  195. int s[MAXSOCK];
  196. int opt;
  197. char *ptr, *nptr;
  198. struct sockaddr_can addr;
  199. struct can_frame frame;
  200. int nbytes, i;
  201. struct ifreq ifr;
  202. sigset_t sigmask, savesigmask;
  203. signal(SIGTERM, sigterm);
  204. signal(SIGHUP, sigterm);
  205. signal(SIGINT, sigterm);
  206. signal(SIGALRM, printstats);
  207. prg = basename(argv[0]);
  208. while ((opt = getopt(argc, argv, "rtbcieh?")) != -1) {
  209. switch (opt) {
  210. case 'r':
  211. redraw = 1;
  212. break;
  213. case 't':
  214. timestamp = 1;
  215. break;
  216. case 'b':
  217. bargraph = 1;
  218. break;
  219. case 'c':
  220. color = 1;
  221. break;
  222. case 'i':
  223. mode = CFL_NO_BITSTUFFING;
  224. break;
  225. case 'e':
  226. mode = CFL_EXACT;
  227. break;
  228. default:
  229. print_usage(prg);
  230. exit(1);
  231. break;
  232. }
  233. }
  234. if (optind == argc) {
  235. print_usage(prg);
  236. exit(0);
  237. }
  238. currmax = argc - optind; /* find real number of CAN devices */
  239. if (currmax > MAXSOCK) {
  240. printf("More than %d CAN devices given on commandline!\n", MAXSOCK);
  241. return 1;
  242. }
  243. for (i=0; i < currmax; i++) {
  244. ptr = argv[optind+i];
  245. nbytes = strlen(ptr);
  246. if (nbytes >= IFNAMSIZ+sizeof("@1000000")+1) {
  247. printf("name of CAN device '%s' is too long!\n", ptr);
  248. return 1;
  249. }
  250. #ifdef DEBUG
  251. printf("open %d '%s'.\n", i, ptr);
  252. #endif
  253. s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  254. if (s[i] < 0) {
  255. perror("socket");
  256. return 1;
  257. }
  258. nptr = strchr(ptr, '@');
  259. if (!nptr) {
  260. print_usage(prg);
  261. return 1;
  262. }
  263. nbytes = nptr - ptr; /* interface name is up the first '@' */
  264. if (nbytes >= IFNAMSIZ) {
  265. printf("name of CAN device '%s' is too long!\n", ptr);
  266. return 1;
  267. }
  268. strncpy(stat[i].devname, ptr, nbytes);
  269. memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
  270. strncpy(ifr.ifr_name, ptr, nbytes);
  271. if (nbytes > max_devname_len)
  272. max_devname_len = nbytes; /* for nice printing */
  273. stat[i].bitrate = atoi(nptr+1); /* bitrate is placed behind the '@' */
  274. if (!stat[i].bitrate || stat[i].bitrate > 1000000) {
  275. printf("invalid bitrate for CAN device '%s'!\n", ptr);
  276. return 1;
  277. }
  278. nbytes = strlen(nptr+1);
  279. if (nbytes > max_bitrate_len)
  280. max_bitrate_len = nbytes; /* for nice printing */
  281. #ifdef DEBUG
  282. printf("using interface name '%s'.\n", ifr.ifr_name);
  283. #endif
  284. if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
  285. perror("SIOCGIFINDEX");
  286. exit(1);
  287. }
  288. addr.can_family = AF_CAN;
  289. addr.can_ifindex = ifr.ifr_ifindex;
  290. if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  291. perror("bind");
  292. return 1;
  293. }
  294. }
  295. alarm(1);
  296. if (redraw)
  297. printf("%s", CLR_SCREEN);
  298. while (1) {
  299. FD_ZERO(&rdfs);
  300. for (i=0; i<currmax; i++)
  301. FD_SET(s[i], &rdfs);
  302. savesigmask = sigmask;
  303. if (pselect(s[currmax-1]+1, &rdfs, NULL, NULL, NULL, &sigmask) < 0) {
  304. //perror("pselect");
  305. sigmask = savesigmask;
  306. continue;
  307. }
  308. for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
  309. if (FD_ISSET(s[i], &rdfs)) {
  310. nbytes = read(s[i], &frame, sizeof(struct can_frame));
  311. if (nbytes < 0) {
  312. perror("read");
  313. return 1;
  314. }
  315. if (nbytes < sizeof(struct can_frame)) {
  316. fprintf(stderr, "read: incomplete CAN frame\n");
  317. return 1;
  318. }
  319. stat[i].recv_frames++;
  320. stat[i].recv_bits_payload += frame.can_dlc*8;
  321. stat[i].recv_bits_total += can_frame_length((struct canfd_frame*)&frame,
  322. mode, sizeof(frame));
  323. }
  324. }
  325. }
  326. for (i=0; i<currmax; i++)
  327. close(s[i]);
  328. return 0;
  329. }