morningman opened a new pull request, #70847:
URL: https://github.com/apache/doris/pull/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` reads `/proc/self/status` on Linux and is a stub on 
macOS.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #62475 (added the counter check that aborts), #13195 (added the 
macOS `PerfCounters` stub), #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`. 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.
   - **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. 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`. 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 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. 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. Linux is unchanged.
   
   ### Testing
   
   All runs were local on macOS arm64 with ASAN builds. The unit tests ran 
before a comment-only edit to `process_profile.cpp`; the BE runs with the fix 
are on the final code.
   
   - 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: three `/profile` requests returned 200, each 
`PhysicalMemory(VmRSS)` lay between the `ps` readings taken just before and 
after it (1.01 to 1.03 GiB), and `UntrackedMemory` was RSS minus the 16 KiB 
tracked.
   - The guard alone (this PR with `get_proc_status()` reporting `vm_rss = 0`): 
`/profile` returned 200 and showed `UntrackedMemory Current: -16384.00 B`, the 
value that aborted master.
   - New `PerfCountersMacTest.ProcStatusReportsResidentMemory` (macOS only): 
passed 50 of 50 runs, and fails with `before.vm_rss > 0, actual: 0 vs 0` when 
`get_proc_status()` reports `vm_rss = 0`.
   - `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.*` in the same process: 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.
   
   ### 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.
   - 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. 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.
   


-- 
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