xref: /illumos-gate/usr/src/cmd/powertop/common/cpuidle.c (revision d67944fbe3fa0b31893a7116a09b0718eecf6078)
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 	 * Assume that all the time spent in this interval will
167 	 * be the default "0" state. The DTrace walker will reallocate
168 	 * time out of the default bucket as it processes aggregation
169 	 * records for time spent in other states.
170 	 */
171 	g_cstate_info[0].total_time = (long)(interval * g_ncpus_observed *
172 	    1000);
173 
174 	if (dtrace_status(dtp) == -1)
175 		return (-1);
176 
177 	if (dtrace_aggregate_snap(dtp) != 0)
178 		pt_error("%s : failed to add to aggregation", __FILE__);
179 
180 	if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_cpuidle_dtrace_walk,
181 	    NULL) != 0)
182 		pt_error("%s : failed to sort aggregation", __FILE__);
183 
184 	dtrace_aggregate_clear(dtp);
185 
186 	/*
187 	 * Populate g_cstate_info with the correct amount of time spent
188 	 * in each C state and update the number of C states in g_max_cstate
189 	 */
190 	g_total_c_time = 0;
191 	for (i = 0; i < NSTATES; i++) {
192 		if (g_cstate_info[i].total_time > 0) {
193 			g_total_c_time += g_cstate_info[i].total_time;
194 			if (i > g_max_cstate)
195 				g_max_cstate = i;
196 			if (g_cstate_info[i].last_time > t) {
197 				t = g_cstate_info[i].last_time;
198 				g_longest_cstate = i;
199 			}
200 		}
201 	}
202 
203 	return (0);
204 }
205 
206 /*
207  * DTrace aggregation walker that sorts through a snapshot of data records
208  * collected during firings of the idle-state-transition probe.
209  *
210  * XXX A way of querying the current idle state for a CPU is needed in addition
211  *     to logic similar to that in cpufreq.c
212  */
213 /*ARGSUSED*/
214 static int
215 pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *data, void *arg)
216 {
217 	dtrace_aggdesc_t 	*aggdesc = data->dtada_desc;
218 	dtrace_recdesc_t 	*rec;
219 	uint64_t 		n = 0;
220 	int32_t 		state;
221 	int 			i;
222 
223 	rec = &aggdesc->dtagd_rec[1];
224 	/* LINTED - alignment */
225 	state = *(int32_t *)(data->dtada_data + rec->dtrd_offset);
226 
227 	if (strcmp(aggdesc->dtagd_name, "number") == 0) {
228 		for (i = 0; i < g_ncpus; i++) {
229 			/* LINTED - alignment */
230 			n += *((uint64_t *)(data->dtada_percpu[i]));
231 		}
232 		g_total_events += n;
233 		g_cstate_info[state].events += n;
234 	}
235 	else
236 		if (strcmp(aggdesc->dtagd_name, "times") == 0) {
237 			for (i = 0; i < g_ncpus; i++) {
238 				/* LINTED - alignment */
239 				n += *((uint64_t *)(data->dtada_percpu[i]));
240 			}
241 			g_cstate_info[state].last_time = n;
242 			g_cstate_info[state].total_time += n;
243 			if (g_cstate_info[0].total_time >= n)
244 				g_cstate_info[0].total_time -= n;
245 		}
246 
247 	return (DTRACE_AGGWALK_NEXT);
248 }
249