【问题标题】:Obtain LWP id from a pthread_t on Solaris to use with processor_bind从 Solaris 上的 pthread_t 获取 LWP id 以与 processor_bind 一起使用
【发布时间】:2012-12-14 15:55:34
【问题描述】:

在 Solaris 上,processor_bind 用于设置线程的亲和性。你需要知道目标线程的LWPID或者使用常量P_MYID来引用自己。

我有一个看起来像这样的函数:

void set_affinity(pthread_t thr, int cpu_number)
{
   id_t lwpid = what_do_I_call_here(thr);
   processor_bind(P_LWPID, lwpid, cpu_number, NULL);
}

实际上,我的函数中有一堆跨平台的东西,为了清楚起见,我已经省略了。

关键是我想设置任意 pthread_t 的亲和力,所以我不能使用P_MYID

如何使用processor_bind 或其他接口实现此目的?

【问题讨论】:

  • 相当于P_MYID; pthread_self()P_MYID 都不能在该代码中使用,因为它需要能够在任意目标线程上运行,而不仅仅是它自己。

标签: pthreads solaris setthreadaffinitymask


【解决方案1】:

跟进此事,由于我的困惑:

lwpid 是由

创建的
pthread_create( &lwpid, NULL, some_func, NULL);

线程数据可通过/proc 接口从外部提供给不是发出pthread_create() 调用的进程的进程

/proc/<pid>/lwp/<lwpid>/    lwpid == 1 is the main thread, 2 .. n are the lwpid in the above example.

但这几乎没有告诉你你正在处理哪个线程,除了它是上面示例中的 lwpid。

/proc/pid/lwp/lwpid/lwpsinfo

可以读入包含更多信息的 struct lwpsinfo,您可以从中确定是否正在查看所需的线程。见/usr/include/sys/procfs.h

man -s 4 proc

【讨论】:

  • pthread_create() 是否保证在 Solaris 10 和 11 上映射 lwpid -> pthread_t?
  • pthread_t 是一种数据类型。 pthread_create 的第一个参数将参数 1 修改为成功创建的线程的 lwpid 的值。是的,保证。在 Solaris 10 和 11 中。/usr/include/sys/types.h:typedef uint_t pthread_t; /* = thread.h 中的 thread_t */
【解决方案2】:

Solaris 11 内核具有关键的线程优化。您设置哪些线程需要特别小心,其余的由内核完成。这似乎是你想要的。请阅读这个简短的解释,看看我是否理解你想要的。

https://blogs.oracle.com/observatory/entry/critical_threads_optimization

以上是替代品。它可能根本不适合你。但是,根据 Oracle,这是首选机制。

对于 Solaris 10,在对 processor_bind 的调用中使用 LWP 的 pthread_t tidP_LWPIDidtype_t。这适用于 Solaris 8 -> 11。它仅适用于进程中的 LWP。我不清楚那是不是你的模型。

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2011-05-24
    • 2020-01-18
    • 1970-01-01
    • 2020-05-29
    • 2012-09-23
    • 2018-10-11
    相关资源
    最近更新 更多