The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
hw.c
Go to the documentation of this file.
1/*
2 * This program 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
26/*
27 * fr_hw_num_cores_active() on Linux reports a failure as 0 cores,
28 * and does not use a default.
29 */
30#ifndef __linux__
31# define CORES_DEFAULT 1
32#endif
33
34#include <freeradius-devel/util/hw.h>
35#include <freeradius-devel/util/syserror.h>
36#include <freeradius-devel/util/strerror.h>
37
38#if defined(__APPLE__) || defined(__FreeBSD__)
39#include <sys/sysctl.h>
40size_t fr_hw_cache_line_size(void)
41{
42 size_t cache_line_size = CACHE_LINE_DEFAULT;
43 size_t cache_line_size_len = sizeof(cache_line_size);
44
45 sysctlbyname("hw.cachelinesize", &cache_line_size, &cache_line_size_len, 0, 0);
46
47 return cache_line_size;
48}
49
51{
52 uint32_t num_cores = CORES_DEFAULT;
53 size_t num_cores_len = sizeof(num_cores);
54
55 sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0);
56
57 return num_cores;
58}
59
60#elif defined(__linux__)
61#include <stdio.h>
62#include <unistd.h>
63size_t fr_hw_cache_line_size(void)
64{
65 FILE *file = NULL;
66 unsigned int cache_line_size = CACHE_LINE_DEFAULT;
67
68 file = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_cache_line_size", "r");
69 if (file) {
70 if (fscanf(file, "%u", &cache_line_size) != 1) {
71 cache_line_size = CACHE_LINE_DEFAULT;
72 }
73 fclose(file);
74 }
75
76 return cache_line_size;
77}
78
80{
81 uint32_t lcores = 0, tsibs = 0;
82
83 char buff[32];
84 char path[64];
85
86 for (lcores = 0;;lcores++) {
87 FILE *cpu;
88
89 snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);
90
91 cpu = fopen(path, "r");
92 if (!cpu) break;
93
94 /*
95 * Each number is a sibling. EOF is non-zero, so a
96 * test against zero does not stop at the end of the
97 * file. Test for the one requested conversion.
98 */
99 while (fscanf(cpu, "%31[0-9]", buff) == 1) {
100 tsibs++;
101 if (fgetc(cpu) != ',') break;
102 }
103
104 fclose(cpu);
105 }
106
107 /*
108 * Catch Linux weirdness.
109 *
110 * You'd think this would be enough to quiet clang scan,
111 * but it's not.
112 */
113 if (unlikely((tsibs == 0) || (lcores == 0) || (lcores > tsibs))) {
114 fr_strerror_printf("Failed retrieving cpu topology info: %s", fr_syserror(errno));
115 return 0;
116 }
117
118#ifdef STATIC_ANALYZER
119 /*
120 * Prevent static analyzer from warning about divide by zero
121 */
122 if ((tsibs / lcores) == 0) return 0;
123#endif
124
125 return lcores / (tsibs / lcores);
126}
127#else
129{
130 return CACHE_LINE_DEFAULT;
131}
132
137#endif
int const char * file
Definition acutest.h:855
#define unlikely(_x)
Definition build.h:486
size_t fr_hw_cache_line_size(void)
Definition hw.c:128
#define CACHE_LINE_DEFAULT
Definition hw.c:24
#define CORES_DEFAULT
Definition hw.c:31
uint32_t fr_hw_num_cores_active(void)
Definition hw.c:133
unsigned int uint32_t
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
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