statistics.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #define STR(str) ( str ? str : "" )
  14. void parse_proc_stat(lub_list_t *cpus)
  15. {
  16. FILE *file;
  17. char *line = NULL;
  18. size_t size = 0;
  19. int cpunr, rc, cpucount;
  20. unsigned long long l_user;
  21. unsigned long long l_nice;
  22. unsigned long long l_system;
  23. unsigned long long l_idle;
  24. unsigned long long l_iowait;
  25. unsigned long long l_irq;
  26. unsigned long long l_softirq;
  27. unsigned long long l_steal;
  28. unsigned long long l_guest;
  29. unsigned long long l_guest_nice;
  30. unsigned long long load_irq, load_all;
  31. file = fopen("/proc/stat", "r");
  32. if (!file) {
  33. fprintf(stderr, "Warning: Can't open /proc/stat. Balacing is broken.\n");
  34. return;
  35. }
  36. /* first line is the header we don't need; nuke it */
  37. if (getline(&line, &size, file) == 0) {
  38. free(line);
  39. fprintf(stderr, "Warning: Can't read /proc/stat. Balancing is broken.\n");
  40. fclose(file);
  41. return;
  42. }
  43. cpucount = 0;
  44. while (!feof(file)) {
  45. cpu_t *cpu;
  46. if (getline(&line, &size, file)==0)
  47. break;
  48. if (!strstr(line, "cpu"))
  49. break;
  50. cpunr = strtoul(&line[3], NULL, 10);
  51. cpu = cpu_list_search(cpus, cpunr);
  52. if (!cpu)
  53. continue;
  54. rc = sscanf(line, "%*s %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
  55. &l_user, &l_nice, &l_system, &l_idle, &l_iowait,
  56. &l_irq, &l_softirq, &l_steal, &l_guest, &l_guest_nice);
  57. if (rc < 2)
  58. break;
  59. cpucount++;
  60. load_all = l_user + l_nice + l_system + l_idle + l_iowait +
  61. l_irq + l_softirq + l_steal + l_guest + l_guest_nice;
  62. load_irq = l_irq + l_softirq;
  63. if (cpu->old_load_all == 0) {
  64. cpu->load = 0;
  65. } else {
  66. float d_all = (float)(load_all - cpu->old_load_all);
  67. float d_irq = (float)(load_irq - cpu->old_load_irq);
  68. cpu->load = d_irq * 100 / d_all;
  69. }
  70. cpu->old_load_all = load_all;
  71. cpu->old_load_irq = load_irq;
  72. printf("CPU %u %.2f\%\n", cpunr, cpu->load);
  73. /*
  74. * For each cpu add the irq and softirq load and propagate that
  75. * all the way up the device tree
  76. */
  77. // if (cycle_count) {
  78. // cpu->load = (irq_load + softirq_load) - (cpu->last_load);
  79. /*
  80. * the [soft]irq_load values are in jiffies, with
  81. * HZ jiffies per second. Convert the load to nanoseconds
  82. * to get a better integer resolution of nanoseconds per
  83. * interrupt.
  84. */
  85. // cpu->load *= NSEC_PER_SEC/HZ;
  86. // }
  87. // cpu->last_load = (irq_load + softirq_load);
  88. }
  89. fclose(file);
  90. free(line);
  91. // if (cpucount != get_cpu_count()) {
  92. // fprintf(stderr, "Warning: Can't collect load info for all cpus, balancing is broken\n");
  93. // return;
  94. // }
  95. /*
  96. * Reset the load values for all objects above cpus
  97. */
  98. // for_each_object(cache_domains, reset_load, NULL);
  99. /*
  100. * Now that we have load for each cpu attribute a fair share of the load
  101. * to each irq on that cpu
  102. */
  103. // for_each_object(cpus, compute_irq_branch_load_share, NULL);
  104. // for_each_object(cache_domains, compute_irq_branch_load_share, NULL);
  105. // for_each_object(packages, compute_irq_branch_load_share, NULL);
  106. // for_each_object(numa_nodes, compute_irq_branch_load_share, NULL);
  107. }