CUBRID Engine  latest
cpuinfo.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /*
4 
5  Heap Layers: An Extensible Memory Allocation Infrastructure
6 
7  Copyright (C) 2000-2020 by Emery Berger
8  http://www.emeryberger.com
9  emery@cs.umass.edu
10 
11  Heap Layers is distributed under the terms of the Apache 2.0 license.
12 
13  You may obtain a copy of the License at
14  http://www.apache.org/licenses/LICENSE-2.0
15 
16 */
17 
18 #ifndef HL_CPUINFO_H
19 #define HL_CPUINFO_H
20 
21 #if defined(_WIN32)
22 #include <windows.h>
23 #include <process.h>
24 #else
25 #include <unistd.h>
26 #endif
27 
28 
29 #if !defined(_WIN32)
30 #include <pthread.h>
31 #endif
32 
33 #if defined(__SVR4) // Solaris
34 #include <sys/lwp.h>
35 extern "C" unsigned int lwp_self(void);
36 #include <thread.h>
37 extern "C" int _thr_self(void);
38 #endif
39 
40 #if defined(__linux)
41 #include <sys/syscall.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <string.h>
46 #include <unistd.h>
47 #endif
48 
49 #if defined(__APPLE__)
50 #include <sys/types.h>
51 #include <sys/sysctl.h>
52 #endif
53 
54 #if defined(__sgi)
55 #include <sys/types.h>
56 #include <sys/sysmp.h>
57 #include <sys/sysinfo.h>
58 #endif
59 
60 #if defined(hpux)
61 #include <sys/mpctl.h>
62 #endif
63 
64 #if defined(_WIN32)
65 extern __declspec(thread) int localThreadId;
66 #endif
67 
68 #if defined(__SVR4) && defined(MAP_ALIGN)
69 extern volatile int anyThreadStackCreated;
70 #endif
71 
72 namespace HL {
73 
81 class CPUInfo {
82 public:
83 
84  // Good for practically all platforms.
85  enum { PageSize = 4096UL };
86 
87  inline static int getNumProcessors() {
88  static int _numProcessors = computeNumProcessors();
89  return _numProcessors;
90  }
91 
92  static inline unsigned int getThreadId();
93  inline static int computeNumProcessors();
94 
95 };
96 
97 
99 {
100  static int np = 0;
101  if (!np) {
102 #if defined(__linux) || defined(__APPLE__)
103  np = (int) sysconf(_SC_NPROCESSORS_ONLN);
104 #elif defined(_WIN32)
105  SYSTEM_INFO infoReturn[1];
106  GetSystemInfo (infoReturn);
107  np = (int) (infoReturn->dwNumberOfProcessors);
108 #elif defined(__sgi)
109  np = (int) sysmp(MP_NAPROCS);
110 #elif defined(hpux)
111  np = mpctl(MPC_GETNUMSPUS, NULL, NULL); // or pthread_num_processors_np()?
112 #elif defined(_SC_NPROCESSORS_ONLN)
113  np = (int) (sysconf(_SC_NPROCESSORS_ONLN));
114 #else
115  np = 2;
116  // Unsupported platform.
117  // Pretend we have at least two processors. This approach avoids the risk of assuming
118  // we're on a uniprocessor, which might lead clever allocators to avoid using atomic
119  // operations for all locks.
120 #endif
121  return np;
122  } else {
123  return np;
124  }
125 }
126 
127 #if defined(USE_THREAD_KEYWORD)
128  extern __thread int localThreadId;
129 #endif
130 
131 unsigned int CPUInfo::getThreadId() {
132 #if defined(__SVR4)
133  return (unsigned int) pthread_self();
134 #elif defined(_WIN32)
135  // It looks like thread id's are always multiples of 4, so...
136  return GetCurrentThreadId() >> 2;
137 #elif defined(__APPLE__)
138  // Consecutive thread id's in Mac OS are 4096 apart;
139  // dividing off the 4096 gives us an appropriate thread id.
140  unsigned int tid = (unsigned int) (((size_t) pthread_self()) >> 12);
141  return tid;
142 #elif defined(__BEOS__)
143  return find_thread(0);
144 #elif defined(__linux)
145  return (unsigned int) ((size_t) pthread_self()) >> 12;
146  // return (unsigned int) syscall (SYS_gettid);
147 #elif defined(PTHREAD_KEYS_MAX)
148  // As with Apple, above.
149  return (unsigned int) ((size_t) pthread_self()) >> 12;
150 #elif defined(POSIX)
151  return (unsigned int) pthread_self();
152 #elif USE_SPROC
153  // This hairiness has the same effect as calling getpid(),
154  // but it's MUCH faster since it avoids making a system call
155  // and just accesses the sproc-local data directly.
156  unsigned int pid = (unsigned int) PRDA->sys_prda.prda_sys.t_pid;
157  return pid;
158 #else
159 #error "This platform is not currently supported."
160  return 0;
161 #endif
162 }
163 
164 }
165 
166 #endif
static int computeNumProcessors()
Definition: cpuinfo.h:98
static unsigned int getThreadId()
Definition: cpuinfo.h:131
Definition: heaplayers.h:34
#define NULL
Definition: freelistheap.h:34
pid_t pid
Definition: dynamic_load.c:955
static void error(const char *msg)
Definition: gencat.c:331
static int getNumProcessors()
Definition: cpuinfo.h:87
Architecture-independent wrapper to get number of CPUs.
Definition: cpuinfo.h:81