Skip to content

fix: Fix data race in memory profiling #1727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions spark/src/main/java/org/apache/spark/CometTaskMemoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
package org.apache.spark;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.spark.memory.MemoryConsumer;
import org.apache.spark.memory.MemoryMode;
Expand All @@ -30,40 +34,46 @@
* memory manager. This assumes Spark's off-heap memory mode is enabled.
*/
public class CometTaskMemoryManager {

private static final Logger logger = LoggerFactory.getLogger(CometTaskMemoryManager.class);

/** The id uniquely identifies the native plan this memory manager is associated to */
private final long id;

private final TaskMemoryManager internal;
private final NativeMemoryConsumer nativeMemoryConsumer;
private long used;
private final AtomicLong used = new AtomicLong();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍


public CometTaskMemoryManager(long id) {
this.id = id;
this.internal = TaskContext$.MODULE$.get().taskMemoryManager();
this.nativeMemoryConsumer = new NativeMemoryConsumer();
this.used = 0;
}

// Called by Comet native through JNI.
// Returns the actual amount of memory (in bytes) granted.
public long acquireMemory(long size) {
long acquired = internal.acquireExecutionMemory(size, nativeMemoryConsumer);
used.addAndGet(acquired);
if (acquired < size) {
// If memory manager is not able to acquire the requested size, log memory usage
internal.showMemoryUsage();
}
used += acquired;
return acquired;
}

// Called by Comet native through JNI
public void releaseMemory(long size) {
used -= size;
long newUsed = used.addAndGet(-size);
if (newUsed < 0) {
logger.error(
"Used memory is negative: " + newUsed + " after releasing memory chunk of: " + size);
}
internal.releaseExecutionMemory(size, nativeMemoryConsumer);
}

public long getUsed() {
return used;
return used.get();
}

/**
Expand Down
Loading