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, | |
906c08f6 | 178 | unsigned long timestamp, char *hostname) |
1fc22eb4 JD |
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); | |
da4353bb JD |
203 | if (lookup_tid_list(tid)) { |
204 | add_filter_tid_list(tid, newproc); | |
205 | } | |
e05a35a6 JD |
206 | ctx->nbnewthreads++; |
207 | ctx->nbthreads++; | |
1fc22eb4 JD |
208 | } |
209 | newproc->comm = strdup(comm); | |
906c08f6 JD |
210 | if (hostname) { |
211 | if (newproc->hostname && strcmp(newproc->hostname, hostname) != 0) { | |
212 | free(newproc->hostname); | |
213 | } | |
214 | newproc->hostname = strdup(hostname); | |
215 | if (lookup_hostname_list(hostname)) { | |
216 | add_filter_tid_list(tid, newproc); | |
217 | } | |
218 | } | |
1fc22eb4 JD |
219 | |
220 | return newproc; | |
221 | } | |
222 | ||
223 | struct processtop* update_proc(struct processtop* proc, int pid, int tid, | |
c8d75a13 | 224 | int ppid, int vpid, int vtid, int vppid, char *comm, char *hostname) |
1fc22eb4 JD |
225 | { |
226 | if (proc) { | |
227 | proc->pid = pid; | |
228 | proc->tid = tid; | |
229 | proc->ppid = ppid; | |
1402044a JD |
230 | proc->vpid = vpid; |
231 | proc->vtid = vtid; | |
232 | proc->vppid = vppid; | |
1fc22eb4 JD |
233 | if (strcmp(proc->comm, comm) != 0) { |
234 | free(proc->comm); | |
235 | proc->comm = strdup(comm); | |
236 | } | |
1d2391b4 JD |
237 | if (hostname && !proc->hostname) { |
238 | proc->hostname = strdup(hostname); | |
239 | if (lookup_hostname_list(hostname)) { | |
240 | add_filter_tid_list(tid, proc); | |
241 | } | |
242 | } | |
1fc22eb4 JD |
243 | } |
244 | return proc; | |
245 | } | |
246 | ||
247 | /* | |
248 | * This function just sets the time of death of a process. | |
249 | * When we rotate the cputime we remove it from the process list. | |
250 | */ | |
251 | void death_proc(struct lttngtop *ctx, int tid, char *comm, | |
252 | unsigned long timestamp) | |
253 | { | |
254 | struct processtop *tmp; | |
255 | tmp = find_process_tid(ctx, tid, comm); | |
30b646c4 JD |
256 | |
257 | g_hash_table_remove(ctx->process_hash_table, | |
258 | (gpointer) (unsigned long) tid); | |
e05a35a6 | 259 | if (tmp && strcmp(tmp->comm, comm) == 0) { |
1fc22eb4 | 260 | tmp->death = timestamp; |
e05a35a6 JD |
261 | ctx->nbdeadthreads++; |
262 | ctx->nbthreads--; | |
263 | } | |
1fc22eb4 JD |
264 | } |
265 | ||
266 | struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm, | |
906c08f6 | 267 | unsigned long timestamp, char *hostname) |
1fc22eb4 JD |
268 | { |
269 | struct processtop *tmp; | |
1d2391b4 | 270 | |
1fc22eb4 | 271 | tmp = find_process_tid(ctx, tid, comm); |
1d2391b4 | 272 | if (tmp && strcmp(tmp->comm, comm) == 0) { |
1fc22eb4 | 273 | return tmp; |
1d2391b4 | 274 | } |
906c08f6 | 275 | return add_proc(ctx, tid, comm, timestamp, hostname); |
1fc22eb4 JD |
276 | } |
277 | ||
59288610 | 278 | struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid, |
906c08f6 | 279 | unsigned long timestamp, char *hostname) |
59288610 MB |
280 | { |
281 | struct processtop *tmp; | |
282 | tmp = find_process_tid(ctx, tid, NULL); | |
283 | if (tmp && tmp->pid == pid) | |
284 | return tmp; | |
906c08f6 | 285 | return add_proc(ctx, tid, "Unknown", timestamp, hostname); |
59288610 MB |
286 | } |
287 | ||
1fc22eb4 JD |
288 | void add_thread(struct processtop *parent, struct processtop *thread) |
289 | { | |
290 | gint i; | |
291 | struct processtop *tmp; | |
292 | ||
96aa77de JD |
293 | if (!parent) |
294 | return; | |
295 | ||
1fc22eb4 JD |
296 | for (i = 0; i < parent->threads->len; i++) { |
297 | tmp = g_ptr_array_index(parent->threads, i); | |
298 | if (tmp == thread) | |
299 | return; | |
300 | } | |
301 | g_ptr_array_add(parent->threads, thread); | |
302 | } | |
303 | ||
304 | struct cputime* add_cpu(int cpu) | |
305 | { | |
306 | struct cputime *newcpu; | |
307 | ||
559c9f86 | 308 | newcpu = g_new0(struct cputime, 1); |
1fc22eb4 JD |
309 | newcpu->id = cpu; |
310 | newcpu->current_task = NULL; | |
85db4618 | 311 | newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
312 | |
313 | g_ptr_array_add(lttngtop.cpu_table, newcpu); | |
314 | ||
315 | return newcpu; | |
316 | } | |
317 | struct cputime* get_cpu(int cpu) | |
318 | { | |
319 | gint i; | |
320 | struct cputime *tmp; | |
321 | ||
322 | for (i = 0; i < lttngtop.cpu_table->len; i++) { | |
323 | tmp = g_ptr_array_index(lttngtop.cpu_table, i); | |
324 | if (tmp->id == cpu) | |
325 | return tmp; | |
326 | } | |
327 | ||
328 | return add_cpu(cpu); | |
329 | } | |
330 | ||
331 | /* | |
332 | * At the end of a sampling period, we need to display the cpu time for each | |
333 | * process and to reset it to zero for the next period | |
334 | */ | |
335 | void rotate_cputime(unsigned long end) | |
336 | { | |
337 | gint i; | |
338 | struct cputime *tmp; | |
339 | unsigned long elapsed; | |
340 | ||
341 | for (i = 0; i < lttngtop.cpu_table->len; i++) { | |
342 | tmp = g_ptr_array_index(lttngtop.cpu_table, i); | |
343 | elapsed = end - tmp->task_start; | |
344 | if (tmp->current_task) { | |
345 | tmp->current_task->totalcpunsec += elapsed; | |
346 | tmp->current_task->threadstotalcpunsec += elapsed; | |
347 | if (tmp->current_task->pid != tmp->current_task->tid && | |
348 | tmp->current_task->threadparent) { | |
349 | tmp->current_task->threadparent->threadstotalcpunsec += elapsed; | |
350 | } | |
351 | } | |
352 | tmp->task_start = end; | |
353 | } | |
354 | } | |
355 | ||
356 | void reset_perf_counter(gpointer key, gpointer value, gpointer user_data) | |
357 | { | |
358 | ((struct perfcounter*) value)->count = 0; | |
359 | } | |
360 | ||
361 | void copy_perf_counter(gpointer key, gpointer value, gpointer new_table) | |
362 | { | |
363 | struct perfcounter *newperf; | |
59288610 | 364 | |
559c9f86 | 365 | newperf = g_new0(struct perfcounter, 1); |
1fc22eb4 JD |
366 | newperf->count = ((struct perfcounter *) value)->count; |
367 | newperf->visible = ((struct perfcounter *) value)->visible; | |
368 | newperf->sort = ((struct perfcounter *) value)->sort; | |
85db4618 | 369 | g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf); |
1fc22eb4 JD |
370 | } |
371 | ||
30b646c4 JD |
372 | void copy_process_table(gpointer key, gpointer value, gpointer new_table) |
373 | { | |
374 | g_hash_table_insert((GHashTable *) new_table, key, value); | |
375 | } | |
376 | ||
1fc22eb4 JD |
377 | void rotate_perfcounter() { |
378 | int i; | |
379 | struct processtop *tmp; | |
380 | for (i = 0; i < lttngtop.process_table->len; i++) { | |
381 | tmp = g_ptr_array_index(lttngtop.process_table, i); | |
382 | g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL); | |
383 | } | |
384 | } | |
385 | ||
386 | void cleanup_processtop() | |
387 | { | |
b093de8a | 388 | gint i, j; |
1fc22eb4 | 389 | struct processtop *tmp; |
b093de8a | 390 | struct files *tmpf; /* a temporary file */ |
1fc22eb4 JD |
391 | |
392 | for (i = 0; i < lttngtop.process_table->len; i++) { | |
393 | tmp = g_ptr_array_index(lttngtop.process_table, i); | |
394 | tmp->totalcpunsec = 0; | |
395 | tmp->threadstotalcpunsec = 0; | |
b093de8a MB |
396 | tmp->fileread = 0; |
397 | tmp->filewrite = 0; | |
398 | ||
399 | for (j = 0; j < tmp->process_files_table->len; j++) { | |
400 | tmpf = g_ptr_array_index(tmp->process_files_table, j); | |
401 | if (tmpf != NULL) { | |
402 | tmpf->read = 0; | |
403 | tmpf->write = 0; | |
ceb3a221 MB |
404 | |
405 | if (tmpf->flag == __NR_close) | |
406 | g_ptr_array_index( | |
407 | tmp->process_files_table, j | |
408 | ) = NULL; | |
b093de8a MB |
409 | } |
410 | } | |
1fc22eb4 JD |
411 | } |
412 | } | |
413 | ||
e05a35a6 JD |
414 | void reset_global_counters() |
415 | { | |
416 | lttngtop.nbnewproc = 0; | |
417 | lttngtop.nbdeadproc = 0; | |
418 | lttngtop.nbnewthreads = 0; | |
419 | lttngtop.nbdeadthreads = 0; | |
420 | lttngtop.nbnewfiles = 0; | |
421 | lttngtop.nbclosedfiles = 0; | |
422 | } | |
423 | ||
424 | void copy_global_counters(struct lttngtop *dst) | |
425 | { | |
426 | dst->nbproc = lttngtop.nbproc; | |
427 | dst->nbnewproc = lttngtop.nbnewproc; | |
428 | dst->nbdeadproc = lttngtop.nbdeadproc; | |
429 | dst->nbthreads = lttngtop.nbthreads; | |
430 | dst->nbnewthreads = lttngtop.nbnewthreads; | |
431 | dst->nbdeadthreads = lttngtop.nbdeadthreads; | |
432 | dst->nbfiles = lttngtop.nbfiles; | |
433 | dst->nbnewfiles = lttngtop.nbnewfiles; | |
434 | dst->nbclosedfiles = lttngtop.nbclosedfiles; | |
435 | reset_global_counters(); | |
436 | } | |
437 | ||
1fc22eb4 JD |
438 | struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end) |
439 | { | |
440 | gint i, j; | |
441 | unsigned long time; | |
442 | struct lttngtop *dst; | |
443 | struct processtop *tmp, *tmp2, *new; | |
444 | struct cputime *tmpcpu, *newcpu; | |
445 | struct files *tmpfile, *newfile; | |
246d5992 | 446 | struct kprobes *tmpprobe, *newprobe; |
1fc22eb4 | 447 | |
559c9f86 | 448 | dst = g_new0(struct lttngtop, 1); |
1fc22eb4 JD |
449 | dst->start = start; |
450 | dst->end = end; | |
e05a35a6 | 451 | copy_global_counters(dst); |
1fc22eb4 JD |
452 | dst->process_table = g_ptr_array_new(); |
453 | dst->files_table = g_ptr_array_new(); | |
454 | dst->cpu_table = g_ptr_array_new(); | |
246d5992 | 455 | dst->kprobes_table = g_ptr_array_new(); |
30b646c4 JD |
456 | dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal); |
457 | g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table, | |
458 | dst->process_hash_table); | |
1fc22eb4 JD |
459 | |
460 | rotate_cputime(end); | |
461 | ||
1fc22eb4 JD |
462 | for (i = 0; i < lttngtop.process_table->len; i++) { |
463 | tmp = g_ptr_array_index(lttngtop.process_table, i); | |
559c9f86 | 464 | new = g_new0(struct processtop, 1); |
1fc22eb4 JD |
465 | |
466 | memcpy(new, tmp, sizeof(struct processtop)); | |
467 | new->threads = g_ptr_array_new(); | |
468 | new->comm = strdup(tmp->comm); | |
469 | new->process_files_table = g_ptr_array_new(); | |
ceb3a221 | 470 | new->files_history = tmp->files_history; |
85db4618 | 471 | new->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
472 | g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf); |
473 | ||
1fc22eb4 | 474 | /* compute the stream speed */ |
85db4618 JD |
475 | if (end - start != 0) { |
476 | time = (end - start) / NSEC_PER_SEC; | |
b093de8a MB |
477 | new->fileread = new->fileread/(time); |
478 | new->filewrite = new->filewrite/(time); | |
1fc22eb4 JD |
479 | } |
480 | ||
481 | for (j = 0; j < tmp->process_files_table->len; j++) { | |
482 | tmpfile = g_ptr_array_index(tmp->process_files_table, j); | |
1fc22eb4 | 483 | |
b093de8a MB |
484 | newfile = malloc(sizeof(struct files)); |
485 | ||
486 | if (tmpfile != NULL) { | |
487 | memcpy(newfile, tmpfile, sizeof(struct files)); | |
488 | newfile->name = strdup(tmpfile->name); | |
489 | newfile->ref = new; | |
490 | g_ptr_array_add(new->process_files_table, | |
491 | newfile); | |
492 | g_ptr_array_add(dst->files_table, newfile); | |
493 | } else { | |
494 | g_ptr_array_add(new->process_files_table, NULL); | |
495 | g_ptr_array_add(dst->files_table, NULL); | |
496 | } | |
1fc22eb4 JD |
497 | /* |
498 | * if the process died during the last period, we remove all | |
499 | * files associated with if after the copy | |
500 | */ | |
501 | if (tmp->death > 0 && tmp->death < end) { | |
e05a35a6 | 502 | /* FIXME : close the files before */ |
1fc22eb4 | 503 | g_ptr_array_remove(tmp->process_files_table, tmpfile); |
559c9f86 | 504 | g_free(tmpfile); |
1fc22eb4 JD |
505 | } |
506 | } | |
507 | g_ptr_array_add(dst->process_table, new); | |
508 | ||
509 | /* | |
510 | * if the process died during the last period, we remove it from | |
511 | * the current process list after the copy | |
512 | */ | |
513 | if (tmp->death > 0 && tmp->death < end) { | |
514 | g_ptr_array_remove(lttngtop.process_table, tmp); | |
85db4618 | 515 | /* FIXME : TRUE does not mean clears the object in it */ |
1fc22eb4 JD |
516 | g_ptr_array_free(tmp->threads, TRUE); |
517 | free(tmp->comm); | |
518 | g_ptr_array_free(tmp->process_files_table, TRUE); | |
85db4618 | 519 | /* FIXME : clear elements */ |
1fc22eb4 | 520 | g_hash_table_destroy(tmp->perf); |
559c9f86 | 521 | g_free(tmp); |
1fc22eb4 JD |
522 | } |
523 | } | |
524 | rotate_perfcounter(); | |
525 | ||
526 | for (i = 0; i < lttngtop.cpu_table->len; i++) { | |
527 | tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i); | |
559c9f86 | 528 | newcpu = g_new0(struct cputime, 1); |
1fc22eb4 | 529 | memcpy(newcpu, tmpcpu, sizeof(struct cputime)); |
85db4618 | 530 | newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal); |
1fc22eb4 JD |
531 | g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf); |
532 | /* | |
533 | * note : we don't care about the current process pointer in the copy | |
534 | * so the reference is invalid after the memcpy | |
535 | */ | |
536 | g_ptr_array_add(dst->cpu_table, newcpu); | |
537 | } | |
da4353bb JD |
538 | if (lttngtop.kprobes_table) { |
539 | for (i = 0; i < lttngtop.kprobes_table->len; i++) { | |
540 | tmpprobe = g_ptr_array_index(lttngtop.kprobes_table, i); | |
541 | newprobe = g_new0(struct kprobes, 1); | |
542 | memcpy(newprobe, tmpprobe, sizeof(struct kprobes)); | |
543 | tmpprobe->count = 0; | |
544 | g_ptr_array_add(dst->kprobes_table, newprobe); | |
545 | } | |
246d5992 | 546 | } |
85db4618 | 547 | /* FIXME : better algo */ |
1fc22eb4 JD |
548 | /* create the threads index if required */ |
549 | for (i = 0; i < dst->process_table->len; i++) { | |
550 | tmp = g_ptr_array_index(dst->process_table, i); | |
551 | if (tmp->pid == tmp->tid) { | |
552 | for (j = 0; j < dst->process_table->len; j++) { | |
553 | tmp2 = g_ptr_array_index(dst->process_table, j); | |
554 | if (tmp2->pid == tmp->pid) { | |
555 | tmp2->threadparent = tmp; | |
556 | g_ptr_array_add(tmp->threads, tmp2); | |
557 | } | |
558 | } | |
559 | } | |
560 | } | |
561 | ||
562 | // update_global_stats(dst); | |
563 | cleanup_processtop(); | |
564 | ||
565 | return dst; | |
566 | } | |
567 | ||
928f18a6 MB |
568 | |
569 | enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data, | |
570 | void *private_data) | |
571 | { | |
2e0a1190 | 572 | const struct bt_definition *scope; |
928f18a6 MB |
573 | struct processtop *proc; |
574 | unsigned long timestamp; | |
11d218ce | 575 | int64_t pid, tid, ppid, vtid, vpid, vppid; |
1d2391b4 | 576 | char *procname, *hostname = NULL; |
928f18a6 | 577 | |
c78f2cdc | 578 | timestamp = bt_ctf_get_timestamp(call_data); |
928f18a6 MB |
579 | if (timestamp == -1ULL) |
580 | goto error; | |
581 | ||
582 | scope = bt_ctf_get_top_level_scope(call_data, | |
583 | BT_EVENT_FIELDS); | |
584 | pid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
585 | scope, "_pid")); | |
586 | if (bt_ctf_field_get_error()) { | |
587 | fprintf(stderr, "Missing pid context info\n"); | |
588 | goto error; | |
589 | } | |
11d218ce JD |
590 | ppid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
591 | scope, "_ppid")); | |
592 | if (bt_ctf_field_get_error()) { | |
b7194a4e | 593 | fprintf(stderr, "Missing ppid context info\n"); |
11d218ce JD |
594 | goto error; |
595 | } | |
928f18a6 MB |
596 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
597 | scope, "_tid")); | |
598 | if (bt_ctf_field_get_error()) { | |
599 | fprintf(stderr, "Missing tid context info\n"); | |
600 | goto error; | |
601 | } | |
11d218ce JD |
602 | vtid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
603 | scope, "_vtid")); | |
604 | if (bt_ctf_field_get_error()) { | |
605 | fprintf(stderr, "Missing vtid context info\n"); | |
606 | goto error; | |
607 | } | |
608 | vpid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
609 | scope, "_vpid")); | |
610 | if (bt_ctf_field_get_error()) { | |
b7194a4e | 611 | fprintf(stderr, "Missing vpid context info\n"); |
11d218ce JD |
612 | goto error; |
613 | } | |
614 | vppid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
615 | scope, "_vppid")); | |
616 | if (bt_ctf_field_get_error()) { | |
b7194a4e | 617 | fprintf(stderr, "Missing vppid context info\n"); |
11d218ce JD |
618 | goto error; |
619 | } | |
620 | ||
928f18a6 MB |
621 | scope = bt_ctf_get_top_level_scope(call_data, |
622 | BT_EVENT_FIELDS); | |
623 | procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
624 | scope, "_name")); | |
625 | if (bt_ctf_field_get_error()) { | |
626 | fprintf(stderr, "Missing process name context info\n"); | |
627 | goto error; | |
628 | } | |
1d2391b4 JD |
629 | /* |
630 | hostname = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
631 | scope, "_hostname")); | |
632 | if (bt_ctf_field_get_error()) { | |
633 | } | |
634 | */ | |
928f18a6 MB |
635 | |
636 | proc = find_process_tid(<tngtop, tid, procname); | |
637 | if (proc == NULL) | |
1d2391b4 JD |
638 | proc = add_proc(<tngtop, tid, procname, timestamp, hostname); |
639 | update_proc(proc, pid, tid, ppid, vpid, vtid, vppid, procname, hostname); | |
928f18a6 | 640 | |
96aa77de JD |
641 | if (proc) { |
642 | free(proc->comm); | |
643 | proc->comm = strdup(procname); | |
644 | proc->pid = pid; | |
645 | } | |
928f18a6 | 646 | |
928f18a6 MB |
647 | return BT_CB_OK; |
648 | ||
649 | error: | |
650 | return BT_CB_ERROR_STOP; | |
651 | } | |
b520ab45 JD |
652 | |
653 | struct tm format_timestamp(uint64_t timestamp) | |
654 | { | |
655 | struct tm tm; | |
656 | uint64_t ts_sec = 0, ts_nsec; | |
657 | time_t time_s; | |
658 | ||
659 | ts_nsec = timestamp; | |
660 | ts_sec += ts_nsec / NSEC_PER_SEC; | |
661 | ts_nsec = ts_nsec % NSEC_PER_SEC; | |
662 | ||
663 | time_s = (time_t) ts_sec; | |
664 | ||
665 | localtime_r(&time_s, &tm); | |
666 | ||
667 | return tm; | |
668 | } | |
57bff788 JD |
669 | |
670 | int *lookup_tid_list(int tid) | |
671 | { | |
da4353bb JD |
672 | if (!tid_list) |
673 | return NULL; | |
674 | ||
57bff788 JD |
675 | return g_hash_table_lookup(tid_list, (gpointer) &tid); |
676 | } | |
c8d75a13 JD |
677 | |
678 | char *lookup_hostname_list(const char *hostname) | |
679 | { | |
da4353bb | 680 | if (!hostname || !hostname_list) |
c8d75a13 JD |
681 | return NULL; |
682 | ||
683 | return g_hash_table_lookup(hostname_list, (gpointer) hostname); | |
684 | } | |
da4353bb | 685 | |
906c08f6 JD |
686 | void remove_hostname_list(const char *hostname) |
687 | { | |
688 | if (!hostname || !hostname_list) | |
689 | return; | |
690 | ||
691 | g_hash_table_remove(hostname_list, (gpointer) hostname); | |
692 | } | |
693 | ||
da4353bb JD |
694 | int *lookup_filter_tid_list(int tid) |
695 | { | |
696 | return g_hash_table_lookup(global_filter_list, (gpointer) &tid); | |
697 | } | |
698 | ||
699 | void add_filter_tid_list(int tid, struct processtop *newproc) | |
700 | { | |
701 | unsigned long *hash_tid; | |
702 | ||
703 | hash_tid = malloc(sizeof(unsigned long)); | |
704 | *hash_tid = tid; | |
705 | g_hash_table_insert(global_filter_list, | |
706 | (gpointer) (unsigned long) hash_tid, newproc); | |
707 | } | |
708 | ||
709 | void remove_filter_tid_list(int tid) | |
710 | { | |
711 | g_hash_table_remove(global_filter_list, | |
712 | (gpointer) (unsigned long) &tid); | |
713 | } |