dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / powertop / common / events.c
blob829059fc70b70a1274c5b8f8bf305bd0d138540d
1 /*
2 * Copyright 2009, Intel Corporation
3 * Copyright 2009, Sun Microsystems, Inc
5 * This file is part of PowerTOP
7 * This program file is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program in a file named COPYING; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
22 * Authors:
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Eric C Saxe <eric.saxe@sun.com>
25 * Aubrey Li <aubrey.li@intel.com>
29 * GPL Disclaimer
31 * For the avoidance of doubt, except that if any license choice other
32 * than GPL or LGPL is available it will apply instead, Sun elects to
33 * use only the General Public License version 2 (GPLv2) at this time
34 * for any software where a choice of GPL license versions is made
35 * available with the language indicating that GPLv2 or any later
36 * version may be used, or where a choice of which version of the GPL
37 * is applied is otherwise unspecified.
40 #include <string.h>
41 #include <stdlib.h>
42 #include <dtrace.h>
43 #include "powertop.h"
45 static dtrace_hdl_t *dtp;
46 static event_info_t *event;
48 /*ARGSUSED*/
49 static int
50 pt_events_walk(const dtrace_aggdata_t *data, void *arg)
52 dtrace_aggdesc_t *aggdesc = data->dtada_desc;
53 dtrace_recdesc_t *rec1, *rec2, *rec3;
54 dtrace_syminfo_t dts;
55 GElf_Sym sym;
56 uint64_t offender_addr;
57 uint64_t n = 0;
58 int32_t *instance, *offender_cpu;
59 int i;
60 char *offense_name;
62 if (g_top_events >= EVENT_NUM_MAX)
63 return (0);
65 rec1 = &aggdesc->dtagd_rec[1];
66 rec2 = &aggdesc->dtagd_rec[2];
69 * Report interrupts
71 if (strcmp(aggdesc->dtagd_name, "interrupts") == 0) {
72 offense_name = data->dtada_data + rec1->dtrd_offset;
74 /* LINTED - alignment */
75 instance = (int32_t *)(data->dtada_data + rec2->dtrd_offset);
76 (void) snprintf((char *)(event->offender_name),
77 EVENT_NAME_MAX, "%s", "<interrupt>");
78 (void) snprintf((char *)(event->offense_name),
79 EVENT_NAME_MAX, "%s#%d", offense_name, *instance);
81 * Report kernel events
83 } else if (strcmp(aggdesc->dtagd_name, "events_k") == 0) {
85 (void) snprintf((char *)(event->offender_name),
86 EVENT_NAME_MAX, "%s", "<kernel>");
89 * Casting offender_addr to the wrong type will cause
90 * dtrace_lookup_by_addr to return 0 and the report
91 * to show an address instead of a name.
93 switch (g_bit_depth) {
94 case 32:
95 /* LINTED - alignment */
96 offender_addr = *(uint32_t *)(data->dtada_data +
97 rec1->dtrd_offset);
98 break;
99 case 64:
100 /* LINTED - alignment */
101 offender_addr = *(uint64_t *)(data->dtada_data +
102 rec1->dtrd_offset);
103 break;
107 * We have the address of the kernel callout.
108 * Try to resolve it into a meaningful symbol
110 if (offender_addr != (uintptr_t)NULL &&
111 dtrace_lookup_by_addr(dtp, offender_addr, &sym, &dts)
112 == 0) {
113 (void) snprintf((char *)(event->offense_name),
114 EVENT_NAME_MAX, "%s`%s", dts.dts_object,
115 dts.dts_name);
116 } else {
117 (void) snprintf((char *)(event->offense_name),
118 EVENT_NAME_MAX, "0x%llx", offender_addr);
121 * Report user events
123 } else if (strcmp(aggdesc->dtagd_name, "events_u") == 0) {
124 offense_name = data->dtada_data + rec1->dtrd_offset;
126 (void) snprintf((char *)(event->offender_name),
127 EVENT_NAME_MAX, "%s", offense_name);
128 (void) snprintf((char *)(event->offense_name),
129 EVENT_NAME_MAX, "<scheduled timeout expiration>");
131 * Report cross calls
133 } else if (strcmp(aggdesc->dtagd_name, "events_x") == 0) {
134 offense_name = data->dtada_data + rec1->dtrd_offset;
136 (void) snprintf((char *)(event->offender_name),
137 EVENT_NAME_MAX, "%s", offense_name);
139 switch (g_bit_depth) {
140 case 32:
141 /* LINTED - alignment */
142 offender_addr = *(uint32_t *)(data->dtada_data +
143 rec2->dtrd_offset);
144 break;
145 case 64:
146 /* LINTED - alignment */
147 offender_addr = *(uint64_t *)(data->dtada_data +
148 rec2->dtrd_offset);
149 break;
153 * Try to resolve the address of the cross call function.
155 if (offender_addr != (uintptr_t)NULL &&
156 dtrace_lookup_by_addr(dtp, offender_addr, &sym, &dts)
157 == 0) {
158 (void) snprintf((char *)(event->offense_name),
159 EVENT_NAME_MAX, "<xcalls> %s`%s",
160 dts.dts_object, dts.dts_name);
161 } else {
162 (void) snprintf((char *)(event->offense_name),
163 EVENT_NAME_MAX, "<xcalls>");
166 * Report cross calls from other CPUs than the one we're observing
167 * with the -C option
169 } else if (strcmp(aggdesc->dtagd_name, "events_xc") == 0) {
170 rec3 = &aggdesc->dtagd_rec[3];
171 offense_name = data->dtada_data + rec1->dtrd_offset;
173 (void) snprintf((char *)(event->offender_name),
174 EVENT_NAME_MAX, "%s", offense_name);
176 switch (g_bit_depth) {
177 case 32:
178 /* LINTED - alignment */
179 offender_addr = *(uint32_t *)(data->dtada_data +
180 rec2->dtrd_offset);
181 break;
182 case 64:
183 /* LINTED - alignment */
184 offender_addr = *(uint64_t *)(data->dtada_data +
185 rec2->dtrd_offset);
186 break;
188 /* LINTED - alignment */
189 offender_cpu = (int32_t *)(data->dtada_data +
190 rec3->dtrd_offset);
193 * Try to resolve the address of the cross call function.
195 if (offender_addr != (uintptr_t)NULL &&
196 dtrace_lookup_by_addr(dtp, offender_addr, &sym, &dts)
197 == 0) {
198 (void) snprintf((char *)(event->offense_name),
199 EVENT_NAME_MAX, "<xcalls> %s`%s (CPU %d)",
200 dts.dts_object, dts.dts_name, *offender_cpu);
201 } else {
202 (void) snprintf((char *)(event->offense_name),
203 EVENT_NAME_MAX, "<xcalls> (CPU %d)",
204 *offender_cpu);
207 * Report unknown events
209 } else {
210 (void) snprintf((char *)(event->offender_name),
211 EVENT_NAME_MAX, "%s", "<unknown>");
212 (void) snprintf((char *)(event->offense_name),
213 EVENT_NAME_MAX, "%s", "<unknown>");
216 for (i = 0; i < g_ncpus; i++) {
217 /* LINTED - alignment */
218 n += *((uint64_t *)(data->dtada_percpu[i]));
221 event->total_count = n;
223 event++;
224 g_top_events++;
226 return (DTRACE_AGGWALK_NEXT);
230 pt_events_stat_prepare(void)
232 dtrace_prog_t *prog;
233 dtrace_proginfo_t info;
234 dtrace_optval_t statustime;
235 int err;
236 char *prog_ptr;
238 event = g_event_info;
240 if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
241 pt_error("cannot open dtrace library for the event report: "
242 "%s\n", dtrace_errmsg(NULL, err));
243 return (-1);
247 * Execute different scripts (defined in the platform specific file)
248 * depending on user specified options.
250 if (PT_ON_VERBOSE) {
251 prog_ptr = (char *)g_dtp_events_v;
252 } else {
253 if (PT_ON_CPU)
254 prog_ptr = (char *)g_dtp_events_c;
255 else
256 prog_ptr = (char *)g_dtp_events;
259 if ((prog = dtrace_program_strcompile(dtp, prog_ptr,
260 DTRACE_PROBESPEC_NAME, 0, g_argc, g_argv)) == NULL) {
261 pt_error("failed to compile the event report program\n");
262 return (dtrace_errno(dtp));
265 if (dtrace_program_exec(dtp, prog, &info) == -1) {
266 pt_error("failed to enable probes for the event report\n");
267 return (dtrace_errno(dtp));
270 if (dtrace_setopt(dtp, "aggsize", "128k") == -1) {
271 pt_error("failed to set 'aggsize' for the event report\n");
272 return (dtrace_errno(dtp));
275 if (dtrace_setopt(dtp, "aggrate", "0") == -1) {
276 pt_error("failed to set 'aggrate' for the event report\n");
277 return (dtrace_errno(dtp));
280 if (dtrace_setopt(dtp, "aggpercpu", 0) == -1) {
281 pt_error("failed to set 'aggpercpu' for the event report\n");
282 return (dtrace_errno(dtp));
285 if (dtrace_go(dtp) != 0) {
286 pt_error("failed to start the event report observation\n");
287 return (dtrace_errno(dtp));
290 if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) {
291 pt_error("failed to get 'statusrate' for the event report\n");
292 return (dtrace_errno(dtp));
295 return (0);
299 pt_events_stat_collect(void)
301 g_top_events = 0;
302 event = g_event_info;
304 if (dtrace_status(dtp) == -1)
305 return (-1);
307 if (dtrace_aggregate_snap(dtp) != 0)
308 pt_error("failed to collect data for the event report\n");
310 if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_events_walk, NULL) != 0)
311 pt_error("failed to sort data for the event report\n");
313 dtrace_aggregate_clear(dtp);
315 return (0);