flush at every second
[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 char *get_context_comm(const struct bt_ctf_event *event)
88 {
89 const struct bt_definition *scope;
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
103 /*
104 * To get the parent process, put the pid in the tid field
105 * because the parent process gets pid = tid
106 */
107 struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
108 {
109 struct processtop *tmp;
110
111 tmp = g_hash_table_lookup(ctx->process_hash_table,
112 (gconstpointer) (unsigned long) tid);
113
114 return tmp;
115 }
116
117 struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
118 unsigned long timestamp)
119 {
120 struct processtop *newproc;
121
122 /* if the PID already exists, we just rename the process */
123 /* FIXME : need to integrate with clone/fork/exit to be accurate */
124 newproc = find_process_tid(ctx, tid, comm);
125 if (!newproc) {
126 newproc = g_new0(struct processtop, 1);
127 newproc->tid = tid;
128 newproc->birth = timestamp;
129 newproc->process_files_table = g_ptr_array_new();
130 newproc->files_history = NULL;
131 newproc->totalfileread = 0;
132 newproc->totalfilewrite = 0;
133 newproc->fileread = 0;
134 newproc->filewrite = 0;
135 newproc->syscall_info = NULL;
136 newproc->threadparent = NULL;
137 newproc->threads = g_ptr_array_new();
138 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
139 g_ptr_array_add(ctx->process_table, newproc);
140 g_hash_table_insert(ctx->process_hash_table,
141 (gpointer) (unsigned long) tid, newproc);
142
143 ctx->nbnewthreads++;
144 ctx->nbthreads++;
145 }
146 newproc->comm = strdup(comm);
147
148 return newproc;
149 }
150
151 struct processtop* update_proc(struct processtop* proc, int pid, int tid,
152 int ppid, char *comm)
153 {
154 if (proc) {
155 proc->pid = pid;
156 proc->tid = tid;
157 proc->ppid = ppid;
158 if (strcmp(proc->comm, comm) != 0) {
159 free(proc->comm);
160 proc->comm = strdup(comm);
161 }
162 }
163 return proc;
164 }
165
166 /*
167 * This function just sets the time of death of a process.
168 * When we rotate the cputime we remove it from the process list.
169 */
170 void death_proc(struct lttngtop *ctx, int tid, char *comm,
171 unsigned long timestamp)
172 {
173 struct processtop *tmp;
174 tmp = find_process_tid(ctx, tid, comm);
175
176 g_hash_table_remove(ctx->process_hash_table,
177 (gpointer) (unsigned long) tid);
178 if (tmp && strcmp(tmp->comm, comm) == 0) {
179 tmp->death = timestamp;
180 ctx->nbdeadthreads++;
181 ctx->nbthreads--;
182 }
183 }
184
185 struct 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
195 struct 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
205 void 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
218 struct cputime* add_cpu(int cpu)
219 {
220 struct cputime *newcpu;
221
222 newcpu = g_new0(struct cputime, 1);
223 newcpu->id = cpu;
224 newcpu->current_task = NULL;
225 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
226
227 g_ptr_array_add(lttngtop.cpu_table, newcpu);
228
229 return newcpu;
230 }
231 struct 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 */
249 void 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
270 void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
271 {
272 ((struct perfcounter*) value)->count = 0;
273 }
274
275 void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
276 {
277 struct perfcounter *newperf;
278
279 newperf = g_new0(struct perfcounter, 1);
280 newperf->count = ((struct perfcounter *) value)->count;
281 newperf->visible = ((struct perfcounter *) value)->visible;
282 newperf->sort = ((struct perfcounter *) value)->sort;
283 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
284 }
285
286 void copy_process_table(gpointer key, gpointer value, gpointer new_table)
287 {
288 g_hash_table_insert((GHashTable *) new_table, key, value);
289 }
290
291 void rotate_perfcounter() {
292 int i;
293 struct processtop *tmp;
294 for (i = 0; i < lttngtop.process_table->len; i++) {
295 tmp = g_ptr_array_index(lttngtop.process_table, i);
296 g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL);
297 }
298 }
299
300 void cleanup_processtop()
301 {
302 gint i, j;
303 struct processtop *tmp;
304 struct files *tmpf; /* a temporary file */
305
306 for (i = 0; i < lttngtop.process_table->len; i++) {
307 tmp = g_ptr_array_index(lttngtop.process_table, i);
308 tmp->totalcpunsec = 0;
309 tmp->threadstotalcpunsec = 0;
310 tmp->fileread = 0;
311 tmp->filewrite = 0;
312
313 for (j = 0; j < tmp->process_files_table->len; j++) {
314 tmpf = g_ptr_array_index(tmp->process_files_table, j);
315 if (tmpf != NULL) {
316 tmpf->read = 0;
317 tmpf->write = 0;
318
319 if (tmpf->flag == __NR_close)
320 g_ptr_array_index(
321 tmp->process_files_table, j
322 ) = NULL;
323 }
324 }
325 }
326 }
327
328 void reset_global_counters()
329 {
330 lttngtop.nbnewproc = 0;
331 lttngtop.nbdeadproc = 0;
332 lttngtop.nbnewthreads = 0;
333 lttngtop.nbdeadthreads = 0;
334 lttngtop.nbnewfiles = 0;
335 lttngtop.nbclosedfiles = 0;
336 }
337
338 void copy_global_counters(struct lttngtop *dst)
339 {
340 dst->nbproc = lttngtop.nbproc;
341 dst->nbnewproc = lttngtop.nbnewproc;
342 dst->nbdeadproc = lttngtop.nbdeadproc;
343 dst->nbthreads = lttngtop.nbthreads;
344 dst->nbnewthreads = lttngtop.nbnewthreads;
345 dst->nbdeadthreads = lttngtop.nbdeadthreads;
346 dst->nbfiles = lttngtop.nbfiles;
347 dst->nbnewfiles = lttngtop.nbnewfiles;
348 dst->nbclosedfiles = lttngtop.nbclosedfiles;
349 reset_global_counters();
350 }
351
352 struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
353 {
354 gint i, j;
355 unsigned long time;
356 struct lttngtop *dst;
357 struct processtop *tmp, *tmp2, *new;
358 struct cputime *tmpcpu, *newcpu;
359 struct files *tmpfile, *newfile;
360
361 dst = g_new0(struct lttngtop, 1);
362 dst->start = start;
363 dst->end = end;
364 copy_global_counters(dst);
365 dst->process_table = g_ptr_array_new();
366 dst->files_table = g_ptr_array_new();
367 dst->cpu_table = g_ptr_array_new();
368 dst->process_hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
369 g_hash_table_foreach(lttngtop.process_hash_table, copy_process_table,
370 dst->process_hash_table);
371
372 rotate_cputime(end);
373
374 for (i = 0; i < lttngtop.process_table->len; i++) {
375 tmp = g_ptr_array_index(lttngtop.process_table, i);
376 new = g_new0(struct processtop, 1);
377
378 memcpy(new, tmp, sizeof(struct processtop));
379 new->threads = g_ptr_array_new();
380 new->comm = strdup(tmp->comm);
381 new->process_files_table = g_ptr_array_new();
382 new->files_history = tmp->files_history;
383 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
384 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
385
386 /* compute the stream speed */
387 if (end - start != 0) {
388 time = (end - start) / NSEC_PER_SEC;
389 new->fileread = new->fileread/(time);
390 new->filewrite = new->filewrite/(time);
391 }
392
393 for (j = 0; j < tmp->process_files_table->len; j++) {
394 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
395
396 newfile = malloc(sizeof(struct files));
397
398 if (tmpfile != NULL) {
399 memcpy(newfile, tmpfile, sizeof(struct files));
400 newfile->name = strdup(tmpfile->name);
401 newfile->ref = new;
402 g_ptr_array_add(new->process_files_table,
403 newfile);
404 g_ptr_array_add(dst->files_table, newfile);
405 } else {
406 g_ptr_array_add(new->process_files_table, NULL);
407 g_ptr_array_add(dst->files_table, NULL);
408 }
409 /*
410 * if the process died during the last period, we remove all
411 * files associated with if after the copy
412 */
413 if (tmp->death > 0 && tmp->death < end) {
414 /* FIXME : close the files before */
415 g_ptr_array_remove(tmp->process_files_table, tmpfile);
416 g_free(tmpfile);
417 }
418 }
419 g_ptr_array_add(dst->process_table, new);
420
421 /*
422 * if the process died during the last period, we remove it from
423 * the current process list after the copy
424 */
425 if (tmp->death > 0 && tmp->death < end) {
426 g_ptr_array_remove(lttngtop.process_table, tmp);
427 /* FIXME : TRUE does not mean clears the object in it */
428 g_ptr_array_free(tmp->threads, TRUE);
429 free(tmp->comm);
430 g_ptr_array_free(tmp->process_files_table, TRUE);
431 /* FIXME : clear elements */
432 g_hash_table_destroy(tmp->perf);
433 g_free(tmp);
434 }
435 }
436 rotate_perfcounter();
437
438 for (i = 0; i < lttngtop.cpu_table->len; i++) {
439 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
440 newcpu = g_new0(struct cputime, 1);
441 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
442 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
443 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
444 /*
445 * note : we don't care about the current process pointer in the copy
446 * so the reference is invalid after the memcpy
447 */
448 g_ptr_array_add(dst->cpu_table, newcpu);
449 }
450 /* FIXME : better algo */
451 /* create the threads index if required */
452 for (i = 0; i < dst->process_table->len; i++) {
453 tmp = g_ptr_array_index(dst->process_table, i);
454 if (tmp->pid == tmp->tid) {
455 for (j = 0; j < dst->process_table->len; j++) {
456 tmp2 = g_ptr_array_index(dst->process_table, j);
457 if (tmp2->pid == tmp->pid) {
458 tmp2->threadparent = tmp;
459 g_ptr_array_add(tmp->threads, tmp2);
460 }
461 }
462 }
463 }
464
465 // update_global_stats(dst);
466 cleanup_processtop();
467
468 return dst;
469 }
470
471
472 enum bt_cb_ret handle_statedump_process_state(struct bt_ctf_event *call_data,
473 void *private_data)
474 {
475 const struct bt_definition *scope;
476 struct processtop *proc;
477 unsigned long timestamp;
478 int64_t pid, tid;
479 char *procname;
480
481 timestamp = bt_ctf_get_timestamp(call_data);
482 if (timestamp == -1ULL)
483 goto error;
484
485 scope = bt_ctf_get_top_level_scope(call_data,
486 BT_EVENT_FIELDS);
487 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
488 scope, "_pid"));
489 if (bt_ctf_field_get_error()) {
490 fprintf(stderr, "Missing pid context info\n");
491 goto error;
492 }
493
494 scope = bt_ctf_get_top_level_scope(call_data,
495 BT_EVENT_FIELDS);
496 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
497 scope, "_tid"));
498 if (bt_ctf_field_get_error()) {
499 fprintf(stderr, "Missing tid context info\n");
500 goto error;
501 }
502
503 /*
504 * FIXME
505 * I first tried with bt_ctf_get_string but doesn`t work at all
506 * It couldn`t find the field _name because it is an integer in
507 * the metadata and not a string like _filename for the
508 * statedump_file_descriptor
509 */
510 scope = bt_ctf_get_top_level_scope(call_data,
511 BT_EVENT_FIELDS);
512 procname = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
513 scope, "_name"));
514 if (bt_ctf_field_get_error()) {
515 fprintf(stderr, "Missing process name context info\n");
516 goto error;
517 }
518
519 proc = find_process_tid(&lttngtop, tid, procname);
520 if (proc == NULL)
521 proc = add_proc(&lttngtop, tid, procname, timestamp);
522
523 free(proc->comm);
524 proc->comm = strdup(procname);
525 proc->pid = pid;
526
527 /*
528 * FIXME
529 * I would like to free procname because it is duplicated
530 * when the process is created but it segfaults...
531 *
532 * free(procname);
533 */
534
535 return BT_CB_OK;
536
537 error:
538 return BT_CB_ERROR_STOP;
539 }
540
541 struct tm format_timestamp(uint64_t timestamp)
542 {
543 struct tm tm;
544 uint64_t ts_sec = 0, ts_nsec;
545 time_t time_s;
546
547 ts_nsec = timestamp;
548 ts_sec += ts_nsec / NSEC_PER_SEC;
549 ts_nsec = ts_nsec % NSEC_PER_SEC;
550
551 time_s = (time_t) ts_sec;
552
553 localtime_r(&time_s, &tm);
554
555 return tm;
556 }
This page took 0.039514 seconds and 4 git commands to generate.