The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
hw.c
Go to the documentation of this file.
1/*
2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2 of the
4 * License as published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
14 */
15
16/** Functions for getting information about the system architecture
17 *
18 * @file src/lib/util/hw.c
19 *
20 * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
21 * @copyright 2020 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
22 */
23
24#define CACHE_LINE_DEFAULT 128
25#define CORES_DEFAULT 1
26
27#include <freeradius-devel/util/hw.h>
28#include <freeradius-devel/util/syserror.h>
29#include <freeradius-devel/util/strerror.h>
30
31#if defined(__APPLE__) || defined(__FreeBSD__)
32#include <sys/sysctl.h>
33size_t fr_hw_cache_line_size(void)
34{
35 size_t cache_line_size = CACHE_LINE_DEFAULT;
36 size_t cache_line_size_len = sizeof(cache_line_size);
37
38 sysctlbyname("hw.cachelinesize", &cache_line_size, &cache_line_size_len, 0, 0);
39
40 return cache_line_size;
41}
42
44{
45 uint32_t num_cores = CORES_DEFAULT;
46 size_t num_cores_len = sizeof(num_cores);
47
48 sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);
49
50 return num_cores;
51}
52
53#elif defined(__linux__)
54#include <stdio.h>
55#include <unistd.h>
56size_t fr_hw_cache_line_size(void)
57{
58 FILE *file = NULL;
59 unsigned int cache_line_size = CACHE_LINE_DEFAULT;
60
61 file = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_cache_line_size", "r");
62 if (file) {
63 if (fscanf(file, "%d", &cache_line_size) != 1) {
64 cache_line_size = CACHE_LINE_DEFAULT;
65 }
66 fclose(file);
67 }
68
69 return cache_line_size;
70}
71
73{
74 uint32_t lcores = 0, tsibs = 0;
75
76 char buff[32];
77 char path[64];
78
79 for (lcores = 0;;lcores++) {
80 FILE *cpu;
81
82 snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);
83
84 cpu = fopen(path, "r");
85 if (!cpu) break;
86
87 while (fscanf(cpu, "%[0-9]", buff)) {
88 tsibs++;
89 if (fgetc(cpu) != ',') break;
90 }
91
92 fclose(cpu);
93 }
94
95 /*
96 * Catch Linux weirdness.
97 *
98 * You'd think this would be enough to quiet clang scan,
99 * but it's not.
100 */
101 if (unlikely((tsibs == 0) || (lcores == 0) || (lcores > tsibs))) {
102 fr_strerror_printf("Failed retrieving cpu topology info: %s", fr_syserror(errno));
103 return 0;
104 }
105
106#ifdef STATIC_ANALYZER
107 /*
108 * Prevent static analyzer from warning about divide by zero
109 */
110 if ((tsibs / lcores) == 0) return 0;
111#endif
112
113 return lcores / (tsibs / lcores);
114}
115#else
117{
118 return CACHE_LINE_DEFAULT;
119}
120
125#endif
int const char * file
Definition acutest.h:702
#define unlikely(_x)
Definition build.h:383
size_t fr_hw_cache_line_size(void)
Definition hw.c:116
#define CACHE_LINE_DEFAULT
Definition hw.c:24
#define CORES_DEFAULT
Definition hw.c:25
uint32_t fr_hw_num_cores_active(void)
Definition hw.c:121
unsigned int uint32_t
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64