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