add_proc now takes hostname param
[lttngtop.git] / src / cputop.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/babeltrace.h>
19
20 #include "lttngtoptypes.h"
21 #include "common.h"
22 #include "cputop.h"
23
24 void update_cputop_data(unsigned long timestamp, int64_t cpu, int prev_pid,
25 int next_pid, char *prev_comm, char *next_comm, char *hostname)
26 {
27 struct cputime *tmpcpu;
28 unsigned long elapsed;
29
30 tmpcpu = get_cpu(cpu);
31
32 if (tmpcpu->current_task && tmpcpu->current_task->pid == prev_pid) {
33 elapsed = timestamp - tmpcpu->task_start;
34 tmpcpu->current_task->totalcpunsec += elapsed;
35 tmpcpu->current_task->threadstotalcpunsec += elapsed;
36 if (tmpcpu->current_task->threadparent &&
37 tmpcpu->current_task->pid != tmpcpu->current_task->tid)
38 tmpcpu->current_task->threadparent->threadstotalcpunsec += elapsed;
39 }
40
41 if (next_pid != 0)
42 tmpcpu->current_task = get_proc(&lttngtop, next_pid, next_comm,
43 timestamp, hostname);
44 else
45 tmpcpu->current_task = NULL;
46
47 tmpcpu->task_start = timestamp;
48 }
49
50 enum bt_cb_ret handle_sched_switch(struct bt_ctf_event *call_data,
51 void *private_data)
52 {
53 const struct bt_definition *scope;
54 unsigned long timestamp;
55 uint64_t cpu_id;
56 char *prev_comm, *next_comm;
57 int prev_tid, next_tid;
58 char *hostname;
59
60 timestamp = bt_ctf_get_timestamp(call_data);
61 if (timestamp == -1ULL)
62 goto error;
63
64 scope = bt_ctf_get_top_level_scope(call_data,
65 BT_EVENT_FIELDS);
66 prev_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
67 scope, "_prev_comm"));
68 if (bt_ctf_field_get_error()) {
69 fprintf(stderr, "Missing prev_comm context info\n");
70 goto error;
71 }
72
73 next_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data,
74 scope, "_next_comm"));
75 if (bt_ctf_field_get_error()) {
76 fprintf(stderr, "Missing next_comm context info\n");
77 goto error;
78 }
79
80 prev_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
81 scope, "_prev_tid"));
82 if (bt_ctf_field_get_error()) {
83 fprintf(stderr, "Missing prev_tid context info\n");
84 goto error;
85 }
86
87 next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
88 scope, "_next_tid"));
89 if (bt_ctf_field_get_error()) {
90 fprintf(stderr, "Missing next_tid context info\n");
91 goto error;
92 }
93 hostname = get_context_hostname(call_data);
94
95 cpu_id = get_cpu_id(call_data);
96
97 update_cputop_data(timestamp, cpu_id, prev_tid, next_tid,
98 prev_comm, next_comm, hostname);
99
100 return BT_CB_OK;
101
102 error:
103 return BT_CB_ERROR_STOP;
104 }
105
106 enum bt_cb_ret handle_sched_process_free(struct bt_ctf_event *call_data,
107 void *private_data)
108 {
109 const struct bt_definition *scope;
110 unsigned long timestamp;
111 char *comm;
112 int tid;
113
114 timestamp = bt_ctf_get_timestamp(call_data);
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
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 }
133
134 death_proc(&lttngtop, tid, comm, timestamp);
135
136 return BT_CB_OK;
137
138 error:
139 return BT_CB_ERROR_STOP;
140
141 }
142
This page took 0.032747 seconds and 5 git commands to generate.