Each view can access process details view
[lttngtop.git] / src / common.c
CommitLineData
1fc22eb4
JD
1/*
2 * Copyright (C) 2011 Julien Desfossez
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();
85db4618 363 dst->perf_list = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
364
365 rotate_cputime(end);
366
367 g_hash_table_foreach(lttngtop.perf_list, copy_perf_counter, dst->perf_list);
368 for (i = 0; i < lttngtop.process_table->len; i++) {
369 tmp = g_ptr_array_index(lttngtop.process_table, i);
559c9f86 370 new = g_new0(struct processtop, 1);
1fc22eb4
JD
371
372 memcpy(new, tmp, sizeof(struct processtop));
373 new->threads = g_ptr_array_new();
374 new->comm = strdup(tmp->comm);
375 new->process_files_table = g_ptr_array_new();
ceb3a221 376 new->files_history = tmp->files_history;
85db4618 377 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
378 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
379
1fc22eb4 380 /* compute the stream speed */
85db4618
JD
381 if (end - start != 0) {
382 time = (end - start) / NSEC_PER_SEC;
b093de8a
MB
383 new->fileread = new->fileread/(time);
384 new->filewrite = new->filewrite/(time);
1fc22eb4
JD
385 }
386
387 for (j = 0; j < tmp->process_files_table->len; j++) {
388 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
1fc22eb4 389
b093de8a
MB
390 newfile = malloc(sizeof(struct files));
391
392 if (tmpfile != NULL) {
393 memcpy(newfile, tmpfile, sizeof(struct files));
394 newfile->name = strdup(tmpfile->name);
395 newfile->ref = new;
396 g_ptr_array_add(new->process_files_table,
397 newfile);
398 g_ptr_array_add(dst->files_table, newfile);
399 } else {
400 g_ptr_array_add(new->process_files_table, NULL);
401 g_ptr_array_add(dst->files_table, NULL);
402 }
1fc22eb4
JD
403 /*
404 * if the process died during the last period, we remove all
405 * files associated with if after the copy
406 */
407 if (tmp->death > 0 && tmp->death < end) {
e05a35a6 408 /* FIXME : close the files before */
1fc22eb4 409 g_ptr_array_remove(tmp->process_files_table, tmpfile);
559c9f86 410 g_free(tmpfile);
1fc22eb4
JD
411 }
412 }
413 g_ptr_array_add(dst->process_table, new);
414
415 /*
416 * if the process died during the last period, we remove it from
417 * the current process list after the copy
418 */
419 if (tmp->death > 0 && tmp->death < end) {
420 g_ptr_array_remove(lttngtop.process_table, tmp);
85db4618 421 /* FIXME : TRUE does not mean clears the object in it */
1fc22eb4
JD
422 g_ptr_array_free(tmp->threads, TRUE);
423 free(tmp->comm);
424 g_ptr_array_free(tmp->process_files_table, TRUE);
85db4618 425 /* FIXME : clear elements */
1fc22eb4 426 g_hash_table_destroy(tmp->perf);
559c9f86 427 g_free(tmp);
1fc22eb4
JD
428 }
429 }
430 rotate_perfcounter();
431
432 for (i = 0; i < lttngtop.cpu_table->len; i++) {
433 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
559c9f86 434 newcpu = g_new0(struct cputime, 1);
1fc22eb4 435 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
85db4618 436 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
437 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
438 /*
439 * note : we don't care about the current process pointer in the copy
440 * so the reference is invalid after the memcpy
441 */
442 g_ptr_array_add(dst->cpu_table, newcpu);
443 }
85db4618 444 /* FIXME : better algo */
1fc22eb4
JD
445 /* create the threads index if required */
446 for (i = 0; i < dst->process_table->len; i++) {
447 tmp = g_ptr_array_index(dst->process_table, i);
448 if (tmp->pid == tmp->tid) {
449 for (j = 0; j < dst->process_table->len; j++) {
450 tmp2 = g_ptr_array_index(dst->process_table, j);
451 if (tmp2->pid == tmp->pid) {
452 tmp2->threadparent = tmp;
453 g_ptr_array_add(tmp->threads, tmp2);
454 }
455 }
456 }
457 }
458
459 // update_global_stats(dst);
460 cleanup_processtop();
461
462 return dst;
463}
464
928f18a6
MB
465
466enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
467 void *private_data)
468{
469 const struct definition *scope;
470 struct processtop *proc;
471 unsigned long timestamp;
472 int64_t pid, tid;
473 char *procname;
474
475 timestamp = bt_ctf_get_timestamp(call_data);
476 if (timestamp == -1ULL)
477 goto error;
478
479 scope = bt_ctf_get_top_level_scope(call_data,
480 BT_EVENT_FIELDS);
481 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
482 scope, "_pid"));
483 if (bt_ctf_field_get_error()) {
484 fprintf(stderr, "Missing pid context info\n");
485 goto error;
486 }
487
488 scope = bt_ctf_get_top_level_scope(call_data,
489 BT_EVENT_FIELDS);
490 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
491 scope, "_tid"));
492 if (bt_ctf_field_get_error()) {
493 fprintf(stderr, "Missing tid context info\n");
494 goto error;
495 }
496
497 /*
498 * FIXME
499 * I first tried with bt_ctf_get_string but doesn`t work at all
500 * It couldn`t find the field _name because it is an integer in
501 * the metadata and not a string like _filename for the
502 * statedump_file_descriptor
503 */
504 scope = bt_ctf_get_top_level_scope(call_data,
505 BT_EVENT_FIELDS);
506 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
507 scope, "_name"));
508 if (bt_ctf_field_get_error()) {
509 fprintf(stderr, "Missing process name context info\n");
510 goto error;
511 }
512
513 proc = find_process_tid(&lttngtop, tid, procname);
514 if (proc == NULL)
515 proc = add_proc(&lttngtop, tid, procname, timestamp);
516
517 free(proc->comm);
518 proc->comm = strdup(procname);
519 proc->pid = pid;
520
521 /*
522 * FIXME
523 * I would like to free procname because it is duplicated
524 * when the process is created but it segfaults...
525 *
526 * free(procname);
527 */
528
529 return BT_CB_OK;
530
531error:
532 return BT_CB_ERROR_STOP;
533}
This page took 0.070509 seconds and 4 git commands to generate.