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