xref: /titanic_41/usr/src/cmd/powertop/common/util.c (revision b47b5b34b42fa8056577c43496cdb99a4c99f8d7)
1*b47b5b34SRafael Vanoni /*
2*b47b5b34SRafael Vanoni  * Copyright 2009, Intel Corporation
3*b47b5b34SRafael Vanoni  * Copyright 2009, Sun Microsystems, Inc
4*b47b5b34SRafael Vanoni  *
5*b47b5b34SRafael Vanoni  * This file is part of PowerTOP
6*b47b5b34SRafael Vanoni  *
7*b47b5b34SRafael Vanoni  * This program file is free software; you can redistribute it and/or modify it
8*b47b5b34SRafael Vanoni  * under the terms of the GNU General Public License as published by the
9*b47b5b34SRafael Vanoni  * Free Software Foundation; version 2 of the License.
10*b47b5b34SRafael Vanoni  *
11*b47b5b34SRafael Vanoni  * This program is distributed in the hope that it will be useful, but WITHOUT
12*b47b5b34SRafael Vanoni  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*b47b5b34SRafael Vanoni  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*b47b5b34SRafael Vanoni  * for more details.
15*b47b5b34SRafael Vanoni  *
16*b47b5b34SRafael Vanoni  * You should have received a copy of the GNU General Public License
17*b47b5b34SRafael Vanoni  * along with this program in a file named COPYING; if not, write to the
18*b47b5b34SRafael Vanoni  * Free Software Foundation, Inc.,
19*b47b5b34SRafael Vanoni  * 51 Franklin Street, Fifth Floor,
20*b47b5b34SRafael Vanoni  * Boston, MA 02110-1301 USA
21*b47b5b34SRafael Vanoni  *
22*b47b5b34SRafael Vanoni  * Authors:
23*b47b5b34SRafael Vanoni  *	Arjan van de Ven <arjan@linux.intel.com>
24*b47b5b34SRafael Vanoni  *	Eric C Saxe <eric.saxe@sun.com>
25*b47b5b34SRafael Vanoni  *	Aubrey Li <aubrey.li@intel.com>
26*b47b5b34SRafael Vanoni  */
27*b47b5b34SRafael Vanoni 
28*b47b5b34SRafael Vanoni /*
29*b47b5b34SRafael Vanoni  * GPL Disclaimer
30*b47b5b34SRafael Vanoni  *
31*b47b5b34SRafael Vanoni  * For the avoidance of doubt, except that if any license choice other
32*b47b5b34SRafael Vanoni  * than GPL or LGPL is available it will apply instead, Sun elects to
33*b47b5b34SRafael Vanoni  * use only the General Public License version 2 (GPLv2) at this time
34*b47b5b34SRafael Vanoni  * for any software where a choice of GPL license versions is made
35*b47b5b34SRafael Vanoni  * available with the language indicating that GPLv2 or any later
36*b47b5b34SRafael Vanoni  * version may be used, or where a choice of which version of the GPL
37*b47b5b34SRafael Vanoni  * is applied is otherwise unspecified.
38*b47b5b34SRafael Vanoni  */
39*b47b5b34SRafael Vanoni 
40*b47b5b34SRafael Vanoni #include <stdarg.h>
41*b47b5b34SRafael Vanoni #include <stdlib.h>
42*b47b5b34SRafael Vanoni #include <libgen.h>
43*b47b5b34SRafael Vanoni #include <unistd.h>
44*b47b5b34SRafael Vanoni #include <strings.h>
45*b47b5b34SRafael Vanoni #include <sys/systeminfo.h>
46*b47b5b34SRafael Vanoni #include <kstat.h>
47*b47b5b34SRafael Vanoni #include <errno.h>
48*b47b5b34SRafael Vanoni #include "powertop.h"
49*b47b5b34SRafael Vanoni 
50*b47b5b34SRafael Vanoni static char 	PROG_FMT[] = "%s: ";
51*b47b5b34SRafael Vanoni static char 	ERR_FMT[] = ": %s";
52*b47b5b34SRafael Vanoni static char 	*progname;
53*b47b5b34SRafael Vanoni 
54*b47b5b34SRafael Vanoni void
55*b47b5b34SRafael Vanoni pt_set_progname(char *name)
56*b47b5b34SRafael Vanoni {
57*b47b5b34SRafael Vanoni 	progname = basename(name);
58*b47b5b34SRafael Vanoni }
59*b47b5b34SRafael Vanoni 
60*b47b5b34SRafael Vanoni /*PRINTFLIKE1*/
61*b47b5b34SRafael Vanoni void
62*b47b5b34SRafael Vanoni pt_error(char *format, ...)
63*b47b5b34SRafael Vanoni {
64*b47b5b34SRafael Vanoni 	int 	err = errno;
65*b47b5b34SRafael Vanoni 	va_list alist;
66*b47b5b34SRafael Vanoni 
67*b47b5b34SRafael Vanoni 	if (g_gui)
68*b47b5b34SRafael Vanoni 		return;
69*b47b5b34SRafael Vanoni 
70*b47b5b34SRafael Vanoni 	if (progname != NULL)
71*b47b5b34SRafael Vanoni 		(void) fprintf(stderr, PROG_FMT, progname);
72*b47b5b34SRafael Vanoni 
73*b47b5b34SRafael Vanoni 	va_start(alist, format);
74*b47b5b34SRafael Vanoni 	(void) vfprintf(stderr, format, alist);
75*b47b5b34SRafael Vanoni 	va_end(alist);
76*b47b5b34SRafael Vanoni 
77*b47b5b34SRafael Vanoni 	if (strchr(format, '\n') == NULL)
78*b47b5b34SRafael Vanoni 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
79*b47b5b34SRafael Vanoni }
80*b47b5b34SRafael Vanoni 
81*b47b5b34SRafael Vanoni /*
82*b47b5b34SRafael Vanoni  * Returns the number of online CPUs.
83*b47b5b34SRafael Vanoni  */
84*b47b5b34SRafael Vanoni uint_t
85*b47b5b34SRafael Vanoni enumerate_cpus(void)
86*b47b5b34SRafael Vanoni {
87*b47b5b34SRafael Vanoni 	int	cpuid;
88*b47b5b34SRafael Vanoni 	int	max, cpus_conf;
89*b47b5b34SRafael Vanoni 	uint_t	ncpus = 0;
90*b47b5b34SRafael Vanoni 
91*b47b5b34SRafael Vanoni 	max 		= sysconf(_SC_CPUID_MAX);
92*b47b5b34SRafael Vanoni 	cpus_conf	= sysconf(_SC_NPROCESSORS_CONF);
93*b47b5b34SRafael Vanoni 
94*b47b5b34SRafael Vanoni 	/* Fall back to one CPU if any of the sysconf calls above failed */
95*b47b5b34SRafael Vanoni 	if (max == -1 || cpus_conf == -1) {
96*b47b5b34SRafael Vanoni 		max = cpus_conf = 1;
97*b47b5b34SRafael Vanoni 	}
98*b47b5b34SRafael Vanoni 
99*b47b5b34SRafael Vanoni 	if ((g_cpu_table = malloc(cpus_conf * sizeof (processorid_t))) == NULL)
100*b47b5b34SRafael Vanoni 		return (0);
101*b47b5b34SRafael Vanoni 
102*b47b5b34SRafael Vanoni 	for (cpuid = 0; cpuid < max; cpuid++) {
103*b47b5b34SRafael Vanoni 		if (p_online(cpuid, P_STATUS) != -1) {
104*b47b5b34SRafael Vanoni 			g_cpu_table[ncpus] = cpuid;
105*b47b5b34SRafael Vanoni 			ncpus++;
106*b47b5b34SRafael Vanoni 		}
107*b47b5b34SRafael Vanoni 	}
108*b47b5b34SRafael Vanoni 	return (ncpus);
109*b47b5b34SRafael Vanoni }
110*b47b5b34SRafael Vanoni 
111*b47b5b34SRafael Vanoni void
112*b47b5b34SRafael Vanoni usage(void)
113*b47b5b34SRafael Vanoni {
114*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "%s   %s\n\n", TITLE, COPYRIGHT_INTEL);
115*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "Usage: powertop [option]\n");
116*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "  -d, --dump [count]	Read wakeups count "
117*b47b5b34SRafael Vanoni 	    "times and print list of top offenders\n");
118*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "  -t, --time [interval]	Default time to gather "
119*b47b5b34SRafael Vanoni 	    "data in seconds [1-100s]\n");
120*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "  -v, --verbose		Verbose mode, reports "
121*b47b5b34SRafael Vanoni 	    "kernel cyclic activity\n");
122*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "  -c, --cpu [CPU]	Only observe a specific"
123*b47b5b34SRafael Vanoni 	    " CPU\n");
124*b47b5b34SRafael Vanoni 	(void) fprintf(stderr, "  -h, --help		Show this help "
125*b47b5b34SRafael Vanoni 	    "message\n");
126*b47b5b34SRafael Vanoni 
127*b47b5b34SRafael Vanoni 	exit(EXIT_USAGE);
128*b47b5b34SRafael Vanoni }
129*b47b5b34SRafael Vanoni 
130*b47b5b34SRafael Vanoni int
131*b47b5b34SRafael Vanoni get_bit_depth(void)
132*b47b5b34SRafael Vanoni {
133*b47b5b34SRafael Vanoni 	/*
134*b47b5b34SRafael Vanoni 	 * This little routine was derived from isainfo.c to look up
135*b47b5b34SRafael Vanoni 	 * the system's bit depth. It feeds a 10 byte long buffer to
136*b47b5b34SRafael Vanoni 	 * sysinfo (we only need the first word, sysinfo truncates and
137*b47b5b34SRafael Vanoni 	 * \0 terminates the rest) from which we figure out which isa
138*b47b5b34SRafael Vanoni 	 * we're running on.
139*b47b5b34SRafael Vanoni 	 */
140*b47b5b34SRafael Vanoni 	char	buf[BIT_DEPTH_BUF];
141*b47b5b34SRafael Vanoni 
142*b47b5b34SRafael Vanoni 	if (sysinfo(SI_ARCHITECTURE_64, buf, BIT_DEPTH_BUF) == -1)
143*b47b5b34SRafael Vanoni 		if (sysinfo(SI_ARCHITECTURE_32, buf, BIT_DEPTH_BUF) == -1)
144*b47b5b34SRafael Vanoni 			return (-2);
145*b47b5b34SRafael Vanoni 
146*b47b5b34SRafael Vanoni 	if (strcmp(buf, "sparc") == 0 || strcmp(buf, "i386") == 0)
147*b47b5b34SRafael Vanoni 		return (32);
148*b47b5b34SRafael Vanoni 
149*b47b5b34SRafael Vanoni 	if (strcmp(buf, "sparcv9") == 0 || strcmp(buf, "amd64") == 0)
150*b47b5b34SRafael Vanoni 		return (64);
151*b47b5b34SRafael Vanoni 
152*b47b5b34SRafael Vanoni 	return (-3);
153*b47b5b34SRafael Vanoni }
154*b47b5b34SRafael Vanoni 
155*b47b5b34SRafael Vanoni /*
156*b47b5b34SRafael Vanoni  * Simple integer comparison routine for the event report qsort(3C).
157*b47b5b34SRafael Vanoni  */
158*b47b5b34SRafael Vanoni int
159*b47b5b34SRafael Vanoni event_compare(const void *p1, const void *p2)
160*b47b5b34SRafael Vanoni {
161*b47b5b34SRafael Vanoni 	event_info_t i = *((event_info_t *)p1);
162*b47b5b34SRafael Vanoni 	event_info_t j = *((event_info_t *)p2);
163*b47b5b34SRafael Vanoni 
164*b47b5b34SRafael Vanoni 	if (i.total_count > j.total_count)
165*b47b5b34SRafael Vanoni 		return (-1);
166*b47b5b34SRafael Vanoni 
167*b47b5b34SRafael Vanoni 	if (i.total_count < j.total_count)
168*b47b5b34SRafael Vanoni 		return (1);
169*b47b5b34SRafael Vanoni 
170*b47b5b34SRafael Vanoni 	return (0);
171*b47b5b34SRafael Vanoni }
172