Skip to contentSkip to conversation

tools: add power/x86/turbostat

PATCH Superseded Len Brown 4 participants 20 messages Oct 23, 2010 – Feb 11, 2011 Turbostat utility

lore
Share & tools
lore

Len Brown

author maintainer
#0 lore raw

PATCH 1/1 tools: add power/x86/turbostat

From: Len Brown <[email protected]>

turbostat displays actual processor frequency and
idle power saving state residency on Intel Core i3/i5/i7 (Nehalem) and newer processors.

turbostat depends on a non-stop TSC and free-running ACNT/MCNT MSRs hardware, plus the msr driver.

Rather than refuse to run on older hardware turbostat does some simple error checking -- as some older hardware does have a functional TSC, and for some those that do not, deep C-states can be disabled in order to make the TSC work.

Note also that acpu_cpufreq scribbled on ACNT/MCNT MSRs up through Linux-2.6.29, and so turbostat runs best on kernels 2.6.30 and newer.

See the comments in the source code for example usage.

---
 tools/power/x86/turbostat/Makefile    |    7 +
 tools/power/x86/turbostat/turbostat.c | 1103 +++++++++++++++++++++++++++++++++
 2 files changed, 1110 insertions(+), 0 deletions(-)
 create mode 100644 tools/power/x86/turbostat/Makefile
 create mode 100644 tools/power/x86/turbostat/turbostat.c
tools/power/x86/turbostat/Makefile new file +7 -0
new file mode 100644
index 0000000..5a18f55
@@ -0,0 +1,7 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
tools/power/x86/turbostat/turbostat.c new file +1103 -0

1,103 lines not on this page · raw message

Signature · 1 line
1.7.3.1.127.g1bb28
author maintainer
#1 lore raw

PATCH 1/1 tools: add power/x86/turbostat - v2

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

---
v2 refreshed in response to style review from Otavio Salvador

 tools/power/x86/turbostat/Makefile    |    7 +
 tools/power/x86/turbostat/turbostat.c | 1103 +++++++++++++++++++++++++++++++++
 2 files changed, 1110 insertions(+), 0 deletions(-)
 create mode 100644 tools/power/x86/turbostat/Makefile
 create mode 100644 tools/power/x86/turbostat/turbostat.c
tools/power/x86/turbostat/Makefile new file +7 -0
new file mode 100644
index 0000000..5a18f55
@@ -0,0 +1,7 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
tools/power/x86/turbostat/turbostat.c new file +1103 -0

1,103 lines not on this page · raw message

Signature · 1 line
1.7.3.1.127.g1bb28
author maintainer
#2 lore raw

PATCH RESEND 1/1 tools: add power/x86/turbostat - v2

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

---
v2 refreshed in response to style review from Otavio Salvador v2-resent, as 1st v2 sent was the same as v1:-)

 tools/power/x86/turbostat/Makefile    |    7 +
 tools/power/x86/turbostat/turbostat.c | 1107 +++++++++++++++++++++++++++++++++
 2 files changed, 1114 insertions(+), 0 deletions(-)
 create mode 100644 tools/power/x86/turbostat/Makefile
 create mode 100644 tools/power/x86/turbostat/turbostat.c
tools/power/x86/turbostat/Makefile new file +7 -0
new file mode 100644
index 0000000..5a18f55
@@ -0,0 +1,7 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
tools/power/x86/turbostat/turbostat.c new file +1107 -0

1,107 lines not on this page · raw message

Signature · 1 line
1.7.3.2.90.gd4c43
author maintainer
#3 lore raw

PATCH RESEND 1/1 tools: add power/x86/turbostat - v2

In reply to Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

---
v2 refreshed in response to style review from Otavio Salvador v2-resent, as 1st v2 sent was the same as v1:-)

 tools/power/x86/turbostat/Makefile    |    7 +
 tools/power/x86/turbostat/turbostat.c | 1107 +++++++++++++++++++++++++++++++++
 2 files changed, 1114 insertions(+), 0 deletions(-)
 create mode 100644 tools/power/x86/turbostat/Makefile
 create mode 100644 tools/power/x86/turbostat/turbostat.c
tools/power/x86/turbostat/Makefile new file +7 -0
new file mode 100644
index 0000000..5a18f55
@@ -0,0 +1,7 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
tools/power/x86/turbostat/turbostat.c new file +1107 -0

1,107 lines not on this page · raw message

Signature · 1 line
1.7.3.2.90.gd4c43
author maintainer
#4 lore raw

PATCH RESEND 1/1 tools: add power/x86/turbostat - v2

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

Reviewed-by Otavio Salvador
 tools/power/x86/turbostat/Makefile    |    7 +
 tools/power/x86/turbostat/turbostat.c | 1107 +++++++++++++++++++++++++++++++++
 2 files changed, 1114 insertions(+), 0 deletions(-)
 create mode 100644 tools/power/x86/turbostat/Makefile
 create mode 100644 tools/power/x86/turbostat/turbostat.c
tools/power/x86/turbostat/Makefile new file +7 -0
new file mode 100644
index 0000000..5a18f55
@@ -0,0 +1,7 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
tools/power/x86/turbostat/turbostat.c new file +1107 -0

1,107 lines not on this page · raw message

Signature · 3 lines
1.7.3.2.90.gd4c43

--8323328-1715002308-1288149298=:3209--
author maintainer
#5 lore raw

PATCH RESEND 1/1 tools: add power/x86/turbostat - v2

In reply to Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

Reviewed-by Otavio Salvador
 tools/power/x86/turbostat/Makefile    |    7 +
 tools/power/x86/turbostat/turbostat.c | 1107 +++++++++++++++++++++++++++++++++
 2 files changed, 1114 insertions(+), 0 deletions(-)
 create mode 100644 tools/power/x86/turbostat/Makefile
 create mode 100644 tools/power/x86/turbostat/turbostat.c
tools/power/x86/turbostat/Makefile new file +7 -0
new file mode 100644
index 0000000..5a18f55
@@ -0,0 +1,7 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
tools/power/x86/turbostat/turbostat.c new file +1107 -0

1,107 lines not on this page · raw message

Signature · 3 lines
1.7.3.2.90.gd4c43

--8323328-1715002308-1288149298=:3209--
Reviewed-by on v1 1/1
#6 lore raw

Re: [PATCH RESEND] tools: add power/x86/turbostat - v2

Quoted 7 lines · Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors.  Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

Quoted 7 lines

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

Reviewed-by Otavio Salvador
Signature · 3 lines
Otavio Salvador                  O.S. Systems
E-mail: [email protected]  http://www.ossystems.com.br
Mobile: +55 53 9981-7854         http://projetos.ossystems.com.br
Reviewed-by on v1 1/1
#7 lore raw

Re: [PATCH RESEND] tools: add power/x86/turbostat - v2

In reply to Len Brown
Quoted 7 lines · Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors.  Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

Quoted 7 lines

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

See the comments in the source code for example usage.

Reviewed-by Otavio Salvador
Signature · 3 lines
Otavio Salvador                  O.S. Systems
E-mail: [email protected]  http://www.ossystems.com.br
Mobile: +55 53 9981-7854         http://projetos.ossystems.com.br
#8 lore raw

Re: [PATCH RESEND] tools: add power/x86/turbostat - v2

In reply to Len Brown
Quoted 6 lines · Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

Why isn't cpufreq-aperf sufficient for showing the actual frequency? The tool can handle both AMD and Intel CPUs.

There is a CPUID flag which can be checked for APERF/MPERF existence, no need to enforce a general vendor check like this:

  $ ./turbostat
  CPUID: AuthenticAMD != GenuineIntel
Quoted 2 lines

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

Can't this be added to cpuidle-info (see branch cpupowerutils in
git://git.kernel.org/pub/scm/utils/kernel/cpufreq/cpufrequtils.git)?

BTW, gcc v4.4 throws several warnings when compiling your code:

linux-2.6/tools/power/x86/turbostat $ make
cc -Wall turbostat.c -o turbostat
turbostat.c: In function ‘get_msr’:
turbostat.c:234: warning: implicit declaration of function ‘pread’
turbostat.c:237: warning: format ‘%x’ expects type ‘unsigned int’, but argument 4 has type ‘off_t’ turbostat.c:237: warning: format ‘%d’ expects type ‘int’, but argument 5 has type ‘ssize_t’ turbostat.c: In function ‘print_counters’:
turbostat.c:368: warning: unused variable ‘i’
turbostat.c: In function ‘compute_average’:
turbostat.c:508: warning: unused variable ‘i’
turbostat.c: In function ‘insert_cpu_counters’:
turbostat.c:642: warning: unused variable ‘next’
turbostat.c: In function ‘re_initialize’:
turbostat.c:747: warning: implicit declaration of function ‘for_all_cpus’
turbostat.c: In function ‘turbostat_loop’:
turbostat.c:779: warning: unused variable ‘retval’
turbostat.c: In function ‘check_cpuid’:
turbostat.c:868: warning: format ‘%.4s’ expects type ‘char *’, but argument 3 has type ‘unsigned int *’ turbostat.c:868: warning: format ‘%.4s’ expects type ‘char *’, but argument 4 has type ‘unsigned int *’ turbostat.c:868: warning: format ‘%.4s’ expects type ‘char *’, but argument 5 has type ‘unsigned int *’ turbostat.c:870: warning: implicit declaration of function ‘strncmp’
turbostat.c: At top level:
turbostat.c:912: warning: return type defaults to ‘int’
turbostat.c: In function ‘dir_filter’:
turbostat.c:957: warning: implicit declaration of function ‘isdigit’
turbostat.c: In function ‘turbostat_init’:
turbostat.c:1004: warning: unused variable ‘i’
turbostat.c: In function ‘fork_it’:
turbostat.c:1038: warning: implicit declaration of function ‘waitpid’
turbostat.c: In function ‘usage’:
turbostat.c:915: warning: control reaches end of non-void function
turbostat.c: In function ‘compute_delta’:
turbostat.c:401: warning: ‘i’ may be used uninitialized in this function

Regards,
Andreas
_______________________________________________
linux-pm mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/linux-pm

#9 lore raw

Re: [PATCH RESEND] tools: add power/x86/turbostat - v2

In reply to Len Brown
Quoted 6 lines · Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

Why isn't cpufreq-aperf sufficient for showing the actual frequency? The tool can handle both AMD and Intel CPUs.

There is a CPUID flag which can be checked for APERF/MPERF existence, no need to enforce a general vendor check like this:

  $ ./turbostat
  CPUID: AuthenticAMD != GenuineIntel
Quoted 2 lines

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

Can't this be added to cpuidle-info (see branch cpupowerutils in
git://git.kernel.org/pub/scm/utils/kernel/cpufreq/cpufrequtils.git)?

BTW, gcc v4.4 throws several warnings when compiling your code:

linux-2.6/tools/power/x86/turbostat $ make
cc -Wall turbostat.c -o turbostat
turbostat.c: In function ‘get_msr’:
turbostat.c:234: warning: implicit declaration of function ‘pread’
turbostat.c:237: warning: format ‘%x’ expects type ‘unsigned int’, but argument 4 has type ‘off_t’ turbostat.c:237: warning: format ‘%d’ expects type ‘int’, but argument 5 has type ‘ssize_t’ turbostat.c: In function ‘print_counters’:
turbostat.c:368: warning: unused variable ‘i’
turbostat.c: In function ‘compute_average’:
turbostat.c:508: warning: unused variable ‘i’
turbostat.c: In function ‘insert_cpu_counters’:
turbostat.c:642: warning: unused variable ‘next’
turbostat.c: In function ‘re_initialize’:
turbostat.c:747: warning: implicit declaration of function ‘for_all_cpus’
turbostat.c: In function ‘turbostat_loop’:
turbostat.c:779: warning: unused variable ‘retval’
turbostat.c: In function ‘check_cpuid’:
turbostat.c:868: warning: format ‘%.4s’ expects type ‘char *’, but argument 3 has type ‘unsigned int *’ turbostat.c:868: warning: format ‘%.4s’ expects type ‘char *’, but argument 4 has type ‘unsigned int *’ turbostat.c:868: warning: format ‘%.4s’ expects type ‘char *’, but argument 5 has type ‘unsigned int *’ turbostat.c:870: warning: implicit declaration of function ‘strncmp’
turbostat.c: At top level:
turbostat.c:912: warning: return type defaults to ‘int’
turbostat.c: In function ‘dir_filter’:
turbostat.c:957: warning: implicit declaration of function ‘isdigit’
turbostat.c: In function ‘turbostat_init’:
turbostat.c:1004: warning: unused variable ‘i’
turbostat.c: In function ‘fork_it’:
turbostat.c:1038: warning: implicit declaration of function ‘waitpid’
turbostat.c: In function ‘usage’:
turbostat.c:915: warning: control reaches end of non-void function
turbostat.c: In function ‘compute_delta’:
turbostat.c:401: warning: ‘i’ may be used uninitialized in this function

Regards,
Andreas

author maintainer
#10 lore raw

Re: [PATCH RESEND] tools: add power/x86/turbostat - v2

Quoted 10 lines · Andreas Herrmann
Quoted 6 lines · Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

Why isn't cpufreq-aperf sufficient for showing the actual frequency? The tool can handle both AMD and Intel CPUs.

Looking over its source code, I don't see any reason that cpufreq-aperf wouldn't be sufficient for showing actual frequency, were that your goal. I hadn't heard of this tool until your note. I complied it, but I guess I'm too lazy to figure out why it didnt' run.
It seems somewhat elaborate as compared to the task at hand...

Quoted 5 lines

There is a CPUID flag which can be checked for APERF/MPERF existence, no need to enforce a general vendor check like this:

  $ ./turbostat
  CPUID: AuthenticAMD != GenuineIntel

turbostat isn't very interesting on systems that don't have a reliable TSC or C-state residency MSRs. AFAIK, AMD has neither. If you have information to the contrary and can test it on AMD hardware, I'd be happy to enable AMD, as I have neither.

Quoted 5 lines
Quoted 2 lines

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

Can't this be added to cpuidle-info (see branch cpupowerutils in
git://git.kernel.org/pub/scm/utils/kernel/cpufreq/cpufrequtils.git)?

I suppose it could if somebody were in need of a hobby. However, if you want a big tool in this space, you'd probably be better off running powertop2, which will incorporate what turbostat does, and more.

Quoted 2 lines

BTW, gcc v4.4 throws several warnings when compiling your code:

thanks, I'll fix these.

cheers,
Len Brown, Intel Open Source Technology Center

author maintainer
#11 lore raw

Re: [PATCH RESEND] tools: add power/x86/turbostat - v2

In reply to Andreas Herrmann
Quoted 10 lines · Andreas Herrmann
Quoted 6 lines · Len Brown

From: Len Brown <[email protected]>

turbostat displays actual processor frequency on modern Intel processors. Note that this capability depends on free-running APERF and MPERF MSRs, which were cleared by the acpi_cpufreq driver up through Linux-2.6.29.

Why isn't cpufreq-aperf sufficient for showing the actual frequency? The tool can handle both AMD and Intel CPUs.

Looking over its source code, I don't see any reason that cpufreq-aperf wouldn't be sufficient for showing actual frequency, were that your goal. I hadn't heard of this tool until your note. I complied it, but I guess I'm too lazy to figure out why it didnt' run.
It seems somewhat elaborate as compared to the task at hand...

Quoted 5 lines

There is a CPUID flag which can be checked for APERF/MPERF existence, no need to enforce a general vendor check like this:

  $ ./turbostat
  CPUID: AuthenticAMD != GenuineIntel

turbostat isn't very interesting on systems that don't have a reliable TSC or C-state residency MSRs. AFAIK, AMD has neither. If you have information to the contrary and can test it on AMD hardware, I'd be happy to enable AMD, as I have neither.

Quoted 5 lines
Quoted 2 lines

On Intel Core i3/i5/i7 (Nehalem) and newer processors, turbostat displays residency in idle power saving states.

Can't this be added to cpuidle-info (see branch cpupowerutils in
git://git.kernel.org/pub/scm/utils/kernel/cpufreq/cpufrequtils.git)?

I suppose it could if somebody were in need of a hobby. However, if you want a big tool in this space, you'd probably be better off running powertop2, which will incorporate what turbostat does, and more.

Quoted 2 lines

BTW, gcc v4.4 throws several warnings when compiling your code:

thanks, I'll fix these.

cheers,
Len Brown, Intel Open Source Technology Center

author maintainer
#12 lore raw

PATCH v3 1/1 tools: create power/x86/turbostat

In reply to Len Brown

turbostat is a tool to help verify proper operation of CPU idle power saving states, processor performance states, and turbo-mode on modern x86 processors.

---
v3:
Move source comments to a man page
Allow operation on AMD
Add support for new counters in Sandy Bridge

 tools/power/x86/turbostat/Makefile    |    8 +
 tools/power/x86/turbostat/turbostat.8 |  172 ++++++
 tools/power/x86/turbostat/turbostat.c | 1047 +++++++++++++++++++++++++++++++++
 3 files changed, 1227 insertions(+), 0 deletions(-)
tools/power/x86/turbostat/Makefile new file +8 -0
new file mode 100644
index 0000000..fd8e1f1
@@ -0,0 +1,8 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
+	install turbostat.8 /usr/share/man/man8
tools/power/x86/turbostat/turbostat.8 new file +172 -0

172 lines not on this page · raw message

tools/power/x86/turbostat/turbostat.c new file +1047 -0

1,047 lines not on this page · raw message

author maintainer
#13 lore raw

PATCH v3 1/1 tools: create power/x86/turbostat

In reply to Len Brown

turbostat is a tool to help verify proper operation of CPU idle power saving states, processor performance states, and turbo-mode on modern x86 processors.

---
v3:
Move source comments to a man page
Allow operation on AMD
Add support for new counters in Sandy Bridge

 tools/power/x86/turbostat/Makefile    |    8 +
 tools/power/x86/turbostat/turbostat.8 |  172 ++++++
 tools/power/x86/turbostat/turbostat.c | 1047 +++++++++++++++++++++++++++++++++
 3 files changed, 1227 insertions(+), 0 deletions(-)
tools/power/x86/turbostat/Makefile new file +8 -0
new file mode 100644
index 0000000..fd8e1f1
@@ -0,0 +1,8 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
+	install turbostat.8 /usr/share/man/man8
tools/power/x86/turbostat/turbostat.8 new file +172 -0

172 lines not on this page · raw message

tools/power/x86/turbostat/turbostat.c new file +1047 -0

1,047 lines not on this page · raw message

author maintainer
#14 lore raw

PATCH v4 1/1 tools: create power/x86/turbostat

From: Len Brown <[email protected]>

turbostat is a tool to help verify proper operation of processor power management states on modern x86.

---
v4: fix open file descriptor leak, reported by David C. Wong

 tools/power/x86/turbostat/Makefile    |    8 +
 tools/power/x86/turbostat/turbostat.8 |  172 ++++++
 tools/power/x86/turbostat/turbostat.c | 1048 +++++++++++++++++++++++++++++++++
 3 files changed, 1228 insertions(+), 0 deletions(-)
tools/power/x86/turbostat/Makefile new file +8 -0
new file mode 100644
index 0000000..fd8e1f1
@@ -0,0 +1,8 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
+	install turbostat.8 /usr/share/man/man8
tools/power/x86/turbostat/turbostat.8 new file +172 -0

172 lines not on this page · raw message

tools/power/x86/turbostat/turbostat.c new file +1048 -0

1,048 lines not on this page · raw message

author maintainer
#15 lore raw

PATCH v4 1/1 tools: create power/x86/turbostat

In reply to Len Brown

From: Len Brown <[email protected]>

turbostat is a tool to help verify proper operation of processor power management states on modern x86.

---
v4: fix open file descriptor leak, reported by David C. Wong

 tools/power/x86/turbostat/Makefile    |    8 +
 tools/power/x86/turbostat/turbostat.8 |  172 ++++++
 tools/power/x86/turbostat/turbostat.c | 1048 +++++++++++++++++++++++++++++++++
 3 files changed, 1228 insertions(+), 0 deletions(-)
tools/power/x86/turbostat/Makefile new file +8 -0
new file mode 100644
index 0000000..fd8e1f1
@@ -0,0 +1,8 @@
+turbostat : turbostat.c
+
+clean :
+	rm -f turbostat
+
+install :
+	install turbostat /usr/bin/turbostat
+	install turbostat.8 /usr/share/man/man8
tools/power/x86/turbostat/turbostat.8 new file +172 -0

172 lines not on this page · raw message

tools/power/x86/turbostat/turbostat.c new file +1048 -0

1,048 lines not on this page · raw message

#16 lore raw

Re: [PATCH v4] tools: create power/x86/turbostat

Quoted 103 lines · Len Brown

From: Len Brown <[email protected]>

turbostat is a tool to help verify proper operation of processor power management states on modern x86.

---
v4: fix open file descriptor leak, reported by David C. Wong

 tools/power/x86/turbostat/Makefile    |    8 + tools/power/x86/turbostat/turbostat.8 |  172 ++++++ tools/power/x86/turbostat/turbostat.c | 1048 +++++++++++++++++++++++++++++++++

 3 files changed, 1228 insertions(+), 0 deletions(-)
tools/power/x86/turbostat/turbostat.c new file +83 -0
new file mode 100644
index 0000000..4c6983d
@@ -0,0 +1,1048 @@
+/*
+ * turbostat -- show CPU frequency and C-state residency
+ * on modern Intel turbo-capable processors.
+ *
+ * Copyright (c) 2010, Intel Corporation.
+ * Len Brown <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <string.h>
+#include <ctype.h>
+
+#define MSR_TSC        0x10
+#define MSR_NEHALEM_PLATFORM_INFO      0xCE
+#define MSR_NEHALEM_TURBO_RATIO_LIMIT  0x1AD
+#define MSR_APERF      0xE8
+#define MSR_MPERF      0xE7
+#define MSR_PKG_C2_RESIDENCY   0x60D   /* SNB only */
+#define MSR_PKG_C3_RESIDENCY   0x3F8
+#define MSR_PKG_C6_RESIDENCY   0x3F9
+#define MSR_PKG_C7_RESIDENCY   0x3FA   /* SNB only */
+#define MSR_CORE_C3_RESIDENCY  0x3FC
+#define MSR_CORE_C6_RESIDENCY  0x3FD
+#define MSR_CORE_C7_RESIDENCY  0x3FE   /* SNB only */
+
+char *proc_stat = "/proc/stat";
+unsigned int interval_sec = 5; /* set with -i interval_sec */
+unsigned int verbose;          /* set with -v */
+unsigned int skip_c0;
+unsigned int skip_c1;
+unsigned int do_nhm_cstates;
+unsigned int do_snb_cstates;
+unsigned int has_aperf;
+unsigned int units = 1000000000;       /* Ghz etc */
+unsigned int genuine_intel;
+unsigned int has_invariant_tsc;
+unsigned int do_nehalem_platform_info;
+unsigned int do_nehalem_turbo_ratio_limit;
+unsigned int extra_msr_offset;
+double bclk;
+unsigned int show_pkg;
+unsigned int show_core;
+unsigned int show_cpu;
+
+int aperf_mperf_unstable;
+int backwards_count;
+char *progname;
+int need_reinitialize;
+
+int num_cpus;
+
+typedef struct per_cpu_counters {
+       unsigned long long tsc;         /* per thread */
+       unsigned long long aperf;       /* per thread */
+       unsigned long long mperf;       /* per thread */
+       unsigned long long c1;  /* per thread (calculated) */
+       unsigned long long c3;  /* per core */
+       unsigned long long c6;  /* per core */
+       unsigned long long c7;  /* per core */
+       unsigned long long pc2; /* per package */

tools/power/x86/turbostat/turbostat.c line 93

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+       unsigned long long pc3; /* per package */
+       unsigned long long pc6; /* per package */
+       unsigned long long pc7; /* per package */
+       unsigned long long extra_msr;   /* per thread */
+       int pkg;
+       int core;
+       int cpu;
+       struct per_cpu_counters *next;
+} PCC;
+

Why the typedef? Isn't preferable to use struct per_cpu_counters all around (yeah I know it requires more typing, but is less obfuscated).

Quoted 25 lines
tools/power/x86/turbostat/turbostat.c new file +25 -0
+PCC *pcc_even;
+PCC *pcc_odd;
+PCC *pcc_delta;
+PCC *pcc_average;
+struct timeval tv_even;
+struct timeval tv_odd;
+struct timeval tv_delta;
+
+unsigned long long get_msr(int cpu, off_t offset)
+{
+       ssize_t retval;
+       unsigned long long msr;
+       char pathname[32];
+       int fd;
+
+       sprintf(pathname, "/dev/cpu/%d/msr", cpu);
+       fd = open(pathname, O_RDONLY);
+       if (fd < 0) {
+               perror(pathname);
+               need_reinitialize = 1;
+               return 0;
+       }
+
+       retval = pread(fd, &msr, sizeof msr, offset);
+       if (retval != sizeof msr) {

tools/power/x86/turbostat/turbostat.c line 128

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+               fprintf(stderr, "cpu%d pread(..., 0x%zx) = %jd\n",
+                       cpu, offset, retval);
+               exit(-2);
+       }
+
+       close(fd);
+       return msr;
+}
+
+void print_header()

use void in the paramenter? Also make this static?

tools/power/x86/turbostat/turbostat.c line 281

Quoted 4 lines
tools/power/x86/turbostat/turbostat.c new file +4 -0
+
+#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
+
+

Remove this extra blank line?

Quoted 19 lines
tools/power/x86/turbostat/turbostat.c new file +19 -0
+
+void get_counters(PCC *pcc)
+{
+       for ( ; pcc; pcc = pcc->next) {
+               pcc->tsc = get_msr(pcc->cpu, MSR_TSC);
+               if (do_nhm_cstates)
+                       pcc->c3 = get_msr(pcc->cpu, MSR_CORE_C3_RESIDENCY);
+               if (do_nhm_cstates)
+                       pcc->c6 = get_msr(pcc->cpu, MSR_CORE_C6_RESIDENCY);
+               if (do_snb_cstates)
+                       pcc->c7 = get_msr(pcc->cpu, MSR_CORE_C7_RESIDENCY);
+               if (has_aperf)
+                       pcc->aperf = get_msr(pcc->cpu, MSR_APERF);
+               if (has_aperf)
+                       pcc->mperf = get_msr(pcc->cpu, MSR_MPERF);
+               if (do_snb_cstates)
+                       pcc->pc2 = get_msr(pcc->cpu, MSR_PKG_C2_RESIDENCY);
+               if (do_nhm_cstates)
+                       pcc->pc3 = get_msr(pcc->cpu, MSR_PKG_C3_RESIDENCY);

tools/power/x86/turbostat/turbostat.c line 468

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+               if (do_nhm_cstates)
+                       pcc->pc6 = get_msr(pcc->cpu, MSR_PKG_C6_RESIDENCY);
+               if (do_snb_cstates)
+                       pcc->pc7 = get_msr(pcc->cpu, MSR_PKG_C7_RESIDENCY);
+               if (extra_msr_offset)
+                       pcc->extra_msr = get_msr(pcc->cpu, extra_msr_offset);
+       }
+}
+
+

Remove this blank line too?

tools/power/x86/turbostat/turbostat.c line 163

Quoted 2 lines
tools/power/x86/turbostat/turbostat.c new file +2 -0
+void print_nehalem_info()
+{

Add void into the parameter?

Quoted 48 lines
tools/power/x86/turbostat/turbostat.c new file +48 -0
+       unsigned long long msr;
+       unsigned int ratio;
+
+       if (!do_nehalem_platform_info)
+               return;
+
+       msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO);
+
+       ratio = (msr >> 40) & 0xFF;
+       fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
+               ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 8) & 0xFF;
+       fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
+               ratio, bclk, ratio * bclk);
+
+       if (verbose > 1)
+               fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
+
+       if (!do_nehalem_turbo_ratio_limit)
+               return;
+
+       msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT);
+
+       ratio = (msr >> 24) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 16) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 8) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 0) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+}
+
+void free_counter_list(PCC *list)
+{

tools/power/x86/turbostat/turbostat.c line 528

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+       PCC *p;
+
+       for (p = list; p; ) {
+               PCC *free_me;
+
+               free_me = p;
+               p = p->next;
+               free(free_me);
+       }
+       return;

Why the return here? Isn't redundant?

Quoted 44 lines
tools/power/x86/turbostat/turbostat.c new file +44 -0
+
+void insert_cpu_counters(PCC **list, PCC *new)
+{
+       PCC *prev;
+
+       /*
+        * list was empty
+        */
+       if (*list == NULL) {
+               new->next = *list;
+               *list = new;
+               return;
+       }
+
+       show_cpu = 1;   /* there is more than one CPU */
+
+       /*
+        * insert on front of list.
+        * It is sorted by ascending package#, core#, cpu#
+        */
+       if (((*list)->pkg > new->pkg) ||
+           (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
+           (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
+               new->next = *list;
+               *list = new;
+               return;
+       }
+
+       prev = *list;
+
+       while (prev->next && (prev->next->pkg < new->pkg)) {
+               prev = prev->next;
+               show_pkg = 1;   /* there is more than 1 package */
+       }
+
+       while (prev->next && (prev->next->pkg == new->pkg)
+               && (prev->next->core < new->core)) {
+               prev = prev->next;
+               show_core = 1;  /* there is more than 1 core */
+       }
+
+       while (prev->next && (prev->next->pkg == new->pkg)
+               && (prev->next->core == new->core)
+               && (prev->next->cpu < new->cpu)) {

tools/power/x86/turbostat/turbostat.c line 598

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+               prev = prev->next;
+       }
+
+       /*
+        * insert after "prev"
+        */
+       new->next = prev->next;
+       prev->next = new;
+
+       return;

Same thing here about the return.

Quoted 74 lines
tools/power/x86/turbostat/turbostat.c new file +74 -0
+
+int get_physical_package_id(int cpu)
+{
+       char path[64];
+       FILE *filep;
+       int pkg;
+
+       sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
+       filep = fopen(path, "r");
+       if (filep == NULL) {
+               perror(path);
+               exit(1);
+       }
+       fscanf(filep, "%d", &pkg);
+       fclose(filep);
+       return pkg;
+}
+
+int get_core_id(int cpu)
+{
+       char path[64];
+       FILE *filep;
+       int core;
+
+       sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
+       filep = fopen(path, "r");
+       if (filep == NULL) {
+               perror(path);
+               exit(1);
+       }
+       fscanf(filep, "%d", &core);
+       fclose(filep);
+       return core;
+}
+
+/*
+ * run func(index, cpu) on every cpu in /proc/stat
+ */
+
+int for_all_cpus(void (func)(int, int, int))
+{
+       FILE *fp;
+       int cpu_count;
+       int retval;
+
+       fp = fopen(proc_stat, "r");
+       if (fp == NULL) {
+               perror(proc_stat);
+               exit(1);
+       }
+
+       retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
+       if (retval != 0) {
+               perror("/proc/stat format");
+               exit(1);
+       }
+
+       for (cpu_count = 0; ; cpu_count++) {
+               int cpu;
+
+               retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
+               if (retval != 1)
+                       break;
+
+               func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
+       }
+       fclose(fp);
+       return cpu_count;
+}
+
+void re_initialize(void)
+{
+       printf("turbostat: topology changed, re-initializing.\n");
+       free_all_counters();

tools/power/x86/turbostat/turbostat.c line 731

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+       num_cpus = for_all_cpus(alloc_new_cpu_counters);
+       need_reinitialize = 0;
+       printf("num_cpus is now %d\n", num_cpus);
+}
+
+void dummy(int pkg, int core, int cpu) { return; }
+/*
+ * check to see if a cpu came on-line
+ */
+void verify_num_cpus()

Use void on parameter?

Quoted 4 lines
tools/power/x86/turbostat/turbostat.c new file +4 -0
+{
+       int new_num_cpus;
+
+       new_num_cpus = for_all_cpus(dummy);

tools/power/x86/turbostat/turbostat.c line 745

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+
+       if (new_num_cpus != num_cpus) {
+               if (verbose)
+                       printf("num_cpus was %d, is now  %d\n",
+                               num_cpus, new_num_cpus);
+               need_reinitialize = 1;
+       }
+
+       return;
+}

No need of this return, right?
_______________________________________________
linux-pm mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/linux-pm

#17 lore raw

Re: [PATCH v4] tools: create power/x86/turbostat

In reply to Len Brown
Quoted 103 lines · Len Brown

From: Len Brown <[email protected]>

turbostat is a tool to help verify proper operation of processor power management states on modern x86.

---
v4: fix open file descriptor leak, reported by David C. Wong

 tools/power/x86/turbostat/Makefile    |    8 + tools/power/x86/turbostat/turbostat.8 |  172 ++++++ tools/power/x86/turbostat/turbostat.c | 1048 +++++++++++++++++++++++++++++++++

 3 files changed, 1228 insertions(+), 0 deletions(-)
tools/power/x86/turbostat/turbostat.c new file +83 -0
new file mode 100644
index 0000000..4c6983d
@@ -0,0 +1,1048 @@
+/*
+ * turbostat -- show CPU frequency and C-state residency
+ * on modern Intel turbo-capable processors.
+ *
+ * Copyright (c) 2010, Intel Corporation.
+ * Len Brown <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <string.h>
+#include <ctype.h>
+
+#define MSR_TSC        0x10
+#define MSR_NEHALEM_PLATFORM_INFO      0xCE
+#define MSR_NEHALEM_TURBO_RATIO_LIMIT  0x1AD
+#define MSR_APERF      0xE8
+#define MSR_MPERF      0xE7
+#define MSR_PKG_C2_RESIDENCY   0x60D   /* SNB only */
+#define MSR_PKG_C3_RESIDENCY   0x3F8
+#define MSR_PKG_C6_RESIDENCY   0x3F9
+#define MSR_PKG_C7_RESIDENCY   0x3FA   /* SNB only */
+#define MSR_CORE_C3_RESIDENCY  0x3FC
+#define MSR_CORE_C6_RESIDENCY  0x3FD
+#define MSR_CORE_C7_RESIDENCY  0x3FE   /* SNB only */
+
+char *proc_stat = "/proc/stat";
+unsigned int interval_sec = 5; /* set with -i interval_sec */
+unsigned int verbose;          /* set with -v */
+unsigned int skip_c0;
+unsigned int skip_c1;
+unsigned int do_nhm_cstates;
+unsigned int do_snb_cstates;
+unsigned int has_aperf;
+unsigned int units = 1000000000;       /* Ghz etc */
+unsigned int genuine_intel;
+unsigned int has_invariant_tsc;
+unsigned int do_nehalem_platform_info;
+unsigned int do_nehalem_turbo_ratio_limit;
+unsigned int extra_msr_offset;
+double bclk;
+unsigned int show_pkg;
+unsigned int show_core;
+unsigned int show_cpu;
+
+int aperf_mperf_unstable;
+int backwards_count;
+char *progname;
+int need_reinitialize;
+
+int num_cpus;
+
+typedef struct per_cpu_counters {
+       unsigned long long tsc;         /* per thread */
+       unsigned long long aperf;       /* per thread */
+       unsigned long long mperf;       /* per thread */
+       unsigned long long c1;  /* per thread (calculated) */
+       unsigned long long c3;  /* per core */
+       unsigned long long c6;  /* per core */
+       unsigned long long c7;  /* per core */
+       unsigned long long pc2; /* per package */

tools/power/x86/turbostat/turbostat.c line 93

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+       unsigned long long pc3; /* per package */
+       unsigned long long pc6; /* per package */
+       unsigned long long pc7; /* per package */
+       unsigned long long extra_msr;   /* per thread */
+       int pkg;
+       int core;
+       int cpu;
+       struct per_cpu_counters *next;
+} PCC;
+

Why the typedef? Isn't preferable to use struct per_cpu_counters all around (yeah I know it requires more typing, but is less obfuscated).

Quoted 25 lines
tools/power/x86/turbostat/turbostat.c new file +25 -0
+PCC *pcc_even;
+PCC *pcc_odd;
+PCC *pcc_delta;
+PCC *pcc_average;
+struct timeval tv_even;
+struct timeval tv_odd;
+struct timeval tv_delta;
+
+unsigned long long get_msr(int cpu, off_t offset)
+{
+       ssize_t retval;
+       unsigned long long msr;
+       char pathname[32];
+       int fd;
+
+       sprintf(pathname, "/dev/cpu/%d/msr", cpu);
+       fd = open(pathname, O_RDONLY);
+       if (fd < 0) {
+               perror(pathname);
+               need_reinitialize = 1;
+               return 0;
+       }
+
+       retval = pread(fd, &msr, sizeof msr, offset);
+       if (retval != sizeof msr) {

tools/power/x86/turbostat/turbostat.c line 128

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+               fprintf(stderr, "cpu%d pread(..., 0x%zx) = %jd\n",
+                       cpu, offset, retval);
+               exit(-2);
+       }
+
+       close(fd);
+       return msr;
+}
+
+void print_header()

use void in the paramenter? Also make this static?

tools/power/x86/turbostat/turbostat.c line 281

Quoted 4 lines
tools/power/x86/turbostat/turbostat.c new file +4 -0
+
+#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
+
+

Remove this extra blank line?

Quoted 19 lines
tools/power/x86/turbostat/turbostat.c new file +19 -0
+
+void get_counters(PCC *pcc)
+{
+       for ( ; pcc; pcc = pcc->next) {
+               pcc->tsc = get_msr(pcc->cpu, MSR_TSC);
+               if (do_nhm_cstates)
+                       pcc->c3 = get_msr(pcc->cpu, MSR_CORE_C3_RESIDENCY);
+               if (do_nhm_cstates)
+                       pcc->c6 = get_msr(pcc->cpu, MSR_CORE_C6_RESIDENCY);
+               if (do_snb_cstates)
+                       pcc->c7 = get_msr(pcc->cpu, MSR_CORE_C7_RESIDENCY);
+               if (has_aperf)
+                       pcc->aperf = get_msr(pcc->cpu, MSR_APERF);
+               if (has_aperf)
+                       pcc->mperf = get_msr(pcc->cpu, MSR_MPERF);
+               if (do_snb_cstates)
+                       pcc->pc2 = get_msr(pcc->cpu, MSR_PKG_C2_RESIDENCY);
+               if (do_nhm_cstates)
+                       pcc->pc3 = get_msr(pcc->cpu, MSR_PKG_C3_RESIDENCY);

tools/power/x86/turbostat/turbostat.c line 468

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+               if (do_nhm_cstates)
+                       pcc->pc6 = get_msr(pcc->cpu, MSR_PKG_C6_RESIDENCY);
+               if (do_snb_cstates)
+                       pcc->pc7 = get_msr(pcc->cpu, MSR_PKG_C7_RESIDENCY);
+               if (extra_msr_offset)
+                       pcc->extra_msr = get_msr(pcc->cpu, extra_msr_offset);
+       }
+}
+
+

Remove this blank line too?

tools/power/x86/turbostat/turbostat.c line 163

Quoted 2 lines
tools/power/x86/turbostat/turbostat.c new file +2 -0
+void print_nehalem_info()
+{

Add void into the parameter?

Quoted 48 lines
tools/power/x86/turbostat/turbostat.c new file +48 -0
+       unsigned long long msr;
+       unsigned int ratio;
+
+       if (!do_nehalem_platform_info)
+               return;
+
+       msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO);
+
+       ratio = (msr >> 40) & 0xFF;
+       fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
+               ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 8) & 0xFF;
+       fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
+               ratio, bclk, ratio * bclk);
+
+       if (verbose > 1)
+               fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
+
+       if (!do_nehalem_turbo_ratio_limit)
+               return;
+
+       msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT);
+
+       ratio = (msr >> 24) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 16) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 8) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+       ratio = (msr >> 0) & 0xFF;
+       if (ratio)
+               fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
+                       ratio, bclk, ratio * bclk);
+
+}
+
+void free_counter_list(PCC *list)
+{

tools/power/x86/turbostat/turbostat.c line 528

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+       PCC *p;
+
+       for (p = list; p; ) {
+               PCC *free_me;
+
+               free_me = p;
+               p = p->next;
+               free(free_me);
+       }
+       return;

Why the return here? Isn't redundant?

Quoted 44 lines
tools/power/x86/turbostat/turbostat.c new file +44 -0
+
+void insert_cpu_counters(PCC **list, PCC *new)
+{
+       PCC *prev;
+
+       /*
+        * list was empty
+        */
+       if (*list == NULL) {
+               new->next = *list;
+               *list = new;
+               return;
+       }
+
+       show_cpu = 1;   /* there is more than one CPU */
+
+       /*
+        * insert on front of list.
+        * It is sorted by ascending package#, core#, cpu#
+        */
+       if (((*list)->pkg > new->pkg) ||
+           (((*list)->pkg == new->pkg) && ((*list)->core > new->core)) ||
+           (((*list)->pkg == new->pkg) && ((*list)->core == new->core) && ((*list)->cpu > new->cpu))) {
+               new->next = *list;
+               *list = new;
+               return;
+       }
+
+       prev = *list;
+
+       while (prev->next && (prev->next->pkg < new->pkg)) {
+               prev = prev->next;
+               show_pkg = 1;   /* there is more than 1 package */
+       }
+
+       while (prev->next && (prev->next->pkg == new->pkg)
+               && (prev->next->core < new->core)) {
+               prev = prev->next;
+               show_core = 1;  /* there is more than 1 core */
+       }
+
+       while (prev->next && (prev->next->pkg == new->pkg)
+               && (prev->next->core == new->core)
+               && (prev->next->cpu < new->cpu)) {

tools/power/x86/turbostat/turbostat.c line 598

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+               prev = prev->next;
+       }
+
+       /*
+        * insert after "prev"
+        */
+       new->next = prev->next;
+       prev->next = new;
+
+       return;

Same thing here about the return.

Quoted 74 lines
tools/power/x86/turbostat/turbostat.c new file +74 -0
+
+int get_physical_package_id(int cpu)
+{
+       char path[64];
+       FILE *filep;
+       int pkg;
+
+       sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
+       filep = fopen(path, "r");
+       if (filep == NULL) {
+               perror(path);
+               exit(1);
+       }
+       fscanf(filep, "%d", &pkg);
+       fclose(filep);
+       return pkg;
+}
+
+int get_core_id(int cpu)
+{
+       char path[64];
+       FILE *filep;
+       int core;
+
+       sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
+       filep = fopen(path, "r");
+       if (filep == NULL) {
+               perror(path);
+               exit(1);
+       }
+       fscanf(filep, "%d", &core);
+       fclose(filep);
+       return core;
+}
+
+/*
+ * run func(index, cpu) on every cpu in /proc/stat
+ */
+
+int for_all_cpus(void (func)(int, int, int))
+{
+       FILE *fp;
+       int cpu_count;
+       int retval;
+
+       fp = fopen(proc_stat, "r");
+       if (fp == NULL) {
+               perror(proc_stat);
+               exit(1);
+       }
+
+       retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
+       if (retval != 0) {
+               perror("/proc/stat format");
+               exit(1);
+       }
+
+       for (cpu_count = 0; ; cpu_count++) {
+               int cpu;
+
+               retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu);
+               if (retval != 1)
+                       break;
+
+               func(get_physical_package_id(cpu), get_core_id(cpu), cpu);
+       }
+       fclose(fp);
+       return cpu_count;
+}
+
+void re_initialize(void)
+{
+       printf("turbostat: topology changed, re-initializing.\n");
+       free_all_counters();

tools/power/x86/turbostat/turbostat.c line 731

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+       num_cpus = for_all_cpus(alloc_new_cpu_counters);
+       need_reinitialize = 0;
+       printf("num_cpus is now %d\n", num_cpus);
+}
+
+void dummy(int pkg, int core, int cpu) { return; }
+/*
+ * check to see if a cpu came on-line
+ */
+void verify_num_cpus()

Use void on parameter?

Quoted 4 lines
tools/power/x86/turbostat/turbostat.c new file +4 -0
+{
+       int new_num_cpus;
+
+       new_num_cpus = for_all_cpus(dummy);

tools/power/x86/turbostat/turbostat.c line 745

Quoted 10 lines
tools/power/x86/turbostat/turbostat.c new file +10 -0
+
+       if (new_num_cpus != num_cpus) {
+               if (verbose)
+                       printf("num_cpus was %d, is now  %d\n",
+                               num_cpus, new_num_cpus);
+               need_reinitialize = 1;
+       }
+
+       return;
+}

No need of this return, right?

author maintainer
#18 lore raw

Re: [PATCH v4] tools: create power/x86/turbostat

From: Len Brown <[email protected]>

Follow kernel style more closely.
Nuke typdef, re-name "per cpu counters" simply to be counters etc.

This patch changes no functionality.

---
Thanks for the feedback, Thiago.

-Len

 tools/power/x86/turbostat/turbostat.c |  196 ++++++++++++++++-----------------
 1 files changed, 96 insertions(+), 100 deletions(-)
tools/power/x86/turbostat/turbostat.c +96 -100
index 4c6983d..87d309e 100644
@@ -72,7 +72,7 @@ int need_reinitialize;
 
 int num_cpus;
 
-typedef struct per_cpu_counters {
+struct counters {
 	unsigned long long tsc;		/* per thread */
 	unsigned long long aperf;	/* per thread */
 	unsigned long long mperf;	/* per thread */
@@ -88,13 +88,13 @@ typedef struct per_cpu_counters {
 	int pkg;
 	int core;
 	int cpu;
-	struct per_cpu_counters *next;
-} PCC;
+	struct counters *next;
+};
 
-PCC *pcc_even;
-PCC *pcc_odd;
-PCC *pcc_delta;
-PCC *pcc_average;
+struct counters *cnt_even;
+struct counters *cnt_odd;
+struct counters *cnt_delta;
+struct counters *cnt_average;
 struct timeval tv_even;
 struct timeval tv_odd;
 struct timeval tv_delta;
@@ -125,7 +125,7 @@ unsigned long long get_msr(int cpu, off_t offset)
 	return msr;
 }
 
-void print_header()
+void print_header(void)
 {
 	if (show_pkg)
 		fprintf(stderr, "pkg ");
@@ -160,39 +160,39 @@ void print_header()
 	putc('\n', stderr);
 }
 
-void dump_pcc(PCC *pcc)
+void dump_cnt(struct counters *cnt)
 {
-	fprintf(stderr, "package: %d ", pcc->pkg);
-	fprintf(stderr, "core:: %d ", pcc->core);
-	fprintf(stderr, "CPU: %d ", pcc->cpu);
-	fprintf(stderr, "TSC: %016llX\n", pcc->tsc);
-	fprintf(stderr, "c3: %016llX\n", pcc->c3);
-	fprintf(stderr, "c6: %016llX\n", pcc->c6);
-	fprintf(stderr, "c7: %016llX\n", pcc->c7);
-	fprintf(stderr, "aperf: %016llX\n", pcc->aperf);
-	fprintf(stderr, "pc2: %016llX\n", pcc->pc2);
-	fprintf(stderr, "pc3: %016llX\n", pcc->pc3);
-	fprintf(stderr, "pc6: %016llX\n", pcc->pc6);
-	fprintf(stderr, "pc7: %016llX\n", pcc->pc7);
-	fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, pcc->extra_msr);
+	fprintf(stderr, "package: %d ", cnt->pkg);
+	fprintf(stderr, "core:: %d ", cnt->core);
+	fprintf(stderr, "CPU: %d ", cnt->cpu);
+	fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
+	fprintf(stderr, "c3: %016llX\n", cnt->c3);
+	fprintf(stderr, "c6: %016llX\n", cnt->c6);
+	fprintf(stderr, "c7: %016llX\n", cnt->c7);
+	fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
+	fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
+	fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
+	fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
+	fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
+	fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
 }
 
-void dump_list(PCC *pcc)
+void dump_list(struct counters *cnt)
 {
-	printf("dump_list 0x%p\n", pcc);
+	printf("dump_list 0x%p\n", cnt);
 
-	for (; pcc; pcc = pcc->next)
-		dump_pcc(pcc);
+	for (; cnt; cnt = cnt->next)
+		dump_cnt(cnt);
 }
 
-void print_pcc(PCC *p)
+void print_cnt(struct counters *p)
 {
 	double interval_float;
 
 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
 
 	/* topology columns, print blanks on 1st (average) line */
-	if (p == pcc_average) {
+	if (p == cnt_average) {
 		if (show_pkg)
 			fprintf(stderr, "    ");
 		if (show_core)
@@ -262,24 +262,24 @@ void print_pcc(PCC *p)
 	putc('\n', stderr);
 }
 
-void print_counters(PCC *cnt)
+void print_counters(struct counters *counters)
 {
-	PCC *pcc;
+	struct counters *cnt;
 
 	print_header();
 
 	if (num_cpus > 1)
-		print_pcc(pcc_average);
+		print_cnt(cnt_average);
 
-	for (pcc = cnt; pcc != NULL; pcc = pcc->next)
-		print_pcc(pcc);
+	for (cnt = counters; cnt != NULL; cnt = cnt->next)
+		print_cnt(cnt);
 
 }
 
 #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
 
-
-int compute_delta(PCC *after, PCC *before, PCC *delta)
+int compute_delta(struct counters *after,
+	struct counters *before, struct counters *delta)
 {
 	int errors = 0;
 	int perf_err = 0;
@@ -391,20 +391,20 @@ int compute_delta(PCC *after, PCC *before, PCC *delta)
 		delta->extra_msr = after->extra_msr;
 		if (errors) {
 			fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
-			dump_pcc(before);
+			dump_cnt(before);
 			fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
-			dump_pcc(after);
+			dump_cnt(after);
 			errors = 0;
 		}
 	}
 	return 0;
 }
 
-void compute_average(PCC *delta, PCC *avg)
+void compute_average(struct counters *delta, struct counters *avg)
 {
-	PCC *sum;
+	struct counters *sum;
 
-	sum = calloc(1, sizeof(PCC));
+	sum = calloc(1, sizeof(struct counters));
 	if (sum == NULL) {
 		perror("calloc sum");
 		exit(1);
@@ -438,35 +438,34 @@ void compute_average(PCC *delta, PCC *avg)
 	free(sum);
 }
 
-void get_counters(PCC *pcc)
+void get_counters(struct counters *cnt)
 {
-	for ( ; pcc; pcc = pcc->next) {
-		pcc->tsc = get_msr(pcc->cpu, MSR_TSC);
+	for ( ; cnt; cnt = cnt->next) {
+		cnt->tsc = get_msr(cnt->cpu, MSR_TSC);
 		if (do_nhm_cstates)
-			pcc->c3 = get_msr(pcc->cpu, MSR_CORE_C3_RESIDENCY);
+			cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY);
 		if (do_nhm_cstates)
-			pcc->c6 = get_msr(pcc->cpu, MSR_CORE_C6_RESIDENCY);
+			cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY);
 		if (do_snb_cstates)
-			pcc->c7 = get_msr(pcc->cpu, MSR_CORE_C7_RESIDENCY);
+			cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY);
 		if (has_aperf)
-			pcc->aperf = get_msr(pcc->cpu, MSR_APERF);
+			cnt->aperf = get_msr(cnt->cpu, MSR_APERF);
 		if (has_aperf)
-			pcc->mperf = get_msr(pcc->cpu, MSR_MPERF);
+			cnt->mperf = get_msr(cnt->cpu, MSR_MPERF);
 		if (do_snb_cstates)
-			pcc->pc2 = get_msr(pcc->cpu, MSR_PKG_C2_RESIDENCY);
+			cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY);
 		if (do_nhm_cstates)
-			pcc->pc3 = get_msr(pcc->cpu, MSR_PKG_C3_RESIDENCY);
+			cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY);
 		if (do_nhm_cstates)
-			pcc->pc6 = get_msr(pcc->cpu, MSR_PKG_C6_RESIDENCY);
+			cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY);
 		if (do_snb_cstates)
-			pcc->pc7 = get_msr(pcc->cpu, MSR_PKG_C7_RESIDENCY);
+			cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY);
 		if (extra_msr_offset)
-			pcc->extra_msr = get_msr(pcc->cpu, extra_msr_offset);
+			cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset);
 	}
 }
 
-
-void print_nehalem_info()
+void print_nehalem_info(void)
 {
 	unsigned long long msr;
 	unsigned int ratio;
@@ -514,38 +513,38 @@ void print_nehalem_info()
 
 }
 
-void free_counter_list(PCC *list)
+void free_counter_list(struct counters *list)
 {
-	PCC *p;
+	struct counters *p;
 
 	for (p = list; p; ) {
-		PCC *free_me;
+		struct counters *free_me;
 
 		free_me = p;
 		p = p->next;
 		free(free_me);
 	}
-	return;
 }
 
 void free_all_counters(void)
 {
-	free_counter_list(pcc_even);
-	pcc_even = NULL;
+	free_counter_list(cnt_even);
+	cnt_even = NULL;
 
-	free_counter_list(pcc_odd);
-	pcc_odd = NULL;
+	free_counter_list(cnt_odd);
+	cnt_odd = NULL;
 
-	free_counter_list(pcc_delta);
-	pcc_delta = NULL;
+	free_counter_list(cnt_delta);
+	cnt_delta = NULL;
 
-	free_counter_list(pcc_average);
-	pcc_average = NULL;
+	free_counter_list(cnt_average);
+	cnt_average = NULL;
 }
 
-void insert_cpu_counters(PCC **list, PCC *new)
+void insert_counters(struct counters **list,
+	struct counters *new)
 {
-	PCC *prev;
+	struct counters *prev;
 
 	/*
 	 * list was empty
@@ -594,18 +593,16 @@ void insert_cpu_counters(PCC **list, PCC *new)
 	 */
 	new->next = prev->next;
 	prev->next = new;
-
-	return;
 }
 
-void alloc_new_cpu_counters(int pkg, int core, int cpu)
+void alloc_new_counters(int pkg, int core, int cpu)
 {
-	PCC *new;
+	struct counters *new;
 
 	if (verbose > 1)
 		printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1, sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -613,9 +610,10 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	insert_cpu_counters(&pcc_odd, new);
+	insert_counters(&cnt_odd, new);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1,
+		sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -623,9 +621,9 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	insert_cpu_counters(&pcc_even, new);
+	insert_counters(&cnt_even, new);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1, sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -633,9 +631,9 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	insert_cpu_counters(&pcc_delta, new);
+	insert_counters(&cnt_delta, new);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1, sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -643,7 +641,7 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	pcc_average = new;
+	cnt_average = new;
 }
 
 int get_physical_package_id(int cpu)
@@ -719,7 +717,7 @@ void re_initialize(void)
 {
 	printf("turbostat: topology changed, re-initializing.\n");
 	free_all_counters();
-	num_cpus = for_all_cpus(alloc_new_cpu_counters);
+	num_cpus = for_all_cpus(alloc_new_counters);
 	need_reinitialize = 0;
 	printf("num_cpus is now %d\n", num_cpus);
 }
@@ -728,7 +726,7 @@ void dummy(int pkg, int core, int cpu) { return; }
 /*
  * check to see if a cpu came on-line
  */
-void verify_num_cpus()
+void verify_num_cpus(void)
 {
 	int new_num_cpus;
 
@@ -740,14 +738,12 @@ void verify_num_cpus()
 				num_cpus, new_num_cpus);
 		need_reinitialize = 1;
 	}
-
-	return;
 }
 
 void turbostat_loop()
 {
 restart:
-	get_counters(pcc_even);
+	get_counters(cnt_even);
 	gettimeofday(&tv_even, (struct timezone *)NULL);
 
 	while (1) {
@@ -757,24 +753,24 @@ restart:
 			goto restart;
 		}
 		sleep(interval_sec);
-		get_counters(pcc_odd);
+		get_counters(cnt_odd);
 		gettimeofday(&tv_odd, (struct timezone *)NULL);
 
-		compute_delta(pcc_odd, pcc_even, pcc_delta);
+		compute_delta(cnt_odd, cnt_even, cnt_delta);
 		timersub(&tv_odd, &tv_even, &tv_delta);
-		compute_average(pcc_delta, pcc_average);
-		print_counters(pcc_delta);
+		compute_average(cnt_delta, cnt_average);
+		print_counters(cnt_delta);
 		if (need_reinitialize) {
 			re_initialize();
 			goto restart;
 		}
 		sleep(interval_sec);
-		get_counters(pcc_even);
+		get_counters(cnt_even);
 		gettimeofday(&tv_even, (struct timezone *)NULL);
-		compute_delta(pcc_even, pcc_odd, pcc_delta);
+		compute_delta(cnt_even, cnt_odd, cnt_delta);
 		timersub(&tv_even, &tv_odd, &tv_delta);
-		compute_average(pcc_delta, pcc_average);
-		print_counters(pcc_delta);
+		compute_average(cnt_delta, cnt_average);
+		print_counters(cnt_delta);
 	}
 }
 
@@ -952,7 +948,7 @@ void turbostat_init()
 	check_dev_msr();
 	check_super_user();
 
-	num_cpus = for_all_cpus(alloc_new_cpu_counters);
+	num_cpus = for_all_cpus(alloc_new_counters);
 
 	if (verbose)
 		print_nehalem_info();
@@ -962,7 +958,7 @@ int fork_it(char **argv)
 {
 	int retval;
 	pid_t child_pid;
-	get_counters(pcc_even);
+	get_counters(cnt_even);
 	gettimeofday(&tv_even, (struct timezone *)NULL);
 
 	child_pid = fork();
@@ -985,14 +981,14 @@ int fork_it(char **argv)
 			exit(1);
 		}
 	}
-	get_counters(pcc_odd);
+	get_counters(cnt_odd);
 	gettimeofday(&tv_odd, (struct timezone *)NULL);
-	retval = compute_delta(pcc_odd, pcc_even, pcc_delta);
+	retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
 
 	timersub(&tv_odd, &tv_even, &tv_delta);
-	compute_average(pcc_delta, pcc_average);
+	compute_average(cnt_delta, cnt_average);
 	if (!retval)
-		print_counters(pcc_delta);
+		print_counters(cnt_delta);
 
 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);;
 
Signature · 1 line
1.7.4.45.g1a9f
author maintainer
#19 lore raw

Re: [PATCH v4] tools: create power/x86/turbostat

In reply to Thiago Farina

From: Len Brown <[email protected]>

Follow kernel style more closely.
Nuke typdef, re-name "per cpu counters" simply to be counters etc.

This patch changes no functionality.

---
Thanks for the feedback, Thiago.

-Len

 tools/power/x86/turbostat/turbostat.c |  196 ++++++++++++++++-----------------
 1 files changed, 96 insertions(+), 100 deletions(-)
tools/power/x86/turbostat/turbostat.c +96 -100
index 4c6983d..87d309e 100644
@@ -72,7 +72,7 @@ int need_reinitialize;
 
 int num_cpus;
 
-typedef struct per_cpu_counters {
+struct counters {
 	unsigned long long tsc;		/* per thread */
 	unsigned long long aperf;	/* per thread */
 	unsigned long long mperf;	/* per thread */
@@ -88,13 +88,13 @@ typedef struct per_cpu_counters {
 	int pkg;
 	int core;
 	int cpu;
-	struct per_cpu_counters *next;
-} PCC;
+	struct counters *next;
+};
 
-PCC *pcc_even;
-PCC *pcc_odd;
-PCC *pcc_delta;
-PCC *pcc_average;
+struct counters *cnt_even;
+struct counters *cnt_odd;
+struct counters *cnt_delta;
+struct counters *cnt_average;
 struct timeval tv_even;
 struct timeval tv_odd;
 struct timeval tv_delta;
@@ -125,7 +125,7 @@ unsigned long long get_msr(int cpu, off_t offset)
 	return msr;
 }
 
-void print_header()
+void print_header(void)
 {
 	if (show_pkg)
 		fprintf(stderr, "pkg ");
@@ -160,39 +160,39 @@ void print_header()
 	putc('\n', stderr);
 }
 
-void dump_pcc(PCC *pcc)
+void dump_cnt(struct counters *cnt)
 {
-	fprintf(stderr, "package: %d ", pcc->pkg);
-	fprintf(stderr, "core:: %d ", pcc->core);
-	fprintf(stderr, "CPU: %d ", pcc->cpu);
-	fprintf(stderr, "TSC: %016llX\n", pcc->tsc);
-	fprintf(stderr, "c3: %016llX\n", pcc->c3);
-	fprintf(stderr, "c6: %016llX\n", pcc->c6);
-	fprintf(stderr, "c7: %016llX\n", pcc->c7);
-	fprintf(stderr, "aperf: %016llX\n", pcc->aperf);
-	fprintf(stderr, "pc2: %016llX\n", pcc->pc2);
-	fprintf(stderr, "pc3: %016llX\n", pcc->pc3);
-	fprintf(stderr, "pc6: %016llX\n", pcc->pc6);
-	fprintf(stderr, "pc7: %016llX\n", pcc->pc7);
-	fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, pcc->extra_msr);
+	fprintf(stderr, "package: %d ", cnt->pkg);
+	fprintf(stderr, "core:: %d ", cnt->core);
+	fprintf(stderr, "CPU: %d ", cnt->cpu);
+	fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
+	fprintf(stderr, "c3: %016llX\n", cnt->c3);
+	fprintf(stderr, "c6: %016llX\n", cnt->c6);
+	fprintf(stderr, "c7: %016llX\n", cnt->c7);
+	fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
+	fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
+	fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
+	fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
+	fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
+	fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
 }
 
-void dump_list(PCC *pcc)
+void dump_list(struct counters *cnt)
 {
-	printf("dump_list 0x%p\n", pcc);
+	printf("dump_list 0x%p\n", cnt);
 
-	for (; pcc; pcc = pcc->next)
-		dump_pcc(pcc);
+	for (; cnt; cnt = cnt->next)
+		dump_cnt(cnt);
 }
 
-void print_pcc(PCC *p)
+void print_cnt(struct counters *p)
 {
 	double interval_float;
 
 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
 
 	/* topology columns, print blanks on 1st (average) line */
-	if (p == pcc_average) {
+	if (p == cnt_average) {
 		if (show_pkg)
 			fprintf(stderr, "    ");
 		if (show_core)
@@ -262,24 +262,24 @@ void print_pcc(PCC *p)
 	putc('\n', stderr);
 }
 
-void print_counters(PCC *cnt)
+void print_counters(struct counters *counters)
 {
-	PCC *pcc;
+	struct counters *cnt;
 
 	print_header();
 
 	if (num_cpus > 1)
-		print_pcc(pcc_average);
+		print_cnt(cnt_average);
 
-	for (pcc = cnt; pcc != NULL; pcc = pcc->next)
-		print_pcc(pcc);
+	for (cnt = counters; cnt != NULL; cnt = cnt->next)
+		print_cnt(cnt);
 
 }
 
 #define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
 
-
-int compute_delta(PCC *after, PCC *before, PCC *delta)
+int compute_delta(struct counters *after,
+	struct counters *before, struct counters *delta)
 {
 	int errors = 0;
 	int perf_err = 0;
@@ -391,20 +391,20 @@ int compute_delta(PCC *after, PCC *before, PCC *delta)
 		delta->extra_msr = after->extra_msr;
 		if (errors) {
 			fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
-			dump_pcc(before);
+			dump_cnt(before);
 			fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
-			dump_pcc(after);
+			dump_cnt(after);
 			errors = 0;
 		}
 	}
 	return 0;
 }
 
-void compute_average(PCC *delta, PCC *avg)
+void compute_average(struct counters *delta, struct counters *avg)
 {
-	PCC *sum;
+	struct counters *sum;
 
-	sum = calloc(1, sizeof(PCC));
+	sum = calloc(1, sizeof(struct counters));
 	if (sum == NULL) {
 		perror("calloc sum");
 		exit(1);
@@ -438,35 +438,34 @@ void compute_average(PCC *delta, PCC *avg)
 	free(sum);
 }
 
-void get_counters(PCC *pcc)
+void get_counters(struct counters *cnt)
 {
-	for ( ; pcc; pcc = pcc->next) {
-		pcc->tsc = get_msr(pcc->cpu, MSR_TSC);
+	for ( ; cnt; cnt = cnt->next) {
+		cnt->tsc = get_msr(cnt->cpu, MSR_TSC);
 		if (do_nhm_cstates)
-			pcc->c3 = get_msr(pcc->cpu, MSR_CORE_C3_RESIDENCY);
+			cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY);
 		if (do_nhm_cstates)
-			pcc->c6 = get_msr(pcc->cpu, MSR_CORE_C6_RESIDENCY);
+			cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY);
 		if (do_snb_cstates)
-			pcc->c7 = get_msr(pcc->cpu, MSR_CORE_C7_RESIDENCY);
+			cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY);
 		if (has_aperf)
-			pcc->aperf = get_msr(pcc->cpu, MSR_APERF);
+			cnt->aperf = get_msr(cnt->cpu, MSR_APERF);
 		if (has_aperf)
-			pcc->mperf = get_msr(pcc->cpu, MSR_MPERF);
+			cnt->mperf = get_msr(cnt->cpu, MSR_MPERF);
 		if (do_snb_cstates)
-			pcc->pc2 = get_msr(pcc->cpu, MSR_PKG_C2_RESIDENCY);
+			cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY);
 		if (do_nhm_cstates)
-			pcc->pc3 = get_msr(pcc->cpu, MSR_PKG_C3_RESIDENCY);
+			cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY);
 		if (do_nhm_cstates)
-			pcc->pc6 = get_msr(pcc->cpu, MSR_PKG_C6_RESIDENCY);
+			cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY);
 		if (do_snb_cstates)
-			pcc->pc7 = get_msr(pcc->cpu, MSR_PKG_C7_RESIDENCY);
+			cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY);
 		if (extra_msr_offset)
-			pcc->extra_msr = get_msr(pcc->cpu, extra_msr_offset);
+			cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset);
 	}
 }
 
-
-void print_nehalem_info()
+void print_nehalem_info(void)
 {
 	unsigned long long msr;
 	unsigned int ratio;
@@ -514,38 +513,38 @@ void print_nehalem_info()
 
 }
 
-void free_counter_list(PCC *list)
+void free_counter_list(struct counters *list)
 {
-	PCC *p;
+	struct counters *p;
 
 	for (p = list; p; ) {
-		PCC *free_me;
+		struct counters *free_me;
 
 		free_me = p;
 		p = p->next;
 		free(free_me);
 	}
-	return;
 }
 
 void free_all_counters(void)
 {
-	free_counter_list(pcc_even);
-	pcc_even = NULL;
+	free_counter_list(cnt_even);
+	cnt_even = NULL;
 
-	free_counter_list(pcc_odd);
-	pcc_odd = NULL;
+	free_counter_list(cnt_odd);
+	cnt_odd = NULL;
 
-	free_counter_list(pcc_delta);
-	pcc_delta = NULL;
+	free_counter_list(cnt_delta);
+	cnt_delta = NULL;
 
-	free_counter_list(pcc_average);
-	pcc_average = NULL;
+	free_counter_list(cnt_average);
+	cnt_average = NULL;
 }
 
-void insert_cpu_counters(PCC **list, PCC *new)
+void insert_counters(struct counters **list,
+	struct counters *new)
 {
-	PCC *prev;
+	struct counters *prev;
 
 	/*
 	 * list was empty
@@ -594,18 +593,16 @@ void insert_cpu_counters(PCC **list, PCC *new)
 	 */
 	new->next = prev->next;
 	prev->next = new;
-
-	return;
 }
 
-void alloc_new_cpu_counters(int pkg, int core, int cpu)
+void alloc_new_counters(int pkg, int core, int cpu)
 {
-	PCC *new;
+	struct counters *new;
 
 	if (verbose > 1)
 		printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1, sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -613,9 +610,10 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	insert_cpu_counters(&pcc_odd, new);
+	insert_counters(&cnt_odd, new);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1,
+		sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -623,9 +621,9 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	insert_cpu_counters(&pcc_even, new);
+	insert_counters(&cnt_even, new);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1, sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -633,9 +631,9 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	insert_cpu_counters(&pcc_delta, new);
+	insert_counters(&cnt_delta, new);
 
-	new = (PCC *)calloc(1, sizeof(PCC));
+	new = (struct counters *)calloc(1, sizeof(struct counters));
 	if (new == NULL) {
 		perror("calloc");
 		exit(1);
@@ -643,7 +641,7 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
 	new->pkg = pkg;
 	new->core = core;
 	new->cpu = cpu;
-	pcc_average = new;
+	cnt_average = new;
 }
 
 int get_physical_package_id(int cpu)
@@ -719,7 +717,7 @@ void re_initialize(void)
 {
 	printf("turbostat: topology changed, re-initializing.\n");
 	free_all_counters();
-	num_cpus = for_all_cpus(alloc_new_cpu_counters);
+	num_cpus = for_all_cpus(alloc_new_counters);
 	need_reinitialize = 0;
 	printf("num_cpus is now %d\n", num_cpus);
 }
@@ -728,7 +726,7 @@ void dummy(int pkg, int core, int cpu) { return; }
 /*
  * check to see if a cpu came on-line
  */
-void verify_num_cpus()
+void verify_num_cpus(void)
 {
 	int new_num_cpus;
 
@@ -740,14 +738,12 @@ void verify_num_cpus()
 				num_cpus, new_num_cpus);
 		need_reinitialize = 1;
 	}
-
-	return;
 }
 
 void turbostat_loop()
 {
 restart:
-	get_counters(pcc_even);
+	get_counters(cnt_even);
 	gettimeofday(&tv_even, (struct timezone *)NULL);
 
 	while (1) {
@@ -757,24 +753,24 @@ restart:
 			goto restart;
 		}
 		sleep(interval_sec);
-		get_counters(pcc_odd);
+		get_counters(cnt_odd);
 		gettimeofday(&tv_odd, (struct timezone *)NULL);
 
-		compute_delta(pcc_odd, pcc_even, pcc_delta);
+		compute_delta(cnt_odd, cnt_even, cnt_delta);
 		timersub(&tv_odd, &tv_even, &tv_delta);
-		compute_average(pcc_delta, pcc_average);
-		print_counters(pcc_delta);
+		compute_average(cnt_delta, cnt_average);
+		print_counters(cnt_delta);
 		if (need_reinitialize) {
 			re_initialize();
 			goto restart;
 		}
 		sleep(interval_sec);
-		get_counters(pcc_even);
+		get_counters(cnt_even);
 		gettimeofday(&tv_even, (struct timezone *)NULL);
-		compute_delta(pcc_even, pcc_odd, pcc_delta);
+		compute_delta(cnt_even, cnt_odd, cnt_delta);
 		timersub(&tv_even, &tv_odd, &tv_delta);
-		compute_average(pcc_delta, pcc_average);
-		print_counters(pcc_delta);
+		compute_average(cnt_delta, cnt_average);
+		print_counters(cnt_delta);
 	}
 }
 
@@ -952,7 +948,7 @@ void turbostat_init()
 	check_dev_msr();
 	check_super_user();
 
-	num_cpus = for_all_cpus(alloc_new_cpu_counters);
+	num_cpus = for_all_cpus(alloc_new_counters);
 
 	if (verbose)
 		print_nehalem_info();
@@ -962,7 +958,7 @@ int fork_it(char **argv)
 {
 	int retval;
 	pid_t child_pid;
-	get_counters(pcc_even);
+	get_counters(cnt_even);
 	gettimeofday(&tv_even, (struct timezone *)NULL);
 
 	child_pid = fork();
@@ -985,14 +981,14 @@ int fork_it(char **argv)
 			exit(1);
 		}
 	}
-	get_counters(pcc_odd);
+	get_counters(cnt_odd);
 	gettimeofday(&tv_odd, (struct timezone *)NULL);
-	retval = compute_delta(pcc_odd, pcc_even, pcc_delta);
+	retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
 
 	timersub(&tv_odd, &tv_even, &tv_delta);
-	compute_average(pcc_delta, pcc_average);
+	compute_average(cnt_delta, cnt_average);
 	if (!retval)
-		print_counters(pcc_delta);
+		print_counters(cnt_delta);
 
 	fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);;
 
Signature · 1 line
1.7.4.45.g1a9f