Skip to content

Register ProfileEvents for executable UDF resource usage - #104998

Merged
vdimir merged 7 commits into
ClickHouse:masterfrom
XuJia0210:xujia/udf-profile-events
May 19, 2026
Merged

vdimir merged 7 commits into
ClickHouse:masterfrom
XuJia0210:xujia/udf-profile-events

Conversation

@XuJia0210

@XuJia0210 XuJia0210 commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Step 1 of adding resource usage observability for executable and executable_pool UDFs. Registers seven ProfileEvents so that subsequent PRs can wire ProfileEvents::increment in UserDefinedExecutableFunctionFactory::executeImpl and the rusage plumbing through ShellCommand / ShellCommandSource. The ExecutableUserDefinedFunction prefix is shared between both UDF types; per-UDF attribution is via system.query_log and used_executable_user_defined_functions.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.802

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`.
@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ad6eec4]

Summary: ✅


AI Review

Summary

This PR adds seven ProfileEvents registrations in src/Common/ProfileEvents.cpp for executable/executable pool UDF resource accounting. After reviewing the current diff and all existing discussion threads, I do not see unresolved correctness, safety, or performance issues in the current code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 15, 2026
Comment thread src/Common/ProfileEvents.cpp Resolved
XuJia0210 added 2 commits May 15, 2026 13:56
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.
@XuJia0210
XuJia0210 marked this pull request as ready for review May 15, 2026 06:42
@vdimir vdimir self-assigned this May 15, 2026
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) \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the Executable prefix for all of these ProfileEvents? I think it can be dropped.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, ExecutableUserDefined sounds good, it clearly distinguishes it from other UDF types (SQL and WASM).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The descriptions make it clear already. Just wondering about the names,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our naming convention uses singular.
You can check the file src/Common/ProfileEvents.cpp.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thank you.

Comment thread src/Common/ProfileEvents.cpp Outdated
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) \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this would just be memory amount × time, so it’s not really “peak” memory, but rather memory usage integrated over time, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Vdimir. It's not the real peak memory usage. It's integrated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will add peak to the name

Comment thread src/Common/ProfileEvents.cpp Outdated
\
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) \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit, the convention seems to be UserTimeMicroseconds instead of CPUUserMicroseconds, SystemTimeMicroseconds instead of CPUSystemMicroseconds

XuJia0210 and others added 2 commits May 18, 2026 09:32
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]>
@XuJia0210

Copy link
Copy Markdown
Contributor Author

02180_group_by_lowcardinality
test_distributed_frozen_replica/test.py
They are known to be flaky. No need to concern

@vdimir

vdimir commented May 18, 2026

Copy link
Copy Markdown
Member

02180_group_by_lowcardinality

Fixed in #105194

test_distributed_frozen_replica/test.py

Fixed in #105112

@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.20% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (14/14) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 19, 2026
@vdimir
vdimir added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit d668bd7 May 19, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Jun 4, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants