statedump_process_state & problems patches
[lttngtop.git] / src / common.c
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 *
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 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 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 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 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 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 * FIXME : char *comm useful ???
108 */
109 struct 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
122 struct 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) {
131 newproc = g_new0(struct processtop, 1);
132 newproc->tid = tid;
133 newproc->birth = timestamp;
134 newproc->process_files_table = g_ptr_array_new();
135 newproc->files_history = NULL;
136 newproc->totalfileread = 0;
137 newproc->totalfilewrite = 0;
138 newproc->fileread = 0;
139 newproc->filewrite = 0;
140 newproc->syscall_info = NULL;
141 newproc->threadparent = NULL;
142 newproc->threads = g_ptr_array_new();
143 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
144 g_ptr_array_add(ctx->process_table, newproc);
145
146 ctx->nbnewthreads++;
147 ctx->nbthreads++;
148 }
149 newproc->comm = strdup(comm);
150
151 return newproc;
152 }
153
154 struct 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 */
173 void 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);
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 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
295 void cleanup_processtop()
296 {
297 gint i, j;
298 struct processtop *tmp;
299 struct files *tmpf; /* a temporary file */
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;
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;
313
314 if (tmpf->flag == __NR_close)
315 g_ptr_array_index(
316 tmp->process_files_table, j
317 ) = NULL;
318 }
319 }
320 }
321 }
322
323 void 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
333 void 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
347 struct 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
356 dst = g_new0(struct lttngtop, 1);
357 dst->start = start;
358 dst->end = end;
359 copy_global_counters(dst);
360 dst->process_table = g_ptr_array_new();
361 dst->files_table = g_ptr_array_new();
362 dst->cpu_table = g_ptr_array_new();
363 dst->perf_list = g_hash_table_new(g_str_hash, g_str_equal);
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);
370 new = g_new0(struct processtop, 1);
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();
376 new->files_history = tmp->files_history;
377 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
378 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
379
380 /* compute the stream speed */
381 if (end - start != 0) {
382 time = (end - start) / NSEC_PER_SEC;
383 new->fileread = new->fileread/(time);
384 new->filewrite = new->filewrite/(time);
385 }
386
387 for (j = 0; j < tmp->process_files_table->len; j++) {
388 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
389
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 }
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) {
408 /* FIXME : close the files before */
409 g_ptr_array_remove(tmp->process_files_table, tmpfile);
410 g_free(tmpfile);
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);
421 /* FIXME : TRUE does not mean clears the object in it */
422 g_ptr_array_free(tmp->threads, TRUE);
423 free(tmp->comm);
424 g_ptr_array_free(tmp->process_files_table, TRUE);
425 /* FIXME : clear elements */
426 g_hash_table_destroy(tmp->perf);
427 g_free(tmp);
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);
434 newcpu = g_new0(struct cputime, 1);
435 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
436 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
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 }
444 /* FIXME : better algo */
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
465
466 enum 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
531 error:
532 return BT_CB_ERROR_STOP;
533 }
This page took 0.059824 seconds and 4 git commands to generate.