From c605f595796896c53eda1577fe43bd9de18d0c78 Mon Sep 17 00:00:00 2001 From: fengbojiang Date: Sat, 18 Aug 2018 01:45:11 +0800 Subject: [PATCH] add "idle_sleep" to reduce CPU usage when no pkts incomming. --- config.ini | 4 ++++ lib/ff_config.c | 4 +++- lib/ff_config.h | 10 +++++++--- lib/ff_dpdk_if.c | 17 +++++++++++++++-- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/config.ini b/config.ini index 5f390e77..f9901e64 100644 --- a/config.ini +++ b/config.ini @@ -9,6 +9,10 @@ tso=0 ## HW vlan strip, default: enabled. vlan_strip=1 +# sleep when no pkts incomming +# unit: microseconds +idle_sleep=100 + # enabled port list # # EBNF grammar: diff --git a/lib/ff_config.c b/lib/ff_config.c index 77070b24..9ebc7c91 100644 --- a/lib/ff_config.c +++ b/lib/ff_config.c @@ -297,7 +297,7 @@ __parse_config_list(uint16_t *arr, int *sz, const char *value) { } } if (nr_ele <= 0) { - printf("list %s is empty\n", value); + fprintf(stderr, "list %s is empty\n", value); return 1; } sort_uint16_array(arr, nr_ele); @@ -430,6 +430,8 @@ ini_parse_handler(void* user, const char* section, const char* name, pconfig->dpdk.tso = atoi(value); } else if (MATCH("dpdk", "vlan_strip")) { pconfig->dpdk.vlan_strip = atoi(value); + } else if (MATCH("dpdk", "idle_sleep")) { + pconfig->dpdk.idle_sleep = atoi(value); } else if (MATCH("kni", "enable")) { pconfig->kni.enable= atoi(value); } else if (MATCH("kni", "method")) { diff --git a/lib/ff_config.h b/lib/ff_config.h index 9608e9bd..6424b0da 100644 --- a/lib/ff_config.h +++ b/lib/ff_config.h @@ -31,8 +31,8 @@ extern "C" { #endif -// dpdk argc, argv, max argc: 4, member of dpdk_config -#define DPDK_CONFIG_NUM 4 +// dpdk argc, argv, max argc: 16, member of dpdk_config +#define DPDK_CONFIG_NUM 16 #define DPDK_CONFIG_MAXLEN 64 #define DPDK_MAX_LCORE 128 @@ -88,12 +88,16 @@ struct ff_config { int numa_on; int tso; int vlan_strip; + + /* sleep x microseconds when no pkts incomming */ + unsigned idle_sleep; + /* list of proc-lcore */ uint16_t *proc_lcore; int nb_ports; - uint16_t *portid_list; uint16_t max_portid; + uint16_t *portid_list; // MAP(portid => struct ff_port_cfg*) struct ff_port_cfg *port_cfgs; } dpdk; diff --git a/lib/ff_dpdk_if.c b/lib/ff_dpdk_if.c index aae50985..dcf2b44d 100644 --- a/lib/ff_dpdk_if.c +++ b/lib/ff_dpdk_if.c @@ -24,6 +24,7 @@ * */ #include +#include #include #include @@ -98,6 +99,8 @@ static int kni_accept; static int numa_on; +static unsigned idle_sleep; + static struct rte_timer freebsd_clock; // Mellanox Linux's driver key @@ -821,6 +824,8 @@ ff_dpdk_init(int argc, char **argv) numa_on = ff_global_cfg.dpdk.numa_on; + idle_sleep = ff_global_cfg.dpdk.idle_sleep; + init_lcore_conf(); init_mem_pool(); @@ -1429,7 +1434,7 @@ main_loop(void *arg) struct loop_routine *lr = (struct loop_routine *)arg; struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; - uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc; + uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc, idle_sleep_tsc; int i, j, nb_rx, idle; uint16_t port_id, queue_id; struct lcore_conf *qconf; @@ -1524,10 +1529,18 @@ main_loop(void *arg) lr->loop(lr->arg); } + idle_sleep_tsc = rte_rdtsc(); + if (likely(idle && idle_sleep)) { + usleep(idle_sleep); + end_tsc = rte_rdtsc(); + } else { + end_tsc = idle_sleep_tsc; + } + end_tsc = rte_rdtsc(); if (usch_tsc == cur_tsc) { - usr_tsc = end_tsc - div_tsc; + usr_tsc = idle_sleep_tsc - div_tsc; } if (!idle) {