Revert "fix compat with babeltrace API change at commit 03798a93f959f6c694fe98f564748...
[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{
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 39uint64_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 55uint64_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 71uint64_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
4adc8274 87char *get_context_comm(const struct bt_ctf_event *event)
1dec520a 88{
3ba84bed 89 const struct definition *scope;
1dec520a
JD
90 char *comm;
91
92 scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT);
93 comm = bt_ctf_get_char_array(bt_ctf_get_field(event,
94 scope, "_procname"));
95 if (bt_ctf_field_get_error()) {
96 fprintf(stderr, "Missing comm context info\n");
97 return NULL;
98 }
99
100 return comm;
101}
102
59288610
MB
103/*
104 * To get the parent process, put the pid in the tid field
105 * because the parent process gets pid = tid
106 *
107 * FIXME : char *comm useful ???
108 */
1fc22eb4
JD
109struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
110{
111 gint i;
112 struct processtop *tmp;
113
114 for (i = 0; i < ctx->process_table->len; i++) {
115 tmp = g_ptr_array_index(ctx->process_table, i);
116 if (tmp && tmp->tid == tid)
117 return tmp;
118 }
119 return NULL;
120}
121
122struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
123 unsigned long timestamp)
124{
125 struct processtop *newproc;
126
127 /* if the PID already exists, we just rename the process */
128 /* FIXME : need to integrate with clone/fork/exit to be accurate */
129 newproc = find_process_tid(ctx, tid, comm);
130 if (!newproc) {
559c9f86 131 newproc = g_new0(struct processtop, 1);
1fc22eb4
JD
132 newproc->tid = tid;
133 newproc->birth = timestamp;
134 newproc->process_files_table = g_ptr_array_new();
ceb3a221 135 newproc->files_history = NULL;
b093de8a
MB
136 newproc->totalfileread = 0;
137 newproc->totalfilewrite = 0;
138 newproc->fileread = 0;
139 newproc->filewrite = 0;
140 newproc->syscall_info = NULL;
59288610 141 newproc->threadparent = NULL;
1fc22eb4 142 newproc->threads = g_ptr_array_new();
85db4618 143 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4 144 g_ptr_array_add(ctx->process_table, newproc);
e05a35a6
JD
145
146 ctx->nbnewthreads++;
147 ctx->nbthreads++;
1fc22eb4
JD
148 }
149 newproc->comm = strdup(comm);
150
151 return newproc;
152}
153
154struct processtop* update_proc(struct processtop* proc, int pid, int tid,
155 int ppid, char *comm)
156{
157 if (proc) {
158 proc->pid = pid;
159 proc->tid = tid;
160 proc->ppid = ppid;
161 if (strcmp(proc->comm, comm) != 0) {
162 free(proc->comm);
163 proc->comm = strdup(comm);
164 }
165 }
166 return proc;
167}
168
169/*
170 * This function just sets the time of death of a process.
171 * When we rotate the cputime we remove it from the process list.
172 */
173void death_proc(struct lttngtop *ctx, int tid, char *comm,
174 unsigned long timestamp)
175{
176 struct processtop *tmp;
177 tmp = find_process_tid(ctx, tid, comm);
e05a35a6 178 if (tmp && strcmp(tmp->comm, comm) == 0) {
1fc22eb4 179 tmp->death = timestamp;
e05a35a6
JD
180 ctx->nbdeadthreads++;
181 ctx->nbthreads--;
182 }
1fc22eb4
JD
183}
184
185struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
186 unsigned long timestamp)
187{
188 struct processtop *tmp;
189 tmp = find_process_tid(ctx, tid, comm);
190 if (tmp && strcmp(tmp->comm, comm) == 0)
191 return tmp;
192 return add_proc(ctx, tid, comm, timestamp);
193}
194
59288610
MB
195struct processtop *get_proc_pid(struct lttngtop *ctx, int tid, int pid,
196 unsigned long timestamp)
197{
198 struct processtop *tmp;
199 tmp = find_process_tid(ctx, tid, NULL);
200 if (tmp && tmp->pid == pid)
201 return tmp;
202 return add_proc(ctx, tid, "Unknown", timestamp);
203}
204
1fc22eb4
JD
205void add_thread(struct processtop *parent, struct processtop *thread)
206{
207 gint i;
208 struct processtop *tmp;
209
210 for (i = 0; i < parent->threads->len; i++) {
211 tmp = g_ptr_array_index(parent->threads, i);
212 if (tmp == thread)
213 return;
214 }
215 g_ptr_array_add(parent->threads, thread);
216}
217
218struct cputime* add_cpu(int cpu)
219{
220 struct cputime *newcpu;
221
559c9f86 222 newcpu = g_new0(struct cputime, 1);
1fc22eb4
JD
223 newcpu->id = cpu;
224 newcpu->current_task = NULL;
85db4618 225 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
226
227 g_ptr_array_add(lttngtop.cpu_table, newcpu);
228
229 return newcpu;
230}
231struct cputime* get_cpu(int cpu)
232{
233 gint i;
234 struct cputime *tmp;
235
236 for (i = 0; i < lttngtop.cpu_table->len; i++) {
237 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
238 if (tmp->id == cpu)
239 return tmp;
240 }
241
242 return add_cpu(cpu);
243}
244
245/*
246 * At the end of a sampling period, we need to display the cpu time for each
247 * process and to reset it to zero for the next period
248 */
249void rotate_cputime(unsigned long end)
250{
251 gint i;
252 struct cputime *tmp;
253 unsigned long elapsed;
254
255 for (i = 0; i < lttngtop.cpu_table->len; i++) {
256 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
257 elapsed = end - tmp->task_start;
258 if (tmp->current_task) {
259 tmp->current_task->totalcpunsec += elapsed;
260 tmp->current_task->threadstotalcpunsec += elapsed;
261 if (tmp->current_task->pid != tmp->current_task->tid &&
262 tmp->current_task->threadparent) {
263 tmp->current_task->threadparent->threadstotalcpunsec += elapsed;
264 }
265 }
266 tmp->task_start = end;
267 }
268}
269
270void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
271{
272 ((struct perfcounter*) value)->count = 0;
273}
274
275void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
276{
277 struct perfcounter *newperf;
59288610 278
559c9f86 279 newperf = g_new0(struct perfcounter, 1);
1fc22eb4
JD
280 newperf->count = ((struct perfcounter *) value)->count;
281 newperf->visible = ((struct perfcounter *) value)->visible;
282 newperf->sort = ((struct perfcounter *) value)->sort;
85db4618 283 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
1fc22eb4
JD
284}
285
286void rotate_perfcounter() {
287 int i;
288 struct processtop *tmp;
289 for (i = 0; i < lttngtop.process_table->len; i++) {
290 tmp = g_ptr_array_index(lttngtop.process_table, i);
291 g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL);
292 }
293}
294
295void cleanup_processtop()
296{
b093de8a 297 gint i, j;
1fc22eb4 298 struct processtop *tmp;
b093de8a 299 struct files *tmpf; /* a temporary file */
1fc22eb4
JD
300
301 for (i = 0; i < lttngtop.process_table->len; i++) {
302 tmp = g_ptr_array_index(lttngtop.process_table, i);
303 tmp->totalcpunsec = 0;
304 tmp->threadstotalcpunsec = 0;
b093de8a
MB
305 tmp->fileread = 0;
306 tmp->filewrite = 0;
307
308 for (j = 0; j < tmp->process_files_table->len; j++) {
309 tmpf = g_ptr_array_index(tmp->process_files_table, j);
310 if (tmpf != NULL) {
311 tmpf->read = 0;
312 tmpf->write = 0;
ceb3a221
MB
313
314 if (tmpf->flag == __NR_close)
315 g_ptr_array_index(
316 tmp->process_files_table, j
317 ) = NULL;
b093de8a
MB
318 }
319 }
1fc22eb4
JD
320 }
321}
322
e05a35a6
JD
323void reset_global_counters()
324{
325 lttngtop.nbnewproc = 0;
326 lttngtop.nbdeadproc = 0;
327 lttngtop.nbnewthreads = 0;
328 lttngtop.nbdeadthreads = 0;
329 lttngtop.nbnewfiles = 0;
330 lttngtop.nbclosedfiles = 0;
331}
332
333void copy_global_counters(struct lttngtop *dst)
334{
335 dst->nbproc = lttngtop.nbproc;
336 dst->nbnewproc = lttngtop.nbnewproc;
337 dst->nbdeadproc = lttngtop.nbdeadproc;
338 dst->nbthreads = lttngtop.nbthreads;
339 dst->nbnewthreads = lttngtop.nbnewthreads;
340 dst->nbdeadthreads = lttngtop.nbdeadthreads;
341 dst->nbfiles = lttngtop.nbfiles;
342 dst->nbnewfiles = lttngtop.nbnewfiles;
343 dst->nbclosedfiles = lttngtop.nbclosedfiles;
344 reset_global_counters();
345}
346
1fc22eb4
JD
347struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
348{
349 gint i, j;
350 unsigned long time;
351 struct lttngtop *dst;
352 struct processtop *tmp, *tmp2, *new;
353 struct cputime *tmpcpu, *newcpu;
354 struct files *tmpfile, *newfile;
355
559c9f86 356 dst = g_new0(struct lttngtop, 1);
1fc22eb4
JD
357 dst->start = start;
358 dst->end = end;
e05a35a6 359 copy_global_counters(dst);
1fc22eb4
JD
360 dst->process_table = g_ptr_array_new();
361 dst->files_table = g_ptr_array_new();
362 dst->cpu_table = g_ptr_array_new();
1fc22eb4
JD
363
364 rotate_cputime(end);
365
1fc22eb4
JD
366 for (i = 0; i < lttngtop.process_table->len; i++) {
367 tmp = g_ptr_array_index(lttngtop.process_table, i);
559c9f86 368 new = g_new0(struct processtop, 1);
1fc22eb4
JD
369
370 memcpy(new, tmp, sizeof(struct processtop));
371 new->threads = g_ptr_array_new();
372 new->comm = strdup(tmp->comm);
373 new->process_files_table = g_ptr_array_new();
ceb3a221 374 new->files_history = tmp->files_history;
85db4618 375 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
376 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
377
1fc22eb4 378 /* compute the stream speed */
85db4618
JD
379 if (end - start != 0) {
380 time = (end - start) / NSEC_PER_SEC;
b093de8a
MB
381 new->fileread = new->fileread/(time);
382 new->filewrite = new->filewrite/(time);
1fc22eb4
JD
383 }
384
385 for (j = 0; j < tmp->process_files_table->len; j++) {
386 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
1fc22eb4 387
b093de8a
MB
388 newfile = malloc(sizeof(struct files));
389
390 if (tmpfile != NULL) {
391 memcpy(newfile, tmpfile, sizeof(struct files));
392 newfile->name = strdup(tmpfile->name);
393 newfile->ref = new;
394 g_ptr_array_add(new->process_files_table,
395 newfile);
396 g_ptr_array_add(dst->files_table, newfile);
397 } else {
398 g_ptr_array_add(new->process_files_table, NULL);
399 g_ptr_array_add(dst->files_table, NULL);
400 }
1fc22eb4
JD
401 /*
402 * if the process died during the last period, we remove all
403 * files associated with if after the copy
404 */
405 if (tmp->death > 0 && tmp->death < end) {
e05a35a6 406 /* FIXME : close the files before */
1fc22eb4 407 g_ptr_array_remove(tmp->process_files_table, tmpfile);
559c9f86 408 g_free(tmpfile);
1fc22eb4
JD
409 }
410 }
411 g_ptr_array_add(dst->process_table, new);
412
413 /*
414 * if the process died during the last period, we remove it from
415 * the current process list after the copy
416 */
417 if (tmp->death > 0 && tmp->death < end) {
418 g_ptr_array_remove(lttngtop.process_table, tmp);
85db4618 419 /* FIXME : TRUE does not mean clears the object in it */
1fc22eb4
JD
420 g_ptr_array_free(tmp->threads, TRUE);
421 free(tmp->comm);
422 g_ptr_array_free(tmp->process_files_table, TRUE);
85db4618 423 /* FIXME : clear elements */
1fc22eb4 424 g_hash_table_destroy(tmp->perf);
559c9f86 425 g_free(tmp);
1fc22eb4
JD
426 }
427 }
428 rotate_perfcounter();
429
430 for (i = 0; i < lttngtop.cpu_table->len; i++) {
431 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
559c9f86 432 newcpu = g_new0(struct cputime, 1);
1fc22eb4 433 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
85db4618 434 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
435 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
436 /*
437 * note : we don't care about the current process pointer in the copy
438 * so the reference is invalid after the memcpy
439 */
440 g_ptr_array_add(dst->cpu_table, newcpu);
441 }
85db4618 442 /* FIXME : better algo */
1fc22eb4
JD
443 /* create the threads index if required */
444 for (i = 0; i < dst->process_table->len; i++) {
445 tmp = g_ptr_array_index(dst->process_table, i);
446 if (tmp->pid == tmp->tid) {
447 for (j = 0; j < dst->process_table->len; j++) {
448 tmp2 = g_ptr_array_index(dst->process_table, j);
449 if (tmp2->pid == tmp->pid) {
450 tmp2->threadparent = tmp;
451 g_ptr_array_add(tmp->threads, tmp2);
452 }
453 }
454 }
455 }
456
457 // update_global_stats(dst);
458 cleanup_processtop();
459
460 return dst;
461}
462
928f18a6
MB
463
464enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
465 void *private_data)
466{
467 const struct definition *scope;
468 struct processtop *proc;
469 unsigned long timestamp;
470 int64_t pid, tid;
471 char *procname;
472
480b40ce 473 timestamp = bt_ctf_get_timestamp(call_data);
928f18a6
MB
474 if (timestamp == -1ULL)
475 goto error;
476
477 scope = bt_ctf_get_top_level_scope(call_data,
478 BT_EVENT_FIELDS);
479 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
480 scope, "_pid"));
481 if (bt_ctf_field_get_error()) {
482 fprintf(stderr, "Missing pid context info\n");
483 goto error;
484 }
485
486 scope = bt_ctf_get_top_level_scope(call_data,
487 BT_EVENT_FIELDS);
488 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
489 scope, "_tid"));
490 if (bt_ctf_field_get_error()) {
491 fprintf(stderr, "Missing tid context info\n");
492 goto error;
493 }
494
495 /*
496 * FIXME
497 * I first tried with bt_ctf_get_string but doesn`t work at all
498 * It couldn`t find the field _name because it is an integer in
499 * the metadata and not a string like _filename for the
500 * statedump_file_descriptor
501 */
502 scope = bt_ctf_get_top_level_scope(call_data,
503 BT_EVENT_FIELDS);
504 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
505 scope, "_name"));
506 if (bt_ctf_field_get_error()) {
507 fprintf(stderr, "Missing process name context info\n");
508 goto error;
509 }
510
511 proc = find_process_tid(&lttngtop, tid, procname);
512 if (proc == NULL)
513 proc = add_proc(&lttngtop, tid, procname, timestamp);
514
515 free(proc->comm);
516 proc->comm = strdup(procname);
517 proc->pid = pid;
518
519 /*
520 * FIXME
521 * I would like to free procname because it is duplicated
522 * when the process is created but it segfaults...
523 *
524 * free(procname);
525 */
526
527 return BT_CB_OK;
528
529error:
530 return BT_CB_ERROR_STOP;
531}
This page took 0.046382 seconds and 4 git commands to generate.