MongoDB  2.7.0
processinfo.h
1 // processinfo.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <string>
21 
22 #include "mongo/platform/cstdint.h"
23 #include "mongo/platform/process_id.h"
24 #include "mongo/db/jsobj.h"
25 
26 namespace mongo {
27 
28  class ProcessInfo {
29  public:
30  ProcessInfo( ProcessId pid = ProcessId::getCurrent() );
31  ~ProcessInfo();
32 
37 
41  int getResidentSize();
42 
46  const string& getOsType() const { return sysInfo().osType; }
47 
51  const string& getOsName() const { return sysInfo().osName; }
52 
56  const string& getOsVersion() const { return sysInfo().osVersion; }
57 
61  unsigned getAddrSize() const { return sysInfo().addrSize; }
62 
66  unsigned long long getMemSizeMB() const { return sysInfo().memSize / (1024 * 1024); }
67 
71  unsigned getNumCores() const { return sysInfo().numCores; }
72 
76  static unsigned long long getPageSize() { return systemInfo->pageSize; }
77 
81  const string& getArch() const { return sysInfo().cpuArch; }
82 
86  bool hasNumaEnabled() const { return sysInfo().hasNuma; }
87 
91  static bool isDataFileZeroingNeeded() { return systemInfo->fileZeroNeeded; }
92 
96  void appendSystemDetails( BSONObjBuilder& details ) const {
97  details.append( StringData("extra"), sysInfo()._extraStats.copy() );
98  }
99 
103  void getExtraInfo( BSONObjBuilder& info );
104 
105  bool supported();
106 
107  static bool blockCheckSupported();
108 
109  static bool blockInMemory(const void* start);
110 
116  inline static const void* alignToStartOfPage(const void* ptr) {
117  return reinterpret_cast<const void*>(
118  reinterpret_cast<unsigned long long>(ptr) & ~(getPageSize() - 1));
119  }
120 
129  static bool pagesInMemory(const void* start, size_t numPages, vector<char>* out);
130 
131  private:
135  class SystemInfo {
136  public:
137  string osType;
138  string osName;
139  string osVersion;
140  unsigned addrSize;
141  unsigned long long memSize;
142  unsigned numCores;
143  unsigned long long pageSize;
144  string cpuArch;
145  bool hasNuma;
146  BSONObj _extraStats;
147 
148  // This is an OS specific value, which determines whether files should be zero-filled
149  // at allocation time in order to avoid Microsoft KB 2731284.
150  //
151  bool fileZeroNeeded;
152 
153  SystemInfo() :
154  addrSize( 0 ),
155  memSize( 0 ),
156  numCores( 0 ),
157  pageSize( 0 ),
158  hasNuma( false ),
159  fileZeroNeeded (false) {
160  // populate SystemInfo during construction
161  collectSystemInfo();
162  }
163  private:
165  void collectSystemInfo();
166  };
167 
168  ProcessId _pid;
169  static mongo::mutex _sysInfoLock;
170 
171  static bool checkNumaEnabled();
172 
173  static ProcessInfo::SystemInfo* systemInfo;
174 
175  inline const SystemInfo& sysInfo() const {
176  return *systemInfo;
177  }
178 
179  public:
180  static void initializeSystemInfo();
181 
182  };
183 
184  bool writePidFile( const std::string& path );
185 
186  void printMemInfo( const char * whereContextStr = 0 );
187 
188 }
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:52
static bool isDataFileZeroingNeeded()
Determine if file zeroing is necessary for newly allocated data files.
Definition: processinfo.h:91
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition: bsonobjbuilder.h:85
const string & getOsName() const
Get the os Name (e.g.
Definition: processinfo.h:51
int getResidentSize()
Definition: processinfo_darwin.cpp:74
static const void * alignToStartOfPage(const void *ptr)
Definition: processinfo.h:116
LogstreamBuilder out()
Synonym for log().
Definition: log.h:79
A generic pointer type for function arguments.
Definition: goodies.h:176
BSON classes.
unsigned long long getMemSizeMB() const
Get the total amount of system memory in MB.
Definition: processinfo.h:66
void appendSystemDetails(BSONObjBuilder &details) const
Get extra system stats.
Definition: processinfo.h:96
void getExtraInfo(BSONObjBuilder &info)
Append platform-specific data to obj.
Definition: processinfo_darwin.cpp:98
const string & getOsVersion() const
Get the os version (e.g.
Definition: processinfo.h:56
unsigned getNumCores() const
Get the number of CPUs.
Definition: processinfo.h:71
bool hasNumaEnabled() const
Determine if NUMA is enabled (interleaved) for this process.
Definition: processinfo.h:86
On pthread systems, it is an error to destroy a mutex while held (boost mutex may use pthread)...
Definition: mutex.h:74
int getVirtualMemorySize()
Definition: processinfo_darwin.cpp:51
Definition: processinfo.h:28
const string & getArch() const
Get the CPU architecture (e.g.
Definition: processinfo.h:81
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition: bsonobj.h:77
static bool pagesInMemory(const void *start, size_t numPages, vector< char > *out)
Sets i-th element of 'out' to non-zero if the i-th page starting from the one containing 'start' is i...
Definition: processinfo_darwin.cpp:197
static unsigned long long getPageSize()
Get the system page size in bytes.
Definition: processinfo.h:76
unsigned getAddrSize() const
Get the cpu address size (e.g.
Definition: processinfo.h:61
const string & getOsType() const
Get the type of os (e.g.
Definition: processinfo.h:46