ClickHouse/src/Common/MemoryStatisticsOS.h

55 lines
984 B
C++
Raw Normal View History

2020-04-19 20:49:13 +00:00
#pragma once
#if defined(OS_LINUX) || defined(OS_FREEBSD)
2020-04-19 20:49:13 +00:00
#include <cstdint>
#if defined(OS_FREEBSD)
#include <unistd.h>
#endif
2020-04-19 20:49:13 +00:00
namespace DB
{
2021-05-04 07:54:13 +00:00
/** Opens a file /proc/self/statm. Keeps it open and reads memory statistics via 'pread'.
2020-04-19 20:49:13 +00:00
* This is Linux specific.
* See: man procfs
*
* Note: a class is used instead of a single function to avoid excessive file open/close on every use.
* pread is used to avoid lseek.
2020-04-19 20:57:34 +00:00
*
* Actual performance is from 1 to 5 million iterations per second.
2020-04-19 20:49:13 +00:00
*/
class MemoryStatisticsOS
{
public:
/// In number of bytes.
struct Data
{
uint64_t virt;
uint64_t resident;
#if defined(OS_LINUX)
2020-04-19 20:49:13 +00:00
uint64_t shared;
#endif
2020-04-19 20:49:13 +00:00
uint64_t code;
uint64_t data_and_stack;
};
MemoryStatisticsOS();
~MemoryStatisticsOS();
/// Thread-safe.
Data get() const;
private:
#if defined(OS_LINUX)
2020-04-19 20:49:13 +00:00
int fd;
#endif
#if defined(OS_FREEBSD)
int pagesize;
pid_t self;
#endif
2020-04-19 20:49:13 +00:00
};
}
2020-07-15 11:16:00 +00:00
#endif