Blame libringbuffer/getcpu.h

Packit c04fcb
#ifndef _LTTNG_GETCPU_H
Packit c04fcb
#define _LTTNG_GETCPU_H
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Copyright (c) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; only
Packit c04fcb
 * version 2.1 of the License.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <urcu/compiler.h>
Packit c04fcb
#include <urcu/system.h>
Packit c04fcb
#include <urcu/arch.h>
Packit c04fcb
#include <config.h>
Packit c04fcb
Packit c04fcb
void lttng_ust_getcpu_init(void);
Packit c04fcb
Packit c04fcb
extern int (*lttng_get_cpu)(void);
Packit c04fcb
Packit c04fcb
#ifdef LTTNG_UST_DEBUG_VALGRIND
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Fallback on cpu 0 if liblttng-ust is build with Valgrind support.
Packit c04fcb
 * get_cpu() returns the current CPU number. It may change due to
Packit c04fcb
 * migration, so it is only statistically accurate.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int lttng_ust_get_cpu_internal(void)
Packit c04fcb
{
Packit c04fcb
	return 0;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#else
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * sched_getcpu.
Packit c04fcb
 */
Packit c04fcb
#ifdef __linux__
Packit c04fcb
Packit c04fcb
#if !HAVE_SCHED_GETCPU
Packit c04fcb
#include <sys/syscall.h>
Packit c04fcb
#define __getcpu(cpu, node, cache)	syscall(__NR_getcpu, cpu, node, cache)
Packit c04fcb
/*
Packit c04fcb
 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int lttng_ust_get_cpu_internal(void)
Packit c04fcb
{
Packit c04fcb
	int cpu, ret;
Packit c04fcb
Packit c04fcb
	ret = __getcpu(&cpu, NULL, NULL);
Packit c04fcb
	if (caa_unlikely(ret < 0))
Packit c04fcb
		return 0;
Packit c04fcb
	return cpu;
Packit c04fcb
}
Packit c04fcb
#else /* HAVE_SCHED_GETCPU */
Packit c04fcb
#include <sched.h>
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int lttng_ust_get_cpu_internal(void)
Packit c04fcb
{
Packit c04fcb
	int cpu;
Packit c04fcb
Packit c04fcb
	cpu = sched_getcpu();
Packit c04fcb
	if (caa_unlikely(cpu < 0))
Packit c04fcb
		return 0;
Packit c04fcb
	return cpu;
Packit c04fcb
}
Packit c04fcb
#endif	/* HAVE_SCHED_GETCPU */
Packit c04fcb
Packit c04fcb
#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
Packit c04fcb
 * number 0, with the assocated performance degradation on SMP.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
int lttng_ust_get_cpu_internal(void)
Packit c04fcb
{
Packit c04fcb
	return 0;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#else
Packit c04fcb
#error "Please add support for your OS into liblttng-ust/compat.h."
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
#endif
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
int lttng_ust_get_cpu(void)
Packit c04fcb
{
Packit c04fcb
	int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu);
Packit c04fcb
Packit c04fcb
	if (caa_likely(!getcpu)) {
Packit c04fcb
		return lttng_ust_get_cpu_internal();
Packit c04fcb
	} else {
Packit c04fcb
		return getcpu();
Packit c04fcb
	}
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#endif /* _LTTNG_GETCPU_H */