• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-cpu.c
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)
UNCOV
60
{
×
UNCOV
61
#ifdef SYSCONF_NPROCESSORS_CONF_COMPAT
×
UNCOV
62
        long nprocs = -1;
×
UNCOV
63
    nprocs = sysconf(_SC_NPROCESSORS_CONF);
×
UNCOV
64
    if (nprocs < 1) {
×
65
        SCLogError("Couldn't retrieve the number of cpus "
×
66
                   "configured (%s)",
×
67
                strerror(errno));
×
68
        return 0;
×
69
    }
×
70

UNCOV
71
    if (nprocs > UINT16_MAX) {
×
72
        SCLogDebug("It seems that there are more than %d CPUs "
×
73
                   "configured on this system. You can modify util-cpu.{c,h} "
×
74
                   "to use uint32_t to support it", UINT16_MAX);
×
75
        return UINT16_MAX;
×
76
    }
×
77

UNCOV
78
    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
UNCOV
101
}
×
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)
UNCOV
109
{
×
UNCOV
110
#ifdef SYSCONF_NPROCESSORS_ONLN_COMPAT
×
UNCOV
111
    long nprocs = -1;
×
UNCOV
112
    nprocs = sysconf(_SC_NPROCESSORS_ONLN);
×
UNCOV
113
    if (nprocs < 1) {
×
114
        SCLogError("Couldn't retrieve the number of cpus "
×
115
                   "online (%s)",
×
116
                strerror(errno));
×
117
        return 0;
×
118
    }
×
119

UNCOV
120
    if (nprocs > UINT16_MAX) {
×
121
        SCLogDebug("It seems that there are more than %d CPUs online. "
×
122
                   "You can modify util-cpu.{c,h} to use uint32_t to "
×
123
                   "support it", UINT16_MAX);
×
124
        return UINT16_MAX;
×
125
    }
×
126

UNCOV
127
    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
UNCOV
135
}
×
136

137
/**
138
 * \brief Print a summary of CPUs detected (configured and online)
139
 */
140
void UtilCpuPrintSummary(void)
UNCOV
141
{
×
UNCOV
142
    uint16_t cpus_conf = UtilCpuGetNumProcessorsConfigured();
×
UNCOV
143
    uint16_t cpus_online = UtilCpuGetNumProcessorsOnline();
×
144

UNCOV
145
    SCLogDebug("CPUs Summary: ");
×
UNCOV
146
    if (cpus_conf > 0)
×
UNCOV
147
        SCLogDebug("CPUs configured: %"PRIu16, cpus_conf);
×
UNCOV
148
    if (cpus_online > 0)
×
UNCOV
149
        SCLogInfo("CPUs/cores online: %"PRIu16, cpus_online);
×
UNCOV
150
    if (cpus_online == 0 && cpus_conf == 0)
×
151
        SCLogInfo("Couldn't retrieve any information of CPU's, please, send your operating "
×
UNCOV
152
                  "system info and check util-cpu.{c,h}");
×
UNCOV
153
}
×
154

155
/**
156
 * Get the current number of ticks from the CPU.
157
 *
158
 * \todo We'll have to deal with removing ticks from the extra cpuids in between
159
 *       2 calls.
160
 */
161
uint64_t UtilCpuGetTicks(void)
UNCOV
162
{
×
UNCOV
163
    uint64_t val;
×
UNCOV
164
#if defined(__GNUC__) && (defined(__x86_64) || defined(_X86_64_) || defined(ia_64) || defined(__i386__))
×
UNCOV
165
#if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
×
UNCOV
166
    __asm__ __volatile__ (
×
UNCOV
167
    "xorl %%eax,%%eax\n\t"
×
UNCOV
168
    "cpuid\n\t"
×
UNCOV
169
    ::: "%rax", "%rbx", "%rcx", "%rdx");
×
170
#else
171
    __asm__ __volatile__ (
172
    "xorl %%eax,%%eax\n\t"
173
    "pushl %%ebx\n\t"
174
    "cpuid\n\t"
175
    "popl %%ebx\n\t"
176
    ::: "%eax", "%ecx", "%edx");
177
#endif
UNCOV
178
    uint32_t a, d;
×
UNCOV
179
    __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
×
UNCOV
180
    val = ((uint64_t)a) | (((uint64_t)d) << 32);
×
UNCOV
181
#if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
×
UNCOV
182
    __asm__ __volatile__ (
×
UNCOV
183
    "xorl %%eax,%%eax\n\t"
×
UNCOV
184
    "cpuid\n\t"
×
UNCOV
185
    ::: "%rax", "%rbx", "%rcx", "%rdx");
×
186
#else
187
    __asm__ __volatile__ (
188
    "xorl %%eax,%%eax\n\t"
189
    "pushl %%ebx\n\t"
190
    "cpuid\n\t"
191
    "popl %%ebx\n\t"
192
    ::: "%eax", "%ecx", "%edx");
193
#endif
194

195
#elif defined(__GNUC__) && defined(__aarch64__)
196
    __asm__ __volatile__("isb\n\t"
197
                         "mrs %0,  cntvct_el0\n\t"
198
                         : "=r"(val));
199
#else /* #if defined(__GNU__) */
200
//#warning Using inferior version of UtilCpuGetTicks
201
    struct timeval now;
202
    gettimeofday(&now, NULL);
203
    val = (now.tv_sec * 1000000) + now.tv_usec;
204
#endif
UNCOV
205
    return val;
×
UNCOV
206
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc