DPDK之(五)——多核多线程机制简析

来源:互联网 发布:淘宝mg美即面膜 编辑:程序博客网 时间:2024/05/21 11:18

DPDK通过在多核设备上,创建多个线程,每个线程绑定到单独的核上,减少线程调度的开销,以提高性能。

DPDK的线程分为控制线程和数据线程,控制线程一般绑定到MASTER核上,主要是接受用户配置,并传递配置参数给数据线程等;数据线程主要是处理数据包。

一、初始化

1、rte_eal_cpu_init()函数中,通过读取/sys/devices/system/cpu/cpuX/下的相关信息,确定当前系统有哪些CPU核,已经每个核属于哪个CPU Socket。

2、eal_parse_args()函数,解析-c参数,确认哪些CPU核是可以使用的,以及设置第一个核为MASTER。

3、为每一个SLAVE核创建线程,并调用eal_thread_set_affinity()绑定cpu。线程的执行体是eal_thread_loop()。eal_thread_loop()的主体是一个while死循环,调用不同模块注册到lcore_config[lcore_id].f的回调函数。

复制代码
 1 RTE_LCORE_FOREACH_SLAVE(i) { 2  3     /* 4      * create communication pipes between master thread 5      * and children 6      */ 7     if (pipe(lcore_config[i].pipe_master2slave) < 0) 8         rte_panic("Cannot create pipe\n"); 9     if (pipe(lcore_config[i].pipe_slave2master) < 0)10         rte_panic("Cannot create pipe\n");11 12     lcore_config[i].state = WAIT;13 14     /* create a thread for each lcore */15     ret = pthread_create(&lcore_config[i].thread_id, NULL,16                  eal_thread_loop, NULL);17     if (ret != 0)18         rte_panic("Cannot create thread\n");19 }
复制代码

 

二、注册

不同的模块需要调用rte_eal_mp_remote_launch(),将自己的回调处理函数注册到lcore_config[].f中。以l2fwd为例,注册的回调处理函数是l2fwd_launch_on_lcore()。

1 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MASTER);

 DPDK每个核上的线程最终会调用eal_thread_loop()--->l2fwd_launch_on_lcore(),调用到自己实现的处理函数。

0 0
原创粉丝点击