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 #include <sys/systeminfo.h>
50 static char PROG_FMT
[] = "%s: ";
51 static char ERR_FMT
[] = ": %s";
52 static char *progname
;
55 pt_set_progname(char *name
)
57 progname
= basename(name
);
62 pt_error(char *format
, ...)
71 (void) fprintf(stderr
, PROG_FMT
, progname
);
73 va_start(alist
, format
);
74 (void) vfprintf(stderr
, format
, alist
);
77 if (strchr(format
, '\n') == NULL
)
78 (void) fprintf(stderr
, ERR_FMT
, strerror(err
));
82 * Returns the number of online CPUs.
85 pt_enumerate_cpus(void)
91 max
= sysconf(_SC_CPUID_MAX
);
92 cpus_conf
= sysconf(_SC_NPROCESSORS_CONF
);
94 /* Fall back to one CPU if any of the sysconf calls above failed */
95 if (max
== -1 || cpus_conf
== -1) {
99 if ((g_cpu_table
= malloc(cpus_conf
* sizeof (processorid_t
))) == NULL
)
102 for (cpuid
= 0; cpuid
< max
; cpuid
++) {
103 if (p_online(cpuid
, P_STATUS
) != -1) {
104 g_cpu_table
[ncpus
] = cpuid
;
114 (void) fprintf(stderr
, "%s %s\n\n", TITLE
, COPYRIGHT_INTEL
);
115 (void) fprintf(stderr
, "usage: powertop [option]\n");
116 (void) fprintf(stderr
, " -d, --dump [count] Read wakeups count "
117 "times and print list of top offenders\n");
118 (void) fprintf(stderr
, " -t, --time [interval] Default time to gather "
119 "data in seconds [1-30s]\n");
120 (void) fprintf(stderr
, " -v, --verbose Verbose mode, reports "
121 "kernel cyclic activity\n");
122 (void) fprintf(stderr
, " -c, --cpu [CPU] Only observe a specific"
124 (void) fprintf(stderr
, " -h, --help Show this help "
129 pt_get_bit_depth(void)
132 * This little routine was derived from isainfo.c to look up
133 * the system's bit depth. It feeds a 10 byte long buffer to
134 * sysinfo (we only need the first word, sysinfo truncates and
135 * \0 terminates the rest) from which we figure out which isa
138 char buf
[BIT_DEPTH_BUF
];
140 if (sysinfo(SI_ARCHITECTURE_64
, buf
, BIT_DEPTH_BUF
) == -1)
141 if (sysinfo(SI_ARCHITECTURE_32
, buf
, BIT_DEPTH_BUF
) == -1)
144 if (strcmp(buf
, "sparc") == 0 || strcmp(buf
, "i386") == 0)
147 if (strcmp(buf
, "sparcv9") == 0 || strcmp(buf
, "amd64") == 0)
154 * Simple integer comparison routine for the event report qsort(3C).
157 pt_event_compare(const void *p1
, const void *p2
)
159 event_info_t i
= *((event_info_t
*)p1
);
160 event_info_t j
= *((event_info_t
*)p2
);
162 if (i
.total_count
> j
.total_count
)
165 if (i
.total_count
< j
.total_count
)