Skip to content
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

Fix some buffer leaks #2106

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
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
33 changes: 13 additions & 20 deletions sql-plugin/src/main/scala/com/nvidia/spark/rapids/GpuSortExec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,12 @@ case class GpuOutOfCoreSortIterator(
// The entire thing is sorted
withResource(sortedTbl.contiguousSplit()) { splits =>
assert(splits.length == 1)
memUsed += splits.head.getBuffer.getLength
closeOnExcept(
GpuColumnVectorFromBuffer.from(splits.head, sorter.projectedBatchTypes)) { cb =>
val sp = SpillableColumnarBatch(cb, SpillPriorities.ACTIVE_ON_DECK_PRIORITY,
spillCallback)
sortedSize += sp.sizeInBytes
sorted.add(sp)
}
val ct = splits.head
memUsed += ct.getBuffer.getLength
val sp = SpillableColumnarBatch(ct, sorter.projectedBatchTypes,
SpillPriorities.ACTIVE_ON_DECK_PRIORITY, spillCallback)
sortedSize += sp.sizeInBytes
sorted.add(sp)
}
} else {
val splitIndexes = if (sortedOffset >= 0) {
Expand Down Expand Up @@ -326,13 +324,10 @@ case class GpuOutOfCoreSortIterator(
withResource(sortedTbl.contiguousSplit(splitIndexes: _*)) { splits =>
memUsed += splits.map(_.getBuffer.getLength).sum
val stillPending = if (sortedOffset >= 0) {
closeOnExcept(
GpuColumnVectorFromBuffer.from(splits.head, sorter.projectedBatchTypes)) { cb =>
val sp = SpillableColumnarBatch(cb, SpillPriorities.ACTIVE_ON_DECK_PRIORITY,
spillCallback)
sortedSize += sp.sizeInBytes
sorted.add(sp)
}
val sp = SpillableColumnarBatch(splits.head, sorter.projectedBatchTypes,
SpillPriorities.ACTIVE_ON_DECK_PRIORITY, spillCallback)
sortedSize += sp.sizeInBytes
sorted.add(sp)
splits.slice(1, splits.length)
} else {
splits
Expand All @@ -342,11 +337,9 @@ case class GpuOutOfCoreSortIterator(
stillPending.zip(boundaries).foreach {
case (ct: ContiguousTable, lower: UnsafeRow) =>
if (ct.getRowCount > 0) {
closeOnExcept(
GpuColumnVectorFromBuffer.from(ct, sorter.projectedBatchTypes)) { cb =>
pending.add(SpillableColumnarBatch(cb, SpillPriorities.ACTIVE_BATCHING_PRIORITY,
spillCallback), lower)
}
val sp = SpillableColumnarBatch(splits.head, sorter.projectedBatchTypes,
SpillPriorities.ACTIVE_ON_DECK_PRIORITY, spillCallback)
pending.add(sp, lower)
} else {
ct.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.nvidia.spark.rapids

import ai.rapids.cudf.ContiguousTable

import org.apache.spark.TaskContext
import org.apache.spark.sql.rapids.TempSpillBufferId
import org.apache.spark.sql.types.DataType
Expand Down Expand Up @@ -150,6 +152,25 @@ object SpillableColumnarBatch extends Arm {
}
}

/**
* Create a new SpillableColumnarBatch
* @note The caller is responsible for closing the contiguous table parameter.
* @param ct contiguous table containing the batch GPU data
* @param sparkTypes array of Spark types describing the data schema
* @param priority the initial spill priority of this batch
* @param spillCallback a callback when the buffer is spilled. This should be very light weight.
* It should never allocate GPU memory and really just be used for metrics.
*/
def apply(
ct: ContiguousTable,
sparkTypes: Array[DataType],
priority: Long,
spillCallback: RapidsBuffer.SpillCallback): SpillableColumnarBatch = {
val id = TempSpillBufferId()
RapidsBufferCatalog.addContiguousTable(id, ct, priority, spillCallback)
new SpillableColumnarBatchImpl(id, ct.getRowCount.toInt, sparkTypes)
}

private[this] def addBatch(
id: RapidsBufferId,
batch: ColumnarBatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,17 @@ class RapidsDeviceMemoryStoreSuite extends FunSuite with Arm with MockitoSugar {
}

class MockSpillStore(catalog: RapidsBufferCatalog)
extends RapidsBufferStore(StorageTier.HOST, catalog) {
extends RapidsBufferStore(StorageTier.HOST, catalog) with Arm {
val spilledBuffers = new ArrayBuffer[RapidsBufferId]

override protected def createBuffer(b: RapidsBuffer, m: MemoryBuffer, s: Cuda.Stream)
: RapidsBufferBase = {
spilledBuffers += b.id
new MockRapidsBuffer(b.id, b.size, b.meta, b.getSpillPriority)
override protected def createBuffer(
b: RapidsBuffer,
m: MemoryBuffer,
s: Cuda.Stream): RapidsBufferBase = {
withResource(m) { _ =>
spilledBuffers += b.id
new MockRapidsBuffer(b.id, b.size, b.meta, b.getSpillPriority)
}
}

class MockRapidsBuffer(id: RapidsBufferId, size: Long, meta: TableMeta, spillPriority: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ class RapidsHostMemoryStoreSuite extends FunSuite with Arm with MockitoSugar {

devStore.addContiguousTable(smallBufferId, smallTable, spillPriority)
devStore.synchronousSpill(0)
val ac: ArgumentCaptor[RapidsBuffer] = ArgumentCaptor.forClass(classOf[RapidsBuffer])
verify(mockStore).copyBuffer(ac.capture(), ArgumentMatchers.any[MemoryBuffer],
ArgumentMatchers.any[Cuda.Stream])
assertResult(bigBufferId)(ac.getValue.id)
val rapidsBufferCaptor: ArgumentCaptor[RapidsBuffer] =
ArgumentCaptor.forClass(classOf[RapidsBuffer])
val memoryBufferCaptor: ArgumentCaptor[MemoryBuffer] =
ArgumentCaptor.forClass(classOf[MemoryBuffer])
verify(mockStore).copyBuffer(rapidsBufferCaptor.capture(),
memoryBufferCaptor.capture(), ArgumentMatchers.any[Cuda.Stream])
withResource(memoryBufferCaptor.getValue) { _ =>
assertResult(bigBufferId)(rapidsBufferCaptor.getValue.id)
}
}
}
}
Expand Down