summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/intel_gpu_top.c121
1 files changed, 70 insertions, 51 deletions
diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index 8c771f3..14f333e 100644
--- a/tools/intel_gpu_top.c
+++ b/tools/intel_gpu_top.c
@@ -130,15 +130,14 @@ struct proc_cpudata
struct powerdata
{
+ char *battery_file;
+ char *battery_file_ext;
float watts;
float watts_chipset;
float watts_gfx;
};
-#define BATTERY "BAT1"
-#define DEBUGFS "/sys/kernel/debug/dri/0/"
-
static void
get_cpu_stat(struct cpudata *cpu)
{
@@ -162,6 +161,40 @@ get_cpu_stat(struct cpudata *cpu)
}
+static void
+power_stat_init(struct powerdata *power)
+{
+#ifdef __linux__
+ int i, ret;
+ power->battery_file = (char *)malloc(FILENAME_MAX);
+ for (i=0, ret=-1; i < 16; i++) {
+ /* Discover battery */
+ snprintf(power->battery_file, FILENAME_MAX, "/proc/acpi/battery/BAT%d/state", i);
+ if ((ret = access(power->battery_file, R_OK)) == 0)
+ break;
+ }
+ if (ret < 0) {
+ free(power->battery_file);
+ power->battery_file = NULL;
+ }
+
+
+ power->battery_file_ext = (char *)malloc(FILENAME_MAX);
+ for (i=0, ret=-1; i < 16; i++) {
+ snprintf(power->battery_file_ext, FILENAME_MAX, "/sys/kernel/debug/dri/%d/i915_emon_status", i);
+ if ((ret = access(power->battery_file_ext, R_OK)) == 0)
+ break;
+ }
+ if (ret < 0) {
+ free(power->battery_file_ext);
+ power->battery_file_ext = NULL;
+ }
+#else
+#warning Non-Linux OS detected, POWER initialization not done.
+#endif
+
+}
+
/* Reading from /sys/kernel/debug/dri/0/i915_emon_status can cause a division by zero within kernel, at least as of 3.2-rc1.
*
* So disabling it for now.
@@ -171,22 +204,9 @@ get_cpu_stat(struct cpudata *cpu)
*/
#define USE_I915_EMON_STATUS 0
-static char*
-get_battery_file(char *battery)
-{
-#ifdef __linux__
- /* The format is /proc/acpi/battery/BAT?/state */
- char *temp = (char *)malloc(sizeof(char) * (35 * strlen(battery)));
- sprintf(temp, "/proc/acpi/battery/%s/state", battery);
- return temp;
-#else
-#warning Non-Linux OS detected, skipping battery detection
- return NULL;
-#endif
-}
- static void
-get_power_stat(struct powerdata *power, const char *battery_file, char *debugfs, float elapsed_time)
+static void
+get_power_stat(struct powerdata *power, float elapsed_time)
{
#ifdef __linux__
FILE *file;
@@ -196,39 +216,39 @@ get_power_stat(struct powerdata *power, const char *battery_file, char *debugfs,
/* Reset values */
power->watts = power->watts_chipset = power->watts_gfx = -1;
- file = fopen(battery_file, "r");
- if (!file) {
- /* Unable to read battery information */
- return;
- }
- /* Reading ACPI-adquired power
- * Sample /proc/acpi/battery/BAT?/state file format:
- * present: yes
- * capacity state: ok
- * charging state: discharging
- * present rate: 1746 mA
- * remaining capacity: 5301 mAh
- * present voltage: 11788 mV
- */
- fgets(line, 255, file);
- fgets(line, 255, file);
- fgets(line, 255, file);
- fgets(line, 255, file);
- sscanf(line, "present rate:\t%lu mAh\n", &rate);
- fgets(line, 255, file);
- fgets(line, 255, file);
- sscanf(line, "present voltage:\t%lu mV\n", &voltage);
- fclose(file);
+ if (power->battery_file) {
+ file = fopen(power->battery_file, "r");
+ if (!file) {
+ /* Unable to read battery information */
+ return;
+ }
+ /* Reading ACPI-adquired power
+ * Sample /proc/acpi/battery/BAT?/state file format:
+ * present: yes
+ * capacity state: ok
+ * charging state: discharging
+ * present rate: 1746 mA
+ * remaining capacity: 5301 mAh
+ * present voltage: 11788 mV
+ */
+ fgets(line, 255, file);
+ fgets(line, 255, file);
+ fgets(line, 255, file);
+ fgets(line, 255, file);
+ sscanf(line, "present rate:\t%lu mAh\n", &rate);
+ fgets(line, 255, file);
+ fgets(line, 255, file);
+ sscanf(line, "present voltage:\t%lu mV\n", &voltage);
+ fclose(file);
- power->watts = (float) (rate * voltage) / 1000000;
- power->watts *= elapsed_time;
+ power->watts = (float) (rate * voltage) / 1000000;
+ power->watts *= elapsed_time;
+ }
/* Going deeper and reading GPU power */
- if (debugfs && USE_I915_EMON_STATUS) {
- char debugfs_entry[128];
+ if (power->battery_file_ext && USE_I915_EMON_STATUS) {
long tmp;
- sprintf(debugfs_entry, "%s%s", debugfs, "i915_emon_status");
- file = fopen(debugfs_entry, "r");
+ file = fopen(power->battery_file_ext, "r");
if (!file) {
/* Unable to read debugfs file */
return;
@@ -247,6 +267,7 @@ get_power_stat(struct powerdata *power, const char *battery_file, char *debugfs,
fgets(line, 255, file);
sscanf(line, "GFX power: %lu\n", &tmp);
power->watts_gfx = (float) tmp / 1000;
+ fclose(file);
}
#else
/* We are not on Linux, give a warning on compile */
@@ -592,8 +613,6 @@ int main(int argc, char **argv)
struct cpudata oldcpu, cpu;
struct powerdata power;
- char *battery = BATTERY, *battery_file = NULL;
- char *debugfs = DEBUGFS;
#ifdef __linux__
int do_perf=0;
char *perf_output=NULL;
@@ -730,7 +749,7 @@ int main(int argc, char **argv)
/* Calculate initial stats */
/* Battery */
- battery_file = get_battery_file(battery);
+ power_stat_init(&power);
/* CPU */
get_cpu_stat(&oldcpu);
@@ -831,7 +850,7 @@ int main(int argc, char **argv)
get_cpu_stat(&cpu);
/* Power */
- get_power_stat(&power, battery_file, debugfs, time_diff);
+ get_power_stat(&power, time_diff);
if (interactive) {
printf("%s", clear_screen);