remove debug code
[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
3ba84bed 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
3ba84bed 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
3ba84bed 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
3ba84bed 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
3ba84bed 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
1fc22eb4
JD
103struct processtop *find_process_tid(struct lttngtop *ctx, int tid, char *comm)
104{
105 gint i;
106 struct processtop *tmp;
107
108 for (i = 0; i < ctx->process_table->len; i++) {
109 tmp = g_ptr_array_index(ctx->process_table, i);
110 if (tmp && tmp->tid == tid)
111 return tmp;
112 }
113 return NULL;
114}
115
116struct processtop* add_proc(struct lttngtop *ctx, int tid, char *comm,
117 unsigned long timestamp)
118{
119 struct processtop *newproc;
120
121 /* if the PID already exists, we just rename the process */
122 /* FIXME : need to integrate with clone/fork/exit to be accurate */
123 newproc = find_process_tid(ctx, tid, comm);
124 if (!newproc) {
559c9f86 125 newproc = g_new0(struct processtop, 1);
1fc22eb4
JD
126 newproc->tid = tid;
127 newproc->birth = timestamp;
128 newproc->process_files_table = g_ptr_array_new();
ceb3a221 129 newproc->files_history = NULL;
b093de8a
MB
130 newproc->totalfileread = 0;
131 newproc->totalfilewrite = 0;
132 newproc->fileread = 0;
133 newproc->filewrite = 0;
134 newproc->syscall_info = NULL;
1fc22eb4 135 newproc->threads = g_ptr_array_new();
85db4618 136 newproc->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4 137 g_ptr_array_add(ctx->process_table, newproc);
e05a35a6
JD
138
139 ctx->nbnewthreads++;
140 ctx->nbthreads++;
1fc22eb4
JD
141 }
142 newproc->comm = strdup(comm);
143
144 return newproc;
145}
146
147struct processtop* update_proc(struct processtop* proc, int pid, int tid,
148 int ppid, char *comm)
149{
150 if (proc) {
151 proc->pid = pid;
152 proc->tid = tid;
153 proc->ppid = ppid;
154 if (strcmp(proc->comm, comm) != 0) {
155 free(proc->comm);
156 proc->comm = strdup(comm);
157 }
158 }
159 return proc;
160}
161
162/*
163 * This function just sets the time of death of a process.
164 * When we rotate the cputime we remove it from the process list.
165 */
166void death_proc(struct lttngtop *ctx, int tid, char *comm,
167 unsigned long timestamp)
168{
169 struct processtop *tmp;
170 tmp = find_process_tid(ctx, tid, comm);
e05a35a6 171 if (tmp && strcmp(tmp->comm, comm) == 0) {
1fc22eb4 172 tmp->death = timestamp;
e05a35a6
JD
173 ctx->nbdeadthreads++;
174 ctx->nbthreads--;
175 }
1fc22eb4
JD
176}
177
178struct processtop* get_proc(struct lttngtop *ctx, int tid, char *comm,
179 unsigned long timestamp)
180{
181 struct processtop *tmp;
182 tmp = find_process_tid(ctx, tid, comm);
183 if (tmp && strcmp(tmp->comm, comm) == 0)
184 return tmp;
185 return add_proc(ctx, tid, comm, timestamp);
186}
187
188void add_thread(struct processtop *parent, struct processtop *thread)
189{
190 gint i;
191 struct processtop *tmp;
192
193 for (i = 0; i < parent->threads->len; i++) {
194 tmp = g_ptr_array_index(parent->threads, i);
195 if (tmp == thread)
196 return;
197 }
198 g_ptr_array_add(parent->threads, thread);
199}
200
201struct cputime* add_cpu(int cpu)
202{
203 struct cputime *newcpu;
204
559c9f86 205 newcpu = g_new0(struct cputime, 1);
1fc22eb4
JD
206 newcpu->id = cpu;
207 newcpu->current_task = NULL;
85db4618 208 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
209
210 g_ptr_array_add(lttngtop.cpu_table, newcpu);
211
212 return newcpu;
213}
214struct cputime* get_cpu(int cpu)
215{
216 gint i;
217 struct cputime *tmp;
218
219 for (i = 0; i < lttngtop.cpu_table->len; i++) {
220 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
221 if (tmp->id == cpu)
222 return tmp;
223 }
224
225 return add_cpu(cpu);
226}
227
228/*
229 * At the end of a sampling period, we need to display the cpu time for each
230 * process and to reset it to zero for the next period
231 */
232void rotate_cputime(unsigned long end)
233{
234 gint i;
235 struct cputime *tmp;
236 unsigned long elapsed;
237
238 for (i = 0; i < lttngtop.cpu_table->len; i++) {
239 tmp = g_ptr_array_index(lttngtop.cpu_table, i);
240 elapsed = end - tmp->task_start;
241 if (tmp->current_task) {
242 tmp->current_task->totalcpunsec += elapsed;
243 tmp->current_task->threadstotalcpunsec += elapsed;
244 if (tmp->current_task->pid != tmp->current_task->tid &&
245 tmp->current_task->threadparent) {
246 tmp->current_task->threadparent->threadstotalcpunsec += elapsed;
247 }
248 }
249 tmp->task_start = end;
250 }
251}
252
253void reset_perf_counter(gpointer key, gpointer value, gpointer user_data)
254{
255 ((struct perfcounter*) value)->count = 0;
256}
257
258void copy_perf_counter(gpointer key, gpointer value, gpointer new_table)
259{
260 struct perfcounter *newperf;
261
559c9f86 262 newperf = g_new0(struct perfcounter, 1);
1fc22eb4
JD
263 newperf->count = ((struct perfcounter *) value)->count;
264 newperf->visible = ((struct perfcounter *) value)->visible;
265 newperf->sort = ((struct perfcounter *) value)->sort;
85db4618 266 g_hash_table_insert((GHashTable *) new_table, strdup(key), newperf);
1fc22eb4
JD
267}
268
269void rotate_perfcounter() {
270 int i;
271 struct processtop *tmp;
272 for (i = 0; i < lttngtop.process_table->len; i++) {
273 tmp = g_ptr_array_index(lttngtop.process_table, i);
274 g_hash_table_foreach(tmp->perf, reset_perf_counter, NULL);
275 }
276}
277
278void cleanup_processtop()
279{
b093de8a 280 gint i, j;
1fc22eb4 281 struct processtop *tmp;
b093de8a 282 struct files *tmpf; /* a temporary file */
1fc22eb4
JD
283
284 for (i = 0; i < lttngtop.process_table->len; i++) {
285 tmp = g_ptr_array_index(lttngtop.process_table, i);
286 tmp->totalcpunsec = 0;
287 tmp->threadstotalcpunsec = 0;
b093de8a
MB
288 tmp->fileread = 0;
289 tmp->filewrite = 0;
290
291 for (j = 0; j < tmp->process_files_table->len; j++) {
292 tmpf = g_ptr_array_index(tmp->process_files_table, j);
293 if (tmpf != NULL) {
294 tmpf->read = 0;
295 tmpf->write = 0;
ceb3a221
MB
296
297 if (tmpf->flag == __NR_close)
298 g_ptr_array_index(
299 tmp->process_files_table, j
300 ) = NULL;
b093de8a
MB
301 }
302 }
1fc22eb4
JD
303 }
304}
305
e05a35a6
JD
306void reset_global_counters()
307{
308 lttngtop.nbnewproc = 0;
309 lttngtop.nbdeadproc = 0;
310 lttngtop.nbnewthreads = 0;
311 lttngtop.nbdeadthreads = 0;
312 lttngtop.nbnewfiles = 0;
313 lttngtop.nbclosedfiles = 0;
314}
315
316void copy_global_counters(struct lttngtop *dst)
317{
318 dst->nbproc = lttngtop.nbproc;
319 dst->nbnewproc = lttngtop.nbnewproc;
320 dst->nbdeadproc = lttngtop.nbdeadproc;
321 dst->nbthreads = lttngtop.nbthreads;
322 dst->nbnewthreads = lttngtop.nbnewthreads;
323 dst->nbdeadthreads = lttngtop.nbdeadthreads;
324 dst->nbfiles = lttngtop.nbfiles;
325 dst->nbnewfiles = lttngtop.nbnewfiles;
326 dst->nbclosedfiles = lttngtop.nbclosedfiles;
327 reset_global_counters();
328}
329
1fc22eb4
JD
330struct lttngtop* get_copy_lttngtop(unsigned long start, unsigned long end)
331{
332 gint i, j;
333 unsigned long time;
334 struct lttngtop *dst;
335 struct processtop *tmp, *tmp2, *new;
336 struct cputime *tmpcpu, *newcpu;
337 struct files *tmpfile, *newfile;
338
559c9f86 339 dst = g_new0(struct lttngtop, 1);
1fc22eb4
JD
340 dst->start = start;
341 dst->end = end;
e05a35a6 342 copy_global_counters(dst);
1fc22eb4
JD
343 dst->process_table = g_ptr_array_new();
344 dst->files_table = g_ptr_array_new();
345 dst->cpu_table = g_ptr_array_new();
85db4618 346 dst->perf_list = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
347
348 rotate_cputime(end);
349
350 g_hash_table_foreach(lttngtop.perf_list, copy_perf_counter, dst->perf_list);
351 for (i = 0; i < lttngtop.process_table->len; i++) {
352 tmp = g_ptr_array_index(lttngtop.process_table, i);
559c9f86 353 new = g_new0(struct processtop, 1);
1fc22eb4
JD
354
355 memcpy(new, tmp, sizeof(struct processtop));
356 new->threads = g_ptr_array_new();
357 new->comm = strdup(tmp->comm);
358 new->process_files_table = g_ptr_array_new();
ceb3a221 359 new->files_history = tmp->files_history;
85db4618 360 new->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
361 g_hash_table_foreach(tmp->perf, copy_perf_counter, new->perf);
362
1fc22eb4 363 /* compute the stream speed */
85db4618
JD
364 if (end - start != 0) {
365 time = (end - start) / NSEC_PER_SEC;
b093de8a
MB
366 new->fileread = new->fileread/(time);
367 new->filewrite = new->filewrite/(time);
1fc22eb4
JD
368 }
369
370 for (j = 0; j < tmp->process_files_table->len; j++) {
371 tmpfile = g_ptr_array_index(tmp->process_files_table, j);
1fc22eb4 372
b093de8a
MB
373 newfile = malloc(sizeof(struct files));
374
375 if (tmpfile != NULL) {
376 memcpy(newfile, tmpfile, sizeof(struct files));
377 newfile->name = strdup(tmpfile->name);
378 newfile->ref = new;
379 g_ptr_array_add(new->process_files_table,
380 newfile);
381 g_ptr_array_add(dst->files_table, newfile);
382 } else {
383 g_ptr_array_add(new->process_files_table, NULL);
384 g_ptr_array_add(dst->files_table, NULL);
385 }
1fc22eb4
JD
386 /*
387 * if the process died during the last period, we remove all
388 * files associated with if after the copy
389 */
390 if (tmp->death > 0 && tmp->death < end) {
e05a35a6 391 /* FIXME : close the files before */
1fc22eb4 392 g_ptr_array_remove(tmp->process_files_table, tmpfile);
559c9f86 393 g_free(tmpfile);
1fc22eb4
JD
394 }
395 }
396 g_ptr_array_add(dst->process_table, new);
397
398 /*
399 * if the process died during the last period, we remove it from
400 * the current process list after the copy
401 */
402 if (tmp->death > 0 && tmp->death < end) {
403 g_ptr_array_remove(lttngtop.process_table, tmp);
85db4618 404 /* FIXME : TRUE does not mean clears the object in it */
1fc22eb4
JD
405 g_ptr_array_free(tmp->threads, TRUE);
406 free(tmp->comm);
407 g_ptr_array_free(tmp->process_files_table, TRUE);
85db4618 408 /* FIXME : clear elements */
1fc22eb4 409 g_hash_table_destroy(tmp->perf);
559c9f86 410 g_free(tmp);
1fc22eb4
JD
411 }
412 }
413 rotate_perfcounter();
414
415 for (i = 0; i < lttngtop.cpu_table->len; i++) {
416 tmpcpu = g_ptr_array_index(lttngtop.cpu_table, i);
559c9f86 417 newcpu = g_new0(struct cputime, 1);
1fc22eb4 418 memcpy(newcpu, tmpcpu, sizeof(struct cputime));
85db4618 419 newcpu->perf = g_hash_table_new(g_str_hash, g_str_equal);
1fc22eb4
JD
420 g_hash_table_foreach(tmpcpu->perf, copy_perf_counter, newcpu->perf);
421 /*
422 * note : we don't care about the current process pointer in the copy
423 * so the reference is invalid after the memcpy
424 */
425 g_ptr_array_add(dst->cpu_table, newcpu);
426 }
85db4618 427 /* FIXME : better algo */
1fc22eb4
JD
428 /* create the threads index if required */
429 for (i = 0; i < dst->process_table->len; i++) {
430 tmp = g_ptr_array_index(dst->process_table, i);
431 if (tmp->pid == tmp->tid) {
432 for (j = 0; j < dst->process_table->len; j++) {
433 tmp2 = g_ptr_array_index(dst->process_table, j);
434 if (tmp2->pid == tmp->pid) {
435 tmp2->threadparent = tmp;
436 g_ptr_array_add(tmp->threads, tmp2);
437 }
438 }
439 }
440 }
441
442 // update_global_stats(dst);
443 cleanup_processtop();
444
445 return dst;
446}
447
This page took 0.063606 seconds and 4 git commands to generate.