You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpubar.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* cpubar - ascii cpu usage bar, best used with dzen
  2. *
  3. * Copyright (C) 2007 by Robert Manea, <rob dot manea at gmail dot com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. *
  20. * What you look for is \r it is Carriage Return.
  21. * Makes the carriage go back to the beginning of the line.
  22. * Or use \n as it will go down one line.
  23. *
  24. * Compile it with:
  25. * gcc cpubar.c -o cpubar
  26. *
  27. */
  28. #include<stdio.h>
  29. #include<stdlib.h>
  30. #include<unistd.h>
  31. #include<string.h>
  32. void pbar (double, int, char);
  33. struct cpu_info {
  34. unsigned long long user;
  35. unsigned long long system;
  36. unsigned long long idle;
  37. unsigned long long iowait;
  38. } ncpu, ocpu;
  39. void
  40. pbar(double perc, int maxc, char sym) {
  41. int i, rp;
  42. double l;
  43. l = perc * ((double)maxc / 100);
  44. if( (int)(perc + 0.5) >= (int)perc)
  45. rp = (int)(perc + 0.5);
  46. else
  47. rp = (int)perc;
  48. printf("CPU: %3d%% [", rp);
  49. for(i=0; i < (int)l; i++)
  50. printf("%c", sym);
  51. for(; i < maxc; i++)
  52. printf(" ");
  53. printf("]\r");
  54. fflush(stdout);
  55. }
  56. int
  57. main(int argc, char *argv[])
  58. {
  59. int i;
  60. double total;
  61. struct cpu_info mcpu;
  62. FILE *statfp;
  63. char buf[256];
  64. /* defaults */
  65. int maxchars=25;
  66. int ival=1;
  67. char psym='=';
  68. for(i=1; i < argc; i++) {
  69. if(!strncmp(argv[i], "-c", 3)) {
  70. if(++i < argc)
  71. maxchars = atoi(argv[i]);
  72. }
  73. else if(!strncmp(argv[i], "-s", 3)) {
  74. if(++i < argc)
  75. psym = argv[i][0];
  76. }
  77. else if(!strncmp(argv[i], "-i", 3)) {
  78. if(++i < argc)
  79. ival = atoi(argv[i]);
  80. }
  81. else {
  82. printf("usage: cpubar [-c <charcount>] [-s <symbol>] [-i <interval>]\n");
  83. return EXIT_FAILURE;
  84. }
  85. }
  86. if(!(statfp = fopen("/proc/stat", "r"))) {
  87. printf("cpubar: error opening '/proc/stat'\n");
  88. return EXIT_FAILURE;
  89. }
  90. while(1) {
  91. rewind(statfp);
  92. while(fgets(buf, sizeof buf, statfp)) {
  93. if(!strncmp(buf, "cpu ", 4)) {
  94. unsigned long long unice;
  95. double myload;
  96. /* linux >= 2.6 */
  97. if((sscanf(buf, "cpu %llu %llu %llu %llu %llu",
  98. &ncpu.user, &unice, &ncpu.system, &ncpu.idle, &ncpu.iowait)) < 5) {
  99. printf("cpubar: wrong field count in /proc/stat\n");
  100. return EXIT_FAILURE;
  101. }
  102. ncpu.user += unice;
  103. mcpu.user = ncpu.user - ocpu.user;
  104. mcpu.system = ncpu.system - ocpu.system;
  105. mcpu.idle = ncpu.idle - ocpu.idle;
  106. mcpu.iowait = ncpu.iowait - ocpu.iowait;
  107. total = (mcpu.user + mcpu.system + mcpu.idle + mcpu.iowait) / 100.0;
  108. myload = (mcpu.user + mcpu.system + mcpu.iowait) / total;
  109. pbar(myload, maxchars, psym);
  110. ocpu = ncpu;
  111. }
  112. }
  113. sleep(ival);
  114. }
  115. return EXIT_SUCCESS;
  116. }