2016-09-07 09:16:31 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-07 09:16:31 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (__APPLE__)
|
2016-09-07 09:42:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2016-09-07 09:16:31 +00:00
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-07 09:16:31 +00:00
|
|
|
#include "memory.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "affinity.h"
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (__APPLE__)
|
2016-09-07 09:53:23 +00:00
|
|
|
static void CPU_ZERO (cpu_set_t *cs)
|
|
|
|
{
|
|
|
|
cs->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CPU_SET (int num, cpu_set_t *cs)
|
|
|
|
{
|
|
|
|
cs->count |= (1 << num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CPU_ISSET (int num, cpu_set_t *cs)
|
|
|
|
{
|
|
|
|
return (cs->count & (1 << num));
|
|
|
|
}
|
|
|
|
|
2016-09-07 09:16:31 +00:00
|
|
|
static int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
|
|
|
|
{
|
|
|
|
int core;
|
|
|
|
|
2016-09-07 09:42:05 +00:00
|
|
|
for (core = 0; core < (8 * (int) cpu_size); core++)
|
|
|
|
{
|
|
|
|
if (CPU_ISSET (core, cpu_set)) break;
|
|
|
|
}
|
2016-09-07 09:16:31 +00:00
|
|
|
|
|
|
|
thread_affinity_policy_data_t policy = { core };
|
|
|
|
|
|
|
|
const int rc = thread_policy_set (pthread_mach_thread_np (thread), THREAD_AFFINITY_POLICY, (thread_policy_t) &policy, 1);
|
|
|
|
|
2016-09-07 09:42:05 +00:00
|
|
|
if (rc != KERN_SUCCESS)
|
2016-09-07 09:16:31 +00:00
|
|
|
{
|
2016-09-07 09:42:05 +00:00
|
|
|
log_error ("ERROR: %s : %d", "thread_policy_set()", rc);
|
|
|
|
|
|
|
|
exit (-1);
|
2016-09-07 09:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void set_cpu_affinity (char *cpu_affinity)
|
|
|
|
{
|
|
|
|
#if defined(_WIN)
|
|
|
|
DWORD_PTR aff_mask = 0;
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
cpuset_t cpuset;
|
|
|
|
CPU_ZERO (&cpuset);
|
|
|
|
#elif defined(_POSIX)
|
|
|
|
cpu_set_t cpuset;
|
|
|
|
CPU_ZERO (&cpuset);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (cpu_affinity)
|
|
|
|
{
|
|
|
|
char *devices = mystrdup (cpu_affinity);
|
|
|
|
|
|
|
|
char *next = strtok (devices, ",");
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
uint cpu_id = atoi (next);
|
|
|
|
|
|
|
|
if (cpu_id == 0)
|
|
|
|
{
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (_WIN)
|
2016-09-07 09:16:31 +00:00
|
|
|
aff_mask = 0;
|
2016-09-07 20:29:57 +00:00
|
|
|
#elif defined (_POSIX)
|
2016-09-07 09:16:31 +00:00
|
|
|
CPU_ZERO (&cpuset);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpu_id > 32)
|
|
|
|
{
|
|
|
|
log_error ("ERROR: Invalid cpu_id %u specified", cpu_id);
|
|
|
|
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (_WIN)
|
2016-09-07 09:16:31 +00:00
|
|
|
aff_mask |= 1u << (cpu_id - 1);
|
2016-09-07 20:29:57 +00:00
|
|
|
#elif defined (_POSIX)
|
2016-09-07 09:16:31 +00:00
|
|
|
CPU_SET ((cpu_id - 1), &cpuset);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} while ((next = strtok (NULL, ",")) != NULL);
|
|
|
|
|
|
|
|
myfree (devices);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined( _WIN)
|
|
|
|
SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
|
|
|
|
SetThreadAffinityMask (GetCurrentThread (), aff_mask);
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
pthread_t thread = pthread_self ();
|
|
|
|
pthread_setaffinity_np (thread, sizeof (cpuset_t), &cpuset);
|
|
|
|
#elif defined(_POSIX)
|
|
|
|
pthread_t thread = pthread_self ();
|
|
|
|
pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
|
|
|
|
#endif
|
|
|
|
}
|