statistics.c 3.4 KB

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