Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::utils::memory_profiler Class Reference

#include <memory_profiler.h>

Collaboration diagram for kcenon::network::utils::memory_profiler:
Collaboration graph

Public Member Functions

void start (std::chrono::milliseconds interval=std::chrono::milliseconds{1000})
 
void stop ()
 
memory_snapshot snapshot ()
 
std::vector< memory_snapshotget_history (std::size_t max_count=256) const
 
void clear_history ()
 
std::string to_tsv () const
 

Static Public Member Functions

static memory_profilerinstance ()
 

Private Member Functions

 memory_profiler ()=default
 
 ~memory_profiler ()
 
 memory_profiler (const memory_profiler &)=delete
 
memory_profileroperator= (const memory_profiler &)=delete
 
void schedule_next_sample ()
 

Static Private Member Functions

static bool query_process_memory (std::uint64_t &rss, std::uint64_t &vms)
 

Private Attributes

std::atomic< bool > running_ {false}
 
std::chrono::milliseconds sampling_interval_ {1000}
 
std::mutex mutex_ {}
 
std::vector< memory_snapshothistory_ {}
 
std::size_t max_history_ {4096}
 

Detailed Description

Definition at line 32 of file memory_profiler.h.

Constructor & Destructor Documentation

◆ memory_profiler() [1/2]

kcenon::network::utils::memory_profiler::memory_profiler ( )
privatedefault

◆ ~memory_profiler()

kcenon::network::utils::memory_profiler::~memory_profiler ( )
private

Definition at line 29 of file memory_profiler.cpp.

References stop().

Here is the call graph for this function:

◆ memory_profiler() [2/2]

kcenon::network::utils::memory_profiler::memory_profiler ( const memory_profiler & )
privatedelete

Member Function Documentation

◆ clear_history()

void kcenon::network::utils::memory_profiler::clear_history ( )

Definition at line 70 of file memory_profiler.cpp.

70 {
71 std::lock_guard<std::mutex> lock(mutex_);
72 history_.clear();
73}
std::vector< memory_snapshot > history_

References history_, and mutex_.

◆ get_history()

std::vector< memory_snapshot > kcenon::network::utils::memory_profiler::get_history ( std::size_t max_count = 256) const

Definition at line 64 of file memory_profiler.cpp.

64 {
65 std::lock_guard<std::mutex> lock(mutex_);
66 if (max_count >= history_.size()) return history_;
67 return std::vector<memory_snapshot>(history_.end() - max_count, history_.end());
68}

References history_, and mutex_.

◆ instance()

memory_profiler & kcenon::network::utils::memory_profiler::instance ( )
static

Definition at line 24 of file memory_profiler.cpp.

24 {
25 static memory_profiler s;
26 return s;
27}

◆ operator=()

memory_profiler & kcenon::network::utils::memory_profiler::operator= ( const memory_profiler & )
privatedelete

◆ query_process_memory()

bool kcenon::network::utils::memory_profiler::query_process_memory ( std::uint64_t & rss,
std::uint64_t & vms )
staticprivate

Definition at line 104 of file memory_profiler.cpp.

104 {
105#if defined(__APPLE__)
106 task_basic_info_data_t info;
107 mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
108 if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count) != KERN_SUCCESS) {
109 return false;
110 }
111 rss = static_cast<std::uint64_t>(info.resident_size);
112 vms = static_cast<std::uint64_t>(info.virtual_size);
113 return true;
114#elif defined(__linux__)
115 FILE* f = std::fopen("/proc/self/statm", "r");
116 if (!f) return false;
117 long pages_res=0, pages_total=0;
118 int rc = std::fscanf(f, "%ld %ld", &pages_total, &pages_res);
119 std::fclose(f);
120 if (rc != 2) return false;
121 long page_size = sysconf(_SC_PAGESIZE);
122 vms = static_cast<std::uint64_t>(pages_total) * static_cast<std::uint64_t>(page_size);
123 rss = static_cast<std::uint64_t>(pages_res) * static_cast<std::uint64_t>(page_size);
124 return true;
125#elif defined(_WIN32)
126 PROCESS_MEMORY_COUNTERS_EX pmc{};
127 if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc))) {
128 rss = static_cast<std::uint64_t>(pmc.WorkingSetSize);
129 vms = static_cast<std::uint64_t>(pmc.PrivateUsage);
130 return true;
131 }
132 return false;
133#else
134 // Unknown platform
135 rss = vms = 0;
136 return false;
137#endif
138}

Referenced by snapshot().

Here is the caller graph for this function:

◆ schedule_next_sample()

void kcenon::network::utils::memory_profiler::schedule_next_sample ( )
private

Definition at line 88 of file memory_profiler.cpp.

88 {
89#ifdef NETWORK_ENABLE_MEMORY_PROFILER
90 if (!running_.load()) return;
91
94 [this]() {
95 if (running_.load()) {
96 snapshot();
98 }
99 },
101#endif
102}
std::future< void > submit_delayed_task(std::function< void()> task, std::chrono::milliseconds delay)
Submit a task with delay.
static thread_integration_manager & instance()
Get the singleton instance.
std::chrono::milliseconds sampling_interval_

References kcenon::network::integration::thread_integration_manager::instance(), running_, sampling_interval_, schedule_next_sample(), snapshot(), and kcenon::network::integration::thread_integration_manager::submit_delayed_task().

Referenced by schedule_next_sample(), and start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ snapshot()

memory_snapshot kcenon::network::utils::memory_profiler::snapshot ( )

Definition at line 48 of file memory_profiler.cpp.

48 {
49 memory_snapshot snap;
50 snap.timestamp = std::chrono::system_clock::now();
51 std::uint64_t rss=0, vms=0;
52 query_process_memory(rss, vms);
53 snap.resident_bytes = rss;
54 snap.virtual_bytes = vms;
55
56 std::lock_guard<std::mutex> lock(mutex_);
57 history_.push_back(snap);
58 if (history_.size() > max_history_) {
59 history_.erase(history_.begin(), history_.begin() + (history_.size() - max_history_));
60 }
61 return snap;
62}
static bool query_process_memory(std::uint64_t &rss, std::uint64_t &vms)

References history_, max_history_, mutex_, query_process_memory(), kcenon::network::utils::memory_snapshot::resident_bytes, kcenon::network::utils::memory_snapshot::timestamp, and kcenon::network::utils::memory_snapshot::virtual_bytes.

Referenced by schedule_next_sample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start()

void kcenon::network::utils::memory_profiler::start ( std::chrono::milliseconds interval = std::chrono::milliseconds{1000})

Definition at line 31 of file memory_profiler.cpp.

31 {
32#ifndef NETWORK_ENABLE_MEMORY_PROFILER
33 (void)interval;
34 return; // profiling disabled
35#else
36 if (running_.exchange(true)) return;
37 sampling_interval_ = interval;
39#endif
40}

References running_, sampling_interval_, and schedule_next_sample().

Here is the call graph for this function:

◆ stop()

void kcenon::network::utils::memory_profiler::stop ( )

Definition at line 42 of file memory_profiler.cpp.

42 {
43#ifdef NETWORK_ENABLE_MEMORY_PROFILER
44 running_.store(false);
45#endif
46}

References running_.

Referenced by ~memory_profiler().

Here is the caller graph for this function:

◆ to_tsv()

std::string kcenon::network::utils::memory_profiler::to_tsv ( ) const

Definition at line 75 of file memory_profiler.cpp.

75 {
76 std::ostringstream oss;
77 std::lock_guard<std::mutex> lock(mutex_);
78 for (const auto& s : history_) {
79 auto t = std::chrono::system_clock::to_time_t(s.timestamp);
80 oss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S")
81 << '\t' << s.resident_bytes
82 << '\t' << s.virtual_bytes
83 << '\n';
84 }
85 return oss.str();
86}

References history_, and mutex_.

Member Data Documentation

◆ history_

std::vector<memory_snapshot> kcenon::network::utils::memory_profiler::history_ {}
private

Definition at line 69 of file memory_profiler.h.

69{};

Referenced by clear_history(), get_history(), snapshot(), and to_tsv().

◆ max_history_

std::size_t kcenon::network::utils::memory_profiler::max_history_ {4096}
private

Definition at line 70 of file memory_profiler.h.

70{4096};

Referenced by snapshot().

◆ mutex_

std::mutex kcenon::network::utils::memory_profiler::mutex_ {}
mutableprivate

Definition at line 68 of file memory_profiler.h.

68{};

Referenced by clear_history(), get_history(), snapshot(), and to_tsv().

◆ running_

std::atomic<bool> kcenon::network::utils::memory_profiler::running_ {false}
private

Definition at line 66 of file memory_profiler.h.

66{false};

Referenced by schedule_next_sample(), start(), and stop().

◆ sampling_interval_

std::chrono::milliseconds kcenon::network::utils::memory_profiler::sampling_interval_ {1000}
private

Definition at line 67 of file memory_profiler.h.

67{1000};

Referenced by schedule_next_sample(), and start().


The documentation for this class was generated from the following files: