Skip to content

Commit

Permalink
Merge pull request #636 from lplewa/memtargetCapacity
Browse files Browse the repository at this point in the history
Memtarget capacity
  • Loading branch information
lukaszstolarczuk authored Aug 14, 2024
2 parents 9c8d119 + 892f752 commit 4f57234
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 11 deletions.
8 changes: 8 additions & 0 deletions include/umf/memtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef UMF_MEMTARGET_H
#define UMF_MEMTARGET_H 1

#include <stddef.h>
#include <umf/base.h>

#ifdef __cplusplus
Expand All @@ -31,6 +32,13 @@ typedef enum umf_memtarget_type_t {
umf_result_t umfMemtargetGetType(umf_const_memtarget_handle_t hMemtarget,
umf_memtarget_type_t *type);

/// \brief Get size of the memory target in bytes.
/// \param hMemtarget handle to the memory target
/// \param capacity [out] capacity of the memory target
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfMemtargetGetCapacity(umf_const_memtarget_handle_t hMemtarget,
size_t *capacity);

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions src/libumf.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ EXPORTS
umfMemspaceDestroy
umfMemspaceMemtargetNum
umfMemspaceMemtargetGet
umfMemtargetGetCapacity
umfMemtargetGetType
umfOpenIPCHandle
umfPoolAlignedMalloc
Expand Down
1 change: 1 addition & 0 deletions src/libumf.map.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ UMF_1.0 {
umfMemspaceDestroy;
umfMemspaceMemtargetNum;
umfMemspaceMemtargetGet;
umfMemtargetGetCapacity;
umfMemtargetGetType;
umfOpenIPCHandle;
umfPoolAlignedMalloc;
Expand Down
6 changes: 2 additions & 4 deletions src/memspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,8 @@ static int propertyCmp(const void *a, const void *b) {
}
}

umf_result_t
umfMemspaceSortDesc(umf_memspace_handle_t hMemspace,
umf_result_t (*getProperty)(umf_memtarget_handle_t node,
uint64_t *property)) {
umf_result_t umfMemspaceSortDesc(umf_memspace_handle_t hMemspace,
umfGetPropertyFn getProperty) {
if (!hMemspace || !getProperty) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
Expand Down
3 changes: 2 additions & 1 deletion src/memspace_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct umf_memspace_t {
umf_result_t umfMemspaceClone(umf_const_memspace_handle_t hMemspace,
umf_memspace_handle_t *outHandle);

typedef umf_result_t (*umfGetPropertyFn)(umf_memtarget_handle_t, uint64_t *);
typedef umf_result_t (*umfGetPropertyFn)(umf_const_memtarget_handle_t,
uint64_t *);

///
/// \brief Sorts memspace by getProperty() in descending order
Expand Down
2 changes: 1 addition & 1 deletion src/memtarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ umf_result_t umfMemtargetClone(umf_memtarget_handle_t memoryTarget,
return UMF_RESULT_SUCCESS;
}

umf_result_t umfMemtargetGetCapacity(umf_memtarget_handle_t memoryTarget,
umf_result_t umfMemtargetGetCapacity(umf_const_memtarget_handle_t memoryTarget,
size_t *capacity) {
if (!memoryTarget || !capacity) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
Expand Down
3 changes: 1 addition & 2 deletions src/memtarget_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ void umfMemtargetDestroy(umf_memtarget_handle_t memoryTarget);

umf_result_t umfMemtargetClone(umf_memtarget_handle_t memoryTarget,
umf_memtarget_handle_t *outHandle);
umf_result_t umfMemtargetGetCapacity(umf_memtarget_handle_t memoryTarget,
size_t *capacity);

umf_result_t umfMemtargetGetBandwidth(umf_memtarget_handle_t srcMemoryTarget,
umf_memtarget_handle_t dstMemoryTarget,
size_t *bandwidth);
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ if(LINUX AND (NOT UMF_DISABLE_HWLOC)) # OS-specific functions are implemented
add_umf_test(
NAME memtarget
SRCS memspaces/memtarget.cpp
LIBS ${LIBNUMA_LIBRARIES})
LIBS ${LIBNUMA_LIBRARIES} ${LIBHWLOC_LIBRARIES})
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND UMF_BUILD_FUZZTESTS)
add_subdirectory(fuzz)
endif()
Expand Down
41 changes: 39 additions & 2 deletions test/memspaces/memtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "memspace_fixtures.hpp"
#include "memspace_helpers.hpp"

#include <umf/base.h>
Expand All @@ -13,17 +14,53 @@ using umf_test::test;
TEST_F(test, memTargetNuma) {
auto memspace = umfMemspaceHostAllGet();
ASSERT_NE(memspace, nullptr);

umf_memtarget_type_t type;
for (size_t i = 0; i < umfMemspaceMemtargetNum(memspace); i++) {
auto hTarget = umfMemspaceMemtargetGet(memspace, i);
ASSERT_NE(hTarget, nullptr);
umf_memtarget_type_t type;
auto ret = umfMemtargetGetType(hTarget, &type);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
EXPECT_EQ(type, UMF_MEMTARGET_TYPE_NUMA);
}
}

TEST_F(numaNodesTest, getCapacity) {
auto memspace = umfMemspaceHostAllGet();
ASSERT_NE(memspace, nullptr);
std::vector<size_t> capacities;
for (auto nodeId : nodeIds) {
capacities.push_back(numa_node_size64(nodeId, nullptr));
}

for (size_t i = 0; i < umfMemspaceMemtargetNum(memspace); i++) {
auto hTarget = umfMemspaceMemtargetGet(memspace, i);
ASSERT_NE(hTarget, nullptr);
size_t capacity;
auto ret = umfMemtargetGetCapacity(hTarget, &capacity);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
auto it = std::find(capacities.begin(), capacities.end(), capacity);
EXPECT_NE(it, capacities.end());
if (it != capacities.end()) {
capacities.erase(it);
}
}
ASSERT_EQ(capacities.size(), 0);
}

TEST_F(numaNodesTest, getCapacityInvalid) {
auto memspace = umfMemspaceHostAllGet();
ASSERT_NE(memspace, nullptr);
size_t capacity;
auto ret = umfMemtargetGetCapacity(NULL, &capacity);
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
ret = umfMemtargetGetCapacity(NULL, NULL);
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
auto hTarget = umfMemspaceMemtargetGet(memspace, 0);
ASSERT_NE(hTarget, nullptr);
ret = umfMemtargetGetCapacity(hTarget, NULL);
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}

TEST_F(test, memTargetInvalid) {
auto memspace = umfMemspaceHostAllGet();
ASSERT_NE(memspace, nullptr);
Expand Down

0 comments on commit 4f57234

Please # to comment.