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