values.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <stdlib.h>
  2. #include "util.h"
  3. #include "values.h"
  4. void perf_read_values_init(struct perf_read_values *values)
  5. {
  6. values->threads_max = 16;
  7. values->pid = malloc(values->threads_max * sizeof(*values->pid));
  8. values->tid = malloc(values->threads_max * sizeof(*values->tid));
  9. values->value = malloc(values->threads_max * sizeof(*values->value));
  10. if (!values->pid || !values->tid || !values->value)
  11. die("failed to allocate read_values threads arrays");
  12. values->threads = 0;
  13. values->counters_max = 16;
  14. values->counterrawid = malloc(values->counters_max
  15. * sizeof(*values->counterrawid));
  16. values->countername = malloc(values->counters_max
  17. * sizeof(*values->countername));
  18. if (!values->counterrawid || !values->countername)
  19. die("failed to allocate read_values counters arrays");
  20. values->counters = 0;
  21. }
  22. void perf_read_values_destroy(struct perf_read_values *values)
  23. {
  24. int i;
  25. if (!values->threads_max || !values->counters_max)
  26. return;
  27. for (i = 0; i < values->threads; i++)
  28. zfree(&values->value[i]);
  29. zfree(&values->value);
  30. zfree(&values->pid);
  31. zfree(&values->tid);
  32. zfree(&values->counterrawid);
  33. for (i = 0; i < values->counters; i++)
  34. zfree(&values->countername[i]);
  35. zfree(&values->countername);
  36. }
  37. static void perf_read_values__enlarge_threads(struct perf_read_values *values)
  38. {
  39. values->threads_max *= 2;
  40. values->pid = realloc(values->pid,
  41. values->threads_max * sizeof(*values->pid));
  42. values->tid = realloc(values->tid,
  43. values->threads_max * sizeof(*values->tid));
  44. values->value = realloc(values->value,
  45. values->threads_max * sizeof(*values->value));
  46. if (!values->pid || !values->tid || !values->value)
  47. die("failed to enlarge read_values threads arrays");
  48. }
  49. static int perf_read_values__findnew_thread(struct perf_read_values *values,
  50. u32 pid, u32 tid)
  51. {
  52. int i;
  53. for (i = 0; i < values->threads; i++)
  54. if (values->pid[i] == pid && values->tid[i] == tid)
  55. return i;
  56. if (values->threads == values->threads_max)
  57. perf_read_values__enlarge_threads(values);
  58. i = values->threads++;
  59. values->pid[i] = pid;
  60. values->tid[i] = tid;
  61. values->value[i] = malloc(values->counters_max * sizeof(**values->value));
  62. if (!values->value[i])
  63. die("failed to allocate read_values counters array");
  64. return i;
  65. }
  66. static void perf_read_values__enlarge_counters(struct perf_read_values *values)
  67. {
  68. int i;
  69. values->counters_max *= 2;
  70. values->counterrawid = realloc(values->counterrawid,
  71. values->counters_max * sizeof(*values->counterrawid));
  72. values->countername = realloc(values->countername,
  73. values->counters_max * sizeof(*values->countername));
  74. if (!values->counterrawid || !values->countername)
  75. die("failed to enlarge read_values counters arrays");
  76. for (i = 0; i < values->threads; i++) {
  77. values->value[i] = realloc(values->value[i],
  78. values->counters_max * sizeof(**values->value));
  79. if (!values->value[i])
  80. die("failed to enlarge read_values counters arrays");
  81. }
  82. }
  83. static int perf_read_values__findnew_counter(struct perf_read_values *values,
  84. u64 rawid, const char *name)
  85. {
  86. int i;
  87. for (i = 0; i < values->counters; i++)
  88. if (values->counterrawid[i] == rawid)
  89. return i;
  90. if (values->counters == values->counters_max)
  91. perf_read_values__enlarge_counters(values);
  92. i = values->counters++;
  93. values->counterrawid[i] = rawid;
  94. values->countername[i] = strdup(name);
  95. return i;
  96. }
  97. void perf_read_values_add_value(struct perf_read_values *values,
  98. u32 pid, u32 tid,
  99. u64 rawid, const char *name, u64 value)
  100. {
  101. int tindex, cindex;
  102. tindex = perf_read_values__findnew_thread(values, pid, tid);
  103. cindex = perf_read_values__findnew_counter(values, rawid, name);
  104. values->value[tindex][cindex] = value;
  105. }
  106. static void perf_read_values__display_pretty(FILE *fp,
  107. struct perf_read_values *values)
  108. {
  109. int i, j;
  110. int pidwidth, tidwidth;
  111. int *counterwidth;
  112. counterwidth = malloc(values->counters * sizeof(*counterwidth));
  113. if (!counterwidth)
  114. die("failed to allocate counterwidth array");
  115. tidwidth = 3;
  116. pidwidth = 3;
  117. for (j = 0; j < values->counters; j++)
  118. counterwidth[j] = strlen(values->countername[j]);
  119. for (i = 0; i < values->threads; i++) {
  120. int width;
  121. width = snprintf(NULL, 0, "%d", values->pid[i]);
  122. if (width > pidwidth)
  123. pidwidth = width;
  124. width = snprintf(NULL, 0, "%d", values->tid[i]);
  125. if (width > tidwidth)
  126. tidwidth = width;
  127. for (j = 0; j < values->counters; j++) {
  128. width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
  129. if (width > counterwidth[j])
  130. counterwidth[j] = width;
  131. }
  132. }
  133. fprintf(fp, "# %*s %*s", pidwidth, "PID", tidwidth, "TID");
  134. for (j = 0; j < values->counters; j++)
  135. fprintf(fp, " %*s", counterwidth[j], values->countername[j]);
  136. fprintf(fp, "\n");
  137. for (i = 0; i < values->threads; i++) {
  138. fprintf(fp, " %*d %*d", pidwidth, values->pid[i],
  139. tidwidth, values->tid[i]);
  140. for (j = 0; j < values->counters; j++)
  141. fprintf(fp, " %*" PRIu64,
  142. counterwidth[j], values->value[i][j]);
  143. fprintf(fp, "\n");
  144. }
  145. free(counterwidth);
  146. }
  147. static void perf_read_values__display_raw(FILE *fp,
  148. struct perf_read_values *values)
  149. {
  150. int width, pidwidth, tidwidth, namewidth, rawwidth, countwidth;
  151. int i, j;
  152. tidwidth = 3; /* TID */
  153. pidwidth = 3; /* PID */
  154. namewidth = 4; /* "Name" */
  155. rawwidth = 3; /* "Raw" */
  156. countwidth = 5; /* "Count" */
  157. for (i = 0; i < values->threads; i++) {
  158. width = snprintf(NULL, 0, "%d", values->pid[i]);
  159. if (width > pidwidth)
  160. pidwidth = width;
  161. width = snprintf(NULL, 0, "%d", values->tid[i]);
  162. if (width > tidwidth)
  163. tidwidth = width;
  164. }
  165. for (j = 0; j < values->counters; j++) {
  166. width = strlen(values->countername[j]);
  167. if (width > namewidth)
  168. namewidth = width;
  169. width = snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]);
  170. if (width > rawwidth)
  171. rawwidth = width;
  172. }
  173. for (i = 0; i < values->threads; i++) {
  174. for (j = 0; j < values->counters; j++) {
  175. width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
  176. if (width > countwidth)
  177. countwidth = width;
  178. }
  179. }
  180. fprintf(fp, "# %*s %*s %*s %*s %*s\n",
  181. pidwidth, "PID", tidwidth, "TID",
  182. namewidth, "Name", rawwidth, "Raw",
  183. countwidth, "Count");
  184. for (i = 0; i < values->threads; i++)
  185. for (j = 0; j < values->counters; j++)
  186. fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64,
  187. pidwidth, values->pid[i],
  188. tidwidth, values->tid[i],
  189. namewidth, values->countername[j],
  190. rawwidth, values->counterrawid[j],
  191. countwidth, values->value[i][j]);
  192. }
  193. void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw)
  194. {
  195. if (raw)
  196. perf_read_values__display_raw(fp, values);
  197. else
  198. perf_read_values__display_pretty(fp, values);
  199. }