suricata
util-cpu.c
Go to the documentation of this file.
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  */
60 {
61 #ifdef SYSCONF_NPROCESSORS_CONF_COMPAT
62  long nprocs = -1;
63  nprocs = sysconf(_SC_NPROCESSORS_CONF);
64  if (nprocs < 1) {
65  SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus "
66  "configured (%s)", strerror(errno));
67  return 0;
68  }
69 
70  if (nprocs > UINT16_MAX) {
71  SCLogDebug("It seems that there are more than %d CPUs "
72  "configured on this system. You can modify util-cpu.{c,h} "
73  "to use uint32_t to support it", UINT16_MAX);
74  return UINT16_MAX;
75  }
76 
77  return (uint16_t)nprocs;
78 #elif OS_WIN32
79  int64_t nprocs = 0;
80  const char* envvar = getenv("NUMBER_OF_PROCESSORS");
81  if (envvar != NULL) {
82  if (StringParseInt64(&nprocs, 10, 0, envvar) < 0) {
83  SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid value for number of "
84  "processors: %s", envvar);
85  return 0;
86  }
87  }
88  if (nprocs < 1) {
89  SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus "
90  "configured from the NUMBER_OF_PROCESSORS environment variable");
91  return 0;
92  }
93  return (uint16_t)nprocs;
94 #else
95  SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the number of cpus "
96  "configured, sysconf macro unavailable");
97  return 0;
98 #endif
99 }
100 
101 /**
102  * \brief Get the number of cpus online in the system
103  * \retval 0 if the syscall is not available or we have an error;
104  * otherwise it will return the number of cpus online
105  */
107 {
108 #ifdef SYSCONF_NPROCESSORS_ONLN_COMPAT
109  long nprocs = -1;
110  nprocs = sysconf(_SC_NPROCESSORS_ONLN);
111  if (nprocs < 1) {
112  SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus "
113  "online (%s)", strerror(errno));
114  return 0;
115  }
116 
117  if (nprocs > UINT16_MAX) {
118  SCLogDebug("It seems that there are more than %d CPUs online. "
119  "You can modify util-cpu.{c,h} to use uint32_t to "
120  "support it", UINT16_MAX);
121  return UINT16_MAX;
122  }
123 
124  return nprocs;
125 #elif OS_WIN32
127 #else
128  SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the number of cpus online, "
129  "synconf macro unavailable");
130  return 0;
131 #endif
132 }
133 
134 /**
135  * \brief Get the maximum number of cpus allowed in the system
136  * This syscall is present on Solaris, but it's not on linux
137  * or macosx. Maybe you should look at UtilCpuGetNumProcessorsConfigured()
138  * \retval 0 if the syscall is not available or we have an error;
139  * otherwise it will return the number of cpus allowed
140  */
142 {
143 #ifdef SYSCONF_NPROCESSORS_MAX_COMPAT
144  long nprocs = -1;
145  nprocs = sysconf(_SC_NPROCESSORS_MAX);
146  if (nprocs < 1) {
147  SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the maximum number of cpus "
148  "allowed by the system (%s)", strerror(errno));
149  return 0;
150  }
151 
152  if (nprocs > UINT16_MAX) {
153  SCLogDebug("It seems that the system support more that %"PRIu16" CPUs. You "
154  "can modify util-cpu.{c,h} to use uint32_t to support it", UINT16_MAX);
155  return UINT16_MAX;
156  }
157 
158  return (uint16_t)nprocs;
159 #else
160  SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the maximum number of cpus allowed by "
161  "the system, synconf macro unavailable");
162  return 0;
163 #endif
164 }
165 
166 /**
167  * \brief Print a summary of CPUs detected (configured and online)
168  */
170 {
171  uint16_t cpus_conf = UtilCpuGetNumProcessorsConfigured();
172  uint16_t cpus_online = UtilCpuGetNumProcessorsOnline();
173 
174  SCLogDebug("CPUs Summary: ");
175  if (cpus_conf > 0)
176  SCLogDebug("CPUs configured: %"PRIu16, cpus_conf);
177  if (cpus_online > 0)
178  SCLogInfo("CPUs/cores online: %"PRIu16, cpus_online);
179  if (cpus_online == 0 && cpus_conf == 0)
180  SCLogInfo("Couldn't retireve any information of CPU's, please, send your operating "
181  "system info and check util-cpu.{c,h}");
182 }
183 
184 /**
185  * Get the current number of ticks from the CPU.
186  *
187  * \todo We'll have to deal with removig ticks from the extra cpuids inbetween
188  * 2 calls.
189  */
190 uint64_t UtilCpuGetTicks(void)
191 {
192  uint64_t val;
193 #if defined(__GNUC__) && (defined(__x86_64) || defined(_X86_64_) || defined(ia_64) || defined(__i386__))
194 #if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
195  __asm__ __volatile__ (
196  "xorl %%eax,%%eax\n\t"
197  "cpuid\n\t"
198  ::: "%rax", "%rbx", "%rcx", "%rdx");
199 #else
200  __asm__ __volatile__ (
201  "xorl %%eax,%%eax\n\t"
202  "pushl %%ebx\n\t"
203  "cpuid\n\t"
204  "popl %%ebx\n\t"
205  ::: "%eax", "%ecx", "%edx");
206 #endif
207  uint32_t a, d;
208  __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
209  val = ((uint64_t)a) | (((uint64_t)d) << 32);
210 #if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
211  __asm__ __volatile__ (
212  "xorl %%eax,%%eax\n\t"
213  "cpuid\n\t"
214  ::: "%rax", "%rbx", "%rcx", "%rdx");
215 #else
216  __asm__ __volatile__ (
217  "xorl %%eax,%%eax\n\t"
218  "pushl %%ebx\n\t"
219  "cpuid\n\t"
220  "popl %%ebx\n\t"
221  ::: "%eax", "%ecx", "%edx");
222 #endif
223 
224 #else /* #if defined(__GNU__) */
225 //#warning Using inferior version of UtilCpuGetTicks
226  struct timeval now;
227  gettimeofday(&now, NULL);
228  val = (now.tv_sec * 1000000) + now.tv_usec;
229 #endif
230  return val;
231 }
util-byte.h
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
UtilCpuGetNumProcessorsConfigured
uint16_t UtilCpuGetNumProcessorsConfigured(void)
Get the number of cpus configured in the system.
Definition: util-cpu.c:59
SC_ERR_SYSCONF
@ SC_ERR_SYSCONF
Definition: util-error.h:81
UtilCpuGetTicks
uint64_t UtilCpuGetTicks(void)
Definition: util-cpu.c:190
util-debug.h
util-error.h
util-cpu.h
SC_ERR_SYSCALL
@ SC_ERR_SYSCALL
Definition: util-error.h:80
UtilCpuPrintSummary
void UtilCpuPrintSummary(void)
Print a summary of CPUs detected (configured and online)
Definition: util-cpu.c:169
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
StringParseInt64
int StringParseInt64(int64_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:608
suricata-common.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:106
UtilCpuGetNumProcessorsMax
uint16_t UtilCpuGetNumProcessorsMax(void)
Get the maximum number of cpus allowed in the system This syscall is present on Solaris,...
Definition: util-cpu.c:141