Skip to content

Commit

Permalink
Merge pull request #741 from kswiecicki/opt-pool-tracking-2
Browse files Browse the repository at this point in the history
[umf] Make pool tracking optional
  • Loading branch information
pbalcer committed Jul 26, 2023
2 parents 2ffcda3 + ff705f5 commit 6ae608c
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 125 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
build_type: [Debug, Release]
compiler: [{c: gcc, cxx: g++}]
libbacktrace: ['-DVAL_USE_LIBBACKTRACE_BACKTRACE=OFF']
pool_tracking: ['-DUMF_ENABLE_POOL_TRACKING=ON', '-DUMF_ENABLE_POOL_TRACKING=OFF']
include:
- os: 'ubuntu-22.04'
build_type: Release
Expand Down Expand Up @@ -53,9 +54,9 @@ jobs:
cd libbacktrace
./configure
make
sudo make install
sudo make install
cd ..
- name: Download DPC++
run: |
sudo apt install libncurses5
Expand All @@ -75,6 +76,7 @@ jobs:
-DUR_FORMAT_CPP_STYLE=ON
-DUR_DPCXX=${{github.workspace}}/dpcpp_compiler/bin/clang++
${{matrix.libbacktrace}}
${{matrix.pool_tracking}}
- name: Generate source from spec, check for uncommitted diff
if: matrix.os == 'ubuntu-22.04'
Expand Down Expand Up @@ -153,5 +155,6 @@ jobs:
-DCMAKE_BUILD_TYPE=Release
-DUR_BUILD_TESTS=ON
-DUR_FORMAT_CPP_STYLE=ON
-DUMF_ENABLE_POOL_TRACKING=ON
- name: Build
run: cmake --build ${{github.workspace}}/build -j $(nproc)
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: pip install -r third_party/requirements.txt

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DUR_DEVELOPER_MODE=ON -DUR_BUILD_TESTS=ON -DUR_ENABLE_TRACING=ON -DUR_BUILD_TOOLS=ON
run: cmake -B ${{github.workspace}}/build -DUR_DEVELOPER_MODE=ON -DUR_BUILD_TESTS=ON -DUR_ENABLE_TRACING=ON -DUR_BUILD_TOOLS=ON -DUMF_ENABLE_POOL_TRACKING=ON

- name: Build
run: cmake --build ${{github.workspace}}/build -j $(nproc)
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
run: python3 -m pip install -r third_party/requirements.txt

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_POLICY_DEFAULT_CMP0094=NEW -DUR_DEVELOPER_MODE=ON -DUR_BUILD_TESTS=ON -DUR_ENABLE_TRACING=ON -DUR_BUILD_TOOLS=ON
run: cmake -B ${{github.workspace}}/build -DCMAKE_POLICY_DEFAULT_CMP0094=NEW -DUR_DEVELOPER_MODE=ON -DUR_BUILD_TESTS=ON -DUR_ENABLE_TRACING=ON -DUR_BUILD_TOOLS=ON -DUMF_ENABLE_POOL_TRACKING=ON

- name: Build
run: cmake --build ${{github.workspace}}/build -j $(nproc) --config Release
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/coverity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
linux:
name: Coverity
runs-on: ubuntu-latest

steps:
- name: Clone the git repo
uses: actions/checkout@v3
Expand All @@ -35,12 +35,12 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y doxygen
- name: Install pip packages
run: pip install -r third_party/requirements.txt

- name: Configure CMake
run: cmake -B $WORKDIR/build -DUR_ENABLE_TRACING=ON -DUR_DEVELOPER_MODE=ON -DUR_BUILD_TESTS=ON -DUR_FORMAT_CPP_STYLE=ON
run: cmake -B $WORKDIR/build -DUR_ENABLE_TRACING=ON -DUR_DEVELOPER_MODE=ON -DUR_BUILD_TESTS=ON -DUR_FORMAT_CPP_STYLE=ON -DUMF_ENABLE_POOL_TRACKING=ON

- name: Generate source from spec, check for uncommitted diff
run: |
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" OFF)
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace for linux" OFF)
option(UR_BUILD_TOOLS "build ur tools" ON)
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" OFF)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
Expand Down
6 changes: 6 additions & 0 deletions source/common/unified_malloc_framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ else()
${UMF_SOURCES})
endif()

if (UMF_ENABLE_POOL_TRACKING)
target_sources(unified_malloc_framework PRIVATE src/memory_pool_tracking.c)
else()
target_sources(unified_malloc_framework PRIVATE src/memory_pool_default.c)
endif()

add_library(${PROJECT_NAME}::unified_malloc_framework ALIAS unified_malloc_framework)

target_include_directories(unified_malloc_framework PUBLIC include)
118 changes: 1 addition & 117 deletions source/common/unified_malloc_framework/src/memory_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,14 @@
*
*/

#include "memory_provider_internal.h"
#include "memory_tracker.h"
#include "memory_pool_internal.h"

#include <umf/memory_pool.h>
#include <umf/memory_pool_ops.h>

#include <assert.h>
#include <stdlib.h>

struct umf_memory_pool_t {
void *pool_priv;
struct umf_memory_pool_ops_t ops;

// Holds array of memory providers. All providers are wrapped
// by memory tracking providers (owned and released by UMF).
umf_memory_provider_handle_t *providers;

size_t numProviders;
};

static void
destroyMemoryProviderWrappers(umf_memory_provider_handle_t *providers,
size_t numProviders) {
for (size_t i = 0; i < numProviders; i++) {
umfMemoryProviderDestroy(providers[i]);
}

free(providers);
}

enum umf_result_t umfPoolCreate(const struct umf_memory_pool_ops_t *ops,
umf_memory_provider_handle_t *providers,
size_t numProviders, void *params,
umf_memory_pool_handle_t *hPool) {
if (!numProviders || !providers) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

enum umf_result_t ret = UMF_RESULT_SUCCESS;
umf_memory_pool_handle_t pool = malloc(sizeof(struct umf_memory_pool_t));
if (!pool) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

assert(ops->version == UMF_VERSION_CURRENT);

pool->providers =
calloc(numProviders, sizeof(umf_memory_provider_handle_t));
if (!pool->providers) {
ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
goto err_providers_alloc;
}

size_t providerInd = 0;
pool->numProviders = numProviders;

// Wrap each provider with memory tracking provider.
for (providerInd = 0; providerInd < numProviders; providerInd++) {
ret = umfTrackingMemoryProviderCreate(providers[providerInd], pool,
&pool->providers[providerInd]);
if (ret != UMF_RESULT_SUCCESS) {
goto err_providers_init;
}
}

pool->ops = *ops;
ret = ops->initialize(pool->providers, pool->numProviders, params,
&pool->pool_priv);
if (ret != UMF_RESULT_SUCCESS) {
goto err_pool_init;
}

*hPool = pool;
return UMF_RESULT_SUCCESS;

err_pool_init:
err_providers_init:
destroyMemoryProviderWrappers(pool->providers, providerInd);
err_providers_alloc:
free(pool);

return ret;
}

void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
hPool->ops.finalize(hPool->pool_priv);
destroyMemoryProviderWrappers(hPool->providers, hPool->numProviders);
free(hPool);
}

void *umfPoolMalloc(umf_memory_pool_handle_t hPool, size_t size) {
return hPool->ops.malloc(hPool->pool_priv, size);
}
Expand All @@ -123,41 +41,7 @@ enum umf_result_t umfPoolFree(umf_memory_pool_handle_t hPool, void *ptr) {
return hPool->ops.free(hPool->pool_priv, ptr);
}

enum umf_result_t umfFree(void *ptr) {
umf_memory_pool_handle_t hPool = umfPoolByPtr(ptr);
if (hPool) {
return umfPoolFree(hPool, ptr);
}
return UMF_RESULT_SUCCESS;
}

enum umf_result_t
umfPoolGetLastAllocationError(umf_memory_pool_handle_t hPool) {
return hPool->ops.get_last_allocation_error(hPool->pool_priv);
}

umf_memory_pool_handle_t umfPoolByPtr(const void *ptr) {
return umfMemoryTrackerGetPool(umfMemoryTrackerGet(), ptr);
}

enum umf_result_t
umfPoolGetMemoryProviders(umf_memory_pool_handle_t hPool, size_t numProviders,
umf_memory_provider_handle_t *hProviders,
size_t *numProvidersRet) {
if (hProviders && numProviders < hPool->numProviders) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if (numProvidersRet) {
*numProvidersRet = hPool->numProviders;
}

if (hProviders) {
for (size_t i = 0; i < hPool->numProviders; i++) {
umfTrackingMemoryProviderGetUpstreamProvider(
umfMemoryProviderGetPriv(hPool->providers[i]), hProviders + i);
}
}

return UMF_RESULT_SUCCESS;
}
93 changes: 93 additions & 0 deletions source/common/unified_malloc_framework/src/memory_pool_default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
*
* Copyright (C) 2023 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#include "memory_pool_internal.h"

#include <umf/memory_pool.h>

#include <assert.h>
#include <stdlib.h>

enum umf_result_t umfPoolCreate(const struct umf_memory_pool_ops_t *ops,
umf_memory_provider_handle_t *providers,
size_t numProviders, void *params,
umf_memory_pool_handle_t *hPool) {
if (!numProviders || !providers) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

enum umf_result_t ret = UMF_RESULT_SUCCESS;
umf_memory_pool_handle_t pool = malloc(sizeof(struct umf_memory_pool_t));
if (!pool) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

assert(ops->version == UMF_VERSION_CURRENT);

pool->providers =
calloc(numProviders, sizeof(umf_memory_provider_handle_t));
if (!pool->providers) {
ret = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
goto err_providers_alloc;
}

size_t providerInd = 0;
pool->numProviders = numProviders;

for (providerInd = 0; providerInd < numProviders; providerInd++) {
pool->providers[providerInd] = providers[providerInd];
}

pool->ops = *ops;
ret = ops->initialize(pool->providers, pool->numProviders, params,
&pool->pool_priv);
if (ret != UMF_RESULT_SUCCESS) {
goto err_pool_init;
}

*hPool = pool;
return UMF_RESULT_SUCCESS;

err_pool_init:
err_providers_alloc:
free(pool);

return ret;
}

void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
hPool->ops.finalize(hPool->pool_priv);
free(hPool);
}

enum umf_result_t umfFree(void *ptr) { return UMF_RESULT_ERROR_NOT_SUPPORTED; }

umf_memory_pool_handle_t umfPoolByPtr(const void *ptr) { return NULL; }

enum umf_result_t
umfPoolGetMemoryProviders(umf_memory_pool_handle_t hPool, size_t numProviders,
umf_memory_provider_handle_t *hProviders,
size_t *numProvidersRet) {
if (hProviders && numProviders < hPool->numProviders) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if (numProvidersRet) {
*numProvidersRet = hPool->numProviders;
}

if (hProviders) {
for (size_t i = 0; i < hPool->numProviders; i++) {
hProviders[i] = hPool->providers[i];
}
}

return UMF_RESULT_SUCCESS;
}
37 changes: 37 additions & 0 deletions source/common/unified_malloc_framework/src/memory_pool_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* Copyright (C) 2023 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#ifndef UMF_MEMORY_POOL_INTERNAL_H
#define UMF_MEMORY_POOL_INTERNAL_H 1

#include <umf/base.h>
#include <umf/memory_pool_ops.h>
#include <umf/memory_provider.h>

#ifdef __cplusplus
extern "C" {
#endif

struct umf_memory_pool_t {
void *pool_priv;
struct umf_memory_pool_ops_t ops;

// Holds array of memory providers. All providers are wrapped
// by memory tracking providers (owned and released by UMF).
umf_memory_provider_handle_t *providers;

size_t numProviders;
};

#ifdef __cplusplus
}
#endif

#endif /* UMF_MEMORY_POOL_INTERNAL_H */
Loading

0 comments on commit 6ae608c

Please sign in to comment.