Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
memory_profiler.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
14#include <cstddef>
15#include <cstdint>
16#include <atomic>
17#include <chrono>
18#include <functional>
19#include <future>
20#include <mutex>
21#include <vector>
22#include <string>
23
24namespace kcenon::network::utils {
25
27 std::chrono::system_clock::time_point timestamp{};
28 std::uint64_t resident_bytes{0}; // RSS
29 std::uint64_t virtual_bytes{0}; // VSZ
30};
31
33public:
34 // Singleton accessor
35 static memory_profiler& instance();
36
37 // Start periodic sampling (no-op if already running)
38 void start(std::chrono::milliseconds interval = std::chrono::milliseconds{1000});
39
40 // Stop periodic sampling
41 void stop();
42
43 // Take one snapshot immediately
45
46 // Get recent snapshots (copy)
47 std::vector<memory_snapshot> get_history(std::size_t max_count = 256) const;
48
49 // Clear stored snapshots
50 void clear_history();
51
52 // Export as TSV string (timestamp, rss, vms)
53 std::string to_tsv() const;
54
55private:
56 memory_profiler() = default;
58
61
63 static bool query_process_memory(std::uint64_t& rss, std::uint64_t& vms);
64
65private:
66 std::atomic<bool> running_{false};
67 std::chrono::milliseconds sampling_interval_{1000};
68 mutable std::mutex mutex_{};
69 std::vector<memory_snapshot> history_{};
70 std::size_t max_history_{4096};
71};
72
73} // namespace kcenon::network::utils
74
static bool query_process_memory(std::uint64_t &rss, std::uint64_t &vms)
std::vector< memory_snapshot > get_history(std::size_t max_count=256) const
std::chrono::milliseconds sampling_interval_
std::vector< memory_snapshot > history_
memory_profiler(const memory_profiler &)=delete
memory_profiler & operator=(const memory_profiler &)=delete
void start(std::chrono::milliseconds interval=std::chrono::milliseconds{1000})
Utility components for network_system.
std::chrono::system_clock::time_point timestamp