diff options
Diffstat (limited to 'src/mongo/util/processinfo_freebsd.cpp')
-rw-r--r-- | src/mongo/util/processinfo_freebsd.cpp | 248 |
1 files changed, 121 insertions, 127 deletions
diff --git a/src/mongo/util/processinfo_freebsd.cpp b/src/mongo/util/processinfo_freebsd.cpp index 8b7de762394..f335addfd9e 100644 --- a/src/mongo/util/processinfo_freebsd.cpp +++ b/src/mongo/util/processinfo_freebsd.cpp @@ -49,148 +49,142 @@ using namespace std; namespace mongo { - ProcessInfo::ProcessInfo(ProcessId pid) : _pid( pid ) { - } +ProcessInfo::ProcessInfo(ProcessId pid) : _pid(pid) {} - ProcessInfo::~ProcessInfo() { - } +ProcessInfo::~ProcessInfo() {} - /** - * Get a sysctl string value by name. Use string specialization by default. - */ - template <typename T> - int getSysctlByNameWithDefault(const char* sysctlName, - const T& defaultValue, - T* result); - - template <> - int getSysctlByNameWithDefault<uintptr_t>(const char* sysctlName, - const uintptr_t& defaultValue, - uintptr_t* result) { - uintptr_t value = 0; - size_t len = sizeof(value); - if (sysctlbyname(sysctlName, &value, &len, NULL, 0) == -1) { - *result = defaultValue; - return errno; - } - if (len > sizeof(value)) { - *result = defaultValue; - return EINVAL; - } - - *result = value; - return 0; +/** + * Get a sysctl string value by name. Use string specialization by default. + */ +template <typename T> +int getSysctlByNameWithDefault(const char* sysctlName, const T& defaultValue, T* result); + +template <> +int getSysctlByNameWithDefault<uintptr_t>(const char* sysctlName, + const uintptr_t& defaultValue, + uintptr_t* result) { + uintptr_t value = 0; + size_t len = sizeof(value); + if (sysctlbyname(sysctlName, &value, &len, NULL, 0) == -1) { + *result = defaultValue; + return errno; } - - template <> - int getSysctlByNameWithDefault<string>(const char* sysctlName, - const string& defaultValue, - string* result) { - char value[256] = {0}; - size_t len = sizeof(value); - if (sysctlbyname(sysctlName, &value, &len, NULL, 0) == -1) { - *result = defaultValue; - return errno; - } - *result = value; - return 0; + if (len > sizeof(value)) { + *result = defaultValue; + return EINVAL; } - bool ProcessInfo::checkNumaEnabled() { - return false; - } + *result = value; + return 0; +} - int ProcessInfo::getVirtualMemorySize() { - kvm_t *kd = NULL; - int cnt = 0; - char err[_POSIX2_LINE_MAX] = {0}; - if ((kd = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, err)) == NULL) - return -1; - kinfo_proc * task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), &cnt); - kvm_close(kd); - return task->ki_size / 1024 / 1024; // convert from bytes to MB +template <> +int getSysctlByNameWithDefault<string>(const char* sysctlName, + const string& defaultValue, + string* result) { + char value[256] = {0}; + size_t len = sizeof(value); + if (sysctlbyname(sysctlName, &value, &len, NULL, 0) == -1) { + *result = defaultValue; + return errno; } + *result = value; + return 0; +} - int ProcessInfo::getResidentSize() { - kvm_t *kd = NULL; - int cnt = 0; - char err[_POSIX2_LINE_MAX] = {0}; - if ((kd = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, err)) == NULL) - return -1; - kinfo_proc * task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), &cnt); - kvm_close(kd); - return task->ki_rssize * sysconf( _SC_PAGESIZE ) / 1024 / 1024; // convert from pages to MB - } +bool ProcessInfo::checkNumaEnabled() { + return false; +} - double ProcessInfo::getSystemMemoryPressurePercentage() { - return 0.0; - } +int ProcessInfo::getVirtualMemorySize() { + kvm_t* kd = NULL; + int cnt = 0; + char err[_POSIX2_LINE_MAX] = {0}; + if ((kd = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, err)) == NULL) + return -1; + kinfo_proc* task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), &cnt); + kvm_close(kd); + return task->ki_size / 1024 / 1024; // convert from bytes to MB +} - void ProcessInfo::SystemInfo::collectSystemInfo() { - osType = "BSD"; - osName = "FreeBSD"; - - int status = getSysctlByNameWithDefault("kern.version", string("unknown"), &osVersion); - if (status != 0) - log() << "Unable to collect OS Version. (errno: " - << status << " msg: " << strerror(status) << ")" << endl; - - status = getSysctlByNameWithDefault("hw.machine_arch", string("unknown"), &cpuArch); - if (status != 0) - log() << "Unable to collect Machine Architecture. (errno: " - << status << " msg: " << strerror(status) << ")" << endl; - addrSize = cpuArch.find("64") != std::string::npos ? 64 : 32; - - uintptr_t numBuffer; - uintptr_t defaultNum = 1; - status = getSysctlByNameWithDefault("hw.physmem", defaultNum, &numBuffer); - memSize = numBuffer; - if (status != 0) - log() << "Unable to collect Physical Memory. (errno: " - << status << " msg: " << strerror(status) << ")" << endl; - - status = getSysctlByNameWithDefault("hw.ncpu", defaultNum, &numBuffer); - numCores = numBuffer; - if (status != 0) - log() << "Unable to collect Number of CPUs. (errno: " - << status << " msg: " << strerror(status) << ")" << endl; - - pageSize = static_cast<unsigned long long>(sysconf(_SC_PAGESIZE)); - - hasNuma = checkNumaEnabled(); - } +int ProcessInfo::getResidentSize() { + kvm_t* kd = NULL; + int cnt = 0; + char err[_POSIX2_LINE_MAX] = {0}; + if ((kd = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, err)) == NULL) + return -1; + kinfo_proc* task = kvm_getprocs(kd, KERN_PROC_PID, _pid.toNative(), &cnt); + kvm_close(kd); + return task->ki_rssize * sysconf(_SC_PAGESIZE) / 1024 / 1024; // convert from pages to MB +} - void ProcessInfo::getExtraInfo( BSONObjBuilder& info ) { - } +double ProcessInfo::getSystemMemoryPressurePercentage() { + return 0.0; +} - bool ProcessInfo::supported() { - return true; - } +void ProcessInfo::SystemInfo::collectSystemInfo() { + osType = "BSD"; + osName = "FreeBSD"; + + int status = getSysctlByNameWithDefault("kern.version", string("unknown"), &osVersion); + if (status != 0) + log() << "Unable to collect OS Version. (errno: " << status << " msg: " << strerror(status) + << ")" << endl; + + status = getSysctlByNameWithDefault("hw.machine_arch", string("unknown"), &cpuArch); + if (status != 0) + log() << "Unable to collect Machine Architecture. (errno: " << status + << " msg: " << strerror(status) << ")" << endl; + addrSize = cpuArch.find("64") != std::string::npos ? 64 : 32; + + uintptr_t numBuffer; + uintptr_t defaultNum = 1; + status = getSysctlByNameWithDefault("hw.physmem", defaultNum, &numBuffer); + memSize = numBuffer; + if (status != 0) + log() << "Unable to collect Physical Memory. (errno: " << status + << " msg: " << strerror(status) << ")" << endl; + + status = getSysctlByNameWithDefault("hw.ncpu", defaultNum, &numBuffer); + numCores = numBuffer; + if (status != 0) + log() << "Unable to collect Number of CPUs. (errno: " << status + << " msg: " << strerror(status) << ")" << endl; + + pageSize = static_cast<unsigned long long>(sysconf(_SC_PAGESIZE)); + + hasNuma = checkNumaEnabled(); +} - bool ProcessInfo::blockCheckSupported() { - return true; - } +void ProcessInfo::getExtraInfo(BSONObjBuilder& info) {} + +bool ProcessInfo::supported() { + return true; +} - bool ProcessInfo::blockInMemory(const void* start) { - char x = 0; - if (mincore(alignToStartOfPage(start), getPageSize(), &x)) { - log() << "mincore failed: " << errnoWithDescription() << endl; - return 1; - } - return x & 0x1; +bool ProcessInfo::blockCheckSupported() { + return true; +} + +bool ProcessInfo::blockInMemory(const void* start) { + char x = 0; + if (mincore(alignToStartOfPage(start), getPageSize(), &x)) { + log() << "mincore failed: " << errnoWithDescription() << endl; + return 1; } + return x & 0x1; +} - bool ProcessInfo::pagesInMemory(const void* start, size_t numPages, vector<char>* out) { - out->resize(numPages); - // int mincore(const void *addr, size_t len, char *vec); - if (mincore(alignToStartOfPage(start), numPages * getPageSize(), - &(out->front()))) { - log() << "mincore failed: " << errnoWithDescription() << endl; - return false; - } - for (size_t i = 0; i < numPages; ++i) { - (*out)[i] = 0x1; - } - return true; +bool ProcessInfo::pagesInMemory(const void* start, size_t numPages, vector<char>* out) { + out->resize(numPages); + // int mincore(const void *addr, size_t len, char *vec); + if (mincore(alignToStartOfPage(start), numPages * getPageSize(), &(out->front()))) { + log() << "mincore failed: " << errnoWithDescription() << endl; + return false; + } + for (size_t i = 0; i < numPages; ++i) { + (*out)[i] = 0x1; } + return true; +} } |