From 2f4c52cc49de8d68534b67a989245b5b268f8116 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Mon, 27 Jul 2020 09:27:32 +0200 Subject: [PATCH] Make device name consistent between sar and iostat Use a common function to determine the name of the device to be displayed by sar (sar -d) and iostat. Signed-off-by: Sebastien GODARD --- common.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ common.h | 3 ++ iostat.c | 50 +++-------------- json_stats.c | 5 +- pcp_stats.c | 5 +- pr_stats.c | 4 +- raw_stats.c | 5 +- rndr_stats.c | 5 +- sa_common.c | 122 ----------------------------------------- sadf_misc.c | 5 +- sar.c | 5 -- svg_stats.c | 5 +- xml_stats.c | 5 +- 13 files changed, 184 insertions(+), 184 deletions(-) diff --git a/common.c b/common.c index 93e84567..8b2ab86f 100644 --- a/common.c +++ b/common.c @@ -36,6 +36,7 @@ #include "version.h" #include "common.h" +#include "ioconf.h" #ifdef USE_NLS #include @@ -1048,6 +1049,154 @@ char *get_pretty_name_from_persistent(char *persistent) return pretty; } +/* + * ************************************************************************** + * Try to get device real name from sysfs tree. + * + * IN: + * @major Major number of the device. + * @minor Minor number of the device. + * + * RETURNS: + * The name of the device, which may be the real name (as it appears in /dev) + * or NULL. + *************************************************************************** + */ +char *get_devname_from_sysfs(unsigned int major, unsigned int minor) +{ + static char link[256], target[PATH_MAX]; + char *devname; + ssize_t r; + + snprintf(link, 256, "%s/%u:%u", SYSFS_DEV_BLOCK, major, minor); + + /* Get full path to device knowing its major and minor numbers */ + r = readlink(link, target, PATH_MAX); + if (r <= 0 || r >= PATH_MAX) { + return (NULL); + } + + target[r] = '\0'; + + /* Get device name */ + devname = basename(target); + if (!devname || strnlen(devname, FILENAME_MAX) == 0) { + return (NULL); + } + + return (devname); +} + +/* + * ************************************************************************** + * Get device real name if possible. + * + * IN: + * @major Major number of the device. + * @minor Minor number of the device. + * + * RETURNS: + * The name of the device, which may be the real name (as it appears in /dev) + * or a string with the following format devM-n. + *************************************************************************** + */ +char *get_devname(unsigned int major, unsigned int minor) +{ + static char buf[32]; + char *name; + + name = get_devname_from_sysfs(major, minor); + if (name != NULL) + return (name); + + name = ioc_name(major, minor); + if ((name != NULL) && strcmp(name, K_NODEV)) + return (name); + + snprintf(buf, 32, "dev%u-%u", major, minor); + return (buf); +} + +/* + * ************************************************************************** + * Get device name (whether pretty-printed, persistent or not). + * + * IN: + * @major Major number of the device. + * @minor Minor number of the device. + * @wwn WWN identifier of the device (0 if unknown). + * @part_nr Partition number (0 if unknown). + * @disp_devmap_name Display device mapper name. + * @disp_persist_name Display persistent name of the device. + * @use_stable_id Display stable-across-reboots name. + * @dflt_name Device name to use by default (if existent). + * + * RETURNS: + * The name of the device. + *************************************************************************** + */ +char *get_device_name(unsigned int major, unsigned int minor, unsigned long long wwn[], + unsigned int part_nr, unsigned int disp_devmap_name, + unsigned int disp_persist_name, unsigned int use_stable_id, + char *dflt_name) +{ + static unsigned int dm_major = 0; + char *dev_name = NULL, *persist_dev_name = NULL, *bang; + static char sid[64], dname[MAX_NAME_LEN]; + char xsid[32] = "", pn[16] = ""; + + if (disp_persist_name) { + persist_dev_name = get_persistent_name_from_pretty(get_devname(major, minor)); + } + + if (persist_dev_name) { + dev_name = persist_dev_name; + } + else { + if (use_stable_id && (wwn[0] != 0)) { + if (wwn[1] != 0) { + sprintf(xsid, "%016llx", wwn[1]); + } + if (part_nr) { + sprintf(pn, "-%d", part_nr); + } + snprintf(sid, sizeof(sid), "%#016llx%s%s", wwn[0], xsid, pn); + dev_name = sid; + } + else if (disp_devmap_name) { + if (!dm_major) { + dm_major = get_devmap_major(); + } + if (major == dm_major) { + dev_name = transform_devmapname(major, minor); + } + } + + if (!dev_name) { + if (dflt_name) { + dev_name = dflt_name; + } + else { + dev_name = get_devname(major, minor); + } + } + } + + strncpy(dname, dev_name, sizeof(dname)); + dname[sizeof(dname) - 1] = '\0'; + + while ((bang = strchr(dname, '!'))) { + /* + * Some devices may have had a slash replaced with + * a bang character (eg. cciss!c0d0...) + * Restore their original names. + */ + *bang = '/'; + } + + return dname; +} + /* *************************************************************************** * Init color strings. diff --git a/common.h b/common.h index 0119df74..a9902ee1 100644 --- a/common.h +++ b/common.h @@ -273,6 +273,9 @@ void cprintf_x (int, int, ...); char *device_name (char *); +char *get_device_name + (unsigned int, unsigned int, unsigned long long [], + unsigned int, unsigned int, unsigned int, unsigned int, char *); unsigned int get_devmap_major (void); unsigned long long get_interval diff --git a/iostat.c b/iostat.c index cc2bd659..0e2fc125 100644 --- a/iostat.c +++ b/iostat.c @@ -67,7 +67,6 @@ int dplaces_nr = -1; int group_nr = 0; /* Nb of device groups */ int cpu_nr = 0; /* Nb of processors on the machine */ int flags = 0; /* Flag for common options and system state */ -unsigned int dm_major; /* Device-mapper major number */ long interval = 0; char timestamp[TIMESTAMP_LEN]; @@ -1709,7 +1708,7 @@ void write_stats(int curr, struct tm *rectime, int skip) int h, hl = 0, hh = 0, fctr = 1, tab = 4, next = FALSE; unsigned long long itv; struct io_device *d, *dtmp, *g = NULL, *dnext = NULL; - char *dev_name, *pdname, *bang, dname[MAX_NAME_LEN]; + char *dev_name, *pdname; /* Test stdout */ TEST_STDOUT(STDOUT_FILENO); @@ -1835,39 +1834,10 @@ void write_stats(int curr, struct tm *rectime, int skip) ioj = &iozero; } - /* Get device name to print */ - dev_name = NULL; - - if ((DISPLAY_DEVMAP_NAME(flags)) && (d->major == dm_major)) { - /* - * If the device is a device mapper device, try to get its - * assigned name of its logical device. - */ - dev_name = transform_devmapname(d->major, d->minor); - } - - if (DISPLAY_PERSIST_NAME_I(flags)) { - pdname = get_persistent_name_from_pretty(dev_name ? dev_name : d->name); - if (pdname) { - dev_name = pdname; - } - } - - if (!dev_name) { - dev_name = d->name; - } - strncpy(dname, dev_name, sizeof(dname)); - dname[sizeof(dname) - 1] = '\0'; - - while ((bang = strchr(dname, '!'))) { - /* - * Some devices may have had a slash replaced with - * a bang character (eg. cciss!c0d0...) - * Restore their original names. - */ - *bang = '/'; - } - + dev_name = get_device_name(d->major, d->minor, NULL, 0, + DISPLAY_DEVMAP_NAME(flags), + DISPLAY_PERSIST_NAME_I(flags), + FALSE, d->name); #ifdef DEBUG if (DISPLAY_DEBUG(flags)) { /* Debug output */ @@ -1880,7 +1850,7 @@ void write_stats(int curr, struct tm *rectime, int skip) "fl_ios=%lu fl_ticks=%u " "ios_pgr=%u tot_ticks=%u " "rq_ticks=%u }\n", - dname, + dev_name, itv, fctr, ioi->rd_sectors, @@ -1910,10 +1880,10 @@ void write_stats(int curr, struct tm *rectime, int skip) next = TRUE; if (DISPLAY_EXTENDED(flags)) { - write_ext_stat(itv, fctr, h, d, ioi, ioj, tab, dname); + write_ext_stat(itv, fctr, h, d, ioi, ioj, tab, dev_name); } else { - write_basic_stat(itv, fctr, d, ioi, ioj, tab, dname); + write_basic_stat(itv, fctr, d, ioi, ioj, tab, dev_name); } } } @@ -2349,10 +2319,6 @@ int main(int argc, char **argv) /* Select disk output unit (kB/s or blocks/s) */ set_disk_output_unit(); - if (DISPLAY_DEVMAP_NAME(flags)) { - dm_major = get_devmap_major(); - } - if (DISPLAY_JSON_OUTPUT(flags)) { /* Use a decimal point to make JSON code compliant with RFC7159 */ setlocale(LC_NUMERIC, "C"); diff --git a/json_stats.c b/json_stats.c index ac7bbbf8..d4861ce3 100644 --- a/json_stats.c +++ b/json_stats.c @@ -762,8 +762,9 @@ __print_funct_t json_print_disk_stats(struct activity *a, int curr, int tab, } /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, - sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */ diff --git a/pcp_stats.c b/pcp_stats.c index e4e365b8..002fca4a 100644 --- a/pcp_stats.c +++ b/pcp_stats.c @@ -610,8 +610,9 @@ __print_funct_t pcp_print_disk_stats(struct activity *a, int curr, unsigned long } /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, - sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */ diff --git a/pr_stats.c b/pr_stats.c index e34575ec..cf61f61e 100644 --- a/pr_stats.c +++ b/pr_stats.c @@ -1049,7 +1049,9 @@ __print_funct_t print_disk_stats(struct activity *a, int prev, int curr, continue; /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */ diff --git a/raw_stats.c b/raw_stats.c index 4dd41034..189e3240 100644 --- a/raw_stats.c +++ b/raw_stats.c @@ -537,8 +537,9 @@ __print_funct_t raw_print_disk_stats(struct activity *a, char *timestr, int curr sdc = (struct stats_disk *) ((char *) a->buf[curr] + i * a->msize); /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, - sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */ diff --git a/rndr_stats.c b/rndr_stats.c index 4b2cf55b..f4d0eb6e 100644 --- a/rndr_stats.c +++ b/rndr_stats.c @@ -1091,8 +1091,9 @@ __print_funct_t render_disk_stats(struct activity *a, int isdb, char *pre, } /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, - sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */ diff --git a/sa_common.c b/sa_common.c index d040f507..eb32a91b 100644 --- a/sa_common.c +++ b/sa_common.c @@ -525,75 +525,6 @@ void reallocate_all_buffers(struct activity *a, __nr_t nr_min) a->nr_allocated = nr_realloc; } -/* - *************************************************************************** - * Try to get device real name from sysfs tree. - * - * IN: - * @major Major number of the device. - * @minor Minor number of the device. - * - * RETURNS: - * The name of the device, which may be the real name (as it appears in /dev) - * or NULL. - *************************************************************************** - */ -char *get_devname_from_sysfs(unsigned int major, unsigned int minor) -{ - static char link[256], target[PATH_MAX]; - char *devname; - ssize_t r; - - snprintf(link, 256, "%s/%u:%u", SYSFS_DEV_BLOCK, major, minor); - - /* Get full path to device knowing its major and minor numbers */ - r = readlink(link, target, PATH_MAX); - if (r <= 0 || r >= PATH_MAX) { - return (NULL); - } - - target[r] = '\0'; - - /* Get device name */ - devname = basename(target); - if (!devname || strnlen(devname, FILENAME_MAX) == 0) { - return (NULL); - } - - return (devname); -} - -/* - *************************************************************************** - * Get device real name if possible. - * - * IN: - * @major Major number of the device. - * @minor Minor number of the device. - * - * RETURNS: - * The name of the device, which may be the real name (as it appears in /dev) - * or a string with the following format devM-n. - *************************************************************************** - */ -char *get_devname(unsigned int major, unsigned int minor) -{ - static char buf[32]; - char *name; - - snprintf(buf, 32, "dev%u-%u", major, minor); - - name = get_devname_from_sysfs(major, minor); - if (name != NULL) - return (name); - - name = ioc_name(major, minor); - if ((name != NULL) && strcmp(name, K_NODEV)) - return (name); - - return (buf); -} - /* *************************************************************************** * Check if we are close enough to desired interval. @@ -3376,57 +3307,4 @@ void get_global_soft_statistics(struct activity *a, int prev, int curr, ssnp_all->flow_limit += ssnp->flow_limit; } } - -/* - *************************************************************************** - * Get device name (whether pretty-printed, persistent or not). - * - * IN: - * @major Major number of the device. - * @minor Minor number of the device. - * @wwn WWN identifier of the device (0 if unknown). - * @part_nr Partition number (0 if unknown). - * @flags Flags for common options and system state. - * - * RETURNS: - * The name of the device. - *************************************************************************** - */ -char *get_sa_devname(unsigned int major, unsigned int minor, unsigned long long wwn[], - unsigned int part_nr, uint64_t flags) -{ - char *dev_name = NULL, *persist_dev_name = NULL; - static char sid[64]; - char xsid[32] = "", pn[16] = ""; - - if (DISPLAY_PERSIST_NAME_S(flags)) { - persist_dev_name = get_persistent_name_from_pretty(get_devname(major, minor)); - } - - if (persist_dev_name) { - dev_name = persist_dev_name; - } - else { - if ((USE_STABLE_ID(flags)) && (wwn[0] != 0)) { - if (wwn[1] != 0) { - sprintf(xsid, "%016llx", wwn[1]); - } - if (part_nr) { - sprintf(pn, "-%d", part_nr); - } - snprintf(sid, sizeof(sid), "%#016llx%s%s", wwn[0], xsid, pn); - dev_name = sid; - } - else if ((DISPLAY_PRETTY(flags)) && (major == dm_major)) { - dev_name = transform_devmapname(major, minor); - } - - if (!dev_name) { - dev_name = get_devname(major, minor); - } - } - - return dev_name; -} - #endif /* SOURCE_SADC undefined */ diff --git a/sadf_misc.c b/sadf_misc.c index c07465f3..de231093 100644 --- a/sadf_misc.c +++ b/sadf_misc.c @@ -1599,8 +1599,9 @@ __nr_t count_new_disk(struct activity *a, int curr) sdc = (struct stats_disk *) ((char *) a->buf[curr] + i * a->msize); nr += add_list_item(&(a->item_list), - get_sa_devname(sdc->major, sdc->minor, sdc->wwn, - sdc->part_nr, flags), + get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL), MAX_DEV_LEN); } diff --git a/sar.c b/sar.c index f7e1ae97..30521414 100644 --- a/sar.c +++ b/sar.c @@ -63,7 +63,6 @@ int arch_64 = FALSE; int dplaces_nr = -1; uint64_t flags = 0; -unsigned int dm_major; /* Device-mapper major number */ char timestamp[2][TIMESTAMP_LEN]; extern unsigned int rec_types_nr[]; @@ -1569,10 +1568,6 @@ int main(int argc, char **argv) usage(argv[0]); } - if (DISPLAY_PRETTY(flags)) { - dm_major = get_devmap_major(); - } - if (!count) { /* * count parameter not set: Display all the contents of the file diff --git a/svg_stats.c b/svg_stats.c index 4a38d6a5..987e737e 100644 --- a/svg_stats.c +++ b/svg_stats.c @@ -2126,8 +2126,9 @@ __print_funct_t svg_print_disk_stats(struct activity *a, int curr, int action, s sdc = (struct stats_disk *) ((char *) a->buf[curr] + i * a->msize); /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, - sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */ diff --git a/xml_stats.c b/xml_stats.c index a7097c59..2e0753c5 100644 --- a/xml_stats.c +++ b/xml_stats.c @@ -745,8 +745,9 @@ __print_funct_t xml_print_disk_stats(struct activity *a, int curr, int tab, } /* Get device name */ - dev_name = get_sa_devname(sdc->major, sdc->minor, - sdc->wwn, sdc->part_nr, flags); + dev_name = get_device_name(sdc->major, sdc->minor, sdc->wwn, sdc->part_nr, + DISPLAY_PRETTY(flags), DISPLAY_PERSIST_NAME_S(flags), + USE_STABLE_ID(flags), NULL); if (a->item_list != NULL) { /* A list of devices has been entered on the command line */