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
!= NULL
&& dtrace_lookup_by_addr(dtp
,
111 offender_addr
, &sym
, &dts
) == 0) {
112 (void) snprintf((char *)(event
->offense_name
),
113 EVENT_NAME_MAX
, "%s`%s", dts
.dts_object
,
116 (void) snprintf((char *)(event
->offense_name
),
117 EVENT_NAME_MAX
, "0x%llx", offender_addr
);
122 } else if (strcmp(aggdesc
->dtagd_name
, "events_u") == 0) {
123 offense_name
= data
->dtada_data
+ rec1
->dtrd_offset
;
125 (void) snprintf((char *)(event
->offender_name
),
126 EVENT_NAME_MAX
, "%s", offense_name
);
127 (void) snprintf((char *)(event
->offense_name
),
128 EVENT_NAME_MAX
, "<scheduled timeout expiration>");
132 } else if (strcmp(aggdesc
->dtagd_name
, "events_x") == 0) {
133 offense_name
= data
->dtada_data
+ rec1
->dtrd_offset
;
135 (void) snprintf((char *)(event
->offender_name
),
136 EVENT_NAME_MAX
, "%s", offense_name
);
138 switch (g_bit_depth
) {
140 /* LINTED - alignment */
141 offender_addr
= *(uint32_t *)(data
->dtada_data
+
145 /* LINTED - alignment */
146 offender_addr
= *(uint64_t *)(data
->dtada_data
+
152 * Try to resolve the address of the cross call function.
154 if (offender_addr
!= NULL
&& dtrace_lookup_by_addr(dtp
,
155 offender_addr
, &sym
, &dts
) == 0) {
156 (void) snprintf((char *)(event
->offense_name
),
157 EVENT_NAME_MAX
, "<xcalls> %s`%s",
158 dts
.dts_object
, dts
.dts_name
);
160 (void) snprintf((char *)(event
->offense_name
),
161 EVENT_NAME_MAX
, "<xcalls>");
164 * Report cross calls from other CPUs than the one we're observing
167 } else if (strcmp(aggdesc
->dtagd_name
, "events_xc") == 0) {
168 rec3
= &aggdesc
->dtagd_rec
[3];
169 offense_name
= data
->dtada_data
+ rec1
->dtrd_offset
;
171 (void) snprintf((char *)(event
->offender_name
),
172 EVENT_NAME_MAX
, "%s", offense_name
);
174 switch (g_bit_depth
) {
176 /* LINTED - alignment */
177 offender_addr
= *(uint32_t *)(data
->dtada_data
+
181 /* LINTED - alignment */
182 offender_addr
= *(uint64_t *)(data
->dtada_data
+
186 /* LINTED - alignment */
187 offender_cpu
= (int32_t *)(data
->dtada_data
+
191 * Try to resolve the address of the cross call function.
193 if (offender_addr
!= NULL
&& dtrace_lookup_by_addr(dtp
,
194 offender_addr
, &sym
, &dts
) == 0) {
195 (void) snprintf((char *)(event
->offense_name
),
196 EVENT_NAME_MAX
, "<xcalls> %s`%s (CPU %d)",
197 dts
.dts_object
, dts
.dts_name
, *offender_cpu
);
199 (void) snprintf((char *)(event
->offense_name
),
200 EVENT_NAME_MAX
, "<xcalls> (CPU %d)",
204 * Report unknown events
207 (void) snprintf((char *)(event
->offender_name
),
208 EVENT_NAME_MAX
, "%s", "<unknown>");
209 (void) snprintf((char *)(event
->offense_name
),
210 EVENT_NAME_MAX
, "%s", "<unknown>");
213 for (i
= 0; i
< g_ncpus
; i
++) {
214 /* LINTED - alignment */
215 n
+= *((uint64_t *)(data
->dtada_percpu
[i
]));
218 event
->total_count
= n
;
223 return (DTRACE_AGGWALK_NEXT
);
227 pt_events_stat_prepare(void)
230 dtrace_proginfo_t info
;
231 dtrace_optval_t statustime
;
235 event
= g_event_info
;
237 if ((dtp
= dtrace_open(DTRACE_VERSION
, 0, &err
)) == NULL
) {
238 pt_error("cannot open dtrace library for the event report: "
239 "%s\n", dtrace_errmsg(NULL
, err
));
244 * Execute different scripts (defined in the platform specific file)
245 * depending on user specified options.
248 prog_ptr
= (char *)g_dtp_events_v
;
251 prog_ptr
= (char *)g_dtp_events_c
;
253 prog_ptr
= (char *)g_dtp_events
;
256 if ((prog
= dtrace_program_strcompile(dtp
, prog_ptr
,
257 DTRACE_PROBESPEC_NAME
, 0, g_argc
, g_argv
)) == NULL
) {
258 pt_error("failed to compile the event report program\n");
259 return (dtrace_errno(dtp
));
262 if (dtrace_program_exec(dtp
, prog
, &info
) == -1) {
263 pt_error("failed to enable probes for the event report\n");
264 return (dtrace_errno(dtp
));
267 if (dtrace_setopt(dtp
, "aggsize", "128k") == -1) {
268 pt_error("failed to set 'aggsize' for the event report\n");
269 return (dtrace_errno(dtp
));
272 if (dtrace_setopt(dtp
, "aggrate", "0") == -1) {
273 pt_error("failed to set 'aggrate' for the event report\n");
274 return (dtrace_errno(dtp
));
277 if (dtrace_setopt(dtp
, "aggpercpu", 0) == -1) {
278 pt_error("failed to set 'aggpercpu' for the event report\n");
279 return (dtrace_errno(dtp
));
282 if (dtrace_go(dtp
) != 0) {
283 pt_error("failed to start the event report observation\n");
284 return (dtrace_errno(dtp
));
287 if (dtrace_getopt(dtp
, "statusrate", &statustime
) == -1) {
288 pt_error("failed to get 'statusrate' for the event report\n");
289 return (dtrace_errno(dtp
));
296 pt_events_stat_collect(void)
299 event
= g_event_info
;
301 if (dtrace_status(dtp
) == -1)
304 if (dtrace_aggregate_snap(dtp
) != 0)
305 pt_error("failed to collect data for the event report\n");
307 if (dtrace_aggregate_walk_keyvarsorted(dtp
, pt_events_walk
, NULL
) != 0)
308 pt_error("failed to sort data for the event report\n");
310 dtrace_aggregate_clear(dtp
);