statistics.c 3.6 KB

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