refactor get_fsdev for future expansion

This commit is contained in:
Doug MacEachern 2007-09-27 02:32:48 +00:00
parent b9b8a326f1
commit 53a95a5de5
1 changed files with 68 additions and 72 deletions

View File

@ -1174,8 +1174,6 @@ int sigar_file_system_list_get(sigar_t *sigar,
return SIGAR_OK; return SIGAR_OK;
} }
#define FSDEV_NONE "__NONE__"
#define FSDEV_ID(sb) (sb.st_ino + sb.st_dev) #define FSDEV_ID(sb) (sb.st_ino + sb.st_dev)
#define FSDEV_IS_DEV(dev) strnEQ(dev, "/dev/", 5) #define FSDEV_IS_DEV(dev) strnEQ(dev, "/dev/", 5)
@ -1183,20 +1181,18 @@ int sigar_file_system_list_get(sigar_t *sigar,
#define ST_MAJOR(sb) major((sb).st_rdev) #define ST_MAJOR(sb) major((sb).st_rdev)
#define ST_MINOR(sb) minor((sb).st_rdev) #define ST_MINOR(sb) minor((sb).st_rdev)
static void fsdev_free(void *ptr) typedef struct {
{ char name[256];
if (ptr != FSDEV_NONE) { } iodev_t;
free(ptr);
}
}
static char *get_fsdev(sigar_t *sigar, static iodev_t *get_fsdev(sigar_t *sigar,
const char *dirname, const char *dirname)
char *fsdev)
{ {
sigar_cache_entry_t *entry; sigar_cache_entry_t *entry;
struct stat sb; struct stat sb;
sigar_uint64_t id; sigar_uint64_t id;
sigar_file_system_list_t fslist;
int i, status;
int debug = SIGAR_LOG_IS_DEBUG(sigar); int debug = SIGAR_LOG_IS_DEBUG(sigar);
if (stat(dirname, &sb) < 0) { if (stat(dirname, &sb) < 0) {
@ -1212,15 +1208,15 @@ static char *get_fsdev(sigar_t *sigar,
if (!sigar->fsdev) { if (!sigar->fsdev) {
sigar->fsdev = sigar_cache_new(15); sigar->fsdev = sigar_cache_new(15);
sigar->fsdev->free_value = fsdev_free;
} }
entry = sigar_cache_get(sigar->fsdev, id); entry = sigar_cache_get(sigar->fsdev, id);
if (entry->value == NULL) { if (entry->value != NULL) {
sigar_file_system_list_t fslist; return (iodev_t *)entry->value;
int status = sigar_file_system_list_get(sigar, &fslist); }
int i;
status = sigar_file_system_list_get(sigar, &fslist);
if (status != SIGAR_OK) { if (status != SIGAR_OK) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
@ -1235,7 +1231,6 @@ static char *get_fsdev(sigar_t *sigar,
if (fsp->type == SIGAR_FSTYPE_LOCAL_DISK) { if (fsp->type == SIGAR_FSTYPE_LOCAL_DISK) {
int retval = stat(fsp->dir_name, &sb); int retval = stat(fsp->dir_name, &sb);
sigar_cache_entry_t *ent; sigar_cache_entry_t *ent;
char *ptr;
if (retval < 0) { if (retval < 0) {
if (debug) { if (debug) {
@ -1251,34 +1246,30 @@ static char *get_fsdev(sigar_t *sigar,
continue; /* already cached */ continue; /* already cached */
} }
ptr = fsp->dev_name; if (FSDEV_IS_DEV(fsp->dev_name)) {
if (FSDEV_IS_DEV(ptr)) { iodev_t *iodev;
ent->value = sigar_strdup(ptr); ent->value = iodev = malloc(sizeof(*iodev));
SIGAR_ZERO(iodev);
SIGAR_SSTRCPY(iodev->name, fsp->dev_name);
if (debug) { if (debug) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[fsdev] map %s -> %s", "[fsdev] map %s -> %s",
fsp->dir_name, (char*)ent->value); fsp->dir_name, iodev->name);
} }
continue;
} }
ent->value = FSDEV_NONE;
} }
} }
sigar_file_system_list_destroy(sigar, &fslist); sigar_file_system_list_destroy(sigar, &fslist);
}
if (entry->value == FSDEV_NONE) { if (entry->value &&
return NULL; (((iodev_t *)entry->value)->name[0] != '\0'))
} {
else if (entry->value == NULL) { return (iodev_t *)entry->value;
entry->value = FSDEV_NONE;
return NULL;
} }
else { else {
strcpy(fsdev, (char*)entry->value); return NULL;
return fsdev;
} }
} }
@ -1289,13 +1280,14 @@ static int get_iostat_sys(sigar_t *sigar,
char stat[1025], dev[1025]; char stat[1025], dev[1025];
char *name, *ptr, *fsdev; char *name, *ptr, *fsdev;
int partition, status; int partition, status;
iodev_t *iodev;
name = fsdev = get_fsdev(sigar, dirname, dev); if (!(iodev = get_fsdev(sigar, dirname))) {
if (!name) {
return ENOENT; return ENOENT;
} }
name = fsdev = iodev->name;
if (FSDEV_IS_DEV(name)) { if (FSDEV_IS_DEV(name)) {
name += 5; /* strip "/dev/" */ name += 5; /* strip "/dev/" */
} }
@ -1333,22 +1325,24 @@ static int get_iostat_proc_dstat(sigar_t *sigar,
sigar_file_system_usage_t *fsusage) sigar_file_system_usage_t *fsusage)
{ {
FILE *fp; FILE *fp;
char buffer[1025], dev[1025]; char buffer[1025];
char *ptr; char *ptr;
struct stat sb; struct stat sb;
iodev_t *iodev;
if (!get_fsdev(sigar, dirname, dev)) { if (!(iodev = get_fsdev(sigar, dirname))) {
return ENOENT; return ENOENT;
} }
if (stat(dev, &sb) < 0) { if (stat(iodev->name, &sb) < 0) {
return errno; return errno;
} }
if (SIGAR_LOG_IS_DEBUG(sigar)) { if (SIGAR_LOG_IS_DEBUG(sigar)) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
PROC_DISKSTATS " %s -> %s [%d,%d]", PROC_DISKSTATS " %s -> %s [%d,%d]",
dirname, dev, ST_MAJOR(sb), ST_MINOR(sb)); dirname, iodev->name,
ST_MAJOR(sb), ST_MINOR(sb));
} }
if (!(fp = fopen(PROC_DISKSTATS, "r"))) { if (!(fp = fopen(PROC_DISKSTATS, "r"))) {
@ -1423,22 +1417,24 @@ static int get_iostat_procp(sigar_t *sigar,
sigar_file_system_usage_t *fsusage) sigar_file_system_usage_t *fsusage)
{ {
FILE *fp; FILE *fp;
char buffer[1025], dev[1025]; char buffer[1025];
char *ptr; char *ptr;
struct stat sb; struct stat sb;
iodev_t *iodev;
if (!get_fsdev(sigar, dirname, dev)) { if (!(iodev = get_fsdev(sigar, dirname))) {
return ENOENT; return ENOENT;
} }
if (stat(dev, &sb) < 0) { if (stat(iodev->name, &sb) < 0) {
return errno; return errno;
} }
if (SIGAR_LOG_IS_DEBUG(sigar)) { if (SIGAR_LOG_IS_DEBUG(sigar)) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
PROC_PARTITIONS " %s -> %s [%d,%d]", PROC_PARTITIONS " %s -> %s [%d,%d]",
dirname, dev, ST_MAJOR(sb), ST_MINOR(sb)); dirname, iodev->name,
ST_MAJOR(sb), ST_MINOR(sb));
} }
if (!(fp = fopen(PROC_PARTITIONS, "r"))) { if (!(fp = fopen(PROC_PARTITIONS, "r"))) {