fd941bde92673530019ffe921d03ec1054686617
[lttngtop.git] / src / common.c
1 /*
2 * Copyright (C) 2011-2012 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 *
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.
16 */
17
18 #include <babeltrace/ctf/events.h>
19 #include <stdlib.h>
20 #include <linux/unistd.h>
21 #include <string.h>
22 #include "common.h"
23
24 uint64_t get_cpu_id(const struct bt_ctf_event *event)
25 {
26 const struct bt_definition *scope;
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
39 uint64_t get_context_tid(const struct bt_ctf_event *event)
40 {
41 const struct bt_definition *scope;
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
55 uint64_t get_context_pid(const struct bt_ctf_event *event)
56 {
57 const struct bt_definition *scope;
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
71 uint64_t get_context_ppid(const struct bt_ctf_event *event)
72 {
73 const struct bt_definition *scope;
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
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
132 char *get_context_comm(const struct bt_ctf_event *event)
133 {
134 const struct bt_definition *scope;
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
148 /*
149 * To get the parent process, put the pid in the tid field
150 * because the parent process gets pid = tid
151 */
152 struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
153 {
154 struct processtop *tmp;
155
156 tmp = g_hash_table_lookup(ctx->process_hash_table,
157 (gconstpointer) (unsigned long) tid);
158
159 return tmp;
160 }
161
162 struct 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) {
171 newproc = g_new0(struct processtop, 1);
172 newproc->tid = tid;
173 newproc->birth = timestamp;
174 newproc->process_files_table = g_ptr_array_new();
175 newproc->files_history = NULL;
176 newproc->totalfileread = 0;
177 newproc->totalfilewrite = 0;
178 newproc->fileread = 0;
179 newproc->filewrite = 0;
180 newproc->syscall_info = NULL;
181 newproc->threadparent = NULL;
182 newproc->threads = g_ptr_array_new();
183 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
184 g_ptr_array_add(ctx->process_table, newproc);
185 g_hash_table_insert(ctx->process_hash_table,
186 (gpointer) (unsigned long) tid, newproc);
187
188 ctx->nbnewthreads++;
189 ctx->nbthreads++;
190 }
191 newproc->comm = strdup(comm);
192
193 return newproc;
194 }
195
196 struct processtop* update_proc(struct processtop* proc, int pid, int tid,
197 int ppid, int vpid, int vtid, int vppid, char *comm)
198 {
199 if (proc) {
200 proc->pid = pid;
201 proc->tid = tid;
202 proc->ppid = ppid;
203 proc->vpid = vpid;
204 proc->vtid = vtid;
205 proc->vppid = vppid;
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 */
218 void 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);
223
224 g_hash_table_remove(ctx->process_hash_table,
225 (gpointer) (unsigned long) tid);
226 if (tmp && strcmp(tmp->comm, comm) == 0) {
227 tmp->death = timestamp;
228 ctx->nbdeadthreads++;
229 ctx->nbthreads--;
230 }
231 }
232
233 struct 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
243 struct 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
253 void 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
266 struct cputime* add_cpu(int cpu)
267 {
268 struct cputime *newcpu;
269
270 newcpu = g_new0(struct cputime, 1);
271 newcpu->id = cpu;
272 newcpu->current_task = NULL;
273 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
274
275 g_ptr_array_add(lttngtop.cpu_table, newcpu);
276
277 return newcpu;
278 }
279 struct 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 */
297 void 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
318 void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
319 {
320 ((struct perfcounter*) value)->count = 0;
321 }
322
323 void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
324 {
325 struct perfcounter *newperf;
326
327 newperf = g_new0(struct perfcounter, 1);
328 newperf->count = ((struct perfcounter *) value)->count;
329 newperf->visible = ((struct perfcounter *) value)->visible;
330 newperf->sort = ((struct perfcounter *) value)->sort;
331 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
332 }
333
334 void copy_process_table(gpointer key, gpointer value, gpointer new_table)
335 {
336 g_hash_table_insert((GHashTable *) new_table, key, value);
337 }
338
339 void 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
348 void cleanup_processtop()
349 {
350 gint i, j;
351 struct processtop *tmp;
352 struct files *tmpf; /* a temporary file */
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;
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;
366
367 if (tmpf->flag == __NR_close)
368 g_ptr_array_index(
369 tmp->process_files_table, j
370 ) = NULL;
371 }
372 }
373 }
374 }
375
376 void 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
386 void 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
400 struct 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
409 dst = g_new0(struct lttngtop, 1);
410 dst->start = start;
411 dst->end = end;
412 copy_global_counters(dst);
413 dst->process_table = g_ptr_array_new();
414 dst->files_table = g_ptr_array_new();
415 dst->cpu_table = g_ptr_array_new();
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);
419
420 rotate_cputime(end);
421
422 for (i = 0; i < lttngtop.process_table->len; i++) {
423 tmp = g_ptr_array_index(lttngtop.process_table, i);
424 new = g_new0(struct processtop, 1);
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();
430 new->files_history = tmp->files_history;
431 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
432 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
433
434 /* compute the stream speed */
435 if (end - start != 0) {
436 time = (end - start) / NSEC_PER_SEC;
437 new->fileread = new->fileread/(time);
438 new->filewrite = new->filewrite/(time);
439 }
440
441 for (j = 0; j < tmp->process_files_table->len; j++) {
442 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
443
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 }
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) {
462 /* FIXME : close the files before */
463 g_ptr_array_remove(tmp->process_files_table, tmpfile);
464 g_free(tmpfile);
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);
475 /* FIXME : TRUE does not mean clears the object in it */
476 g_ptr_array_free(tmp->threads, TRUE);
477 free(tmp->comm);
478 g_ptr_array_free(tmp->process_files_table, TRUE);
479 /* FIXME : clear elements */
480 g_hash_table_destroy(tmp->perf);
481 g_free(tmp);
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);
488 newcpu = g_new0(struct cputime, 1);
489 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
490 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
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 }
498 /* FIXME : better algo */
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
519
520 enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
521 void *private_data)
522 {
523 const struct bt_definition *scope;
524 struct processtop *proc;
525 unsigned long timestamp;
526 int64_t pid, tid;
527 char *procname;
528
529 timestamp = bt_ctf_get_timestamp(call_data);
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 }
541
542 scope = bt_ctf_get_top_level_scope(call_data,
543 BT_EVENT_FIELDS);
544 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
545 scope, "_tid"));
546 if (bt_ctf_field_get_error()) {
547 fprintf(stderr, "Missing tid context info\n");
548 goto error;
549 }
550
551 /*
552 * FIXME
553 * I first tried with bt_ctf_get_string but doesn`t work at all
554 * It couldn`t find the field _name because it is an integer in
555 * the metadata and not a string like _filename for the
556 * statedump_file_descriptor
557 */
558 scope = bt_ctf_get_top_level_scope(call_data,
559 BT_EVENT_FIELDS);
560 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
561 scope, "_name"));
562 if (bt_ctf_field_get_error()) {
563 fprintf(stderr, "Missing process name context info\n");
564 goto error;
565 }
566
567 proc = find_process_tid(&lttngtop, tid, procname);
568 if (proc == NULL)
569 proc = add_proc(&lttngtop, tid, procname, timestamp);
570
571 free(proc->comm);
572 proc->comm = strdup(procname);
573 proc->pid = pid;
574
575 /*
576 * FIXME
577 * I would like to free procname because it is duplicated
578 * when the process is created but it segfaults...
579 *
580 * free(procname);
581 */
582
583 return BT_CB_OK;
584
585 error:
586 return BT_CB_ERROR_STOP;
587 }
588
589 struct tm format_timestamp(uint64_t timestamp)
590 {
591 struct tm tm;
592 uint64_t ts_sec = 0, ts_nsec;
593 time_t time_s;
594
595 ts_nsec = timestamp;
596 ts_sec += ts_nsec / NSEC_PER_SEC;
597 ts_nsec = ts_nsec % NSEC_PER_SEC;
598
599 time_s = (time_t) ts_sec;
600
601 localtime_r(&time_s, &tm);
602
603 return tm;
604 }
This page took 0.039219 seconds and 3 git commands to generate.