int set_thread_affinity(int cpu) { int retval = -1; #if defined(CPU_ZERO) cpu_set_t cpu_mask; CPU_ZERO(&cpu_mask); if (cpu >= 0 && cpu <= CPU_SETSIZE) { CPU_SET(cpu, &cpu_mask); } else { fprintf (stderr, "Wrong cpu id: %d\n", cpu); return -1; } retval = pthread_setaffinity_np(pthread_self(), sizeof(cpu_mask), &cpu_mask); #elif defined(cpuset_create) cpuset_t *cpu_mask = cpuset_create(); cpuset_zero(cpu_mask); if (cpu >= 0 && cpu <= cpuset_size(cpu_mask)) { cpuset_set(cpu, cpu_mask); } else { fprintf (stderr, "Wrong cpu id: %d\n", cpu); return -1; } retval = pthread_setaffinity_np(0, cpuset_size(cpu_mask), cpu_mask); cpuset_destroy(cpu_mask); #else #error "no cpuset" #endif if (retval != 0) fprintf (stderr, "Error at pthread_setaffinity_np():\n"); return retval; }
static void hwloc_netbsd_hwloc2bsd(hwloc_const_bitmap_t hwloc_cpuset, cpuset_t *cpuset) { unsigned cpu, cpulimit; cpuset_zero(cpuset); cpulimit = cpuset_size(cpuset) * CHAR_BIT; for (cpu = 0; cpu < cpulimit; cpu++) if (hwloc_bitmap_isset(hwloc_cpuset, cpu)) cpuset_set(cpu, cpuset); }
static void intrctl_affinity(int argc, char **argv) { struct intrio_set iset; cpuset_t *cpuset; unsigned long index; int ch, error; index = ULONG_MAX; memset(&iset.intrid, 0, sizeof(iset.intrid)); while ((ch = getopt(argc, argv, "c:i:")) != -1) { switch (ch) { case 'c': index = strtoul(optarg, NULL, 10); break; case 'i': if (strnlen(optarg, ARG_MAX) > INTRIDBUF) usage(); strlcpy(iset.intrid, optarg, INTRIDBUF); break; default: usage(); } } if (iset.intrid[0] == '\0' || index == ULONG_MAX) usage(); if (index >= (u_long)sysconf(_SC_NPROCESSORS_CONF)) err(EXIT_FAILURE, "invalid cpu index"); cpuset = cpuset_create(); if (cpuset == NULL) err(EXIT_FAILURE, "create_cpuset()"); cpuset_zero(cpuset); cpuset_set(index, cpuset); iset.cpuset = cpuset; iset.cpuset_size = cpuset_size(cpuset); error = sysctlbyname("kern.intr.affinity", NULL, NULL, &iset, sizeof(iset)); cpuset_destroy(cpuset); if (error < 0) err(EXIT_FAILURE, "sysctl kern.intr.affinity"); }
int dt_setaffinity(dthread_t *thread, unsigned* cpu_id, size_t cpu_count) { if (thread == NULL) { return KNOT_EINVAL; } #ifdef HAVE_PTHREAD_SETAFFINITY_NP int ret = -1; /* Linux, FreeBSD interface. */ #if defined(HAVE_CPUSET_LINUX) || defined(HAVE_CPUSET_BSD) cpu_set_t set; CPU_ZERO(&set); for (unsigned i = 0; i < cpu_count; ++i) { CPU_SET(cpu_id[i], &set); } ret = pthread_setaffinity_np(thread->_thr, sizeof(cpu_set_t), &set); /* NetBSD interface. */ #elif defined(HAVE_CPUSET_NETBSD) cpuset_t *set = cpuset_create(); if (set == NULL) { return KNOT_ENOMEM; } cpuset_zero(set); for (unsigned i = 0; i < cpu_count; ++i) { cpuset_set(cpu_id[i], set); } ret = pthread_setaffinity_np(thread->_thr, cpuset_size(set), set); cpuset_destroy(set); #endif /* interface */ if (ret < 0) { return KNOT_ERROR; } #else /* HAVE_PTHREAD_SETAFFINITY_NP */ return KNOT_ENOTSUP; #endif return KNOT_EOK; }
static void intrctl_nointr(int argc, char **argv) { struct intrio_set iset; cpuset_t *cpuset; unsigned long index; int ch, error; index = ULONG_MAX; while ((ch = getopt(argc, argv, "c:")) != -1) { switch (ch) { case 'c': index = strtoul(optarg, NULL, 10); break; default: usage(); } } if (index == ULONG_MAX) usage(); if (index >= (u_long)sysconf(_SC_NPROCESSORS_CONF)) err(EXIT_FAILURE, "invalid cpu index"); cpuset = cpuset_create(); if (cpuset == NULL) err(EXIT_FAILURE, "create_cpuset()"); cpuset_zero(cpuset); cpuset_set(index, cpuset); iset.cpuset = cpuset; iset.cpuset_size = cpuset_size(cpuset); error = sysctlbyname("kern.intr.nointr", NULL, NULL, &iset, sizeof(iset)); cpuset_destroy(cpuset); if (error < 0) err(EXIT_FAILURE, "sysctl kern.intr.nointr"); }
int set_process_affinity(int cpu) { int retval = -1; #if defined(CPU_ZERO) cpu_set_t cpu_mask; CPU_ZERO(&cpu_mask); if (cpu >= 0 && cpu <= CPU_SETSIZE) { CPU_SET(cpu, &cpu_mask); } else { fprintf (stderr, "Wrong cpu id: %d\n", cpu); return -1; } retval = sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask); #elif defined(cpuset_create) cpuset_t *cpu_mask = cpuset_create(); cpuset_zero(cpu_mask); if (cpu >= 0 && cpu <= cpuset_size(cpu_mask)) { cpuset_set(cpu, cpu_mask); } else { fprintf (stderr, "Wrong cpu id: %d\n", cpu); return -1; } retval = _sched_setaffinity(0, 0, cpuset_size(cpu_mask), cpu_mask); cpuset_destroy(cpu_mask); #else #error "no cpuset" #endif if (retval == -1) perror("Error at sched_setaffinity()"); return retval; }