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#include <errno.h>
31
32#if defined(__APPLE__) || defined(__FreeBSD__)
33#include <sys/sysctl.h>
34size_t fr_hw_cache_line_size(void)
35{
36 size_t cache_line_size = CACHE_LINE_DEFAULT;
37 size_t cache_line_size_len = sizeof(cache_line_size);
38
39 sysctlbyname("hw.cachelinesize", &cache_line_size, &cache_line_size_len, 0, 0);
40
41 return cache_line_size;
42}
43
45{
46 uint32_t num_cores = CORES_DEFAULT;
47 size_t num_cores_len = sizeof(num_cores);
48
49 sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);
50
51 return num_cores;
52}
53
54#elif defined(__linux__)
55#include <stdio.h>
56#include <unistd.h>
57size_t fr_hw_cache_line_size(void)
58{
59 FILE *file = NULL;
60 unsigned int cache_line_size = CACHE_LINE_DEFAULT;
61
62 file = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_cache_line_size", "r");
63 if (file) {
64 if (fscanf(file, "%d", &cache_line_size) != 1) {
65 cache_line_size = CACHE_LINE_DEFAULT;
66 }
67 fclose(file);
68 }
69
70 return cache_line_size;
71}
72
74{
75 uint32_t lcores = 0, tsibs = 0;
76
77 char buff[32];
78 char path[64];
79
80 for (lcores = 0;;lcores++) {
81 FILE *cpu;
82
83 snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);
84
85 cpu = fopen(path, "r");
86 if (!cpu) break;
87
88 while (fscanf(cpu, "%[0-9]", buff)) {
89 tsibs++;
90 if (fgetc(cpu) != ',') break;
91 }
92
93 fclose(cpu);
94 }
95
96 /*
97 * Catch Linux weirdness.
98 *
99 * You'd think this would be enough to quiet clang scan,
100 * but it's not.
101 */
102 if (unlikely((tsibs == 0) || (lcores == 0) || (lcores > tsibs))) {
103 fr_strerror_printf("Failed retrieving cpu topology info: %s", fr_syserror(errno));
104 return 0;
105 }
106
107#ifdef STATIC_ANALYZER
108 /*
109 * Prevent static analyzer from warning about divide by zero
110 */
111 if ((tsibs / lcores) == 0) return 0;
112#endif
113
114 return lcores / (tsibs / lcores);
115}
116#else
118{
119 return CACHE_LINE_DEFAULT;
120}
121
126#endif
int const char * file
Definition acutest.h:702
#define unlikely(_x)
Definition build.h:381
size_t fr_hw_cache_line_size(void)
Definition hw.c:117
#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:122
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