searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

ovs thread分析

2023-09-25 14:00:57
108
0

一、前言

    ovs自己提供了一套线程api(具体参考ovs-thread.c),底层也是基于pthread_xxx实现的。ovs这么做的目的是为了与ovs rcu机制紧密结合,本文只分析ovs线程的创建和使用部分,对ovs rcu一笔带过。

二、ovs线程代码分析

1、线程创建函数分析:

pthread_t ovs_thread_create(const char *name, void *(*start)(void *), void *arg)
{
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; //静态局部变量
    struct ovsthread_aux *aux;
    pthread_t thread;
    int error;
    forbid_forking("multiple threads exist"); 
    if (ovsthread_once_start(&once)) { //保证只执行一次
        ovsrcu_quiesce_end(); 
        ovsthread_once_done(&once);
    }
    multithreaded = true;
    aux = xmalloc(sizeof *aux);
    aux->start = start;
    aux->arg = arg;
    ovs_strlcpy(aux->name, name, sizeof aux->name);

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    set_min_stack_size(&attr, 512 * 1024);

    error = pthread_create(&thread, &attr, ovsthread_wrapper, aux);
    if (error) {
        ovs_abort(error, "pthread_create failed");
    }
    pthread_attr_destroy(&attr);
    return thread;
}

a、ovsrcu_quiesce_end(),保证只执行一次,将本线程加入到rcu线程链表中:ovs_list_push_back(&ovsrcu_threads, &perthread->list_node);

b、aux是自定义数据结构,attr标准线程数据结构,都是用来描述本线程的,需要注意线程名只有16个字符,过长了会被截断

c、ovsthread_wrapper在里面会维护一个父线程创建线程的个数*ovsthread_id_get(),并且在最后会调用一次ovsrcu_quiesce_end(),如果是父线程创建子线程,那子线程就是在这里加入的rcu线程链表。

d、ovsthread_once_start是一个内联函数,具体的实现如下图所示,ovsthread_once_start函数就只是把once->done置为true。

2、线程函数执行与ovs rcu的简单分析:

ovs rcu主要流程如下所示,对rcu里面的latch和poll_block机制不做具体分析。

a、调用ovsrcu_synchronize,去等待ovs rcu线程链表上的线程都经历过一次quiescent状态。所以如果有其他线程没有经历过quiescent状态,就会使本线程卡住,出现如下打印:

b、LIST_FOR_EACH_POP (cbset, list_node, &cbsets),遍历所有之前注册的延迟回调,执行回调函数。

3、线程退出:

     ovs的线程在创建的时候以及注册了退出时执行的函数,但线程资源没有被回收,所以线程创建后,需要在里面加入pthread_detach(pthread_self())来自动回收线程资源。

三、小结

    不管是自己通过标准实现创建线程还是通过ovs线程接口创建的线程,如果线程里面有用到rcu相关的数据或接口,需要调用ovsrcu_quiesced类的接口让线程进入quiescent状态;如果是频繁删建的线程,需要用pthread_detach(pthread_self())回收线程资源。

0条评论
作者已关闭评论
1****m
2文章数
0粉丝数
1****m
2 文章 | 0 粉丝
1****m
2文章数
0粉丝数
1****m
2 文章 | 0 粉丝
原创

ovs thread分析

2023-09-25 14:00:57
108
0

一、前言

    ovs自己提供了一套线程api(具体参考ovs-thread.c),底层也是基于pthread_xxx实现的。ovs这么做的目的是为了与ovs rcu机制紧密结合,本文只分析ovs线程的创建和使用部分,对ovs rcu一笔带过。

二、ovs线程代码分析

1、线程创建函数分析:

pthread_t ovs_thread_create(const char *name, void *(*start)(void *), void *arg)
{
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; //静态局部变量
    struct ovsthread_aux *aux;
    pthread_t thread;
    int error;
    forbid_forking("multiple threads exist"); 
    if (ovsthread_once_start(&once)) { //保证只执行一次
        ovsrcu_quiesce_end(); 
        ovsthread_once_done(&once);
    }
    multithreaded = true;
    aux = xmalloc(sizeof *aux);
    aux->start = start;
    aux->arg = arg;
    ovs_strlcpy(aux->name, name, sizeof aux->name);

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    set_min_stack_size(&attr, 512 * 1024);

    error = pthread_create(&thread, &attr, ovsthread_wrapper, aux);
    if (error) {
        ovs_abort(error, "pthread_create failed");
    }
    pthread_attr_destroy(&attr);
    return thread;
}

a、ovsrcu_quiesce_end(),保证只执行一次,将本线程加入到rcu线程链表中:ovs_list_push_back(&ovsrcu_threads, &perthread->list_node);

b、aux是自定义数据结构,attr标准线程数据结构,都是用来描述本线程的,需要注意线程名只有16个字符,过长了会被截断

c、ovsthread_wrapper在里面会维护一个父线程创建线程的个数*ovsthread_id_get(),并且在最后会调用一次ovsrcu_quiesce_end(),如果是父线程创建子线程,那子线程就是在这里加入的rcu线程链表。

d、ovsthread_once_start是一个内联函数,具体的实现如下图所示,ovsthread_once_start函数就只是把once->done置为true。

2、线程函数执行与ovs rcu的简单分析:

ovs rcu主要流程如下所示,对rcu里面的latch和poll_block机制不做具体分析。

a、调用ovsrcu_synchronize,去等待ovs rcu线程链表上的线程都经历过一次quiescent状态。所以如果有其他线程没有经历过quiescent状态,就会使本线程卡住,出现如下打印:

b、LIST_FOR_EACH_POP (cbset, list_node, &cbsets),遍历所有之前注册的延迟回调,执行回调函数。

3、线程退出:

     ovs的线程在创建的时候以及注册了退出时执行的函数,但线程资源没有被回收,所以线程创建后,需要在里面加入pthread_detach(pthread_self())来自动回收线程资源。

三、小结

    不管是自己通过标准实现创建线程还是通过ovs线程接口创建的线程,如果线程里面有用到rcu相关的数据或接口,需要调用ovsrcu_quiesced类的接口让线程进入quiescent状态;如果是频繁删建的线程,需要用pthread_detach(pthread_self())回收线程资源。

文章来自个人专栏
文章 | 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0