Skip to content

Commit

Permalink
Make device name consistent between sar and iostat
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
sysstat committed Jul 27, 2020
1 parent e2afb97 commit 2f4c52c
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 184 deletions.
149 changes: 149 additions & 0 deletions common.c
Expand Up @@ -36,6 +36,7 @@

#include "version.h"
#include "common.h"
#include "ioconf.h"

#ifdef USE_NLS
#include <locale.h>
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions common.h
Expand Up @@ -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
Expand Down
50 changes: 8 additions & 42 deletions iostat.c
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions json_stats.c
Expand Up @@ -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 */
Expand Down
5 changes: 3 additions & 2 deletions pcp_stats.c
Expand Up @@ -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 */
Expand Down
4 changes: 3 additions & 1 deletion pr_stats.c
Expand Up @@ -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 */
Expand Down
5 changes: 3 additions & 2 deletions raw_stats.c
Expand Up @@ -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 */
Expand Down
5 changes: 3 additions & 2 deletions rndr_stats.c
Expand Up @@ -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 */
Expand Down

0 comments on commit 2f4c52c

Please sign in to comment.