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("Couldn't retrieve the number of cpus "
66  "configured (%s)",
67  strerror(errno));
68  return 0;
69  }
70 
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 
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
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  */
109 {
110 #ifdef SYSCONF_NPROCESSORS_ONLN_COMPAT
111  long nprocs = -1;
112  nprocs = sysconf(_SC_NPROCESSORS_ONLN);
113  if (nprocs < 1) {
114  SCLogError("Couldn't retrieve the number of cpus "
115  "online (%s)",
116  strerror(errno));
117  return 0;
118  }
119 
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 
127  return (uint16_t)nprocs;
128 #elif OS_WIN32
130 #else
131  SCLogError("Couldn't retrieve the number of cpus online, "
132  "synconf macro unavailable");
133  return 0;
134 #endif
135 }
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  */
145 {
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  SCLogError("Couldn't retrieve the maximum number of cpus allowed by "
165  "the system, synconf macro unavailable");
166  return 0;
167 #endif
168 }
169 
170 /**
171  * \brief Print a summary of CPUs detected (configured and online)
172  */
174 {
175  uint16_t cpus_conf = UtilCpuGetNumProcessorsConfigured();
176  uint16_t cpus_online = UtilCpuGetNumProcessorsOnline();
177 
178  SCLogDebug("CPUs Summary: ");
179  if (cpus_conf > 0)
180  SCLogDebug("CPUs configured: %"PRIu16, cpus_conf);
181  if (cpus_online > 0)
182  SCLogInfo("CPUs/cores online: %"PRIu16, cpus_online);
183  if (cpus_online == 0 && cpus_conf == 0)
184  SCLogInfo("Couldn't retrieve any information of CPU's, please, send your operating "
185  "system info and check util-cpu.{c,h}");
186 }
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 {
196  uint64_t val;
197 #if defined(__GNUC__) && (defined(__x86_64) || defined(_X86_64_) || defined(ia_64) || defined(__i386__))
198 #if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
199  __asm__ __volatile__ (
200  "xorl %%eax,%%eax\n\t"
201  "cpuid\n\t"
202  ::: "%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  uint32_t a, d;
212  __asm__ __volatile__ ("rdtsc" : "=a" (a), "=d" (d));
213  val = ((uint64_t)a) | (((uint64_t)d) << 32);
214 #if defined(__x86_64) || defined(_X86_64_) || defined(ia_64)
215  __asm__ __volatile__ (
216  "xorl %%eax,%%eax\n\t"
217  "cpuid\n\t"
218  ::: "%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  return val;
235 }
util-byte.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
UtilCpuGetNumProcessorsConfigured
uint16_t UtilCpuGetNumProcessorsConfigured(void)
Get the number of cpus configured in the system.
Definition: util-cpu.c:59
UtilCpuGetTicks
uint64_t UtilCpuGetTicks(void)
Definition: util-cpu.c:194
util-debug.h
util-error.h
util-cpu.h
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
UtilCpuPrintSummary
void UtilCpuPrintSummary(void)
Print a summary of CPUs detected (configured and online)
Definition: util-cpu.c:173
StringParseInt64
int StringParseInt64(int64_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:617
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
suricata-common.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
UtilCpuGetNumProcessorsOnline
uint16_t UtilCpuGetNumProcessorsOnline(void)
Get the number of cpus online in the system.
Definition: util-cpu.c:108
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:144