From: Sebastien GODARD Date: Sat, 19 May 2018 14:24:12 +0000 (+0200) Subject: sar/sadf: "--iface=" option: Allocate only the necessary slots X-Git-Tag: v11.7.4~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0df1ada126bedc188069f325b57c8f0c58ca48b6;p=sysstat sar/sadf: "--iface=" option: Allocate only the necessary slots Signed-off-by: Sebastien GODARD --- diff --git a/sa.h b/sa.h index abcfc84..71c1eeb 100644 --- a/sa.h +++ b/sa.h @@ -1273,7 +1273,7 @@ void get_itv_value int next_slice (unsigned long long, unsigned long long, int, long); void parse_sa_devices - (int, char *[], struct sa_dlist **, int *, int *, int); + (char *, struct sa_dlist **, int *, int *, int); int parse_sar_opt (char * [], int *, struct activity * [], unsigned int *, int); int parse_sar_I_opt diff --git a/sa_common.c b/sa_common.c index 09d46a7..d92a8fb 100644 --- a/sa_common.c +++ b/sa_common.c @@ -137,12 +137,9 @@ int get_activity_nr(struct activity *act[], unsigned int option, int count_outpu */ void salloc_sa_dlist(struct sa_dlist **st_list, int nr_alloc_slots, int nr_used_slots) { - if (!nr_alloc_slots) { - /* Allocate at least one slot */ - nr_alloc_slots = 1; + if (nr_alloc_slots) { + SREALLOC(*st_list, struct sa_dlist, sizeof(struct sa_dlist) * (nr_used_slots + nr_alloc_slots)); } - - SREALLOC(*st_list, struct sa_dlist, sizeof(struct sa_dlist) * (nr_used_slots + nr_alloc_slots)); } /* @@ -2428,13 +2425,45 @@ int parse_sa_P_opt(char *argv[], int *opt, unsigned int *flags, struct activity return 1; } +/* + *************************************************************************** + * Count number of comma-separated values in arguments list. For example, + * the number will be 3 for "sar --dev=sda,sdb,sdc -dp 2 5", 1 for + * "sar --dev=sda -dp 2 5" and 0 for "sar --dev= -dp 2 5". + * + * IN: + * @arg_v Argument containing the list of coma-separated values. + * + * RETURNS: + * Number of comma-separated values in the list. + *************************************************************************** + */ +int count_csval_arg(char *arg_v) +{ + int nr = 0; + char *t; + + if (arg_v[0] == '\0') + return 0; + + if (strchr(arg_v, ',')) { + for (t = arg_v; t; t = strchr(t + 1, ',')) { + nr++; + } + } + if (!nr) { + nr = 1; + } + + return nr; +} + /* *************************************************************************** * Parse devices entered on the command line. * * IN: - * @argc Number of arguments in the list. - * @argv Arguments list. + * @argv Argument with list of devices. * @st_list Structure where devices will be saved. * @dlst_idx Number of devices previously saved in the list. * @opt Index in list of arguments. @@ -2446,16 +2475,16 @@ int parse_sa_P_opt(char *argv[], int *opt, unsigned int *flags, struct activity * @opt Index on next argument. *************************************************************************** */ -void parse_sa_devices(int argc, char *argv[], struct sa_dlist **st_list, +void parse_sa_devices(char *argv, struct sa_dlist **st_list, int *dlst_idx, int *opt, int pos) { char *t; struct sa_dlist *st_list_i; /* (Re)allocate device list */ - salloc_sa_dlist(st_list, count_csvalues(argc, argv), *dlst_idx); + salloc_sa_dlist(st_list, count_csval_arg(argv + pos), *dlst_idx); - for (t = strtok(argv[*opt] + pos, ","); t; t = strtok(NULL, ",")) { + for (t = strtok(argv + pos, ","); t; t = strtok(NULL, ",")) { st_list_i = *st_list + (*dlst_idx)++; strncpy(st_list_i->dev_name, t, MAX_NAME_LEN - 1); st_list_i->dev_name[MAX_NAME_LEN - 1] = '\0'; diff --git a/sadf.c b/sadf.c index da9f7dc..a975f01 100644 --- a/sadf.c +++ b/sadf.c @@ -1428,13 +1428,13 @@ int main(int argc, char **argv) else if (!strncmp(argv[opt], "--dev=", 6)) { /* Parse devices entered on the command line */ - parse_sa_devices(argc, argv, &st_dev_list, + parse_sa_devices(argv[opt], &st_dev_list, &dlst_dev_idx, &opt, 6); } else if (!strncmp(argv[opt], "--iface=", 8)) { /* Parse devices entered on the command line */ - parse_sa_devices(argc, argv, &st_iface_list, + parse_sa_devices(argv[opt], &st_iface_list, &dlst_iface_idx, &opt, 8); } diff --git a/sar.c b/sar.c index 7029801..582f603 100644 --- a/sar.c +++ b/sar.c @@ -1278,13 +1278,13 @@ int main(int argc, char **argv) else if (!strncmp(argv[opt], "--dev=", 6)) { /* Parse devices entered on the command line */ - parse_sa_devices(argc, argv, &st_dev_list, + parse_sa_devices(argv[opt], &st_dev_list, &dlst_dev_idx, &opt, 6); } else if (!strncmp(argv[opt], "--iface=", 8)) { /* Parse devices entered on the command line */ - parse_sa_devices(argc, argv, &st_iface_list, + parse_sa_devices(argv[opt], &st_iface_list, &dlst_iface_idx, &opt, 8); }