ClickHouse/base/common/getMemoryAmount.cpp

37 lines
721 B
C++
Raw Normal View History

2019-02-09 22:38:26 +00:00
#include <stdexcept>
#include "common/getMemoryAmount.h"
2020-12-16 21:23:41 +00:00
#include "common/getPageSize.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif
2020-11-29 12:13:10 +00:00
/** Returns the size of physical memory (RAM) in bytes.
* Returns 0 on unsupported platform
*/
2019-02-09 22:38:26 +00:00
uint64_t getMemoryAmountOrZero()
{
2020-11-29 12:13:10 +00:00
int64_t num_pages = sysconf(_SC_PHYS_PAGES);
if (num_pages <= 0)
return 0;
2020-02-17 19:09:56 +00:00
2020-12-16 21:23:41 +00:00
int64_t page_size = getPageSize();
2020-11-29 12:13:10 +00:00
if (page_size <= 0)
return 0;
2020-11-29 12:13:10 +00:00
return num_pages * page_size;
}
2019-02-09 22:38:26 +00:00
uint64_t getMemoryAmount()
{
auto res = getMemoryAmountOrZero();
if (!res)
throw std::runtime_error("Cannot determine memory amount");
return res;
}