statistics.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <time.h>
  12. #include "statistics.h"
  13. #include "cpu.h"
  14. #include "irq.h"
  15. /* Gather load statistics for CPUs and number of interrupts
  16. for current iteration. */
  17. void gather_statistics(lub_list_t *cpus, lub_list_t *irqs)
  18. {
  19. FILE *file;
  20. char *line = NULL;
  21. size_t size = 0;
  22. int cpunr, rc, cpucount;
  23. unsigned long long l_user;
  24. unsigned long long l_nice;
  25. unsigned long long l_system;
  26. unsigned long long l_idle;
  27. unsigned long long l_iowait;
  28. unsigned long long l_irq;
  29. unsigned long long l_softirq;
  30. unsigned long long l_steal;
  31. unsigned long long l_guest;
  32. unsigned long long l_guest_nice;
  33. unsigned long long load_irq, load_all;
  34. char *intr_str;
  35. char *saveptr;
  36. unsigned int inum = 0;
  37. file = fopen("/proc/stat", "r");
  38. if (!file) {
  39. fprintf(stderr, "Warning: Can't open /proc/stat. Balacing is broken.\n");
  40. return;
  41. }
  42. /* Get statistics for CPUs */
  43. /* First line is the header. */
  44. if (getline(&line, &size, file) == 0) {
  45. free(line);
  46. fprintf(stderr, "Warning: Can't read /proc/stat. Balancing is broken.\n");
  47. fclose(file);
  48. return;
  49. }
  50. cpucount = 0;
  51. while (!feof(file)) {
  52. cpu_t *cpu;
  53. if (getline(&line, &size, file)==0)
  54. break;
  55. if (!strstr(line, "cpu"))
  56. break;
  57. cpunr = strtoul(&line[3], NULL, 10);
  58. cpu = cpu_list_search(cpus, cpunr);
  59. if (!cpu)
  60. continue;
  61. rc = sscanf(line, "%*s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
  62. &l_user, &l_nice, &l_system, &l_idle, &l_iowait,
  63. &l_irq, &l_softirq, &l_steal, &l_guest, &l_guest_nice);
  64. if (rc < 2)
  65. break;
  66. cpucount++;
  67. load_all = l_user + l_nice + l_system + l_idle + l_iowait +
  68. l_irq + l_softirq + l_steal + l_guest + l_guest_nice;
  69. load_irq = l_irq + l_softirq;
  70. if (cpu->old_load_all == 0) {
  71. /* When old_load_all = 0 - it's first iteration */
  72. cpu->load = 0;
  73. } else {
  74. float d_all = (float)(load_all - cpu->old_load_all);
  75. float d_irq = (float)(load_irq - cpu->old_load_irq);
  76. cpu->load = d_irq * 100 / d_all;
  77. }
  78. cpu->old_load_all = load_all;
  79. cpu->old_load_irq = load_irq;
  80. }
  81. /* Parse "intr" line. Get number of interrupts. */
  82. intr_str = strtok_r(line, " ", &saveptr); /* String "intr" */
  83. intr_str = strtok_r(NULL, " ", &saveptr); /* Total number of interrupts */
  84. for (intr_str = strtok_r(NULL, " ", &saveptr);
  85. intr_str; intr_str = strtok_r(NULL, " ", &saveptr)) {
  86. unsigned long long intr = 0;
  87. char *endptr;
  88. irq_t *irq;
  89. irq = irq_list_search(irqs, inum);
  90. inum++;
  91. if (!irq)
  92. continue;
  93. intr = strtoull(intr_str, &endptr, 10);
  94. if (endptr == intr_str)
  95. intr = 0;
  96. if (irq->old_intr == 0)
  97. irq->intr = 0;
  98. else
  99. irq->intr = intr - irq->old_intr;
  100. irq->old_intr = intr;
  101. }
  102. fclose(file);
  103. free(line);
  104. }
  105. void show_statistics(lub_list_t *cpus)
  106. {
  107. lub_list_node_t *iter;
  108. char outstr[10];
  109. time_t t;
  110. struct tm *tmp;
  111. t = time(NULL);
  112. tmp = localtime(&t);
  113. strftime(outstr, sizeof(outstr), "%H:%M:%S", tmp);
  114. printf("----[ %s ]----------------------------------------------------------------\n", outstr);
  115. for (iter = lub_list_iterator_init(cpus); iter;
  116. iter = lub_list_iterator_next(iter)) {
  117. cpu_t *cpu;
  118. lub_list_node_t *irq_iter;
  119. cpu = (cpu_t *)lub_list_node__get_data(iter);
  120. printf("CPU%u package %u, core %u, irqs %d, load %.2f%%\n",
  121. cpu->id, cpu->package_id, cpu->core_id,
  122. lub_list_len(cpu->irqs), cpu->load);
  123. for (irq_iter = lub_list_iterator_init(cpu->irqs); irq_iter;
  124. irq_iter = lub_list_iterator_next(irq_iter)) {
  125. irq_t *irq;
  126. irq = (irq_t *)lub_list_node__get_data(irq_iter);
  127. printf(" IRQ %3u, intr %llu, %s\n", irq->irq, irq->intr, irq->desc);
  128. }
  129. }
  130. }