-
Notifications
You must be signed in to change notification settings - Fork 194
Expose raft::memory_tracking_resources to C/Python/Java/Rust #2073
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
base: main
Are you sure you want to change the base?
Changes from all commits
48cd2de
ffb3921
364e61a
2161697
3c88ea3
cf1a6a7
6bbe05e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.nvidia.cuvs.internal; | ||
|
|
@@ -13,7 +13,10 @@ | |
| import com.nvidia.cuvs.internal.common.PinnedMemoryBuffer; | ||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.lang.foreign.ValueLayout; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.time.Duration; | ||
|
|
||
| /** | ||
| * Used for allocating resources for cuVS | ||
|
|
@@ -46,6 +49,45 @@ public CuVSResourcesImpl(Path tempDirectory) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructor that allocates a tracking resources handle. All memory | ||
| * allocations made through this handle are written as CSV samples to | ||
|
Comment on lines
+53
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Would it be worth clarifying here that this does not include the |
||
| * {@code memoryTrackingCsvPath} from a background thread, restoring the | ||
| * global memory resources on {@link #close()}. | ||
| * | ||
| * @param tempDirectory the temporary directory to use for | ||
| * intermediate operations | ||
| * @param memoryTrackingCsvPath path to the output CSV file | ||
| * (created/truncated) | ||
| * @param memoryTrackingSampleInterval minimum interval between successive | ||
| * CSV samples | ||
| */ | ||
| public CuVSResourcesImpl( | ||
| Path tempDirectory, | ||
| Path memoryTrackingCsvPath, | ||
| Duration memoryTrackingSampleInterval) { | ||
| this.tempDirectory = tempDirectory; | ||
| try (var localArena = Arena.ofConfined()) { | ||
| var resourcesMemorySegment = localArena.allocate(cuvsResources_t); | ||
| byte[] pathBytes = | ||
| memoryTrackingCsvPath.toString().getBytes(StandardCharsets.UTF_8); | ||
| var pathSegment = localArena.allocate(pathBytes.length + 1L); | ||
| MemorySegment.copy( | ||
| pathBytes, 0, pathSegment, ValueLayout.JAVA_BYTE, 0, pathBytes.length); | ||
| pathSegment.set(ValueLayout.JAVA_BYTE, pathBytes.length, (byte) 0); | ||
| long sampleIntervalMs = memoryTrackingSampleInterval.toMillis(); | ||
| checkCuVSError( | ||
| cuvsResourcesCreateWithMemoryTracking( | ||
| resourcesMemorySegment, pathSegment, sampleIntervalMs), | ||
| "cuvsResourcesCreateWithMemoryTracking"); | ||
| this.resourceHandle = resourcesMemorySegment.get(cuvsResources_t, 0); | ||
| var deviceIdPtr = localArena.allocate(C_INT); | ||
| checkCuVSError(cuvsDeviceIdGet(resourceHandle, deviceIdPtr), "cuvsDeviceIdGet"); | ||
| this.deviceId = deviceIdPtr.get(C_INT, 0); | ||
| this.access = new ScopedAccessWithHostBuffer(resourceHandle, hostBuffer.address()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ScopedAccess access() { | ||
| return this.access; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.nvidia.cuvs; | ||
|
|
||
| import static com.carrotsearch.randomizedtesting.RandomizedTest.assumeTrue; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import com.nvidia.cuvs.spi.CuVSProvider; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.time.Duration; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class MemoryTrackingResourcesIT extends CuVSTestCase { | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| assumeTrue("not supported on " + System.getProperty("os.name"), isLinuxAmd64()); | ||
| } | ||
|
|
||
| @Test | ||
| public void writesNonEmptyCsv() throws Throwable { | ||
| Path csv = Files.createTempFile("cuvs-mtrack", ".csv"); | ||
| try { | ||
| try (var resources = | ||
| CuVSResources.create( | ||
| CuVSProvider.tempDirectory(), csv, Duration.ofMillis(2))) { | ||
|
|
||
| // Allocate / release a couple of small device buffers so the | ||
| // background CSV reporter has something to report. | ||
| var b1 = | ||
| CuVSMatrix.deviceBuilder(resources, 64, 32, CuVSMatrix.DataType.FLOAT); | ||
| for (int i = 0; i < 64; ++i) { | ||
| b1.addVector(new float[32]); | ||
| } | ||
| try (var m1 = b1.build()) { | ||
| var b2 = | ||
| CuVSMatrix.deviceBuilder(resources, 32, 16, CuVSMatrix.DataType.FLOAT); | ||
| for (int i = 0; i < 32; ++i) { | ||
| b2.addVector(new float[16]); | ||
| } | ||
| try (var m2 = b2.build()) { | ||
| // Allow the background CSV reporter at least a few ticks | ||
| // before the matrices are released and the handle closed. | ||
| Thread.sleep(20); | ||
| } | ||
| } | ||
| } | ||
| // closing the resources flushes the CSV and restores globals | ||
| assertTrue("csv should be non-empty", Files.size(csv) > 0); | ||
| } finally { | ||
| Files.deleteIfExists(csv); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.