6 void perf_read_values_init(struct perf_read_values
*values
)
8 values
->threads_max
= 16;
9 values
->pid
= malloc(values
->threads_max
* sizeof(*values
->pid
));
10 values
->tid
= malloc(values
->threads_max
* sizeof(*values
->tid
));
11 values
->value
= malloc(values
->threads_max
* sizeof(*values
->value
));
12 if (!values
->pid
|| !values
->tid
|| !values
->value
)
13 die("failed to allocate read_values threads arrays");
16 values
->counters_max
= 16;
17 values
->counterrawid
= malloc(values
->counters_max
18 * sizeof(*values
->counterrawid
));
19 values
->countername
= malloc(values
->counters_max
20 * sizeof(*values
->countername
));
21 if (!values
->counterrawid
|| !values
->countername
)
22 die("failed to allocate read_values counters arrays");
26 void perf_read_values_destroy(struct perf_read_values
*values
)
30 if (!values
->threads_max
|| !values
->counters_max
)
33 for (i
= 0; i
< values
->threads
; i
++)
34 free(values
->value
[i
]);
37 free(values
->counterrawid
);
38 for (i
= 0; i
< values
->counters
; i
++)
39 free(values
->countername
[i
]);
40 free(values
->countername
);
43 static void perf_read_values__enlarge_threads(struct perf_read_values
*values
)
45 values
->threads_max
*= 2;
46 values
->pid
= realloc(values
->pid
,
47 values
->threads_max
* sizeof(*values
->pid
));
48 values
->tid
= realloc(values
->tid
,
49 values
->threads_max
* sizeof(*values
->tid
));
50 values
->value
= realloc(values
->value
,
51 values
->threads_max
* sizeof(*values
->value
));
52 if (!values
->pid
|| !values
->tid
|| !values
->value
)
53 die("failed to enlarge read_values threads arrays");
56 static int perf_read_values__findnew_thread(struct perf_read_values
*values
,
61 for (i
= 0; i
< values
->threads
; i
++)
62 if (values
->pid
[i
] == pid
&& values
->tid
[i
] == tid
)
65 if (values
->threads
== values
->threads_max
)
66 perf_read_values__enlarge_threads(values
);
68 i
= values
->threads
++;
71 values
->value
[i
] = malloc(values
->counters_max
* sizeof(**values
->value
));
72 if (!values
->value
[i
])
73 die("failed to allocate read_values counters array");
78 static void perf_read_values__enlarge_counters(struct perf_read_values
*values
)
82 values
->counters_max
*= 2;
83 values
->counterrawid
= realloc(values
->counterrawid
,
84 values
->counters_max
* sizeof(*values
->counterrawid
));
85 values
->countername
= realloc(values
->countername
,
86 values
->counters_max
* sizeof(*values
->countername
));
87 if (!values
->counterrawid
|| !values
->countername
)
88 die("failed to enlarge read_values counters arrays");
90 for (i
= 0; i
< values
->threads
; i
++) {
91 values
->value
[i
] = realloc(values
->value
[i
],
92 values
->counters_max
* sizeof(**values
->value
));
93 if (!values
->value
[i
])
94 die("failed to enlarge read_values counters arrays");
98 static int perf_read_values__findnew_counter(struct perf_read_values
*values
,
99 u64 rawid
, const char *name
)
103 for (i
= 0; i
< values
->counters
; i
++)
104 if (values
->counterrawid
[i
] == rawid
)
107 if (values
->counters
== values
->counters_max
)
108 perf_read_values__enlarge_counters(values
);
110 i
= values
->counters
++;
111 values
->counterrawid
[i
] = rawid
;
112 values
->countername
[i
] = strdup(name
);
117 void perf_read_values_add_value(struct perf_read_values
*values
,
119 u64 rawid
, const char *name
, u64 value
)
123 tindex
= perf_read_values__findnew_thread(values
, pid
, tid
);
124 cindex
= perf_read_values__findnew_counter(values
, rawid
, name
);
126 values
->value
[tindex
][cindex
] = value
;
129 static void perf_read_values__display_pretty(FILE *fp
,
130 struct perf_read_values
*values
)
133 int pidwidth
, tidwidth
;
136 counterwidth
= malloc(values
->counters
* sizeof(*counterwidth
));
138 die("failed to allocate counterwidth array");
141 for (j
= 0; j
< values
->counters
; j
++)
142 counterwidth
[j
] = strlen(values
->countername
[j
]);
143 for (i
= 0; i
< values
->threads
; i
++) {
146 width
= snprintf(NULL
, 0, "%d", values
->pid
[i
]);
147 if (width
> pidwidth
)
149 width
= snprintf(NULL
, 0, "%d", values
->tid
[i
]);
150 if (width
> tidwidth
)
152 for (j
= 0; j
< values
->counters
; j
++) {
153 width
= snprintf(NULL
, 0, "%Lu", values
->value
[i
][j
]);
154 if (width
> counterwidth
[j
])
155 counterwidth
[j
] = width
;
159 fprintf(fp
, "# %*s %*s", pidwidth
, "PID", tidwidth
, "TID");
160 for (j
= 0; j
< values
->counters
; j
++)
161 fprintf(fp
, " %*s", counterwidth
[j
], values
->countername
[j
]);
164 for (i
= 0; i
< values
->threads
; i
++) {
165 fprintf(fp
, " %*d %*d", pidwidth
, values
->pid
[i
],
166 tidwidth
, values
->tid
[i
]);
167 for (j
= 0; j
< values
->counters
; j
++)
169 counterwidth
[j
], values
->value
[i
][j
]);
174 static void perf_read_values__display_raw(FILE *fp
,
175 struct perf_read_values
*values
)
177 int width
, pidwidth
, tidwidth
, namewidth
, rawwidth
, countwidth
;
180 tidwidth
= 3; /* TID */
181 pidwidth
= 3; /* PID */
182 namewidth
= 4; /* "Name" */
183 rawwidth
= 3; /* "Raw" */
184 countwidth
= 5; /* "Count" */
186 for (i
= 0; i
< values
->threads
; i
++) {
187 width
= snprintf(NULL
, 0, "%d", values
->pid
[i
]);
188 if (width
> pidwidth
)
190 width
= snprintf(NULL
, 0, "%d", values
->tid
[i
]);
191 if (width
> tidwidth
)
194 for (j
= 0; j
< values
->counters
; j
++) {
195 width
= strlen(values
->countername
[j
]);
196 if (width
> namewidth
)
198 width
= snprintf(NULL
, 0, "%llx", values
->counterrawid
[j
]);
199 if (width
> rawwidth
)
202 for (i
= 0; i
< values
->threads
; i
++) {
203 for (j
= 0; j
< values
->counters
; j
++) {
204 width
= snprintf(NULL
, 0, "%Lu", values
->value
[i
][j
]);
205 if (width
> countwidth
)
210 fprintf(fp
, "# %*s %*s %*s %*s %*s\n",
211 pidwidth
, "PID", tidwidth
, "TID",
212 namewidth
, "Name", rawwidth
, "Raw",
213 countwidth
, "Count");
214 for (i
= 0; i
< values
->threads
; i
++)
215 for (j
= 0; j
< values
->counters
; j
++)
216 fprintf(fp
, " %*d %*d %*s %*llx %*Lu\n",
217 pidwidth
, values
->pid
[i
],
218 tidwidth
, values
->tid
[i
],
219 namewidth
, values
->countername
[j
],
220 rawwidth
, values
->counterrawid
[j
],
221 countwidth
, values
->value
[i
][j
]);
224 void perf_read_values_display(FILE *fp
, struct perf_read_values
*values
, int raw
)
227 perf_read_values__display_raw(fp
, values
);
229 perf_read_values__display_pretty(fp
, values
);