From 4f3d36ebaf0e9c443eb82ae24eb4a6e5e7d3318d Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Wed, 16 May 2018 14:01:55 +0200 Subject: [PATCH] sar/sadf: "--iface=" option: Code refactoring Make code ready for the addition of future options ("--dev=", "--fs="...) Signed-off-by: Sebastien GODARD --- json_stats.c | 14 ++++++------ pr_stats.c | 14 ++++++------ raw_stats.c | 14 ++++++------ rndr_stats.c | 14 ++++++------ sa.h | 8 ++----- sa_common.c | 60 +++++++++++++++++++++++++--------------------------- sadf.c | 8 +++---- sar.c | 16 +++++++------- svg_stats.c | 14 ++++++------ xml_stats.c | 14 ++++++------ 10 files changed, 79 insertions(+), 97 deletions(-) diff --git a/json_stats.c b/json_stats.c index 412f850..20f2de6 100644 --- a/json_stats.c +++ b/json_stats.c @@ -37,8 +37,8 @@ extern unsigned int flags; extern unsigned int dm_major; -extern struct sa_dlist *st_dev_list; -extern int dlist_idx; +extern struct sa_dlist *st_iface_list; +extern int dlst_iface_idx; /* *************************************************************************** @@ -825,10 +825,9 @@ __print_funct_t json_print_net_dev_stats(struct activity *a, int curr, int tab, sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, sndc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, sndc->interface)) /* Device not found */ continue; } @@ -914,10 +913,9 @@ __print_funct_t json_print_net_edev_stats(struct activity *a, int curr, int tab, snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, snedc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, snedc->interface)) /* Device not found */ continue; } diff --git a/pr_stats.c b/pr_stats.c index 8499d35..8015e41 100644 --- a/pr_stats.c +++ b/pr_stats.c @@ -41,8 +41,8 @@ extern unsigned int dm_major; extern int dis; extern char timestamp[][TIMESTAMP_LEN]; extern unsigned long avg_count; -extern struct sa_dlist *st_dev_list; -extern int dlist_idx; +extern struct sa_dlist *st_iface_list; +extern int dlst_iface_idx; /* *************************************************************************** @@ -1134,10 +1134,9 @@ __print_funct_t print_net_dev_stats(struct activity *a, int prev, int curr, for (i = 0; i < a->nr[curr]; i++) { sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, sndc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, sndc->interface)) /* Device not found */ continue; } @@ -1215,10 +1214,9 @@ __print_funct_t print_net_edev_stats(struct activity *a, int prev, int curr, for (i = 0; i < a->nr[curr]; i++) { snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, snedc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, snedc->interface)) /* Device not found */ continue; } diff --git a/raw_stats.c b/raw_stats.c index c667521..4e253eb 100644 --- a/raw_stats.c +++ b/raw_stats.c @@ -30,8 +30,8 @@ extern unsigned int flags; extern unsigned int dm_major; -extern struct sa_dlist *st_dev_list; -extern int dlist_idx; +extern struct sa_dlist *st_iface_list; +extern int dlst_iface_idx; /* *************************************************************************** @@ -603,10 +603,9 @@ __print_funct_t raw_print_net_dev_stats(struct activity *a, char *timestr, int c sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, sndc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, sndc->interface)) /* Device not found */ continue; } @@ -664,10 +663,9 @@ __print_funct_t raw_print_net_edev_stats(struct activity *a, char *timestr, int snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, snedc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, snedc->interface)) /* Device not found */ continue; } diff --git a/rndr_stats.c b/rndr_stats.c index 38e50e4..237fd3b 100644 --- a/rndr_stats.c +++ b/rndr_stats.c @@ -39,8 +39,8 @@ char *seps[] = {"\t", ";"}; extern unsigned int flags; extern unsigned int dm_major; -extern struct sa_dlist *st_dev_list; -extern int dlist_idx; +extern struct sa_dlist *st_iface_list; +extern int dlst_iface_idx; /* *************************************************************************** @@ -1188,10 +1188,9 @@ __print_funct_t render_net_dev_stats(struct activity *a, int isdb, char *pre, sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, sndc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, sndc->interface)) /* Device not found */ continue; } @@ -1292,10 +1291,9 @@ __print_funct_t render_net_edev_stats(struct activity *a, int isdb, char *pre, snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, snedc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, snedc->interface)) /* Device not found */ continue; } diff --git a/sa.h b/sa.h index ca15751..abcfc84 100644 --- a/sa.h +++ b/sa.h @@ -1099,8 +1099,6 @@ struct tstamp { /* List of devices entered on the command line */ struct sa_dlist { - /* Device type */ - int dev_id __attribute__ ((aligned (4))); /* Device name */ char dev_name[MAX_NAME_LEN]; }; @@ -1275,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, 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 @@ -1317,10 +1315,8 @@ int sa_get_record_timestamp_struct (unsigned int, struct record_header *, struct tm *, struct tm *); int sa_open_read_magic (int *, char *, struct file_magic *, int, int *, int); -void salloc_sa_dlist - (struct sa_dlist **, int); int search_sa_dlist - (struct sa_dlist *, int, char *, int); + (struct sa_dlist *, int, char *); void select_all_activities (struct activity * []); void select_default_activity diff --git a/sa_common.c b/sa_common.c index 434fd12..09d46a7 100644 --- a/sa_common.c +++ b/sa_common.c @@ -128,19 +128,21 @@ int get_activity_nr(struct activity *act[], unsigned int option, int count_outpu * Allocate structures for devices entered on the command line. * * IN: - * @list_len Number of arguments on the command line. + * @nr_alloc_slots Number of slots to allocate. + * @nr_used_slots Number of slots currently used. * * OUT: - * @st_dev_list Address of allocated structures. + * @st_list Address of allocated structures. *************************************************************************** */ -void salloc_sa_dlist(struct sa_dlist **st_dev_list, int list_len) +void salloc_sa_dlist(struct sa_dlist **st_list, int nr_alloc_slots, int nr_used_slots) { - if ((*st_dev_list = (struct sa_dlist *) malloc(SA_DLIST_SIZE * list_len)) == NULL) { - perror("malloc"); - exit(4); + if (!nr_alloc_slots) { + /* Allocate at least one slot */ + nr_alloc_slots = 1; } - memset(*st_dev_list, 0, SA_DLIST_SIZE * list_len); + + SREALLOC(*st_list, struct sa_dlist, sizeof(struct sa_dlist) * (nr_used_slots + nr_alloc_slots)); } /* @@ -148,24 +150,22 @@ void salloc_sa_dlist(struct sa_dlist **st_dev_list, int list_len) * Look for device in list. * * IN: - * @st_dev_list Structure where devices are saved. - * @dlist_idx Number of devices in the list. + * @st_list Structure where devices are saved. + * @dlst_idx Number of devices in the list. * @d_name Device name to look for. - * @d_id Device type. * * RETURNS: * 1 if device found in list, 0 otherwise. *************************************************************************** */ -int search_sa_dlist(struct sa_dlist *st_dev_list, int dlist_idx, char *d_name, int d_id) +int search_sa_dlist(struct sa_dlist *st_list, int dlst_idx, char *d_name) { int i; - struct sa_dlist *st_dev_list_i; + struct sa_dlist *st_list_i; - for (i = 0; i < dlist_idx; i++) { - st_dev_list_i = st_dev_list + i; - if ((st_dev_list_i->dev_id == d_id) && - !strcmp(st_dev_list_i->dev_name, d_name)) + for (i = 0; i < dlst_idx; i++) { + st_list_i = st_list + i; + if (!strcmp(st_list_i->dev_name, d_name)) return 1; } @@ -2435,32 +2435,30 @@ int parse_sa_P_opt(char *argv[], int *opt, unsigned int *flags, struct activity * IN: * @argc Number of arguments in the list. * @argv Arguments list. - * @st_dev_list Structure where devices will be saved. - * @dlist_idx Number of devices previously saved in the list. + * @st_list Structure where devices will be saved. + * @dlst_idx Number of devices previously saved in the list. * @opt Index in list of arguments. - * @d_id Type of device. * @pos Position is string where is located the first device. * * OUT: - * @st_dev_list Structure where devices have been saved. - * @dlist_idx Total number of devices saved in the list. + * @st_list Structure where devices have been saved. + * @dlst_idx Total number of devices saved in the list. * @opt Index on next argument. *************************************************************************** */ -void parse_sa_devices(int argc, char *argv[], struct sa_dlist **st_dev_list, - int *dlist_idx, int *opt, int d_id, int pos) +void parse_sa_devices(int argc, char *argv[], struct sa_dlist **st_list, + int *dlst_idx, int *opt, int pos) { char *t; - struct sa_dlist *st_dev_list_i; + struct sa_dlist *st_list_i; + + /* (Re)allocate device list */ + salloc_sa_dlist(st_list, count_csvalues(argc, argv), *dlst_idx); - if (*st_dev_list == NULL) { - /* Allocate device list */ - salloc_sa_dlist(st_dev_list, argc - 1 + count_csvalues(argc, argv)); - } for (t = strtok(argv[*opt] + pos, ","); t; t = strtok(NULL, ",")) { - st_dev_list_i = *st_dev_list + (*dlist_idx)++; - st_dev_list_i->dev_id = d_id; - strncpy(st_dev_list_i->dev_name, t, MAX_NAME_LEN - 1); + 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'; } (*opt)++; } diff --git a/sadf.c b/sadf.c index e54bdbe..7e447aa 100644 --- a/sadf.c +++ b/sadf.c @@ -78,8 +78,8 @@ struct tstamp tm_start, tm_end; char *args[MAX_ARGV_NR]; /* Devices entered on the command line */ -struct sa_dlist *st_dev_list = NULL; -int dlist_idx = 0; +struct sa_dlist *st_iface_list = NULL; +int dlst_iface_idx = 0; extern struct activity *act[]; extern struct report_format *fmt[]; @@ -1427,8 +1427,8 @@ int main(int argc, char **argv) else if (!strncmp(argv[opt], "--iface=", 8)) { /* Parse devices entered on the command line */ - parse_sa_devices(argc, argv, &st_dev_list, - &dlist_idx, &opt, A_NET_DEV, 8); + parse_sa_devices(argc, argv, &st_iface_list, + &dlst_iface_idx, &opt, 8); } else if (!strcmp(argv[opt], "-s")) { diff --git a/sar.c b/sar.c index 85082ee..e4daf41 100644 --- a/sar.c +++ b/sar.c @@ -77,8 +77,8 @@ struct record_header record_hdr[3]; unsigned int id_seq[NR_ACT]; /* Devices entered on the command line */ -struct sa_dlist *st_dev_list = NULL; -int dlist_idx = 0; +struct sa_dlist *st_iface_list = NULL; +int dlst_iface_idx = 0; struct tm rectime; @@ -1278,8 +1278,8 @@ int main(int argc, char **argv) else if (!strncmp(argv[opt], "--iface=", 8)) { /* Parse devices entered on the command line */ - parse_sa_devices(argc, argv, &st_dev_list, - &dlist_idx, &opt, A_NET_DEV, 8); + parse_sa_devices(argc, argv, &st_iface_list, + &dlst_iface_idx, &opt, 8); } else if (!strcmp(argv[opt], "--help")) { @@ -1512,8 +1512,8 @@ int main(int argc, char **argv) /* Free stuctures and activity bitmaps */ free_bitmaps(act); free_structures(act); - if (st_dev_list) { - free(st_dev_list); + if (st_iface_list) { + free(st_iface_list); } return 0; @@ -1639,8 +1639,8 @@ int main(int argc, char **argv) /* Free structures and activity bitmaps */ free_bitmaps(act); free_structures(act); - if (st_dev_list) { - free(st_dev_list); + if (st_iface_list) { + free(st_iface_list); } return 0; diff --git a/svg_stats.c b/svg_stats.c index 7afbd91..cffeeba 100644 --- a/svg_stats.c +++ b/svg_stats.c @@ -39,8 +39,8 @@ extern unsigned int flags; extern unsigned int dm_major; -extern struct sa_dlist *st_dev_list; -extern int dlist_idx; +extern struct sa_dlist *st_iface_list; +extern int dlst_iface_idx; unsigned int svg_colors[] = {0x00cc00, 0xff00bf, 0x00ffff, 0xff0000, 0xe85f00, 0x0000ff, 0x006020, 0x7030a0, @@ -2332,10 +2332,9 @@ __print_funct_t svg_print_net_dev_stats(struct activity *a, int curr, int action for (i = 0; i < a->nr[curr]; i++) { sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, sndc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, sndc->interface)) /* Device not found */ continue; } @@ -2545,10 +2544,9 @@ __print_funct_t svg_print_net_edev_stats(struct activity *a, int curr, int actio /* Empty structure: This is the end of the list */ break; - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, snedc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, snedc->interface)) /* Device not found */ continue; } diff --git a/xml_stats.c b/xml_stats.c index 3d3b973..c00afae 100644 --- a/xml_stats.c +++ b/xml_stats.c @@ -37,8 +37,8 @@ extern unsigned int flags; extern unsigned int dm_major; -extern struct sa_dlist *st_dev_list; -extern int dlist_idx; +extern struct sa_dlist *st_iface_list; +extern int dlst_iface_idx; /* *************************************************************************** @@ -801,10 +801,9 @@ __print_funct_t xml_print_net_dev_stats(struct activity *a, int curr, int tab, sndc = (struct stats_net_dev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, sndc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, sndc->interface)) /* Device not found */ continue; } @@ -878,10 +877,9 @@ __print_funct_t xml_print_net_edev_stats(struct activity *a, int curr, int tab, snedc = (struct stats_net_edev *) ((char *) a->buf[curr] + i * a->msize); - if (dlist_idx) { + if (dlst_iface_idx) { /* A list of devices has been entered on the command line */ - if (!search_sa_dlist(st_dev_list, dlist_idx, snedc->interface, - A_NET_DEV)) + if (!search_sa_dlist(st_iface_list, dlst_iface_idx, snedc->interface)) /* Device not found */ continue; } -- 2.40.0