Skip to contentSkip to conversation

Calculate the number of joules consumed over time

PATCH New Daniel Black 2 participants 3 messages Jul 21, 2016

lore
Share & tools
lore

Daniel Black

author
#0 lore raw

PATCH 1/1 Calculate the number of joules consumed over time

The *joules_consumed functions where returning power so renamed to reflect this.

Calculate the joules used at intervals by multipling the instantaneous power by the time interval since the last measurement.

Signed-off-by Daniel Black <daniel.black(a)au1.ibm.com>
---
 src/calibrate/calibrate.cpp      | 26 +++++++++----------
 src/calibrate/calibrate.h        |  2 +-
 src/devices/device.cpp           | 11 ++++++--
 src/main.cpp                     | 54 ++++++++++++++++++++++++++++++++--------
 src/measurement/acpi.cpp         |  2 +-
 src/measurement/acpi.h           |  2 +-
 src/measurement/extech.cpp       |  2 +-
 src/measurement/extech.h         |  2 +-
 src/measurement/measurement.cpp  | 31 ++++++++++++++++++++---
 src/measurement/measurement.h    |  6 +++--
 src/measurement/opal-sensors.cpp |  2 +-
 src/measurement/opal-sensors.h   |  2 +-
 src/measurement/sysfs.h          |  2 +-
 src/parameters/parameters.cpp    |  2 +-
 src/parameters/parameters.h      |  1 +
 src/process/do_process.cpp       |  8 ++++--
 16 files changed, 113 insertions(+), 42 deletions(-)
src/calibrate/calibrate.cpp +13 -13
index 60ab892..745d544 100644
@@ -261,7 +261,7 @@ static void cpu_calibration(int threads)
 	for (i = 0; i < threads; i++)
 		pthread_create(&thr, NULL, burn_cpu, NULL);
 
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	stop_measurement = 1;
 	sleep(1);
 }
@@ -276,7 +276,7 @@ static void wakeup_calibration(unsigned long interval)
 
 	pthread_create(&thr, NULL, burn_cpu_wakeups, (void *)interval);
 
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	stop_measurement = 1;
 	sleep(1);
 }
@@ -292,7 +292,7 @@ static void usb_calibration(void)
 		printf(_(".... device %s \n"), usb_devices[i].c_str());
 		suspend_all_usb_devices();
 		write_sysfs(usb_devices[i], "on\n");
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		suspend_all_usb_devices();
 		sleep(3);
 	}
@@ -309,7 +309,7 @@ static void rfkill_calibration(void)
 		printf(_(".... device %s \n"), rfkill_devices[i].c_str());
 		rfkill_all_radios();
 		write_sysfs(rfkill_devices[i], "0\n");
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		rfkill_all_radios();
 		sleep(3);
 	}
@@ -317,7 +317,7 @@ static void rfkill_calibration(void)
 		printf(_(".... device %s \n"), rfkill_devices[i].c_str());
 		unrfkill_all_radios();
 		write_sysfs(rfkill_devices[i], "1\n");
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		unrfkill_all_radios();
 		sleep(3);
 	}
@@ -333,29 +333,29 @@ static void backlight_calibration(void)
 		char str[4096];
 		printf(_(".... device %s \n"), backlight_devices[i].c_str());
 		lower_backlight();
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		sprintf(str, "%i\n", blmax / 4);
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 
 		sprintf(str, "%i\n", blmax / 2);
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 
 		sprintf(str, "%i\n", 3 * blmax / 4 );
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 
 		sprintf(str, "%i\n", blmax);
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		lower_backlight();
 		sleep(1);
 	}
 	printf(_("Calibrating idle\n"));
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force off"))
 		printf("System is not available\n");
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force on"))
 		printf("System is not available\n");
 }
@@ -365,7 +365,7 @@ static void idle_calibration(void)
 	printf(_("Calibrating idle\n"));
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force off"))
 		printf("System is not available\n");
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force on"))
 		printf("System is not available\n");
 }
@@ -382,7 +382,7 @@ static void disk_calibration(void)
 	stop_measurement = 0;
 	pthread_create(&thr, NULL, burn_disk, NULL);
 
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	stop_measurement = 1;
 	sleep(1);
 
src/calibrate/calibrate.h +1 -1
index c279ae2..de71938 100644
@@ -25,7 +25,7 @@
 #ifndef __INCLUDE_GUARD_CALIBRATE_H
 #define __INCLUDE_GUARD_CALIBRATE_H
 
-extern void one_measurement(int seconds, char *workload);
+extern void one_measurement(int seconds, int sample_interval, char *workload);
 extern void calibrate(void);
 
 
src/devices/device.cpp +9 -2
index 1757a55..2245b65 100644
@@ -166,11 +166,13 @@ void report_devices(void)
 
 
 
-	pw = global_joules_consumed();
+	pw = global_power();
 	if (pw > 0.0001) {
 		char buf[32];
 		wprintw(win, _("The battery reports a discharge rate of %sW\n"),
 				fmt_prefix(pw, buf));
+		wprintw(win, _("The power consumed was %sJ\n"),
+				fmt_prefix(global_joules(), buf));
 	}
 
 	if (show_power) {
@@ -248,13 +250,18 @@ void show_report_devices(void)
 	/* Device Summary */
 	int summary_size=2;
 	string *summary = new string[summary_size];
-	pw = global_joules_consumed();
+	pw = global_power();
 	char buf[32];
 	if (pw > 0.0001) {
 		summary[0]= __("The battery reports a discharge rate of: ");
 		summary[1]=string(fmt_prefix(pw, buf));
 		summary[1].append(" W");
 		report.add_summary_list(summary, summary_size);
+
+		summary[0]= __("The power consumed was : ");
+		summary[1]=string(fmt_prefix(global_joules(), buf));
+		summary[1].append(" J");
+		report.add_summary_list(summary, summary_size);
 	}
 
 	if (show_power) {
src/main.cpp +44 -10

135 lines not on this page · raw message

src/measurement/acpi.cpp +1 -1
index 2c9815d..ede2782 100644
@@ -207,7 +207,7 @@ void acpi_power_meter::start_measurement(void)
 }
 
 
-double acpi_power_meter::joules_consumed(void)
+double acpi_power_meter::power(void)
 {
 	return rate;
 }
src/measurement/acpi.h +1 -1
index 315fddf..25bbe04 100644
@@ -39,7 +39,7 @@ public:
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
 
-	virtual double joules_consumed(void);
+	virtual double power(void);
 	virtual double dev_capacity(void) { return capacity; };
 };
 
src/measurement/extech.cpp +1 -1
index e014a5d..0849907 100644
@@ -347,7 +347,7 @@ void extech_power_meter::start_measurement(void)
 }
 
 
-double extech_power_meter::joules_consumed(void)
+double extech_power_meter::power(void)
 {
 	return rate;
 }
src/measurement/extech.h +1 -1
index 5f4c338..b7b330a 100644
@@ -44,7 +44,7 @@ public:
 	virtual void end_measurement(void);
 	virtual void sample(void);
 	
-	virtual double joules_consumed(void);
+	virtual double power(void);
 	virtual double dev_capacity(void) { return 0.0; };
 };
 
src/measurement/measurement.cpp +27 -4
index 1c8e4da..caee24e 100644
@@ -36,6 +36,7 @@
 #include <stdio.h>
 #include <fstream>
 #include <unistd.h>
+#include <time.h>
 
 double min_power = 50000.0;
 
@@ -49,18 +50,22 @@ void power_meter::end_measurement(void)
 }
 
 
-double power_meter::joules_consumed(void)
+double power_meter::power(void)
 {
 	return 0.0;
 }
 
 vector<class power_meter *> power_meters;
 
+static struct timespec tlast;
+
 void start_power_measurement(void)
 {
 	unsigned int i;
+	clock_gettime(CLOCK_REALTIME, &tlast);
 	for (i = 0; i < power_meters.size(); i++)
 		power_meters[i]->start_measurement();
+	all_results.joules = 0.0;
 }
 void end_power_measurement(void)
 {
@@ -69,7 +74,7 @@ void end_power_measurement(void)
 		power_meters[i]->end_measurement();
 }
 
-double global_joules_consumed(void)
+double global_power(void)
 {
 	bool global_discharging = false;
 	double total = 0.0;
@@ -77,8 +82,9 @@ double global_joules_consumed(void)
 
 	for (i = 0; i < power_meters.size(); i++) {
 		global_discharging |= power_meters[i]->is_discharging();
-		total += power_meters[i]->joules_consumed();
+		total += power_meters[i]->power();
 	}
+
 	/* report global time left if at least one battery is discharging */
 	if (!global_discharging)
 		return 0.0;
@@ -89,6 +95,23 @@ double global_joules_consumed(void)
 	return total;
 }
 
+void global_sample_power(void)
+{
+	struct timespec tnow;
+
+	clock_gettime(CLOCK_REALTIME, &tnow);
+	/* power * time = joules */
+	all_results.joules += global_power() * \
+			 ( ((double)tnow.tv_sec + 1.0e-9*tnow.tv_nsec) - \
+			   ((double)tlast.tv_sec + 1.0e-9*tlast.tv_nsec));
+	tlast = tnow;
+}
+
+double global_joules(void)
+{
+	return all_results.joules;
+}
+
 double global_time_left(void)
 {
 	bool global_discharging = false;
@@ -98,7 +121,7 @@ double global_time_left(void)
 	for (i = 0; i < power_meters.size(); i++) {
 		global_discharging |= power_meters[i]->is_discharging();
 		total_capacity += power_meters[i]->dev_capacity();
-		total_rate += power_meters[i]->joules_consumed();
+		total_rate += power_meters[i]->power();
 	}
 	/* report global time left if at least one battery is discharging */
 	if (!global_discharging)
src/measurement/measurement.h +4 -2
index 83ffc34..df0ceca 100644
@@ -36,7 +36,7 @@ public:
 
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
-	virtual double joules_consumed(void);
+	virtual double power(void);
 
 	virtual double dev_capacity(void)
 	{
@@ -58,7 +58,9 @@ extern vector<class power_meter *> power_meters;
 
 extern void start_power_measurement(void);
 extern void end_power_measurement(void);
-extern double global_joules_consumed(void);
+extern double global_power(void);
+extern void global_sample_power(void);
+extern double global_joules(void);
 extern double global_time_left(void);
 
 extern void detect_power_meters(void);
src/measurement/opal-sensors.cpp +1 -1
index 7316d1f..a70ec56 100644
@@ -34,7 +34,7 @@ opal_sensors_power_meter::opal_sensors_power_meter(const char *power_supply_name
 	strncpy(name, power_supply_name, sizeof(name));
 }
 
-double opal_sensors_power_meter::joules_consumed(void)
+double opal_sensors_power_meter::power(void)
 {
 	bool ok;
 	int value;
src/measurement/opal-sensors.h +1 -1
index 8347733..7962091 100644
@@ -35,7 +35,7 @@ public:
 	virtual void start_measurement(void) {};
 	virtual void end_measurement(void) {};
 
-	virtual double joules_consumed(void);
+	virtual double power(void);
 	virtual double dev_capacity(void) { return 0.0; }
 };
 
src/measurement/sysfs.h +1 -1
index b7efa3d..5b440f5 100644
@@ -48,7 +48,7 @@ public:
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
 
-	virtual double joules_consumed(void) { return rate; }
+	virtual double power(void) { return rate; }
 	virtual double dev_capacity(void) { return capacity; }
 };
 
src/parameters/parameters.cpp +1 -1
index 3bd5c05..38e1752 100644
@@ -327,7 +327,7 @@ void store_results(double duration)
 {
 	if (duration < 5)
 		return;
-	global_joules_consumed();
+	global_power();
 	if (all_results.power > 0.01) {
 		unsigned int overflow_index;
 		overflow_index = 50 + (rand() % MAX_KEEP);
src/parameters/parameters.h +1 -0
index 72f8d32..1781a0e 100644
@@ -66,6 +66,7 @@ extern void set_parameter_value(const char *name, double value, struct parameter
 
 struct result_bundle
 {
+	double joules;
 	double power;
 	vector <double> utilization; /* device name, device utilization %age */
 };
src/process/do_process.cpp +6 -2
index 391cb7f..812572c 100644
@@ -787,6 +787,7 @@ void process_update_display(void)
 	unsigned int i;
 	WINDOW *win;
 	double pw;
+	double joules;
 	int tl;
 	int tlt;
 	int tlr;
@@ -816,10 +817,11 @@ void process_update_display(void)
 	}
 
 	wprintw(win, _("Estimated power: %5.1f    Measured power: %5.1f    Sum: %5.1f\n\n"),
-				all_parameters.guessed_power, global_joules_consumed(), sum);
+				all_parameters.guessed_power, global_power(), sum);
 #endif
 
-	pw = global_joules_consumed();
+	pw = global_power();
+	joules = global_joules();
 	tl = global_time_left() / 60;
 	tlt = (tl /60);
 	tlr = tl % 60;
@@ -828,6 +830,8 @@ void process_update_display(void)
 		char buf[32];
 		wprintw(win, _("The battery reports a discharge rate of %sW\n"),
 				fmt_prefix(pw, buf));
+		wprintw(win, _("The power consumed was %sJ\n"),
+				fmt_prefix(joules, buf));
 		need_linebreak = 1;
 	}
 	if (tl > 0 && pw > 0.0001) {
Signature · 1 line
2.7.4
#1 lore raw

v2 cover Rebase of "Calculate the number of joules consumed over time"

From: Joe Konno <joe.konno(a)intel.com>

Hey gang,

An attempt to apply v1 of this patch resulted in a git-am rejection in main.cpp, in the main() function. This was due to a context change introduced in main.cpp line 514 (post git am --reject). The following patch introduced the context change:

- 06696d4e5f2e ("enable --auto-tune without debugfs")

As a courtesy (and as thanks for the submission), I am re-submitting with the new context incorporated.

Cheers

Daniel Black (1):
Calculate the number of joules consumed over time

 src/calibrate/calibrate.cpp      | 26 +++++++++----------
 src/calibrate/calibrate.h        |  2 +-
 src/devices/device.cpp           | 11 ++++++--
 src/main.cpp                     | 54 ++++++++++++++++++++++++++++++++--------
 src/measurement/acpi.cpp         |  2 +-
 src/measurement/acpi.h           |  2 +-
 src/measurement/extech.cpp       |  2 +-
 src/measurement/extech.h         |  2 +-
 src/measurement/measurement.cpp  | 31 ++++++++++++++++++++---
 src/measurement/measurement.h    |  6 +++--
 src/measurement/opal-sensors.cpp |  2 +-
 src/measurement/opal-sensors.h   |  2 +-
 src/measurement/sysfs.h          |  2 +-
 src/parameters/parameters.cpp    |  2 +-
 src/parameters/parameters.h      |  1 +
 src/process/do_process.cpp       |  8 ++++--
 16 files changed, 113 insertions(+), 42 deletions(-)
Signature · 1 line
2.9.0
#2 lore raw

PATCH v2 1/1 Calculate the number of joules consumed over time

From: Daniel Black <daniel.black(a)au1.ibm.com>

The *joules_consumed functions where returning power so renamed to reflect this.

Calculate the joules used at intervals by multipling the instantaneous power by the time interval since the last measurement.

Signed-off-by Daniel Black <daniel.black(a)au1.ibm.com>

v2: (Joe) Resolved a trivial hunk rejection in main.cpp +514 with

    6696d4e5f2e ("enable --auto-tune without debugfs"); Acked-by added
Acked-by Joe Konno <joe.konno(a)intel.com>
---
 src/calibrate/calibrate.cpp      | 26 +++++++++----------
 src/calibrate/calibrate.h        |  2 +-
 src/devices/device.cpp           | 11 ++++++--
 src/main.cpp                     | 54 ++++++++++++++++++++++++++++++++--------
 src/measurement/acpi.cpp         |  2 +-
 src/measurement/acpi.h           |  2 +-
 src/measurement/extech.cpp       |  2 +-
 src/measurement/extech.h         |  2 +-
 src/measurement/measurement.cpp  | 31 ++++++++++++++++++++---
 src/measurement/measurement.h    |  6 +++--
 src/measurement/opal-sensors.cpp |  2 +-
 src/measurement/opal-sensors.h   |  2 +-
 src/measurement/sysfs.h          |  2 +-
 src/parameters/parameters.cpp    |  2 +-
 src/parameters/parameters.h      |  1 +
 src/process/do_process.cpp       |  8 ++++--
 16 files changed, 113 insertions(+), 42 deletions(-)
src/calibrate/calibrate.cpp +13 -13
index 60ab89233e34..745d544b6b65 100644
@@ -261,7 +261,7 @@ static void cpu_calibration(int threads)
 	for (i = 0; i < threads; i++)
 		pthread_create(&thr, NULL, burn_cpu, NULL);
 
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	stop_measurement = 1;
 	sleep(1);
 }
@@ -276,7 +276,7 @@ static void wakeup_calibration(unsigned long interval)
 
 	pthread_create(&thr, NULL, burn_cpu_wakeups, (void *)interval);
 
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	stop_measurement = 1;
 	sleep(1);
 }
@@ -292,7 +292,7 @@ static void usb_calibration(void)
 		printf(_(".... device %s \n"), usb_devices[i].c_str());
 		suspend_all_usb_devices();
 		write_sysfs(usb_devices[i], "on\n");
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		suspend_all_usb_devices();
 		sleep(3);
 	}
@@ -309,7 +309,7 @@ static void rfkill_calibration(void)
 		printf(_(".... device %s \n"), rfkill_devices[i].c_str());
 		rfkill_all_radios();
 		write_sysfs(rfkill_devices[i], "0\n");
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		rfkill_all_radios();
 		sleep(3);
 	}
@@ -317,7 +317,7 @@ static void rfkill_calibration(void)
 		printf(_(".... device %s \n"), rfkill_devices[i].c_str());
 		unrfkill_all_radios();
 		write_sysfs(rfkill_devices[i], "1\n");
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		unrfkill_all_radios();
 		sleep(3);
 	}
@@ -333,29 +333,29 @@ static void backlight_calibration(void)
 		char str[4096];
 		printf(_(".... device %s \n"), backlight_devices[i].c_str());
 		lower_backlight();
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		sprintf(str, "%i\n", blmax / 4);
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 
 		sprintf(str, "%i\n", blmax / 2);
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 
 		sprintf(str, "%i\n", 3 * blmax / 4 );
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 
 		sprintf(str, "%i\n", blmax);
 		write_sysfs(backlight_devices[i], str);
-		one_measurement(15, NULL);
+		one_measurement(15, 15, NULL);
 		lower_backlight();
 		sleep(1);
 	}
 	printf(_("Calibrating idle\n"));
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force off"))
 		printf("System is not available\n");
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force on"))
 		printf("System is not available\n");
 }
@@ -365,7 +365,7 @@ static void idle_calibration(void)
 	printf(_("Calibrating idle\n"));
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force off"))
 		printf("System is not available\n");
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	if(!system("DISPLAY=:0 /usr/bin/xset dpms force on"))
 		printf("System is not available\n");
 }
@@ -382,7 +382,7 @@ static void disk_calibration(void)
 	stop_measurement = 0;
 	pthread_create(&thr, NULL, burn_disk, NULL);
 
-	one_measurement(15, NULL);
+	one_measurement(15, 15, NULL);
 	stop_measurement = 1;
 	sleep(1);
 
src/calibrate/calibrate.h +1 -1
index c279ae2783f9..de71938b89b0 100644
@@ -25,7 +25,7 @@
 #ifndef __INCLUDE_GUARD_CALIBRATE_H
 #define __INCLUDE_GUARD_CALIBRATE_H
 
-extern void one_measurement(int seconds, char *workload);
+extern void one_measurement(int seconds, int sample_interval, char *workload);
 extern void calibrate(void);
 
 
src/devices/device.cpp +9 -2
index 1757a55f914e..2245b65983d4 100644
@@ -166,11 +166,13 @@ void report_devices(void)
 
 
 
-	pw = global_joules_consumed();
+	pw = global_power();
 	if (pw > 0.0001) {
 		char buf[32];
 		wprintw(win, _("The battery reports a discharge rate of %sW\n"),
 				fmt_prefix(pw, buf));
+		wprintw(win, _("The power consumed was %sJ\n"),
+				fmt_prefix(global_joules(), buf));
 	}
 
 	if (show_power) {
@@ -248,13 +250,18 @@ void show_report_devices(void)
 	/* Device Summary */
 	int summary_size=2;
 	string *summary = new string[summary_size];
-	pw = global_joules_consumed();
+	pw = global_power();
 	char buf[32];
 	if (pw > 0.0001) {
 		summary[0]= __("The battery reports a discharge rate of: ");
 		summary[1]=string(fmt_prefix(pw, buf));
 		summary[1].append(" W");
 		report.add_summary_list(summary, summary_size);
+
+		summary[0]= __("The power consumed was : ");
+		summary[1]=string(fmt_prefix(global_joules(), buf));
+		summary[1].append(" J");
+		report.add_summary_list(summary, summary_size);
 	}
 
 	if (show_power) {
src/main.cpp +44 -10

135 lines not on this page · raw message

src/measurement/acpi.cpp +1 -1
index 2c9815d11955..ede278221f2d 100644
@@ -207,7 +207,7 @@ void acpi_power_meter::start_measurement(void)
 }
 
 
-double acpi_power_meter::joules_consumed(void)
+double acpi_power_meter::power(void)
 {
 	return rate;
 }
src/measurement/acpi.h +1 -1
index 315fddfc77a1..25bbe04133ad 100644
@@ -39,7 +39,7 @@ public:
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
 
-	virtual double joules_consumed(void);
+	virtual double power(void);
 	virtual double dev_capacity(void) { return capacity; };
 };
 
src/measurement/extech.cpp +1 -1
index e014a5d2adc9..0849907609a8 100644
@@ -347,7 +347,7 @@ void extech_power_meter::start_measurement(void)
 }
 
 
-double extech_power_meter::joules_consumed(void)
+double extech_power_meter::power(void)
 {
 	return rate;
 }
src/measurement/extech.h +1 -1
index 5f4c3388120c..b7b330a23f75 100644
@@ -44,7 +44,7 @@ public:
 	virtual void end_measurement(void);
 	virtual void sample(void);
 	
-	virtual double joules_consumed(void);
+	virtual double power(void);
 	virtual double dev_capacity(void) { return 0.0; };
 };
 
src/measurement/measurement.cpp +27 -4
index 1c8e4da12dab..caee24e5d5e8 100644
@@ -36,6 +36,7 @@
 #include <stdio.h>
 #include <fstream>
 #include <unistd.h>
+#include <time.h>
 
 double min_power = 50000.0;
 
@@ -49,18 +50,22 @@ void power_meter::end_measurement(void)
 }
 
 
-double power_meter::joules_consumed(void)
+double power_meter::power(void)
 {
 	return 0.0;
 }
 
 vector<class power_meter *> power_meters;
 
+static struct timespec tlast;
+
 void start_power_measurement(void)
 {
 	unsigned int i;
+	clock_gettime(CLOCK_REALTIME, &tlast);
 	for (i = 0; i < power_meters.size(); i++)
 		power_meters[i]->start_measurement();
+	all_results.joules = 0.0;
 }
 void end_power_measurement(void)
 {
@@ -69,7 +74,7 @@ void end_power_measurement(void)
 		power_meters[i]->end_measurement();
 }
 
-double global_joules_consumed(void)
+double global_power(void)
 {
 	bool global_discharging = false;
 	double total = 0.0;
@@ -77,8 +82,9 @@ double global_joules_consumed(void)
 
 	for (i = 0; i < power_meters.size(); i++) {
 		global_discharging |= power_meters[i]->is_discharging();
-		total += power_meters[i]->joules_consumed();
+		total += power_meters[i]->power();
 	}
+
 	/* report global time left if at least one battery is discharging */
 	if (!global_discharging)
 		return 0.0;
@@ -89,6 +95,23 @@ double global_joules_consumed(void)
 	return total;
 }
 
+void global_sample_power(void)
+{
+	struct timespec tnow;
+
+	clock_gettime(CLOCK_REALTIME, &tnow);
+	/* power * time = joules */
+	all_results.joules += global_power() * \
+			 ( ((double)tnow.tv_sec + 1.0e-9*tnow.tv_nsec) - \
+			   ((double)tlast.tv_sec + 1.0e-9*tlast.tv_nsec));
+	tlast = tnow;
+}
+
+double global_joules(void)
+{
+	return all_results.joules;
+}
+
 double global_time_left(void)
 {
 	bool global_discharging = false;
@@ -98,7 +121,7 @@ double global_time_left(void)
 	for (i = 0; i < power_meters.size(); i++) {
 		global_discharging |= power_meters[i]->is_discharging();
 		total_capacity += power_meters[i]->dev_capacity();
-		total_rate += power_meters[i]->joules_consumed();
+		total_rate += power_meters[i]->power();
 	}
 	/* report global time left if at least one battery is discharging */
 	if (!global_discharging)
src/measurement/measurement.h +4 -2
index 83ffc34f0dee..df0ceca79eb4 100644
@@ -36,7 +36,7 @@ public:
 
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
-	virtual double joules_consumed(void);
+	virtual double power(void);
 
 	virtual double dev_capacity(void)
 	{
@@ -58,7 +58,9 @@ extern vector<class power_meter *> power_meters;
 
 extern void start_power_measurement(void);
 extern void end_power_measurement(void);
-extern double global_joules_consumed(void);
+extern double global_power(void);
+extern void global_sample_power(void);
+extern double global_joules(void);
 extern double global_time_left(void);
 
 extern void detect_power_meters(void);
src/measurement/opal-sensors.cpp +1 -1
index 7316d1f436f6..a70ec56edcf2 100644
@@ -34,7 +34,7 @@ opal_sensors_power_meter::opal_sensors_power_meter(const char *power_supply_name
 	strncpy(name, power_supply_name, sizeof(name));
 }
 
-double opal_sensors_power_meter::joules_consumed(void)
+double opal_sensors_power_meter::power(void)
 {
 	bool ok;
 	int value;
src/measurement/opal-sensors.h +1 -1
index 8347733ad9ce..796209170eff 100644
@@ -35,7 +35,7 @@ public:
 	virtual void start_measurement(void) {};
 	virtual void end_measurement(void) {};
 
-	virtual double joules_consumed(void);
+	virtual double power(void);
 	virtual double dev_capacity(void) { return 0.0; }
 };
 
src/measurement/sysfs.h +1 -1
index b7efa3d70ca5..5b440f5f6f43 100644
@@ -48,7 +48,7 @@ public:
 	virtual void start_measurement(void);
 	virtual void end_measurement(void);
 
-	virtual double joules_consumed(void) { return rate; }
+	virtual double power(void) { return rate; }
 	virtual double dev_capacity(void) { return capacity; }
 };
 
src/parameters/parameters.cpp +1 -1
index 3bd5c05d92ef..38e17523e797 100644
@@ -327,7 +327,7 @@ void store_results(double duration)
 {
 	if (duration < 5)
 		return;
-	global_joules_consumed();
+	global_power();
 	if (all_results.power > 0.01) {
 		unsigned int overflow_index;
 		overflow_index = 50 + (rand() % MAX_KEEP);
src/parameters/parameters.h +1 -0
index 72f8d32f6962..1781a0e9cba1 100644
@@ -66,6 +66,7 @@ extern void set_parameter_value(const char *name, double value, struct parameter
 
 struct result_bundle
 {
+	double joules;
 	double power;
 	vector <double> utilization; /* device name, device utilization %age */
 };
src/process/do_process.cpp +6 -2
index 391cb7f892db..812572c0c9c8 100644
@@ -787,6 +787,7 @@ void process_update_display(void)
 	unsigned int i;
 	WINDOW *win;
 	double pw;
+	double joules;
 	int tl;
 	int tlt;
 	int tlr;
@@ -816,10 +817,11 @@ void process_update_display(void)
 	}
 
 	wprintw(win, _("Estimated power: %5.1f    Measured power: %5.1f    Sum: %5.1f\n\n"),
-				all_parameters.guessed_power, global_joules_consumed(), sum);
+				all_parameters.guessed_power, global_power(), sum);
 #endif
 
-	pw = global_joules_consumed();
+	pw = global_power();
+	joules = global_joules();
 	tl = global_time_left() / 60;
 	tlt = (tl /60);
 	tlr = tl % 60;
@@ -828,6 +830,8 @@ void process_update_display(void)
 		char buf[32];
 		wprintw(win, _("The battery reports a discharge rate of %sW\n"),
 				fmt_prefix(pw, buf));
+		wprintw(win, _("The power consumed was %sJ\n"),
+				fmt_prefix(joules, buf));
 		need_linebreak = 1;
 	}
 	if (tl > 0 && pw > 0.0001) {
Signature · 1 line
2.9.0