Skip to content

Commit c605f59

Browse files
committed
add "idle_sleep" to reduce CPU usage when no pkts incomming.
1 parent e4983a3 commit c605f59

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

config.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ tso=0
99
## HW vlan strip, default: enabled.
1010
vlan_strip=1
1111

12+
# sleep when no pkts incomming
13+
# unit: microseconds
14+
idle_sleep=100
15+
1216
# enabled port list
1317
#
1418
# EBNF grammar:

lib/ff_config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ __parse_config_list(uint16_t *arr, int *sz, const char *value) {
297297
}
298298
}
299299
if (nr_ele <= 0) {
300-
printf("list %s is empty\n", value);
300+
fprintf(stderr, "list %s is empty\n", value);
301301
return 1;
302302
}
303303
sort_uint16_array(arr, nr_ele);
@@ -430,6 +430,8 @@ ini_parse_handler(void* user, const char* section, const char* name,
430430
pconfig->dpdk.tso = atoi(value);
431431
} else if (MATCH("dpdk", "vlan_strip")) {
432432
pconfig->dpdk.vlan_strip = atoi(value);
433+
} else if (MATCH("dpdk", "idle_sleep")) {
434+
pconfig->dpdk.idle_sleep = atoi(value);
433435
} else if (MATCH("kni", "enable")) {
434436
pconfig->kni.enable= atoi(value);
435437
} else if (MATCH("kni", "method")) {

lib/ff_config.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
extern "C" {
3232
#endif
3333

34-
// dpdk argc, argv, max argc: 4, member of dpdk_config
35-
#define DPDK_CONFIG_NUM 4
34+
// dpdk argc, argv, max argc: 16, member of dpdk_config
35+
#define DPDK_CONFIG_NUM 16
3636
#define DPDK_CONFIG_MAXLEN 64
3737
#define DPDK_MAX_LCORE 128
3838

@@ -88,12 +88,16 @@ struct ff_config {
8888
int numa_on;
8989
int tso;
9090
int vlan_strip;
91+
92+
/* sleep x microseconds when no pkts incomming */
93+
unsigned idle_sleep;
94+
9195
/* list of proc-lcore */
9296
uint16_t *proc_lcore;
9397

9498
int nb_ports;
95-
uint16_t *portid_list;
9699
uint16_t max_portid;
100+
uint16_t *portid_list;
97101
// MAP(portid => struct ff_port_cfg*)
98102
struct ff_port_cfg *port_cfgs;
99103
} dpdk;

lib/ff_dpdk_if.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
*/
2626
#include <assert.h>
27+
#include <unistd.h>
2728

2829
#include <rte_common.h>
2930
#include <rte_byteorder.h>
@@ -98,6 +99,8 @@ static int kni_accept;
9899

99100
static int numa_on;
100101

102+
static unsigned idle_sleep;
103+
101104
static struct rte_timer freebsd_clock;
102105

103106
// Mellanox Linux's driver key
@@ -821,6 +824,8 @@ ff_dpdk_init(int argc, char **argv)
821824

822825
numa_on = ff_global_cfg.dpdk.numa_on;
823826

827+
idle_sleep = ff_global_cfg.dpdk.idle_sleep;
828+
824829
init_lcore_conf();
825830

826831
init_mem_pool();
@@ -1429,7 +1434,7 @@ main_loop(void *arg)
14291434
struct loop_routine *lr = (struct loop_routine *)arg;
14301435

14311436
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1432-
uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc;
1437+
uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc, idle_sleep_tsc;
14331438
int i, j, nb_rx, idle;
14341439
uint16_t port_id, queue_id;
14351440
struct lcore_conf *qconf;
@@ -1524,10 +1529,18 @@ main_loop(void *arg)
15241529
lr->loop(lr->arg);
15251530
}
15261531

1532+
idle_sleep_tsc = rte_rdtsc();
1533+
if (likely(idle && idle_sleep)) {
1534+
usleep(idle_sleep);
1535+
end_tsc = rte_rdtsc();
1536+
} else {
1537+
end_tsc = idle_sleep_tsc;
1538+
}
1539+
15271540
end_tsc = rte_rdtsc();
15281541

15291542
if (usch_tsc == cur_tsc) {
1530-
usr_tsc = end_tsc - div_tsc;
1543+
usr_tsc = idle_sleep_tsc - div_tsc;
15311544
}
15321545

15331546
if (!idle) {

0 commit comments

Comments
 (0)