statistics.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* stat_parse.c
  2. * Parse statistics files.
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <limits.h>
  10. #include <ctype.h>
  11. #include "statistics.h"
  12. #include "cpu.h"
  13. #include "irq.h"
  14. #define STR(str) ( str ? str : "" )
  15. void parse_proc_stat(lub_list_t *cpus, lub_list_t *irqs)
  16. {
  17. FILE *file;
  18. char *line = NULL;
  19. size_t size = 0;
  20. int cpunr, rc, cpucount;
  21. unsigned long long l_user;
  22. unsigned long long l_nice;
  23. unsigned long long l_system;
  24. unsigned long long l_idle;
  25. unsigned long long l_iowait;
  26. unsigned long long l_irq;
  27. unsigned long long l_softirq;
  28. unsigned long long l_steal;
  29. unsigned long long l_guest;
  30. unsigned long long l_guest_nice;
  31. unsigned long long load_irq, load_all;
  32. char *intr_str;
  33. char *saveptr;
  34. unsigned int inum = 0;
  35. file = fopen("/proc/stat", "r");
  36. if (!file) {
  37. fprintf(stderr, "Warning: Can't open /proc/stat. Balacing is broken.\n");
  38. return;
  39. }
  40. /* first line is the header we don't need; nuke it */
  41. if (getline(&line, &size, file) == 0) {
  42. free(line);
  43. fprintf(stderr, "Warning: Can't read /proc/stat. Balancing is broken.\n");
  44. fclose(file);
  45. return;
  46. }
  47. cpucount = 0;
  48. while (!feof(file)) {
  49. cpu_t *cpu;
  50. if (getline(&line, &size, file)==0)
  51. break;
  52. if (!strstr(line, "cpu"))
  53. break;
  54. cpunr = strtoul(&line[3], NULL, 10);
  55. cpu = cpu_list_search(cpus, cpunr);
  56. if (!cpu)
  57. continue;
  58. rc = sscanf(line, "%*s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
  59. &l_user, &l_nice, &l_system, &l_idle, &l_iowait,
  60. &l_irq, &l_softirq, &l_steal, &l_guest, &l_guest_nice);
  61. if (rc < 2)
  62. break;
  63. cpucount++;
  64. load_all = l_user + l_nice + l_system + l_idle + l_iowait +
  65. l_irq + l_softirq + l_steal + l_guest + l_guest_nice;
  66. load_irq = l_irq + l_softirq;
  67. if (cpu->old_load_all == 0) {
  68. /* When old_load_all = 0 - it's first iteration */
  69. cpu->load = 0;
  70. } else {
  71. float d_all = (float)(load_all - cpu->old_load_all);
  72. float d_irq = (float)(load_irq - cpu->old_load_irq);
  73. cpu->load = d_irq * 100 / d_all;
  74. }
  75. cpu->old_load_all = load_all;
  76. cpu->old_load_irq = load_irq;
  77. printf("CPU %u %.2f%%\n", cpunr, cpu->load);
  78. }
  79. /* Parse "intr" line. Get number of interrupts. */
  80. intr_str = strtok_r(line, " ", &saveptr); /* String "intr" */
  81. intr_str = strtok_r(NULL, " ", &saveptr); /* Total number of interrupts */
  82. for (intr_str = strtok_r(NULL, " ", &saveptr);
  83. intr_str; intr_str = strtok_r(NULL, " ", &saveptr)) {
  84. unsigned long long intr = 0;
  85. char *endptr;
  86. irq_t *irq;
  87. irq = irq_list_search(irqs, inum);
  88. inum++;
  89. if (!irq)
  90. continue;
  91. intr = strtoull(intr_str, &endptr, 10);
  92. if (endptr == intr_str)
  93. intr = 0;
  94. if (irq->old_intr == 0)
  95. irq->intr = 0;
  96. else
  97. irq->intr = intr - irq->old_intr;
  98. irq->old_intr = intr;
  99. printf("IRQ %u %llu %s\n", irq->irq, irq->intr, irq->desc);
  100. }
  101. fclose(file);
  102. free(line);
  103. }