Filter based on procname
[lttngtop.git] / src / cputop.c
CommitLineData
1fc22eb4 1/*
aa15ac1c 2 * Copyright (C) 2011-2012 Julien Desfossez
1fc22eb4
JD
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
18#include <babeltrace/babeltrace.h>
19
20#include "lttngtoptypes.h"
21#include "common.h"
22#include "cputop.h"
6e11e0d0 23#include "lttngtop.h"
1fc22eb4
JD
24
25void update_cputop_data(unsigned long timestamp, int64_t cpu, int prev_pid,
906c08f6 26 int next_pid, char *prev_comm, char *next_comm, char *hostname)
1fc22eb4
JD
27{
28 struct cputime *tmpcpu;
29 unsigned long elapsed;
30
31 tmpcpu = get_cpu(cpu);
32
33 if (tmpcpu->current_task && tmpcpu->current_task->pid == prev_pid) {
34 elapsed = timestamp - tmpcpu->task_start;
35 tmpcpu->current_task->totalcpunsec += elapsed;
36 tmpcpu->current_task->threadstotalcpunsec += elapsed;
050f9cf9
JD
37 if (tmpcpu->current_task->threadparent &&
38 tmpcpu->current_task->pid != tmpcpu->current_task->tid)
1fc22eb4
JD
39 tmpcpu->current_task->threadparent->threadstotalcpunsec += elapsed;
40 }
41
42 if (next_pid != 0)
906c08f6
JD
43 tmpcpu->current_task = get_proc(&lttngtop, next_pid, next_comm,
44 timestamp, hostname);
1fc22eb4
JD
45 else
46 tmpcpu->current_task = NULL;
47
48 tmpcpu->task_start = timestamp;
49}
50
4adc8274 51enum bt_cb_ret handle_sched_switch(struct bt_ctf_event *call_data,
1fc22eb4
JD
52 void *private_data)
53{
2e0a1190 54 const struct bt_definition *scope;
1fc22eb4
JD
55 unsigned long timestamp;
56 uint64_t cpu_id;
57 char *prev_comm, *next_comm;
58 int prev_tid, next_tid;
1d2391b4 59 char *hostname = NULL;
1fc22eb4 60
c78f2cdc 61 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
62 if (timestamp == -1ULL)
63 goto error;
64
65 scope = bt_ctf_get_top_level_scope(call_data,
66 BT_EVENT_FIELDS);
67 prev_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
68 scope, "_prev_comm"));
69 if (bt_ctf_field_get_error()) {
70 fprintf(stderr, "Missing prev_comm context info\n");
71 goto error;
72 }
73
74 next_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
75 scope, "_next_comm"));
76 if (bt_ctf_field_get_error()) {
77 fprintf(stderr, "Missing next_comm context info\n");
78 goto error;
79 }
80
81 prev_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
82 scope, "_prev_tid"));
83 if (bt_ctf_field_get_error()) {
84 fprintf(stderr, "Missing prev_tid context info\n");
85 goto error;
86 }
87
88 next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
89 scope, "_next_tid"));
90 if (bt_ctf_field_get_error()) {
91 fprintf(stderr, "Missing next_tid context info\n");
92 goto error;
93 }
94
d67167cd 95 cpu_id = get_cpu_id(call_data);
1fc22eb4
JD
96
97 update_cputop_data(timestamp, cpu_id, prev_tid, next_tid,
906c08f6 98 prev_comm, next_comm, hostname);
1fc22eb4
JD
99
100 return BT_CB_OK;
101
102error:
103 return BT_CB_ERROR_STOP;
104}
105
4adc8274 106enum bt_cb_ret handle_sched_process_free(struct bt_ctf_event *call_data,
1fc22eb4
JD
107 void *private_data)
108{
2e0a1190 109 const struct bt_definition *scope;
1fc22eb4
JD
110 unsigned long timestamp;
111 char *comm;
112 int tid;
113
c78f2cdc 114 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
115 if (timestamp == -1ULL)
116 goto error;
117
118 scope = bt_ctf_get_top_level_scope(call_data,
119 BT_EVENT_FIELDS);
120 comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
121 scope, "_comm"));
122 if (bt_ctf_field_get_error()) {
123 fprintf(stderr, "Missing procname context info\n");
124 goto error;
125 }
126
e05a35a6
JD
127 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
128 scope, "_tid"));
129 if (bt_ctf_field_get_error()) {
130 fprintf(stderr, "Missing tid field\n");
131 goto error;
132 }
54761a45
JD
133 if (tid == opt_exec_pid) {
134 quit = 1;
135 }
e05a35a6 136
1fc22eb4
JD
137 death_proc(&lttngtop, tid, comm, timestamp);
138
139 return BT_CB_OK;
140
141error:
142 return BT_CB_ERROR_STOP;
143
144}
145
6e11e0d0
JD
146enum bt_cb_ret handle_sched_process_fork(struct bt_ctf_event *call_data,
147 void *private_data)
148{
149 const struct bt_definition *scope;
150 struct processtop *tmp;
151 int tid, *hash_tid, parent_pid;
152 unsigned long timestamp;
153 char *comm;
154
155 timestamp = bt_ctf_get_timestamp(call_data);
156 if (timestamp == -1ULL)
157 goto error;
158
159 scope = bt_ctf_get_top_level_scope(call_data,
160 BT_EVENT_FIELDS);
161 comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
162 scope, "_child_comm"));
163 if (bt_ctf_field_get_error()) {
164 fprintf(stderr, "Missing procname context info\n");
165 goto error;
166 }
167
168 tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
169 scope, "_child_tid"));
170 if (bt_ctf_field_get_error()) {
171 fprintf(stderr, "Missing child_tid field\n");
172 goto error;
173 }
174
175 parent_pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
176 scope, "_parent_pid"));
177 if (bt_ctf_field_get_error()) {
178 fprintf(stderr, "Missing parent_pid field\n");
179 goto error;
180 }
181
182 tmp = get_proc(&lttngtop, tid, comm, timestamp, NULL);
183
184 if (opt_child) {
185 hash_tid = lookup_filter_tid_list(parent_pid);
186 if (hash_tid) {
187 add_filter_tid_list(tmp);
188 }
189 }
190
191 return BT_CB_OK;
192
193error:
194 return BT_CB_ERROR_STOP;
195
196}
197
This page took 0.038563 seconds and 4 git commands to generate.