linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Len Brown <lenb@kernel.org>
To: linux-acpi@vger.kernel.org, linux-pm@lists.linux-foundation.org
Cc: linux-kernel@vger.kernel.org, Len Brown <len.brown@intel.com>
Subject: [PATCH 41/76] tools turbostat: harden against cpu online/offline
Date: Fri, 30 Mar 2012 06:13:44 -0400	[thread overview]
Message-ID: <15aaa34654831e98dd76f7738b6c7f5d05a66430.1333101989.git.len.brown@intel.com> (raw)
In-Reply-To: <1333102459-23750-1-git-send-email-lenb@kernel.org>
In-Reply-To: <09f98a825a821f7a3f1b162f9ed023f37213a63b.1333101989.git.len.brown@intel.com>

From: Len Brown <len.brown@intel.com>

Sometimes users have turbostat running in interval mode
when they take processors offline/online.

Previously, turbostat would survive, but not gracefully.

Tighten up the error checking so turbostat notices
changesn sooner, and print just 1 line on change:

turbostat: re-initialized with num_cpus %d

Signed-off-by: Len Brown <len.brown@intel.com>
---
 tools/power/x86/turbostat/turbostat.c |  115 +++++++++++++++++----------------
 1 file changed, 61 insertions(+), 54 deletions(-)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index fa60872..ab2f682 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -71,7 +71,6 @@ unsigned int show_cpu;
 int aperf_mperf_unstable;
 int backwards_count;
 char *progname;
-int need_reinitialize;
 
 int num_cpus;
 cpu_set_t *cpu_mask;
@@ -138,30 +137,24 @@ int cpu_migrate(int cpu)
 		return 0;
 }
 
-unsigned long long get_msr(int cpu, off_t offset)
+int get_msr(int cpu, off_t offset, unsigned long long *msr)
 {
 	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) {
-		fprintf(stderr, "cpu%d pread(..., 0x%zx) = %jd\n",
-			cpu, offset, retval);
-		exit(-2);
-	}
+	if (fd < 0)
+		return -1;
 
+	retval = pread(fd, msr, sizeof *msr, offset);
 	close(fd);
-	return msr;
+
+	if (retval != sizeof *msr)
+		return -1;
+
+	return 0;
 }
 
 void print_header(void)
@@ -506,36 +499,51 @@ void compute_average(struct counters *delta, struct counters *avg)
 	free(sum);
 }
 
-void get_counters(struct counters *cnt)
+int get_counters(struct counters *cnt)
 {
 	for ( ; cnt; cnt = cnt->next) {
-		if (cpu_migrate(cnt->cpu)) {
-			need_reinitialize = 1;
-			return;
+
+		if (cpu_migrate(cnt->cpu))
+			return -1;
+
+		if (get_msr(cnt->cpu, MSR_TSC, &cnt->tsc))
+			return -1;
+
+		if (has_aperf) {
+			if (get_msr(cnt->cpu, MSR_APERF, &cnt->aperf))
+				return -1;
+			if (get_msr(cnt->cpu, MSR_MPERF, &cnt->mperf))
+				return -1;
+		}
+
+		if (do_nhm_cstates) {
+			if (get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY, &cnt->c3))
+				return -1;
+			if (get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY, &cnt->c6))
+				return -1;
 		}
 
-		cnt->tsc = get_msr(cnt->cpu, MSR_TSC);
-		if (do_nhm_cstates)
-			cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY);
-		if (do_nhm_cstates)
-			cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY);
-		if (do_snb_cstates)
-			cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY);
-		if (has_aperf)
-			cnt->aperf = get_msr(cnt->cpu, MSR_APERF);
-		if (has_aperf)
-			cnt->mperf = get_msr(cnt->cpu, MSR_MPERF);
-		if (do_snb_cstates)
-			cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY);
-		if (do_nhm_cstates)
-			cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY);
-		if (do_nhm_cstates)
-			cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY);
 		if (do_snb_cstates)
-			cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY);
+			if (get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY, &cnt->c7))
+				return -1;
+
+		if (do_nhm_cstates) {
+			if (get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY, &cnt->pc3))
+				return -1;
+			if (get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY, &cnt->pc6))
+				return -1;
+		}
+		if (do_snb_cstates) {
+			if (get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY, &cnt->pc2))
+				return -1;
+			if (get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY, &cnt->pc7))
+				return -1;
+		}
 		if (extra_msr_offset)
-			cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset);
+			if (get_msr(cnt->cpu, extra_msr_offset, &cnt->extra_msr))
+				return -1;
 	}
+	return 0;
 }
 
 void print_nehalem_info(void)
@@ -546,7 +554,7 @@ void print_nehalem_info(void)
 	if (!do_nehalem_platform_info)
 		return;
 
-	msr = get_msr(0, MSR_NEHALEM_PLATFORM_INFO);
+	get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
 
 	ratio = (msr >> 40) & 0xFF;
 	fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
@@ -562,7 +570,7 @@ void print_nehalem_info(void)
 	if (!do_nehalem_turbo_ratio_limit)
 		return;
 
-	msr = get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT);
+	get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
 
 	ratio = (msr >> 24) & 0xFF;
 	if (ratio)
@@ -755,7 +763,7 @@ int get_core_id(int cpu)
 }
 
 /*
- * run func(index, cpu) on every cpu in /proc/stat
+ * run func(pkg, core, cpu) on every cpu in /proc/stat
  */
 
 int for_all_cpus(void (func)(int, int, int))
@@ -791,20 +799,18 @@ int for_all_cpus(void (func)(int, int, int))
 
 void re_initialize(void)
 {
-	printf("turbostat: topology changed, re-initializing.\n");
 	free_all_counters();
 	num_cpus = for_all_cpus(alloc_new_counters);
-	need_reinitialize = 0;
 	cpu_mask_uninit();
 	cpu_mask_init(num_cpus);
-	printf("num_cpus is now %d\n", num_cpus);
+	printf("turbostat: re-initialized with num_cpus %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(void)
+int verify_num_cpus(void)
 {
 	int new_num_cpus;
 
@@ -814,8 +820,9 @@ void verify_num_cpus(void)
 		if (verbose)
 			printf("num_cpus was %d, is now  %d\n",
 				num_cpus, new_num_cpus);
-		need_reinitialize = 1;
+		return -1;
 	}
+	return 0;
 }
 
 void turbostat_loop()
@@ -825,25 +832,25 @@ restart:
 	gettimeofday(&tv_even, (struct timezone *)NULL);
 
 	while (1) {
-		verify_num_cpus();
-		if (need_reinitialize) {
+		if (verify_num_cpus()) {
 			re_initialize();
 			goto restart;
 		}
 		sleep(interval_sec);
-		get_counters(cnt_odd);
+		if (get_counters(cnt_odd)) {
+			re_initialize();
+			goto restart;
+		}
 		gettimeofday(&tv_odd, (struct timezone *)NULL);
-
 		compute_delta(cnt_odd, cnt_even, cnt_delta);
 		timersub(&tv_odd, &tv_even, &tv_delta);
 		compute_average(cnt_delta, cnt_average);
 		print_counters(cnt_delta);
-		if (need_reinitialize) {
+		sleep(interval_sec);
+		if (get_counters(cnt_even)) {
 			re_initialize();
 			goto restart;
 		}
-		sleep(interval_sec);
-		get_counters(cnt_even);
 		gettimeofday(&tv_even, (struct timezone *)NULL);
 		compute_delta(cnt_even, cnt_odd, cnt_delta);
 		timersub(&tv_even, &tv_odd, &tv_delta);
-- 
1.7.10.rc2.19.gfae9d


  parent reply	other threads:[~2012-03-30 10:18 UTC|newest]

Thread overview: 93+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-30 10:13 ACPI & Power Management patches for Linux-3.4 Len Brown
2012-03-30 10:13 ` [PATCH 01/76] x86, acpi, tboot: Have a ACPI os prepare sleep instead of calling tboot_sleep Len Brown
2012-03-30 10:13   ` [PATCH 02/76] tboot: Add return values for tboot_sleep Len Brown
2012-03-30 10:13   ` [PATCH 03/76] ACPI: ignore FADT reset-reg-sup flag Len Brown
2012-03-30 10:13   ` [PATCH 04/76] ACPICA: Fix regression in FADT revision checks Len Brown
2012-03-30 13:14     ` Josh Boyer
2012-04-03 19:58       ` [3.0.y, 3.2.y, 3.3.y] " Jonathan Nieder
2012-04-03 20:15         ` Josh Boyer
2012-04-04 18:58           ` Greg Kroah-Hartman
2012-03-30 10:13   ` [PATCH 05/76] cpuidle: Add common time keeping and irq enabling Len Brown
2012-03-30 10:13   ` [PATCH 06/76] ARM: at91: Consolidate time keeping and irq enable Len Brown
2012-03-30 10:13   ` [PATCH 07/76] ARM: kirkwood: " Len Brown
2012-03-30 10:13   ` [PATCH 08/76] ARM: davinci: " Len Brown
2012-03-30 10:13   ` [PATCH 09/76] ARM: omap: Consolidate OMAP3 " Len Brown
2012-03-30 10:13   ` [PATCH 10/76] ARM: omap: Consolidate OMAP4 " Len Brown
2012-03-30 10:13   ` [PATCH 11/76] ARM: shmobile: Consolidate " Len Brown
2012-03-30 10:13   ` [PATCH 12/76] SH: " Len Brown
2012-03-30 10:13   ` [PATCH 13/76] drivers/thermal/thermal_sys.c: fix build warning Len Brown
2012-03-30 10:13   ` [PATCH 14/76] thermal_sys: remove unnecessary line continuations Len Brown
2012-03-30 10:13   ` [PATCH 15/76] thermal_sys: remove obfuscating used-once macros Len Brown
2012-03-30 10:13   ` [PATCH 16/76] thermal_sys: kernel style cleanups Len Brown
2012-03-30 10:13   ` [PATCH 17/76] thermal_sys: convert printks to pr_<level> Len Brown
2012-03-30 13:41     ` [linux-pm] " Eduardo Valentin
2012-03-30 19:08       ` Joe Perches
2012-04-01 19:13         ` Eduardo Valentin
2012-03-30 10:13   ` [PATCH 18/76] thermal: add support for thermal sensor present on SPEAr13xx machines Len Brown
2012-03-30 10:13   ` [PATCH 19/76] thermal/spear_thermal: replace readl/writel with lighter _relaxed variants Len Brown
2012-03-30 10:13   ` [PATCH 20/76] thermal: spear13xx: checking for NULL instead of IS_ERR() Len Brown
2012-03-30 10:13   ` [PATCH 21/76] thermal: Fix for setting the thermal zone mode to enable/disable Len Brown
2012-03-30 10:13   ` [PATCH 22/76] ARM: davinci: Fix for cpuidle consolidation changes Len Brown
2012-03-30 10:13   ` [PATCH 23/76] ACPICA: Update _REV return value to 5 Len Brown
2012-03-30 10:13   ` [PATCH 24/76] ACPICA: ACPI 5: Support for new FADT SleepStatus, SleepControl registers Len Brown
2012-03-30 10:13   ` [PATCH 25/76] ACPICA: Move ACPI timer prototypes to public acpixf file Len Brown
2012-03-30 10:13   ` [PATCH 26/76] ACPICA: Support for custom ACPICA build for ACPI 5 reduced hardware Len Brown
2012-03-30 10:13   ` [PATCH 27/76] ACPICA: Expand OSL memory read/write interfaces to 64 bits Len Brown
2012-03-30 10:13   ` [PATCH 28/76] ACPICA: ACPI 5: Update debug output for new notify values Len Brown
2012-03-30 10:13   ` [PATCH 29/76] ACPICA: Add acpi_os_physical_table_override interface Len Brown
2012-03-30 10:13   ` [PATCH 30/76] ACPICA: Distill multiple sleep method functions to a single function Len Brown
2012-03-30 10:13   ` [PATCH 31/76] ACPICA: Split sleep/wake functions into two files Len Brown
2012-03-30 10:13   ` [PATCH 32/76] ACPICA: Add table-driven dispatch for sleep/wake functions Len Brown
2012-03-30 10:13   ` [PATCH 33/76] ACPICA: Update to version 20120215 Len Brown
2012-03-30 10:13   ` [PATCH 34/76] ACPICA: Clarify METHOD_NAME* defines for full-pathname cases Len Brown
2012-03-30 10:13   ` [PATCH 35/76] ACPICA: Change exception code for invalid pathname in acpi_evaluate_object Len Brown
2012-03-30 10:13   ` [PATCH 36/76] ACPICA: Debugger: Add missing object info to namespace dump Len Brown
2012-03-30 10:13   ` [PATCH 37/76] ACPICA: Sleep/Wake interfaces: optionally execute _GTS and _BFS Len Brown
2012-03-30 10:13   ` [PATCH 38/76] ACPI: Move module parameter gts and bfs to sleep.c Len Brown
2012-03-30 10:13   ` [PATCH 39/76] tools turbostat: add summary option Len Brown
2012-03-30 10:13   ` [PATCH 40/76] tools turbostat: reduce measurement overhead due to IPIs Len Brown
2012-03-30 10:13   ` Len Brown [this message]
2012-03-30 10:13   ` [PATCH 42/76] ACPI: ec: Do request_region outside WARN() Len Brown
2012-03-30 10:13   ` [PATCH 43/76] ACPI: Make ACPI interrupt threaded Len Brown
2012-03-30 10:13   ` [PATCH 44/76] ACPICA: Object repair code: Support to add Package wrappers Len Brown
2012-03-30 10:13   ` [PATCH 45/76] ACPICA: Update to version 20120320 Len Brown
2012-03-30 10:13   ` [PATCH 46/76] ACPI: Introduce ACPI D3_COLD state support Len Brown
2012-04-01  6:53     ` [linux-pm] " Rafael J. Wysocki
2012-03-30 10:13   ` [PATCH 47/76] ACPI: Add interface to register/unregister device to/from power resources Len Brown
2012-03-30 10:13   ` [PATCH 48/76] cpuidle: add a sysfs entry to disable specific C state for debug purpose Len Brown
2012-03-30 10:13   ` [PATCH 49/76] cpuidle: use the driver's state_count as default Len Brown
2012-03-30 10:13   ` [PATCH 50/76] cpuidle: remove useless array definition in cpuidle_structure Len Brown
2012-03-30 10:13   ` [PATCH 51/76] cpuidle: remove unused 'governor_data' field Len Brown
2012-03-30 10:13   ` [PATCH 52/76] ACPI, PCI: Move acpi_dev_run_wake() to ACPI core Len Brown
2012-03-30 10:13   ` [PATCH 53/76] ACPI: Evaluate thermal trip points before reading temperature Len Brown
2012-03-30 10:13   ` [PATCH 54/76] ACPI: Ensure thermal limits match CPU frequencies Len Brown
2012-03-30 10:13   ` [PATCH 55/76] ACPI / PM: print physical addresses consistently with other parts of kernel Len Brown
2012-03-30 10:13   ` [PATCH 56/76] ACPI: Add CPU hotplug support for processor device objects Len Brown
2012-03-30 10:14   ` [PATCH 57/76] ACPI / Video: blacklist some samsung laptops Len Brown
2012-03-30 12:07     ` Corentin Chary
2012-03-30 12:16       ` Len Brown
2012-03-30 10:14   ` [PATCH 58/76] idle, x86: Allow off-lined CPU to enter deeper C states Len Brown
2012-04-02 16:13     ` Tony Luck
2012-04-02 17:25       ` Tony Luck
2012-04-02 17:45         ` Konrad Rzeszutek Wilk
2012-04-02 17:56         ` Boris Ostrovsky
2012-04-02 18:02           ` Tony Luck
2012-04-02 18:10             ` Boris Ostrovsky
2012-03-30 10:14   ` [PATCH 59/76] cpuidle: power_usage should be declared signed integer Len Brown
2012-03-30 10:14   ` [PATCH 60/76] ACPI, APEI, Fix ERST header length check Len Brown
2012-03-30 10:14   ` [PATCH 61/76] ACPI, APEI, EINJ, limit the range of einj_param Len Brown
2012-03-30 10:14   ` [PATCH 62/76] ACPI, APEI, EINJ, new parameter to control trigger action Len Brown
2012-03-30 10:14   ` [PATCH 63/76] Update documentation for parameter *notrigger* in einj.txt Len Brown
2012-03-30 10:14   ` [PATCH 64/76] ACPI, APEI: Fix incorrect APEI register bit width check and usage Len Brown
2012-03-30 10:14   ` [PATCH 65/76] ACPI: processor_driver: add missing kfree Len Brown
2012-03-30 10:14   ` [PATCH 66/76] ACPI: Fix use-after-free in acpi_map_lsapic Len Brown
2012-03-30 10:14   ` [PATCH 67/76] PNPACPI: Fix device ref leaking in acpi_pnp_match Len Brown
2012-03-30 10:14   ` [PATCH 68/76] ACPI: consistently use should_use_kmap() Len Brown
2012-03-30 10:14   ` [PATCH 69/76] ACPI: Fix unprotected smp_processor_id() in acpi_processor_cst_has_changed() Len Brown
2012-03-30 10:14   ` [PATCH 70/76] ACPI: Clean redundant codes in scan.c Len Brown
2012-03-30 10:14   ` [PATCH 71/76] CPER failed to handle generic error records with multiple sections Len Brown
2012-03-30 10:14   ` [PATCH 72/76] ACPI: Fix logic for removing mappings in 'acpi_unmap' Len Brown
2012-03-30 10:14   ` [PATCH 73/76] ACPI: export acpi_kobj Len Brown
2012-03-30 10:14   ` [PATCH 74/76] ACPI: Add support for exposing BGRT data Len Brown
2012-03-30 10:14   ` [PATCH 75/76] Disable MCP limit exceeded messages from Intel IPS driver Len Brown
2012-03-30 10:14   ` [PATCH 76/76] ACPI throttling: fix endian bug in acpi_read_throttling_status() Len Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15aaa34654831e98dd76f7738b6c7f5d05a66430.1333101989.git.len.brown@intel.com \
    --to=lenb@kernel.org \
    --cc=len.brown@intel.com \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).