Question

On Solaris, processor_bind is used to set affinity for threads. You need to know the LWPID of the target thread or use the constant P_MYID to refer to yourself.

I have a function that looks like this:

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);
}

In reality my function has a bunch of cross platform stuff in it that I've elided for clarity.

The key point is that I'd like to set the affinity of an arbitrary pthread_t so I can't use P_MYID.

How can I achieve this using processor_bind or an alternative interface?

Was it helpful?

Solution

Following up on this, and due to my confusion:

The lwpid is what is created by

pthread_create( &lwpid, NULL, some_func, NULL);

Thread data is available externally to a process that is not the one making the pthread_create() call - via the /proc interface

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

But this tells you almost nothing about which thread you are dealing with, except that it is the lwpid in the example above.

/proc/pid/lwp/lwpid/lwpsinfo

can be read into a struct lwpsinfo which has some more information, from which you might be able to ascertain if you are looking at the thread you want. see /usr/include/sys/procfs.h

Or man -s 4 proc

OTHER TIPS

The Solaris 11 kernel has critical threads optimization. You setup which threads require special care, the kernel does the rest. This appears to be what you want. Please read this short explanation to see if I understood what you want.

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

The above is an alternate. It may not fly at all for you. But is the preferred mechanism, per Oracle.

For Solaris 10, use the pthread_t tid of the LWP with an idtype_t of P_LWPID in your call to processor_bind. This works in Solaris 8 -> 11. It works ONLY for LWP's in the process. It is not clear to me if that is your model.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top