Skip to contentSkip to conversation

factor out powertop_init() function [was "Segmentation fault occurs with --calibrate"]

PATCH In review Sergey Senozhatsky 3 participants 6 messages May 18, 2012 – May 21, 2012 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 6002814.JjpEWR9aH8@penguin

Sergey Senozhatsky

author
#1 lore raw
Quoted 7 lines · Lekensteyn
Quoted 5 lines · On Thursday 17 May 2012 19:44:58 Sergey Senozhatsky wrote:

[PATCH] factor out powertop_init() function

Factor out powertop_init() function to perform basic initialization on powertop start up.

Quoted 6 lines
Quoted 6 lines

Commit 057d6126eb6329c86b29a2e0219c0d0e49a84191 moved init after commandline options parsing. However, in some cases we need it before parsing, e.g. -- calibration.

Reported-by Lekensteyn <lekensteyn(a)gmail.com>
Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

Please Cc the list next time.

Quoted 4 lines
Tested-by Lekensteyn <lekensteyn(a)gmail.com>

Thank you for testing.

Quoted 13 lines

Now this problem is solved, I hit another problem with --calibrate. The program throws an exception because my /sys/class/power_supply/BAT0/current_now returns -ENODEV when the cable is removed from the laptop (and thus when the battery is being used). This issue is at least in kernel 3.2, but probably occurs in 3.0 too. 2.6.38 is half unaffected (only shortly after disconnecting the power cable). I saw somewhere that this device is replaced by power_current (cannot remember exact name), but I have no power... entries in the BAT0 directory.

With the 1.97 version from Debian, I do not see ACPI power estimate, but it does not crash either. The old powertop 1 (1.13?) showed the ACPI power estimate correctly.

Quoted 8 lines

The backtrace for the error:
Loaded 0 prior measurements
Cannot load from file /var/cache/powertop/saved_parameters.powertop Starting PowerTOP power estimate calibration
Calibrating idle
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_filebuf::underflow error reading the file

well, from the top of my head, the only sane solution is to catch fstream (as fstream cast) exceptions.

I'll send a patch in a minute.

	-ss
Quoted 89 lines

#10 0x000000000041b0e3 in read_sysfs (filename=..., ok=0x7fffffffc45d) at lib.cpp:190
#11 0x0000000000441f84 in sysfs_power_meter::get_sysfs_attr (this=<optimized out>, attribute=<optimized out>, value=0x7fffffffd4ac)
at measurement/sysfs.cpp:44
#12 0x00000000004420ff in sysfs_power_meter::set_rate_from_current (this=0x83faf0, voltage=11.994) at measurement/sysfs.cpp:86
#13 0x00000000004422c9 in sysfs_power_meter::measure (this=0x83faf0) at measurement/sysfs.cpp:143
#14 0x000000000043ff6d in end_power_measurement () at measurement/measurement.cpp:86
#15 0x0000000000442e3a in one_measurement (seconds=<optimized out>) at main.cpp:190
#16 0x000000000043f762 in idle_calibration () at calibrate/calibrate.cpp:423 #17 calibrate () at calibrate/calibrate.cpp:466
#18 0x00000000004066b5 in main (argc=2, argv=0x7fffffffe728) at main.cpp:379 ... (gdb) up ...
#10 0x000000000041b0e3 in read_sysfs (filename=..., ok=0x7fffffffc45d) at lib.cpp:190
190 file >> i;
(gdb) print filename.c_str()
$2 = 0x844d78 "/sys/class/power_supply/BAT0/current_now"

Below is are the contents of the BAT0 directory with connected power cable:

Quoted 21 lines

alarm:0
charge_full:4231000
charge_full_design:5600000
charge_now:4231000
current_now:0
cycle_count:0
manufacturer:NOTEBOOK
model_name:BAT
present:1
serial_number:0001
status:Full
technology:Li-ion
type:Battery
uevent:POWER_SUPPLY_NAME=BAT0
uevent:POWER_SUPPLY_STATUS=Full
uevent:POWER_SUPPLY_PRESENT=1
uevent:POWER_SUPPLY_TECHNOLOGY=Li-ion
uevent:POWER_SUPPLY_CYCLE_COUNT=0
uevent:POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
uevent:POWER_SUPPLY_VOLTAGE_NOW=12720000
uevent:POWER_SUPPLY_CURRENT_NOW=0

Quoted 8 lines

uevent:POWER_SUPPLY_CHARGE_FULL_DESIGN=5600000
uevent:POWER_SUPPLY_CHARGE_FULL=4231000
uevent:POWER_SUPPLY_CHARGE_NOW=4231000
uevent:POWER_SUPPLY_MODEL_NAME=BAT
uevent:POWER_SUPPLY_MANUFACTURER=NOTEBOOK
uevent:POWER_SUPPLY_SERIAL_NUMBER=0001
voltage_min_design:11100000
voltage_now:12720000

And here with cable disconnected:

Quoted 20 lines

alarm:0
charge_full:4231000
charge_full_design:5600000
charge_now:4205000
grep: current_now: No such device
cycle_count:0
manufacturer:NOTEBOOK
model_name:BAT
present:1
serial_number:0001
status:Discharging
technology:Li-ion
type:Battery
uevent:POWER_SUPPLY_NAME=BAT0
uevent:POWER_SUPPLY_STATUS=Discharging
uevent:POWER_SUPPLY_PRESENT=1
uevent:POWER_SUPPLY_TECHNOLOGY=Li-ion
uevent:POWER_SUPPLY_CYCLE_COUNT=0
uevent:POWER_SUPPLY_VOLTAGE_MIN_DESIGN=11100000
uevent:POWER_SUPPLY_VOLTAGE_NOW=12263000

Quoted 8 lines

uevent:POWER_SUPPLY_CHARGE_FULL_DESIGN=5600000
uevent:POWER_SUPPLY_CHARGE_FULL=4231000
uevent:POWER_SUPPLY_CHARGE_NOW=4205000
uevent:POWER_SUPPLY_MODEL_NAME=BAT
uevent:POWER_SUPPLY_MANUFACTURER=NOTEBOOK
uevent:POWER_SUPPLY_SERIAL_NUMBER=0001
voltage_min_design:11100000
voltage_now:12263000

I am trying to find out which commit caused it.

Regards,
Peter

author
#2 lore raw

PATCH 1/1 catch fstream exceptions in lib routines

In reply to a message not in the archive

[PATCH] catch fstream exceptions in lib routines

Catch possible fstream and traits_type exceptions in lib.

Reported-by Lekensteyn <lekensteyn(a)gmail.com>
Signed-off-by Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/lib.cpp |   54 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 15 deletions(-)
src/lib.cpp +39 -15
index 53638dd..bcd809f 100644
@@ -172,8 +172,13 @@ void write_sysfs(const string &filename, const string &value)
 	file.open(filename.c_str(), ios::out);
 	if (!file)
 		return;
-	file << value;
-	file.close();
+	try 
+	{
+		file << value;
+		file.close();
+	} catch (std::exception &exc) {
+		return;
+	}
 }
 
 int read_sysfs(const string &filename, bool *ok)
@@ -187,10 +192,17 @@ int read_sysfs(const string &filename, bool *ok)
 			*ok = false;
 		return 0;
 	}
-	file >> i;
+	try
+	{
+		file >> i;
+		if (ok)
+			*ok = true;
+	} catch (std::exception &exc) {
+		if (ok)
+			*ok = "false";
+		i = 0;
+	}
 	file.close();
-	if (ok)
-		*ok = true;
 	return i;
 }
 
@@ -203,11 +215,17 @@ string read_sysfs_string(const string &filename)
 	file.open(filename.c_str(), ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception &exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
 
@@ -224,11 +242,17 @@ string read_sysfs_string(const char *format, const char *param)
 	file.open(filename, ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception &exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
 
#3 lore raw

Re: [Powertop] [PATCH] catch fstream exceptions in lib routines

On Friday 18 May 2012 13:17:39 Sergey Senozhatsky wrote:
Hi,

src/lib.cpp int read_sysfs(const string &filename, bool *ok) · line 202

Quoted 13 lines
src/lib.cpp +8 -1
@@ -187,10 +192,17 @@ int read_sysfs(const string &filename, bool *ok)
 			*ok = false;
 		return 0;
 	}
-	file >> i;
+	try
+	{
+		file >> i;
+		if (ok)
+			*ok = true;
+	} catch (std::exception &exc) {
+		if (ok)
+			*ok = "false";

I have changed this to false (without quotes), I guess that is what you meant.

With that change,
Tested-by: Lekensteyn <lekensteyn(a)gmail.com>

Thanks,
Peter

author
#4 lore raw

Re: [Powertop] [PATCH] catch fstream exceptions in lib routines (v2)

[PATCH] catch fstream exceptions in lib routines (v2)

Catch possible fstream and traits_type exceptions in lib.

V2: Thanks to Peter, he pointed to silly typo I've done in V1.

Reported-and-tested-by: Lekensteyn <lekensteyn(a)gmail.com>
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky(a)gmail.com>

---

 src/lib.cpp |   54 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 15 deletions(-)
src/lib.cpp +39 -15
index 53638dd..0c2c1f1 100644
@@ -172,8 +172,13 @@ void write_sysfs(const string &filename, const string &value)
 	file.open(filename.c_str(), ios::out);
 	if (!file)
 		return;
-	file << value;
-	file.close();
+	try 
+	{
+		file << value;
+		file.close();
+	} catch (std::exception &exc) {
+		return;
+	}
 }
 
 int read_sysfs(const string &filename, bool *ok)
@@ -187,10 +192,17 @@ int read_sysfs(const string &filename, bool *ok)
 			*ok = false;
 		return 0;
 	}
-	file >> i;
+	try
+	{
+		file >> i;
+		if (ok)
+			*ok = true;
+	} catch (std::exception &exc) {
+		if (ok)
+			*ok = false;
+		i = 0;
+	}
 	file.close();
-	if (ok)
-		*ok = true;
 	return i;
 }
 
@@ -203,11 +215,17 @@ string read_sysfs_string(const string &filename)
 	file.open(filename.c_str(), ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception &exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
 
@@ -224,11 +242,17 @@ string read_sysfs_string(const char *format, const char *param)
 	file.open(filename, ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception &exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
 
#5 lore raw

Re: [Powertop] [PATCH] catch fstream exceptions in lib routines (v2)

patch accepted by hand merge from v1.
Thanks
-Chris

Quoted 102 lines · Sergey Senozhatsky

[PATCH] catch fstream exceptions in lib routines (v2)

Catch possible fstream and traits_type exceptions in lib.

V2: Thanks to Peter, he pointed to silly typo I've done in V1.

Reported-and-tested-by: Lekensteyn<lekensteyn(a)gmail.com>
Signed-off-by: Sergey Senozhatsky<sergey.senozhatsky(a)gmail.com>

---

  src/lib.cpp |   54 +++++++++++++++++++++++++++++++++++++++---------------
  1 file changed, 39 insertions(+), 15 deletions(-)
src/lib.cpp +39 -15
index 53638dd..0c2c1f1 100644
@@ -172,8 +172,13 @@ void write_sysfs(const string&filename, const string&value)
  	file.open(filename.c_str(), ios::out);
  	if (!file)
  		return;
-	file<<  value;
-	file.close();
+	try
+	{
+		file<<  value;
+		file.close();
+	} catch (std::exception&exc) {
+		return;
+	}
  }
 
  int read_sysfs(const string&filename, bool *ok)
@@ -187,10 +192,17 @@ int read_sysfs(const string&filename, bool *ok)
  			*ok = false;
  		return 0;
  	}
-	file>>  i;
+	try
+	{
+		file>>  i;
+		if (ok)
+			*ok = true;
+	} catch (std::exception&exc) {
+		if (ok)
+			*ok = false;
+		i = 0;
+	}
  	file.close();
-	if (ok)
-		*ok = true;
  	return i;
  }
 
@@ -203,11 +215,17 @@ string read_sysfs_string(const string&filename)
  	file.open(filename.c_str(), ios::in);
  	if (!file)
  		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception&exc) {
+		file.close();
+		return "";
+	}
  	return content;
  }
 
@@ -224,11 +242,17 @@ string read_sysfs_string(const char *format, const char *param)
  	file.open(filename, ios::in);
  	if (!file)
  		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception&exc) {
+		file.close();
+		return "";
+	}
  	return content;
  }
 
author
#6 lore raw

Re: [Powertop] [PATCH] catch fstream exceptions in lib routines (v2)

Quoted 5 lines · Chris Ferron

patch accepted by hand merge from v1.
Thanks
-Chris

Thank you.

	-ss
Quoted 105 lines · Sergey Senozhatsky
Quoted 102 lines

[PATCH] catch fstream exceptions in lib routines (v2)

Catch possible fstream and traits_type exceptions in lib.

V2: Thanks to Peter, he pointed to silly typo I've done in V1.

Reported-and-tested-by: Lekensteyn<lekensteyn(a)gmail.com>
Signed-off-by: Sergey Senozhatsky<sergey.senozhatsky(a)gmail.com>

---

 src/lib.cpp |   54 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 15 deletions(-)
src/lib.cpp +39 -15
index 53638dd..0c2c1f1 100644
@@ -172,8 +172,13 @@ void write_sysfs(const string&filename, const string&value)
 	file.open(filename.c_str(), ios::out);
 	if (!file)
 		return;
-	file<<  value;
-	file.close();
+	try
+	{
+		file<<  value;
+		file.close();
+	} catch (std::exception&exc) {
+		return;
+	}
 }
 
 int read_sysfs(const string&filename, bool *ok)
@@ -187,10 +192,17 @@ int read_sysfs(const string&filename, bool *ok)
 			*ok = false;
 		return 0;
 	}
-	file>>  i;
+	try
+	{
+		file>>  i;
+		if (ok)
+			*ok = true;
+	} catch (std::exception&exc) {
+		if (ok)
+			*ok = false;
+		i = 0;
+	}
 	file.close();
-	if (ok)
-		*ok = true;
 	return i;
 }
 
@@ -203,11 +215,17 @@ string read_sysfs_string(const string&filename)
 	file.open(filename.c_str(), ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception&exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }
 
@@ -224,11 +242,17 @@ string read_sysfs_string(const char *format, const char *param)
 	file.open(filename, ios::in);
 	if (!file)
 		return "";
-	file.getline(content, 4096);
-	file.close();
-	c = strchr(content, '\n');
-	if (c)
-		*c = 0;
+	try
+	{
+		file.getline(content, 4096);
+		file.close();
+		c = strchr(content, '\n');
+		if (c)
+			*c = 0;
+	} catch (std::exception&exc) {
+		file.close();
+		return "";
+	}
 	return content;
 }