This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new accf3d6bd49 [fix](be) Stop GET /profile from aborting debug BEs and 
read RSS on macOS (#70847)
accf3d6bd49 is described below

commit accf3d6bd49a0b5bb341872a4bdda267ce23e1a2
Author: Mingyu Chen (Rayner) <[email protected]>
AuthorDate: Tue Nov 24 10:09:17 2026 +0800

    [fix](be) Stop GET /profile from aborting debug BEs and read RSS on macOS 
(#70847)
    
    ### Background
    
    The BE web page `/profile` builds a process memory snapshot in
    `ProcessProfile::refresh_profile()`: the resident size from
    `PerfCounters` (`PhysicalMemory(VmRSS)`), the sum of all memory trackers
    (`TrackedMemory`), and their difference (`UntrackedMemory`). The memory
    maintenance thread refreshes the same snapshot in the background. Since
    #62475, debug builds check that profile counters never go negative; the
    check has a thread-local switch, `enable_profile_counter_check`, which
    the memory maintenance thread turns off for itself. `PerfCounters`
    caches the resident size for the rest of the BE; the memory maintenance
    thread refreshes it from `/proc/self/status` on Linux, and on macOS the
    refresh is a stub.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: #62475 (added the counter check that aborts), #13195 (added
    the macOS `PerfCounters` stub), #12782 (added the `PerfCounters` cache
    made atomic here), #67675 (added the Variant test fixed here), #70798
    (applied locally so that `doris_be_test` builds on macOS)
    PR Type: Bugfix
    
    Problem Summary:
    
    A debug (ASAN or DEBUG) BE aborts on `GET /profile` whenever a snapshot
    counter is negative, because the request runs on an HTTP worker where
    the check is still on. On macOS that happens as soon as any memory is
    tracked: the macOS stub reports RSS as 0, so `UntrackedMemory` is minus
    the tracked bytes. This PR builds the `/profile` snapshot with the check
    off, as the memory maintenance thread already does, and makes macOS read
    its resident size, peak resident size and virtual size from Mach
    `task_info`. The cache that holds them becomes atomic, which also ends a
    data race on Linux. It also fixes a Variant unit test that fails on
    aarch64 because it expects `compare_at` to return exactly -1, 0 or 1.
    
    A standalone macOS ASAN BE, after one request to its brpc port (which
    makes it track 16 KiB of IOBuf blocks):
    
    | | Before | With the fix |
    |---|---|---|
    | `GET /profile` | empty reply, the BE aborts: `Check failed:
    current_value_.load(std::memory_order_seq_cst) > -1L (-16384 vs. -1)` |
    HTTP 200, the BE keeps running |
    | `PhysicalMemory(VmRSS)` (`ps` RSS about 1.0 GiB) | `Current: 0 (Peak:
    0)` | `Current: 1.02 GB` |
    
    ### Details
    
    - **Frequency:** Always on a macOS debug BE once it tracks any memory;
    one brpc request is enough. Elsewhere only while a snapshot counter is
    negative, such as when tracked bytes are not resident yet; not observed,
    found by code review. The cache race happens whenever a refresh overlaps
    a read; ThreadSanitizer reports it at once, and no wrong value has been
    observed.
    - **Root cause:** `ProcessProfile::refresh_profile()` sets the snapshot
    counters on an HTTP worker, where `enable_profile_counter_check` is
    true, so `HighWaterMarkCounter::set()` fails `DCHECK_GT(current_value,
    -1)` on a negative `UntrackedMemory`, or on a tracker sum that batched
    tracking leaves briefly negative; #62475 exempted only the memory
    maintenance thread. On macOS, `PerfCounters::refresh_proc_status()` is
    empty, so RSS stays 0. `PerfCounters` keeps its cache in plain static
    fields, plus a string that `process_memory_used_details_str()` copies
    while a refresh may be assigning it. The Variant test expects -1/0/1,
    but `memcmp_small_allow_overflow15` returns `memcmp`'s byte difference
    on aarch64 (-1/0/1 only with SSE2), and `IColumn::compare_at` promises
    only the sign.
    - **Why existing tests missed it:** Unit tests: the counter check is
    compiled out of BE UT builds (`#if !defined(BE_TEST)`), no test calls
    `ProcessProfile` or `refresh_proc_status()`, and CI runs BE UT only on
    Linux x86; the macOS workflow (`be-ut-mac.yml`) does not build
    `be/test`. No pipeline builds BE unit tests with ThreadSanitizer, and no
    test reads the cache during a refresh. Regression: no suite requests the
    BE's `/profile` (`test_compaction_profile_action` calls
    `/api/compaction/profile`), and the pipelines run Linux release BEs,
    which have no counter check.
    - **Fix:** The `/profile` refresh turns the thread-local check off and
    restores it afterwards, since the HTTP worker goes on to serve other
    handlers; the check stays on everywhere else. On macOS,
    `PerfCounters::get_proc_status()` reads
    `task_info(MACH_TASK_BASIC_INFO)` (`resident_size`, `resident_size_max`
    and `virtual_size` for VmRSS, VmHWM and VmSize), and
    `refresh_proc_status()` stores it. The cached values become
    `std::atomic<int64_t>` with relaxed stores and loads, like the `MemInfo`
    values refreshed next to them, and `get_vm_rss_str()` formats the
    resident size from the atomic value on each call. The Variant test
    compares signs.
    - **Other paths and branches:** The memory maintenance thread is the
    only other caller of the snapshot refresh and already runs with the
    check off. The check exists only on master, so `branch-4.1` and
    `branch-4.2` don't abort, but their macOS stub also reports RSS as 0,
    and their Linux cache has the same data race (since #12782). The Variant
    test is also on `branch-4.2` (#68882) and fails there on aarch64 the
    same way; `branch-4.1` doesn't have it.
    - **Behavior change:** On macOS, everything that reads
    `PerfCounters::get_vm_rss()` now sees the real resident size instead of
    0: the process memory limits and memory GC in `GlobalMemoryArbitrator`,
    the process memory log lines, and the BE memory usage that
    `get_be_resource` reports to FE admission control. On Linux the cached
    values and the log text stay the same; only their publication is atomic.
    
    ### Testing
    
    All runs were local on macOS arm64 with ASAN builds, unless noted. The
    PR has two commits: `686ccd63f0d`, and `d17d586a505`, which makes the
    cache atomic and moves the macOS test to a fresh mapping. The Linux
    `perf_counters.cpp` was not compiled locally.
    
    - Standalone BE on master `598b967ff0f`: `/profile` showed
    `PhysicalMemory(VmRSS) Current: 0` while `ps` showed 1,110,992 KB, and
    after one brpc request it aborted as shown above.
    - Standalone BE with this PR, after one brpc request: three `/profile`
    requests returned 200 and the BE kept running. Each
    `PhysicalMemory(VmRSS)` was within 0.02 MB of the `ps` RSS read just
    before it (606.58 to 612.52 MB), `UntrackedMemory` was RSS minus the 32
    KiB tracked, and the process memory log line kept its format (`process
    memory used 734.92 MB(= [ASAN]734.92 MB[vm/rss] + ...`). The first
    commit gave the same results.
    - The guard alone (the first commit with `get_proc_status()` reporting
    `vm_rss = 0`): `/profile` returned 200 and showed `UntrackedMemory
    Current: -16384.00 B`, the value that aborted master.
    - `PerfCountersMacTest.ProcStatusReportsResidentMemory` (macOS only) in
    `doris_be_test`: passed 50 of 50 runs in one process, and fails both RSS
    assertions (`before.vm_rss > 0, actual: 0 vs 0`; `actual: 0 vs
    33554432`) when `get_proc_status()` reports `vm_rss = 0`. Built on its
    own with the system allocator, which macOS arm64 `BUILD_TYPE_UT=DEBUG`
    builds use, the first commit's version of the test fails the 2nd and 3rd
    of 3 runs in one process (`actual: 16384 vs 33554432`, then `actual: 0
    vs 33554432`); this version passes 50 of 50.
    - `ColumnVariantV2Test.*`: 67 of 67 pass. On master,
    `TypedComparisonBoundariesMatchEncodedWithoutMaterializing` failed 12
    assertions, typed rows returning 98, 25, -98, -73, -25 or 73 where
    encoded rows return 1 or -1. With this PR it passed 50 of 50 runs, and
    it still fails 12 assertions when the typed STRING order in
    `ColumnVariantV2::compare_at` is reversed.
    - `WorkloadGroupManagerTest.*`, `ThreadMemTrackerMgrTest.*` and
    `AsyncResultWriterTest.*`, whose process memory checks read the cached
    RSS: all pass except
    `process_mem_exceeded_resumes_when_reservation_fits_sys_mem_available`,
    which asserts a positive
    `MemInfo::sys_mem_available_warning_water_mark()`; macOS
    `MemInfo::init()` never sets it, so the case fails on macOS regardless
    of this change.
    - ThreadSanitizer, with one thread refreshing the cache 2000 times while
    another calls every getter, built from `perf_counters.h` and
    `perf_counters_mac.cpp` with `PrettyPrinter` replaced by
    `std::to_string`: the first commit gives data race reports between
    `refresh_proc_status()` and `get_vm_size()` in every run; `d17d586a505`
    gives none in 4 runs.
    
    ### Notes for Reviewers
    
    - The check is turned off for the whole snapshot instead of clamping
    `UntrackedMemory` at 0: tracker sums can be briefly negative too (#62475
    notes this in `memory_reclamation.cpp`), and a clamp would hide tracked
    bytes that exceed the resident size.
    - `resident_size` matches Linux `VmRSS`; `phys_footprint` would also
    count compressed and swapped memory. Mach has no peak virtual size, so
    `vm_peak` keeps its default; nothing reads it.
    - `get_vm_rss_str()` is defined next to each platform's refresh instead
    of inline in `perf_counters.h`, which keeps `pretty_printer.h` out of a
    header that most BE files include.
    - The macOS test calls `get_proc_status()`, not `refresh_proc_status()`,
    because a refresh would store the real RSS in process-wide counters that
    `workload_group_manager_test.cpp` relies on staying 0. It writes a fresh
    `mmap` region instead of a heap buffer, because an allocator can hand
    back a freed block whose pages are still resident. Like
    `perf_counters_mac.cpp`, it is built only on macOS, so no PR pipeline
    runs it.
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Behavior changed: Yes (see Details)
    - Does this need documentation: No
    - Pre-submission status: Ready
    
    ### AI Disclosure
    
    - Tool: Claude Code (Claude Opus 5.5)
    - Generated: the fix, the tests, and this description
    - Human review: not stated
    - Prompts: Asked to fix, in one PR, three problems found on macOS: the
    Variant test that fails on aarch64, a `printf` format that breaks the BE
    test build, and the `/profile` abort; then chose to leave the `printf`
    fix to its own open PR #70798. Later asked to judge the review comments
    and fix the ones that need it.
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 be/src/runtime/process_profile.cpp             | 11 ++++++
 be/src/util/perf_counters.cpp                  | 24 ++++++------
 be/src/util/perf_counters.h                    | 23 ++++++-----
 be/src/util/perf_counters_mac.cpp              | 43 ++++++++++++++++++---
 be/test/CMakeLists.txt                         |  2 +
 be/test/core/column/column_variant_v2_test.cpp | 12 ++++--
 be/test/util/perf_counters_mac_test.cpp        | 53 ++++++++++++++++++++++++++
 7 files changed, 137 insertions(+), 31 deletions(-)

diff --git a/be/src/runtime/process_profile.cpp 
b/be/src/runtime/process_profile.cpp
index 8852f25bbf9..2d6fb70d5b0 100644
--- a/be/src/runtime/process_profile.cpp
+++ b/be/src/runtime/process_profile.cpp
@@ -21,6 +21,7 @@
 
 #include "runtime/memory/memory_profile.h"
 #include "storage/metadata_adder.h"
+#include "util/defer_op.h"
 
 namespace doris {
 
@@ -29,6 +30,16 @@ ProcessProfile::ProcessProfile() {
 }
 
 void ProcessProfile::refresh_profile() {
+    // Untracked memory is the resident size minus every tracked byte, so it 
drops below zero
+    // while tracked memory is not resident, and a tracker can be briefly 
negative under batched
+    // tracking. The memory maintenance thread refreshes the same snapshot 
with the non-negative
+    // counter check off; turn it off here too, so such a value does not abort 
a debug build on
+    // GET /profile. The switch is thread local and this runs on a shared HTTP 
worker, so put it
+    // back afterwards.
+    const bool counter_check = enable_profile_counter_check;
+    enable_profile_counter_check = false;
+    Defer restore_counter_check {[&]() { enable_profile_counter_check = 
counter_check; }};
+
     // 1. refresh profile
     _memory_profile->refresh_memory_overview_profile();
     _memory_profile->refresh_tasks_memory_profile();
diff --git a/be/src/util/perf_counters.cpp b/be/src/util/perf_counters.cpp
index 76d79a256fd..0022875be75 100644
--- a/be/src/util/perf_counters.cpp
+++ b/be/src/util/perf_counters.cpp
@@ -44,11 +44,10 @@ namespace doris {
 
 static std::unordered_map<std::string, std::string> _process_state;
 
-int64_t PerfCounters::_vm_rss = 0;
-std::string PerfCounters::_vm_rss_str = "";
-int64_t PerfCounters::_vm_hwm = 0;
-int64_t PerfCounters::_vm_size = 0;
-int64_t PerfCounters::_vm_peak = 0;
+std::atomic<int64_t> PerfCounters::_vm_rss = 0;
+std::atomic<int64_t> PerfCounters::_vm_hwm = 0;
+std::atomic<int64_t> PerfCounters::_vm_size = 0;
+std::atomic<int64_t> PerfCounters::_vm_peak = 0;
 
 // This is the order of the counters in /proc/self/io
 enum PERF_IO_IDX {
@@ -580,15 +579,18 @@ void PerfCounters::refresh_proc_status() {
 
     if (statusinfo.is_open()) statusinfo.close();
 
-    _vm_size = parse_bytes("status/VmSize");
-    _vm_peak = parse_bytes("status/VmPeak");
-    _vm_rss = parse_bytes("status/VmRSS");
+    _vm_size.store(parse_bytes("status/VmSize"), std::memory_order_relaxed);
+    _vm_peak.store(parse_bytes("status/VmPeak"), std::memory_order_relaxed);
+    _vm_rss.store(parse_bytes("status/VmRSS"), std::memory_order_relaxed);
+    _vm_hwm.store(parse_bytes("status/VmHWM"), std::memory_order_relaxed);
+}
+
+std::string PerfCounters::get_vm_rss_str() {
 #ifdef ADDRESS_SANITIZER
-    _vm_rss_str = "[ASAN]" + PrettyPrinter::print(_vm_rss, TUnit::BYTES);
+    return "[ASAN]" + PrettyPrinter::print(get_vm_rss(), TUnit::BYTES);
 #else
-    _vm_rss_str = PrettyPrinter::print(_vm_rss, TUnit::BYTES);
+    return PrettyPrinter::print(get_vm_rss(), TUnit::BYTES);
 #endif
-    _vm_hwm = parse_bytes("status/VmHWM");
 }
 
 void PerfCounters::get_proc_status(ProcStatus* out) {
diff --git a/be/src/util/perf_counters.h b/be/src/util/perf_counters.h
index 01af39f65b8..4c8c6a1b997 100644
--- a/be/src/util/perf_counters.h
+++ b/be/src/util/perf_counters.h
@@ -23,6 +23,7 @@
 #include <gen_cpp/Metrics_types.h>
 #include <stdint.h>
 
+#include <atomic>
 #include <iostream>
 #include <string>
 #include <vector>
@@ -117,11 +118,13 @@ public:
     static void refresh_proc_status();
     static void get_proc_status(ProcStatus* out);
     // Return the process actual physical memory in bytes.
-    static inline int64_t get_vm_rss() { return _vm_rss; }
-    static inline std::string get_vm_rss_str() { return _vm_rss_str; }
-    static inline int64_t get_vm_hwm() { return _vm_hwm; }
-    static inline int64_t get_vm_size() { return _vm_size; }
-    static inline int64_t get_vm_peak() { return _vm_peak; }
+    static inline int64_t get_vm_rss() { return 
_vm_rss.load(std::memory_order_relaxed); }
+    // Formatted from get_vm_rss() on every call, so callers never copy a 
string that a refresh
+    // is assigning.
+    static std::string get_vm_rss_str();
+    static inline int64_t get_vm_hwm() { return 
_vm_hwm.load(std::memory_order_relaxed); }
+    static inline int64_t get_vm_size() { return 
_vm_size.load(std::memory_order_relaxed); }
+    static inline int64_t get_vm_peak() { return 
_vm_peak.load(std::memory_order_relaxed); }
 
 private:
     // Copy constructor and assignment not allowed
@@ -166,11 +169,11 @@ private:
     // at the same time.  This is useful to better correlate counter values.
     int64_t _group_fd;
 
-    static int64_t _vm_rss;
-    static std::string _vm_rss_str;
-    static int64_t _vm_hwm;
-    static int64_t _vm_size;
-    static int64_t _vm_peak;
+    // The memory maintenance thread refreshes these while any other thread 
may read them.
+    static std::atomic<int64_t> _vm_rss;
+    static std::atomic<int64_t> _vm_hwm;
+    static std::atomic<int64_t> _vm_size;
+    static std::atomic<int64_t> _vm_peak;
 };
 
 } // namespace doris
diff --git a/be/src/util/perf_counters_mac.cpp 
b/be/src/util/perf_counters_mac.cpp
index cb11d4b1f61..ca9bf53c836 100644
--- a/be/src/util/perf_counters_mac.cpp
+++ b/be/src/util/perf_counters_mac.cpp
@@ -15,16 +15,47 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include <mach/mach.h>
+
+#include "common/check.h"
 #include "util/perf_counters.h"
+#include "util/pretty_printer.h"
 
 namespace doris {
 
-int64_t PerfCounters::_vm_rss = 0;
-std::string PerfCounters::_vm_rss_str = "";
-int64_t PerfCounters::_vm_hwm = 0;
-int64_t PerfCounters::_vm_peak = 0;
-int64_t PerfCounters::_vm_size = 0;
+std::atomic<int64_t> PerfCounters::_vm_rss = 0;
+std::atomic<int64_t> PerfCounters::_vm_hwm = 0;
+std::atomic<int64_t> PerfCounters::_vm_peak = 0;
+std::atomic<int64_t> PerfCounters::_vm_size = 0;
+
+// macOS has no /proc/self/status, so read the task's basic info from the 
kernel: resident_size
+// is the counterpart of VmRSS, resident_size_max of VmHWM and virtual_size of 
VmSize. Mach does
+// not report a peak virtual size, so vm_peak keeps its default.
+void PerfCounters::get_proc_status(ProcStatus* out) {
+    mach_task_basic_info_data_t info;
+    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
+    const kern_return_t ret = task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
+                                        reinterpret_cast<task_info_t>(&info), 
&count);
+    DORIS_CHECK(ret == KERN_SUCCESS) << "task_info(MACH_TASK_BASIC_INFO) 
failed: " << ret;
+    out->vm_size = static_cast<int64_t>(info.virtual_size);
+    out->vm_rss = static_cast<int64_t>(info.resident_size);
+    out->vm_hwm = static_cast<int64_t>(info.resident_size_max);
+}
+
+void PerfCounters::refresh_proc_status() {
+    ProcStatus status;
+    get_proc_status(&status);
+    _vm_size.store(status.vm_size, std::memory_order_relaxed);
+    _vm_rss.store(status.vm_rss, std::memory_order_relaxed);
+    _vm_hwm.store(status.vm_hwm, std::memory_order_relaxed);
+}
 
-void PerfCounters::refresh_proc_status() {}
+std::string PerfCounters::get_vm_rss_str() {
+#ifdef ADDRESS_SANITIZER
+    return "[ASAN]" + PrettyPrinter::print(get_vm_rss(), TUnit::BYTES);
+#else
+    return PrettyPrinter::print(get_vm_rss(), TUnit::BYTES);
+#endif
+}
 
 } // namespace doris
diff --git a/be/test/CMakeLists.txt b/be/test/CMakeLists.txt
index 487f4c1c1f9..b891d984591 100644
--- a/be/test/CMakeLists.txt
+++ b/be/test/CMakeLists.txt
@@ -84,6 +84,8 @@ endif()
 
 if (OS_MACOSX)
     list(REMOVE_ITEM UT_FILES 
${CMAKE_CURRENT_SOURCE_DIR}/util/system_metrics_test.cpp)
+else ()
+    list(REMOVE_ITEM UT_FILES 
${CMAKE_CURRENT_SOURCE_DIR}/util/perf_counters_mac_test.cpp)
 endif()
 
 list(REMOVE_ITEM UT_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark_tool.cpp)
diff --git a/be/test/core/column/column_variant_v2_test.cpp 
b/be/test/core/column/column_variant_v2_test.cpp
index aeed33e9223..17e9b8c4184 100644
--- a/be/test/core/column/column_variant_v2_test.cpp
+++ b/be/test/core/column/column_variant_v2_test.cpp
@@ -3673,6 +3673,10 @@ TEST(ColumnVariantV2Test, 
ETCrossCheckTemporalClassMatrix) {
 
 TEST(ColumnVariantV2Test, 
TypedComparisonBoundariesMatchEncodedWithoutMaterializing) {
     auto check = [](const ColumnVariantV2::MutablePtr& typed) {
+        // IColumn::compare_at promises only the sign of its result. A typed 
STRING row compares
+        // with memcmp_small_allow_overflow15, which returns -1/0/1 on SSE2 
but the raw byte
+        // difference elsewhere (aarch64), while the encoded comparison 
returns -1/0/1.
+        auto sign = [](int cmp) { return (cmp > 0) - (cmp < 0); };
         ASSERT_TRUE(typed->is_typed());
         const auto* original = &typed->typed_column();
         auto encoded = encoded_copy_of(*typed);
@@ -3680,10 +3684,10 @@ TEST(ColumnVariantV2Test, 
TypedComparisonBoundariesMatchEncodedWithoutMaterializ
         for (size_t right = 0; right < typed->size(); ++right) {
             for (int hint : {-1, 1}) {
                 for (size_t left = 0; left < typed->size(); ++left) {
-                    const int expected = encoded->compare_at(left, right, 
*encoded, hint);
-                    EXPECT_EQ(typed->compare_at(left, right, *typed, hint), 
expected);
-                    EXPECT_EQ(typed->compare_at(left, right, *encoded, hint), 
expected);
-                    EXPECT_EQ(encoded->compare_at(left, right, *typed, hint), 
expected);
+                    const int expected = sign(encoded->compare_at(left, right, 
*encoded, hint));
+                    EXPECT_EQ(sign(typed->compare_at(left, right, *typed, 
hint)), expected);
+                    EXPECT_EQ(sign(typed->compare_at(left, right, *encoded, 
hint)), expected);
+                    EXPECT_EQ(sign(encoded->compare_at(left, right, *typed, 
hint)), expected);
                 }
                 for (int direction : {-1, 1}) {
                     std::vector<uint8_t> typed_cmp(typed->size(), 0);
diff --git a/be/test/util/perf_counters_mac_test.cpp 
b/be/test/util/perf_counters_mac_test.cpp
new file mode 100644
index 00000000000..828968aabaa
--- /dev/null
+++ b/be/test/util/perf_counters_mac_test.cpp
@@ -0,0 +1,53 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+#include <sys/mman.h>
+
+#include <cstring>
+
+#include "util/defer_op.h"
+#include "util/perf_counters.h"
+
+namespace doris {
+
+// refresh_proc_status() stores what it reads in process-wide counters that 
other tests expect to
+// stay unrefreshed, so this test reads the kernel's values through 
get_proc_status() instead.
+TEST(PerfCountersMacTest, ProcStatusReportsResidentMemory) {
+    PerfCounters::ProcStatus before;
+    PerfCounters::get_proc_status(&before);
+    EXPECT_GT(before.vm_rss, 0);
+    EXPECT_GE(before.vm_hwm, before.vm_rss);
+    EXPECT_GE(before.vm_size, before.vm_rss);
+
+    // Writing 64 MiB of newly mapped pages makes them resident, so the 
resident size has to grow
+    // with them. A heap buffer would not do: the allocator can hand back a 
freed block whose pages
+    // are still resident, and writing those adds nothing.
+    constexpr size_t TOUCHED_BYTES = 64UL * 1024 * 1024;
+    void* pages = mmap(nullptr, TOUCHED_BYTES, PROT_READ | PROT_WRITE, 
MAP_PRIVATE | MAP_ANONYMOUS,
+                       -1, 0);
+    ASSERT_NE(pages, MAP_FAILED);
+    Defer unmap_pages {[&]() { munmap(pages, TOUCHED_BYTES); }};
+    std::memset(pages, 1, TOUCHED_BYTES);
+    PerfCounters::ProcStatus after;
+    PerfCounters::get_proc_status(&after);
+    EXPECT_EQ(static_cast<char*>(pages)[TOUCHED_BYTES - 1], 1);
+    EXPECT_GE(after.vm_rss - before.vm_rss, static_cast<int64_t>(TOUCHED_BYTES 
/ 2));
+    EXPECT_GE(after.vm_hwm, after.vm_rss);
+}
+
+} // namespace doris


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to