Hi Denis,
attached is a patch, wich replace the device name lookup table by bb linked
list. Please review and consider usage.
Regards.
Max
diff --git a/procps/iostat.c b/procps/iostat.c
index 06a33eb..b771fe1 100644
--- a/procps/iostat.c
+++ b/procps/iostat.c
@@ -59,18 +59,12 @@ struct stats_dev {
unsigned long wr_ops;
};
-/* List of devices entered on the command line */
-struct device_list {
- char dname[MAX_DEVICE_NAME];
-};
-
/* Globals. Sort by size and access frequency. */
struct globals {
smallint show_all;
- unsigned devlist_i; /* Index to the list of devices */
unsigned total_cpus; /* Number of CPUs */
unsigned clk_tck; /* Number of clock ticks per second */
- struct device_list *dlist;
+ llist_t *dev_list; /* List of devices entered on the command line */
struct stats_dev *saved_stats_dev;
struct tm tmtime;
};
@@ -289,37 +283,6 @@ static int is_partition(const char *dev)
return ((dev[0] - 's') | (dev[1] - 'd') | (dev[2] - 'a')) == 0 && isdigit(dev[3]);
}
-/*
- * Return number of numbers on cmdline.
- * Reasonable values are only 0 (no interval/count specified),
- * 1 (interval specified) and 2 (both interval and count specified)
- */
-static int numbers_on_cmdline(int argc, char *argv[])
-{
- int sum = 0;
-
- if (isdigit(argv[argc-1][0]))
- sum++;
- if (argc > 2 && isdigit(argv[argc-2][0]))
- sum++;
-
- return sum;
-}
-
-static int is_dev_in_dlist(const char *dev)
-{
- int i;
-
- /* Go through the device list */
- for (i = 0; i < G.devlist_i; i++)
- if (strcmp(G.dlist[i].dname, dev) == 0)
- /* Found a match */
- return 1;
-
- /* No match found */
- return 0;
-}
-
static void do_disk_statistics(cputime_t itv)
{
FILE *fp;
@@ -356,7 +319,7 @@ static void do_disk_statistics(cputime_t itv)
break;
}
- if (!G.devlist_i && !is_partition(sd.dname)) {
+ if (!G.dev_list && !is_partition(sd.dname)) {
/* User didn't specify device */
if (!G.show_all && !sd.rd_ops && !sd.wr_ops) {
/* Don't print unused device */
@@ -367,7 +330,7 @@ static void do_disk_statistics(cputime_t itv)
i++;
} else {
/* Is device in device list? */
- if (is_dev_in_dlist(sd.dname)) {
+ if (llist_find_str(G.dev_list, sd.dname)) {
/* Print current statistics */
print_stats_dev_struct(&G.saved_stats_dev[i], &sd, itv);
G.saved_stats_dev[i] = sd;
@@ -389,28 +352,6 @@ static void dev_report(cputime_t itv)
do_disk_statistics(itv);
}
-static void save_to_devlist(const char *dname)
-{
- int i;
- struct device_list *tmp = G.dlist;
-
- if (strncmp(dname, "/dev/", 5) == 0)
- /* We'll ignore prefix '/dev/' */
- dname += 5;
-
- /* Go through the list */
- for (i = 0; i < G.devlist_i; i++, tmp++)
- if (strcmp(tmp->dname, dname) == 0)
- /* Already in the list */
- return;
-
- /* Add device name to the list */
- strncpy(tmp->dname, dname, MAX_DEVICE_NAME - 1);
-
- /* Update device list index */
- G.devlist_i++;
-}
-
static unsigned get_number_of_devices(void)
{
FILE *fp;
@@ -440,18 +381,6 @@ static unsigned get_number_of_devices(void)
return n;
}
-static int number_of_ALL_on_cmdline(char **argv)
-{
- int alls = 0;
-
- /* Iterate over cmd line arguments, count "ALL" */
- while (*argv)
- if (strcmp(*argv++, "ALL") == 0)
- alls++;
-
- return alls;
-}
-
//usage:#define iostat_trivial_usage
//usage: "[-c] [-d] [-t] [-z] [-k|-m] [ALL|BLOCKDEV...] [INTERVAL [COUNT]]"
//usage:#define iostat_full_usage "\n\n"
@@ -467,7 +396,7 @@ static int number_of_ALL_on_cmdline(char **argv)
int iostat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int iostat_main(int argc, char **argv)
{
- int opt, dev_num;
+ int opt, dev_num = 0;
unsigned interval = 0;
int count;
cputime_t global_uptime[2] = { 0 };
@@ -498,20 +427,26 @@ int iostat_main(int argc, char **argv)
argv += optind;
argc -= optind;
- dev_num = argc - numbers_on_cmdline(argc, argv);
- /* We don't want to allocate space for 'ALL' */
- dev_num -= number_of_ALL_on_cmdline(argv);
- if (dev_num > 0)
- /* Make space for device list */
- G.dlist = xzalloc(sizeof(G.dlist[0]) * dev_num);
-
/* Store device names into device list */
while (*argv && !isdigit(*argv[0])) {
- if (strcmp(*argv, "ALL") != 0)
- /* If not ALL, save device name */
- save_to_devlist(*argv);
- else
+ if (strcmp(*argv, "ALL") == 0) {
G.show_all = 1;
+ } else {
+ char *dev_name = *argv;
+ if (strncmp(dev_name, "/dev/", 5) == 0) {
+ /* We'll ignore prefix '/dev/' */
+ dev_name += 5;
+ }
+ // remove me please
+ if (strlen(dev_name) > MAX_DEVICE_NAME - 1) {
+ dev_name[MAX_DEVICE_NAME - 1] = '\0';
+ }
+ if (llist_find_str(G.dev_list, dev_name) == NULL) {
+ llist_add_to(&G.dev_list, dev_name);
+ //llist_add_to_end(&G.dev_list, dev_name);
+ dev_num++;
+ }
+ }
argv++;
}
@@ -585,9 +520,9 @@ int iostat_main(int argc, char **argv)
bb_putchar('\n');
if (ENABLE_FEATURE_CLEAN_UP) {
- free(&G);
- free(G.dlist);
+ llist_free(G.dev_list, NULL);
free(G.saved_stats_dev);
+ free(&G);
}
return EXIT_SUCCESS;
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox