Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
static int
hwloc_netbsd_get_proc_cpubind(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_pid_t pid, hwloc_bitmap_t hwloc_cpuset, int flags __hwloc_attribute_unused)
{
  int status;
  cpuset_t *cpuset = cpuset_create();
  status = sched_getaffinity_np(pid, cpuset_size(cpuset), cpuset);
  hwloc_netbsd_bsd2hwloc(hwloc_cpuset, cpuset);
  cpuset_destroy(cpuset);
  return status;
}
Ejemplo n.º 3
0
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);
}
int
_sys__sched_setaffinity(pid_t pid, lwpid_t lid,
		size_t size, const cpuset_t *cpuset)
{
	int ret = _asan__sys__sched_setaffinity(pid, lid,
			size, cpuset);

	if(ret == 0)
		ASAN_READ_RANGE(cpuset, cpuset_size(cpuset));

	return ret;
}
Ejemplo n.º 5
0
static int
hwloc_netbsd_set_thread_cpubind(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_thread_t tid, hwloc_const_bitmap_t hwloc_cpuset, int flags __hwloc_attribute_unused)
{
  int status;
  cpuset_t *cpuset = cpuset_create();
  hwloc_netbsd_hwloc2bsd(hwloc_cpuset, cpuset);
  status = pthread_setaffinity_np(tid, cpuset_size(cpuset), cpuset);
  cpuset_destroy(cpuset);

  if (status) {
    errno = status;
    return -1;
  }
  return 0;
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
0
static void
hwloc_netbsd_bsd2hwloc(hwloc_bitmap_t hwloc_cpuset, const cpuset_t *cpuset)
{
  unsigned cpu, cpulimit;
  int found = 0;
  hwloc_bitmap_zero(hwloc_cpuset);
  cpulimit = cpuset_size(cpuset) * CHAR_BIT;
  for (cpu = 0; cpu < cpulimit; cpu++)
    if (cpuset_isset(cpu, cpuset)) {
      hwloc_bitmap_set(hwloc_cpuset, cpu);
      found++;
    }
  /* when never bound, it returns an empty set, fill it instead */
  if (!found)
    hwloc_bitmap_fill(hwloc_cpuset);
}
Ejemplo n.º 8
0
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");
}
Ejemplo n.º 9
0
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;
}
Ejemplo n.º 10
0
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");
}
Ejemplo n.º 11
0
/* Return the number of processors available to the current process, based
   on a modern system call that returns the "affinity" between the current
   process and each CPU.  Return 0 if unknown or if such a system call does
   not exist.  */
static unsigned long
num_processors_via_affinity_mask (void)
{
    /* glibc >= 2.3.3 with NPTL and NetBSD 5 have pthread_getaffinity_np,
       but with different APIs.  Also it requires linking with -lpthread.
       Therefore this code is not enabled.
       glibc >= 2.3.4 has sched_getaffinity whereas NetBSD 5 has
       sched_getaffinity_np.  */
#if HAVE_PTHREAD_GETAFFINITY_NP && defined __GLIBC__ && 0
    {
        cpu_set_t set;

        if (pthread_getaffinity_np (pthread_self (), sizeof (set), &set) == 0)
        {
            unsigned long count;

# ifdef CPU_COUNT
            /* glibc >= 2.6 has the CPU_COUNT macro.  */
            count = CPU_COUNT (&set);
# else
            size_t i;

            count = 0;
            for (i = 0; i < CPU_SETSIZE; i++)
                if (CPU_ISSET (i, &set))
                    count++;
# endif
            if (count > 0)
                return count;
        }
    }
#elif HAVE_PTHREAD_GETAFFINITY_NP && defined __NetBSD__ && 0
    {
        cpuset_t *set;

        set = cpuset_create ();
        if (set != NULL)
        {
            unsigned long count = 0;

            if (pthread_getaffinity_np (pthread_self (), cpuset_size (set), set)
                    == 0)
            {
                cpuid_t i;

                for (i = 0;; i++)
                {
                    int ret = cpuset_isset (i, set);
                    if (ret < 0)
                        break;
                    if (ret > 0)
                        count++;
                }
            }
            cpuset_destroy (set);
            if (count > 0)
                return count;
        }
    }
#elif HAVE_SCHED_GETAFFINITY_LIKE_GLIBC /* glibc >= 2.3.4 */
    {
        cpu_set_t set;

        if (sched_getaffinity (0, sizeof (set), &set) == 0)
        {
            unsigned long count;

# ifdef CPU_COUNT
            /* glibc >= 2.6 has the CPU_COUNT macro.  */
            count = CPU_COUNT (&set);
# else
            size_t i;

            count = 0;
            for (i = 0; i < CPU_SETSIZE; i++)
                if (CPU_ISSET (i, &set))
                    count++;
# endif
            if (count > 0)
                return count;
        }
    }
#elif HAVE_SCHED_GETAFFINITY_NP /* NetBSD >= 5 */
    {
        cpuset_t *set;

        set = cpuset_create ();
        if (set != NULL)
        {
            unsigned long count = 0;

            if (sched_getaffinity_np (getpid (), cpuset_size (set), set) == 0)
            {
                cpuid_t i;

                for (i = 0;; i++)
                {
                    int ret = cpuset_isset (i, set);
                    if (ret < 0)
                        break;
                    if (ret > 0)
                        count++;
                }
            }
            cpuset_destroy (set);
            if (count > 0)
                return count;
        }
    }
#endif

#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
    {   /* This works on native Windows platforms.  */
        DWORD_PTR process_mask;
        DWORD_PTR system_mask;

        if (GetProcessAffinityMask (GetCurrentProcess (),
                                    &process_mask, &system_mask))
        {
            DWORD_PTR mask = process_mask;
            unsigned long count = 0;

            for (; mask != 0; mask = mask >> 1)
                if (mask & 1)
                    count++;
            if (count > 0)
                return count;
        }
    }