~lttng/lttng-ust/lttng-ust

« back to all changes in this revision

Viewing changes to src/common/ringbuffer/smp.c

  • Committer: Mathieu Desnoyers
  • Author(s): Michael Jeanson
  • Date: 2021-04-16 15:40:38 UTC
  • Revision ID: git-v1:74cc1f594e05b5a3524529f018d85efaad53f9a3
Remove duplicated 'smp' code

libcounter contains a namespaced copy of the smp code from libringbuffer,
remove it and move the original to libcommon.

Also add comments and hide some implementation details following the ABI
cleanup.

Change-Id: Ie7d78d4f34312c5c788abc07d1806de3ce6fc04c
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * SPDX-License-Identifier: LGPL-2.1-only
3
 
 *
4
 
 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5
 
 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
6
 
 */
7
 
 
8
 
#define _LGPL_SOURCE
9
 
#include <unistd.h>
10
 
#include <pthread.h>
11
 
#include "smp.h"
12
 
 
13
 
int __num_possible_cpus;
14
 
 
15
 
#if (defined(__GLIBC__) || defined( __UCLIBC__))
16
 
void _get_num_possible_cpus(void)
17
 
{
18
 
        int result;
19
 
 
20
 
        /* On Linux, when some processors are offline
21
 
         * _SC_NPROCESSORS_CONF counts the offline
22
 
         * processors, whereas _SC_NPROCESSORS_ONLN
23
 
         * does not. If we used _SC_NPROCESSORS_ONLN,
24
 
         * getcpu() could return a value greater than
25
 
         * this sysconf, in which case the arrays
26
 
         * indexed by processor would overflow.
27
 
         */
28
 
        result = sysconf(_SC_NPROCESSORS_CONF);
29
 
        if (result == -1)
30
 
                return;
31
 
        __num_possible_cpus = result;
32
 
}
33
 
 
34
 
#else
35
 
 
36
 
/*
37
 
 * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not
38
 
 * return the number of configured CPUs in the system but relies on the cpu
39
 
 * affinity mask of the current task.
40
 
 *
41
 
 * So instead we use a strategy similar to GLIBC's, counting the cpu
42
 
 * directories in "/sys/devices/system/cpu" and fallback on the value from
43
 
 * sysconf if it fails.
44
 
 */
45
 
 
46
 
#include <dirent.h>
47
 
#include <limits.h>
48
 
#include <stdlib.h>
49
 
#include <string.h>
50
 
#include <sys/types.h>
51
 
 
52
 
#define __max(a,b) ((a)>(b)?(a):(b))
53
 
 
54
 
void _get_num_possible_cpus(void)
55
 
{
56
 
        int result, count = 0;
57
 
        DIR *cpudir;
58
 
        struct dirent *entry;
59
 
 
60
 
        cpudir = opendir("/sys/devices/system/cpu");
61
 
        if (cpudir == NULL)
62
 
                goto end;
63
 
 
64
 
        /*
65
 
         * Count the number of directories named "cpu" followed by and
66
 
         * integer. This is the same strategy as glibc uses.
67
 
         */
68
 
        while ((entry = readdir(cpudir))) {
69
 
                if (entry->d_type == DT_DIR &&
70
 
                        strncmp(entry->d_name, "cpu", 3) == 0) {
71
 
 
72
 
                        char *endptr;
73
 
                        unsigned long cpu_num;
74
 
 
75
 
                        cpu_num = strtoul(entry->d_name + 3, &endptr, 10);
76
 
                        if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3)
77
 
                                        && (*endptr == '\0')) {
78
 
                                count++;
79
 
                        }
80
 
                }
81
 
        }
82
 
 
83
 
end:
84
 
        /*
85
 
         * Get the sysconf value as a fallback. Keep the highest number.
86
 
         */
87
 
        result = __max(sysconf(_SC_NPROCESSORS_CONF), count);
88
 
 
89
 
        /*
90
 
         * If both methods failed, don't store the value.
91
 
         */
92
 
        if (result < 1)
93
 
                return;
94
 
        __num_possible_cpus = result;
95
 
}
96
 
#endif