Commit | Line | Data |
---|---|---|
1fc22eb4 | 1 | /* |
aa15ac1c | 2 | * Copyright (C) 2011-2012 Julien Desfossez |
1fc22eb4 JD |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License Version 2 as | |
6 | * published by the Free Software Foundation; | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
71bd7ce1 AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
1fc22eb4 JD |
16 | */ |
17 | ||
1dec520a | 18 | #include <babeltrace/ctf/events.h> |
1fc22eb4 | 19 | #include <stdlib.h> |
ceb3a221 | 20 | #include <linux/unistd.h> |
1fc22eb4 JD |
21 | #include <string.h> |
22 | #include "common.h" | |
23 | ||
4adc8274 | 24 | uint64_t get_cpu_id(const struct bt_ctf_event *event) |
d67167cd | 25 | { |
2e0a1190 | 26 | const struct bt_definition *scope; |
d67167cd JD |
27 | uint64_t cpu_id; |
28 | ||
29 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); | |
30 | cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(event, scope, "cpu_id")); | |
31 | if (bt_ctf_field_get_error()) { | |
32 | fprintf(stderr, "[error] get cpu_id\n"); | |
33 | return -1ULL; | |
34 | } | |
35 | ||
36 | return cpu_id; | |
37 | } | |
38 | ||
4adc8274 | 39 | uint64_t get_context_tid(const struct bt_ctf_event *event) |
1dec520a | 40 | { |
2e0a1190 | 41 | const struct bt_definition *scope; |
1dec520a JD |
42 | uint64_t tid; |
43 | ||
44 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
45 | tid = bt_ctf_get_int64(bt_ctf_get_field(event, | |
46 | scope, "_tid")); | |
47 | if (bt_ctf_field_get_error()) { | |
48 | fprintf(stderr, "Missing tid context info\n"); | |
49 | return -1ULL; | |
50 | } | |
51 | ||
52 | return tid; | |
53 | } | |
54 | ||
4adc8274 | 55 | uint64_t get_context_pid(const struct bt_ctf_event *event) |
1dec520a | 56 | { |
2e0a1190 | 57 | const struct bt_definition *scope; |
1dec520a JD |
58 | uint64_t pid; |
59 | ||
60 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
61 | pid = bt_ctf_get_int64(bt_ctf_get_field(event, | |
62 | scope, "_pid")); | |
63 | if (bt_ctf_field_get_error()) { | |
64 | fprintf(stderr, "Missing pid context info\n"); | |
65 | return -1ULL; | |
66 | } | |
67 | ||
68 | return pid; | |
69 | } | |
70 | ||
4adc8274 | 71 | uint64_t get_context_ppid(const struct bt_ctf_event *event) |
1dec520a | 72 | { |
2e0a1190 | 73 | const struct bt_definition *scope; |
1dec520a JD |
74 | uint64_t ppid; |
75 | ||
76 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
77 | ppid = bt_ctf_get_int64(bt_ctf_get_field(event, | |
78 | scope, "_ppid")); | |
79 | if (bt_ctf_field_get_error()) { | |
80 | fprintf(stderr, "Missing ppid context info\n"); | |
81 | return -1ULL; | |
82 | } | |
83 | ||
84 | return ppid; | |
85 | } | |
86 | ||
1402044a JD |
87 | uint64_t get_context_vtid(const struct bt_ctf_event *event) |
88 | { | |
89 | const struct definition *scope; | |
90 | uint64_t vtid; | |
91 | ||
92 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
93 | vtid = bt_ctf_get_int64(bt_ctf_get_field(event, | |
94 | scope, "_vtid")); | |
95 | if (bt_ctf_field_get_error()) { | |
96 | return -1ULL; | |
97 | } | |
98 | ||
99 | return vtid; | |
100 | } | |
101 | ||
102 | uint64_t get_context_vpid(const struct bt_ctf_event *event) | |
103 | { | |
104 | const struct definition *scope; | |
105 | uint64_t vpid; | |
106 | ||
107 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
108 | vpid = bt_ctf_get_int64(bt_ctf_get_field(event, | |
109 | scope, "_vpid")); | |
110 | if (bt_ctf_field_get_error()) { | |
111 | return -1ULL; | |
112 | } | |
113 | ||
114 | return vpid; | |
115 | } | |
116 | ||
117 | uint64_t get_context_vppid(const struct bt_ctf_event *event) | |
118 | { | |
119 | const struct definition *scope; | |
120 | uint64_t vppid; | |
121 | ||
122 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
123 | vppid = bt_ctf_get_int64(bt_ctf_get_field(event, | |
124 | scope, "_vppid")); | |
125 | if (bt_ctf_field_get_error()) { | |
126 | return -1ULL; | |
127 | } | |
128 | ||
129 | return vppid; | |
130 | } | |
131 | ||
4adc8274 | 132 | char *get_context_comm(const struct bt_ctf_event *event) |
1dec520a | 133 | { |
2e0a1190 | 134 | const struct bt_definition *scope; |
1dec520a JD |
135 | char *comm; |
136 | ||
137 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
138 | comm = bt_ctf_get_char_array(bt_ctf_get_field(event, | |
139 | scope, "_procname")); | |
140 | if (bt_ctf_field_get_error()) { | |
141 | fprintf(stderr, "Missing comm context info\n"); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | return comm; | |
146 | } | |
147 | ||
c8d75a13 JD |
148 | char *get_context_hostname(const struct bt_ctf_event *event) |
149 | { | |
150 | const struct definition *scope; | |
151 | char *hostname; | |
152 | ||
153 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); | |
154 | hostname = bt_ctf_get_char_array(bt_ctf_get_field(event, | |
155 | scope, "_hostname")); | |
156 | if (bt_ctf_field_get_error()) { | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | return hostname; | |
161 | } | |
162 | ||
59288610 MB |
163 | /* |
164 | * To get the parent process, put the pid in the tid field | |
165 | * because the parent process gets pid = tid | |
59288610 | 166 | */ |
1fc22eb4 JD |
167 | struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm) |
168 | { | |
1fc22eb4 JD |
169 | struct processtop *tmp; |
170 | ||
30b646c4 JD |
171 | tmp = g_hash_table_lookup(ctx->process_hash_table, |
172 | (gconstpointer) (unsigned long) tid); | |
173 | ||
174 | return tmp; | |
1fc22eb4 JD |
175 | } |
176 | ||
177 | struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm, | |
178 | unsigned long timestamp) | |
179 | { | |
180 | struct processtop *newproc; | |
181 | ||
182 | /* if the PID already exists, we just rename the process */ | |
183 | /* FIXME : need to integrate with clone/fork/exit to be accurate */ | |
184 | newproc = find_process_tid(ctx, tid, comm); | |
96aa77de | 185 | |
1fc22eb4 | 186 | if (!newproc) { |
559c9f86 | 187 | newproc = g_new0(struct processtop, 1); |
1fc22eb4 JD |
188 | newproc->tid = tid; |
189 | newproc->birth = timestamp; | |
190 | newproc->process_files_table = g_ptr_array_new(); | |
ceb3a221 | 191 | newproc->files_history = NULL; |
b093de8a MB |
192 | newproc->totalfileread = 0; |
193 | newproc->totalfilewrite = 0; | |
194 | newproc->fileread = 0; | |
195 | newproc->filewrite = 0; | |
196 | newproc->syscall_info = NULL; | |
59288610 | 197 | newproc->threadparent = NULL; |
1fc22eb4 | 198 | newproc->threads = g_ptr_array_new(); |
85db4618 | 199 | newproc->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 | 200 | g_ptr_array_add(ctx->process_table, newproc); |
30b646c4 JD |
201 | g_hash_table_insert(ctx->process_hash_table, |
202 | (gpointer) (unsigned long) tid, newproc); | |
e05a35a6 JD |
203 | |
204 | ctx->nbnewthreads++; | |
205 | ctx->nbthreads++; | |
1fc22eb4 JD |
206 | } |
207 | newproc->comm = strdup(comm); | |
208 | ||
209 | return newproc; | |
210 | } | |
211 | ||
212 | struct processtop* update_proc(struct processtop* proc, int pid, int tid, | |
c8d75a13 | 213 | int ppid, int vpid, int vtid, int vppid, char *comm, char *hostname) |
1fc22eb4 JD |
214 | { |
215 | if (proc) { | |
216 | proc->pid = pid; | |
217 | proc->tid = tid; | |
218 | proc->ppid = ppid; | |
1402044a JD |
219 | proc->vpid = vpid; |
220 | proc->vtid = vtid; | |
221 | proc->vppid = vppid; | |
1fc22eb4 JD |
222 | if (strcmp(proc->comm, comm) != 0) { |
223 | free(proc->comm); | |
224 | proc->comm = strdup(comm); | |
225 | } | |
c8d75a13 JD |
226 | if (hostname) { |
227 | if (proc->hostname && strcmp(proc->hostname, hostname) != 0) { | |
228 | free(proc->hostname); | |
229 | } | |
230 | proc->hostname = strdup(hostname); | |
231 | } | |
1fc22eb4 JD |
232 | } |
233 | return proc; | |
234 | } | |
235 | ||
236 | /* | |
237 | * This function just sets the time of death of a process. | |
238 | * When we rotate the cputime we remove it from the process list. | |
239 | */ | |
240 | void death_proc(struct lttngtop *ctx, int tid, char *comm, | |
241 | unsigned long timestamp) | |
242 | { | |
243 | struct processtop *tmp; | |
244 | tmp = find_process_tid(ctx, tid, comm); | |
30b646c4 JD |
245 | |
246 | g_hash_table_remove(ctx->process_hash_table, | |
247 | (gpointer) (unsigned long) tid); | |
e05a35a6 | 248 | if (tmp && strcmp(tmp->comm, comm) == 0) { |
1fc22eb4 | 249 | tmp->death = timestamp; |
e05a35a6 JD |
250 | ctx->nbdeadthreads++; |
251 | ctx->nbthreads--; | |
252 | } | |
1fc22eb4 JD |
253 | } |
254 | ||
255 | struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm, | |
256 | unsigned long timestamp) | |
257 | { | |
258 | struct processtop *tmp; | |
259 | tmp = find_process_tid(ctx, tid, comm); | |
260 | if (tmp && strcmp(tmp->comm, comm) == 0) | |
261 | return tmp; | |
262 | return add_proc(ctx, tid, comm, timestamp); | |
263 | } | |
264 | ||
59288610 MB |
265 | struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid, |
266 | unsigned long timestamp) | |
267 | { | |
268 | struct processtop *tmp; | |
269 | tmp = find_process_tid(ctx, tid, NULL); | |
270 | if (tmp && tmp->pid == pid) | |
271 | return tmp; | |
272 | return add_proc(ctx, tid, "Unknown", timestamp); | |
273 | } | |
274 | ||
1fc22eb4 JD |
275 | void add_thread(struct processtop *parent, struct processtop *thread) |
276 | { | |
277 | gint i; | |
278 | struct processtop *tmp; | |
279 | ||
96aa77de JD |
280 | if (!parent) |
281 | return; | |
282 | ||
1fc22eb4 JD |
283 | for (i = 0; i < parent->threads->len; i++) { |
284 | tmp = g_ptr_array_index(parent->threads, i); | |
285 | if (tmp == thread) | |
286 | return; | |
287 | } | |
288 | g_ptr_array_add(parent->threads, thread); | |
289 | } | |
290 | ||
291 | struct cputime* add_cpu(int cpu) | |
292 | { | |
293 | struct cputime *newcpu; | |
294 | ||
559c9f86 | 295 | newcpu = g_new0(struct cputime, 1); |
1fc22eb4 JD |
296 | newcpu->id = cpu; |
297 | newcpu->current_task = NULL; | |
85db4618 | 298 | newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
299 | |
300 | g_ptr_array_add(lttngtop.cpu_table, newcpu); | |
301 | ||
302 | return newcpu; | |
303 | } | |
304 | struct cputime* get_cpu(int cpu) | |
305 | { | |
306 | gint i; | |
307 | struct cputime *tmp; | |
308 | ||
309 | for (i = 0; i < lttngtop.cpu_table->len; i++) { | |
310 | tmp = g_ptr_array_index(lttngtop.cpu_table, i); | |
311 | if (tmp->id == cpu) | |
312 | return tmp; | |
313 | } | |
314 | ||
315 | return add_cpu(cpu); | |
316 | } | |
317 | ||
318 | /* | |
319 | * At the end of a sampling period, we need to display the cpu time for each | |
320 | * process and to reset it to zero for the next period | |
321 | */ | |
322 | void rotate_cputime(unsigned long end) | |
323 | { | |
324 | gint i; | |
325 | struct cputime *tmp; | |
326 | unsigned long elapsed; | |
327 | ||
328 | for (i = 0; i < lttngtop.cpu_table->len; i++) { | |
329 | tmp = g_ptr_array_index(lttngtop.cpu_table, i); | |
330 | elapsed = end - tmp->task_start; | |
331 | if (tmp->current_task) { | |
332 | tmp->current_task->totalcpunsec += elapsed; | |
333 | tmp->current_task->threadstotalcpunsec += elapsed; | |
334 | if (tmp->current_task->pid != tmp->current_task->tid && | |
335 | tmp->current_task->threadparent) { | |
336 | tmp->current_task->threadparent->threadstotalcpunsec += elapsed; | |
337 | } | |
338 | } | |
339 | tmp->task_start = end; | |
340 | } | |
341 | } | |
342 | ||
343 | void reset_perf_counter(gpointer key, gpointer value, gpointer user_data) | |
344 | { | |
345 | ((struct perfcounter*) value)->count = 0; | |
346 | } | |
347 | ||
348 | void copy_perf_counter(gpointer key, gpointer value, gpointer new_table) | |
349 | { | |
350 | struct perfcounter *newperf; | |
59288610 | 351 | |
559c9f86 | 352 | newperf = g_new0(struct perfcounter, 1); |
1fc22eb4 JD |
353 | newperf->count = ((struct perfcounter *) value)->count; |
354 | newperf->visible = ((struct perfcounter *) value)->visible; | |
355 | newperf->sort = ((struct perfcounter *) value)->sort; | |
85db4618 | 356 | g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf); |
1fc22eb4 JD |
357 | } |
358 | ||
30b646c4 JD |
359 | void copy_process_table(gpointer key, gpointer value, gpointer new_table) |
360 | { | |
361 | g_hash_table_insert((GHashTable *) new_table, key, value); | |
362 | } | |
363 | ||
1fc22eb4 JD |
364 | void rotate_perfcounter() { |
365 | int i; | |
366 | struct processtop *tmp; | |
367 | for (i = 0; i < lttngtop.process_table->len; i++) { | |
368 | tmp = g_ptr_array_index(lttngtop.process_table, i); | |
369 | g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL); | |
370 | } | |
371 | } | |
372 | ||
373 | void cleanup_processtop() | |
374 | { | |
b093de8a | 375 | gint i, j; |
1fc22eb4 | 376 | struct processtop *tmp; |
b093de8a | 377 | struct files *tmpf; /* a temporary file */ |
1fc22eb4 JD |
378 | |
379 | for (i = 0; i < lttngtop.process_table->len; i++) { | |
380 | tmp = g_ptr_array_index(lttngtop.process_table, i); | |
381 | tmp->totalcpunsec = 0; | |
382 | tmp->threadstotalcpunsec = 0; | |
b093de8a MB |
383 | tmp->fileread = 0; |
384 | tmp->filewrite = 0; | |
385 | ||
386 | for (j = 0; j < tmp->process_files_table->len; j++) { | |
387 | tmpf = g_ptr_array_index(tmp->process_files_table, j); | |
388 | if (tmpf != NULL) { | |
389 | tmpf->read = 0; | |
390 | tmpf->write = 0; | |
ceb3a221 MB |
391 | |
392 | if (tmpf->flag == __NR_close) | |
393 | g_ptr_array_index( | |
394 | tmp->process_files_table, j | |
395 | ) = NULL; | |
b093de8a MB |
396 | } |
397 | } | |
1fc22eb4 JD |
398 | } |
399 | } | |
400 | ||
e05a35a6 JD |
401 | void reset_global_counters() |
402 | { | |
403 | lttngtop.nbnewproc = 0; | |
404 | lttngtop.nbdeadproc = 0; | |
405 | lttngtop.nbnewthreads = 0; | |
406 | lttngtop.nbdeadthreads = 0; | |
407 | lttngtop.nbnewfiles = 0; | |
408 | lttngtop.nbclosedfiles = 0; | |
409 | } | |
410 | ||
411 | void copy_global_counters(struct lttngtop *dst) | |
412 | { | |
413 | dst->nbproc = lttngtop.nbproc; | |
414 | dst->nbnewproc = lttngtop.nbnewproc; | |
415 | dst->nbdeadproc = lttngtop.nbdeadproc; | |
416 | dst->nbthreads = lttngtop.nbthreads; | |
417 | dst->nbnewthreads = lttngtop.nbnewthreads; | |
418 | dst->nbdeadthreads = lttngtop.nbdeadthreads; | |
419 | dst->nbfiles = lttngtop.nbfiles; | |
420 | dst->nbnewfiles = lttngtop.nbnewfiles; | |
421 | dst->nbclosedfiles = lttngtop.nbclosedfiles; | |
422 | reset_global_counters(); | |
423 | } | |
424 | ||
1fc22eb4 JD |
425 | struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end) |
426 | { | |
427 | gint i, j; | |
428 | unsigned long time; | |
429 | struct lttngtop *dst; | |
430 | struct processtop *tmp, *tmp2, *new; | |
431 | struct cputime *tmpcpu, *newcpu; | |
432 | struct files *tmpfile, *newfile; | |
433 | ||
559c9f86 | 434 | dst = g_new0(struct lttngtop, 1); |
1fc22eb4 JD |
435 | dst->start = start; |
436 | dst->end = end; | |
e05a35a6 | 437 | copy_global_counters(dst); |
1fc22eb4 JD |
438 | dst->process_table = g_ptr_array_new(); |
439 | dst->files_table = g_ptr_array_new(); | |
440 | dst->cpu_table = g_ptr_array_new(); | |
30b646c4 JD |
441 | dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal); |
442 | g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table, | |
443 | dst->process_hash_table); | |
1fc22eb4 JD |
444 | |
445 | rotate_cputime(end); | |
446 | ||
1fc22eb4 JD |
447 | for (i = 0; i < lttngtop.process_table->len; i++) { |
448 | tmp = g_ptr_array_index(lttngtop.process_table, i); | |
559c9f86 | 449 | new = g_new0(struct processtop, 1); |
1fc22eb4 JD |
450 | |
451 | memcpy(new, tmp, sizeof(struct processtop)); | |
452 | new->threads = g_ptr_array_new(); | |
453 | new->comm = strdup(tmp->comm); | |
454 | new->process_files_table = g_ptr_array_new(); | |
ceb3a221 | 455 | new->files_history = tmp->files_history; |
85db4618 | 456 | new->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
457 | g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf); |
458 | ||
1fc22eb4 | 459 | /* compute the stream speed */ |
85db4618 JD |
460 | if (end - start != 0) { |
461 | time = (end - start) / NSEC_PER_SEC; | |
b093de8a MB |
462 | new->fileread = new->fileread/(time); |
463 | new->filewrite = new->filewrite/(time); | |
1fc22eb4 JD |
464 | } |
465 | ||
466 | for (j = 0; j < tmp->process_files_table->len; j++) { | |
467 | tmpfile = g_ptr_array_index(tmp->process_files_table, j); | |
1fc22eb4 | 468 | |
b093de8a MB |
469 | newfile = malloc(sizeof(struct files)); |
470 | ||
471 | if (tmpfile != NULL) { | |
472 | memcpy(newfile, tmpfile, sizeof(struct files)); | |
473 | newfile->name = strdup(tmpfile->name); | |
474 | newfile->ref = new; | |
475 | g_ptr_array_add(new->process_files_table, | |
476 | newfile); | |
477 | g_ptr_array_add(dst->files_table, newfile); | |
478 | } else { | |
479 | g_ptr_array_add(new->process_files_table, NULL); | |
480 | g_ptr_array_add(dst->files_table, NULL); | |
481 | } | |
1fc22eb4 JD |
482 | /* |
483 | * if the process died during the last period, we remove all | |
484 | * files associated with if after the copy | |
485 | */ | |
486 | if (tmp->death > 0 && tmp->death < end) { | |
e05a35a6 | 487 | /* FIXME : close the files before */ |
1fc22eb4 | 488 | g_ptr_array_remove(tmp->process_files_table, tmpfile); |
559c9f86 | 489 | g_free(tmpfile); |
1fc22eb4 JD |
490 | } |
491 | } | |
492 | g_ptr_array_add(dst->process_table, new); | |
493 | ||
494 | /* | |
495 | * if the process died during the last period, we remove it from | |
496 | * the current process list after the copy | |
497 | */ | |
498 | if (tmp->death > 0 && tmp->death < end) { | |
499 | g_ptr_array_remove(lttngtop.process_table, tmp); | |
85db4618 | 500 | /* FIXME : TRUE does not mean clears the object in it */ |
1fc22eb4 JD |
501 | g_ptr_array_free(tmp->threads, TRUE); |
502 | free(tmp->comm); | |
503 | g_ptr_array_free(tmp->process_files_table, TRUE); | |
85db4618 | 504 | /* FIXME : clear elements */ |
1fc22eb4 | 505 | g_hash_table_destroy(tmp->perf); |
559c9f86 | 506 | g_free(tmp); |
1fc22eb4 JD |
507 | } |
508 | } | |
509 | rotate_perfcounter(); | |
510 | ||
511 | for (i = 0; i < lttngtop.cpu_table->len; i++) { | |
512 | tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i); | |
559c9f86 | 513 | newcpu = g_new0(struct cputime, 1); |
1fc22eb4 | 514 | memcpy(newcpu, tmpcpu, sizeof(struct cputime)); |
85db4618 | 515 | newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
516 | g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf); |
517 | /* | |
518 | * note : we don't care about the current process pointer in the copy | |
519 | * so the reference is invalid after the memcpy | |
520 | */ | |
521 | g_ptr_array_add(dst->cpu_table, newcpu); | |
522 | } | |
85db4618 | 523 | /* FIXME : better algo */ |
1fc22eb4 JD |
524 | /* create the threads index if required */ |
525 | for (i = 0; i < dst->process_table->len; i++) { | |
526 | tmp = g_ptr_array_index(dst->process_table, i); | |
527 | if (tmp->pid == tmp->tid) { | |
528 | for (j = 0; j < dst->process_table->len; j++) { | |
529 | tmp2 = g_ptr_array_index(dst->process_table, j); | |
530 | if (tmp2->pid == tmp->pid) { | |
531 | tmp2->threadparent = tmp; | |
532 | g_ptr_array_add(tmp->threads, tmp2); | |
533 | } | |
534 | } | |
535 | } | |
536 | } | |
537 | ||
538 | // update_global_stats(dst); | |
539 | cleanup_processtop(); | |
540 | ||
541 | return dst; | |
542 | } | |
543 | ||
928f18a6 MB |
544 | |
545 | enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data, | |
546 | void *private_data) | |
547 | { | |
2e0a1190 | 548 | const struct bt_definition *scope; |
928f18a6 MB |
549 | struct processtop *proc; |
550 | unsigned long timestamp; | |
11d218ce | 551 | int64_t pid, tid, ppid, vtid, vpid, vppid; |
928f18a6 MB |
552 | char *procname; |
553 | ||
c78f2cdc | 554 | timestamp = bt_ctf_get_timestamp(call_data); |
928f18a6 MB |
555 | if (timestamp == -1ULL) |
556 | goto error; | |
557 | ||
558 | scope = bt_ctf_get_top_level_scope(call_data, | |
559 | BT_EVENT_FIELDS); | |
560 | pid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
561 | scope, "_pid")); | |
562 | if (bt_ctf_field_get_error()) { | |
563 | fprintf(stderr, "Missing pid context info\n"); | |
564 | goto error; | |
565 | } | |
11d218ce JD |
566 | ppid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
567 | scope, "_ppid")); | |
568 | if (bt_ctf_field_get_error()) { | |
b7194a4e | 569 | fprintf(stderr, "Missing ppid context info\n"); |
11d218ce JD |
570 | goto error; |
571 | } | |
928f18a6 MB |
572 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
573 | scope, "_tid")); | |
574 | if (bt_ctf_field_get_error()) { | |
575 | fprintf(stderr, "Missing tid context info\n"); | |
576 | goto error; | |
577 | } | |
11d218ce JD |
578 | vtid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
579 | scope, "_vtid")); | |
580 | if (bt_ctf_field_get_error()) { | |
581 | fprintf(stderr, "Missing vtid context info\n"); | |
582 | goto error; | |
583 | } | |
584 | vpid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
585 | scope, "_vpid")); | |
586 | if (bt_ctf_field_get_error()) { | |
b7194a4e | 587 | fprintf(stderr, "Missing vpid context info\n"); |
11d218ce JD |
588 | goto error; |
589 | } | |
590 | vppid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
591 | scope, "_vppid")); | |
592 | if (bt_ctf_field_get_error()) { | |
b7194a4e | 593 | fprintf(stderr, "Missing vppid context info\n"); |
11d218ce JD |
594 | goto error; |
595 | } | |
596 | ||
928f18a6 MB |
597 | scope = bt_ctf_get_top_level_scope(call_data, |
598 | BT_EVENT_FIELDS); | |
599 | procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
600 | scope, "_name")); | |
601 | if (bt_ctf_field_get_error()) { | |
602 | fprintf(stderr, "Missing process name context info\n"); | |
603 | goto error; | |
604 | } | |
605 | ||
606 | proc = find_process_tid(<tngtop, tid, procname); | |
607 | if (proc == NULL) | |
608 | proc = add_proc(<tngtop, tid, procname, timestamp); | |
c8d75a13 | 609 | update_proc(proc, pid, tid, ppid, vpid, vtid, vppid, procname, NULL); |
928f18a6 | 610 | |
96aa77de JD |
611 | if (proc) { |
612 | free(proc->comm); | |
613 | proc->comm = strdup(procname); | |
614 | proc->pid = pid; | |
615 | } | |
928f18a6 | 616 | |
928f18a6 MB |
617 | return BT_CB_OK; |
618 | ||
619 | error: | |
620 | return BT_CB_ERROR_STOP; | |
621 | } | |
b520ab45 JD |
622 | |
623 | struct tm format_timestamp(uint64_t timestamp) | |
624 | { | |
625 | struct tm tm; | |
626 | uint64_t ts_sec = 0, ts_nsec; | |
627 | time_t time_s; | |
628 | ||
629 | ts_nsec = timestamp; | |
630 | ts_sec += ts_nsec / NSEC_PER_SEC; | |
631 | ts_nsec = ts_nsec % NSEC_PER_SEC; | |
632 | ||
633 | time_s = (time_t) ts_sec; | |
634 | ||
635 | localtime_r(&time_s, &tm); | |
636 | ||
637 | return tm; | |
638 | } | |
57bff788 JD |
639 | |
640 | int *lookup_tid_list(int tid) | |
641 | { | |
642 | return g_hash_table_lookup(tid_list, (gpointer) &tid); | |
643 | } | |
c8d75a13 JD |
644 | |
645 | char *lookup_hostname_list(const char *hostname) | |
646 | { | |
647 | if (!hostname) | |
648 | return NULL; | |
649 | ||
650 | return g_hash_table_lookup(hostname_list, (gpointer) hostname); | |
651 | } |