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
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
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Eric C Saxe <eric.saxe@sun.com>
25 * Aubrey Li <aubrey.li@intel.com>
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.
45 static dtrace_hdl_t
*dtp
;
46 static event_info_t
*event
;
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
;
56 uint64_t offender_addr
;
58 int32_t *instance
, *offender_cpu
;
62 if (g_top_events
>= EVENT_NUM_MAX
)
65 rec1
= &aggdesc
->dtagd_rec
[1];
66 rec2
= &aggdesc
->dtagd_rec
[2];
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
) {
95 /* LINTED - alignment */
96 offender_addr
= *(uint32_t *)(data
->dtada_data
+
100 /* LINTED - alignment */
101 offender_addr
= *(uint64_t *)(data
->dtada_data
+
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
)
113 (void) snprintf((char *)(event
->offense_name
),
114 EVENT_NAME_MAX
, "%s`%s", dts
.dts_object
,
117 (void) snprintf((char *)(event
->offense_name
),
118 EVENT_NAME_MAX
, "0x%llx", offender_addr
);
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>");
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
) {
141 /* LINTED - alignment */
142 offender_addr
= *(uint32_t *)(data
->dtada_data
+
146 /* LINTED - alignment */
147 offender_addr
= *(uint64_t *)(data
->dtada_data
+
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
)
158 (void) snprintf((char *)(event
->offense_name
),
159 EVENT_NAME_MAX
, "<xcalls> %s`%s",
160 dts
.dts_object
, dts
.dts_name
);
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
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
) {
178 /* LINTED - alignment */
179 offender_addr
= *(uint32_t *)(data
->dtada_data
+
183 /* LINTED - alignment */
184 offender_addr
= *(uint64_t *)(data
->dtada_data
+
188 /* LINTED - alignment */
189 offender_cpu
= (int32_t *)(data
->dtada_data
+
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
)
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
);
202 (void) snprintf((char *)(event
->offense_name
),
203 EVENT_NAME_MAX
, "<xcalls> (CPU %d)",
207 * Report unknown events
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
;
226 return (DTRACE_AGGWALK_NEXT
);
230 pt_events_stat_prepare(void)
233 dtrace_proginfo_t info
;
234 dtrace_optval_t statustime
;
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
));
247 * Execute different scripts (defined in the platform specific file)
248 * depending on user specified options.
251 prog_ptr
= (char *)g_dtp_events_v
;
254 prog_ptr
= (char *)g_dtp_events_c
;
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
));
299 pt_events_stat_collect(void)
302 event
= g_event_info
;
304 if (dtrace_status(dtp
) == -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
);