Coverage Report

Created: 2025-12-14 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/freeradius-server/src/lib/util/hw.c
Line
Count
Source
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
0
#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>
33
size_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
43
uint32_t fr_hw_num_cores_active(void)
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>
56
size_t fr_hw_cache_line_size(void)
57
0
{
58
0
  FILE *file      = NULL;
59
0
  unsigned int cache_line_size  = CACHE_LINE_DEFAULT;
60
61
0
  file = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_cache_line_size", "r");
62
0
  if (file) {
63
0
    if (fscanf(file, "%d", &cache_line_size) != 1) {
64
0
      cache_line_size = CACHE_LINE_DEFAULT;
65
0
    }
66
0
    fclose(file);
67
0
  }
68
69
0
  return cache_line_size;
70
0
}
71
72
uint32_t fr_hw_num_cores_active(void)
73
0
{
74
0
  uint32_t lcores = 0, tsibs = 0;
75
76
0
  char buff[32];
77
0
  char path[64];
78
79
0
  for (lcores = 0;;lcores++) {
80
0
    FILE *cpu;
81
82
0
    snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);
83
84
0
    cpu = fopen(path, "r");
85
0
    if (!cpu) break;
86
87
0
    while (fscanf(cpu, "%[0-9]", buff)) {
88
0
      tsibs++;
89
0
      if (fgetc(cpu) != ',') break;
90
0
    }
91
92
0
    fclose(cpu);
93
0
  }
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
0
  if (unlikely((tsibs == 0) || (lcores == 0) || (lcores > tsibs))) {
102
0
    fr_strerror_printf("Failed retrieving cpu topology info: %s", fr_syserror(errno));
103
0
    return 0;
104
0
  }
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
0
  return lcores / (tsibs / lcores);
114
0
}
115
#else
116
size_t fr_hw_cache_line_size(void)
117
{
118
  return CACHE_LINE_DEFAULT;
119
}
120
121
uint32_t fr_hw_num_cores_active(void)
122
{
123
  return CORES_DEFAULT;
124
}
125
#endif