diff --git a/be/src/runtime/memory/mem_tracker_limiter.cpp b/be/src/runtime/memory/mem_tracker_limiter.cpp index 25d5d307b5bb999..ac4684835a670c0 100644 --- a/be/src/runtime/memory/mem_tracker_limiter.cpp +++ b/be/src/runtime/memory/mem_tracker_limiter.cpp @@ -212,24 +212,20 @@ std::string MemTrackerLimiter::print_address_sanitizers() { RuntimeProfile* MemTrackerLimiter::make_profile(RuntimeProfile* profile) const { RuntimeProfile* profile_snapshot = profile->create_child( fmt::format("{}@{}@id={}", _label, type_string(_type), _uid.to_string()), true, false); - RuntimeProfile::Counter* current_usage_counter = - ADD_COUNTER(profile_snapshot, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* peak_usage_counter = - ADD_COUNTER(profile_snapshot, "PeakUsage", TUnit::BYTES); - COUNTER_SET(current_usage_counter, consumption()); - COUNTER_SET(peak_usage_counter, peak_consumption()); + RuntimeProfile::HighWaterMarkCounter* usage_counter = + profile_snapshot->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + COUNTER_SET(usage_counter, peak_consumption()); + COUNTER_SET(usage_counter, consumption()); if (has_limit()) { RuntimeProfile::Counter* limit_counter = ADD_COUNTER(profile_snapshot, "Limit", TUnit::BYTES); COUNTER_SET(limit_counter, _limit); } if (reserved_peak_consumption() != 0) { - RuntimeProfile::Counter* reserved_counter = - ADD_COUNTER(profile_snapshot, "ReservedMemory", TUnit::BYTES); - RuntimeProfile::Counter* reserved_peak_counter = - ADD_COUNTER(profile_snapshot, "ReservedPeakMemory", TUnit::BYTES); + RuntimeProfile::HighWaterMarkCounter* reserved_counter = + profile_snapshot->AddHighWaterMarkCounter("ReservedMemory", TUnit::BYTES); + COUNTER_SET(reserved_counter, reserved_peak_consumption()); COUNTER_SET(reserved_counter, reserved_consumption()); - COUNTER_SET(reserved_peak_counter, reserved_peak_consumption()); } return profile_snapshot; } diff --git a/be/src/runtime/memory/memory_profile.cpp b/be/src/runtime/memory/memory_profile.cpp index 651a6a6cefaec15..6eed4c10c088e3b 100644 --- a/be/src/runtime/memory/memory_profile.cpp +++ b/be/src/runtime/memory/memory_profile.cpp @@ -46,36 +46,24 @@ static bvar::Adder memory_all_tasks_memory_bytes("memory_all_tasks_memo static bvar::Adder memory_untracked_memory_bytes("memory_untracked_memory_bytes"); MemoryProfile::MemoryProfile() { - _memory_overview_profile.set(std::make_unique("MemoryOverviewSnapshot")); - _global_memory_profile.set(std::make_unique("GlobalMemorySnapshot")); - _metadata_memory_profile.set(std::make_unique("MetadataMemorySnapshot")); - _cache_memory_profile.set(std::make_unique("CacheMemorySnapshot")); - _top_memory_tasks_profile.set(std::make_unique("TopMemoryTasksSnapshot")); - _tasks_memory_profile.set(std::make_unique("TasksMemorySnapshot")); -} - -void MemoryProfile::refresh_memory_overview_profile() { #ifdef ADDRESS_SANITIZER - std::unique_ptr memory_overview_profile = - std::make_unique("[ASAN]MemoryOverviewSnapshot"); + _memory_overview_profile = std::make_unique("[ASAN]MemoryOverviewSnapshot"); #else - std::unique_ptr memory_overview_profile = - std::make_unique("MemoryOverviewSnapshot"); + _memory_overview_profile = std::make_unique("MemoryOverviewSnapshot"); #endif - std::unique_ptr global_memory_profile = - std::make_unique("GlobalMemorySnapshot"); - std::unique_ptr metadata_memory_profile = - std::make_unique("MetadataMemorySnapshot"); - std::unique_ptr cache_memory_profile = - std::make_unique("CacheMemorySnapshot"); - std::unique_ptr top_memory_tasks_profile = - std::make_unique("TopMemoryTasksSnapshot"); - - // 1. create profile + _global_memory_profile = std::make_unique("GlobalMemorySnapshot"); + _metadata_memory_profile = std::make_unique("MetadataMemorySnapshot"); + _cache_memory_profile = std::make_unique("CacheMemorySnapshot"); + _top_memory_tasks_profile = std::make_unique("TopMemoryTasksSnapshot"); + _tasks_memory_profile.set(std::make_unique("TasksMemorySnapshot")); + init_counter(); +} + +void MemoryProfile::init_counter() { RuntimeProfile* untracked_memory_profile = - memory_overview_profile->create_child("UntrackedMemory", true, false); + _memory_overview_profile->create_child("UntrackedMemory", true, false); RuntimeProfile* tracked_memory_profile = - memory_overview_profile->create_child("TrackedMemory", true, false); + _memory_overview_profile->create_child("TrackedMemory", true, false); RuntimeProfile* tasks_memory_overview_profile = tracked_memory_profile->create_child("TasksMemory", true, false); RuntimeProfile* tasks_memory_overview_details_profile = @@ -91,109 +79,63 @@ void MemoryProfile::refresh_memory_overview_profile() { RuntimeProfile* jemalloc_memory_details_profile = jemalloc_memory_profile->create_child("Details", true, false); - // 2. add counter - // 2.1 add process memory counter - RuntimeProfile::Counter* process_physical_memory_current_usage_counter = - ADD_COUNTER(memory_overview_profile, "PhysicalMemory(VmRSS)", TUnit::BYTES); - RuntimeProfile::Counter* process_physical_memory_peak_usage_counter = - memory_overview_profile->AddHighWaterMarkCounter("PhysicalMemoryPeak", TUnit::BYTES); - RuntimeProfile::Counter* process_virtual_memory_current_usage_counter = - ADD_COUNTER(memory_overview_profile, "VirtualMemory(VmSize)", TUnit::BYTES); - RuntimeProfile::Counter* process_virtual_memory_peak_usage_counter = - memory_overview_profile->AddHighWaterMarkCounter("VirtualMemoryPeak", TUnit::BYTES); - - // 2.2 add untracked memory counter - RuntimeProfile::Counter* untracked_memory_current_usage_counter = - ADD_COUNTER(untracked_memory_profile, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* untracked_memory_peak_usage_counter = - untracked_memory_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - - // 2.3 add tracked memory counter - RuntimeProfile::Counter* tracked_memory_current_usage_counter = - ADD_COUNTER(tracked_memory_profile, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* tracked_memory_peak_usage_counter = - tracked_memory_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - - // 2.4 add jemalloc memory counter - RuntimeProfile::Counter* jemalloc_memory_current_usage_counter = - ADD_COUNTER(jemalloc_memory_profile, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* jemalloc_memory_peak_usage_counter = - jemalloc_memory_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - RuntimeProfile::Counter* jemalloc_cache_current_usage_counter = - ADD_COUNTER(jemalloc_memory_details_profile, "Cache", TUnit::BYTES); - RuntimeProfile::Counter* jemalloc_cache_peak_usage_counter = - jemalloc_memory_details_profile->AddHighWaterMarkCounter("CachePeak", TUnit::BYTES); - RuntimeProfile::Counter* jemalloc_metadata_current_usage_counter = - ADD_COUNTER(jemalloc_memory_details_profile, "Metadata", TUnit::BYTES); - RuntimeProfile::Counter* jemalloc_metadata_peak_usage_counter = - jemalloc_memory_details_profile->AddHighWaterMarkCounter("MetadataPeak", TUnit::BYTES); - - // 2.5 add global/metadata/cache memory counter - RuntimeProfile::Counter* global_current_usage_counter = - ADD_COUNTER(global_memory_overview_profile, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* global_peak_usage_counter = - global_memory_overview_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - RuntimeProfile::Counter* metadata_current_usage_counter = - ADD_COUNTER(metadata_memory_overview_profile, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* metadata_peak_usage_counter = - metadata_memory_overview_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - RuntimeProfile::Counter* cache_current_usage_counter = - ADD_COUNTER(cache_memory_overview_profile, "CurrentUsage", TUnit::BYTES); - RuntimeProfile::Counter* cache_peak_usage_counter = - cache_memory_overview_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - - // 2.6 add tasks memory counter - RuntimeProfile::Counter* tasks_memory_current_usage_counter = - ADD_COUNTER_WITH_LEVEL(tasks_memory_overview_profile, "CurrentUsage", TUnit::BYTES, 1); + // 1 add process memory counter + _process_physical_memory_usage_counter = _memory_overview_profile->AddHighWaterMarkCounter( + "PhysicalMemory(VmRSS)", TUnit::BYTES); + _process_virtual_memory_usage_counter = _memory_overview_profile->AddHighWaterMarkCounter( + "VirtualMemory(VmSize)", TUnit::BYTES); + + // 2 add untracked/tracked memory counter + _untracked_memory_usage_counter = + untracked_memory_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + _tracked_memory_usage_counter = + tracked_memory_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + + // 3 add Jemalloc memory counter + _jemalloc_memory_usage_counter = + jemalloc_memory_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + _jemalloc_cache_usage_counter = + jemalloc_memory_details_profile->AddHighWaterMarkCounter("Cache", TUnit::BYTES); + _jemalloc_metadata_usage_counter = + jemalloc_memory_details_profile->AddHighWaterMarkCounter("Metadata", TUnit::BYTES); + + // 4 add global/metadata/cache memory counter + _global_usage_counter = + global_memory_overview_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + _metadata_usage_counter = + metadata_memory_overview_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + _cache_usage_counter = + cache_memory_overview_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); + + // 5 add tasks memory counter + _tasks_memory_usage_counter = + tasks_memory_overview_profile->AddHighWaterMarkCounter("Memory", TUnit::BYTES); // Reserved memory is the sum of all task reserved memory, is duplicated with all task memory counter. - RuntimeProfile::Counter* reserved_memory_current_usage_counter = ADD_CHILD_COUNTER_WITH_LEVEL( - tasks_memory_overview_profile, "ReservedMemory", TUnit::BYTES, "CurrentUsage", 1); - RuntimeProfile::Counter* reserved_memory_peak_usage_counter = - tasks_memory_overview_profile->AddHighWaterMarkCounter("ReservedMemoryPeak", - TUnit::BYTES, "CurrentUsage", 1); - RuntimeProfile::Counter* tasks_memory_peak_usage_counter = - tasks_memory_overview_profile->AddHighWaterMarkCounter("PeakUsage", TUnit::BYTES); - RuntimeProfile::Counter* query_current_usage_counter = - ADD_COUNTER_WITH_LEVEL(tasks_memory_overview_details_profile, "Query", TUnit::BYTES, 1); - RuntimeProfile::Counter* query_peak_usage_counter = - tasks_memory_overview_details_profile->AddHighWaterMarkCounter( - "QueryPeak", TUnit::BYTES, "Query", 1); - RuntimeProfile::Counter* load_current_usage_counter = - ADD_COUNTER_WITH_LEVEL(tasks_memory_overview_details_profile, "Load", TUnit::BYTES, 1); - RuntimeProfile::Counter* load_peak_usage_counter = - tasks_memory_overview_details_profile->AddHighWaterMarkCounter("LoadPeak", TUnit::BYTES, - "Load", 1); - RuntimeProfile::Counter* load_all_memtables_current_usage_counter = - ADD_CHILD_COUNTER_WITH_LEVEL(tasks_memory_overview_details_profile, - "AllMemTablesMemory", TUnit::BYTES, "Load", 1); - RuntimeProfile::Counter* load_all_memtables_peak_usage_counter = - ADD_CHILD_COUNTER_WITH_LEVEL(tasks_memory_overview_details_profile, - "AllMemTablesMemoryPeak", TUnit::BYTES, "Load", 1); - RuntimeProfile::Counter* compaction_current_usage_counter = ADD_COUNTER_WITH_LEVEL( - tasks_memory_overview_details_profile, "Compaction", TUnit::BYTES, 1); - RuntimeProfile::Counter* compaction_peak_usage_counter = - tasks_memory_overview_details_profile->AddHighWaterMarkCounter( - "CompactionPeak", TUnit::BYTES, "Compaction", 1); - RuntimeProfile::Counter* schema_change_current_usage_counter = ADD_COUNTER_WITH_LEVEL( - tasks_memory_overview_details_profile, "SchemaChange", TUnit::BYTES, 1); - RuntimeProfile::Counter* schema_change_peak_usage_counter = - tasks_memory_overview_details_profile->AddHighWaterMarkCounter( - "SchemaChangePeak", TUnit::BYTES, "SchemaChange", 1); - RuntimeProfile::Counter* other_current_usage_counter = - ADD_COUNTER_WITH_LEVEL(tasks_memory_overview_details_profile, "Other", TUnit::BYTES, 1); - RuntimeProfile::Counter* other_peak_usage_counter = - tasks_memory_overview_details_profile->AddHighWaterMarkCounter( - "OtherPeak", TUnit::BYTES, "Other", 1); - // 3. refresh counter - // 3.1 refresh process memory counter - COUNTER_SET(process_physical_memory_current_usage_counter, + _reserved_memory_usage_counter = tasks_memory_overview_profile->AddHighWaterMarkCounter( + "ReservedMemory", TUnit::BYTES, "Memory", 1); + _query_usage_counter = + tasks_memory_overview_details_profile->AddHighWaterMarkCounter("Query", TUnit::BYTES); + _load_usage_counter = + tasks_memory_overview_details_profile->AddHighWaterMarkCounter("Load", TUnit::BYTES); + _load_all_memtables_usage_counter = + tasks_memory_overview_details_profile->AddHighWaterMarkCounter("AllMemTablesMemory", + TUnit::BYTES, "Load", 1); + _compaction_usage_counter = tasks_memory_overview_details_profile->AddHighWaterMarkCounter( + "Compaction", TUnit::BYTES); + _schema_change_usage_counter = tasks_memory_overview_details_profile->AddHighWaterMarkCounter( + "SchemaChange", TUnit::BYTES); + _other_usage_counter = + tasks_memory_overview_details_profile->AddHighWaterMarkCounter("Other", TUnit::BYTES); +} + +void MemoryProfile::refresh_memory_overview_profile() { + // 1 refresh process memory counter + COUNTER_SET(_process_physical_memory_usage_counter, PerfCounters::get_vm_rss()); // from /proc VmRSS VmHWM - COUNTER_SET(process_physical_memory_peak_usage_counter, PerfCounters::get_vm_hwm()); - COUNTER_SET(process_virtual_memory_current_usage_counter, + COUNTER_SET(_process_virtual_memory_usage_counter, PerfCounters::get_vm_size()); // from /proc VmSize VmPeak - COUNTER_SET(process_virtual_memory_peak_usage_counter, PerfCounters::get_vm_peak()); - // 3.2 refresh metadata memory tracker + // 2 refresh metadata memory tracker ExecEnv::GetInstance()->tablets_no_cache_mem_tracker()->set_consumption( MetadataAdder::get_all_tablets_size() - TabletSchemaCache::instance()->value_mem_consumption() - @@ -204,7 +146,7 @@ void MemoryProfile::refresh_memory_overview_profile() { MetadataAdder::get_all_segments_estimate_size() - SegmentLoader::instance()->cache_mem_usage()); - // 3.3 refresh tracked memory counter + // 3 refresh tracked memory counter std::unordered_map type_mem_sum = { {MemTrackerLimiter::Type::GLOBAL, 0}, {MemTrackerLimiter::Type::QUERY, 0}, {MemTrackerLimiter::Type::LOAD, 0}, {MemTrackerLimiter::Type::COMPACTION, 0}, @@ -227,123 +169,95 @@ void MemoryProfile::refresh_memory_overview_profile() { all_tracked_mem_sum += it.second; switch (it.first) { case MemTrackerLimiter::Type::GLOBAL: - COUNTER_SET(global_current_usage_counter, it.second); - COUNTER_SET(global_peak_usage_counter, it.second); + COUNTER_SET(_global_usage_counter, it.second); memory_global_trackers_sum_bytes << it.second - memory_global_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::QUERY: - COUNTER_SET(query_current_usage_counter, it.second); - COUNTER_SET(query_peak_usage_counter, it.second); + COUNTER_SET(_query_usage_counter, it.second); tasks_trackers_mem_sum += it.second; memory_query_trackers_sum_bytes << it.second - memory_query_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::LOAD: - COUNTER_SET(load_current_usage_counter, it.second); - COUNTER_SET(load_peak_usage_counter, it.second); + COUNTER_SET(_load_usage_counter, it.second); tasks_trackers_mem_sum += it.second; memory_load_trackers_sum_bytes << it.second - memory_load_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::COMPACTION: - COUNTER_SET(compaction_current_usage_counter, it.second); - COUNTER_SET(compaction_peak_usage_counter, it.second); + COUNTER_SET(_compaction_usage_counter, it.second); tasks_trackers_mem_sum += it.second; memory_compaction_trackers_sum_bytes << it.second - memory_compaction_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::SCHEMA_CHANGE: - COUNTER_SET(schema_change_current_usage_counter, it.second); - COUNTER_SET(schema_change_peak_usage_counter, it.second); + COUNTER_SET(_schema_change_usage_counter, it.second); tasks_trackers_mem_sum += it.second; memory_schema_change_trackers_sum_bytes << it.second - memory_schema_change_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::METADATA: - COUNTER_SET(metadata_current_usage_counter, it.second); - COUNTER_SET(metadata_peak_usage_counter, it.second); + COUNTER_SET(_metadata_usage_counter, it.second); memory_metadata_trackers_sum_bytes << it.second - memory_metadata_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::CACHE: - COUNTER_SET(cache_current_usage_counter, it.second); - COUNTER_SET(cache_peak_usage_counter, it.second); + COUNTER_SET(_cache_usage_counter, it.second); memory_cache_trackers_sum_bytes << it.second - memory_cache_trackers_sum_bytes.get_value(); break; case MemTrackerLimiter::Type::OTHER: - COUNTER_SET(other_current_usage_counter, it.second); - COUNTER_SET(other_peak_usage_counter, it.second); + COUNTER_SET(_other_usage_counter, it.second); tasks_trackers_mem_sum += it.second; memory_other_trackers_sum_bytes << it.second - memory_other_trackers_sum_bytes.get_value(); } } - MemTrackerLimiter::make_type_trackers_profile(global_memory_profile.get(), + MemTrackerLimiter::make_type_trackers_profile(_global_memory_profile.get(), MemTrackerLimiter::Type::GLOBAL); - MemTrackerLimiter::make_type_trackers_profile(metadata_memory_profile.get(), + MemTrackerLimiter::make_type_trackers_profile(_metadata_memory_profile.get(), MemTrackerLimiter::Type::METADATA); - MemTrackerLimiter::make_type_trackers_profile(cache_memory_profile.get(), + MemTrackerLimiter::make_type_trackers_profile(_cache_memory_profile.get(), MemTrackerLimiter::Type::CACHE); - MemTrackerLimiter::make_top_consumption_tasks_tracker_profile(top_memory_tasks_profile.get(), + MemTrackerLimiter::make_top_consumption_tasks_tracker_profile(_top_memory_tasks_profile.get(), 15); - COUNTER_SET(tasks_memory_current_usage_counter, tasks_trackers_mem_sum); - COUNTER_SET(tasks_memory_peak_usage_counter, tasks_trackers_mem_sum); + COUNTER_SET(_tasks_memory_usage_counter, tasks_trackers_mem_sum); memory_all_tasks_memory_bytes << tasks_trackers_mem_sum - memory_all_tasks_memory_bytes.get_value(); - COUNTER_SET(reserved_memory_current_usage_counter, - GlobalMemoryArbitrator::process_reserved_memory()); - COUNTER_SET(reserved_memory_peak_usage_counter, - GlobalMemoryArbitrator::process_reserved_memory()); + COUNTER_SET(_reserved_memory_usage_counter, GlobalMemoryArbitrator::process_reserved_memory()); memory_reserved_memory_bytes << GlobalMemoryArbitrator::process_reserved_memory() - memory_reserved_memory_bytes.get_value(); all_tracked_mem_sum += MemInfo::allocator_cache_mem(); - COUNTER_SET(jemalloc_cache_current_usage_counter, - static_cast(MemInfo::allocator_cache_mem())); - COUNTER_SET(jemalloc_cache_peak_usage_counter, + COUNTER_SET(_jemalloc_cache_usage_counter, static_cast(MemInfo::allocator_cache_mem())); all_tracked_mem_sum += MemInfo::allocator_metadata_mem(); - COUNTER_SET(jemalloc_metadata_current_usage_counter, + COUNTER_SET(_jemalloc_metadata_usage_counter, static_cast(MemInfo::allocator_metadata_mem())); - COUNTER_SET(jemalloc_metadata_peak_usage_counter, - static_cast(MemInfo::allocator_metadata_mem())); - COUNTER_SET(jemalloc_memory_current_usage_counter, - jemalloc_cache_current_usage_counter->value() + - jemalloc_metadata_current_usage_counter->value()); - COUNTER_SET(jemalloc_memory_peak_usage_counter, - jemalloc_cache_current_usage_counter->value() + - jemalloc_metadata_current_usage_counter->value()); - - COUNTER_SET(tracked_memory_current_usage_counter, all_tracked_mem_sum); - COUNTER_SET(tracked_memory_peak_usage_counter, all_tracked_mem_sum); + COUNTER_SET(_jemalloc_memory_usage_counter, + _jemalloc_cache_usage_counter->current_value() + + _jemalloc_metadata_usage_counter->current_value()); + + COUNTER_SET(_tracked_memory_usage_counter, all_tracked_mem_sum); memory_all_tracked_sum_bytes << all_tracked_mem_sum - memory_all_tracked_sum_bytes.get_value(); // 3.4 refresh untracked memory counter int64_t untracked_memory = - process_physical_memory_current_usage_counter->value() - all_tracked_mem_sum; - COUNTER_SET(untracked_memory_current_usage_counter, untracked_memory); - COUNTER_SET(untracked_memory_peak_usage_counter, untracked_memory); + _process_physical_memory_usage_counter->current_value() - all_tracked_mem_sum; + COUNTER_SET(_untracked_memory_usage_counter, untracked_memory); memory_untracked_memory_bytes << untracked_memory - memory_untracked_memory_bytes.get_value(); // 3.5 refresh additional tracker printed when memory exceeds limit. - COUNTER_SET(load_all_memtables_current_usage_counter, - ExecEnv::GetInstance()->memtable_memory_limiter()->mem_tracker()->consumption()); COUNTER_SET( - load_all_memtables_peak_usage_counter, + _load_all_memtables_usage_counter, ExecEnv::GetInstance()->memtable_memory_limiter()->mem_tracker()->peak_consumption()); - - // 4. reset profile - _memory_overview_profile.set(std::move(memory_overview_profile)); - _global_memory_profile.set(std::move(global_memory_profile)); - _metadata_memory_profile.set(std::move(metadata_memory_profile)); - _cache_memory_profile.set(std::move(cache_memory_profile)); - _top_memory_tasks_profile.set(std::move(top_memory_tasks_profile)); + COUNTER_SET(_load_all_memtables_usage_counter, + ExecEnv::GetInstance()->memtable_memory_limiter()->mem_tracker()->consumption()); } void MemoryProfile::refresh_tasks_memory_profile() { @@ -356,31 +270,25 @@ void MemoryProfile::refresh_tasks_memory_profile() { void MemoryProfile::make_memory_profile(RuntimeProfile* profile) const { RuntimeProfile* memory_profile_snapshot = profile->create_child("MemoryProfile", true, false); - auto memory_overview_version_ptr = _memory_overview_profile.get(); RuntimeProfile* memory_overview_profile = - memory_profile_snapshot->create_child(memory_overview_version_ptr->name(), true, false); - memory_overview_profile->merge(const_cast(memory_overview_version_ptr.get())); + memory_profile_snapshot->create_child(_memory_overview_profile->name(), true, false); + memory_overview_profile->merge(const_cast(_memory_overview_profile.get())); - auto global_memory_version_ptr = _global_memory_profile.get(); RuntimeProfile* global_memory_profile = - memory_profile_snapshot->create_child(global_memory_version_ptr->name(), true, false); - global_memory_profile->merge(const_cast(global_memory_version_ptr.get())); + memory_profile_snapshot->create_child(_global_memory_profile->name(), true, false); + global_memory_profile->merge(const_cast(_global_memory_profile.get())); - auto metadata_memory_version_ptr = _metadata_memory_profile.get(); RuntimeProfile* metadata_memory_profile = - memory_profile_snapshot->create_child(metadata_memory_version_ptr->name(), true, false); - metadata_memory_profile->merge(const_cast(metadata_memory_version_ptr.get())); + memory_profile_snapshot->create_child(_metadata_memory_profile->name(), true, false); + metadata_memory_profile->merge(const_cast(_metadata_memory_profile.get())); - auto cache_memory_version_ptr = _cache_memory_profile.get(); RuntimeProfile* cache_memory_profile = - memory_profile_snapshot->create_child(cache_memory_version_ptr->name(), true, false); - cache_memory_profile->merge(const_cast(cache_memory_version_ptr.get())); - - auto top_memory_tasks_version_ptr = _top_memory_tasks_profile.get(); - RuntimeProfile* top_memory_tasks_profile = memory_profile_snapshot->create_child( - top_memory_tasks_version_ptr->name(), true, false); - top_memory_tasks_profile->merge( - const_cast(top_memory_tasks_version_ptr.get())); + memory_profile_snapshot->create_child(_cache_memory_profile->name(), true, false); + cache_memory_profile->merge(const_cast(_cache_memory_profile.get())); + + RuntimeProfile* top_memory_tasks_profile = + memory_profile_snapshot->create_child(_top_memory_tasks_profile->name(), true, false); + top_memory_tasks_profile->merge(const_cast(_top_memory_tasks_profile.get())); auto tasks_memory_version_ptr = _tasks_memory_profile.get(); RuntimeProfile* tasks_memory_profile = diff --git a/be/src/runtime/memory/memory_profile.h b/be/src/runtime/memory/memory_profile.h index 6d301a0be8b05c1..02f5b20ddadad74 100644 --- a/be/src/runtime/memory/memory_profile.h +++ b/be/src/runtime/memory/memory_profile.h @@ -53,7 +53,7 @@ class MemoryProfile { } std::string print_tasks_memory_profile() const { - return return_memory_profile_str(_tasks_memory_profile.get()); + return return_memory_profile_str(_tasks_memory_profile.get().get()); } static int64_t query_current_usage(); @@ -67,20 +67,50 @@ class MemoryProfile { void print_log_process_usage(); private: - std::string return_memory_profile_str( - const std::shared_ptr& profile) const { + std::string return_memory_profile_str(const RuntimeProfile* profile) const { std::stringstream ss; profile->pretty_print(&ss); return ss.str(); } - MultiVersion _memory_overview_profile; - MultiVersion _global_memory_profile; - MultiVersion _metadata_memory_profile; - MultiVersion _cache_memory_profile; - MultiVersion _top_memory_tasks_profile; + void init_counter(); + + std::unique_ptr _memory_overview_profile; + std::unique_ptr _global_memory_profile; + std::unique_ptr _metadata_memory_profile; + std::unique_ptr _cache_memory_profile; + std::unique_ptr _top_memory_tasks_profile; MultiVersion _tasks_memory_profile; + // process memory counter + RuntimeProfile::HighWaterMarkCounter* _process_physical_memory_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _process_virtual_memory_usage_counter; + + // untracked/tracked memory counter + RuntimeProfile::HighWaterMarkCounter* _untracked_memory_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _tracked_memory_usage_counter; + + // Jemalloc memory counter + RuntimeProfile::HighWaterMarkCounter* _jemalloc_memory_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _jemalloc_cache_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _jemalloc_metadata_usage_counter; + + // global/metadata/cache memory counter + RuntimeProfile::HighWaterMarkCounter* _global_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _metadata_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _cache_usage_counter; + + // tasks memory counter + RuntimeProfile::HighWaterMarkCounter* _tasks_memory_usage_counter; + // reserved memory is the sum of all task reserved memory, is duplicated with all task memory counter. + RuntimeProfile::HighWaterMarkCounter* _reserved_memory_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _query_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _load_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _load_all_memtables_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _compaction_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _schema_change_usage_counter; + RuntimeProfile::HighWaterMarkCounter* _other_usage_counter; + std::atomic _enable_print_log_process_usage {true}; }; diff --git a/be/src/util/runtime_profile.cpp b/be/src/util/runtime_profile.cpp index 45db607a3427437..8b9e97bfc9cb549 100644 --- a/be/src/util/runtime_profile.cpp +++ b/be/src/util/runtime_profile.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "common/object_pool.h" #include "util/container_util.hpp" @@ -72,8 +73,7 @@ void RuntimeProfile::merge(RuntimeProfile* other) { dst_iter = _counter_map.find(src_iter->first); if (dst_iter == _counter_map.end()) { - _counter_map[src_iter->first] = _pool->add( - new Counter(src_iter->second->type(), src_iter->second->value())); + _counter_map[src_iter->first] = _pool->add(src_iter->second->clone()); } else { DCHECK(dst_iter->second->type() == src_iter->second->type()); diff --git a/be/src/util/runtime_profile.h b/be/src/util/runtime_profile.h index 6e393ac673a6284..7130acbd2f9427d 100644 --- a/be/src/util/runtime_profile.h +++ b/be/src/util/runtime_profile.h @@ -100,6 +100,8 @@ class RuntimeProfile { : _value(value), _type(type), _level(level) {} virtual ~Counter() = default; + virtual Counter* clone() const { return new Counter(type(), value(), _level); } + virtual void update(int64_t delta) { _value.fetch_add(delta, std::memory_order_relaxed); } void bit_or(int64_t delta) { _value.fetch_or(delta, std::memory_order_relaxed); } @@ -137,7 +139,7 @@ class RuntimeProfile { TUnit::type type() const { return _type; } - virtual int64_t level() { return _level; } + virtual int64_t level() const { return _level; } private: friend class RuntimeProfile; @@ -151,8 +153,16 @@ class RuntimeProfile { /// as value()) and the current value. class HighWaterMarkCounter : public Counter { public: - HighWaterMarkCounter(TUnit::type unit, int64_t level, const std::string& parent_name) - : Counter(unit, 0, level), current_value_(0), _parent_name(parent_name) {} + HighWaterMarkCounter(TUnit::type unit, int64_t level, const std::string& parent_name, + int64_t value = 0, int64_t current_value = 0) + : Counter(unit, value, level), + current_value_(current_value), + _parent_name(parent_name) {} + + virtual Counter* clone() const override { + return new HighWaterMarkCounter(type(), level(), parent_name(), value(), + current_value()); + } void add(int64_t delta) { current_value_.fetch_add(delta, std::memory_order_relaxed); @@ -188,10 +198,9 @@ class RuntimeProfile { virtual void pretty_print(std::ostream* s, const std::string& prefix, const std::string& name) const override { std::ostream& stream = *s; - stream << prefix << " - " << name << ": " - << PrettyPrinter::print(current_value(), type()) << std::endl; - stream << prefix << " - " << name << "Peak: " - << PrettyPrinter::print(_value.load(std::memory_order_relaxed), type()) + stream << prefix << " - " << name + << " Current: " << PrettyPrinter::print(current_value(), type()) << " (Peak: " + << PrettyPrinter::print(_value.load(std::memory_order_relaxed), type()) << ")" << std::endl; } @@ -217,6 +226,8 @@ class RuntimeProfile { int64_t current_value() const { return current_value_.load(std::memory_order_relaxed); } + std::string parent_name() const { return _parent_name; } + private: /// Set '_value' to 'v' if 'v' is larger than '_value'. The entire operation is /// atomic. @@ -247,8 +258,13 @@ class RuntimeProfile { // Do not call Set() and Update(). class DerivedCounter : public Counter { public: - DerivedCounter(TUnit::type type, const DerivedCounterFunction& counter_fn) - : Counter(type, 0), _counter_fn(counter_fn) {} + DerivedCounter(TUnit::type type, const DerivedCounterFunction& counter_fn, + int64_t value = 0, int64_t level = 1) + : Counter(type, value, level), _counter_fn(counter_fn) {} + + virtual Counter* clone() const override { + return new DerivedCounter(type(), _counter_fn, value(), level()); + } int64_t value() const override { return _counter_fn(); } @@ -259,8 +275,13 @@ class RuntimeProfile { // NonZeroCounter will not be converted to Thrift if the value is 0. class NonZeroCounter : public Counter { public: - NonZeroCounter(TUnit::type type, int64_t level, const std::string& parent_name) - : Counter(type, 0, level), _parent_name(parent_name) {} + NonZeroCounter(TUnit::type type, int64_t level, const std::string& parent_name, + int64_t value = 0) + : Counter(type, value, level), _parent_name(parent_name) {} + + virtual Counter* clone() const override { + return new NonZeroCounter(type(), level(), parent_name(), value()); + } void to_thrift(const std::string& name, std::vector& tcounters, std::map>& child_counters_map) override { @@ -272,6 +293,8 @@ class RuntimeProfile { } } + std::string parent_name() const { return _parent_name; } + private: const std::string _parent_name; };