strace like working
[lttngtop.git] / src / common.c
CommitLineData
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 24uint64_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 39uint64_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 55uint64_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 71uint64_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
87uint64_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
102uint64_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
117uint64_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 132char *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
59288610
MB
148/*
149 * To get the parent process, put the pid in the tid field
150 * because the parent process gets pid = tid
59288610 151 */
1fc22eb4
JD
152struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
153{
1fc22eb4
JD
154 struct processtop *tmp;
155
30b646c4
JD
156 tmp = g_hash_table_lookup(ctx->process_hash_table,
157 (gconstpointer) (unsigned long) tid);
158
159 return tmp;
1fc22eb4
JD
160}
161
162struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
163 unsigned long timestamp)
164{
165 struct processtop *newproc;
166
167 /* if the PID already exists, we just rename the process */
168 /* FIXME : need to integrate with clone/fork/exit to be accurate */
169 newproc = find_process_tid(ctx, tid, comm);
170 if (!newproc) {
559c9f86 171 newproc = g_new0(struct processtop, 1);
1fc22eb4
JD
172 newproc->tid = tid;
173 newproc->birth = timestamp;
174 newproc->process_files_table = g_ptr_array_new();
ceb3a221 175 newproc->files_history = NULL;
b093de8a
MB
176 newproc->totalfileread = 0;
177 newproc->totalfilewrite = 0;
178 newproc->fileread = 0;
179 newproc->filewrite = 0;
180 newproc->syscall_info = NULL;
59288610 181 newproc->threadparent = NULL;
1fc22eb4 182 newproc->threads = g_ptr_array_new();
85db4618 183 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4 184 g_ptr_array_add(ctx->process_table, newproc);
30b646c4
JD
185 g_hash_table_insert(ctx->process_hash_table,
186 (gpointer) (unsigned long) tid, newproc);
e05a35a6
JD
187
188 ctx->nbnewthreads++;
189 ctx->nbthreads++;
1fc22eb4
JD
190 }
191 newproc->comm = strdup(comm);
192
193 return newproc;
194}
195
196struct processtop* update_proc(struct processtop* proc, int pid, int tid,
1402044a 197 int ppid, int vpid, int vtid, int vppid, char *comm)
1fc22eb4
JD
198{
199 if (proc) {
200 proc->pid = pid;
201 proc->tid = tid;
202 proc->ppid = ppid;
1402044a
JD
203 proc->vpid = vpid;
204 proc->vtid = vtid;
205 proc->vppid = vppid;
1fc22eb4
JD
206 if (strcmp(proc->comm, comm) != 0) {
207 free(proc->comm);
208 proc->comm = strdup(comm);
209 }
210 }
211 return proc;
212}
213
214/*
215 * This function just sets the time of death of a process.
216 * When we rotate the cputime we remove it from the process list.
217 */
218void death_proc(struct lttngtop *ctx, int tid, char *comm,
219 unsigned long timestamp)
220{
221 struct processtop *tmp;
222 tmp = find_process_tid(ctx, tid, comm);
30b646c4
JD
223
224 g_hash_table_remove(ctx->process_hash_table,
225 (gpointer) (unsigned long) tid);
e05a35a6 226 if (tmp && strcmp(tmp->comm, comm) == 0) {
1fc22eb4 227 tmp->death = timestamp;
e05a35a6
JD
228 ctx->nbdeadthreads++;
229 ctx->nbthreads--;
230 }
1fc22eb4
JD
231}
232
233struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
234 unsigned long timestamp)
235{
236 struct processtop *tmp;
237 tmp = find_process_tid(ctx, tid, comm);
238 if (tmp && strcmp(tmp->comm, comm) == 0)
239 return tmp;
240 return add_proc(ctx, tid, comm, timestamp);
241}
242
59288610
MB
243struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid,
244 unsigned long timestamp)
245{
246 struct processtop *tmp;
247 tmp = find_process_tid(ctx, tid, NULL);
248 if (tmp && tmp->pid == pid)
249 return tmp;
250 return add_proc(ctx, tid, "Unknown", timestamp);
251}
252
1fc22eb4
JD
253void add_thread(struct processtop *parent, struct processtop *thread)
254{
255 gint i;
256 struct processtop *tmp;
257
258 for (i = 0; i < parent->threads->len; i++) {
259 tmp = g_ptr_array_index(parent->threads, i);
260 if (tmp == thread)
261 return;
262 }
263 g_ptr_array_add(parent->threads, thread);
264}
265
266struct cputime* add_cpu(int cpu)
267{
268 struct cputime *newcpu;
269
559c9f86 270 newcpu = g_new0(struct cputime, 1);
1fc22eb4
JD
271 newcpu->id = cpu;
272 newcpu->current_task = NULL;
85db4618 273 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
274
275 g_ptr_array_add(lttngtop.cpu_table, newcpu);
276
277 return newcpu;
278}
279struct cputime* get_cpu(int cpu)
280{
281 gint i;
282 struct cputime *tmp;
283
284 for (i = 0; i < lttngtop.cpu_table->len; i++) {
285 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
286 if (tmp->id == cpu)
287 return tmp;
288 }
289
290 return add_cpu(cpu);
291}
292
293/*
294 * At the end of a sampling period, we need to display the cpu time for each
295 * process and to reset it to zero for the next period
296 */
297void rotate_cputime(unsigned long end)
298{
299 gint i;
300 struct cputime *tmp;
301 unsigned long elapsed;
302
303 for (i = 0; i < lttngtop.cpu_table->len; i++) {
304 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
305 elapsed = end - tmp->task_start;
306 if (tmp->current_task) {
307 tmp->current_task->totalcpunsec += elapsed;
308 tmp->current_task->threadstotalcpunsec += elapsed;
309 if (tmp->current_task->pid != tmp->current_task->tid &&
310 tmp->current_task->threadparent) {
311 tmp->current_task->threadparent->threadstotalcpunsec += elapsed;
312 }
313 }
314 tmp->task_start = end;
315 }
316}
317
318void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
319{
320 ((struct perfcounter*) value)->count = 0;
321}
322
323void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
324{
325 struct perfcounter *newperf;
59288610 326
559c9f86 327 newperf = g_new0(struct perfcounter, 1);
1fc22eb4
JD
328 newperf->count = ((struct perfcounter *) value)->count;
329 newperf->visible = ((struct perfcounter *) value)->visible;
330 newperf->sort = ((struct perfcounter *) value)->sort;
85db4618 331 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
1fc22eb4
JD
332}
333
30b646c4
JD
334void copy_process_table(gpointer key, gpointer value, gpointer new_table)
335{
336 g_hash_table_insert((GHashTable *) new_table, key, value);
337}
338
1fc22eb4
JD
339void rotate_perfcounter() {
340 int i;
341 struct processtop *tmp;
342 for (i = 0; i < lttngtop.process_table->len; i++) {
343 tmp = g_ptr_array_index(lttngtop.process_table, i);
344 g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL);
345 }
346}
347
348void cleanup_processtop()
349{
b093de8a 350 gint i, j;
1fc22eb4 351 struct processtop *tmp;
b093de8a 352 struct files *tmpf; /* a temporary file */
1fc22eb4
JD
353
354 for (i = 0; i < lttngtop.process_table->len; i++) {
355 tmp = g_ptr_array_index(lttngtop.process_table, i);
356 tmp->totalcpunsec = 0;
357 tmp->threadstotalcpunsec = 0;
b093de8a
MB
358 tmp->fileread = 0;
359 tmp->filewrite = 0;
360
361 for (j = 0; j < tmp->process_files_table->len; j++) {
362 tmpf = g_ptr_array_index(tmp->process_files_table, j);
363 if (tmpf != NULL) {
364 tmpf->read = 0;
365 tmpf->write = 0;
ceb3a221
MB
366
367 if (tmpf->flag == __NR_close)
368 g_ptr_array_index(
369 tmp->process_files_table, j
370 ) = NULL;
b093de8a
MB
371 }
372 }
1fc22eb4
JD
373 }
374}
375
e05a35a6
JD
376void reset_global_counters()
377{
378 lttngtop.nbnewproc = 0;
379 lttngtop.nbdeadproc = 0;
380 lttngtop.nbnewthreads = 0;
381 lttngtop.nbdeadthreads = 0;
382 lttngtop.nbnewfiles = 0;
383 lttngtop.nbclosedfiles = 0;
384}
385
386void copy_global_counters(struct lttngtop *dst)
387{
388 dst->nbproc = lttngtop.nbproc;
389 dst->nbnewproc = lttngtop.nbnewproc;
390 dst->nbdeadproc = lttngtop.nbdeadproc;
391 dst->nbthreads = lttngtop.nbthreads;
392 dst->nbnewthreads = lttngtop.nbnewthreads;
393 dst->nbdeadthreads = lttngtop.nbdeadthreads;
394 dst->nbfiles = lttngtop.nbfiles;
395 dst->nbnewfiles = lttngtop.nbnewfiles;
396 dst->nbclosedfiles = lttngtop.nbclosedfiles;
397 reset_global_counters();
398}
399
1fc22eb4
JD
400struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
401{
402 gint i, j;
403 unsigned long time;
404 struct lttngtop *dst;
405 struct processtop *tmp, *tmp2, *new;
406 struct cputime *tmpcpu, *newcpu;
407 struct files *tmpfile, *newfile;
408
559c9f86 409 dst = g_new0(struct lttngtop, 1);
1fc22eb4
JD
410 dst->start = start;
411 dst->end = end;
e05a35a6 412 copy_global_counters(dst);
1fc22eb4
JD
413 dst->process_table = g_ptr_array_new();
414 dst->files_table = g_ptr_array_new();
415 dst->cpu_table = g_ptr_array_new();
30b646c4
JD
416 dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
417 g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table,
418 dst->process_hash_table);
1fc22eb4
JD
419
420 rotate_cputime(end);
421
1fc22eb4
JD
422 for (i = 0; i < lttngtop.process_table->len; i++) {
423 tmp = g_ptr_array_index(lttngtop.process_table, i);
559c9f86 424 new = g_new0(struct processtop, 1);
1fc22eb4
JD
425
426 memcpy(new, tmp, sizeof(struct processtop));
427 new->threads = g_ptr_array_new();
428 new->comm = strdup(tmp->comm);
429 new->process_files_table = g_ptr_array_new();
ceb3a221 430 new->files_history = tmp->files_history;
85db4618 431 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
432 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
433
1fc22eb4 434 /* compute the stream speed */
85db4618
JD
435 if (end - start != 0) {
436 time = (end - start) / NSEC_PER_SEC;
b093de8a
MB
437 new->fileread = new->fileread/(time);
438 new->filewrite = new->filewrite/(time);
1fc22eb4
JD
439 }
440
441 for (j = 0; j < tmp->process_files_table->len; j++) {
442 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
1fc22eb4 443
b093de8a
MB
444 newfile = malloc(sizeof(struct files));
445
446 if (tmpfile != NULL) {
447 memcpy(newfile, tmpfile, sizeof(struct files));
448 newfile->name = strdup(tmpfile->name);
449 newfile->ref = new;
450 g_ptr_array_add(new->process_files_table,
451 newfile);
452 g_ptr_array_add(dst->files_table, newfile);
453 } else {
454 g_ptr_array_add(new->process_files_table, NULL);
455 g_ptr_array_add(dst->files_table, NULL);
456 }
1fc22eb4
JD
457 /*
458 * if the process died during the last period, we remove all
459 * files associated with if after the copy
460 */
461 if (tmp->death > 0 && tmp->death < end) {
e05a35a6 462 /* FIXME : close the files before */
1fc22eb4 463 g_ptr_array_remove(tmp->process_files_table, tmpfile);
559c9f86 464 g_free(tmpfile);
1fc22eb4
JD
465 }
466 }
467 g_ptr_array_add(dst->process_table, new);
468
469 /*
470 * if the process died during the last period, we remove it from
471 * the current process list after the copy
472 */
473 if (tmp->death > 0 && tmp->death < end) {
474 g_ptr_array_remove(lttngtop.process_table, tmp);
85db4618 475 /* FIXME : TRUE does not mean clears the object in it */
1fc22eb4
JD
476 g_ptr_array_free(tmp->threads, TRUE);
477 free(tmp->comm);
478 g_ptr_array_free(tmp->process_files_table, TRUE);
85db4618 479 /* FIXME : clear elements */
1fc22eb4 480 g_hash_table_destroy(tmp->perf);
559c9f86 481 g_free(tmp);
1fc22eb4
JD
482 }
483 }
484 rotate_perfcounter();
485
486 for (i = 0; i < lttngtop.cpu_table->len; i++) {
487 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
559c9f86 488 newcpu = g_new0(struct cputime, 1);
1fc22eb4 489 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
85db4618 490 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
491 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
492 /*
493 * note : we don't care about the current process pointer in the copy
494 * so the reference is invalid after the memcpy
495 */
496 g_ptr_array_add(dst->cpu_table, newcpu);
497 }
85db4618 498 /* FIXME : better algo */
1fc22eb4
JD
499 /* create the threads index if required */
500 for (i = 0; i < dst->process_table->len; i++) {
501 tmp = g_ptr_array_index(dst->process_table, i);
502 if (tmp->pid == tmp->tid) {
503 for (j = 0; j < dst->process_table->len; j++) {
504 tmp2 = g_ptr_array_index(dst->process_table, j);
505 if (tmp2->pid == tmp->pid) {
506 tmp2->threadparent = tmp;
507 g_ptr_array_add(tmp->threads, tmp2);
508 }
509 }
510 }
511 }
512
513 // update_global_stats(dst);
514 cleanup_processtop();
515
516 return dst;
517}
518
928f18a6
MB
519
520enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
521 void *private_data)
522{
2e0a1190 523 const struct bt_definition *scope;
928f18a6
MB
524 struct processtop *proc;
525 unsigned long timestamp;
11d218ce 526 int64_t pid, tid, ppid, vtid, vpid, vppid;
928f18a6
MB
527 char *procname;
528
c78f2cdc 529 timestamp = bt_ctf_get_timestamp(call_data);
928f18a6
MB
530 if (timestamp == -1ULL)
531 goto error;
532
533 scope = bt_ctf_get_top_level_scope(call_data,
534 BT_EVENT_FIELDS);
535 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
536 scope, "_pid"));
537 if (bt_ctf_field_get_error()) {
538 fprintf(stderr, "Missing pid context info\n");
539 goto error;
540 }
11d218ce
JD
541 ppid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
542 scope, "_ppid"));
543 if (bt_ctf_field_get_error()) {
b7194a4e 544 fprintf(stderr, "Missing ppid context info\n");
11d218ce
JD
545 goto error;
546 }
928f18a6
MB
547 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
548 scope, "_tid"));
549 if (bt_ctf_field_get_error()) {
550 fprintf(stderr, "Missing tid context info\n");
551 goto error;
552 }
11d218ce
JD
553 vtid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
554 scope, "_vtid"));
555 if (bt_ctf_field_get_error()) {
556 fprintf(stderr, "Missing vtid context info\n");
557 goto error;
558 }
559 vpid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
560 scope, "_vpid"));
561 if (bt_ctf_field_get_error()) {
b7194a4e 562 fprintf(stderr, "Missing vpid context info\n");
11d218ce
JD
563 goto error;
564 }
565 vppid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
566 scope, "_vppid"));
567 if (bt_ctf_field_get_error()) {
b7194a4e 568 fprintf(stderr, "Missing vppid context info\n");
11d218ce
JD
569 goto error;
570 }
571
928f18a6
MB
572 scope = bt_ctf_get_top_level_scope(call_data,
573 BT_EVENT_FIELDS);
574 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
575 scope, "_name"));
576 if (bt_ctf_field_get_error()) {
577 fprintf(stderr, "Missing process name context info\n");
578 goto error;
579 }
580
581 proc = find_process_tid(&lttngtop, tid, procname);
582 if (proc == NULL)
583 proc = add_proc(&lttngtop, tid, procname, timestamp);
11d218ce 584 update_proc(proc, pid, tid, ppid, vpid, vtid, vppid, procname);
928f18a6
MB
585
586 free(proc->comm);
587 proc->comm = strdup(procname);
588 proc->pid = pid;
589
928f18a6
MB
590 return BT_CB_OK;
591
592error:
593 return BT_CB_ERROR_STOP;
594}
b520ab45
JD
595
596struct tm format_timestamp(uint64_t timestamp)
597{
598 struct tm tm;
599 uint64_t ts_sec = 0, ts_nsec;
600 time_t time_s;
601
602 ts_nsec = timestamp;
603 ts_sec += ts_nsec / NSEC_PER_SEC;
604 ts_nsec = ts_nsec % NSEC_PER_SEC;
605
606 time_s = (time_t) ts_sec;
607
608 localtime_r(&time_s, &tm);
609
610 return tm;
611}
This page took 0.053861 seconds and 4 git commands to generate.