Added a host call to get the number of CPUs. It should work on all POSIX unixes,...
authorGreg Clayton <gclayton@apple.com>
Sun, 17 Feb 2013 20:46:30 +0000 (20:46 +0000)
committerGreg Clayton <gclayton@apple.com>
Sun, 17 Feb 2013 20:46:30 +0000 (20:46 +0000)
llvm-svn: 175405

lldb/include/lldb/Host/Host.h
lldb/source/Host/common/Host.cpp

index 1e2b327..710ad55 100644 (file)
@@ -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, 
index 738a4ad..18a8fba 100644 (file)
@@ -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__)
 
 #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