提交 4d8a93c7 编写于 作者: L Linus Torvalds

Merge git://git.kernel.org/pub/scm/linux/kernel/git/brodo/cpupowerutils

* git://git.kernel.org/pub/scm/linux/kernel/git/brodo/cpupowerutils:
  cpupower: Do detect IDA (opportunistic processor performance) via cpuid
  cpupower: Show Intel turbo ratio support via ./cpupower frequency-info
  cpupowerutils: increase MAX_LINE_LEN
  cpupower: Rename package from cpupowerutils to cpupower
  cpupowerutils: Rename: libcpufreq->libcpupower
  cpupowerutils: use kernel version-derived version string
  cpupowerutils: utils - ConfigStyle bugfixes
  cpupowerutils: helpers - ConfigStyle bugfixes
  cpupowerutils: idle_monitor - ConfigStyle bugfixes
  cpupowerutils: lib - ConfigStyle bugfixes
  cpupowerutils: bench - ConfigStyle bugfixes
  cpupowerutils: do not update po files on each and every compile
  cpupowerutils: remove ccdv, use kernel quiet/verbose mechanism
  cpupowerutils: use COPYING, CREDITS from top-level directory
  cpupowerutils - cpufrequtils extended with quite some features
......@@ -504,7 +504,7 @@ N: Dominik Brodowski
E: linux@brodo.de
W: http://www.brodo.de/
P: 1024D/725B37C6 190F 3E77 9C89 3B6D BECD 46EE 67C3 0308 725B 37C6
D: parts of CPUFreq code, ACPI bugfixes
D: parts of CPUFreq code, ACPI bugfixes, PCMCIA rewrite, cpufrequtils
S: Tuebingen, Germany
N: Andries Brouwer
......@@ -857,6 +857,10 @@ S: One Dell Way
S: Round Rock, TX 78682
S: USA
N: Mattia Dongili
E: malattia@gmail.com
D: cpufrequtils (precursor to cpupowerutils)
N: Ben Dooks
E: ben-linux@fluff.org
E: ben@simtec.co.uk
......@@ -1883,6 +1887,11 @@ S: Kruislaan 419
S: 1098 VA Amsterdam
S: The Netherlands
N: Goran Koruga
E: korugag@siol.net
D: cpufrequtils (precursor to cpupowerutils)
S: Slovenia
N: Jiri Kosina
E: jikos@jikos.cz
E: jkosina@suse.cz
......@@ -2916,6 +2925,12 @@ S: Schlossbergring 9
S: 79098 Freiburg
S: Germany
N: Thomas Renninger
E: trenn@suse.de
D: cpupowerutils
S: SUSE Linux GmbH
S: Germany
N: Joerg Reuter
E: jreuter@yaina.de
W: http://yaina.de/jreuter/
......
......@@ -1925,6 +1925,12 @@ S: Maintained
F: arch/x86/kernel/cpuid.c
F: arch/x86/kernel/msr.c
CPU POWER MONITORING SUBSYSTEM
M: Dominik Brodowski <linux@dominikbrodowski.net>
M: Thomas Renninger <trenn@suse.de>
S: Maintained
F: tools/power/cpupower
CPUSETS
M: Paul Menage <menage@google.com>
W: http://www.bullopensource.org/cpuset/
......
.libs
libcpupower.so
libcpupower.so.0
libcpupower.so.0.0.0
build/ccdv
cpufreq-info
cpufreq-set
cpufreq-aperf
lib/.libs
lib/cpufreq.lo
lib/cpufreq.o
lib/proc.lo
lib/proc.o
lib/sysfs.lo
lib/sysfs.o
po/cpupowerutils.pot
po/*.gmo
utils/cpufreq-info.o
utils/cpufreq-set.o
utils/cpufreq-aperf.o
cpupower
bench/cpufreq-bench
# Makefile for cpupower
#
# Copyright (C) 2005,2006 Dominik Brodowski <linux@dominikbrodowski.net>
#
# Based largely on the Makefile for udev by:
#
# Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# --- CONFIGURATION BEGIN ---
# Set the following to `true' to make a unstripped, unoptimized
# binary. Leave this set to `false' for production use.
DEBUG ?= false
# make the build silent. Set this to something else to make it noisy again.
V ?= false
# Internationalization support (output in different languages).
# Requires gettext.
NLS ?= true
# Set the following to 'true' to build/install the
# cpufreq-bench benchmarking tool
CPUFRQ_BENCH ?= true
# Prefix to the directories we're installing to
DESTDIR ?=
# --- CONFIGURATION END ---
# Package-related definitions. Distributions can modify the version
# and _should_ modify the PACKAGE_BUGREPORT definition
VERSION= $(shell ./utils/version-gen.sh)
LIB_MAJ= 0.0.0
LIB_MIN= 0
PACKAGE = cpupower
PACKAGE_BUGREPORT = cpufreq@vger.kernel.org
LANGUAGES = de fr it cs pt
# Directory definitions. These are default and most probably
# do not need to be changed. Please note that DESTDIR is
# added in front of any of them
bindir ?= /usr/bin
sbindir ?= /usr/sbin
mandir ?= /usr/man
includedir ?= /usr/include
libdir ?= /usr/lib
localedir ?= /usr/share/locale
docdir ?= /usr/share/doc/packages/cpupower
confdir ?= /etc/
# Toolchain: what tools do we use, and what options do they need:
CP = cp -fpR
INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL}
INSTALL_DATA = ${INSTALL} -m 644
INSTALL_SCRIPT = ${INSTALL_PROGRAM}
# If you are running a cross compiler, you may want to set this
# to something more interesting, like "arm-linux-". If you want
# to compile vs uClibc, that can be done here as well.
CROSS = #/usr/i386-linux-uclibc/usr/bin/i386-uclibc-
CC = $(CROSS)gcc
LD = $(CROSS)gcc
AR = $(CROSS)ar
STRIP = $(CROSS)strip
RANLIB = $(CROSS)ranlib
HOSTCC = gcc
# Now we set up the build system
#
# set up PWD so that older versions of make will work with our build.
PWD = $(shell pwd)
GMO_FILES = ${shell for HLANG in ${LANGUAGES}; do echo po/$$HLANG.gmo; done;}
export CROSS CC AR STRIP RANLIB CFLAGS LDFLAGS LIB_OBJS
# check if compiler option is supported
cc-supports = ${shell if $(CC) ${1} -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; fi;}
# use '-Os' optimization if available, else use -O2
OPTIMIZATION := $(call cc-supports,-Os,-O2)
WARNINGS := -Wall -Wchar-subscripts -Wpointer-arith -Wsign-compare
WARNINGS += $(call cc-supports,-Wno-pointer-sign)
WARNINGS += $(call cc-supports,-Wdeclaration-after-statement)
WARNINGS += -Wshadow
CFLAGS += -DVERSION=\"$(VERSION)\" -DPACKAGE=\"$(PACKAGE)\" \
-DPACKAGE_BUGREPORT=\"$(PACKAGE_BUGREPORT)\" -D_GNU_SOURCE
UTIL_OBJS = utils/helpers/amd.o utils/helpers/topology.o utils/helpers/msr.o \
utils/helpers/sysfs.o utils/helpers/misc.o utils/helpers/cpuid.o \
utils/helpers/pci.o utils/helpers/bitmask.o \
utils/idle_monitor/nhm_idle.o utils/idle_monitor/snb_idle.o \
utils/idle_monitor/amd_fam14h_idle.o utils/idle_monitor/cpuidle_sysfs.o \
utils/idle_monitor/mperf_monitor.o utils/idle_monitor/cpupower-monitor.o \
utils/cpupower.o utils/cpufreq-info.o utils/cpufreq-set.o \
utils/cpupower-set.o utils/cpupower-info.o utils/cpuidle-info.o
UTIL_HEADERS = utils/helpers/helpers.h utils/idle_monitor/cpupower-monitor.h \
utils/helpers/bitmask.h \
utils/idle_monitor/idle_monitors.h utils/idle_monitor/idle_monitors.def
UTIL_SRC := $(UTIL_OBJS:.o=.c)
LIB_HEADERS = lib/cpufreq.h lib/sysfs.h
LIB_SRC = lib/cpufreq.c lib/sysfs.c
LIB_OBJS = lib/cpufreq.o lib/sysfs.o
CFLAGS += -pipe
ifeq ($(strip $(NLS)),true)
INSTALL_NLS += install-gmo
COMPILE_NLS += create-gmo
endif
ifeq ($(strip $(CPUFRQ_BENCH)),true)
INSTALL_BENCH += install-bench
COMPILE_BENCH += compile-bench
endif
CFLAGS += $(WARNINGS)
ifeq ($(strip $(V)),false)
QUIET=@
ECHO=@echo
else
QUIET=
ECHO=@\#
endif
export QUIET ECHO
# if DEBUG is enabled, then we do not strip or optimize
ifeq ($(strip $(DEBUG)),true)
CFLAGS += -O1 -g -DDEBUG
STRIPCMD = /bin/true -Since_we_are_debugging
else
CFLAGS += $(OPTIMIZATION) -fomit-frame-pointer
STRIPCMD = $(STRIP) -s --remove-section=.note --remove-section=.comment
endif
# the actual make rules
all: libcpupower cpupower $(COMPILE_NLS) $(COMPILE_BENCH)
lib/%.o: $(LIB_SRC) $(LIB_HEADERS)
$(ECHO) " CC " $@
$(QUIET) $(CC) $(CFLAGS) -fPIC -o $@ -c lib/$*.c
libcpupower.so.$(LIB_MAJ): $(LIB_OBJS)
$(ECHO) " LD " $@
$(QUIET) $(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ \
-Wl,-soname,libcpupower.so.$(LIB_MIN) $(LIB_OBJS)
@ln -sf $@ libcpupower.so
@ln -sf $@ libcpupower.so.$(LIB_MIN)
libcpupower: libcpupower.so.$(LIB_MAJ)
# Let all .o files depend on its .c file and all headers
# Might be worth to put this into utils/Makefile at some point of time
$(UTIL_OBJS): $(UTIL_HEADERS)
.c.o:
$(ECHO) " CC " $@
$(QUIET) $(CC) $(CFLAGS) -I./lib -I ./utils -o $@ -c $*.c
cpupower: $(UTIL_OBJS) libcpupower.so.$(LIB_MAJ)
$(ECHO) " CC " $@
$(QUIET) $(CC) $(CFLAGS) $(LDFLAGS) -lcpupower -lrt -lpci -L. -o $@ $(UTIL_OBJS)
$(QUIET) $(STRIPCMD) $@
po/$(PACKAGE).pot: $(UTIL_SRC)
$(ECHO) " GETTEXT " $@
$(QUIET) xgettext --default-domain=$(PACKAGE) --add-comments \
--keyword=_ --keyword=N_ $(UTIL_SRC) && \
test -f $(PACKAGE).po && \
mv -f $(PACKAGE).po po/$(PACKAGE).pot
po/%.gmo: po/%.po
$(ECHO) " MSGFMT " $@
$(QUIET) msgfmt -o $@ po/$*.po
create-gmo: ${GMO_FILES}
update-po: po/$(PACKAGE).pot
$(ECHO) " MSGMRG " $@
$(QUIET) @for HLANG in $(LANGUAGES); do \
echo -n "Updating $$HLANG "; \
if msgmerge po/$$HLANG.po po/$(PACKAGE).pot -o \
po/$$HLANG.new.po; then \
mv -f po/$$HLANG.new.po po/$$HLANG.po; \
else \
echo "msgmerge for $$HLANG failed!"; \
rm -f po/$$HLANG.new.po; \
fi; \
done;
compile-bench: libcpupower.so.$(LIB_MAJ)
@V=$(V) confdir=$(confdir) $(MAKE) -C bench
clean:
-find . \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \
| xargs rm -f
-rm -f $(UTIL_BINS)
-rm -f $(IDLE_OBJS)
-rm -f cpupower
-rm -f libcpupower.so*
-rm -rf po/*.gmo po/*.pot
$(MAKE) -C bench clean
install-lib:
$(INSTALL) -d $(DESTDIR)${libdir}
$(CP) libcpupower.so* $(DESTDIR)${libdir}/
$(INSTALL) -d $(DESTDIR)${includedir}
$(INSTALL_DATA) lib/cpufreq.h $(DESTDIR)${includedir}/cpufreq.h
install-tools:
$(INSTALL) -d $(DESTDIR)${bindir}
$(INSTALL_PROGRAM) cpupower $(DESTDIR)${bindir}
install-man:
$(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1
$(INSTALL_DATA) -D man/cpupower-frequency-set.1 $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1
$(INSTALL_DATA) -D man/cpupower-frequency-info.1 $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1
$(INSTALL_DATA) -D man/cpupower-set.1 $(DESTDIR)${mandir}/man1/cpupower-set.1
$(INSTALL_DATA) -D man/cpupower-info.1 $(DESTDIR)${mandir}/man1/cpupower-info.1
$(INSTALL_DATA) -D man/cpupower-monitor.1 $(DESTDIR)${mandir}/man1/cpupower-monitor.1
install-gmo:
$(INSTALL) -d $(DESTDIR)${localedir}
for HLANG in $(LANGUAGES); do \
echo '$(INSTALL_DATA) -D po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo'; \
$(INSTALL_DATA) -D po/$$HLANG.gmo $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
done;
install-bench:
@#DESTDIR must be set from outside to survive
@sbindir=$(sbindir) bindir=$(bindir) docdir=$(docdir) confdir=$(confdir) $(MAKE) -C bench install
install: all install-lib install-tools install-man $(INSTALL_NLS) $(INSTALL_BENCH)
uninstall:
- rm -f $(DESTDIR)${libdir}/libcpupower.*
- rm -f $(DESTDIR)${includedir}/cpufreq.h
- rm -f $(DESTDIR)${bindir}/utils/cpupower
- rm -f $(DESTDIR)${mandir}/man1/cpufreq-set.1
- rm -f $(DESTDIR)${mandir}/man1/cpufreq-info.1
- for HLANG in $(LANGUAGES); do \
rm -f $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
done;
.PHONY: all utils libcpupower update-po create-gmo install-lib install-tools install-man install-gmo install uninstall clean
The cpufrequtils package (homepage:
http://www.kernel.org/pub/linux/utils/kernel/cpufreq/cpufrequtils.html )
consists of the following elements:
requirements
------------
On x86 pciutils is needed at runtime (-lpci).
For compilation pciutils-devel (pci/pci.h) and a gcc version
providing cpuid.h is needed.
For both it's not explicitly checked for (yet).
libcpufreq
----------
"libcpufreq" is a library which offers a unified access method for userspace
tools and programs to the cpufreq core and drivers in the Linux kernel. This
allows for code reduction in userspace tools, a clean implementation of
the interaction to the cpufreq core, and support for both the sysfs and proc
interfaces [depending on configuration, see below].
compilation and installation
----------------------------
make
su
make install
should suffice on most systems. It builds default libcpufreq,
cpufreq-set and cpufreq-info files and installs them in /usr/lib and
/usr/bin, respectively. If you want to set up the paths differently and/or
want to configure the package to your specific needs, you need to open
"Makefile" with an editor of your choice and edit the block marked
CONFIGURATION.
THANKS
------
Many thanks to Mattia Dongili who wrote the autotoolization and
libtoolization, the manpages and the italian language file for cpufrequtils;
to Dave Jones for his feedback and his dump_psb tool; to Bruno Ducrot for his
powernow-k8-decode and intel_gsic tools as well as the french language file;
and to various others commenting on the previous (pre-)releases of
cpufrequtils.
Dominik Brodowski
ToDos sorted by priority:
- Use bitmask functions to parse CPU topology more robust
(current implementation has issues on AMD)
- Try to read out boost states and frequencies on Intel
- Adjust README
- Somewhere saw the ability to read power consumption of
RAM from HW on Intel SandyBridge -> another monitor?
- Add another c1e debug idle monitor
-> Is by design racy with BIOS, but could be added
with a --force option and some "be careful" messages
LIBS = -L../ -lm -lcpupower
OBJS = main.o parse.o system.o benchmark.o
CFLAGS += -D_GNU_SOURCE -I../lib -DDEFAULT_CONFIG_FILE=\"$(confdir)/cpufreq-bench.conf\"
%.o : %.c
$(ECHO) " CC " $@
$(QUIET) $(CC) -c $(CFLAGS) $< -o $@
cpufreq-bench: $(OBJS)
$(ECHO) " CC " $@
$(QUIET) $(CC) -o $@ $(CFLAGS) $(OBJS) $(LIBS)
all: cpufreq-bench
install:
mkdir -p $(DESTDIR)/$(sbindir)
mkdir -p $(DESTDIR)/$(bindir)
mkdir -p $(DESTDIR)/$(docdir)
mkdir -p $(DESTDIR)/$(confdir)
install -m 755 cpufreq-bench $(DESTDIR)/$(sbindir)/cpufreq-bench
install -m 755 cpufreq-bench_plot.sh $(DESTDIR)/$(bindir)/cpufreq-bench_plot.sh
install -m 644 README-BENCH $(DESTDIR)/$(docdir)/README-BENCH
install -m 755 cpufreq-bench_script.sh $(DESTDIR)/$(docdir)/cpufreq-bench_script.sh
install -m 644 example.cfg $(DESTDIR)/$(confdir)/cpufreq-bench.conf
clean:
rm -f *.o
rm -f cpufreq-bench
This is cpufreq-bench, a microbenchmark for the cpufreq framework.
Purpose
=======
What is this benchmark for:
- Identify worst case performance loss when doing dynamic frequency
scaling using Linux kernel governors
- Identify average reaction time of a governor to CPU load changes
- (Stress) Testing whether a cpufreq low level driver or governor works
as expected
- Identify cpufreq related performance regressions between kernels
- Possibly Real time priority testing? -> what happens if there are
processes with a higher prio than the governor's kernel thread
- ...
What this benchmark does *not* cover:
- Power saving related regressions (In fact as better the performance
throughput is, the worse the power savings will be, but the first should
mostly count more...)
- Real world (workloads)
Description
===========
cpufreq-bench helps to test the condition of a given cpufreq governor.
For that purpose, it compares the performance governor to a configured
powersave module.
How it works
============
You can specify load (100% CPU load) and sleep (0% CPU load) times in us which
will be run X time in a row (cycles):
sleep=25000
load=25000
cycles=20
This part of the configuration file will create 25ms load/sleep turns,
repeated 20 times.
Adding this:
sleep_step=25000
load_step=25000
rounds=5
Will increase load and sleep time by 25ms 5 times.
Together you get following test:
25ms load/sleep time repeated 20 times (cycles).
50ms load/sleep time repeated 20 times (cycles).
..
100ms load/sleep time repeated 20 times (cycles).
First it is calibrated how long a specific CPU intensive calculation
takes on this machine and needs to be run in a loop using the performance
governor.
Then the above test runs are processed using the performance governor
and the governor to test. The time the calculation really needed
with the dynamic freq scaling governor is compared with the time needed
on full performance and you get the overall performance loss.
Example of expected results with ondemand governor:
This shows expected results of the first two test run rounds from
above config, you there have:
100% CPU load (load) | 0 % CPU load (sleep) | round
25 ms | 25 ms | 1
50 ms | 50 ms | 2
For example if ondemand governor is configured to have a 50ms
sampling rate you get:
In round 1, ondemand should have rather static 50% load and probably
won't ever switch up (as long as up_threshold is above).
In round 2, if the ondemand sampling times exactly match the load/sleep
trigger of the cpufreq-bench, you will see no performance loss (compare with
below possible ondemand sample kick ins (1)):
But if ondemand always kicks in in the middle of the load sleep cycles, it
will always see 50% loads and you get worst performance impact never
switching up (compare with below possible ondemand sample kick ins (2))::
50 50 50 50ms ->time
load -----| |-----| |-----| |-----|
| | | | | | |
sleep |-----| |-----| |-----| |----
|-----|-----|-----|-----|-----|-----|-----|---- ondemand sampling (1)
100 0 100 0 100 0 100 load seen by ondemand(%)
|-----|-----|-----|-----|-----|-----|-----|-- ondemand sampling (2)
50 50 50 50 50 50 50 load seen by ondemand(%)
You can easily test all kind of load/sleep times and check whether your
governor in average behaves as expected.
ToDo
====
Provide a gnuplot utility script for easy generation of plots to present
the outcome nicely.
cpufreq-bench Command Usage
===========================
-l, --load=<long int> initial load time in us
-s, --sleep=<long int> initial sleep time in us
-x, --load-step=<long int> time to be added to load time, in us
-y, --sleep-step=<long int> time to be added to sleep time, in us
-c, --cpu=<unsigned int> CPU Number to use, starting at 0
-p, --prio=<priority> scheduler priority, HIGH, LOW or DEFAULT
-g, --governor=<governor> cpufreq governor to test
-n, --cycles=<int> load/sleep cycles to get an avarage value to compare
-r, --rounds<int> load/sleep rounds
-f, --file=<configfile> config file to use
-o, --output=<dir> output dir, must exist
-v, --verbose verbose output on/off
Due to the high priority, the application may not be responsible for some time.
After the benchmark, the logfile is saved in OUTPUTDIR/benchmark_TIMESTAMP.log
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include "config.h"
#include "system.h"
#include "benchmark.h"
/* Print out progress if we log into a file */
#define show_progress(total_time, progress_time) \
if (config->output != stdout) { \
fprintf(stdout, "Progress: %02lu %%\r", \
(progress_time * 100) / total_time); \
fflush(stdout); \
}
/**
* compute how many rounds of calculation we should do
* to get the given load time
*
* @param load aimed load time in µs
*
* @retval rounds of calculation
**/
unsigned int calculate_timespace(long load, struct config *config)
{
int i;
long long now, then;
unsigned int estimated = GAUGECOUNT;
unsigned int rounds = 0;
unsigned int timed = 0;
if (config->verbose)
printf("calibrating load of %lius, please wait...\n", load);
/* get the initial calculation time for a specific number of rounds */
now = get_time();
ROUNDS(estimated);
then = get_time();
timed = (unsigned int)(then - now);
/* approximation of the wanted load time by comparing with the
* initial calculation time */
for (i = 0; i < 4; i++) {
rounds = (unsigned int)(load * estimated / timed);
dprintf("calibrating with %u rounds\n", rounds);
now = get_time();
ROUNDS(rounds);
then = get_time();
timed = (unsigned int)(then - now);
estimated = rounds;
}
if (config->verbose)
printf("calibration done\n");
return estimated;
}
/**
* benchmark
* generates a specific sleep an load time with the performance
* governor and compares the used time for same calculations done
* with the configured powersave governor
*
* @param config config values for the benchmark
*
**/
void start_benchmark(struct config *config)
{
unsigned int _round, cycle;
long long now, then;
long sleep_time = 0, load_time = 0;
long performance_time = 0, powersave_time = 0;
unsigned int calculations;
unsigned long total_time = 0, progress_time = 0;
sleep_time = config->sleep;
load_time = config->load;
/* For the progress bar */
for (_round = 1; _round <= config->rounds; _round++)
total_time += _round * (config->sleep + config->load);
total_time *= 2; /* powersave and performance cycles */
for (_round = 0; _round < config->rounds; _round++) {
performance_time = 0LL;
powersave_time = 0LL;
show_progress(total_time, progress_time);
/* set the cpufreq governor to "performance" which disables
* P-State switching. */
if (set_cpufreq_governor("performance", config->cpu) != 0)
return;
/* calibrate the calculation time. the resulting calculation
* _rounds should produce a load which matches the configured
* load time */
calculations = calculate_timespace(load_time, config);
if (config->verbose)
printf("_round %i: doing %u cycles with %u calculations"
" for %lius\n", _round + 1, config->cycles,
calculations, load_time);
fprintf(config->output, "%u %li %li ",
_round, load_time, sleep_time);
if (config->verbose)
printf("avarage: %lius, rps:%li\n",
load_time / calculations,
1000000 * calculations / load_time);
/* do some sleep/load cycles with the performance governor */
for (cycle = 0; cycle < config->cycles; cycle++) {
now = get_time();
usleep(sleep_time);
ROUNDS(calculations);
then = get_time();
performance_time += then - now - sleep_time;
if (config->verbose)
printf("performance cycle took %lius, "
"sleep: %lius, "
"load: %lius, rounds: %u\n",
(long)(then - now), sleep_time,
load_time, calculations);
}
fprintf(config->output, "%li ",
performance_time / config->cycles);
progress_time += sleep_time + load_time;
show_progress(total_time, progress_time);
/* set the powersave governor which activates P-State switching
* again */
if (set_cpufreq_governor(config->governor, config->cpu) != 0)
return;
/* again, do some sleep/load cycles with the
* powersave governor */
for (cycle = 0; cycle < config->cycles; cycle++) {
now = get_time();
usleep(sleep_time);
ROUNDS(calculations);
then = get_time();
powersave_time += then - now - sleep_time;
if (config->verbose)
printf("powersave cycle took %lius, "
"sleep: %lius, "
"load: %lius, rounds: %u\n",
(long)(then - now), sleep_time,
load_time, calculations);
}
progress_time += sleep_time + load_time;
/* compare the avarage sleep/load cycles */
fprintf(config->output, "%li ",
powersave_time / config->cycles);
fprintf(config->output, "%.3f\n",
performance_time * 100.0 / powersave_time);
fflush(config->output);
if (config->verbose)
printf("performance is at %.2f%%\n",
performance_time * 100.0 / powersave_time);
sleep_time += config->sleep_step;
load_time += config->load_step;
}
}
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* load loop, this schould take about 1 to 2ms to complete */
#define ROUNDS(x) {unsigned int rcnt; \
for (rcnt = 0; rcnt < x*1000; rcnt++) { \
(void)(((int)(pow(rcnt, rcnt) * \
sqrt(rcnt*7230970)) ^ 7230716) ^ \
(int)atan2(rcnt, rcnt)); \
} } \
void start_benchmark(struct config *config);
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* initial loop count for the load calibration */
#define GAUGECOUNT 1500
/* default scheduling policy SCHED_OTHER */
#define SCHEDULER SCHED_OTHER
#define PRIORITY_DEFAULT 0
#define PRIORITY_HIGH sched_get_priority_max(SCHEDULER)
#define PRIORITY_LOW sched_get_priority_min(SCHEDULER)
/* enable further debug messages */
#ifdef DEBUG
#define dprintf printf
#else
#define dprintf(...) do { } while (0)
#endif
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
# Author/Copyright(c): 2009, Thomas Renninger <trenn@suse.de>, Novell Inc.
# Helper script to easily create nice plots of your cpufreq-bench results
dir=`mktemp -d`
output_file="cpufreq-bench.png"
global_title="cpufreq-bench plot"
picture_type="jpeg"
file[0]=""
function usage()
{
echo "cpufreq-bench_plot.sh [OPTIONS] logfile [measure_title] [logfile [measure_title]] ...]"
echo
echo "Options"
echo " -o output_file"
echo " -t global_title"
echo " -p picture_type [jpeg|gif|png|postscript|...]"
exit 1
}
if [ $# -eq 0 ];then
echo "No benchmark results file provided"
echo
usage
fi
while getopts o:t:p: name ; do
case $name in
o)
output_file="$OPTARG".$picture_type
;;
t)
global_title="$OPTARG"
;;
p)
picture_type="$OPTARG"
;;
?)
usage
;;
esac
done
shift $(($OPTIND -1))
plots=0
while [ "$1" ];do
if [ ! -f "$1" ];then
echo "File $1 does not exist"
usage
fi
file[$plots]="$1"
title[$plots]="$2"
# echo "File: ${file[$plots]} - ${title[plots]}"
shift;shift
plots=$((plots + 1))
done
echo "set terminal $picture_type" >> $dir/plot_script.gpl
echo "set output \"$output_file\"" >> $dir/plot_script.gpl
echo "set title \"$global_title\"" >> $dir/plot_script.gpl
echo "set xlabel \"sleep/load time\"" >> $dir/plot_script.gpl
echo "set ylabel \"Performance (%)\"" >> $dir/plot_script.gpl
for((plot=0;plot<$plots;plot++));do
# Sanity check
###### I am to dump to get this redirected to stderr/stdout in one awk call... #####
cat ${file[$plot]} |grep -v "^#" |awk '{if ($2 != $3) printf("Error in measure %d:Load time %s does not equal sleep time %s, plot will not be correct\n", $1, $2, $3); ERR=1}'
###### I am to dump to get this redirected in one awk call... #####
# Parse out load time (which must be equal to sleep time for a plot), divide it by 1000
# to get ms and parse out the performance in percentage and write it to a temp file for plotting
cat ${file[$plot]} |grep -v "^#" |awk '{printf "%lu %.1f\n",$2/1000, $6}' >$dir/data_$plot
if [ $plot -eq 0 ];then
echo -n "plot " >> $dir/plot_script.gpl
fi
echo -n "\"$dir/data_$plot\" title \"${title[$plot]}\" with lines" >> $dir/plot_script.gpl
if [ $(($plot + 1)) -ne $plots ];then
echo -n ", " >> $dir/plot_script.gpl
fi
done
echo >> $dir/plot_script.gpl
gnuplot $dir/plot_script.gpl
rm -r $dir
\ No newline at end of file
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
# Author/Copyright(c): 2009, Thomas Renninger <trenn@suse.de>, Novell Inc.
# Ondemand up_threshold and sampling rate test script for cpufreq-bench
# mircobenchmark.
# Modify the general variables at the top or extend or copy out parts
# if you want to test other things
#
# Default with latest kernels is 95, before micro account patches
# it was 80, cmp. with git commit 808009131046b62ac434dbc796
UP_THRESHOLD="60 80 95"
# Depending on the kernel and the HW sampling rate could be restricted
# and cannot be set that low...
# E.g. before git commit cef9615a853ebc4972084f7 one could only set
# min sampling rate of 80000 if CONFIG_HZ=250
SAMPLING_RATE="20000 80000"
function measure()
{
local -i up_threshold_set
local -i sampling_rate_set
for up_threshold in $UP_THRESHOLD;do
for sampling_rate in $SAMPLING_RATE;do
# Set values in sysfs
echo $up_threshold >/sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold
echo $sampling_rate >/sys/devices/system/cpu/cpu0/cpufreq/ondemand/sampling_rate
up_threshold_set=$(cat /sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold)
sampling_rate_set=$(cat /sys/devices/system/cpu/cpu0/cpufreq/ondemand/sampling_rate)
# Verify set values in sysfs
if [ ${up_threshold_set} -eq ${up_threshold} ];then
echo "up_threshold: $up_threshold, set in sysfs: ${up_threshold_set}"
else
echo "WARNING: Tried to set up_threshold: $up_threshold, set in sysfs: ${up_threshold_set}"
fi
if [ ${sampling_rate_set} -eq ${sampling_rate} ];then
echo "sampling_rate: $sampling_rate, set in sysfs: ${sampling_rate_set}"
else
echo "WARNING: Tried to set sampling_rate: $sampling_rate, set in sysfs: ${sampling_rate_set}"
fi
# Benchmark
cpufreq-bench -o /var/log/cpufreq-bench/up_threshold_${up_threshold}_sampling_rate_${sampling_rate}
done
done
}
function create_plots()
{
local command
for up_threshold in $UP_THRESHOLD;do
command="cpufreq-bench_plot.sh -o \"sampling_rate_${SAMPLING_RATE}_up_threshold_${up_threshold}\" -t \"Ondemand sampling_rate: ${SAMPLING_RATE} comparison - Up_threshold: $up_threshold %\""
for sampling_rate in $SAMPLING_RATE;do
command="${command} /var/log/cpufreq-bench/up_threshold_${up_threshold}_sampling_rate_${sampling_rate}/* \"sampling_rate = $sampling_rate\""
done
echo $command
eval "$command"
echo
done
for sampling_rate in $SAMPLING_RATE;do
command="cpufreq-bench_plot.sh -o \"up_threshold_${UP_THRESHOLD}_sampling_rate_${sampling_rate}\" -t \"Ondemand up_threshold: ${UP_THRESHOLD} % comparison - sampling_rate: $sampling_rate\""
for up_threshold in $UP_THRESHOLD;do
command="${command} /var/log/cpufreq-bench/up_threshold_${up_threshold}_sampling_rate_${sampling_rate}/* \"up_threshold = $up_threshold\""
done
echo $command
eval "$command"
echo
done
command="cpufreq-bench_plot.sh -o \"up_threshold_${UP_THRESHOLD}_sampling_rate_${SAMPLING_RATE}\" -t \"Ondemand up_threshold: ${UP_THRESHOLD} and sampling_rate ${SAMPLING_RATE} comparison\""
for sampling_rate in $SAMPLING_RATE;do
for up_threshold in $UP_THRESHOLD;do
command="${command} /var/log/cpufreq-bench/up_threshold_${up_threshold}_sampling_rate_${sampling_rate}/* \"up_threshold = $up_threshold - sampling_rate = $sampling_rate\""
done
done
echo "$command"
eval "$command"
}
measure
create_plots
\ No newline at end of file
sleep = 50000
load = 50000
cpu = 0
priority = LOW
output = /var/log/cpufreq-bench
sleep_step = 50000
load_step = 50000
cycles = 20
rounds = 40
verbose = 0
governor = ondemand
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include "config.h"
#include "system.h"
#include "benchmark.h"
static struct option long_options[] = {
{"output", 1, 0, 'o'},
{"sleep", 1, 0, 's'},
{"load", 1, 0, 'l'},
{"verbose", 0, 0, 'v'},
{"cpu", 1, 0, 'c'},
{"governor", 1, 0, 'g'},
{"prio", 1, 0, 'p'},
{"file", 1, 0, 'f'},
{"cycles", 1, 0, 'n'},
{"rounds", 1, 0, 'r'},
{"load-step", 1, 0, 'x'},
{"sleep-step", 1, 0, 'y'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
/*******************************************************************
usage
*******************************************************************/
void usage()
{
printf("usage: ./bench\n");
printf("Options:\n");
printf(" -l, --load=<long int>\t\tinitial load time in us\n");
printf(" -s, --sleep=<long int>\t\tinitial sleep time in us\n");
printf(" -x, --load-step=<long int>\ttime to be added to load time, in us\n");
printf(" -y, --sleep-step=<long int>\ttime to be added to sleep time, in us\n");
printf(" -c, --cpu=<cpu #>\t\t\tCPU Nr. to use, starting at 0\n");
printf(" -p, --prio=<priority>\t\t\tscheduler priority, HIGH, LOW or DEFAULT\n");
printf(" -g, --governor=<governor>\t\tcpufreq governor to test\n");
printf(" -n, --cycles=<int>\t\t\tload/sleep cycles\n");
printf(" -r, --rounds<int>\t\t\tload/sleep rounds\n");
printf(" -f, --file=<configfile>\t\tconfig file to use\n");
printf(" -o, --output=<dir>\t\t\toutput path. Filename will be OUTPUTPATH/benchmark_TIMESTAMP.log\n");
printf(" -v, --verbose\t\t\t\tverbose output on/off\n");
printf(" -h, --help\t\t\t\tPrint this help screen\n");
exit(1);
}
/*******************************************************************
main
*******************************************************************/
int main(int argc, char **argv)
{
int c;
int option_index = 0;
struct config *config = NULL;
config = prepare_default_config();
if (config == NULL)
return EXIT_FAILURE;
while (1) {
c = getopt_long (argc, argv, "hg:o:s:l:vc:p:f:n:r:x:y:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'o':
if (config->output != NULL)
fclose(config->output);
config->output = prepare_output(optarg);
if (config->output == NULL)
return EXIT_FAILURE;
dprintf("user output path -> %s\n", optarg);
break;
case 's':
sscanf(optarg, "%li", &config->sleep);
dprintf("user sleep time -> %s\n", optarg);
break;
case 'l':
sscanf(optarg, "%li", &config->load);
dprintf("user load time -> %s\n", optarg);
break;
case 'c':
sscanf(optarg, "%u", &config->cpu);
dprintf("user cpu -> %s\n", optarg);
break;
case 'g':
strncpy(config->governor, optarg, 14);
dprintf("user governor -> %s\n", optarg);
break;
case 'p':
if (string_to_prio(optarg) != SCHED_ERR) {
config->prio = string_to_prio(optarg);
dprintf("user prio -> %s\n", optarg);
} else {
if (config != NULL) {
if (config->output != NULL)
fclose(config->output);
free(config);
}
usage();
}
break;
case 'n':
sscanf(optarg, "%u", &config->cycles);
dprintf("user cycles -> %s\n", optarg);
break;
case 'r':
sscanf(optarg, "%u", &config->rounds);
dprintf("user rounds -> %s\n", optarg);
break;
case 'x':
sscanf(optarg, "%li", &config->load_step);
dprintf("user load_step -> %s\n", optarg);
break;
case 'y':
sscanf(optarg, "%li", &config->sleep_step);
dprintf("user sleep_step -> %s\n", optarg);
break;
case 'f':
if (prepare_config(optarg, config))
return EXIT_FAILURE;
break;
case 'v':
config->verbose = 1;
dprintf("verbose output enabled\n");
break;
case 'h':
case '?':
default:
if (config != NULL) {
if (config->output != NULL)
fclose(config->output);
free(config);
}
usage();
}
}
if (config->verbose) {
printf("starting benchmark with parameters:\n");
printf("config:\n\t"
"sleep=%li\n\t"
"load=%li\n\t"
"sleep_step=%li\n\t"
"load_step=%li\n\t"
"cpu=%u\n\t"
"cycles=%u\n\t"
"rounds=%u\n\t"
"governor=%s\n\n",
config->sleep,
config->load,
config->sleep_step,
config->load_step,
config->cpu,
config->cycles,
config->rounds,
config->governor);
}
prepare_user(config);
prepare_system(config);
start_benchmark(config);
if (config->output != stdout)
fclose(config->output);
free(config);
return EXIT_SUCCESS;
}
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "parse.h"
#include "config.h"
/**
* converts priority string to priority
*
* @param str string that represents a scheduler priority
*
* @retval priority
* @retval SCHED_ERR when the priority doesn't exit
**/
enum sched_prio string_to_prio(const char *str)
{
if (strncasecmp("high", str, strlen(str)) == 0)
return SCHED_HIGH;
else if (strncasecmp("default", str, strlen(str)) == 0)
return SCHED_DEFAULT;
else if (strncasecmp("low", str, strlen(str)) == 0)
return SCHED_LOW;
else
return SCHED_ERR;
}
/**
* create and open logfile
*
* @param dir directory in which the logfile should be created
*
* @retval logfile on success
* @retval NULL when the file can't be created
**/
FILE *prepare_output(const char *dirname)
{
FILE *output = NULL;
int len;
char *filename;
struct utsname sysdata;
DIR *dir;
dir = opendir(dirname);
if (dir == NULL) {
if (mkdir(dirname, 0755)) {
perror("mkdir");
fprintf(stderr, "error: Cannot create dir %s\n",
dirname);
return NULL;
}
}
len = strlen(dirname) + 30;
filename = malloc(sizeof(char) * len);
if (uname(&sysdata) == 0) {
len += strlen(sysdata.nodename) + strlen(sysdata.release);
filename = realloc(filename, sizeof(char) * len);
if (filename == NULL) {
perror("realloc");
return NULL;
}
snprintf(filename, len - 1, "%s/benchmark_%s_%s_%li.log",
dirname, sysdata.nodename, sysdata.release, time(NULL));
} else {
snprintf(filename, len - 1, "%s/benchmark_%li.log",
dirname, time(NULL));
}
dprintf("logilename: %s\n", filename);
output = fopen(filename, "w+");
if (output == NULL) {
perror("fopen");
fprintf(stderr, "error: unable to open logfile\n");
}
fprintf(stdout, "Logfile: %s\n", filename);
free(filename);
fprintf(output, "#round load sleep performance powersave percentage\n");
return output;
}
/**
* returns the default config
*
* @retval default config on success
* @retval NULL when the output file can't be created
**/
struct config *prepare_default_config()
{
struct config *config = malloc(sizeof(struct config));
dprintf("loading defaults\n");
config->sleep = 500000;
config->load = 500000;
config->sleep_step = 500000;
config->load_step = 500000;
config->cycles = 5;
config->rounds = 50;
config->cpu = 0;
config->prio = SCHED_HIGH;
config->verbose = 0;
strncpy(config->governor, "ondemand", 8);
config->output = stdout;
#ifdef DEFAULT_CONFIG_FILE
if (prepare_config(DEFAULT_CONFIG_FILE, config))
return NULL;
#endif
return config;
}
/**
* parses config file and returns the config to the caller
*
* @param path config file name
*
* @retval 1 on error
* @retval 0 on success
**/
int prepare_config(const char *path, struct config *config)
{
size_t len = 0;
char *opt, *val, *line = NULL;
FILE *configfile = fopen(path, "r");
if (config == NULL) {
fprintf(stderr, "error: config is NULL\n");
return 1;
}
if (configfile == NULL) {
perror("fopen");
fprintf(stderr, "error: unable to read configfile\n");
free(config);
return 1;
}
while (getline(&line, &len, configfile) != -1) {
if (line[0] == '#' || line[0] == ' ')
continue;
sscanf(line, "%as = %as", &opt, &val);
dprintf("parsing: %s -> %s\n", opt, val);
if (strncmp("sleep", opt, strlen(opt)) == 0)
sscanf(val, "%li", &config->sleep);
else if (strncmp("load", opt, strlen(opt)) == 0)
sscanf(val, "%li", &config->load);
else if (strncmp("load_step", opt, strlen(opt)) == 0)
sscanf(val, "%li", &config->load_step);
else if (strncmp("sleep_step", opt, strlen(opt)) == 0)
sscanf(val, "%li", &config->sleep_step);
else if (strncmp("cycles", opt, strlen(opt)) == 0)
sscanf(val, "%u", &config->cycles);
else if (strncmp("rounds", opt, strlen(opt)) == 0)
sscanf(val, "%u", &config->rounds);
else if (strncmp("verbose", opt, strlen(opt)) == 0)
sscanf(val, "%u", &config->verbose);
else if (strncmp("output", opt, strlen(opt)) == 0)
config->output = prepare_output(val);
else if (strncmp("cpu", opt, strlen(opt)) == 0)
sscanf(val, "%u", &config->cpu);
else if (strncmp("governor", opt, 14) == 0)
strncpy(config->governor, val, 14);
else if (strncmp("priority", opt, strlen(opt)) == 0) {
if (string_to_prio(val) != SCHED_ERR)
config->prio = string_to_prio(val);
}
}
free(line);
free(opt);
free(val);
return 0;
}
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* struct that holds the required config parameters */
struct config
{
long sleep; /* sleep time in µs */
long load; /* load time in µs */
long sleep_step; /* time value which changes the
* sleep time after every round in µs */
long load_step; /* time value which changes the
* load time after every round in µs */
unsigned int cycles; /* calculation cycles with the same sleep/load time */
unsigned int rounds; /* calculation rounds with iterated sleep/load time */
unsigned int cpu; /* cpu for which the affinity is set */
char governor[15]; /* cpufreq governor */
enum sched_prio /* possible scheduler priorities */
{
SCHED_ERR = -1,
SCHED_HIGH,
SCHED_DEFAULT,
SCHED_LOW
} prio;
unsigned int verbose; /* verbose output */
FILE *output; /* logfile */
char *output_filename; /* logfile name, must be freed at the end
if output != NULL and output != stdout*/
};
enum sched_prio string_to_prio(const char *str);
FILE *prepare_output(const char *dir);
int prepare_config(const char *path, struct config *config);
struct config *prepare_default_config();
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sched.h>
#include <cpufreq.h>
#include "config.h"
#include "system.h"
/**
* returns time since epoch in µs
*
* @retval time
**/
long long int get_time()
{
struct timeval now;
gettimeofday(&now, NULL);
return (long long int)(now.tv_sec * 1000000LL + now.tv_usec);
}
/**
* sets the cpufreq governor
*
* @param governor cpufreq governor name
* @param cpu cpu for which the governor should be set
*
* @retval 0 on success
* @retval -1 when failed
**/
int set_cpufreq_governor(char *governor, unsigned int cpu)
{
dprintf("set %s as cpufreq governor\n", governor);
if (cpufreq_cpu_exists(cpu) != 0) {
perror("cpufreq_cpu_exists");
fprintf(stderr, "error: cpu %u does not exist\n", cpu);
return -1;
}
if (cpufreq_modify_policy_governor(cpu, governor) != 0) {
perror("cpufreq_modify_policy_governor");
fprintf(stderr, "error: unable to set %s governor\n", governor);
return -1;
}
return 0;
}
/**
* sets cpu affinity for the process
*
* @param cpu cpu# to which the affinity should be set
*
* @retval 0 on success
* @retval -1 when setting the affinity failed
**/
int set_cpu_affinity(unsigned int cpu)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
dprintf("set affinity to cpu #%u\n", cpu);
if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) < 0) {
perror("sched_setaffinity");
fprintf(stderr, "warning: unable to set cpu affinity\n");
return -1;
}
return 0;
}
/**
* sets the process priority parameter
*
* @param priority priority value
*
* @retval 0 on success
* @retval -1 when setting the priority failed
**/
int set_process_priority(int priority)
{
struct sched_param param;
dprintf("set scheduler priority to %i\n", priority);
param.sched_priority = priority;
if (sched_setscheduler(0, SCHEDULER, &param) < 0) {
perror("sched_setscheduler");
fprintf(stderr, "warning: unable to set scheduler priority\n");
return -1;
}
return 0;
}
/**
* notifies the user that the benchmark may run some time
*
* @param config benchmark config values
*
**/
void prepare_user(const struct config *config)
{
unsigned long sleep_time = 0;
unsigned long load_time = 0;
unsigned int round;
for (round = 0; round < config->rounds; round++) {
sleep_time += 2 * config->cycles *
(config->sleep + config->sleep_step * round);
load_time += 2 * config->cycles *
(config->load + config->load_step * round) +
(config->load + config->load_step * round * 4);
}
if (config->verbose || config->output != stdout)
printf("approx. test duration: %im\n",
(int)((sleep_time + load_time) / 60000000));
}
/**
* sets up the cpu affinity and scheduler priority
*
* @param config benchmark config values
*
**/
void prepare_system(const struct config *config)
{
if (config->verbose)
printf("set cpu affinity to cpu #%u\n", config->cpu);
set_cpu_affinity(config->cpu);
switch (config->prio) {
case SCHED_HIGH:
if (config->verbose)
printf("high priority condition requested\n");
set_process_priority(PRIORITY_HIGH);
break;
case SCHED_LOW:
if (config->verbose)
printf("low priority condition requested\n");
set_process_priority(PRIORITY_LOW);
break;
default:
if (config->verbose)
printf("default priority condition requested\n");
set_process_priority(PRIORITY_DEFAULT);
}
}
/* cpufreq-bench CPUFreq microbenchmark
*
* Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "parse.h"
long long get_time();
int set_cpufreq_governor(char *governor, unsigned int cpu);
int set_cpu_affinity(unsigned int cpu);
int set_process_priority(int priority);
void prepare_user(const struct config *config);
void prepare_system(const struct config *config);
default: all
centrino-decode: centrino-decode.c
$(CC) $(CFLAGS) -o centrino-decode centrino-decode.c
dump_psb: dump_psb.c
$(CC) $(CFLAGS) -o dump_psb dump_psb.c
intel_gsic: intel_gsic.c
$(CC) $(CFLAGS) -o intel_gsic -llrmi intel_gsic.c
powernow-k8-decode: powernow-k8-decode.c
$(CC) $(CFLAGS) -o powernow-k8-decode powernow-k8-decode.c
all: centrino-decode dump_psb intel_gsic powernow-k8-decode
clean:
rm -rf centrino-decode dump_psb intel_gsic powernow-k8-decode
.PHONY: all default clean
/*
* (C) 2003 - 2004 Dominik Brodowski <linux@dominikbrodowski.de>
*
* Licensed under the terms of the GNU GPL License version 2.
*
* Based on code found in
* linux/arch/i386/kernel/cpu/cpufreq/speedstep-centrino.c
* and originally developed by Jeremy Fitzhardinge.
*
* USAGE: simply run it to decode the current settings on CPU 0,
* or pass the CPU number as argument, or pass the MSR content
* as argument.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MCPU 32
#define MSR_IA32_PERF_STATUS 0x198
static int rdmsr(unsigned int cpu, unsigned int msr,
unsigned int *lo, unsigned int *hi)
{
int fd;
char file[20];
unsigned long long val;
int retval = -1;
*lo = *hi = 0;
if (cpu > MCPU)
goto err1;
sprintf(file, "/dev/cpu/%d/msr", cpu);
fd = open(file, O_RDONLY);
if (fd < 0)
goto err1;
if (lseek(fd, msr, SEEK_CUR) == -1)
goto err2;
if (read(fd, &val, 8) != 8)
goto err2;
*lo = (uint32_t )(val & 0xffffffffull);
*hi = (uint32_t )(val>>32 & 0xffffffffull);
retval = 0;
err2:
close(fd);
err1:
return retval;
}
static void decode (unsigned int msr)
{
unsigned int multiplier;
unsigned int mv;
multiplier = ((msr >> 8) & 0xFF);
mv = (((msr & 0xFF) * 16) + 700);
printf("0x%x means multiplier %d @ %d mV\n", msr, multiplier, mv);
}
static int decode_live(unsigned int cpu)
{
unsigned int lo, hi;
int err;
err = rdmsr(cpu, MSR_IA32_PERF_STATUS, &lo, &hi);
if (err) {
printf("can't get MSR_IA32_PERF_STATUS for cpu %d\n", cpu);
printf("Possible trouble: you don't run an Enhanced SpeedStep capable cpu\n");
printf("or you are not root, or the msr driver is not present\n");
return 1;
}
decode(lo);
return 0;
}
int main (int argc, char **argv)
{
unsigned int cpu, mode = 0;
if (argc < 2)
cpu = 0;
else {
cpu = strtoul(argv[1], NULL, 0);
if (cpu >= MCPU)
mode = 1;
}
if (mode)
decode(cpu);
else
decode_live(cpu);
return 0;
}
/*
* dump_psb. (c) 2004, Dave Jones, Red Hat Inc.
* Licensed under the GPL v2.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <sys/mman.h>
#define LEN (0x100000 - 0xc0000)
#define OFFSET (0xc0000)
#ifndef __packed
#define __packed __attribute((packed))
#endif
static long relevant;
static const int fid_to_mult[32] = {
110, 115, 120, 125, 50, 55, 60, 65,
70, 75, 80, 85, 90, 95, 100, 105,
30, 190, 40, 200, 130, 135, 140, 210,
150, 225, 160, 165, 170, 180, -1, -1,
};
static const int vid_to_voltage[32] = {
2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
1075, 1050, 1024, 1000, 975, 950, 925, 0,
};
struct psb_header {
char signature[10];
u_char version;
u_char flags;
u_short settlingtime;
u_char res1;
u_char numpst;
} __packed;
struct pst_header {
u_int32_t cpuid;
u_char fsb;
u_char maxfid;
u_char startvid;
u_char numpstates;
} __packed;
static u_int fsb;
static u_int sgtc;
static int
decode_pst(char *p, int npstates)
{
int i;
int freq, fid, vid;
for (i = 0; i < npstates; ++i) {
fid = *p++;
vid = *p++;
freq = 100 * fid_to_mult[fid] * fsb;
printf(" %2d %8dkHz FID %02x (%2d.%01d) VID %02x (%4dmV)\n",
i,
freq,
fid, fid_to_mult[fid]/10, fid_to_mult[fid]%10,
vid, vid_to_voltage[vid]);
}
return 0;
}
static
void decode_psb(char *p, int numpst)
{
int i;
struct psb_header *psb;
struct pst_header *pst;
psb = (struct psb_header*) p;
if (psb->version != 0x12)
return;
printf("PSB version: %hhx flags: %hhx settling time %hhuus res1 %hhx num pst %hhu\n",
psb->version,
psb->flags,
psb->settlingtime,
psb->res1,
psb->numpst);
sgtc = psb->settlingtime * 100;
if (sgtc < 10000)
sgtc = 10000;
p = ((char *) psb) + sizeof(struct psb_header);
if (numpst < 0)
numpst = psb->numpst;
else
printf("Overriding number of pst :%d\n", numpst);
for (i = 0; i < numpst; i++) {
pst = (struct pst_header*) p;
if (relevant != 0) {
if (relevant!= pst->cpuid)
goto next_one;
}
printf(" PST %d cpuid %.3x fsb %hhu mfid %hhx svid %hhx numberstates %hhu\n",
i+1,
pst->cpuid,
pst->fsb,
pst->maxfid,
pst->startvid,
pst->numpstates);
fsb = pst->fsb;
decode_pst(p + sizeof(struct pst_header), pst->numpstates);
next_one:
p += sizeof(struct pst_header) + 2*pst->numpstates;
}
}
static struct option info_opts[] = {
{.name = "numpst", .has_arg=no_argument, .flag=NULL, .val='n'},
};
void print_help(void)
{
printf ("Usage: dump_psb [options]\n");
printf ("Options:\n");
printf (" -n, --numpst Set number of PST tables to scan\n");
printf (" -r, --relevant Only display PSTs relevant to cpuid N\n");
}
int
main(int argc, char *argv[])
{
int fd;
int numpst=-1;
int ret=0, cont=1;
char *mem = NULL;
char *p;
do {
ret = getopt_long(argc, argv, "hr:n:", info_opts, NULL);
switch (ret){
case '?':
case 'h':
print_help();
cont = 0;
break;
case 'r':
relevant = strtol(optarg, NULL, 16);
break;
case 'n':
numpst = strtol(optarg, NULL, 10);
break;
case -1:
cont = 0;
break;
}
} while(cont);
fd = open("/dev/mem", O_RDONLY);
if (fd < 0) {
printf ("Couldn't open /dev/mem. Are you root?\n");
exit(1);
}
mem = mmap(mem, 0x100000 - 0xc0000, PROT_READ, MAP_SHARED, fd, 0xc0000);
close(fd);
for (p = mem; p - mem < LEN; p+=16) {
if (memcmp(p, "AMDK7PNOW!", 10) == 0) {
decode_psb(p, numpst);
break;
}
}
munmap(mem, LEN);
return 0;
}
/*
* (C) 2003 Bruno Ducrot
* (C) 2004 Dominik Brodowski <linux@dominikbrodowski.de>
*
* Licensed under the terms of the GNU GPL License version 2.
*
* Based on code found in
* linux/include/asm-i386/ist.h and linux/arch/i386/kernel/setup.c
* and originally developed by Andy Grover <andrew.grover@intel.com>
*/
#include <stdio.h>
#include <string.h>
#include <lrmi.h>
int main (void)
{
struct LRMI_regs r;
int retval;
if (!LRMI_init())
return 0;
memset(&r, 0, sizeof(r));
r.eax = 0x0000E980;
r.edx = 0x47534943;
retval = LRMI_int(0x15, &r);
if (!retval) {
printf("Failed!\n");
return 0;
}
if (r.eax == 0x47534943) {
printf("BIOS supports GSIC call:\n");
printf("\tsignature: %c%c%c%c\n",
(r.eax >> 24) & 0xff,
(r.eax >> 16) & 0xff,
(r.eax >> 8) & 0xff,
(r.eax) & 0xff);
printf("\tcommand port = 0x%.4x\n",
r.ebx & 0xffff);
printf("\tcommand = 0x%.4x\n",
(r.ebx >> 16) & 0xffff);
printf("\tevent port = 0x%.8x\n", r.ecx);
printf("\tflags = 0x%.8x\n", r.edx);
if (((r.ebx >> 16) & 0xffff) != 0x82) {
printf("non-default command value. If speedstep-smi "
"doesn't work out of the box,\nyou may want to "
"try out the default value by passing "
"smi_cmd=0x82 to the module\n ON YOUR OWN "
"RISK.\n");
}
if ((r.ebx & 0xffff) != 0xb2) {
printf("non-default command port. If speedstep-smi "
"doesn't work out of the box,\nyou may want to "
"try out the default value by passing "
"smi_port=0x82 to the module\n ON YOUR OWN "
"RISK.\n");
}
} else {
printf("BIOS DOES NOT support GSIC call. Dumping registers anyway:\n");
printf("eax = 0x%.8x\n", r.eax);
printf("ebx = 0x%.8x\n", r.ebx);
printf("ecx = 0x%.8x\n", r.ecx);
printf("edx = 0x%.8x\n", r.edx);
printf("Note also that some BIOS do not support the initial "
"GSIC call, but the newer\nspeeedstep-smi driver may "
"work.\nFor this, you need to pass some arguments to "
"the speedstep-smi driver:\n");
printf("\tsmi_cmd=0x?? smi_port=0x?? smi_sig=1\n");
printf("\nUnfortunately, you have to know what exactly are "
"smi_cmd and smi_port, and this\nis system "
"dependant.\n");
}
return 1;
}
/*
* (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
*
* Licensed under the terms of the GNU GPL License version 2.
*
* Based on code found in
* linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c
* and originally developed by Paul Devriendt
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MCPU 32
#define MSR_FIDVID_STATUS 0xc0010042
#define MSR_S_HI_CURRENT_VID 0x0000001f
#define MSR_S_LO_CURRENT_FID 0x0000003f
static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid)
{
int err = 1;
uint64_t msr = 0;
int fd;
char file[20];
if (cpu > MCPU)
goto out;
sprintf(file, "/dev/cpu/%d/msr", cpu);
fd = open(file, O_RDONLY);
if (fd < 0)
goto out;
lseek(fd, MSR_FIDVID_STATUS, SEEK_CUR);
if (read(fd, &msr, 8) != 8)
goto err1;
*fid = ((uint32_t )(msr & 0xffffffffull)) & MSR_S_LO_CURRENT_FID;
*vid = ((uint32_t )(msr>>32 & 0xffffffffull)) & MSR_S_HI_CURRENT_VID;
err = 0;
err1:
close(fd);
out:
return err;
}
/* Return a frequency in MHz, given an input fid */
static uint32_t find_freq_from_fid(uint32_t fid)
{
return 800 + (fid * 100);
}
/* Return a voltage in miliVolts, given an input vid */
static uint32_t find_millivolts_from_vid(uint32_t vid)
{
return 1550-vid*25;
}
int main (int argc, char *argv[])
{
int err;
int cpu;
uint32_t fid, vid;
if (argc < 2)
cpu = 0;
else
cpu = strtoul(argv[1], NULL, 0);
err = get_fidvid(cpu, &fid, &vid);
if (err) {
printf("can't get fid, vid from MSR\n");
printf("Possible trouble: you don't run a powernow-k8 capable cpu\n");
printf("or you are not root, or the msr driver is not present\n");
exit(1);
}
printf("cpu %d currently at %d MHz and %d mV\n",
cpu,
find_freq_from_fid(fid),
find_millivolts_from_vid(vid));
return 0;
}
obj-m :=
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
KMISC := /lib/modules/$(shell uname -r)/cpufrequtils/
ifeq ("$(CONFIG_X86_TSC)", "y")
obj-m += cpufreq-test_tsc.o
endif
default:
$(MAKE) -C $(KDIR) M=$(PWD)
clean:
- rm -rf *.o *.ko .tmp-versions .*.cmd .*.mod.* *.mod.c
- rm -rf .tmp_versions* Module.symvers modules.order
install: default
install -d $(KMISC)
install -m 644 -c *.ko $(KMISC)
/sbin/depmod -a
all: default
/*
* test module to check whether the TSC-based delay routine continues
* to work properly after cpufreq transitions. Needs ACPI to work
* properly.
*
* Based partly on the Power Management Timer (PMTMR) code to be found
* in arch/i386/kernel/timers/timer_pm.c on recent 2.6. kernels, especially
* code written by John Stultz. The read_pmtmr function was copied verbatim
* from that file.
*
* (C) 2004 Dominik Brodowski
*
* To use:
* 1.) pass clock=tsc to the kernel on your bootloader
* 2.) modprobe this module (it'll fail)
* 3.) change CPU frequency
* 4.) modprobe this module again
* 5.) if the third value, "diff_pmtmr", changes between 2. and 4., the
* TSC-based delay routine on the Linux kernel does not correctly
* handle the cpufreq transition. Please report this to
* cpufreq@vger.kernel.org
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>
static int pm_tmr_ioport = 0;
/*helper function to safely read acpi pm timesource*/
static u32 read_pmtmr(void)
{
u32 v1=0,v2=0,v3=0;
/* It has been reported that because of various broken
* chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
* source is not latched, so you must read it multiple
* times to insure a safe value is read.
*/
do {
v1 = inl(pm_tmr_ioport);
v2 = inl(pm_tmr_ioport);
v3 = inl(pm_tmr_ioport);
} while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
|| (v3 > v1 && v3 < v2));
/* mask the output to 24 bits */
return (v2 & 0xFFFFFF);
}
static int __init cpufreq_test_tsc(void)
{
u32 now, then, diff;
u64 now_tsc, then_tsc, diff_tsc;
int i;
/* the following code snipped is copied from arch/x86/kernel/acpi/boot.c
of Linux v2.6.25. */
/* detect the location of the ACPI PM Timer */
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) {
/* FADT rev. 2 */
if (acpi_gbl_FADT.xpm_timer_block.space_id !=
ACPI_ADR_SPACE_SYSTEM_IO)
return 0;
pm_tmr_ioport = acpi_gbl_FADT.xpm_timer_block.address;
/*
* "X" fields are optional extensions to the original V1.0
* fields, so we must selectively expand V1.0 fields if the
* corresponding X field is zero.
*/
if (!pm_tmr_ioport)
pm_tmr_ioport = acpi_gbl_FADT.pm_timer_block;
} else {
/* FADT rev. 1 */
pm_tmr_ioport = acpi_gbl_FADT.pm_timer_block;
}
printk(KERN_DEBUG "start--> \n");
then = read_pmtmr();
rdtscll(then_tsc);
for (i=0;i<20;i++) {
mdelay(100);
now = read_pmtmr();
rdtscll(now_tsc);
diff = (now - then) & 0xFFFFFF;
diff_tsc = now_tsc - then_tsc;
printk(KERN_DEBUG "t1: %08u t2: %08u diff_pmtmr: %08u diff_tsc: %016llu\n", then, now, diff, diff_tsc);
then = now;
then_tsc = now_tsc;
}
printk(KERN_DEBUG "<-- end \n");
return -ENODEV;
}
static void __exit cpufreq_none(void)
{
return;
}
module_init(cpufreq_test_tsc)
module_exit(cpufreq_none)
MODULE_AUTHOR("Dominik Brodowski");
MODULE_DESCRIPTION("Verify the TSC cpufreq notifier working correctly -- needs ACPI-enabled system");
MODULE_LICENSE ("GPL");
default: all
centrino-decode: centrino-decode.c
$(CC) $(CFLAGS) -o centrino-decode centrino-decode.c
powernow-k8-decode: powernow-k8-decode.c
$(CC) $(CFLAGS) -o powernow-k8-decode powernow-k8-decode.c
all: centrino-decode powernow-k8-decode
clean:
rm -rf centrino-decode powernow-k8-decode
.PHONY: all default clean
../i386/centrino-decode.c
\ No newline at end of file
../i386/powernow-k8-decode.c
\ No newline at end of file
/*
* (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
*
* Licensed under the terms of the GNU GPL License version 2.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "cpufreq.h"
#include "sysfs.h"
int cpufreq_cpu_exists(unsigned int cpu)
{
return sysfs_cpu_exists(cpu);
}
unsigned long cpufreq_get_freq_kernel(unsigned int cpu)
{
return sysfs_get_freq_kernel(cpu);
}
unsigned long cpufreq_get_freq_hardware(unsigned int cpu)
{
return sysfs_get_freq_hardware(cpu);
}
unsigned long cpufreq_get_transition_latency(unsigned int cpu)
{
return sysfs_get_freq_transition_latency(cpu);
}
int cpufreq_get_hardware_limits(unsigned int cpu,
unsigned long *min,
unsigned long *max)
{
if ((!min) || (!max))
return -EINVAL;
return sysfs_get_freq_hardware_limits(cpu, min, max);
}
char *cpufreq_get_driver(unsigned int cpu)
{
return sysfs_get_freq_driver(cpu);
}
void cpufreq_put_driver(char *ptr)
{
if (!ptr)
return;
free(ptr);
}
struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu)
{
return sysfs_get_freq_policy(cpu);
}
void cpufreq_put_policy(struct cpufreq_policy *policy)
{
if ((!policy) || (!policy->governor))
return;
free(policy->governor);
policy->governor = NULL;
free(policy);
}
struct cpufreq_available_governors *cpufreq_get_available_governors(unsigned
int cpu)
{
return sysfs_get_freq_available_governors(cpu);
}
void cpufreq_put_available_governors(struct cpufreq_available_governors *any)
{
struct cpufreq_available_governors *tmp, *next;
if (!any)
return;
tmp = any->first;
while (tmp) {
next = tmp->next;
if (tmp->governor)
free(tmp->governor);
free(tmp);
tmp = next;
}
}
struct cpufreq_available_frequencies
*cpufreq_get_available_frequencies(unsigned int cpu)
{
return sysfs_get_available_frequencies(cpu);
}
void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies
*any) {
struct cpufreq_available_frequencies *tmp, *next;
if (!any)
return;
tmp = any->first;
while (tmp) {
next = tmp->next;
free(tmp);
tmp = next;
}
}
struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned int cpu)
{
return sysfs_get_freq_affected_cpus(cpu);
}
void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any)
{
struct cpufreq_affected_cpus *tmp, *next;
if (!any)
return;
tmp = any->first;
while (tmp) {
next = tmp->next;
free(tmp);
tmp = next;
}
}
struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned int cpu)
{
return sysfs_get_freq_related_cpus(cpu);
}
void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any)
{
cpufreq_put_affected_cpus(any);
}
int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy)
{
if (!policy || !(policy->governor))
return -EINVAL;
return sysfs_set_freq_policy(cpu, policy);
}
int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq)
{
return sysfs_modify_freq_policy_min(cpu, min_freq);
}
int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq)
{
return sysfs_modify_freq_policy_max(cpu, max_freq);
}
int cpufreq_modify_policy_governor(unsigned int cpu, char *governor)
{
if ((!governor) || (strlen(governor) > 19))
return -EINVAL;
return sysfs_modify_freq_policy_governor(cpu, governor);
}
int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency)
{
return sysfs_set_frequency(cpu, target_frequency);
}
struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu,
unsigned long long *total_time)
{
return sysfs_get_freq_stats(cpu, total_time);
}
void cpufreq_put_stats(struct cpufreq_stats *any)
{
struct cpufreq_stats *tmp, *next;
if (!any)
return;
tmp = any->first;
while (tmp) {
next = tmp->next;
free(tmp);
tmp = next;
}
}
unsigned long cpufreq_get_transitions(unsigned int cpu)
{
return sysfs_get_freq_transitions(cpu);
}
/*
* cpufreq.h - definitions for libcpufreq
*
* Copyright (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _CPUFREQ_H
#define _CPUFREQ_H 1
struct cpufreq_policy {
unsigned long min;
unsigned long max;
char *governor;
};
struct cpufreq_available_governors {
char *governor;
struct cpufreq_available_governors *next;
struct cpufreq_available_governors *first;
};
struct cpufreq_available_frequencies {
unsigned long frequency;
struct cpufreq_available_frequencies *next;
struct cpufreq_available_frequencies *first;
};
struct cpufreq_affected_cpus {
unsigned int cpu;
struct cpufreq_affected_cpus *next;
struct cpufreq_affected_cpus *first;
};
struct cpufreq_stats {
unsigned long frequency;
unsigned long long time_in_state;
struct cpufreq_stats *next;
struct cpufreq_stats *first;
};
#ifdef __cplusplus
extern "C" {
#endif
/*
* returns 0 if the specified CPU is present (it doesn't say
* whether it is online!), and an error value if not.
*/
extern int cpufreq_cpu_exists(unsigned int cpu);
/* determine current CPU frequency
* - _kernel variant means kernel's opinion of CPU frequency
* - _hardware variant means actual hardware CPU frequency,
* which is only available to root.
*
* returns 0 on failure, else frequency in kHz.
*/
extern unsigned long cpufreq_get_freq_kernel(unsigned int cpu);
extern unsigned long cpufreq_get_freq_hardware(unsigned int cpu);
#define cpufreq_get(cpu) cpufreq_get_freq_kernel(cpu);
/* determine CPU transition latency
*
* returns 0 on failure, else transition latency in 10^(-9) s = nanoseconds
*/
extern unsigned long cpufreq_get_transition_latency(unsigned int cpu);
/* determine hardware CPU frequency limits
*
* These may be limited further by thermal, energy or other
* considerations by cpufreq policy notifiers in the kernel.
*/
extern int cpufreq_get_hardware_limits(unsigned int cpu,
unsigned long *min,
unsigned long *max);
/* determine CPUfreq driver used
*
* Remember to call cpufreq_put_driver when no longer needed
* to avoid memory leakage, please.
*/
extern char *cpufreq_get_driver(unsigned int cpu);
extern void cpufreq_put_driver(char *ptr);
/* determine CPUfreq policy currently used
*
* Remember to call cpufreq_put_policy when no longer needed
* to avoid memory leakage, please.
*/
extern struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu);
extern void cpufreq_put_policy(struct cpufreq_policy *policy);
/* determine CPUfreq governors currently available
*
* may be modified by modprobe'ing or rmmod'ing other governors. Please
* free allocated memory by calling cpufreq_put_available_governors
* after use.
*/
extern struct cpufreq_available_governors
*cpufreq_get_available_governors(unsigned int cpu);
extern void cpufreq_put_available_governors(
struct cpufreq_available_governors *first);
/* determine CPU frequency states available
*
* Only present on _some_ ->target() cpufreq drivers. For information purposes
* only. Please free allocated memory by calling
* cpufreq_put_available_frequencies after use.
*/
extern struct cpufreq_available_frequencies
*cpufreq_get_available_frequencies(unsigned int cpu);
extern void cpufreq_put_available_frequencies(
struct cpufreq_available_frequencies *first);
/* determine affected CPUs
*
* Remember to call cpufreq_put_affected_cpus when no longer needed
* to avoid memory leakage, please.
*/
extern struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned
int cpu);
extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first);
/* determine related CPUs
*
* Remember to call cpufreq_put_related_cpus when no longer needed
* to avoid memory leakage, please.
*/
extern struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned
int cpu);
extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first);
/* determine stats for cpufreq subsystem
*
* This is not available in all kernel versions or configurations.
*/
extern struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu,
unsigned long long *total_time);
extern void cpufreq_put_stats(struct cpufreq_stats *stats);
extern unsigned long cpufreq_get_transitions(unsigned int cpu);
/* set new cpufreq policy
*
* Tries to set the passed policy as new policy as close as possible,
* but results may differ depending e.g. on governors being available.
*/
extern int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy);
/* modify a policy by only changing min/max freq or governor
*
* Does not check whether result is what was intended.
*/
extern int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq);
extern int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq);
extern int cpufreq_modify_policy_governor(unsigned int cpu, char *governor);
/* set a specific frequency
*
* Does only work if userspace governor can be used and no external
* interference (other calls to this function or to set/modify_policy)
* occurs. Also does not work on ->range() cpufreq drivers.
*/
extern int cpufreq_set_frequency(unsigned int cpu,
unsigned long target_frequency);
#ifdef __cplusplus
}
#endif
#endif /* _CPUFREQ_H */
/*
* (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
*
* Licensed under the terms of the GNU GPL License version 2.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "cpufreq.h"
#define PATH_TO_CPU "/sys/devices/system/cpu/"
#define MAX_LINE_LEN 4096
#define SYSFS_PATH_MAX 255
static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
{
int fd;
ssize_t numread;
fd = open(path, O_RDONLY);
if (fd == -1)
return 0;
numread = read(fd, buf, buflen - 1);
if (numread < 1) {
close(fd);
return 0;
}
buf[numread] = '\0';
close(fd);
return (unsigned int) numread;
}
/* CPUFREQ sysfs access **************************************************/
/* helper function to read file from /sys into given buffer */
/* fname is a relative path under "cpuX/cpufreq" dir */
static unsigned int sysfs_cpufreq_read_file(unsigned int cpu, const char *fname,
char *buf, size_t buflen)
{
char path[SYSFS_PATH_MAX];
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s",
cpu, fname);
return sysfs_read_file(path, buf, buflen);
}
/* helper function to write a new value to a /sys file */
/* fname is a relative path under "cpuX/cpufreq" dir */
static unsigned int sysfs_cpufreq_write_file(unsigned int cpu,
const char *fname,
const char *value, size_t len)
{
char path[SYSFS_PATH_MAX];
int fd;
ssize_t numwrite;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s",
cpu, fname);
fd = open(path, O_WRONLY);
if (fd == -1)
return 0;
numwrite = write(fd, value, len);
if (numwrite < 1) {
close(fd);
return 0;
}
close(fd);
return (unsigned int) numwrite;
}
/* read access to files which contain one numeric value */
enum cpufreq_value {
CPUINFO_CUR_FREQ,
CPUINFO_MIN_FREQ,
CPUINFO_MAX_FREQ,
CPUINFO_LATENCY,
SCALING_CUR_FREQ,
SCALING_MIN_FREQ,
SCALING_MAX_FREQ,
STATS_NUM_TRANSITIONS,
MAX_CPUFREQ_VALUE_READ_FILES
};
static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
[CPUINFO_CUR_FREQ] = "cpuinfo_cur_freq",
[CPUINFO_MIN_FREQ] = "cpuinfo_min_freq",
[CPUINFO_MAX_FREQ] = "cpuinfo_max_freq",
[CPUINFO_LATENCY] = "cpuinfo_transition_latency",
[SCALING_CUR_FREQ] = "scaling_cur_freq",
[SCALING_MIN_FREQ] = "scaling_min_freq",
[SCALING_MAX_FREQ] = "scaling_max_freq",
[STATS_NUM_TRANSITIONS] = "stats/total_trans"
};
static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
enum cpufreq_value which)
{
unsigned long value;
unsigned int len;
char linebuf[MAX_LINE_LEN];
char *endp;
if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
return 0;
len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which],
linebuf, sizeof(linebuf));
if (len == 0)
return 0;
value = strtoul(linebuf, &endp, 0);
if (endp == linebuf || errno == ERANGE)
return 0;
return value;
}
/* read access to files which contain one string */
enum cpufreq_string {
SCALING_DRIVER,
SCALING_GOVERNOR,
MAX_CPUFREQ_STRING_FILES
};
static const char *cpufreq_string_files[MAX_CPUFREQ_STRING_FILES] = {
[SCALING_DRIVER] = "scaling_driver",
[SCALING_GOVERNOR] = "scaling_governor",
};
static char *sysfs_cpufreq_get_one_string(unsigned int cpu,
enum cpufreq_string which)
{
char linebuf[MAX_LINE_LEN];
char *result;
unsigned int len;
if (which >= MAX_CPUFREQ_STRING_FILES)
return NULL;
len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which],
linebuf, sizeof(linebuf));
if (len == 0)
return NULL;
result = strdup(linebuf);
if (result == NULL)
return NULL;
if (result[strlen(result) - 1] == '\n')
result[strlen(result) - 1] = '\0';
return result;
}
/* write access */
enum cpufreq_write {
WRITE_SCALING_MIN_FREQ,
WRITE_SCALING_MAX_FREQ,
WRITE_SCALING_GOVERNOR,
WRITE_SCALING_SET_SPEED,
MAX_CPUFREQ_WRITE_FILES
};
static const char *cpufreq_write_files[MAX_CPUFREQ_WRITE_FILES] = {
[WRITE_SCALING_MIN_FREQ] = "scaling_min_freq",
[WRITE_SCALING_MAX_FREQ] = "scaling_max_freq",
[WRITE_SCALING_GOVERNOR] = "scaling_governor",
[WRITE_SCALING_SET_SPEED] = "scaling_setspeed",
};
static int sysfs_cpufreq_write_one_value(unsigned int cpu,
enum cpufreq_write which,
const char *new_value, size_t len)
{
if (which >= MAX_CPUFREQ_WRITE_FILES)
return 0;
if (sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which],
new_value, len) != len)
return -ENODEV;
return 0;
};
unsigned long sysfs_get_freq_kernel(unsigned int cpu)
{
return sysfs_cpufreq_get_one_value(cpu, SCALING_CUR_FREQ);
}
unsigned long sysfs_get_freq_hardware(unsigned int cpu)
{
return sysfs_cpufreq_get_one_value(cpu, CPUINFO_CUR_FREQ);
}
unsigned long sysfs_get_freq_transition_latency(unsigned int cpu)
{
return sysfs_cpufreq_get_one_value(cpu, CPUINFO_LATENCY);
}
int sysfs_get_freq_hardware_limits(unsigned int cpu,
unsigned long *min,
unsigned long *max)
{
if ((!min) || (!max))
return -EINVAL;
*min = sysfs_cpufreq_get_one_value(cpu, CPUINFO_MIN_FREQ);
if (!*min)
return -ENODEV;
*max = sysfs_cpufreq_get_one_value(cpu, CPUINFO_MAX_FREQ);
if (!*max)
return -ENODEV;
return 0;
}
char *sysfs_get_freq_driver(unsigned int cpu)
{
return sysfs_cpufreq_get_one_string(cpu, SCALING_DRIVER);
}
struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu)
{
struct cpufreq_policy *policy;
policy = malloc(sizeof(struct cpufreq_policy));
if (!policy)
return NULL;
policy->governor = sysfs_cpufreq_get_one_string(cpu, SCALING_GOVERNOR);
if (!policy->governor) {
free(policy);
return NULL;
}
policy->min = sysfs_cpufreq_get_one_value(cpu, SCALING_MIN_FREQ);
policy->max = sysfs_cpufreq_get_one_value(cpu, SCALING_MAX_FREQ);
if ((!policy->min) || (!policy->max)) {
free(policy->governor);
free(policy);
return NULL;
}
return policy;
}
struct cpufreq_available_governors *
sysfs_get_freq_available_governors(unsigned int cpu) {
struct cpufreq_available_governors *first = NULL;
struct cpufreq_available_governors *current = NULL;
char linebuf[MAX_LINE_LEN];
unsigned int pos, i;
unsigned int len;
len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors",
linebuf, sizeof(linebuf));
if (len == 0)
return NULL;
pos = 0;
for (i = 0; i < len; i++) {
if (linebuf[i] == ' ' || linebuf[i] == '\n') {
if (i - pos < 2)
continue;
if (current) {
current->next = malloc(sizeof(*current));
if (!current->next)
goto error_out;
current = current->next;
} else {
first = malloc(sizeof(*first));
if (!first)
goto error_out;
current = first;
}
current->first = first;
current->next = NULL;
current->governor = malloc(i - pos + 1);
if (!current->governor)
goto error_out;
memcpy(current->governor, linebuf + pos, i - pos);
current->governor[i - pos] = '\0';
pos = i + 1;
}
}
return first;
error_out:
while (first) {
current = first->next;
if (first->governor)
free(first->governor);
free(first);
first = current;
}
return NULL;
}
struct cpufreq_available_frequencies *
sysfs_get_available_frequencies(unsigned int cpu) {
struct cpufreq_available_frequencies *first = NULL;
struct cpufreq_available_frequencies *current = NULL;
char one_value[SYSFS_PATH_MAX];
char linebuf[MAX_LINE_LEN];
unsigned int pos, i;
unsigned int len;
len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies",
linebuf, sizeof(linebuf));
if (len == 0)
return NULL;
pos = 0;
for (i = 0; i < len; i++) {
if (linebuf[i] == ' ' || linebuf[i] == '\n') {
if (i - pos < 2)
continue;
if (i - pos >= SYSFS_PATH_MAX)
goto error_out;
if (current) {
current->next = malloc(sizeof(*current));
if (!current->next)
goto error_out;
current = current->next;
} else {
first = malloc(sizeof(*first));
if (!first)
goto error_out;
current = first;
}
current->first = first;
current->next = NULL;
memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0';
if (sscanf(one_value, "%lu", &current->frequency) != 1)
goto error_out;
pos = i + 1;
}
}
return first;
error_out:
while (first) {
current = first->next;
free(first);
first = current;
}
return NULL;
}
static struct cpufreq_affected_cpus *sysfs_get_cpu_list(unsigned int cpu,
const char *file)
{
struct cpufreq_affected_cpus *first = NULL;
struct cpufreq_affected_cpus *current = NULL;
char one_value[SYSFS_PATH_MAX];
char linebuf[MAX_LINE_LEN];
unsigned int pos, i;
unsigned int len;
len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf));
if (len == 0)
return NULL;
pos = 0;
for (i = 0; i < len; i++) {
if (i == len || linebuf[i] == ' ' || linebuf[i] == '\n') {
if (i - pos < 1)
continue;
if (i - pos >= SYSFS_PATH_MAX)
goto error_out;
if (current) {
current->next = malloc(sizeof(*current));
if (!current->next)
goto error_out;
current = current->next;
} else {
first = malloc(sizeof(*first));
if (!first)
goto error_out;
current = first;
}
current->first = first;
current->next = NULL;
memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0';
if (sscanf(one_value, "%u", &current->cpu) != 1)
goto error_out;
pos = i + 1;
}
}
return first;
error_out:
while (first) {
current = first->next;
free(first);
first = current;
}
return NULL;
}
struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(unsigned int cpu)
{
return sysfs_get_cpu_list(cpu, "affected_cpus");
}
struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(unsigned int cpu)
{
return sysfs_get_cpu_list(cpu, "related_cpus");
}
struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu,
unsigned long long *total_time) {
struct cpufreq_stats *first = NULL;
struct cpufreq_stats *current = NULL;
char one_value[SYSFS_PATH_MAX];
char linebuf[MAX_LINE_LEN];
unsigned int pos, i;
unsigned int len;
len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state",
linebuf, sizeof(linebuf));
if (len == 0)
return NULL;
*total_time = 0;
pos = 0;
for (i = 0; i < len; i++) {
if (i == strlen(linebuf) || linebuf[i] == '\n') {
if (i - pos < 2)
continue;
if ((i - pos) >= SYSFS_PATH_MAX)
goto error_out;
if (current) {
current->next = malloc(sizeof(*current));
if (!current->next)
goto error_out;
current = current->next;
} else {
first = malloc(sizeof(*first));
if (!first)
goto error_out;
current = first;
}
current->first = first;
current->next = NULL;
memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0';
if (sscanf(one_value, "%lu %llu",
&current->frequency,
&current->time_in_state) != 2)
goto error_out;
*total_time = *total_time + current->time_in_state;
pos = i + 1;
}
}
return first;
error_out:
while (first) {
current = first->next;
free(first);
first = current;
}
return NULL;
}
unsigned long sysfs_get_freq_transitions(unsigned int cpu)
{
return sysfs_cpufreq_get_one_value(cpu, STATS_NUM_TRANSITIONS);
}
static int verify_gov(char *new_gov, char *passed_gov)
{
unsigned int i, j = 0;
if (!passed_gov || (strlen(passed_gov) > 19))
return -EINVAL;
strncpy(new_gov, passed_gov, 20);
for (i = 0; i < 20; i++) {
if (j) {
new_gov[i] = '\0';
continue;
}
if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z'))
continue;
if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z'))
continue;
if (new_gov[i] == '-')
continue;
if (new_gov[i] == '_')
continue;
if (new_gov[i] == '\0') {
j = 1;
continue;
}
return -EINVAL;
}
new_gov[19] = '\0';
return 0;
}
int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor)
{
char new_gov[SYSFS_PATH_MAX];
if (!governor)
return -EINVAL;
if (verify_gov(new_gov, governor))
return -EINVAL;
return sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_GOVERNOR,
new_gov, strlen(new_gov));
};
int sysfs_modify_freq_policy_max(unsigned int cpu, unsigned long max_freq)
{
char value[SYSFS_PATH_MAX];
snprintf(value, SYSFS_PATH_MAX, "%lu", max_freq);
return sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_MAX_FREQ,
value, strlen(value));
};
int sysfs_modify_freq_policy_min(unsigned int cpu, unsigned long min_freq)
{
char value[SYSFS_PATH_MAX];
snprintf(value, SYSFS_PATH_MAX, "%lu", min_freq);
return sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_MIN_FREQ,
value, strlen(value));
};
int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy)
{
char min[SYSFS_PATH_MAX];
char max[SYSFS_PATH_MAX];
char gov[SYSFS_PATH_MAX];
int ret;
unsigned long old_min;
int write_max_first;
if (!policy || !(policy->governor))
return -EINVAL;
if (policy->max < policy->min)
return -EINVAL;
if (verify_gov(gov, policy->governor))
return -EINVAL;
snprintf(min, SYSFS_PATH_MAX, "%lu", policy->min);
snprintf(max, SYSFS_PATH_MAX, "%lu", policy->max);
old_min = sysfs_cpufreq_get_one_value(cpu, SCALING_MIN_FREQ);
write_max_first = (old_min && (policy->max < old_min) ? 0 : 1);
if (write_max_first) {
ret = sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_MAX_FREQ,
max, strlen(max));
if (ret)
return ret;
}
ret = sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_MIN_FREQ, min,
strlen(min));
if (ret)
return ret;
if (!write_max_first) {
ret = sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_MAX_FREQ,
max, strlen(max));
if (ret)
return ret;
}
return sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_GOVERNOR,
gov, strlen(gov));
}
int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency)
{
struct cpufreq_policy *pol = sysfs_get_freq_policy(cpu);
char userspace_gov[] = "userspace";
char freq[SYSFS_PATH_MAX];
int ret;
if (!pol)
return -ENODEV;
if (strncmp(pol->governor, userspace_gov, 9) != 0) {
ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov);
if (ret) {
cpufreq_put_policy(pol);
return ret;
}
}
cpufreq_put_policy(pol);
snprintf(freq, SYSFS_PATH_MAX, "%lu", target_frequency);
return sysfs_cpufreq_write_one_value(cpu, WRITE_SCALING_SET_SPEED,
freq, strlen(freq));
}
/* CPUFREQ sysfs access **************************************************/
/* General sysfs access **************************************************/
int sysfs_cpu_exists(unsigned int cpu)
{
char file[SYSFS_PATH_MAX];
struct stat statbuf;
snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/", cpu);
if (stat(file, &statbuf) != 0)
return -ENOSYS;
return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS;
}
/* General sysfs access **************************************************/
/* General */
extern unsigned int sysfs_cpu_exists(unsigned int cpu);
/* CPUfreq */
extern unsigned long sysfs_get_freq_kernel(unsigned int cpu);
extern unsigned long sysfs_get_freq_hardware(unsigned int cpu);
extern unsigned long sysfs_get_freq_transition_latency(unsigned int cpu);
extern int sysfs_get_freq_hardware_limits(unsigned int cpu,
unsigned long *min, unsigned long *max);
extern char *sysfs_get_freq_driver(unsigned int cpu);
extern struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu);
extern struct cpufreq_available_governors *sysfs_get_freq_available_governors(
unsigned int cpu);
extern struct cpufreq_available_frequencies *sysfs_get_available_frequencies(
unsigned int cpu);
extern struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(
unsigned int cpu);
extern struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(
unsigned int cpu);
extern struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu,
unsigned long long *total_time);
extern unsigned long sysfs_get_freq_transitions(unsigned int cpu);
extern int sysfs_set_freq_policy(unsigned int cpu,
struct cpufreq_policy *policy);
extern int sysfs_modify_freq_policy_min(unsigned int cpu,
unsigned long min_freq);
extern int sysfs_modify_freq_policy_max(unsigned int cpu,
unsigned long max_freq);
extern int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor);
extern int sysfs_set_frequency(unsigned int cpu,
unsigned long target_frequency);
.TH "cpufreq-info" "1" "0.1" "Mattia Dongili" ""
.SH "NAME"
.LP
cpufreq\-info \- Utility to retrieve cpufreq kernel information
.SH "SYNTAX"
.LP
cpufreq\-info [\fIoptions\fP]
.SH "DESCRIPTION"
.LP
A small tool which prints out cpufreq information helpful to developers and interested users.
.SH "OPTIONS"
.LP
.TP
\fB\-e\fR \fB\-\-debug\fR
Prints out debug information.
.TP
\fB\-f\fR \fB\-\-freq\fR
Get frequency the CPU currently runs at, according to the cpufreq core.
.TP
\fB\-w\fR \fB\-\-hwfreq\fR
Get frequency the CPU currently runs at, by reading it from hardware (only available to root).
.TP
\fB\-l\fR \fB\-\-hwlimits\fR
Determine the minimum and maximum CPU frequency allowed.
.TP
\fB\-d\fR \fB\-\-driver\fR
Determines the used cpufreq kernel driver.
.TP
\fB\-p\fR \fB\-\-policy\fR
Gets the currently used cpufreq policy.
.TP
\fB\-g\fR \fB\-\-governors\fR
Determines available cpufreq governors.
.TP
\fB\-a\fR \fB\-\-related\-cpus\fR
Determines which CPUs run at the same hardware frequency.
.TP
\fB\-a\fR \fB\-\-affected\-cpus\fR
Determines which CPUs need to have their frequency coordinated by software.
.TP
\fB\-s\fR \fB\-\-stats\fR
Shows cpufreq statistics if available.
.TP
\fB\-y\fR \fB\-\-latency\fR
Determines the maximum latency on CPU frequency changes.
.TP
\fB\-o\fR \fB\-\-proc\fR
Prints out information like provided by the /proc/cpufreq interface in 2.4. and early 2.6. kernels.
.TP
\fB\-m\fR \fB\-\-human\fR
human\-readable output for the \-f, \-w, \-s and \-y parameters.
.TP
\fB\-h\fR \fB\-\-help\fR
Prints out the help screen.
.SH "REMARKS"
.LP
By default only values of core zero are displayed. How to display settings of
other cores is described in the cpupower(1) manpage in the \-\-cpu option section.
.LP
You can't specify more than one of the output specific options \-o \-e \-a \-g \-p \-d \-l \-w \-f \-y.
.LP
You also can't specify the \-o option combined with the \-c option.
.SH "FILES"
.nf
\fI/sys/devices/system/cpu/cpu*/cpufreq/\fP
\fI/proc/cpufreq\fP (deprecated)
\fI/proc/sys/cpu/\fP (deprecated)
.fi
.SH "AUTHORS"
.nf
Dominik Brodowski <linux@brodo.de> \- author
Mattia Dongili<malattia@gmail.com> \- first autolibtoolization
.fi
.SH "SEE ALSO"
.LP
cpupower\-frequency\-set(1), cpupower(1)
.TH "cpufreq-set" "1" "0.1" "Mattia Dongili" ""
.SH "NAME"
.LP
cpufreq\-set \- A small tool which allows to modify cpufreq settings.
.SH "SYNTAX"
.LP
cpufreq\-set [\fIoptions\fP]
.SH "DESCRIPTION"
.LP
cpufreq\-set allows you to modify cpufreq settings without having to type e.g. "/sys/devices/system/cpu/cpu0/cpufreq/scaling_set_speed" all the time.
.SH "OPTIONS"
.LP
.TP
\fB\-d\fR \fB\-\-min\fR <FREQ>
new minimum CPU frequency the governor may select.
.TP
\fB\-u\fR \fB\-\-max\fR <FREQ>
new maximum CPU frequency the governor may select.
.TP
\fB\-g\fR \fB\-\-governor\fR <GOV>
new cpufreq governor.
.TP
\fB\-f\fR \fB\-\-freq\fR <FREQ>
specific frequency to be set. Requires userspace governor to be available and loaded.
.TP
\fB\-r\fR \fB\-\-related\fR
modify all hardware-related CPUs at the same time
.TP
\fB\-h\fR \fB\-\-help\fR
Prints out the help screen.
.SH "REMARKS"
.LP
By default values are applied on all cores. How to modify single core
configurations is described in the cpupower(1) manpage in the \-\-cpu option section.
.LP
The \-f FREQ, \-\-freq FREQ parameter cannot be combined with any other parameter.
.LP
FREQuencies can be passed in Hz, kHz (default), MHz, GHz, or THz by postfixing the value with the wanted unit name, without any space (frequency in kHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000).
.LP
On Linux kernels up to 2.6.29, the \-r or \-\-related parameter is ignored.
.SH "FILES"
.nf
\fI/sys/devices/system/cpu/cpu*/cpufreq/\fP
\fI/proc/cpufreq\fP (deprecated)
\fI/proc/sys/cpu/\fP (deprecated)
.fi
.SH "AUTHORS"
.nf
Dominik Brodowski <linux@brodo.de> \- author
Mattia Dongili<malattia@gmail.com> \- first autolibtoolization
.fi
.SH "SEE ALSO"
.LP
cpupower\-frequency\-info(1), cpupower(1)
.TH CPUPOWER\-INFO "1" "22/02/2011" "" "cpupower Manual"
.SH NAME
cpupower\-info \- Shows processor power related kernel or hardware configurations
.SH SYNOPSIS
.ft B
.B cpupower info [ \-b ] [ \-s ] [ \-m ]
.SH DESCRIPTION
\fBcpupower info \fP shows kernel configurations or processor hardware
registers affecting processor power saving policies.
Some options are platform wide, some affect single cores. By default values
of core zero are displayed only. cpupower --cpu all cpuinfo will show the
settings of all cores, see cpupower(1) how to choose specific cores.
.SH "SEE ALSO"
Options are described in detail in:
cpupower(1), cpupower-set(1)
.TH CPUPOWER\-MONITOR "1" "22/02/2011" "" "cpupower Manual"
.SH NAME
cpupower\-monitor \- Report processor frequency and idle statistics
.SH SYNOPSIS
.ft B
.B cpupower monitor
.RB "\-l"
.B cpupower monitor
.RB [ "\-m <mon1>," [ "<mon2>,..." ] ]
.RB [ "\-i seconds" ]
.br
.B cpupower monitor
.RB [ "\-m <mon1>," [ "<mon2>,..." ] ]
.RB command
.br
.SH DESCRIPTION
\fBcpupower-monitor \fP reports processor topology, frequency and idle power
state statistics. Either \fBcommand\fP is forked and
statistics are printed upon its completion, or statistics are printed periodically.
\fBcpupower-monitor \fP implements independent processor sleep state and
frequency counters. Some are retrieved from kernel statistics, some are
directly reading out hardware registers. Use \-l to get an overview which are
supported on your system.
.SH Options
.PP
\-l
.RS 4
List available monitors on your system. Additional details about each monitor
are shown:
.RS 2
.IP \(bu
The name in quotation marks which can be passed to the \-m parameter.
.IP \(bu
The number of different counters the monitor supports in brackets.
.IP \(bu
The amount of time in seconds the counters might overflow, due to
implementation constraints.
.IP \(bu
The name and a description of each counter and its processor hierarchy level
coverage in square brackets:
.RS 4
.IP \(bu
[T] \-> Thread
.IP \(bu
[C] \-> Core
.IP \(bu
[P] \-> Processor Package (Socket)
.IP \(bu
[M] \-> Machine/Platform wide counter
.RE
.RE
.RE
.PP
\-m <mon1>,<mon2>,...
.RS 4
Only display specific monitors. Use the monitor string(s) provided by \-l option.
.RE
.PP
\-i seconds
.RS 4
Measure intervall.
.RE
.PP
command
.RS 4
Measure idle and frequency characteristics of an arbitrary command/workload.
The executable \fBcommand\fP is forked and upon its exit, statistics gathered since it was
forked are displayed.
.RE
.PP
\-v
.RS 4
Increase verbosity if the binary was compiled with the DEBUG option set.
.RE
.SH MONITOR DESCRIPTIONS
.SS "Idle_Stats"
Shows statistics of the cpuidle kernel subsystem. Values are retrieved from
/sys/devices/system/cpu/cpu*/cpuidle/state*/.
The kernel updates these values every time an idle state is entered or
left. Therefore there can be some inaccuracy when cores are in an idle
state for some time when the measure starts or ends. In worst case it can happen
that one core stayed in an idle state for the whole measure time and the idle
state usage time as exported by the kernel did not get updated. In this case
a state residency of 0 percent is shown while it was 100.
.SS "Mperf"
The name comes from the aperf/mperf (average and maximum) MSR registers used
which are available on recent X86 processors. It shows the average frequency
(including boost frequencies).
The fact that on all recent hardware the mperf timer stops ticking in any idle
state it is also used to show C0 (processor is active) and Cx (processor is in
any sleep state) times. These counters do not have the inaccuracy restrictions
the "Idle_Stats" counters may show.
May work poorly on Linux-2.6.20 through 2.6.29, as the \fBacpi-cpufreq \fP
kernel frequency driver periodically cleared aperf/mperf registers in those
kernels.
.SS "Nehalem" "SandyBridge"
Intel Core and Package sleep state counters.
Threads (hyperthreaded cores) may not be able to enter deeper core states if
its sibling is utilized.
Deepest package sleep states may in reality show up as machine/platform wide
sleep states and can only be entered if all cores are idle. Look up Intel
manuals (some are provided in the References section) for further details.
.SS "Ontario" "Liano"
AMD laptop and desktop processor (family 12h and 14h) sleep state counters.
The registers are accessed via PCI and therefore can still be read out while
cores have been offlined.
There is one special counter: NBP1 (North Bridge P1).
This one always returns 0 or 1, depending on whether the North Bridge P1
power state got entered at least once during measure time.
Being able to enter NBP1 state also depends on graphics power management.
Therefore this counter can be used to verify whether the graphics' driver
power management is working as expected.
.SH EXAMPLES
cpupower monitor -l" may show:
.RS 4
Monitor "Mperf" (3 states) \- Might overflow after 922000000 s
...
Monitor "Idle_Stats" (3 states) \- Might overflow after 4294967295 s
...
.RE
cpupower monitor \-m "Idle_Stats,Mperf" scp /tmp/test /nfs/tmp
Monitor the scp command, show both Mperf and Idle_Stats states counter
statistics, but in exchanged order.
.RE
Be careful that the typical command to fully utilize one CPU by doing:
cpupower monitor cat /dev/zero >/dev/null
Does not work as expected, because the measured output is redirected to
/dev/null. This could get workarounded by putting the line into an own, tiny
shell script. Hit CTRL\-c to terminate the command and get the measure output
displayed.
.SH REFERENCES
"BIOS and Kernel Developer’s Guide (BKDG) for AMD Family 14h Processors"
http://support.amd.com/us/Processor_TechDocs/43170.pdf
"Intel® Turbo Boost Technology
in Intel® Core™ Microarchitecture (Nehalem) Based Processors"
http://download.intel.com/design/processor/applnots/320354.pdf
"Intel® 64 and IA-32 Architectures Software Developer's Manual
Volume 3B: System Programming Guide"
http://www.intel.com/products/processor/manuals
.SH FILES
.ta
.nf
/dev/cpu/*/msr
/sys/devices/system/cpu/cpu*/cpuidle/state*/.
.fi
.SH "SEE ALSO"
powertop(8), msr(4), vmstat(8)
.PP
.SH AUTHORS
.nf
Written by Thomas Renninger <trenn@suse.de>
Nehalem, SandyBridge monitors and command passing
based on turbostat.8 from Len Brown <len.brown@intel.com>
.TH CPUPOWER\-SET "1" "22/02/2011" "" "cpupower Manual"
.SH NAME
cpupower\-set \- Set processor power related kernel or hardware configurations
.SH SYNOPSIS
.ft B
.B cpupower set [ \-b VAL ] [ \-s VAL ] [ \-m VAL ]
.SH DESCRIPTION
\fBcpupower set \fP sets kernel configurations or directly accesses hardware
registers affecting processor power saving policies.
Some options are platform wide, some affect single cores. By default values
are applied on all cores. How to modify single core configurations is
described in the cpupower(1) manpage in the \-\-cpu option section. Whether an
option affects the whole system or can be applied to individual cores is
described in the Options sections.
Use \fBcpupower info \fP to read out current settings and whether they are
supported on the system at all.
.SH Options
.PP
\-\-perf-bias, \-b
.RS 4
Sets a register on supported Intel processore which allows software to convey
its policy for the relative importance of performance versus energy savings to
the processor.
The range of valid numbers is 0-15, where 0 is maximum
performance and 15 is maximum energy efficiency.
The processor uses this information in model-specific ways
when it must select trade-offs between performance and
energy efficiency.
This policy hint does not supersede Processor Performance states
(P-states) or CPU Idle power states (C-states), but allows
software to have influence where it would otherwise be unable
to express a preference.
For example, this setting may tell the hardware how
aggressively or conservatively to control frequency
in the "turbo range" above the explicitly OS-controlled
P-state frequency range. It may also tell the hardware
how aggressively it should enter the OS requested C-states.
This option can be applied to individual cores only via the \-\-cpu option,
cpupower(1).
Setting the performance bias value on one CPU can modify the setting on
related CPUs as well (for example all CPUs on one socket), because of
hardware restrictions.
Use \fBcpupower -c all info -b\fP to verify.
This options needs the msr kernel driver (CONFIG_X86_MSR) loaded.
.RE
.PP
\-\-sched\-mc, \-m [ VAL ]
.RE
\-\-sched\-smt, \-s [ VAL ]
.RS 4
\-\-sched\-mc utilizes cores in one processor package/socket first before
processes are scheduled to other processor packages/sockets.
\-\-sched\-smt utilizes thread siblings of one processor core first before
processes are scheduled to other cores.
The impact on power consumption and performance (positiv or negativ) heavily
depends on processor support for deep sleep states, frequency scaling and
frequency boost modes and their dependencies between other thread siblings
and processor cores.
Taken over from kernel documentation:
Adjust the kernel's multi-core scheduler support.
Possible values are:
.RS 2
0 - No power saving load balance (default value)
1 - Fill one thread/core/package first for long running threads
2 - Also bias task wakeups to semi-idle cpu package for power
savings
.RE
sched_mc_power_savings is dependent upon SCHED_MC, which is
itself architecture dependent.
sched_smt_power_savings is dependent upon SCHED_SMT, which
is itself architecture dependent.
The two files are independent of each other. It is possible
that one file may be present without the other.
.SH "SEE ALSO"
cpupower-info(1), cpupower-monitor(1), powertop(1)
.PP
.SH AUTHORS
.nf
\-\-perf\-bias parts written by Len Brown <len.brown@intel.com>
Thomas Renninger <trenn@suse.de>
.TH CPUPOWER "1" "07/03/2011" "" "cpupower Manual"
.SH NAME
cpupower \- Shows and sets processor power related values
.SH SYNOPSIS
.ft B
.B cpupower [ \-c cpulist ] subcommand [ARGS]
.B cpupower \-v|\-\-version
.B cpupower \-h|\-\-help
.SH DESCRIPTION
\fBcpupower \fP is a collection of tools to examine and tune power saving
related features of your processor.
The manpages of the subcommands (cpupower\-<subcommand>(1)) provide detailed
descriptions of supported features. Run \fBcpupower help\fP to get an overview
of supported subcommands.
.SH Options
.PP
\-\-help, \-h
.RS 4
Shows supported subcommands and general usage.
.RE
.PP
\-\-cpu cpulist, \-c cpulist
.RS 4
Only show or set values for specific cores.
This option is not supported by all subcommands, details can be found in the
manpages of the subcommands.
Some subcommands access all cores (typically the *\-set commands), some only
the first core (typically the *\-info commands) by default.
The syntax for <cpulist> is based on how the kernel exports CPU bitmasks via
sysfs files. Some examples:
.RS 4
.TP 16
Input
Equivalent to
.TP
all
all cores
.TP
0\-3
0,1,2,3
.TP
0\-7:2
0,2,4,6
.TP
1,3,5-7
1,3,5,6,7
.TP
0\-3:2,8\-15:4
0,2,8,12
.RE
.RE
.PP
\-\-version, \-v
.RS 4
Print the package name and version number.
.SH "SEE ALSO"
cpupower-set(1), cpupower-info(1), cpupower-idle(1),
cpupower-frequency-set(1), cpupower-frequency-info(1), cpupower-monitor(1),
powertop(1)
.PP
.SH AUTHORS
.nf
\-\-perf\-bias parts written by Len Brown <len.brown@intel.com>
Thomas Renninger <trenn@suse.de>
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
#ifndef BUILTIN_H
#define BUILTIN_H
extern int cmd_set(int argc, const char **argv);
extern int cmd_info(int argc, const char **argv);
extern int cmd_freq_set(int argc, const char **argv);
extern int cmd_freq_info(int argc, const char **argv);
extern int cmd_idle_info(int argc, const char **argv);
extern int cmd_monitor(int argc, const char **argv);
extern void set_help(void);
extern void info_help(void);
extern void freq_set_help(void);
extern void freq_info_help(void);
extern void idle_info_help(void);
extern void monitor_help(void);
#endif
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册