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