factor out powertop_init() function [was "Segmentation fault occurs with --calibrate"]
A message that is not in the archive (sent off-list or not yet received): 6002814.JjpEWR9aH8@penguin
Sergey Senozhatsky
Quoted 7 lines · Lekensteyn
Quoted 5 lines · On Thursday 17 May 2012 19:44:58 Sergey Senozhatsky wrote:
[PATCH] factor out
powertop_init()functionFactor out
powertop_init()function to perform basic initialization on powertop start up.
Quoted 6 lines
Quoted 6 lines
Commit
057d6126eb6329c86b29a2e0219c0d0e49a84191moved 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_nowreturns-ENODEVwhen 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 bypower_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.powertopStarting PowerTOP power estimate calibration
Calibrating idle
terminate called after throwing an instance of 'std::ios_base::failure'
what():basic_filebuf::underflowerror 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
0x000000000041b0e3inread_sysfs(filename=..., ok=0x7fffffffc45d) at lib.cpp:190
#110x0000000000441f84insysfs_power_meter::get_sysfs_attr(this=<optimized out>, attribute=<optimized out>, value=0x7fffffffd4ac)
at measurement/sysfs.cpp:44
#120x00000000004420ffinsysfs_power_meter::set_rate_from_current(this=0x83faf0, voltage=11.994) at measurement/sysfs.cpp:86
#130x00000000004422c9insysfs_power_meter::measure(this=0x83faf0) at measurement/sysfs.cpp:143
#140x000000000043ff6dinend_power_measurement() at measurement/measurement.cpp:86
#150x0000000000442e3ainone_measurement(seconds=<optimized out>) at main.cpp:190
#160x000000000043f762inidle_calibration() at calibrate/calibrate.cpp:423 #17 calibrate () at calibrate/calibrate.cpp:466
#180x00000000004066b5in main (argc=2, argv=0x7fffffffe728) at main.cpp:379 ... (gdb) up ...
#100x000000000041b0e3inread_sysfs(filename=..., ok=0x7fffffffc45d) at lib.cpp:190
190 file >> i;
(gdb) printfilename.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=0Quoted 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:12720000And 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=12263000Quoted 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:12263000I am trying to find out which commit caused it.
Regards,
Peter
Sergey Senozhatsky
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.
---
src/lib.cpp | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-)
src/lib.cpp +39 -15
@@ -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; }
Lekensteyn
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
Sergey Senozhatsky
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
@@ -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; }
Chris Ferron
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_typeexceptions 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@@ -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; }
Sergey Senozhatsky
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_typeexceptions 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@@ -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; }