github-actions[bot] commented on code in PR #70847:
URL: https://github.com/apache/doris/pull/70847#discussion_r4658574937


##########
be/test/util/perf_counters_mac_test.cpp:
##########
@@ -0,0 +1,47 @@
+// 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 <cstring>
+#include <vector>
+
+#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 makes those pages resident, so the resident size has to 
grow with them.
+    constexpr size_t TOUCHED_BYTES = 64UL * 1024 * 1024;
+    std::vector<char> buffer(TOUCHED_BYTES);
+    std::memset(buffer.data(), 1, buffer.size());
+    PerfCounters::ProcStatus after;
+    PerfCounters::get_proc_status(&after);
+    EXPECT_EQ(buffer.back(), 1);
+    EXPECT_GE(after.vm_rss - before.vm_rss, static_cast<int64_t>(TOUCHED_BYTES 
/ 2));

Review Comment:
   [P2] Use fresh mapped pages for the RSS-growth assertion
   
   A heap allocation can reuse pages already included in `before.vm_rss`. For 
example, supported macOS x86 `BUILD_TYPE_UT=DEBUG` tests link tcmalloc; its 
[pinned PageHeap 
implementation](https://github.com/gperftools/gperftools/blob/gperftools-2.18.1/src/page_heap.cc)
 retains freed resident spans between scavenges and reuses them for later 
allocations. Repeating this test in one process, or running it after a 
sufficiently large allocation, can therefore give this vector resident cached 
pages: touching them adds almost no RSS and the 32 MiB minimum fails even with 
correct counters. Use a fresh anonymous VM mapping with scoped unmapping, or 
avoid deriving mandatory RSS growth from a reusable heap allocation.



##########
be/src/util/perf_counters_mac.cpp:
##########
@@ -25,6 +29,31 @@ int64_t PerfCounters::_vm_hwm = 0;
 int64_t PerfCounters::_vm_peak = 0;
 int64_t PerfCounters::_vm_size = 0;
 
-void PerfCounters::refresh_proc_status() {}
+// 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 = status.vm_size;
+    _vm_rss = status.vm_rss;
+    _vm_hwm = status.vm_hwm;
+#ifdef ADDRESS_SANITIZER
+    _vm_rss_str = "[ASAN]" + PrettyPrinter::print(_vm_rss, TUnit::BYTES);
+#else
+    _vm_rss_str = PrettyPrinter::print(_vm_rss, TUnit::BYTES);

Review Comment:
   [P2] Synchronize the newly populated process-status cache
   
   `Daemon::memory_maintenance_thread()` now writes these shared fields while 
other threads read them: `get_be_resource()` reads `_vm_rss` on its heavy 
worker, `/profile` reads RSS/size on an HTTP worker, and load-side memory-limit 
logging copies `_vm_rss_str` through `process_memory_used_details_str()`. The 
fields and getters in `perf_counters.h` use plain `int64_t` and `std::string`, 
with no shared lock, so these normal concurrent operations cause C++ data 
races, including string copy versus assignment. The previous macOS refresh was 
empty; the equivalent Linux defect is pre-existing. Publish numeric values 
atomically and synchronize the string reads/writes, or format it from an atomic 
numeric snapshot.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to