Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/util-cpu.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2007-2013 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
/**
19
 * \file
20
 *
21
 * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
22
 *
23
 * Retrieve CPU information (configured CPUs, online CPUs)
24
 */
25
26
#include "suricata-common.h"
27
#include "util-error.h"
28
#include "util-debug.h"
29
#include "util-cpu.h"
30
#include "util-byte.h"
31
32
/**
33
 * Ok, if they should use sysconf, check that they have the macro's
34
 * (syscalls) defined;
35
 *
36
 * Note: For windows it's different; Check the following:
37
 *      SYSTEM_INFO info;
38
 *      GetSystemInfo(&info);
39
 *      -> info.dwNumberOfProcessors;
40
 */
41
#ifdef _SC_NPROCESSORS_CONF
42
#define SYSCONF_NPROCESSORS_CONF_COMPAT
43
#endif
44
45
#ifdef _SC_NPROCESSORS_ONLN
46
#define SYSCONF_NPROCESSORS_ONLN_COMPAT
47
#endif
48
49
/* This one is available on Solaris 10 */
50
#ifdef _SC_NPROCESSORS_MAX
51
#define SYSCONF_NPROCESSORS_MAX_COMPAT
52
#endif
53
54
/**
55
 * \brief Get the number of cpus configured in the system
56
 * \retval 0 if the syscall is not available or we have an error;
57
 *           otherwise it will return the number of cpus configured
58
 */
59
uint16_t UtilCpuGetNumProcessorsConfigured(void)
60
0
{
61
0
#ifdef SYSCONF_NPROCESSORS_CONF_COMPAT
62
0
  long nprocs = -1;
63
0
    nprocs = sysconf(_SC_NPROCESSORS_CONF);
64
0
    if (nprocs < 1) {
65
0
        SCLogError("Couldn't retrieve the number of cpus "
66
0
                   "configured (%s)",
67
0
                strerror(errno));
68
0
        return 0;
69
0
    }
70
71
0
    if (nprocs > UINT16_MAX) {
72
0
        SCLogDebug("It seems that there are more than %d CPUs "
73
0
                   "configured on this system. You can modify util-cpu.{c,h} "
74
0
                   "to use uint32_t to support it", UINT16_MAX);
75
0
        return UINT16_MAX;
76
0
    }
77
78
0
    return (uint16_t)nprocs;
79
#elif OS_WIN32
80
    int64_t nprocs = 0;
81
    const char* envvar = getenv("NUMBER_OF_PROCESSORS");
82
    if (envvar != NULL) {
83
        if (StringParseInt64(&nprocs, 10, 0, envvar) < 0) {
84
            SCLogWarning("Invalid value for number of "
85
                         "processors: %s",
86
                    envvar);
87
            return 0;
88
        }
89
    }
90
    if (nprocs < 1) {
91
        SCLogError("Couldn't retrieve the number of cpus "
92
                   "configured from the NUMBER_OF_PROCESSORS environment variable");
93
        return 0;
94
    }
95
    return (uint16_t)nprocs;
96
#else
97
    SCLogError("Couldn't retrieve the number of cpus "
98
               "configured, sysconf macro unavailable");
99
    return 0;
100
#endif
101
0
}
102
103
/**
104
 * \brief Get the number of cpus online in the system
105
 * \retval 0 if the syscall is not available or we have an error;
106
 *           otherwise it will return the number of cpus online
107
 */
108
uint16_t UtilCpuGetNumProcessorsOnline(void)
109
0
{
110
0
#ifdef SYSCONF_NPROCESSORS_ONLN_COMPAT
111
0
    long nprocs = -1;
112
0
    nprocs = sysconf(_SC_NPROCESSORS_ONLN);
113
0
    if (nprocs < 1) {
114
0
        SCLogError("Couldn't retrieve the number of cpus "
115
0
                   "online (%s)",
116
0
                strerror(errno));
117
0
        return 0;
118
0
    }
119
120
0
    if (nprocs > UINT16_MAX) {
121
0
        SCLogDebug("It seems that there are more than %d CPUs online. "
122
0
                   "You can modify util-cpu.{c,h} to use uint32_t to "
123
0
                   "support it", UINT16_MAX);
124
0
        return UINT16_MAX;
125
0
    }
126
127
0
    return (uint16_t)nprocs;
128
#elif OS_WIN32
129
  return UtilCpuGetNumProcessorsConfigured();
130
#else
131
    SCLogError("Couldn't retrieve the number of cpus online, "
132
               "synconf macro unavailable");
133
    return 0;
134
#endif
135
0
}
136
137
/**
138
 * \brief Get the maximum number of cpus allowed in the system
139
 *        This syscall is present on Solaris, but it's not on linux
140
 *        or macosx. Maybe you should look at UtilCpuGetNumProcessorsConfigured()
141
 * \retval 0 if the syscall is not available or we have an error;
142
 *           otherwise it will return the number of cpus allowed
143
 */
144
uint16_t UtilCpuGetNumProcessorsMax(void)
145
0
{
146
#ifdef SYSCONF_NPROCESSORS_MAX_COMPAT
147
    long nprocs = -1;
148
    nprocs = sysconf(_SC_NPROCESSORS_MAX);
149
    if (nprocs < 1) {
150
        SCLogError("Couldn't retrieve the maximum number of cpus "
151
                   "allowed by the system (%s)",
152
                strerror(errno));
153
        return 0;
154
    }
155
156
    if (nprocs > UINT16_MAX) {
157
        SCLogDebug("It seems that the system support more that %"PRIu16" CPUs. You "
158
                   "can modify util-cpu.{c,h} to use uint32_t to support it", UINT16_MAX);
159
        return UINT16_MAX;
160
    }
161
162
    return (uint16_t)nprocs;
163
#else
164
0
    SCLogError("Couldn't retrieve the maximum number of cpus allowed by "
165
0
               "the system, synconf macro unavailable");
166
0
    return 0;
167
0
#endif
168
0
}
169
170
/**
171
 * \brief Print a summary of CPUs detected (configured and online)
172
 */
173
void UtilCpuPrintSummary(void)
174
0
{
175
0
    uint16_t cpus_conf = UtilCpuGetNumProcessorsConfigured();
176
0
    uint16_t cpus_online = UtilCpuGetNumProcessorsOnline();
177
178
0
    SCLogDebug("CPUs Summary: ");
179
0
    if (cpus_conf > 0)
180
0
        SCLogDebug("CPUs configured: %"PRIu16, cpus_conf);
181
0
    if (cpus_online > 0)
182
0
        SCLogInfo("CPUs/cores online: %"PRIu16, cpus_online);
183
0
    if (cpus_online == 0 && cpus_conf == 0)
184
0
        SCLogInfo("Couldn't retrieve any information of CPU's, please, send your operating "
185
0
                  "system info and check util-cpu.{c,h}");
186
0
}
187
188
/**
189
 * Get the current number of ticks from the CPU.
190
 *
191
 * \todo We'll have to deal with removing ticks from the extra cpuids in between
192
 *       2 calls.
193
 */
194
uint64_t UtilCpuGetTicks(void)
195
0
{
196
0
    uint64_t val;
197
0
#if defined(__GNUC__) && (defined(__x86_64) || defined(_X86_64_) || defined(ia_64) || defined(__i386__))
198
0
#if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
199
0
    __asm__ __volatile__ (
200
0
    "xorl %%eax,%%eax\n\t"
201
0
    "cpuid\n\t"
202
0
    ::: "%rax", "%rbx", "%rcx", "%rdx");
203
#else
204
    __asm__ __volatile__ (
205
    "xorl %%eax,%%eax\n\t"
206
    "pushl %%ebx\n\t"
207
    "cpuid\n\t"
208
    "popl %%ebx\n\t"
209
    ::: "%eax", "%ecx", "%edx");
210
#endif
211
0
    uint32_t a, d;
212
0
    __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
213
0
    val = ((uint64_t)a) | (((uint64_t)d) << 32);
214
0
#if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
215
0
    __asm__ __volatile__ (
216
0
    "xorl %%eax,%%eax\n\t"
217
0
    "cpuid\n\t"
218
0
    ::: "%rax", "%rbx", "%rcx", "%rdx");
219
#else
220
    __asm__ __volatile__ (
221
    "xorl %%eax,%%eax\n\t"
222
    "pushl %%ebx\n\t"
223
    "cpuid\n\t"
224
    "popl %%ebx\n\t"
225
    ::: "%eax", "%ecx", "%edx");
226
#endif
227
228
#else /* #if defined(__GNU__) */
229
//#warning Using inferior version of UtilCpuGetTicks
230
    struct timeval now;
231
    gettimeofday(&now, NULL);
232
    val = (now.tv_sec * 1000000) + now.tv_usec;
233
#endif
234
0
    return val;
235
0
}