Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议: 提供一个线程绑定cpu的接口. #81

Closed
wzy2687 opened this issue Mar 14, 2019 · 3 comments
Milestone

Comments

@wzy2687
Copy link

@wzy2687 wzy2687 commented Mar 14, 2019

线程封装, 提供一个绑定cpu的接口, 比如 posix 下的pthread_setaffinity_np.

可以 tb_long_t tb_thread_bind_cpu(tb_thread_ref_t t, tb_long_t cpu_no);

@waruqi waruqi added this to the v1.6.4 milestone Mar 14, 2019
@waruqi

This comment has been minimized.

Copy link
Member

@waruqi waruqi commented Mar 14, 2019

好的,我下个版本会加上。

@waruqi

This comment has been minimized.

Copy link
Member

@waruqi waruqi commented Jun 28, 2019

我实现的差不多了,dev分支上:

线程cpu绑定

在thread.h中

tb_bool_t tb_thread_setaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset);
tb_bool_t tb_thread_getaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset);

进程cpu绑定

在sched.h中

tb_bool_t tb_sched_setaffinity(tb_size_t pid, tb_cpuset_ref_t cpuset);
tb_bool_t tb_sched_getaffinity(tb_size_t pid, tb_cpuset_ref_t cpuset);

相关实现demo,在demo/platform目录下,thread.c/sched.c里面,可参考下。

比如:

    // init threads
    tb_size_t i = 0;
    tb_cpuset_t cpuset;
    tb_thread_ref_t threads[64] = {0};
    for (i = 0; i < cpu_count; i++)
    {
        // init thread
        threads[i] = tb_thread_init(tb_null, tb_demo_thread_func, tb_u2p(i), 0);
        tb_assert_and_check_break(threads[i]);

        // set thread affinity
        TB_CPUSET_ZERO(&cpuset); 
        TB_CPUSET_SET(i, &cpuset);
        if (!tb_thread_setaffinity(threads[i], &cpuset))
            tb_trace_e("set cpu core(%zu) failed for thread(%zu)", i, i);
    }

接口使用上基本上兼容,pthread.h/sched.h里面的那两接口。

  • tb_thread_setaffinity 基本上已经全平台支持了,包括win,linux,mac,ios等, android下还不行(ndk里面没提供相关接口)
  • tb_sched_setaffinitty linux, mac, android上都已实现,win上我过两天也会继续实现掉, ios不支持(ios也没权限操作其他进程)

关于cpuset的操作,我都定义在sched.h里面,不过用户只需要include "tbox/tbox.h"就行了,目前实现了大部分常用操作,并且对bits操作做了很多优化在里面。

// cpu affinity
#define TB_CPUSET_SIZE                              TB_CPU_BITSIZE
#define TB_CPUSET_FFS(pset)                         (tb_sched_affinity_cpu_ffs(pset))
#define TB_CPUSET_EMPTY(pset)                       (tb_sched_affinity_cpu_empty(pset))
#define TB_CPUSET_COUNT(pset)                       (tb_sched_affinity_cpu_count(pset))
#define TB_CPUSET_ZERO(pset)                        (tb_sched_affinity_cpu_zero(pset))
#define TB_CPUSET_SET(cpu, pset)                    (tb_sched_affinity_cpu_set((cpu), (pset)))
#define TB_CPUSET_CLR(cpu, pset)                    (tb_sched_affinity_cpu_clr((cpu), (pset)))
#define TB_CPUSET_ISSET(cpu, pset)                  (tb_sched_affinity_cpu_isset((cpu), (pset)))
#define TB_CPUSET_AND(pdstset, psrcset1, psrcset2)  (tb_sched_affinity_cpu_and((pdstset), (psrcset1), (psrcset2)))
#define TB_CPUSET_OR(pdstset, psrcset1, psrcset2)   (tb_sched_affinity_cpu_or((pdstset), (psrcset1), (psrcset2)))
#define TB_CPUSET_XOR(pdstset, psrcset1, psrcset2)  (tb_sched_affinity_cpu_xor((pdstset), (psrcset1), (psrcset2)))
#define TB_CPUSET_EQUAL(set1ptr, pset2)             (tb_sched_affinity_cpu_equal((set1ptr), (pset2)))
@waruqi

This comment has been minimized.

Copy link
Member

@waruqi waruqi commented Jul 14, 2019

tb_sched_setaffinity/tb_sched_getaffinity在win上也实现了

@waruqi waruqi closed this Jul 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.