]> granicus.if.org Git - sysstat/commitdiff
sar/sadf: "--iface=" option: Code refactoring
authorSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 16 May 2018 12:01:55 +0000 (14:01 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 16 May 2018 12:01:55 +0000 (14:01 +0200)
Make code ready for the addition of future options ("--dev=",
"--fs="...)

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
json_stats.c
pr_stats.c
raw_stats.c
rndr_stats.c
sa.h
sa_common.c
sadf.c
sar.c
svg_stats.c
xml_stats.c

index 412f8505a86e05fdee407c8ad1794b1ce2e8b93d..20f2de6a85d88a84dec74093d2687aa7666a10e5 100644 (file)
@@ -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;
                }
index 8499d351ab2721330daf163fa5a9a6a1cbd8181e..8015e416f3d815fd9bd81b9a5fd034ab09758c0a 100644 (file)
@@ -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;
                }
index c66752121620a0d61d96e2256194f8a79174d613..4e253eb6f03c6d32974cbcd963cea43594fa8aba 100644 (file)
@@ -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;
                }
index 38e50e4eb118cf8753460f2b4cc9fd9c1bd0625d..237fd3b8e27b190a67f9621524101a666ff8dbc6 100644 (file)
@@ -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 ca157517e7dd4d714f4718755a671a8d0057fbc4..abcfc84a3ebf3acf4cb6f826c37291aeb7fc7bab 100644 (file)
--- 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
index 434fd12b12089d3b4759599ca5effcb9b9389c2e..09d46a7c461452808307c5fd0870e178c045b2e6 100644 (file)
@@ -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 e54bdbee4c2926c15e5a04fc7eaa789c82d18451..7e447aa349b85f51608c24c62b07d62610d82480 100644 (file)
--- 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 85082ee1511b4b71c9ee42c37040038ad978c583..e4daf4166e7901be95c6d33314e008d64882963e 100644 (file)
--- 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;
index 7afbd913bf15a9ef5fe163a990798fa6de83b32d..cffeebab8b16ca9e016be067df2892ed7577957c 100644 (file)
@@ -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;
                        }
index 3d3b9734789905890764fd70d9610b6addeaddcb..c00afae63207458b0295d0c0e91b0691b3a7a249 100644 (file)
@@ -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;
                }