morningman commented on code in PR #70847:
URL: https://github.com/apache/doris/pull/70847#discussion_r4658706446
##########
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:
Fixed in d17d586a505. The four cached values are now `std::atomic<int64_t>`,
stored and loaded with `memory_order_relaxed`: each value is published on its
own and readers only need a recent one. This is how `MemInfo` publishes the
values that `refresh_process_memory_metrics()` refreshes right after this one.
The cached `_vm_rss_str` is gone; `get_vm_rss_str()` formats the atomic value
on each call, with the same text as before, so no reader copies a string while
a refresh assigns it. The Linux `refresh_proc_status()` gets the same change,
so the pre-existing race there is fixed too.
Checked with a ThreadSanitizer build of `perf_counters.h` and
`perf_counters_mac.cpp`, one thread refreshing 2000 times while another calls
every getter: data race reports between `refresh_proc_status()` and
`get_vm_size()` on the previous head, none on d17d586a505 in 4 runs.
##########
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:
Fixed in d17d586a505: the test now writes a fresh `MAP_PRIVATE |
MAP_ANONYMOUS` mapping of 64 MiB and unmaps it in a `Defer`.
Reproduced first with the system allocator, which macOS arm64
`BUILD_TYPE_UT=DEBUG`/`RELEASE` builds link instead of tcmalloc: the vector
version passed its first run and failed the next two in the same process (RSS
growth 16384 and 0 against the 33554432 minimum). The mmap version passed 50 of
50 runs there, and still fails both RSS assertions when `get_proc_status()`
reports `vm_rss = 0`. The earlier 50/50 came from an ASAN build, whose
allocator maps every large block fresh, so it could not show the problem.
--
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]