]> granicus.if.org Git - sysstat/commitdiff
Make device name consistent between sar and iostat
authorSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 27 Jul 2020 07:27:32 +0000 (09:27 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 27 Jul 2020 07:27:32 +0000 (09:27 +0200)
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 <sysstat@users.noreply.github.com>
13 files changed:
common.c
common.h
iostat.c
json_stats.c
pcp_stats.c
pr_stats.c
raw_stats.c
rndr_stats.c
sa_common.c
sadf_misc.c
sar.c
svg_stats.c
xml_stats.c

index 93e84567f291f3f32baf3a22107994c488855b98..8b2ab86f923f5f8f714bc365d9cf76df9fbc53e9 100644 (file)
--- a/common.c
+++ b/common.c
@@ -36,6 +36,7 @@
 
 #include "version.h"
 #include "common.h"
+#include "ioconf.h"
 
 #ifdef USE_NLS
 #include <locale.h>
@@ -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.
index 0119df74f24a7ed0c08d9fd55a3fd5364303b802..a9902ee14937599f5d792bca3ad195b0ffaa4d52 100644 (file)
--- 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
index cc2bd65957f252b9f4306a091f2bea9c636c24f7..0e2fc1256d90cb88e5a8aac02549401a01f77a00 100644 (file)
--- 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");
index ac7bbbf8687c7a851234ac5833fdc06144662702..d4861ce356addbc56f0d70e5a0385acff95ce86f 100644 (file)
@@ -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 */
index e4e365b822702e2b3a74f07148e5b302100b01c6..002fca4afb2127549faa4c8b0306999f4162dd38 100644 (file)
@@ -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 */
index e34575ece5b1c2e29ade4836d8f3fc6cbca1c7ef..cf61f61e1ded146226ddb79868ca45f72b70afff 100644 (file)
@@ -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 */
index 4dd41034bfbbbf279d641c2948ec060b140f3db4..189e32400e5a30c86af9998b7534e41651f70c6f 100644 (file)
@@ -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 */
index 4b2cf55beebe71968ae886d7188f6045bd2cd945..f4d0eb6e427fc0316d4fb89961059d5189fc0470 100644 (file)
@@ -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 */
index d040f507632988e6d88bbc78ec711557f76cffa5..eb32a91bd04e7e43c67d3d942db9c590aab2560a 100644 (file)
@@ -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 */
index c07465f3584f3b9684219f191bfa4dfd55661048..de23109363c1b4eaf49271d93e9405f2d4732029 100644 (file)
@@ -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 f7e1ae976be4b832ab6c976e08f7a2865479f743..3052141469253f923e56b0423407a060a640f129 100644 (file)
--- 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
index 4a38d6a537b03b553065c80a9c86130a46377e97..987e737e46fd4b6a4bd8f69bc9c201c2f38f5c65 100644 (file)
@@ -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 */
index a7097c5977620ff840567bef0c8a3652aaa660ab..2e0753c5dd4476c81242f6b704ca29400b1692fc 100644 (file)
@@ -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 */