summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Clayton <gclayton@apple.com>2013-02-17 20:46:30 +0000
committerGreg Clayton <gclayton@apple.com>2013-02-17 20:46:30 +0000
commite3e3feea3ccc782c65f850cb5a46b1c0bdc36179 (patch)
tree4d71ff2aefd595fb1ef452f30a8afece36766b8f
parent2192615d9f346f202d1770af853a32a538b88a20 (diff)
downloadbcm5719-llvm-e3e3feea3ccc782c65f850cb5a46b1c0bdc36179.tar.gz
bcm5719-llvm-e3e3feea3ccc782c65f850cb5a46b1c0bdc36179.zip
Added a host call to get the number of CPUs. It should work on all POSIX unixes, linux and Windows.
llvm-svn: 175405
-rw-r--r--lldb/include/lldb/Host/Host.h10
-rw-r--r--lldb/source/Host/common/Host.cpp95
2 files changed, 82 insertions, 23 deletions
diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h
index 1e2b327fe1c..710ad55b6e4 100644
--- a/lldb/include/lldb/Host/Host.h
+++ b/lldb/include/lldb/Host/Host.h
@@ -98,6 +98,16 @@ public:
static lldb::ByteOrder
GetByteOrder ();
+ //------------------------------------------------------------------
+ /// Returns the number of CPUs on this current host.
+ ///
+ /// @return
+ /// Number of CPUs on this current host, or zero if the number
+ /// of CPUs can't be determined on this host.
+ //------------------------------------------------------------------
+ static uint32_t
+ GetNumberCPUS ();
+
static bool
GetOSVersion (uint32_t &major,
uint32_t &minor,
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index 738a4adcdd8..18a8fbabee5 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -9,33 +9,16 @@
#include "lldb/lldb-python.h"
-#include "lldb/Host/Host.h"
-#include "lldb/Core/ArchSpec.h"
-#include "lldb/Core/ConstString.h"
-#include "lldb/Core/Debugger.h"
-#include "lldb/Core/Error.h"
-#include "lldb/Core/Log.h"
-#include "lldb/Core/StreamString.h"
-#include "lldb/Core/ThreadSafeSTLMap.h"
-#include "lldb/Host/Config.h"
-#include "lldb/Host/Endian.h"
-#include "lldb/Host/FileSpec.h"
-#include "lldb/Host/Mutex.h"
-#include "lldb/Target/Process.h"
-#include "lldb/Target/TargetList.h"
-
-#include "llvm/Support/Host.h"
-#include "llvm/Support/MachO.h"
-#include "llvm/ADT/Twine.h"
-
+// C includes
#include <dlfcn.h>
#include <errno.h>
#include <grp.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
+#include <sys/sysctl.h>
#include <sys/types.h>
-
+#include <unistd.h>
#if defined (__APPLE__)
@@ -43,8 +26,6 @@
#include <libproc.h>
#include <mach-o/dyld.h>
#include <mach/mach_port.h>
-#include <sys/sysctl.h>
-
#elif defined (__linux__)
@@ -53,11 +34,33 @@
#elif defined (__FreeBSD__)
#include <sys/wait.h>
-#include <sys/sysctl.h>
#include <pthread_np.h>
#endif
+#include "lldb/Host/Host.h"
+#include "lldb/Core/ArchSpec.h"
+#include "lldb/Core/ConstString.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/Error.h"
+#include "lldb/Core/Log.h"
+#include "lldb/Core/StreamString.h"
+#include "lldb/Core/ThreadSafeSTLMap.h"
+#include "lldb/Host/Config.h"
+#include "lldb/Host/Endian.h"
+#include "lldb/Host/FileSpec.h"
+#include "lldb/Host/Mutex.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/TargetList.h"
+
+#include "llvm/Support/Host.h"
+#include "llvm/Support/MachO.h"
+#include "llvm/ADT/Twine.h"
+
+
+
+
+
using namespace lldb;
using namespace lldb_private;
@@ -1433,6 +1436,52 @@ Host::RunShellCommand (const char *command,
}
+uint32_t
+Host::GetNumberCPUS ()
+{
+ static uint32_t g_num_cores = UINT32_MAX;
+ if (g_num_cores == UINT32_MAX)
+ {
+#if defined(__APPLE__) or defined (__linux__)
+
+ g_num_cores = ::sysconf(_SC_NPROCESSORS_ONLN);
+
+#elif defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
+
+ // Header file for this might need to be included at the top of this file
+ SYSTEM_INFO system_info;
+ ::GetSystemInfo (&system_info);
+ g_num_cores = system_info.dwNumberOfProcessors;
+
+#else
+
+ // Assume POSIX support if a host specific case has not been supplied above
+ g_num_cores = 0;
+ int num_cores = 0;
+ size_t num_cores_len = sizeof(num_cores);
+ int mib[] = { CTL_HW, HW_AVAILCPU };
+
+ /* get the number of CPUs from the system */
+ if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
+ {
+ g_num_cores = num_cores;
+ }
+ else
+ {
+ mib[1] = HW_NCPU;
+ num_cores_len = sizeof(num_cores);
+ if (sysctl(mib, sizeof(mib)/sizeof(int), &num_cores, &num_cores_len, NULL, 0) == 0 && (num_cores > 0))
+ {
+ if (num_cores > 0)
+ g_num_cores = num_cores;
+ }
+ }
+#endif
+ }
+ return g_num_cores;
+}
+
+
#if !defined (__APPLE__)
bool
OpenPOWER on IntegriCloud