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,73 +1208,68 @@ 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;
if (status != SIGAR_OK) { status = sigar_file_system_list_get(sigar, &fslist);
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[fsdev] file_system_list failed: %s",
sigar_strerror(sigar, status));
return NULL;
}
for (i=0; i<fslist.number; i++) { if (status != SIGAR_OK) {
sigar_file_system_t *fsp = &fslist.data[i]; sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[fsdev] file_system_list failed: %s",
sigar_strerror(sigar, status));
return NULL;
}
if (fsp->type == SIGAR_FSTYPE_LOCAL_DISK) { for (i=0; i<fslist.number; i++) {
int retval = stat(fsp->dir_name, &sb); sigar_file_system_t *fsp = &fslist.data[i];
sigar_cache_entry_t *ent;
char *ptr;
if (retval < 0) { if (fsp->type == SIGAR_FSTYPE_LOCAL_DISK) {
if (debug) { int retval = stat(fsp->dir_name, &sb);
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_cache_entry_t *ent;
"[fsdev] inode stat(%s) failed",
fsp->dir_name); if (retval < 0) {
} if (debug) {
return NULL; /* cant cache w/o inode */ sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[fsdev] inode stat(%s) failed",
fsp->dir_name);
} }
return NULL; /* cant cache w/o inode */
}
ent = sigar_cache_get(sigar->fsdev, FSDEV_ID(sb)); ent = sigar_cache_get(sigar->fsdev, FSDEV_ID(sb));
if (ent->value) { if (ent->value) {
continue; /* already cached */ continue; /* already cached */
}
if (FSDEV_IS_DEV(fsp->dev_name)) {
iodev_t *iodev;
ent->value = iodev = malloc(sizeof(*iodev));
SIGAR_ZERO(iodev);
SIGAR_SSTRCPY(iodev->name, fsp->dev_name);
if (debug) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[fsdev] map %s -> %s",
fsp->dir_name, iodev->name);
} }
ptr = fsp->dev_name;
if (FSDEV_IS_DEV(ptr)) {
ent->value = sigar_strdup(ptr);
if (debug) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[fsdev] map %s -> %s",
fsp->dir_name, (char*)ent->value);
}
continue;
}
ent->value = FSDEV_NONE;
} }
} }
sigar_file_system_list_destroy(sigar, &fslist);
} }
if (entry->value == FSDEV_NONE) { sigar_file_system_list_destroy(sigar, &fslist);
return NULL;
} if (entry->value &&
else if (entry->value == NULL) { (((iodev_t *)entry->value)->name[0] != '\0'))
entry->value = FSDEV_NONE; {
return NULL; return (iodev_t *)entry->value;
} }
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"))) {