xref: /illumos-gate/usr/src/cmd/powertop/common/cpuidle.c (revision dde769a2c00c82faaf80563ddd5610de2f4da339)
1 /*
2  * Copyright 2009, Intel Corporation
3  * Copyright 2009, Sun Microsystems, Inc
4  *
5  * This file is part of PowerTOP
6  *
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.
10  *
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.
15  *
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
21  *
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>
26  */
27 
28 /*
29  * GPL Disclaimer
30  *
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.
38  */
39 
40 #include <string.h>
41 #include <dtrace.h>
42 #include "powertop.h"
43 
44 static dtrace_hdl_t 	*dtp;
45 
46 /*
47  * Buffer containing DTrace program to track CPU idle state transitions
48  */
49 static const char *dtp_cpuidle =
50 ":::idle-state-transition"
51 "/arg0 != 0/"
52 "{"
53 "	self->start = timestamp;"
54 "	self->state = arg0;"
55 "}"
56 ""
57 ":::idle-state-transition"
58 "/arg0 == 0 && self->start/"
59 "{"
60 "	@number[self->state] = count();"
61 "	@times[self->state] = sum((timestamp - self->start)/1000000);"
62 "	self->start = 0;"
63 "	self->state = 0;"
64 "}";
65 
66 /*
67  * Same as above but only for a specific CPU
68  */
69 static const char *dtp_cpuidle_c =
70 ":::idle-state-transition"
71 "/cpu == $0 &&"
72 " arg0 != 0/"
73 "{"
74 "	self->start = timestamp;"
75 "	self->state = arg0;"
76 "}"
77 ""
78 ":::idle-state-transition"
79 "/cpu == $0 &&"
80 " arg0 == 0 && self->start/"
81 "{"
82 "	@number[self->state] = count();"
83 "	@times[self->state] = sum((timestamp - self->start)/1000000);"
84 "	self->start = 0;"
85 "	self->state = 0;"
86 "}";
87 
88 static int 	pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *, void *);
89 
90 /*
91  * Perform setup necessary to track CPU idle state transitions
92  */
93 int
94 pt_cpuidle_stat_prepare(void)
95 {
96 	dtrace_prog_t 		*prog;
97 	dtrace_proginfo_t 	info;
98 	dtrace_optval_t 	statustime;
99 	int 			err;
100 	char			*prog_ptr;
101 
102 	if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
103 		pt_error("%s : cannot open dtrace library: %s\n", __FILE__,
104 		    dtrace_errmsg(NULL, err));
105 		return (-1);
106 	}
107 
108 	/*
109 	 * Execute different scripts (defined above) depending on
110 	 * user specified options.
111 	 */
112 	if (PTOP_ON_CPU)
113 		prog_ptr = (char *)dtp_cpuidle_c;
114 	else
115 		prog_ptr = (char *)dtp_cpuidle;
116 
117 	if ((prog = dtrace_program_strcompile(dtp, prog_ptr,
118 	    DTRACE_PROBESPEC_NAME, 0, g_argc, g_argv)) == NULL) {
119 		pt_error("%s : C-State DTrace probes unavailable\n", __FILE__);
120 		return (dtrace_errno(dtp));
121 	}
122 
123 	if (dtrace_program_exec(dtp, prog, &info) == -1) {
124 		pt_error("%s : failed to enable C State probes\n", __FILE__);
125 		return (dtrace_errno(dtp));
126 	}
127 
128 	if (dtrace_setopt(dtp, "aggsize", "128k") == -1) {
129 		pt_error("%s : failed to set C-state 'aggsize'\n", __FILE__);
130 	}
131 
132 	if (dtrace_setopt(dtp, "aggrate", "0") == -1) {
133 		pt_error("%s : failed to set C-state'aggrate'\n", __FILE__);
134 	}
135 
136 	if (dtrace_setopt(dtp, "aggpercpu", 0) == -1) {
137 		pt_error("%s : failed to set C-state 'aggpercpu'\n", __FILE__);
138 	}
139 
140 	if (dtrace_go(dtp) != 0) {
141 		pt_error("%s : failed to start C-state observation", __FILE__);
142 		return (dtrace_errno(dtp));
143 	}
144 
145 	if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) {
146 		pt_error("%s : failed to get C-state 'statusrate'\n", __FILE__);
147 		return (dtrace_errno(dtp));
148 	}
149 
150 	return (0);
151 }
152 
153 /*
154  * The DTrace probes have been enabled, and are tracking CPU idle state
155  * transitions. Take a snapshot of the aggregations, and invoke the aggregation
156  * walker to process any records. The walker does most of the accounting work
157  * chalking up time spent into the g_cstate_info structure.
158  */
159 int
160 pt_cpuidle_stat_collect(double interval)
161 {
162 	int 		i;
163 	hrtime_t	t = 0;
164 
165 	/*
166 	 * Zero out the interval time reported by DTrace for
167 	 * this interval
168 	 */
169 	for (i = 0; i < NSTATES; i++) {
170 		g_cstate_info[i].total_time = 0;
171 		g_cstate_info[i].events = 0;
172 	}
173 
174 	/*
175 	 * Assume that all the time spent in this interval will
176 	 * be the default "0" state. The DTrace walker will reallocate
177 	 * time out of the default bucket as it processes aggregation
178 	 * records for time spent in other states.
179 	 */
180 	g_cstate_info[0].total_time = (long)(interval * g_ncpus_observed *
181 	    1000);
182 
183 	if (dtrace_status(dtp) == -1)
184 		return (-1);
185 
186 	if (dtrace_aggregate_snap(dtp) != 0)
187 		pt_error("%s : failed to add to aggregation", __FILE__);
188 
189 	if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_cpuidle_dtrace_walk,
190 	    NULL) != 0)
191 		pt_error("%s : failed to sort aggregation", __FILE__);
192 
193 	dtrace_aggregate_clear(dtp);
194 
195 	/*
196 	 * Populate g_cstate_info with the correct amount of time spent
197 	 * in each C state and update the number of C states in g_max_cstate
198 	 */
199 	g_total_c_time = 0;
200 	for (i = 0; i < NSTATES; i++) {
201 		if (g_cstate_info[i].total_time > 0) {
202 			g_total_c_time += g_cstate_info[i].total_time;
203 			if (i > g_max_cstate)
204 				g_max_cstate = i;
205 			if (g_cstate_info[i].last_time > t) {
206 				t = g_cstate_info[i].last_time;
207 				g_longest_cstate = i;
208 			}
209 		}
210 	}
211 
212 	return (0);
213 }
214 
215 /*
216  * DTrace aggregation walker that sorts through a snapshot of data records
217  * collected during firings of the idle-state-transition probe.
218  *
219  * XXX A way of querying the current idle state for a CPU is needed in addition
220  *     to logic similar to that in cpufreq.c
221  */
222 /*ARGSUSED*/
223 static int
224 pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *data, void *arg)
225 {
226 	dtrace_aggdesc_t 	*aggdesc = data->dtada_desc;
227 	dtrace_recdesc_t 	*rec;
228 	uint64_t 		n = 0;
229 	int32_t 		state;
230 	int 			i;
231 
232 	rec = &aggdesc->dtagd_rec[1];
233 	/* LINTED - alignment */
234 	state = *(int32_t *)(data->dtada_data + rec->dtrd_offset);
235 
236 	if (strcmp(aggdesc->dtagd_name, "number") == 0) {
237 		for (i = 0; i < g_ncpus; i++) {
238 			/* LINTED - alignment */
239 			n += *((uint64_t *)(data->dtada_percpu[i]));
240 		}
241 		g_total_events += n;
242 		g_cstate_info[state].events += n;
243 	}
244 	else
245 		if (strcmp(aggdesc->dtagd_name, "times") == 0) {
246 			for (i = 0; i < g_ncpus; i++) {
247 				/* LINTED - alignment */
248 				n += *((uint64_t *)(data->dtada_percpu[i]));
249 			}
250 			g_cstate_info[state].last_time = n;
251 			g_cstate_info[state].total_time += n;
252 			if (g_cstate_info[0].total_time >= n)
253 				g_cstate_info[0].total_time -= n;
254 		}
255 
256 	return (DTRACE_AGGWALK_NEXT);
257 }
258