You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2026/09/19 15:53:13 UTC
[PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
andygrove opened a new pull request, #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048
## Which issue does this PR close?
Closes #6047.
## Rationale for this change
A traced run cannot currently account for Arrow memory held on the JVM. The existing counters cover
native allocations, memory pool reservations and the JVM heap, but Arrow buffers are off-heap, so
they show up in none of them.
Reporting the root allocator's total on its own would not separate the two things a reader needs to
tell apart. Comet imports batches from native over the Arrow C Data Interface, and Arrow charges an
imported buffer to whichever allocator wraps it (`BaseAllocator.wrapForeignAllocation` calls
`allocateBytes`), so the root's total mixes memory the JVM allocated with native memory that
`native_allocated` already counts.
## What changes are included in this PR?
- `CometArrowImportAllocator`, a child of `CometArrowAllocator` that owns buffers imported over the
C Data Interface. It reserves nothing, so every byte still escalates to the parent and the root
keeps reporting the total. Like the root it is never closed, because imported buffers are
reference counted and routinely outlive the task that imported them.
- `NativeUtil`'s `ArrowImporter` and the input imports in `CometUdfBridge` are taken against that
allocator. Struct allocation, exports and Arrow IPC buffers stay on the root, because those
really are memory the JVM allocated.
- `arrowMemoryMetrics` in the `comet` package object returns the two counters, and
`CometExecIterator.traceMemoryUsage` emits them alongside `jvm_heap_used`.
- The tracing guide gains both labels and a note on the overlap: `jvm_arrow_imported` is native
memory that `native_allocated` counts as well, so `jvm_arrow_allocated - jvm_arrow_imported` is
the Arrow memory the JVM allocated itself.
## How are these changes tested?
New tests in `NativeUtilSuite`:
- a batch imported over FFI is charged to the import allocator, and that allocator is a child of
the root
- the counters carry both labels, the imported counter is non-zero while an imported batch is
alive, and the total includes the imported bytes
New `CometUdfBridgeSuite`, registered in both PR build workflows, drives `CometUdfBridge.evaluate`
directly. The bridge closes its imported inputs before returning, so the probe UDF reads the import
allocator from inside the call, while its inputs are alive.
For regression coverage, `CometScalaUDFClassLoaderSuite` and `CometInMemoryCacheSuite` pass (42
tests). The latter already asserts that the root allocator returns to its prior total, which
exercises the escalation the two counters depend on.
The emission call itself is not unit tested, since that needs a traced native run. It will be
exercised on a traced TPC-H run.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048#issuecomment-5744441037
You're right, and the identity case is worse than the two from last round, because the transfer I
added is what causes it. I reproduced your numbers: with a 4096-row int input the import allocator
is left holding 128 bytes of the 16 KiB, matching your root = 17,302 / child = 128 exactly.
**Taking the first option: the counters are allocator charges.** I went after the second one first
and it isn't reachable without vendoring. `BaseAllocator.wrapForeignAllocation` routes through the
same `allocateBytes` / `onAllocation` path as an ordinary `buffer()` call, and release goes through
the same `releaseBytes` / `onRelease`, so an `AllocationListener` cannot distinguish foreign from
JVM-created in either direction. Wrapping the `ForeignAllocation` that
`ReferenceCountedArrowArray.unsafeAssociateAllocation` hands us would work, but `release0()` and
`memoryAddress()` are both protected, so a delegating wrapper has to live in
`org.apache.arrow.memory`. That is a worse trade than an honest contract for a diagnostic counter.
So the tracing guide, the memory management guide and both scaladocs now say what the counters are:
what each allocator is accountable for, not where the bytes were allocated, with the divergence
spelled out in both directions and your `native_allocated` point stated as no guarantee rather than
a caveat. The exact-subtraction claim is out of the PR description too.
**I still fixed the identity case,** because the transfer is my code and it should not make
attribution worse for a shape it was not aimed at. `result.getAllocator() != allocator` was a proxy
for "the UDF allocated its output from the import allocator", and a returned input satisfies the
proxy without being that case. The transfer now skips a result that is reference-identical to an
input, and the input loop's close covers it. I deliberately did not chase the general case, since a
result that merely shares buffers with an input, a slice say, still transfers. That one is named in
the docs as an illustration of the contract rather than as a gap to close.
**The regression you asked for** is in `CometUdfBridgeSuite`: an identity UDF, asserting the charge
stays on the import allocator, that importing the export back reads 4096 correct values after the
bridge's cleanup, and that the import allocator returns to its prior charge. Mutating the skip back
out fails it at 128 of 16384 bytes, which is how I confirmed your measurement.
Also ran `CometCodegenSuite` (98 tests, since it drives the bridge through real native execution),
`CometScalaUDFClassLoaderSuite` and `NativeUtilSuite`, and cross-compiled against Spark 3.5 /
Scala 2.12 as well as the default profile.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048#discussion_r4053871744
##########
spark/src/main/scala/org/apache/comet/Tracing.scala:
##########
@@ -23,6 +23,16 @@ object Tracing {
private val nativeLib = new Native
+ /**
+ * Emits the Arrow memory counters for the JVM side. `jvm_arrow_imported` is the part of
+ * `jvm_arrow_allocated` that came from native over the C Data Interface, so the difference is
+ * the Arrow memory the JVM allocated itself. See [[CometArrowImportAllocator]].
+ */
+ def logArrowMemory(): Unit = {
+ nativeLib.logMemoryUsage("jvm_arrow_allocated", CometArrowAllocator.getAllocatedMemory)
+ nativeLib.logMemoryUsage("jvm_arrow_imported", CometArrowImportAllocator.getAllocatedMemory)
Review Comment:
### Correctness
[P2] Track foreign buffer charges separately from the whole import allocator
This child also pays for memory allocated by the JVM. Arrow 18.3's `ArrayImporter` allocates its owning `ArrowArray` using the supplied allocator, and `BitVectorHelper.loadValidityBuffer` allocates a bitmap there when a native all-valid vector has no validity buffer. A component probe using this head's `ArrowImporter` with 4,096 integers reports root = child = 17,024 bytes, comprising 16,384 foreign payload bytes and 640 JVM-created bytes. The documented subtraction reports zero and loses those 640 bytes.
The error can include an entire UDF output: the interface permits allocating with `inputs.head.getAllocator`, and `Data.exportVector(root, ...)` does not transfer that allocation to the root. A second probe leaves 32,768 bytes of JVM-created output in this child after input cleanup, so all of it is subtracted. Both probes release cleanly.
Please measure foreign buffer charges separately, or expose this as import-allocator accounting without claiming the difference measures JVM-created memory. Add assertions for an absent validity buffer and an output allocated from the input allocator. The current positive-child and parent-rollup assertions pass in both cases.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048#discussion_r4054261775
##########
spark/src/main/scala/org/apache/comet/Tracing.scala:
##########
@@ -23,6 +23,16 @@ object Tracing {
private val nativeLib = new Native
+ /**
+ * Emits the Arrow memory counters for the JVM side. `jvm_arrow_imported` is the part of
+ * `jvm_arrow_allocated` that came from native over the C Data Interface, so the difference is
+ * the Arrow memory the JVM allocated itself. See [[CometArrowImportAllocator]].
+ */
+ def logArrowMemory(): Unit = {
+ nativeLib.logMemoryUsage("jvm_arrow_allocated", CometArrowAllocator.getAllocatedMemory)
+ nativeLib.logMemoryUsage("jvm_arrow_imported", CometArrowImportAllocator.getAllocatedMemory)
Review Comment:
Taking your first option here, in 10fff7090. Full response in
https://github.com/apache/datafusion-comet/pull/6048#issuecomment-5744441037.
Origin tracking is not reachable without vendoring: `wrapForeignAllocation` routes through the same
`allocateBytes` / `onAllocation` / `releaseBytes` path as ordinary allocation, so an
`AllocationListener` cannot tell the two apart, and `ForeignAllocation.release0()` and
`memoryAddress()` are both protected, so a wrapper that could hook the release has to live in
`org.apache.arrow.memory`. So both counters are now documented as allocator charges, with no
allocation-origin bound and no guaranteed overlap with `native_allocated`, in the tracing guide, the
memory management guide and both scaladocs. The PR description no longer carries the subtraction
claim either.
The identity case is also fixed rather than only documented, since the transfer is mine and should
not make attribution worse for a shape it was not aimed at: the transfer now skips a result that is
reference-identical to an input. A result that merely shares buffers with an input still transfers,
and that is named in the docs as an illustration of the contract. The return-input regression is in
`CometUdfBridgeSuite`; it fails at 128 of 16384 bytes with the skip mutated out, matching your
measurement.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048#issuecomment-5743763058
Thanks for the review. The P2 is right, and I reproduced all three mechanisms against the Arrow
18.3 sources before changing anything:
- `ArrayImporter.java:58` allocates the owning `ArrowArray` with `ArrowArray.allocateNew(allocator)`,
so every import charges a JVM-created struct to the import allocator.
- `BitVectorHelper.loadValidityBuffer:328` does `allocator.buffer(getValidityBufferSize(valueCount))`
when the source validity buffer is absent and the field node is all-valid or all-null. For 4096
rows that is 512 bytes, which with the struct matches the 640 bytes you measured.
- `Data.exportVector`'s allocator is documented as being "for allocating C data interface fields"
and only constructs an `ArrayExporter`; it never re-owns the vector's buffers. So a UDF result
allocated from `inputs.head.getAllocator()` stays charged to the import allocator for as long as
the export holds it.
So the docs claim that the difference is the Arrow memory the JVM allocated itself was too strong.
Two changes in response.
**Narrowed the claim.** `jvm_arrow_imported` is now described as what the FFI import path holds,
which is mostly imported buffers plus a little JVM-allocated import overhead, making
`jvm_arrow_allocated - jvm_arrow_imported` a close lower bound rather than an exact split. The
tracing guide names both contaminants with their magnitudes, and states that the two counters are
separate reads of process-wide state, so they are neither an atomic per-query balance nor a measure
of RSS. The scaladoc on `CometArrowImportAllocator` and `Tracing.logArrowMemory` says the same.
Worth noting the direction: these bytes make the reported JVM-own figure an underestimate, so the
unaccounted memory this PR exists to surface is marginally larger than the counter suggests, not
smaller.
**Fixed the UDF case,** since that one is both the largest and avoidable. `CometUdfBridge` now
transfers the result to the root allocator before export when the UDF allocated it elsewhere.
`TransferPair.transfer()` re-parents buffer ownership through
`referenceManager.transferOwnership(...)` without copying the payload, so this costs an ownership
move rather than a copy, and the transferred vector is closed alongside the emptied original.
**Tests,** both of the cases you asked for:
- A characterization test in `NativeUtilSuite` asserting the import allocator holds strictly more
than the sum of the imported buffers, which pins the struct and validity overhead rather than
pretending it is absent.
- A regression test in `CometUdfBridgeSuite` with a UDF that allocates its output from
`inputs.head.getAllocator()`. It asserts before the exported structs are released, since that is
the window in which the export keeps the buffers alive and the miscounting is observable. It
failed at 32768 bytes charged to the import allocator before the fix and passes after it.
I also added the import child to the allocator inventory in the memory management guide, as
suggested.
On making the child exclusively foreign: I looked at it and decided against it. Both the struct and
the validity synthesis happen inside Arrow's own import path using the single allocator we pass, so
excluding them would mean vendoring further into Arrow's importer and re-deriving that logic, which
would be fragile across Arrow upgrades for a few hundred bytes per batch. Narrowing the claim and
removing the one unbounded contaminant seemed the better trade, but I am happy to revisit if you
disagree.
On the performance question: tracing is off by default, so the added JNI calls only occur when it is
enabled. A traced TPC-H SF100 run on 2 executors x 8 cores (Spark 4.1.1, 16g off-heap, build with
`jemalloc,alloc-accounting`) completed its iteration in 208.6 s against a roughly 220 s untraced
baseline on the same machine, so the two extra calls per sample are inside run-to-run variation. For
reference, that run measured peak `native_allocated` 2193.7 MB, `jvm_arrow_allocated` 144.0 MB and
`jvm_arrow_imported` 53.7 MB, with 0 of 16970 paired samples showing imported exceeding allocated.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: trace Arrow memory held on the JVM side [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #6048:
URL: https://github.com/apache/datafusion-comet/pull/6048#discussion_r4053935463
##########
spark/src/main/scala/org/apache/comet/Tracing.scala:
##########
@@ -23,6 +23,16 @@ object Tracing {
private val nativeLib = new Native
+ /**
+ * Emits the Arrow memory counters for the JVM side. `jvm_arrow_imported` is the part of
+ * `jvm_arrow_allocated` that came from native over the C Data Interface, so the difference is
+ * the Arrow memory the JVM allocated itself. See [[CometArrowImportAllocator]].
+ */
+ def logArrowMemory(): Unit = {
+ nativeLib.logMemoryUsage("jvm_arrow_allocated", CometArrowAllocator.getAllocatedMemory)
+ nativeLib.logMemoryUsage("jvm_arrow_imported", CometArrowImportAllocator.getAllocatedMemory)
Review Comment:
Confirmed and fixed in d5faf5626. Full response in
https://github.com/apache/datafusion-comet/pull/6048#issuecomment-5743763058.
I reproduced both mechanisms against the Arrow 18.3 sources before changing anything:
`ArrayImporter.java:58` allocates the owning `ArrowArray` from this allocator, and
`BitVectorHelper.loadValidityBuffer:328` allocates a validity bitmap here when an imported vector
is all-valid or all-null and carries no validity buffer. With the struct that matches the 640 bytes
you measured for 4096 rows.
Taking your second option for the general case: `jvm_arrow_imported` is now documented as what the
import path holds, so `jvm_arrow_allocated - jvm_arrow_imported` is described as a close lower
bound rather than an exact split, in the tracing guide and in both scaladocs.
The UDF case is fixed rather than documented, since it was the one unbounded contaminant.
`CometUdfBridge` now transfers the result to the root before export when the UDF allocated it
elsewhere, which `TransferPair` does as an ownership move rather than a payload copy.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org