2005-09-18 20:30:44 +00:00
|
|
|
/*
|
|
|
|
sysfs.c - Part of libsensors, a library for reading Linux sensor data
|
|
|
|
Copyright (c) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
|
2010-11-03 13:00:59 +00:00
|
|
|
Copyright (C) 2007-2010 Jean Delvare <khali@linux-fr.org>
|
2005-09-18 20:30:44 +00:00
|
|
|
|
2010-07-01 11:56:42 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2005-09-18 20:30:44 +00:00
|
|
|
|
2010-07-01 11:56:42 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
2005-09-18 20:30:44 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-07-01 11:56:42 +00:00
|
|
|
GNU Lesser General Public License for more details.
|
2005-09-18 20:30:44 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2008-03-26 13:37:12 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301 USA.
|
2005-09-18 20:30:44 +00:00
|
|
|
*/
|
|
|
|
|
2005-09-18 20:38:19 +00:00
|
|
|
/* this define needed for strndup() */
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2007-06-25 13:58:55 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-01-31 12:54:03 +00:00
|
|
|
#include <sys/vfs.h>
|
2007-06-25 13:58:55 +00:00
|
|
|
#include <unistd.h>
|
2005-09-18 20:30:44 +00:00
|
|
|
#include <string.h>
|
2007-04-10 11:51:07 +00:00
|
|
|
#include <stdlib.h>
|
2005-09-18 20:30:44 +00:00
|
|
|
#include <limits.h>
|
2006-02-25 21:32:26 +00:00
|
|
|
#include <errno.h>
|
2007-12-10 13:30:22 +00:00
|
|
|
#include <dirent.h>
|
2005-09-18 20:37:42 +00:00
|
|
|
#include "data.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "access.h"
|
|
|
|
#include "general.h"
|
2005-09-18 20:30:44 +00:00
|
|
|
#include "sysfs.h"
|
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
|
|
|
#define ATTR_MAX 128
|
2012-01-31 12:54:03 +00:00
|
|
|
#define SYSFS_MAGIC 0x62656572
|
2007-12-10 13:30:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read an attribute from sysfs
|
|
|
|
* Returns a pointer to a freshly allocated string; free it yourself.
|
|
|
|
* If the file doesn't exist or can't be read, NULL is returned.
|
|
|
|
*/
|
|
|
|
static char *sysfs_read_attr(const char *device, const char *attr)
|
|
|
|
{
|
|
|
|
char path[NAME_MAX];
|
|
|
|
char buf[ATTR_MAX], *p;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
snprintf(path, NAME_MAX, "%s/%s", device, attr);
|
|
|
|
|
|
|
|
if (!(f = fopen(path, "r")))
|
|
|
|
return NULL;
|
|
|
|
p = fgets(buf, ATTR_MAX, f);
|
|
|
|
fclose(f);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Last byte is a '\n'; chop that off */
|
|
|
|
p = strndup(buf, strlen(buf) - 1);
|
|
|
|
if (!p)
|
2008-03-05 07:44:16 +00:00
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
2007-12-10 13:30:22 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call an arbitrary function for each class device of the given class
|
|
|
|
* Returns 0 on success (all calls returned 0), a positive errno for
|
|
|
|
* local errors, or a negative error value if any call fails.
|
|
|
|
*/
|
|
|
|
static int sysfs_foreach_classdev(const char *class_name,
|
2007-12-16 19:37:11 +00:00
|
|
|
int (*func)(const char *, const char *))
|
2007-12-10 13:30:22 +00:00
|
|
|
{
|
|
|
|
char path[NAME_MAX];
|
|
|
|
int path_off, ret;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
|
|
|
path_off = snprintf(path, NAME_MAX, "%s/class/%s",
|
|
|
|
sensors_sysfs_mount, class_name);
|
|
|
|
if (!(dir = opendir(path)))
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (!ret && (ent = readdir(dir))) {
|
|
|
|
if (ent->d_name[0] == '.') /* skip hidden entries */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(path + path_off, NAME_MAX - path_off, "/%s",
|
|
|
|
ent->d_name);
|
|
|
|
ret = func(path, ent->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call an arbitrary function for each device of the given bus type
|
|
|
|
* Returns 0 on success (all calls returned 0), a positive errno for
|
|
|
|
* local errors, or a negative error value if any call fails.
|
|
|
|
*/
|
|
|
|
static int sysfs_foreach_busdev(const char *bus_type,
|
2007-12-16 19:37:11 +00:00
|
|
|
int (*func)(const char *, const char *))
|
2007-12-10 13:30:22 +00:00
|
|
|
{
|
|
|
|
char path[NAME_MAX];
|
|
|
|
int path_off, ret;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
|
|
|
path_off = snprintf(path, NAME_MAX, "%s/bus/%s/devices",
|
|
|
|
sensors_sysfs_mount, bus_type);
|
|
|
|
if (!(dir = opendir(path)))
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (!ret && (ent = readdir(dir))) {
|
|
|
|
if (ent->d_name[0] == '.') /* skip hidden entries */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(path + path_off, NAME_MAX - path_off, "/%s",
|
|
|
|
ent->d_name);
|
|
|
|
ret = func(path, ent->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
2005-09-18 20:30:44 +00:00
|
|
|
char sensors_sysfs_mount[NAME_MAX];
|
|
|
|
|
2010-12-14 09:06:19 +00:00
|
|
|
/* max_subfeatures is now computed dynamically */
|
|
|
|
#define FEATURE_SIZE (max_subfeatures * 2)
|
2007-04-09 14:09:21 +00:00
|
|
|
|
2007-05-28 12:38:28 +00:00
|
|
|
static
|
2007-09-23 12:49:47 +00:00
|
|
|
int get_type_scaling(sensors_subfeature_type type)
|
2007-05-28 12:38:28 +00:00
|
|
|
{
|
2008-04-17 01:27:22 +00:00
|
|
|
/* Multipliers for subfeatures */
|
2007-09-23 17:42:27 +00:00
|
|
|
switch (type & 0xFF80) {
|
2007-09-23 12:12:19 +00:00
|
|
|
case SENSORS_SUBFEATURE_IN_INPUT:
|
|
|
|
case SENSORS_SUBFEATURE_TEMP_INPUT:
|
2008-10-24 09:04:26 +00:00
|
|
|
case SENSORS_SUBFEATURE_CURR_INPUT:
|
2011-02-13 18:55:09 +00:00
|
|
|
case SENSORS_SUBFEATURE_HUMIDITY_INPUT:
|
2007-09-05 08:21:19 +00:00
|
|
|
return 1000;
|
2007-09-23 12:12:19 +00:00
|
|
|
case SENSORS_SUBFEATURE_FAN_INPUT:
|
2007-09-05 08:21:19 +00:00
|
|
|
return 1;
|
2008-04-17 01:27:22 +00:00
|
|
|
case SENSORS_SUBFEATURE_POWER_AVERAGE:
|
|
|
|
case SENSORS_SUBFEATURE_ENERGY_INPUT:
|
|
|
|
return 1000000;
|
2007-05-28 12:38:28 +00:00
|
|
|
}
|
|
|
|
|
2008-04-17 01:27:22 +00:00
|
|
|
/* Multipliers for second class subfeatures
|
|
|
|
that need their own multiplier */
|
2007-05-28 12:38:28 +00:00
|
|
|
switch (type) {
|
2008-04-17 01:27:22 +00:00
|
|
|
case SENSORS_SUBFEATURE_POWER_AVERAGE_INTERVAL:
|
2007-09-23 12:12:19 +00:00
|
|
|
case SENSORS_SUBFEATURE_VID:
|
|
|
|
case SENSORS_SUBFEATURE_TEMP_OFFSET:
|
2007-09-05 08:21:19 +00:00
|
|
|
return 1000;
|
2007-05-28 12:38:28 +00:00
|
|
|
default:
|
2007-09-05 08:21:19 +00:00
|
|
|
return 1;
|
2007-05-28 12:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
static
|
|
|
|
char *get_feature_name(sensors_feature_type ftype, char *sfname)
|
|
|
|
{
|
|
|
|
char *name, *underscore;
|
|
|
|
|
|
|
|
switch (ftype) {
|
|
|
|
case SENSORS_FEATURE_IN:
|
|
|
|
case SENSORS_FEATURE_FAN:
|
|
|
|
case SENSORS_FEATURE_TEMP:
|
2008-04-17 01:27:22 +00:00
|
|
|
case SENSORS_FEATURE_POWER:
|
|
|
|
case SENSORS_FEATURE_ENERGY:
|
2008-10-24 09:04:26 +00:00
|
|
|
case SENSORS_FEATURE_CURR:
|
2011-02-13 18:55:09 +00:00
|
|
|
case SENSORS_FEATURE_HUMIDITY:
|
2010-11-03 13:00:59 +00:00
|
|
|
case SENSORS_FEATURE_INTRUSION:
|
2007-09-23 12:16:50 +00:00
|
|
|
underscore = strchr(sfname, '_');
|
|
|
|
name = strndup(sfname, underscore - sfname);
|
2009-02-20 10:56:52 +00:00
|
|
|
if (!name)
|
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
name = strdup(sfname);
|
2009-02-20 10:56:52 +00:00
|
|
|
if (!name)
|
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
2007-09-23 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2007-09-23 12:07:53 +00:00
|
|
|
/* Static mappings for use by sensors_subfeature_get_type() */
|
2007-09-23 12:12:19 +00:00
|
|
|
struct subfeature_type_match
|
2007-09-05 16:14:53 +00:00
|
|
|
{
|
|
|
|
const char *name;
|
2007-09-23 12:12:19 +00:00
|
|
|
sensors_subfeature_type type;
|
2007-09-05 16:14:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct feature_type_match
|
|
|
|
{
|
|
|
|
const char *name;
|
2007-09-23 12:12:19 +00:00
|
|
|
const struct subfeature_type_match *submatches;
|
2007-09-05 16:14:53 +00:00
|
|
|
};
|
|
|
|
|
2007-09-23 12:12:19 +00:00
|
|
|
static const struct subfeature_type_match temp_matches[] = {
|
|
|
|
{ "input", SENSORS_SUBFEATURE_TEMP_INPUT },
|
|
|
|
{ "max", SENSORS_SUBFEATURE_TEMP_MAX },
|
|
|
|
{ "max_hyst", SENSORS_SUBFEATURE_TEMP_MAX_HYST },
|
|
|
|
{ "min", SENSORS_SUBFEATURE_TEMP_MIN },
|
|
|
|
{ "crit", SENSORS_SUBFEATURE_TEMP_CRIT },
|
|
|
|
{ "crit_hyst", SENSORS_SUBFEATURE_TEMP_CRIT_HYST },
|
2010-12-14 20:26:27 +00:00
|
|
|
{ "lcrit", SENSORS_SUBFEATURE_TEMP_LCRIT },
|
|
|
|
{ "emergency", SENSORS_SUBFEATURE_TEMP_EMERGENCY },
|
|
|
|
{ "emergency_hyst", SENSORS_SUBFEATURE_TEMP_EMERGENCY_HYST },
|
2012-01-09 17:30:10 +00:00
|
|
|
{ "lowest", SENSORS_SUBFEATURE_TEMP_LOWEST },
|
|
|
|
{ "highest", SENSORS_SUBFEATURE_TEMP_HIGHEST },
|
2007-09-23 12:12:19 +00:00
|
|
|
{ "alarm", SENSORS_SUBFEATURE_TEMP_ALARM },
|
|
|
|
{ "min_alarm", SENSORS_SUBFEATURE_TEMP_MIN_ALARM },
|
|
|
|
{ "max_alarm", SENSORS_SUBFEATURE_TEMP_MAX_ALARM },
|
|
|
|
{ "crit_alarm", SENSORS_SUBFEATURE_TEMP_CRIT_ALARM },
|
2010-12-14 20:26:27 +00:00
|
|
|
{ "emergency_alarm", SENSORS_SUBFEATURE_TEMP_EMERGENCY_ALARM },
|
2011-02-09 19:25:22 +00:00
|
|
|
{ "lcrit_alarm", SENSORS_SUBFEATURE_TEMP_LCRIT_ALARM },
|
2007-09-23 12:12:19 +00:00
|
|
|
{ "fault", SENSORS_SUBFEATURE_TEMP_FAULT },
|
|
|
|
{ "type", SENSORS_SUBFEATURE_TEMP_TYPE },
|
|
|
|
{ "offset", SENSORS_SUBFEATURE_TEMP_OFFSET },
|
2010-06-21 09:02:30 +00:00
|
|
|
{ "beep", SENSORS_SUBFEATURE_TEMP_BEEP },
|
2007-09-05 16:14:53 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2007-09-23 12:12:19 +00:00
|
|
|
static const struct subfeature_type_match in_matches[] = {
|
|
|
|
{ "input", SENSORS_SUBFEATURE_IN_INPUT },
|
|
|
|
{ "min", SENSORS_SUBFEATURE_IN_MIN },
|
|
|
|
{ "max", SENSORS_SUBFEATURE_IN_MAX },
|
2010-12-14 20:26:27 +00:00
|
|
|
{ "lcrit", SENSORS_SUBFEATURE_IN_LCRIT },
|
|
|
|
{ "crit", SENSORS_SUBFEATURE_IN_CRIT },
|
2012-01-09 17:30:10 +00:00
|
|
|
{ "average", SENSORS_SUBFEATURE_IN_AVERAGE },
|
|
|
|
{ "lowest", SENSORS_SUBFEATURE_IN_LOWEST },
|
|
|
|
{ "highest", SENSORS_SUBFEATURE_IN_HIGHEST },
|
2007-09-23 12:12:19 +00:00
|
|
|
{ "alarm", SENSORS_SUBFEATURE_IN_ALARM },
|
|
|
|
{ "min_alarm", SENSORS_SUBFEATURE_IN_MIN_ALARM },
|
|
|
|
{ "max_alarm", SENSORS_SUBFEATURE_IN_MAX_ALARM },
|
2011-02-09 19:25:22 +00:00
|
|
|
{ "lcrit_alarm", SENSORS_SUBFEATURE_IN_LCRIT_ALARM },
|
|
|
|
{ "crit_alarm", SENSORS_SUBFEATURE_IN_CRIT_ALARM },
|
2010-06-21 09:02:30 +00:00
|
|
|
{ "beep", SENSORS_SUBFEATURE_IN_BEEP },
|
2007-09-05 16:14:53 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2007-09-23 12:12:19 +00:00
|
|
|
static const struct subfeature_type_match fan_matches[] = {
|
|
|
|
{ "input", SENSORS_SUBFEATURE_FAN_INPUT },
|
|
|
|
{ "min", SENSORS_SUBFEATURE_FAN_MIN },
|
2012-03-06 07:34:13 +00:00
|
|
|
{ "max", SENSORS_SUBFEATURE_FAN_MAX },
|
2007-09-23 12:12:19 +00:00
|
|
|
{ "div", SENSORS_SUBFEATURE_FAN_DIV },
|
2011-03-04 20:37:43 +00:00
|
|
|
{ "pulses", SENSORS_SUBFEATURE_FAN_PULSES },
|
2007-09-23 12:12:19 +00:00
|
|
|
{ "alarm", SENSORS_SUBFEATURE_FAN_ALARM },
|
2012-03-06 07:34:13 +00:00
|
|
|
{ "min_alarm", SENSORS_SUBFEATURE_FAN_MIN_ALARM },
|
|
|
|
{ "max_alarm", SENSORS_SUBFEATURE_FAN_MAX_ALARM },
|
2007-09-23 12:12:19 +00:00
|
|
|
{ "fault", SENSORS_SUBFEATURE_FAN_FAULT },
|
2010-06-21 09:02:30 +00:00
|
|
|
{ "beep", SENSORS_SUBFEATURE_FAN_BEEP },
|
2007-09-05 16:14:53 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2008-04-17 01:27:22 +00:00
|
|
|
static const struct subfeature_type_match power_matches[] = {
|
|
|
|
{ "average", SENSORS_SUBFEATURE_POWER_AVERAGE },
|
|
|
|
{ "average_highest", SENSORS_SUBFEATURE_POWER_AVERAGE_HIGHEST },
|
|
|
|
{ "average_lowest", SENSORS_SUBFEATURE_POWER_AVERAGE_LOWEST },
|
2008-10-24 09:03:18 +00:00
|
|
|
{ "input", SENSORS_SUBFEATURE_POWER_INPUT },
|
|
|
|
{ "input_highest", SENSORS_SUBFEATURE_POWER_INPUT_HIGHEST },
|
|
|
|
{ "input_lowest", SENSORS_SUBFEATURE_POWER_INPUT_LOWEST },
|
2010-12-14 20:26:27 +00:00
|
|
|
{ "cap", SENSORS_SUBFEATURE_POWER_CAP },
|
|
|
|
{ "cap_hyst", SENSORS_SUBFEATURE_POWER_CAP_HYST },
|
2011-02-09 19:25:22 +00:00
|
|
|
{ "cap_alarm", SENSORS_SUBFEATURE_POWER_CAP_ALARM },
|
2010-12-14 20:26:27 +00:00
|
|
|
{ "alarm", SENSORS_SUBFEATURE_POWER_ALARM },
|
2011-02-09 19:25:22 +00:00
|
|
|
{ "max", SENSORS_SUBFEATURE_POWER_MAX },
|
|
|
|
{ "max_alarm", SENSORS_SUBFEATURE_POWER_MAX_ALARM },
|
|
|
|
{ "crit", SENSORS_SUBFEATURE_POWER_CRIT },
|
|
|
|
{ "crit_alarm", SENSORS_SUBFEATURE_POWER_CRIT_ALARM },
|
2008-04-17 01:27:22 +00:00
|
|
|
{ "average_interval", SENSORS_SUBFEATURE_POWER_AVERAGE_INTERVAL },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct subfeature_type_match energy_matches[] = {
|
|
|
|
{ "input", SENSORS_SUBFEATURE_ENERGY_INPUT },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2008-10-24 09:04:26 +00:00
|
|
|
static const struct subfeature_type_match curr_matches[] = {
|
|
|
|
{ "input", SENSORS_SUBFEATURE_CURR_INPUT },
|
|
|
|
{ "min", SENSORS_SUBFEATURE_CURR_MIN },
|
|
|
|
{ "max", SENSORS_SUBFEATURE_CURR_MAX },
|
2011-02-09 19:25:22 +00:00
|
|
|
{ "lcrit", SENSORS_SUBFEATURE_CURR_LCRIT },
|
|
|
|
{ "crit", SENSORS_SUBFEATURE_CURR_CRIT },
|
2012-01-09 17:30:10 +00:00
|
|
|
{ "average", SENSORS_SUBFEATURE_CURR_AVERAGE },
|
|
|
|
{ "lowest", SENSORS_SUBFEATURE_CURR_LOWEST },
|
|
|
|
{ "highest", SENSORS_SUBFEATURE_CURR_HIGHEST },
|
2008-10-24 09:04:26 +00:00
|
|
|
{ "alarm", SENSORS_SUBFEATURE_CURR_ALARM },
|
|
|
|
{ "min_alarm", SENSORS_SUBFEATURE_CURR_MIN_ALARM },
|
|
|
|
{ "max_alarm", SENSORS_SUBFEATURE_CURR_MAX_ALARM },
|
2011-02-09 19:25:22 +00:00
|
|
|
{ "lcrit_alarm", SENSORS_SUBFEATURE_CURR_LCRIT_ALARM },
|
|
|
|
{ "crit_alarm", SENSORS_SUBFEATURE_CURR_CRIT_ALARM },
|
2010-06-21 09:02:30 +00:00
|
|
|
{ "beep", SENSORS_SUBFEATURE_CURR_BEEP },
|
2008-10-24 09:04:26 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2011-02-13 18:55:09 +00:00
|
|
|
static const struct subfeature_type_match humidity_matches[] = {
|
|
|
|
{ "input", SENSORS_SUBFEATURE_HUMIDITY_INPUT },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2007-09-23 12:12:19 +00:00
|
|
|
static const struct subfeature_type_match cpu_matches[] = {
|
|
|
|
{ "vid", SENSORS_SUBFEATURE_VID },
|
2007-09-05 16:14:53 +00:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2010-11-03 13:00:59 +00:00
|
|
|
static const struct subfeature_type_match intrusion_matches[] = {
|
|
|
|
{ "alarm", SENSORS_SUBFEATURE_INTRUSION_ALARM },
|
|
|
|
{ "beep", SENSORS_SUBFEATURE_INTRUSION_BEEP },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
2007-09-05 16:14:53 +00:00
|
|
|
static struct feature_type_match matches[] = {
|
|
|
|
{ "temp%d%c", temp_matches },
|
|
|
|
{ "in%d%c", in_matches },
|
|
|
|
{ "fan%d%c", fan_matches },
|
|
|
|
{ "cpu%d%c", cpu_matches },
|
2008-04-17 01:27:22 +00:00
|
|
|
{ "power%d%c", power_matches },
|
2008-10-24 09:04:26 +00:00
|
|
|
{ "curr%d%c", curr_matches },
|
2008-04-17 01:27:22 +00:00
|
|
|
{ "energy%d%c", energy_matches },
|
2010-11-03 13:00:59 +00:00
|
|
|
{ "intrusion%d%c", intrusion_matches },
|
2011-02-13 18:55:09 +00:00
|
|
|
{ "humidity%d%c", humidity_matches },
|
2007-09-05 16:14:53 +00:00
|
|
|
};
|
|
|
|
|
2007-09-23 12:07:53 +00:00
|
|
|
/* Return the subfeature type and channel number based on the subfeature
|
|
|
|
name */
|
2007-09-05 16:14:53 +00:00
|
|
|
static
|
2007-09-23 12:12:19 +00:00
|
|
|
sensors_subfeature_type sensors_subfeature_get_type(const char *name, int *nr)
|
2007-09-05 16:14:53 +00:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int i, count;
|
2007-09-23 12:12:19 +00:00
|
|
|
const struct subfeature_type_match *submatches;
|
2007-09-05 16:14:53 +00:00
|
|
|
|
|
|
|
/* Special case */
|
|
|
|
if (!strcmp(name, "beep_enable")) {
|
|
|
|
*nr = 0;
|
2007-09-23 12:12:19 +00:00
|
|
|
return SENSORS_SUBFEATURE_BEEP_ENABLE;
|
2007-09-05 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(matches); i++)
|
|
|
|
if ((count = sscanf(name, matches[i].name, nr, &c)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == ARRAY_SIZE(matches) || count != 2 || c != '_')
|
2007-09-23 12:12:19 +00:00
|
|
|
return SENSORS_SUBFEATURE_UNKNOWN; /* no match */
|
2007-09-05 16:14:53 +00:00
|
|
|
|
|
|
|
submatches = matches[i].submatches;
|
|
|
|
name = strchr(name + 3, '_') + 1;
|
|
|
|
for (i = 0; submatches[i].name != NULL; i++)
|
|
|
|
if (!strcmp(name, submatches[i].name))
|
|
|
|
return submatches[i].type;
|
|
|
|
|
2007-09-23 12:12:19 +00:00
|
|
|
return SENSORS_SUBFEATURE_UNKNOWN;
|
2007-09-05 16:14:53 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 21:47:41 +00:00
|
|
|
static int sensors_compute_max_sf(void)
|
2010-12-14 09:06:19 +00:00
|
|
|
{
|
|
|
|
int i, j, max, offset;
|
|
|
|
const struct subfeature_type_match *submatches;
|
|
|
|
sensors_feature_type ftype;
|
|
|
|
|
|
|
|
max = 0;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(matches); i++) {
|
|
|
|
submatches = matches[i].submatches;
|
|
|
|
for (j = 0; submatches[j].name != NULL; j++) {
|
|
|
|
ftype = submatches[j].type >> 8;
|
|
|
|
|
|
|
|
if (ftype < SENSORS_FEATURE_VID) {
|
|
|
|
offset = submatches[j].type & 0x7F;
|
|
|
|
if (offset >= max)
|
|
|
|
max = offset + 1;
|
|
|
|
} else {
|
|
|
|
offset = submatches[j].type & 0xFF;
|
|
|
|
if (offset >= max * 2)
|
|
|
|
max = ((offset + 1) + 1) / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
static int sensors_get_attr_mode(const char *device, const char *attr)
|
|
|
|
{
|
|
|
|
char path[NAME_MAX];
|
|
|
|
struct stat st;
|
|
|
|
int mode = 0;
|
|
|
|
|
|
|
|
snprintf(path, NAME_MAX, "%s/%s", device, attr);
|
|
|
|
if (!stat(path, &st)) {
|
|
|
|
if (st.st_mode & S_IRUSR)
|
|
|
|
mode |= SENSORS_MODE_R;
|
|
|
|
if (st.st_mode & S_IWUSR)
|
|
|
|
mode |= SENSORS_MODE_W;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2007-06-28 14:39:34 +00:00
|
|
|
static int sensors_read_dynamic_chip(sensors_chip_features *chip,
|
2007-12-10 13:30:22 +00:00
|
|
|
const char *dev_path)
|
2007-04-09 14:09:21 +00:00
|
|
|
{
|
2007-09-23 12:16:50 +00:00
|
|
|
int i, fnum = 0, sfnum = 0, prev_slot;
|
2010-12-14 09:06:19 +00:00
|
|
|
static int max_subfeatures;
|
2007-12-10 13:30:22 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
struct {
|
|
|
|
int count;
|
|
|
|
sensors_subfeature *sf;
|
|
|
|
} all_types[SENSORS_FEATURE_MAX];
|
2007-09-23 12:02:22 +00:00
|
|
|
sensors_subfeature *dyn_subfeatures;
|
2007-09-23 12:05:16 +00:00
|
|
|
sensors_feature *dyn_features;
|
2007-09-23 12:16:50 +00:00
|
|
|
sensors_feature_type ftype;
|
|
|
|
sensors_subfeature_type sftype;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
if (!(dir = opendir(dev_path)))
|
|
|
|
return -errno;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2010-12-14 09:06:19 +00:00
|
|
|
/* Dynamically figure out the max number of subfeatures */
|
|
|
|
if (!max_subfeatures)
|
2014-01-28 21:47:41 +00:00
|
|
|
max_subfeatures = sensors_compute_max_sf();
|
2010-12-14 09:06:19 +00:00
|
|
|
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
/* We use a set of large sparse tables at first (one per main
|
|
|
|
feature type present) to store all found subfeatures, so that we
|
|
|
|
can store them sorted and then later create a dense sorted table. */
|
|
|
|
memset(&all_types, 0, sizeof(all_types));
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
while ((ent = readdir(dir))) {
|
2009-02-06 13:14:36 +00:00
|
|
|
char *name;
|
2007-07-19 20:49:29 +00:00
|
|
|
int nr;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2009-02-06 13:14:36 +00:00
|
|
|
/* Skip directories and symlinks */
|
|
|
|
if (ent->d_type != DT_REG)
|
2007-12-10 13:30:22 +00:00
|
|
|
continue;
|
|
|
|
|
2009-02-06 13:14:36 +00:00
|
|
|
name = ent->d_name;
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
sftype = sensors_subfeature_get_type(name, &nr);
|
|
|
|
if (sftype == SENSORS_SUBFEATURE_UNKNOWN)
|
2007-07-19 20:49:29 +00:00
|
|
|
continue;
|
2010-11-02 12:59:31 +00:00
|
|
|
ftype = sftype >> 8;
|
2007-07-19 20:49:29 +00:00
|
|
|
|
|
|
|
/* Adjust the channel number */
|
2010-11-02 12:59:31 +00:00
|
|
|
switch (ftype) {
|
|
|
|
case SENSORS_FEATURE_FAN:
|
|
|
|
case SENSORS_FEATURE_TEMP:
|
|
|
|
case SENSORS_FEATURE_POWER:
|
|
|
|
case SENSORS_FEATURE_ENERGY:
|
|
|
|
case SENSORS_FEATURE_CURR:
|
2011-02-13 18:55:09 +00:00
|
|
|
case SENSORS_FEATURE_HUMIDITY:
|
2008-04-17 01:27:22 +00:00
|
|
|
nr--;
|
|
|
|
break;
|
2010-11-02 12:59:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
2007-04-10 11:51:07 +00:00
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
/* Skip invalid entries */
|
|
|
|
if (nr < 0) {
|
2007-09-23 14:34:42 +00:00
|
|
|
#ifdef DEBUG
|
2008-03-05 07:44:16 +00:00
|
|
|
sensors_fatal_error(__func__,
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
"Invalid channel number!");
|
2007-09-23 14:34:42 +00:00
|
|
|
#endif
|
2007-04-10 11:51:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
/* (Re-)allocate memory if needed */
|
|
|
|
if (all_types[ftype].count < nr + 1) {
|
|
|
|
int old_count = all_types[ftype].count;
|
|
|
|
|
|
|
|
while (all_types[ftype].count < nr + 1)
|
|
|
|
all_types[ftype].count += 8;
|
|
|
|
|
|
|
|
all_types[ftype].sf = realloc(all_types[ftype].sf,
|
|
|
|
all_types[ftype].count *
|
|
|
|
FEATURE_SIZE *
|
|
|
|
sizeof(sensors_subfeature));
|
|
|
|
if (!all_types[ftype].sf)
|
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
|
|
|
memset(all_types[ftype].sf + old_count * FEATURE_SIZE,
|
|
|
|
0, (all_types[ftype].count - old_count) *
|
|
|
|
FEATURE_SIZE * sizeof(sensors_subfeature));
|
|
|
|
}
|
|
|
|
|
2007-09-23 12:07:53 +00:00
|
|
|
/* "calculate" a place to store the subfeature in our sparse,
|
2007-04-10 11:51:07 +00:00
|
|
|
sorted table */
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
if (ftype < SENSORS_FEATURE_VID)
|
|
|
|
i = nr * FEATURE_SIZE +
|
2010-12-14 09:06:19 +00:00
|
|
|
((sftype & 0x80) >> 7) * max_subfeatures +
|
2007-09-23 17:42:27 +00:00
|
|
|
(sftype & 0x7F);
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
else
|
|
|
|
i = nr * FEATURE_SIZE + (sftype & 0xFF);
|
2007-08-31 14:42:20 +00:00
|
|
|
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
if (all_types[ftype].sf[i].name) {
|
2007-09-23 14:34:42 +00:00
|
|
|
#ifdef DEBUG
|
2008-03-05 07:44:16 +00:00
|
|
|
sensors_fatal_error(__func__, "Duplicate subfeature");
|
2007-09-23 14:34:42 +00:00
|
|
|
#endif
|
2007-04-10 11:51:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-09-23 12:07:53 +00:00
|
|
|
/* fill in the subfeature members */
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
all_types[ftype].sf[i].type = sftype;
|
|
|
|
all_types[ftype].sf[i].name = strdup(name);
|
|
|
|
if (!all_types[ftype].sf[i].name)
|
2009-02-20 10:56:52 +00:00
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
|
|
|
|
2010-11-02 13:00:19 +00:00
|
|
|
/* Other and misc subfeatures are never scaled */
|
|
|
|
if (sftype < SENSORS_SUBFEATURE_VID && !(sftype & 0x80))
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
all_types[ftype].sf[i].flags |= SENSORS_COMPUTE_MAPPING;
|
|
|
|
all_types[ftype].sf[i].flags |=
|
|
|
|
sensors_get_attr_mode(dev_path, name);
|
2007-04-10 11:51:07 +00:00
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
sfnum++;
|
2007-04-09 14:09:21 +00:00
|
|
|
}
|
2007-12-10 13:30:22 +00:00
|
|
|
closedir(dir);
|
2007-04-10 11:51:07 +00:00
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
if (!sfnum) { /* No subfeature */
|
2007-09-23 12:02:22 +00:00
|
|
|
chip->subfeature = NULL;
|
2007-08-30 21:10:41 +00:00
|
|
|
goto exit_free;
|
2007-07-21 09:28:12 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
/* How many main features? */
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
for (ftype = 0; ftype < SENSORS_FEATURE_MAX; ftype++) {
|
|
|
|
prev_slot = -1;
|
|
|
|
for (i = 0; i < all_types[ftype].count * FEATURE_SIZE; i++) {
|
|
|
|
if (!all_types[ftype].sf[i].name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i / FEATURE_SIZE != prev_slot) {
|
|
|
|
fnum++;
|
|
|
|
prev_slot = i / FEATURE_SIZE;
|
|
|
|
}
|
2007-09-23 12:16:50 +00:00
|
|
|
}
|
2007-09-23 12:05:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
dyn_subfeatures = calloc(sfnum, sizeof(sensors_subfeature));
|
2007-09-23 12:05:16 +00:00
|
|
|
dyn_features = calloc(fnum, sizeof(sensors_feature));
|
2007-09-23 12:16:50 +00:00
|
|
|
if (!dyn_subfeatures || !dyn_features)
|
2008-03-05 07:44:16 +00:00
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
2007-09-23 12:05:16 +00:00
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
/* Copy from the sparse array to the compact array */
|
|
|
|
sfnum = 0;
|
|
|
|
fnum = -1;
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
for (ftype = 0; ftype < SENSORS_FEATURE_MAX; ftype++) {
|
|
|
|
prev_slot = -1;
|
|
|
|
for (i = 0; i < all_types[ftype].count * FEATURE_SIZE; i++) {
|
|
|
|
if (!all_types[ftype].sf[i].name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* New main feature? */
|
|
|
|
if (i / FEATURE_SIZE != prev_slot) {
|
|
|
|
fnum++;
|
|
|
|
prev_slot = i / FEATURE_SIZE;
|
|
|
|
|
|
|
|
dyn_features[fnum].name =
|
|
|
|
get_feature_name(ftype,
|
|
|
|
all_types[ftype].sf[i].name);
|
|
|
|
dyn_features[fnum].number = fnum;
|
|
|
|
dyn_features[fnum].first_subfeature = sfnum;
|
|
|
|
dyn_features[fnum].type = ftype;
|
|
|
|
}
|
2007-09-23 12:16:50 +00:00
|
|
|
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
dyn_subfeatures[sfnum] = all_types[ftype].sf[i];
|
|
|
|
dyn_subfeatures[sfnum].number = sfnum;
|
|
|
|
/* Back to the feature */
|
|
|
|
dyn_subfeatures[sfnum].mapping = fnum;
|
2007-09-23 12:16:50 +00:00
|
|
|
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
sfnum++;
|
|
|
|
}
|
2007-09-23 12:05:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
chip->subfeature = dyn_subfeatures;
|
|
|
|
chip->subfeature_count = sfnum;
|
2007-09-23 12:05:16 +00:00
|
|
|
chip->feature = dyn_features;
|
2007-09-23 12:16:50 +00:00
|
|
|
chip->feature_count = ++fnum;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-08-30 21:10:41 +00:00
|
|
|
exit_free:
|
libsensors: Get rid of arbitrary limit on per-type sensor count
When gathering the attributes of each hwmon chip, libsensors uses a
temporary structure in memory to order and group all the attributes
into features. This temporary structure used to be a single array with
room for every possible attribute/subfeature. While simple, this
approach required to predefine a maximum number of per-type sensor
that could be handled.
In order to get rid of this arbitrary limit, which we hit and had to
raise three times already, I changed the temporary structure to an
array of dynamically allocated per-type subattribute arrays. This lets
us not allocate any memory for types which aren't implemented by a
given chip, and more importantly, this lets us reallocate room for
more attributes of a given type as needed.
I decided to allocate chunks of 8 attributes at a time, as this seemed
a good compromise between two frequent reallocations and
over-provisioning. It could be tweaked if needed.
Icing on the cake, I benchmarked this change on two different systems
and it results in performance gains. The total heap usage as reported
by valgrind is down by 50% on average, with peak memory consumption
(as reported by valgrind's massif) also down by 43% on average. The
total instructions count (as reported by valgrind's callgrind) is down
by 11% on average, with measured execution time also down by a few
percents. Valgrind rocks, BTW.
I have some ideas to optimize the memory allocations further, but I do
not expect such a huge gain from them. They may not even improve peak
memory consumption as massif shows the peak is somewhere else now at
least in some cases.
2014-01-29 09:25:31 +00:00
|
|
|
for (ftype = 0; ftype < SENSORS_FEATURE_MAX; ftype++)
|
|
|
|
free(all_types[ftype].sf);
|
2007-06-28 14:39:34 +00:00
|
|
|
return 0;
|
2007-04-09 14:09:21 +00:00
|
|
|
}
|
|
|
|
|
2005-09-18 20:30:44 +00:00
|
|
|
/* returns !0 if sysfs filesystem was found, 0 otherwise */
|
|
|
|
int sensors_init_sysfs(void)
|
|
|
|
{
|
2012-01-31 12:54:03 +00:00
|
|
|
struct statfs statfsbuf;
|
2005-09-18 20:30:44 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
snprintf(sensors_sysfs_mount, NAME_MAX, "%s", "/sys");
|
2012-01-31 12:54:03 +00:00
|
|
|
if (statfs(sensors_sysfs_mount, &statfsbuf) < 0
|
|
|
|
|| statfsbuf.f_type != SYSFS_MAGIC)
|
2007-06-25 13:58:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
2005-09-18 20:30:44 +00:00
|
|
|
}
|
2005-09-18 20:37:42 +00:00
|
|
|
|
2008-01-05 15:20:13 +00:00
|
|
|
/* returns: number of devices added (0 or 1) if successful, <0 otherwise */
|
|
|
|
static int sensors_read_one_sysfs_chip(const char *dev_path,
|
|
|
|
const char *dev_name,
|
|
|
|
const char *hwmon_path)
|
2005-09-18 20:38:19 +00:00
|
|
|
{
|
2009-10-18 12:05:16 +00:00
|
|
|
int domain, bus, slot, fn, vendor, product, id;
|
2007-09-23 13:53:11 +00:00
|
|
|
int err = -SENSORS_ERR_KERNEL;
|
2007-12-10 13:30:22 +00:00
|
|
|
char *bus_attr;
|
|
|
|
char bus_path[NAME_MAX];
|
2008-01-05 12:31:55 +00:00
|
|
|
char linkpath[NAME_MAX];
|
|
|
|
char subsys_path[NAME_MAX], *subsys;
|
|
|
|
int sub_len;
|
2007-06-28 14:39:34 +00:00
|
|
|
sensors_chip_features entry;
|
2005-09-18 20:38:19 +00:00
|
|
|
|
|
|
|
/* ignore any device without name attribute */
|
2008-01-05 15:20:13 +00:00
|
|
|
if (!(entry.chip.prefix = sysfs_read_attr(hwmon_path, "name")))
|
2005-09-18 20:38:19 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-01-05 15:20:13 +00:00
|
|
|
entry.chip.path = strdup(hwmon_path);
|
2007-08-16 09:56:07 +00:00
|
|
|
if (!entry.chip.path)
|
2008-03-05 07:44:16 +00:00
|
|
|
sensors_fatal_error(__func__, "Out of memory");
|
2005-09-18 20:38:19 +00:00
|
|
|
|
2008-04-14 15:27:59 +00:00
|
|
|
if (dev_path == NULL) {
|
|
|
|
/* Virtual device */
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_VIRTUAL;
|
|
|
|
entry.chip.bus.nr = 0;
|
|
|
|
/* For now we assume that virtual devices are unique */
|
|
|
|
entry.chip.addr = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2008-01-05 12:31:55 +00:00
|
|
|
/* Find bus type */
|
|
|
|
snprintf(linkpath, NAME_MAX, "%s/subsystem", dev_path);
|
|
|
|
sub_len = readlink(linkpath, subsys_path, NAME_MAX - 1);
|
|
|
|
if (sub_len < 0 && errno == ENOENT) {
|
|
|
|
/* Fallback to "bus" link for kernels <= 2.6.17 */
|
|
|
|
snprintf(linkpath, NAME_MAX, "%s/bus", dev_path);
|
|
|
|
sub_len = readlink(linkpath, subsys_path, NAME_MAX - 1);
|
|
|
|
}
|
|
|
|
if (sub_len < 0) {
|
|
|
|
/* Older kernels (<= 2.6.11) have neither the subsystem
|
|
|
|
symlink nor the bus symlink */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
subsys = NULL;
|
|
|
|
else
|
|
|
|
goto exit_free;
|
|
|
|
} else {
|
|
|
|
subsys_path[sub_len] = '\0';
|
|
|
|
subsys = strrchr(subsys_path, '/') + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!subsys || !strcmp(subsys, "i2c")) &&
|
|
|
|
sscanf(dev_name, "%hd-%x", &entry.chip.bus.nr,
|
|
|
|
&entry.chip.addr) == 2) {
|
2006-01-09 19:55:18 +00:00
|
|
|
/* find out if legacy ISA or not */
|
2007-08-19 15:03:50 +00:00
|
|
|
if (entry.chip.bus.nr == 9191) {
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
|
|
|
|
entry.chip.bus.nr = 0;
|
|
|
|
} else {
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_I2C;
|
2007-04-03 12:55:30 +00:00
|
|
|
snprintf(bus_path, sizeof(bus_path),
|
2007-12-10 13:30:22 +00:00
|
|
|
"%s/class/i2c-adapter/i2c-%d/device",
|
2007-08-19 15:03:50 +00:00
|
|
|
sensors_sysfs_mount, entry.chip.bus.nr);
|
2006-01-09 19:55:18 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
if ((bus_attr = sysfs_read_attr(bus_path, "name"))) {
|
|
|
|
if (!strncmp(bus_attr, "ISA ", 4)) {
|
2007-08-19 15:03:50 +00:00
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
|
|
|
|
entry.chip.bus.nr = 0;
|
|
|
|
}
|
2006-01-09 19:55:18 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
free(bus_attr);
|
2007-04-03 12:55:30 +00:00
|
|
|
}
|
2006-01-09 19:55:18 +00:00
|
|
|
}
|
2008-01-05 12:31:55 +00:00
|
|
|
} else
|
|
|
|
if ((!subsys || !strcmp(subsys, "spi")) &&
|
|
|
|
sscanf(dev_name, "spi%hd.%d", &entry.chip.bus.nr,
|
|
|
|
&entry.chip.addr) == 2) {
|
2007-08-19 15:07:05 +00:00
|
|
|
/* SPI */
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_SPI;
|
2008-01-05 12:31:55 +00:00
|
|
|
} else
|
|
|
|
if ((!subsys || !strcmp(subsys, "pci")) &&
|
|
|
|
sscanf(dev_name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn) == 4) {
|
2006-08-19 15:23:10 +00:00
|
|
|
/* PCI */
|
2007-06-28 14:39:34 +00:00
|
|
|
entry.chip.addr = (domain << 16) + (bus << 8) + (slot << 3) + fn;
|
2007-08-19 15:03:50 +00:00
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_PCI;
|
|
|
|
entry.chip.bus.nr = 0;
|
2008-03-16 12:51:18 +00:00
|
|
|
} else
|
2008-04-10 23:24:57 +00:00
|
|
|
if ((!subsys || !strcmp(subsys, "platform") ||
|
|
|
|
!strcmp(subsys, "of_platform"))) {
|
2008-03-16 12:51:18 +00:00
|
|
|
/* must be new ISA (platform driver) */
|
|
|
|
if (sscanf(dev_name, "%*[a-z0-9_].%d", &entry.chip.addr) != 1)
|
|
|
|
entry.chip.addr = 0;
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_ISA;
|
|
|
|
entry.chip.bus.nr = 0;
|
2009-01-13 08:06:32 +00:00
|
|
|
} else if (subsys && !strcmp(subsys, "acpi")) {
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_ACPI;
|
|
|
|
/* For now we assume that acpi devices are unique */
|
|
|
|
entry.chip.bus.nr = 0;
|
|
|
|
entry.chip.addr = 0;
|
2009-10-18 12:05:16 +00:00
|
|
|
} else
|
|
|
|
if (subsys && !strcmp(subsys, "hid") &&
|
|
|
|
sscanf(dev_name, "%x:%x:%x.%x", &bus, &vendor, &product, &id) == 4) {
|
|
|
|
entry.chip.bus.type = SENSORS_BUS_TYPE_HID;
|
|
|
|
/* As of kernel 2.6.32, the hid device names don't look good */
|
|
|
|
entry.chip.bus.nr = bus;
|
|
|
|
entry.chip.addr = id;
|
2007-09-24 15:22:01 +00:00
|
|
|
} else {
|
2008-01-05 12:31:55 +00:00
|
|
|
/* Ignore unknown device */
|
|
|
|
err = 0;
|
|
|
|
goto exit_free;
|
2007-09-24 15:22:01 +00:00
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2008-04-14 15:27:59 +00:00
|
|
|
done:
|
2008-01-05 15:20:13 +00:00
|
|
|
if (sensors_read_dynamic_chip(&entry, hwmon_path) < 0)
|
2007-07-21 08:44:31 +00:00
|
|
|
goto exit_free;
|
2007-09-23 12:07:53 +00:00
|
|
|
if (!entry.subfeature) { /* No subfeature, discard chip */
|
2007-07-21 09:28:12 +00:00
|
|
|
err = 0;
|
|
|
|
goto exit_free;
|
|
|
|
}
|
2007-06-28 14:39:34 +00:00
|
|
|
sensors_add_proc_chips(&entry);
|
2005-09-18 20:38:19 +00:00
|
|
|
|
2008-01-05 15:20:13 +00:00
|
|
|
return 1;
|
2007-07-21 08:44:31 +00:00
|
|
|
|
|
|
|
exit_free:
|
|
|
|
free(entry.chip.prefix);
|
2007-08-16 09:56:07 +00:00
|
|
|
free(entry.chip.path);
|
2007-07-21 09:28:12 +00:00
|
|
|
return err;
|
2005-09-18 20:38:19 +00:00
|
|
|
}
|
|
|
|
|
2008-01-05 15:20:13 +00:00
|
|
|
static int sensors_add_hwmon_device_compat(const char *path,
|
|
|
|
const char *dev_name)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = sensors_read_one_sysfs_chip(path, dev_name, path);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-18 20:38:19 +00:00
|
|
|
/* returns 0 if successful, !0 otherwise */
|
|
|
|
static int sensors_read_sysfs_chips_compat(void)
|
|
|
|
{
|
2007-12-10 13:30:22 +00:00
|
|
|
int ret;
|
2005-09-18 20:38:19 +00:00
|
|
|
|
2008-01-05 15:20:13 +00:00
|
|
|
ret = sysfs_foreach_busdev("i2c", sensors_add_hwmon_device_compat);
|
2007-12-10 13:30:22 +00:00
|
|
|
if (ret && ret != ENOENT)
|
|
|
|
return -SENSORS_ERR_KERNEL;
|
2005-09-18 20:38:19 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-09-18 20:38:19 +00:00
|
|
|
|
2007-12-16 19:37:11 +00:00
|
|
|
static int sensors_add_hwmon_device(const char *path, const char *classdev)
|
2007-12-10 13:30:22 +00:00
|
|
|
{
|
2007-12-16 19:37:11 +00:00
|
|
|
char linkpath[NAME_MAX];
|
2008-01-05 15:20:13 +00:00
|
|
|
char device[NAME_MAX], *device_p;
|
|
|
|
int dev_len, err;
|
2007-12-10 13:30:22 +00:00
|
|
|
(void)classdev; /* hide warning */
|
|
|
|
|
2007-12-16 19:37:11 +00:00
|
|
|
snprintf(linkpath, NAME_MAX, "%s/device", path);
|
|
|
|
dev_len = readlink(linkpath, device, NAME_MAX - 1);
|
2008-04-14 15:27:59 +00:00
|
|
|
if (dev_len < 0) {
|
|
|
|
/* No device link? Treat as virtual */
|
|
|
|
err = sensors_read_one_sysfs_chip(NULL, NULL, path);
|
|
|
|
} else {
|
|
|
|
device[dev_len] = '\0';
|
|
|
|
device_p = strrchr(device, '/') + 1;
|
|
|
|
|
|
|
|
/* The attributes we want might be those of the hwmon class
|
|
|
|
device, or those of the device itself. */
|
|
|
|
err = sensors_read_one_sysfs_chip(linkpath, device_p, path);
|
|
|
|
if (err == 0)
|
|
|
|
err = sensors_read_one_sysfs_chip(linkpath, device_p,
|
|
|
|
linkpath);
|
|
|
|
}
|
2008-01-05 15:20:13 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
2005-09-18 20:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 0 if successful, !0 otherwise */
|
|
|
|
int sensors_read_sysfs_chips(void)
|
|
|
|
{
|
2007-12-10 13:30:22 +00:00
|
|
|
int ret;
|
2005-09-18 20:38:19 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
ret = sysfs_foreach_classdev("hwmon", sensors_add_hwmon_device);
|
|
|
|
if (ret == ENOENT) {
|
2005-09-18 20:38:19 +00:00
|
|
|
/* compatibility function for kernel 2.6.n where n <= 13 */
|
|
|
|
return sensors_read_sysfs_chips_compat();
|
|
|
|
}
|
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
if (ret > 0)
|
|
|
|
ret = -SENSORS_ERR_KERNEL;
|
2005-09-18 20:38:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-18 20:37:42 +00:00
|
|
|
/* returns 0 if successful, !0 otherwise */
|
2007-12-16 19:37:11 +00:00
|
|
|
static int sensors_add_i2c_bus(const char *path, const char *classdev)
|
2005-09-18 20:37:42 +00:00
|
|
|
{
|
|
|
|
sensors_bus entry;
|
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
if (sscanf(classdev, "i2c-%hd", &entry.bus.nr) != 1 ||
|
|
|
|
entry.bus.nr == 9191) /* legacy ISA */
|
|
|
|
return 0;
|
|
|
|
entry.bus.type = SENSORS_BUS_TYPE_I2C;
|
|
|
|
|
|
|
|
/* Get the adapter name from the classdev "name" attribute
|
|
|
|
* (Linux 2.6.20 and later). If it fails, fall back to
|
|
|
|
* the device "name" attribute (for older kernels). */
|
|
|
|
entry.adapter = sysfs_read_attr(path, "name");
|
|
|
|
if (!entry.adapter)
|
|
|
|
entry.adapter = sysfs_read_attr(path, "device/name");
|
|
|
|
if (entry.adapter)
|
|
|
|
sensors_add_proc_bus(&entry);
|
2007-08-16 09:57:56 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-09-18 20:37:42 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
/* returns 0 if successful, !0 otherwise */
|
|
|
|
int sensors_read_sysfs_bus(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2005-09-18 20:37:42 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
ret = sysfs_foreach_classdev("i2c-adapter", sensors_add_i2c_bus);
|
2009-07-22 12:53:51 +00:00
|
|
|
if (ret == ENOENT)
|
|
|
|
ret = sysfs_foreach_busdev("i2c", sensors_add_i2c_bus);
|
2007-12-10 13:30:22 +00:00
|
|
|
if (ret && ret != ENOENT)
|
|
|
|
return -SENSORS_ERR_KERNEL;
|
2005-09-18 20:37:42 +00:00
|
|
|
|
2007-12-10 13:30:22 +00:00
|
|
|
return 0;
|
2005-09-18 20:37:42 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 12:20:07 +00:00
|
|
|
int sensors_read_sysfs_attr(const sensors_chip_name *name,
|
|
|
|
const sensors_subfeature *subfeature,
|
2007-08-13 20:16:42 +00:00
|
|
|
double *value)
|
2007-07-05 15:03:22 +00:00
|
|
|
{
|
|
|
|
char n[NAME_MAX];
|
|
|
|
FILE *f;
|
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
snprintf(n, NAME_MAX, "%s/%s", name->path, subfeature->name);
|
2007-07-05 15:03:22 +00:00
|
|
|
if ((f = fopen(n, "r"))) {
|
2007-09-29 18:48:20 +00:00
|
|
|
int res, err = 0;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
res = fscanf(f, "%lf", value);
|
|
|
|
if (res == EOF && errno == EIO)
|
|
|
|
err = -SENSORS_ERR_IO;
|
|
|
|
else if (res != 1)
|
|
|
|
err = -SENSORS_ERR_ACCESS_R;
|
|
|
|
res = fclose(f);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (res == EOF) {
|
|
|
|
if (errno == EIO)
|
|
|
|
return -SENSORS_ERR_IO;
|
|
|
|
else
|
|
|
|
return -SENSORS_ERR_ACCESS_R;
|
|
|
|
}
|
2007-09-23 12:07:53 +00:00
|
|
|
*value /= get_type_scaling(subfeature->type);
|
2007-07-05 15:03:22 +00:00
|
|
|
} else
|
2007-09-23 13:53:11 +00:00
|
|
|
return -SENSORS_ERR_KERNEL;
|
2007-07-05 15:03:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-09-23 12:20:07 +00:00
|
|
|
int sensors_write_sysfs_attr(const sensors_chip_name *name,
|
|
|
|
const sensors_subfeature *subfeature,
|
2007-08-13 20:16:42 +00:00
|
|
|
double value)
|
2007-07-05 15:03:22 +00:00
|
|
|
{
|
|
|
|
char n[NAME_MAX];
|
|
|
|
FILE *f;
|
2007-08-31 14:42:20 +00:00
|
|
|
|
2007-09-23 12:16:50 +00:00
|
|
|
snprintf(n, NAME_MAX, "%s/%s", name->path, subfeature->name);
|
2007-07-05 15:03:22 +00:00
|
|
|
if ((f = fopen(n, "w"))) {
|
2007-09-29 18:48:20 +00:00
|
|
|
int res, err = 0;
|
|
|
|
|
2007-09-23 12:07:53 +00:00
|
|
|
value *= get_type_scaling(subfeature->type);
|
2007-09-29 11:57:37 +00:00
|
|
|
res = fprintf(f, "%d", (int) value);
|
2007-09-29 18:48:20 +00:00
|
|
|
if (res == -EIO)
|
|
|
|
err = -SENSORS_ERR_IO;
|
|
|
|
else if (res < 0)
|
|
|
|
err = -SENSORS_ERR_ACCESS_W;
|
|
|
|
res = fclose(f);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (res == EOF) {
|
|
|
|
if (errno == EIO)
|
|
|
|
return -SENSORS_ERR_IO;
|
|
|
|
else
|
|
|
|
return -SENSORS_ERR_ACCESS_W;
|
|
|
|
}
|
2007-07-05 15:03:22 +00:00
|
|
|
} else
|
2007-09-23 13:53:11 +00:00
|
|
|
return -SENSORS_ERR_KERNEL;
|
2007-07-05 15:03:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|