2
0
mirror of https://github.com/lm-sensors/lm-sensors synced 2025-08-31 06:15:15 +00:00

Subject: [PATCH 3/3 v3]: sensors: Automatically scale energy/power units

Automatically scale energy and power values when printing them in cooked
mode.  Fixed all the warnings and warts that were brought up by Jean in
the previous thread.

Signed-off-by: Darrick J. Wong <djwong@us.ibm.com>



git-svn-id: http://lm-sensors.org/svn/lm-sensors/branches/lm-sensors-3.0.0@5185 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
Darrick J. Wong
2008-04-17 01:30:37 +00:00
parent 4c93dffd4f
commit f4e4fcd8c1
2 changed files with 52 additions and 10 deletions

View File

@@ -23,6 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "main.h"
#include "chips.h"
@@ -401,13 +402,44 @@ static void print_chip_fan(const sensors_chip_name *name,
printf("\n");
}
struct scale_table {
double upper_bound;
const char *unit;
};
static void scale_value(double *value, const char **prefixstr)
{
double abs_value = fabs(*value);
double divisor = 1e-9;
static struct scale_table prefix_scales[] = {
{1e-6, "n"},
{1e-3, "u"},
{1, "m"},
{1e3, ""},
{1e6, "k"},
{1e9, "M"},
{0, "G"}, /* no upper bound */
};
struct scale_table *scale = prefix_scales;
while (scale->upper_bound && abs_value > scale->upper_bound) {
divisor = scale->upper_bound;
scale++;
}
*value /= divisor;
*prefixstr = scale->unit;
}
static void print_chip_power(const sensors_chip_name *name,
const sensors_feature *feature,
int label_size)
{
double val;
int need_space = 0;
const sensors_subfeature *sf, *sfmin, *sfmax, *sfint;
char *label;
const char *unit;
if (!(label = sensors_get_label(name, feature))) {
fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
@@ -419,9 +451,11 @@ static void print_chip_power(const sensors_chip_name *name,
sf = sensors_get_subfeature(name, feature,
SENSORS_SUBFEATURE_POWER_AVERAGE);
if (sf)
printf("%6.2f W", get_value(name, sf));
else
if (sf) {
val = get_value(name, sf);
scale_value(&val, &unit);
printf("%6.2f %sW", val, unit);
} else
printf(" N/A");
sfmin = sensors_get_subfeature(name, feature,
@@ -434,13 +468,17 @@ static void print_chip_power(const sensors_chip_name *name,
printf(" (");
if (sfmin) {
printf("min = %6.2f W", get_value(name, sfmin));
val = get_value(name, sfmin);
scale_value(&val, &unit);
printf("min = %6.2f %sW", val, unit);
need_space = 1;
}
if (sfmax) {
printf("%smax = %6.2f W", (need_space ? ", " : ""),
get_value(name, sfmax));
val = get_value(name, sfmax);
scale_value(&val, &unit);
printf("%smax = %6.2f %sW", (need_space ? ", " : ""),
val, unit);
need_space = 1;
}
@@ -459,8 +497,10 @@ static void print_chip_energy(const sensors_chip_name *name,
const sensors_feature *feature,
int label_size)
{
double val;
const sensors_subfeature *sf;
char *label;
const char *unit;
if (!(label = sensors_get_label(name, feature))) {
fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
@@ -472,9 +512,11 @@ static void print_chip_energy(const sensors_chip_name *name,
sf = sensors_get_subfeature(name, feature,
SENSORS_SUBFEATURE_ENERGY_INPUT);
if (sf)
printf("%6.2f J", get_value(name, sf));
else
if (sf) {
val = get_value(name, sf);
scale_value(&val, &unit);
printf("%6.2f %sJ", val, unit);
} else
printf(" N/A");
printf("\n");