Repository navigation
Register ProfileEvents for executable UDF resource usage - #104998
Conversation
Add seven ProfileEvents covering invocations, wall time, CPU (user/system), memory-time integral, and stdin/stdout I/O for executable and executable pool UDFs. Names use the `ExecutableUserDefinedFunction` prefix so the same counters cover both UDF types; per-UDF attribution is left to query log filtering via the `used_executable_user_defined_functions` column. This is step 1 of the UDF Resource Usage Observability RFC. Subsequent PRs will add the `rusage` pipe in `ShellCommand`, per-block sampling in `ShellCommandSource`, and `ProfileEvents::increment` wiring in `UserDefinedExecutableFunctionFactory::executeImpl`.
|
Workflow [PR], commit [ad6eec4] Summary: ✅ AI ReviewSummaryThis PR adds seven Final VerdictStatus: ✅ Approve |
The CPU, memory, and I/O events all cover both `executable` and `executable_pool` UDFs, matching the invocation and elapsed events. Without this, the latter five descriptions read as if pool UDFs were not tracked.
The previous `ValueType::Bytes` would have caused `ProgressTable` to format the counter as bytes (and bytes/s for the rate column), but the quantity is byte-seconds, not bytes. There is no dedicated byte-seconds `ValueType` formatter yet, so fall back to `ValueType::Number` to avoid a unit mismatch in user-visible output.
| M(WasmModuleInstatiate, "Number of WebAssembly compartments created", ValueType::Number) \ | ||
| M(WasmMemoryAllocated, "Total memory allocated for WebAssembly compartments", ValueType::Bytes) \ | ||
| \ | ||
| M(ExecutableUserDefinedFunctionInvocations, "Number of executable and executable pool user-defined function invocations.", ValueType::Number) \ |
There was a problem hiding this comment.
Why do you need the Executable prefix for all of these ProfileEvents? I think it can be dropped.
There was a problem hiding this comment.
For me, ExecutableUserDefined sounds good, it clearly distinguishes it from other UDF types (SQL and WASM).
There was a problem hiding this comment.
Because we have SQL UDFs
In our daily communication, we use UDF to refer to executable UDFs. We ignored SQL UDFs
https://clickhouse.com/docs/sql-reference/functions/udf#sql-user-defined-functions
There was a problem hiding this comment.
I see, I didn't know that, thanks.
| M(WasmMemoryAllocated, "Total memory allocated for WebAssembly compartments", ValueType::Bytes) \ | ||
| \ | ||
| M(ExecutableUserDefinedFunctionInvocations, "Number of executable and executable pool user-defined function invocations.", ValueType::Number) \ | ||
| M(ExecutableUserDefinedFunctionElapsedMicroseconds, "Wall clock time spent executing executable and executable pool user-defined functions, in microseconds.", ValueType::Microseconds) \ |
There was a problem hiding this comment.
What do you think about using the prefix UserDefinedFunctions (plural), to emphasise the fact that these ProfileEvents are summed across all UDF invocations for the query?
There was a problem hiding this comment.
The descriptions make it clear already. Just wondering about the names,
There was a problem hiding this comment.
Our naming convention uses singular.
You can check the file src/Common/ProfileEvents.cpp.
| M(ExecutableUserDefinedFunctionElapsedMicroseconds, "Wall clock time spent executing executable and executable pool user-defined functions, in microseconds.", ValueType::Microseconds) \ | ||
| M(ExecutableUserDefinedFunctionCPUUserMicroseconds, "User mode CPU time consumed by executable and executable pool user-defined function child processes, in microseconds.", ValueType::Microseconds) \ | ||
| M(ExecutableUserDefinedFunctionCPUSystemMicroseconds, "Kernel mode CPU time consumed by executable and executable pool user-defined function child processes, in microseconds.", ValueType::Microseconds) \ | ||
| M(ExecutableUserDefinedFunctionMemoryUsageByteSeconds, "Memory used by executable and executable pool user-defined function child processes integrated over wall clock time, in byte-seconds.", ValueType::Number) \ |
There was a problem hiding this comment.
Should we clarify that this is peak memory? Both in the name and in the description? Something like this:
UserDefinedFunctionsPeakMemoryByteSeconds: Per-process peak memory used by executable and executable pool user-defined function child processes integrated over wall clock time, in byte-seconds.
There was a problem hiding this comment.
But this would just be memory amount × time, so it’s not really “peak” memory, but rather memory usage integrated over time, right?
There was a problem hiding this comment.
I agree with Vdimir. It's not the real peak memory usage. It's integrated
There was a problem hiding this comment.
Actually, no – for each launched process, we will observe its peak memory (M_peak_i) and its duration (T_i), multiply them (M_peak_i x T_i), and then sum across processes.
Breaking M_peak_i down into point-in-time observations (sum(M_tj_i) over j) is much more complicated.
There was a problem hiding this comment.
@XuJia0210 My point is that it's not integrated memory usage either. It would've been integrated if we used a time interval memory sample multiplied by the time interval and then summed across intervals, but we use one memory value, the peak. So it's more like "peak" memory usage integrated over time, where "peak" is individual per process.
There was a problem hiding this comment.
Ok, I will add peak to the name
| \ | ||
| M(ExecutableUserDefinedFunctionInvocations, "Number of executable and executable pool user-defined function invocations.", ValueType::Number) \ | ||
| M(ExecutableUserDefinedFunctionElapsedMicroseconds, "Wall clock time spent executing executable and executable pool user-defined functions, in microseconds.", ValueType::Microseconds) \ | ||
| M(ExecutableUserDefinedFunctionCPUUserMicroseconds, "User mode CPU time consumed by executable and executable pool user-defined function child processes, in microseconds.", ValueType::Microseconds) \ |
There was a problem hiding this comment.
Just a nit, the convention seems to be UserTimeMicroseconds instead of CPUUserMicroseconds, SystemTimeMicroseconds instead of CPUSystemMicroseconds
Match the convention already used by the top-level `UserTimeMicroseconds`
and `SystemTimeMicroseconds` events, replacing the awkward
`CPU{User,System}Microseconds` naming.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Clarify that the memory byte-seconds counter is computed from per-process peak memory (max `VmHWM` across the subtree) integrated over wall time, not from an integrated memory sample. Rename the event to `ExecutableUserDefinedFunctionPeakMemoryByteSeconds` and tighten the description accordingly. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
02180_group_by_lowcardinality |
LLVM Coverage Report
Changed lines: 100.00% (14/14) · Uncovered code |
Adds the eighth ProfileEvent `ExecutableUserDefinedFunctionPoolWaitMicroseconds` (the other seven were registered by PR ClickHouse#104998) and populates all eight on the `executable_pool` UDF path. A new `UDFProcessSubtreeSampler` walks `/proc/<pid>/task/<tid>/children` at borrow and again at release. The pre-snapshot writes `5` to `/proc/<pid>/clear_refs` to reset `VmHWM`, the post-snapshot reads `VmHWM` and sums `utime`/`stime` deltas for pids seen in both walks. CPU is only attributed for pids present at both snapshots, avoiding the over-attribution that would happen if a pre-walk `readStat` failed on a warm pool worker (its lifetime `utime` would land in the post sum with no baseline to subtract). The sampler is wired into the borrow lifecycle via a `shared_ptr` in `ShellCommandSourceConfiguration`. Other consumers (executable dictionaries, `StorageExecutable`) leave it null, so they remain strict no-ops. The increment block runs from `SCOPE_EXIT` so failed UDF queries still report whatever resource was consumed before the failure. Procfs failures (ENOENT, EACCES, parse errors) are silently skipped: CPU/memory increments may be dropped but the other six events keep firing.
Step 1 of adding resource usage observability for executable and executable_pool UDFs. Registers seven
ProfileEventsso that subsequent PRs can wireProfileEvents::incrementinUserDefinedExecutableFunctionFactory::executeImpland the rusage plumbing throughShellCommand/ShellCommandSource. TheExecutableUserDefinedFunctionprefix is shared between both UDF types; per-UDF attribution is viasystem.query_logandused_executable_user_defined_functions.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Version info
26.5.1.802