fix: num_possible_cpus() with hot-unplugged CPUs
[lttng-ust.git] / libringbuffer / smp.h
index 3d138a9a890ce69afede6b5975a334556823be39..1a880994bbe67a656c1e0ff499de67939df0390a 100644 (file)
@@ -4,12 +4,24 @@
 /*
  * libringbuffer/smp.h
  *
- * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * Dual LGPL v2.1/GPL v2 license.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; only
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <ust/core.h>
+#include "getcpu.h"
 
 /*
  * 4kB of per-cpu data available. Enough to hold the control structures,
 #define PER_CPU_MEM_SIZE       4096
 
 extern int __num_possible_cpus;
-extern void _get_num_possible_cpus(void);
 
-static inline
-int num_possible_cpus(void)
-{
-       if (!__num_possible_cpus)
-               _get_num_possible_cpus();
-       return __num_possible_cpus;
-}
+/*
+ * Get the CPU possible mask string from sysfs.
+ *
+ * buf: the buffer where the mask will be read.
+ * max_bytes: the maximum number of bytes to write in the buffer.
+ *
+ * Returns the number of bytes read or -1 on error.
+ */
+int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes)
+       __attribute__((visibility("hidden")));
 
 /*
- * get_cpu() returns the current CPU number. It may change due to
- * migration, so it is only statistically accurate.
+ * Get the number of possible CPUs in the system from either
+ * sysconf(_SC_NPROCESSORS_CONF) or some other mechanism depending on the libc.
+ *
+ * Returns the number of possible CPUs in the system or 0 on error.
  */
-#ifndef UST_VALGRIND
-static inline
-int get_cpu(void)
-{
-       int cpu;
+int get_num_possible_cpus_fallback(void)
+       __attribute__((visibility("hidden")));
 
-       cpu = sched_getcpu();
-       if (likely(cpu >= 0))
-               return cpu;
-       /*
-        * If getcpu(2) is not implemented in the Kernel use CPU 0 as fallback.
-        */
-       return 0;
-}
+/*
+ * Get the number of CPUs from the possible cpu mask.
+ *
+ * pmask: the mask to parse.
+ * len: the len of the mask excluding '\0'.
+ *
+ * Returns the number of possible CPUs from the mask or 0 on error.
+ */
+int get_num_possible_cpus_from_mask(const char *pmask, size_t len)
+       __attribute__((visibility("hidden")));
 
-#else  /* #else #ifndef UST_VALGRIND */
-static inline
-int get_cpu(void)
-{
-       /*
-        * Valgrind does not support the sched_getcpu() vsyscall.
-        * It causes it to detect a segfault in the program and stop it.
-        * So if we want to check libust with valgrind, we have to refrain
-        * from using this call. TODO: it would probably be better to return
-        * other values too, to better test it.
-        */
-       return 0;
-}
-#endif /* #else #ifndef UST_VALGRIND */
+extern void _get_num_possible_cpus(void);
 
+/*
+ * Returns the total number of CPUs in the system. If the cache is not yet
+ * initialized, get the value from "/sys/devices/system/cpu/possible" or
+ * fallback to sysconf and cache it.
+ *
+ * If all methods fail, don't populate the cache and return 0.
+ */
 static inline
-void put_cpu(void)
+int num_possible_cpus(void)
 {
+       if (caa_unlikely(!__num_possible_cpus))
+               _get_num_possible_cpus();
+
+       return __num_possible_cpus;
 }
 
 #define for_each_possible_cpu(cpu)             \
This page took 0.024526 seconds and 4 git commands to generate.