-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GLUTEN-5414] [VL] Support Arrow native memory pool usage track (#5550)
- Loading branch information
1 parent
e633887
commit cee1f3b
Showing
20 changed files
with
161 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
gluten-data/src/main/java/org/apache/gluten/memory/arrow/pool/ArrowNativeMemoryPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.memory.arrow.pool; | ||
|
||
import org.apache.arrow.dataset.jni.NativeMemoryPool; | ||
import org.apache.spark.util.TaskResource; | ||
import org.apache.spark.util.TaskResources; | ||
import org.apache.spark.util.Utils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ArrowNativeMemoryPool implements TaskResource { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ArrowNativeMemoryPool.class); | ||
|
||
private final NativeMemoryPool arrowPool; | ||
private final ArrowReservationListener listener; | ||
|
||
public ArrowNativeMemoryPool() { | ||
listener = new ArrowReservationListener(TaskResources.getSharedUsage()); | ||
arrowPool = NativeMemoryPool.createListenable(listener); | ||
} | ||
|
||
public static NativeMemoryPool arrowPool(String name) { | ||
if (!TaskResources.inSparkTask()) { | ||
throw new IllegalStateException("This method must be called in a Spark task."); | ||
} | ||
String id = "ArrowNativeMemoryPool:" + name; | ||
return TaskResources.addResourceIfNotRegistered(id, () -> createArrowNativeMemoryPool(name)) | ||
.getArrowPool(); | ||
} | ||
|
||
private static ArrowNativeMemoryPool createArrowNativeMemoryPool(String name) { | ||
return new ArrowNativeMemoryPool(); | ||
} | ||
|
||
@Override | ||
public void release() throws Exception { | ||
if (arrowPool.getBytesAllocated() != 0) { | ||
LOGGER.warn( | ||
String.format( | ||
"Arrow pool still reserved non-zero bytes, " | ||
+ "which may cause memory leak, size: %s. ", | ||
Utils.bytesToString(arrowPool.getBytesAllocated()))); | ||
} | ||
arrowPool.close(); | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String resourceName() { | ||
return "arrow_mem"; | ||
} | ||
|
||
public NativeMemoryPool getArrowPool() { | ||
return arrowPool; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
gluten-data/src/main/java/org/apache/gluten/memory/arrow/pool/ArrowReservationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.gluten.memory.arrow.pool; | ||
|
||
import org.apache.gluten.memory.SimpleMemoryUsageRecorder; | ||
|
||
public class ArrowReservationListener implements org.apache.arrow.dataset.jni.ReservationListener { | ||
private final SimpleMemoryUsageRecorder sharedUsage; // shared task metrics | ||
|
||
public ArrowReservationListener(SimpleMemoryUsageRecorder recorder) { | ||
this.sharedUsage = recorder; | ||
} | ||
|
||
@Override | ||
public void reserve(long size) { | ||
synchronized (this) { | ||
sharedUsage.inc(size); | ||
} | ||
} | ||
|
||
@Override | ||
public void unreserve(long size) { | ||
synchronized (this) { | ||
sharedUsage.inc(-size); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters