forked from rostedt/trace-cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace-plot.c
394 lines (316 loc) · 8.41 KB
/
trace-plot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (C) 2010 Red Hat Inc, Steven Rostedt <[email protected]>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License (not later!)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <string.h>
#include "trace-graph.h"
void trace_graph_plot_free(struct graph_info *ginfo)
{
struct graph_plot **array;
int plots;
int i;
/* copy the plot_array since the removing plots will modify it */
array = malloc_or_die(sizeof(*array) * ginfo->plots);
memcpy(array, ginfo->plot_array, sizeof(*array) * ginfo->plots);
plots = ginfo->plots;
for (i = 0; i < plots; i++)
trace_graph_plot_remove(ginfo, array[i]);
free(array);
if (ginfo->plot_array) {
free(ginfo->plot_array);
ginfo->plot_array = NULL;
};
ginfo->plots = 0;
}
void trace_graph_plot_init(struct graph_info *ginfo)
{
ginfo->plots = 0;
ginfo->plot_array = NULL;
}
static struct graph_plot *
allocate_plot(struct graph_info *ginfo,
const char *label, const struct plot_callbacks *cb,
void *data)
{
struct graph_plot *plot;
char *name;
name = strdup(label);
if (!name)
die("Unable to allocate label");
plot = malloc_or_die(sizeof(*plot));
memset(plot, 0, sizeof(*plot));
plot->label = name;
plot->cb = cb;
plot->private = data;
return plot;
}
struct graph_plot *
trace_graph_plot_append(struct graph_info *ginfo,
const char *label, enum graph_plot_type type,
const struct plot_callbacks *cb, void *data)
{
struct graph_plot *plot;
plot = allocate_plot(ginfo, label, cb, data);
plot->type = type;
plot->pos = ginfo->plots;
if (!ginfo->plots) {
ginfo->plot_array = malloc_or_die(sizeof(ginfo->plot_array[0]));
ginfo->plot_array[0] = plot;
} else {
ginfo->plot_array = realloc(ginfo->plot_array,
sizeof(ginfo->plot_array[0]) *
(ginfo->plots + 1));
if (!ginfo->plot_array)
die("unable to resize plot array");
ginfo->plot_array[ginfo->plots] = plot;
}
ginfo->plots++;
return plot;
}
struct graph_plot *
trace_graph_plot_insert(struct graph_info *ginfo,
int pos, const char *label, enum graph_plot_type type,
const struct plot_callbacks *cb, void *data)
{
struct graph_plot *plot;
int i;
if (pos >= ginfo->plots)
return trace_graph_plot_append(ginfo, label, type, cb, data);
if (pos < 0)
pos = 0;
plot = allocate_plot(ginfo, label, cb, data);
plot->pos = pos;
plot->type = type;
ginfo->plot_array = realloc(ginfo->plot_array,
sizeof(ginfo->plot_array[0]) *
(ginfo->plots + 1));
if (!ginfo->plot_array)
die("unable to resize plot array");
memmove(&ginfo->plot_array[pos+1], &ginfo->plot_array[pos],
sizeof(ginfo->plot_array[0]) * (ginfo->plots - pos));
ginfo->plot_array[pos] = plot;
ginfo->plots++;
/* Update the new positions */
for (i = pos + 1; i < ginfo->plots; i++)
ginfo->plot_array[i]->pos = i;
return plot;
}
void trace_graph_plot_remove(struct graph_info *ginfo, struct graph_plot *plot)
{
int pos = plot->pos;
int i;
if (plot->cb->destroy)
plot->cb->destroy(ginfo, plot);
free(plot->label);
free(plot);
ginfo->plots--;
if (ginfo->plots) {
memmove(&ginfo->plot_array[pos], &ginfo->plot_array[pos+1],
sizeof(ginfo->plot_array[0]) * (ginfo->plots - pos));
/* Update the new positions */
for (i = pos; i < ginfo->plots; i++)
ginfo->plot_array[i]->pos = i;
} else {
free(ginfo->plot_array);
ginfo->plot_array = NULL;
}
}
static struct plot_hash *find_hash(struct plot_hash **array, gint val)
{
struct plot_hash *hash;
gint key;
key = trace_hash(val) % PLOT_HASH_SIZE;
for (hash = array[key]; hash; hash = hash->next) {
if (hash->val == val)
return hash;
}
return NULL;
}
static void add_hash(struct plot_hash **array, struct graph_plot *plot, gint val)
{
struct plot_hash *hash;
struct plot_list *list;
gint key;
list = malloc_or_die(sizeof(*list));
hash = find_hash(array, val);
if (!hash) {
hash = g_new0(typeof(*hash), 1);
g_assert(hash);
key = trace_hash(val) % PLOT_HASH_SIZE;
hash->next = array[key];
hash->val = val;
array[key] = hash;
}
list->next = hash->plots;
list->plot = plot;
hash->plots = list;
}
static void remove_hash(struct plot_hash **array, struct graph_plot *plot, gint val)
{
struct plot_hash *hash, **phash;
struct plot_list **pplot;
struct plot_list *list;
gint key;
hash = find_hash(array, val);
pplot = &hash->plots;
while ((list = *pplot)) {
if (list->plot == plot) {
*pplot = list->next;
free(list);
break;
}
pplot = &list->next;
}
if (hash->plots)
return;
/* remove this hash item */
key = trace_hash(val) % PLOT_HASH_SIZE;
phash = &array[key];
while (*phash) {
if (*phash == hash) {
*phash = hash->next;
break;
}
phash = &(*phash)->next;
}
g_free(hash);
}
struct plot_hash *
trace_graph_plot_find_task(struct graph_info *ginfo, gint task)
{
return find_hash(ginfo->task_hash, task);
}
void trace_graph_plot_add_task(struct graph_info *ginfo,
struct graph_plot *plot,
gint task)
{
add_hash(ginfo->task_hash, plot, task);
ginfo->nr_task_hash++;
}
void trace_graph_plot_remove_task(struct graph_info *ginfo,
struct graph_plot *plot,
gint task)
{
remove_hash(ginfo->task_hash, plot, task);
ginfo->nr_task_hash--;
}
struct plot_hash *
trace_graph_plot_find_cpu(struct graph_info *ginfo, gint cpu)
{
return find_hash(ginfo->cpu_hash, cpu);
}
void trace_graph_plot_add_cpu(struct graph_info *ginfo,
struct graph_plot *plot,
gint cpu)
{
add_hash(ginfo->cpu_hash, plot, cpu);
}
void trace_graph_plot_remove_cpu(struct graph_info *ginfo,
struct graph_plot *plot,
gint cpu)
{
remove_hash(ginfo->cpu_hash, plot, cpu);
}
void trace_graph_plot_add_all_recs(struct graph_info *ginfo,
struct graph_plot *plot)
{
struct plot_list *list;
list = malloc_or_die(sizeof(*list));
list->next = ginfo->all_recs;
list->plot = plot;
ginfo->all_recs = list;
}
void trace_graph_plot_remove_all_recs(struct graph_info *ginfo,
struct graph_plot *plot)
{
struct plot_list **pplot;
struct plot_list *list;
pplot = &ginfo->all_recs;
while ((list = *pplot)) {
if (list->plot == plot) {
*pplot = list->next;
free(list);
break;
}
pplot = &list->next;
}
}
/* Plot callback helpers */
int trace_graph_plot_match_time(struct graph_info *ginfo,
struct graph_plot *plot,
unsigned long long time)
{
if (!plot->cb->match_time)
return 0;
return plot->cb->match_time(ginfo, plot, time);
}
void trace_graph_plot_start(struct graph_info *ginfo,
struct graph_plot *plot,
unsigned long long time)
{
if (!plot->cb->start)
return;
return plot->cb->start(ginfo, plot, time);
}
int trace_graph_plot_event(struct graph_info *ginfo,
struct graph_plot *plot,
struct record *record,
struct plot_info *info)
{
info->line = FALSE;
info->box = FALSE;
info->bfill = TRUE;
if (!plot->cb->plot_event)
return 0;
return plot->cb->plot_event(ginfo, plot, record, info);
}
void trace_graph_plot_end(struct graph_info *ginfo,
struct graph_plot *plot)
{
if (!plot->cb->end)
return;
return plot->cb->end(ginfo, plot);
}
int trace_graph_plot_display_last_event(struct graph_info *ginfo,
struct graph_plot *plot,
struct trace_seq *s,
unsigned long long time)
{
if (!plot->cb->display_last_event)
return 0;
return plot->cb->display_last_event(ginfo, plot, s, time);
}
struct record *
trace_graph_plot_find_record(struct graph_info *ginfo,
struct graph_plot *plot,
unsigned long long time)
{
if (!plot->cb->find_record)
return 0;
return plot->cb->find_record(ginfo, plot, time);
}
int trace_graph_plot_display_info(struct graph_info *ginfo,
struct graph_plot *plot,
struct trace_seq *s,
unsigned long long time)
{
if (!plot->cb->display_info)
return 0;
return plot->cb->display_info(ginfo, plot, s, time);
}