diff --git a/src/Makefile.am b/src/Makefile.am index b6d3496b5b..6470688eb6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -75,7 +75,6 @@ detect-sid.c detect-sid.h \ detect-priority.c detect-priority.h \ detect-rev.c detect-rev.h \ detect-classtype.c detect-classtype.h \ -util-fmemopen.c util-fmemopen.h \ detect-reference.c detect-reference.h \ detect-tag.c detect-tag.h \ detect-threshold.c detect-threshold.h \ @@ -105,6 +104,8 @@ detect-dce-opnum.c detect-dce-opnum.h \ detect-dce-stub-data.c detect-dce-stub-data.h \ detect-urilen.c detect-urilen.h \ util-print.c util-print.h \ +util-fmemopen.c util-fmemopen.h \ +util-cpu.c util-cpu.h \ util-mpm.c util-mpm.h \ util-spm.c util-spm.h util-clock.h \ util-spm-bs.c util-spm-bs.h \ diff --git a/src/suricata.c b/src/suricata.c index 438c7a0560..92c69a0ed0 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -444,6 +444,7 @@ int main(int argc, char **argv) } SCLogInfo("This is %s version %s", PROG_NAME, PROG_VER); + UtilCpuPrintSummary(); if (!CheckValidDaemonModes(daemon, mode)) { exit(EXIT_FAILURE); diff --git a/src/util-cpu.c b/src/util-cpu.c new file mode 100644 index 0000000000..8537834074 --- /dev/null +++ b/src/util-cpu.c @@ -0,0 +1,147 @@ +/** + * Copyright (c) 2009 Open Information Security Foundation + * + * \author Pablo Rincon Crespo + * + * Retrieve CPU information (configured CPUs, online CPUs) + */ + +#include +#include +#include +#include +#include +#include "util-error.h" +#include "util-debug.h" +#include "suricata-common.h" + +/** + * Ok, if they should use sysconf, check that they have the macro's + * (syscalls) defined; + * + * Note: For windows it's different; Check the following: + * SYSTEM_INFO info; + * GetSystemInfo(&info); + * -> info.dwNumberOfProcessors; + */ +#ifdef _SC_NPROCESSORS_CONF +#define SYSCONF_NPROCESSORS_CONF_COMPAT +#endif + +#ifdef _SC_NPROCESSORS_ONLN +#define SYSCONF_NPROCESSORS_ONLN_COMPAT +#endif + +/* This one is available on Solaris 10 */ +#ifdef _SC_NPROCESSORS_MAX +#define SYSCONF_NPROCESSORS_MAX_COMPAT +#endif + +/** + * \brief Get the number of cpus configured in the system + * \retval 0 if the syscall is not available or we have an error; + * otherwise it will return the number of cpus configured + */ +uint16_t UtilCpuGetNumProcessorsConfigured() { +#ifdef SYSCONF_NPROCESSORS_CONF_COMPAT + long nprocs = -1; + nprocs = sysconf(_SC_NPROCESSORS_CONF); + if (nprocs < 1) { + SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus " + "configured (%s)", strerror(errno)); + return 0; + } + + if (nprocs > UINT16_MAX) { + SCLogDebug("It seems that there are more than %"PRIu16" CPUs " + "configured on this system. You can modify util-cpu.{c,h} " + "to use uint32_t to support it", UINT16_MAX); + return UINT16_MAX; + } + + return (uint16_t)nprocs; +#else + SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the number of cpus " + "configured, sysconf macro unavailable"); + return 0; +#endif +} + +/** + * \brief Get the number of cpus online in the system + * \retval 0 if the syscall is not available or we have an error; + * otherwise it will return the number of cpus online + */ +uint16_t UtilCpuGetNumProcessorsOnline() { +#ifdef SYSCONF_NPROCESSORS_ONLN_COMPAT + long nprocs = -1; + nprocs = sysconf(_SC_NPROCESSORS_ONLN); + if (nprocs < 1) { + SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the number of cpus " + "online (%s)", strerror(errno)); + return 0; + } + + if (nprocs > UINT16_MAX) { + SCLogDebug("It seems that there are more than %"PRIu16" CPUs online. " + "You can modify util-cpu.{c,h} to use uint32_t to " + "support it", UINT16_MAX); + return UINT16_MAX; + } + + return nprocs; +#else + SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the number of cpus online, " + "synconf macro unavailable"); + return 0; +#endif +} + +/** + * \brief Get the maximum number of cpus allowed in the system + * This syscall is present on Solaris, but it's not on linux + * or macosx. Maybe you should look at UtilCpuGetNumProcessorsConfig() + * \retval 0 if the syscall is not available or we have an error; + * otherwise it will return the number of cpus allowed + */ +uint16_t UtilCpuGetNumProcessorsMax() { +#ifdef SYSCONF_NPROCESSORS_MAX_COMPAT + long nprocs = -1; + nprocs = sysconf(_SC_NPROCESSORS_MAX); + if (nprocs < 1) { + SCLogError(SC_ERR_SYSCALL, "Couldn't retrieve the maximum number of cpus " + "allowed by the system (%s)", strerror(errno)); + return 0; + } + + if (nprocs > UINT16_MAX) { + SCLogDebug("It seems that the system support more that %"PRIu16" CPUs. You " + "can modify util-cpu.{c,h} to use uint32_t to support it", UINT16_MAX); + return UINT16_MAX; + } + + return (uint16_t)nprocs; +#else + SCLogError(SC_ERR_SYSCONF, "Couldn't retrieve the maximum number of cpus allowed by " + "the system, synconf macro unavailable"); + return 0; +#endif +} + +/** + * \brief Print a summary of CPUs detected (configured and online) + */ +void UtilCpuPrintSummary() { + uint16_t cpus_conf = UtilCpuGetNumProcessorsConfigured(); + uint16_t cpus_online = UtilCpuGetNumProcessorsOnline(); + + SCLogInfo("CPUs Summary: "); + if (cpus_conf > 0) + SCLogInfo("CPUs online: %"PRIu16, cpus_conf); + if (cpus_online > 0) + SCLogInfo("CPUs configured %"PRIu16, cpus_online); + if (cpus_online == 0 && cpus_conf == 0) + SCLogInfo("Couldn't retireve any information of CPU's, please, send your operating " + "system info and check util-cpu.{c,h}"); +} + diff --git a/src/util-cpu.h b/src/util-cpu.h new file mode 100644 index 0000000000..9135aac485 --- /dev/null +++ b/src/util-cpu.h @@ -0,0 +1,14 @@ +#ifndef __UTIL_CPU_H__ +#define __UTIL_CPU_H__ + +/* Processors configured: */ +uint16_t UtilCpuGetNumProcessorsConfigured(); +/* Processors online: */ +uint16_t UtilCpuGetNumProcessorsOnline(); + +/* Only on Solaris */ +uint16_t UtilCpuGetNumProcessorsMax(); + +void UtilCpuPrintSummary(); + +#endif /* __UTIL_CPU_H__ */ diff --git a/src/util-error.h b/src/util-error.h index f4fda33d0a..9bf4fe61df 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -39,6 +39,7 @@ typedef enum { SC_INITIALIZATION_ERROR, SC_THREAD_SPAWN_FAILED, SC_ERR_SYSCALL, + SC_ERR_SYSCONF, SC_INVALID_ARGUMENTS, SC_ERR_THREAD_CREATE_ERROR, SC_ERR_PERF_STATS_NOT_INIT,