From 8ecf69c3ded4edaa164ed1d645629c1011de44a9 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 30 May 2013 16:01:16 +0200 Subject: ifpps: Allocate cpu dependent stats in one place Avoid code duplication by allocating the stats arrays dependent on the number of CPUs in main() rather than screen_main() and term_main(). Signed-off-by: Tobias Klauser --- ifpps.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/ifpps.c b/ifpps.c index b7aea30..8d4a079 100644 --- a/ifpps.c +++ b/ifpps.c @@ -828,13 +828,6 @@ static void screen_end(void) static int screen_main(const char *ifname, uint64_t ms_interval) { int first = 1, key; - int cpus = get_number_cpus(); - - stats_alloc(&stats_old, cpus); - stats_alloc(&stats_new, cpus); - stats_alloc(&stats_delta, cpus); - - cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits)); screen_init(&stats_screen); @@ -992,13 +985,6 @@ static void term_csv_header(const char *ifname, const struct ifstat *abs, static int term_main(const char *ifname, uint64_t ms_interval) { int first = 1; - int cpus = get_number_cpus(); - - stats_alloc(&stats_old, cpus); - stats_alloc(&stats_new, cpus); - stats_alloc(&stats_delta, cpus); - - cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits)); do { stats_sample_generic(ifname, ms_interval); @@ -1017,7 +1003,7 @@ static int term_main(const char *ifname, uint64_t ms_interval) int main(int argc, char **argv) { short ifflags = 0; - int c, opt_index, ret, promisc = 0; + int c, opt_index, ret, cpus, promisc = 0; uint64_t interval = 1000; char *ifname = NULL; int (*func_main)(const char *ifname, uint64_t ms_interval) = screen_main; @@ -1081,6 +1067,14 @@ int main(int argc, char **argv) register_signal(SIGINT, signal_handler); register_signal(SIGHUP, signal_handler); + cpus = get_number_cpus(); + + stats_alloc(&stats_old, cpus); + stats_alloc(&stats_new, cpus); + stats_alloc(&stats_delta, cpus); + + cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits)); + if (promisc) ifflags = enter_promiscuous_mode(ifname); ret = func_main(ifname, interval); -- cgit v1.2.3-54-g00ecf