Replace sysctl on Mac with a syscall that is supported in all kernels
This commit is contained in:
parent
a05f51ee08
commit
74288c81e4
@ -2,6 +2,7 @@
|
||||
#include "InMemoryBlockStore.h"
|
||||
#include <memory>
|
||||
#include <cpp-utils/assert/assert.h>
|
||||
#include <cpp-utils/system/get_total_memory.h>
|
||||
|
||||
using std::make_unique;
|
||||
using std::string;
|
||||
@ -53,10 +54,7 @@ uint64_t InMemoryBlockStore::numBlocks() const {
|
||||
}
|
||||
|
||||
uint64_t InMemoryBlockStore::estimateNumFreeBytes() const {
|
||||
//For windows, see http://stackoverflow.com/a/2513561/829568
|
||||
long numRAMPages = sysconf(_SC_PHYS_PAGES);
|
||||
long pageSize = sysconf(_SC_PAGE_SIZE);
|
||||
return numRAMPages*pageSize;
|
||||
return cpputils::system::get_total_memory();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "FakeBlockStore.h"
|
||||
#include <cpp-utils/assert/assert.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <cpp-utils/system/get_total_memory.h>
|
||||
|
||||
using std::make_shared;
|
||||
using std::string;
|
||||
@ -78,21 +79,7 @@ uint64_t FakeBlockStore::numBlocks() const {
|
||||
}
|
||||
|
||||
uint64_t FakeBlockStore::estimateNumFreeBytes() const {
|
||||
uint64_t mem;
|
||||
#ifdef __APPLE__
|
||||
size_t size = sizeof(mem);
|
||||
int result = sysctlbyname("hw.memsize", &mem, &size, nullptr, 0);
|
||||
if (0 != result) {
|
||||
throw std::runtime_error("sysctlbyname syscall failed");
|
||||
}
|
||||
#elif __linux__
|
||||
long numRAMPages = sysconf(_SC_PHYS_PAGES);
|
||||
long pageSize = sysconf(_SC_PAGESIZE);
|
||||
mem = numRAMPages * pageSize;
|
||||
#else
|
||||
#error Not supported on windows yet, TODO http://stackoverflow.com/a/2513561/829568
|
||||
#endif
|
||||
return mem;
|
||||
return cpputils::system::get_total_memory();
|
||||
}
|
||||
|
||||
}
|
||||
|
23
src/cpp-utils/system/get_total_memory.cpp
Normal file
23
src/cpp-utils/system/get_total_memory.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "get_total_memory.h"
|
||||
|
||||
namespace cpputils{
|
||||
namespace system {
|
||||
uint64_t get_total_memory() {
|
||||
uint64_t mem;
|
||||
#ifdef __APPLE__
|
||||
size_t size = sizeof(mem);
|
||||
int result = sysctlbyname("hw.memsize", &mem, &size, nullptr, 0);
|
||||
if (0 != result) {
|
||||
throw std::runtime_error("sysctlbyname syscall failed");
|
||||
}
|
||||
#elif __linux__
|
||||
long numRAMPages = sysconf(_SC_PHYS_PAGES);
|
||||
long pageSize = sysconf(_SC_PAGESIZE);
|
||||
mem = numRAMPages * pageSize;
|
||||
#else
|
||||
#error Not supported on windows yet, TODO http://stackoverflow.com/a/2513561/829568
|
||||
#endif
|
||||
return mem;
|
||||
}
|
||||
}
|
||||
}
|
11
src/cpp-utils/system/get_total_memory.h
Normal file
11
src/cpp-utils/system/get_total_memory.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#ifndef MESSMER_CPPUTILS_SYSTEM_GETTOTALMEMORY_H
|
||||
#define MESSMER_CPPUTILS_SYSTEM_GETTOTALMEMORY_H
|
||||
|
||||
namespace cpputils {
|
||||
namespace system {
|
||||
uint64_t get_total_memory();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user