Skip to content

Commit

Permalink
Split ABI checks for rocprofiler-sdk-roctx into separate file (#21)
Browse files Browse the repository at this point in the history
* Split ABI checks for rocprofiler-sdk-roctx into separate file

* Update source/lib/rocprofiler-sdk-roctx/abi.cpp

Co-authored-by: Indic, Vladimir <[email protected]>

* Update source/lib/rocprofiler-sdk-roctx/abi.cpp

Co-authored-by: Indic, Vladimir <[email protected]>

* New line

---------

Co-authored-by: Jonathan R. Madsen <[email protected]>
Co-authored-by: Indic, Vladimir <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent 720763d commit 1b7ab08
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 80 deletions.
2 changes: 1 addition & 1 deletion source/lib/rocprofiler-sdk-roctx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_library(rocprofiler-sdk::rocprofiler-sdk-roctx-shared-library ALIAS
add_library(rocprofiler-sdk-roctx::rocprofiler-sdk-roctx-shared-library ALIAS
rocprofiler-sdk-roctx-shared-library)

target_sources(rocprofiler-sdk-roctx-shared-library PRIVATE roctx.cpp)
target_sources(rocprofiler-sdk-roctx-shared-library PRIVATE abi.cpp roctx.cpp)
target_include_directories(
rocprofiler-sdk-roctx-shared-library
INTERFACE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/source/include>
Expand Down
58 changes: 58 additions & 0 deletions source/lib/rocprofiler-sdk-roctx/abi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// MIT License
//
// Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "lib/common/abi.hpp"
#include "lib/common/defines.hpp"

#include <rocprofiler-sdk-roctx/api_trace.h>
#include <rocprofiler-sdk-roctx/defines.h>
#include <rocprofiler-sdk-roctx/roctx.h>
#include <rocprofiler-sdk-roctx/types.h>
#include <rocprofiler-sdk/version.h>

namespace rocprofiler
{
namespace roctx
{
// Reminder to update step version if updated
ROCP_SDK_ENFORCE_ABI_VERSIONING(::roctxCoreApiTable_t, 6)
ROCP_SDK_ENFORCE_ABI_VERSIONING(::roctxControlApiTable_t, 2)
ROCP_SDK_ENFORCE_ABI_VERSIONING(::roctxNameApiTable_t, 4)

// These ensure that function pointers are not re-ordered
// core
ROCP_SDK_ENFORCE_ABI(::roctxCoreApiTable_t, roctxMarkA_fn, 0);
ROCP_SDK_ENFORCE_ABI(::roctxCoreApiTable_t, roctxRangePushA_fn, 1);
ROCP_SDK_ENFORCE_ABI(::roctxCoreApiTable_t, roctxRangePop_fn, 2);
ROCP_SDK_ENFORCE_ABI(::roctxCoreApiTable_t, roctxRangeStartA_fn, 3);
ROCP_SDK_ENFORCE_ABI(::roctxCoreApiTable_t, roctxRangeStop_fn, 4);
ROCP_SDK_ENFORCE_ABI(::roctxCoreApiTable_t, roctxGetThreadId_fn, 5);
// control
ROCP_SDK_ENFORCE_ABI(::roctxControlApiTable_t, roctxProfilerPause_fn, 0);
ROCP_SDK_ENFORCE_ABI(::roctxControlApiTable_t, roctxProfilerResume_fn, 1);
// name
ROCP_SDK_ENFORCE_ABI(::roctxNameApiTable_t, roctxNameOsThread_fn, 0);
ROCP_SDK_ENFORCE_ABI(::roctxNameApiTable_t, roctxNameHsaAgent_fn, 1);
ROCP_SDK_ENFORCE_ABI(::roctxNameApiTable_t, roctxNameHipDevice_fn, 2);
ROCP_SDK_ENFORCE_ABI(::roctxNameApiTable_t, roctxNameHipStream_fn, 3);
} // namespace roctx
} // namespace rocprofiler
115 changes: 36 additions & 79 deletions source/lib/rocprofiler-sdk-roctx/roctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,15 @@

ROCPROFILER_REGISTER_DEFINE_IMPORT(roctx, ROCP_REG_VERSION)

namespace rocprofiler
{
namespace roctx
{
namespace
{
namespace common = ::rocprofiler::common;

constexpr size_t
compute_table_offset(size_t n)
{
return (sizeof(uint64_t) + (n * sizeof(void*)));
}

constexpr size_t
compute_table_size(size_t nmembers)
{
return (sizeof(uint64_t) + (nmembers * sizeof(void*)));
}

#define ROCTX_ASSERT_OFFSET(TABLE, MEMBER, IDX) \
static_assert(offsetof(TABLE, MEMBER) == compute_table_offset(IDX), \
"Do not re-arrange the table members")

// core
ROCTX_ASSERT_OFFSET(roctxCoreApiTable_t, roctxMarkA_fn, 0);
ROCTX_ASSERT_OFFSET(roctxCoreApiTable_t, roctxRangePushA_fn, 1);
ROCTX_ASSERT_OFFSET(roctxCoreApiTable_t, roctxRangePop_fn, 2);
ROCTX_ASSERT_OFFSET(roctxCoreApiTable_t, roctxRangeStartA_fn, 3);
ROCTX_ASSERT_OFFSET(roctxCoreApiTable_t, roctxRangeStop_fn, 4);
ROCTX_ASSERT_OFFSET(roctxCoreApiTable_t, roctxGetThreadId_fn, 5);
// control
ROCTX_ASSERT_OFFSET(roctxControlApiTable_t, roctxProfilerPause_fn, 0);
ROCTX_ASSERT_OFFSET(roctxControlApiTable_t, roctxProfilerResume_fn, 1);
// name
ROCTX_ASSERT_OFFSET(roctxNameApiTable_t, roctxNameOsThread_fn, 0);
ROCTX_ASSERT_OFFSET(roctxNameApiTable_t, roctxNameHsaAgent_fn, 1);
ROCTX_ASSERT_OFFSET(roctxNameApiTable_t, roctxNameHipDevice_fn, 2);
ROCTX_ASSERT_OFFSET(roctxNameApiTable_t, roctxNameHipStream_fn, 3);

#undef ROCTX_ASSERT_OFFSET

static_assert(
sizeof(roctxCoreApiTable_t) == compute_table_size(6),
"Update table major/step version and add a new offset assertion if this fails to compile");

static_assert(
sizeof(roctxControlApiTable_t) == compute_table_size(2),
"Update table major/step version and add a new offset assertion if this fails to compile");

static_assert(
sizeof(roctxNameApiTable_t) == compute_table_size(4),
"Update table major/step version and add a new offset assertion if this fails to compile");

using ::rocprofiler::common::static_buffer_size;
using common::static_buffer_size;
using atomic_roctx_range_id_t = std::atomic<roctx_range_id_t>;

constexpr auto atomic_range_id_size_v = static_buffer_size<atomic_roctx_range_id_t>();
Expand All @@ -117,16 +73,16 @@ get_start_stop_range_id()
int
roctxGetThreadId(roctx_thread_id_t* tid)
{
*tid = rocprofiler::common::get_tid();
*tid = common::get_tid();
return 0;
}

void
roctxMarkA(const char*)
roctxMarkA(const char* /*msg*/)
{}

int
roctxRangePushA(const char*)
roctxRangePushA(const char* /*msg*/)
{
return get_nested_range_level()++;
}
Expand All @@ -145,32 +101,32 @@ roctxRangeStartA(const char*)
return range_id;
}

void roctxRangeStop(roctx_range_id_t) {}
void roctxRangeStop(roctx_range_id_t /*id*/) {}

int roctxProfilerPause(roctx_thread_id_t /*tid*/) { return 0; }

int roctxProfilerResume(roctx_thread_id_t /*tid*/) { return 0; }

int
roctxNameOsThread(const char*)
roctxNameOsThread(const char* /*name*/)
{
return 0;
}

int
roctxNameHsaAgent(const char*, const struct hsa_agent_s*)
roctxNameHsaAgent(const char* /*name*/, const struct hsa_agent_s* /*agent*/)
{
return 0;
}

int
roctxNameHipDevice(const char*, int)
roctxNameHipDevice(const char* /*name*/, int /*devid*/)
{
return 0;
}

int
roctxNameHipStream(const char*, const struct ihipStream_t*)
roctxNameHipStream(const char* /*name*/, const struct ihipStream_t* /*stream*/)
{
return 0;
}
Expand All @@ -190,22 +146,22 @@ get_table_impl()
auto*& tbl = rocprofiler::common::static_object<roctx_api_table>::construct();

tbl->core = roctxCoreApiTable_t{sizeof(roctxCoreApiTable_t),
&::roctx::roctxMarkA,
&::roctx::roctxRangePushA,
&::roctx::roctxRangePop,
&::roctx::roctxRangeStartA,
&::roctx::roctxRangeStop,
&::roctx::roctxGetThreadId};
&::rocprofiler::roctx::roctxMarkA,
&::rocprofiler::roctx::roctxRangePushA,
&::rocprofiler::roctx::roctxRangePop,
&::rocprofiler::roctx::roctxRangeStartA,
&::rocprofiler::roctx::roctxRangeStop,
&::rocprofiler::roctx::roctxGetThreadId};

tbl->control = roctxControlApiTable_t{sizeof(roctxControlApiTable_t),
&::roctx::roctxProfilerPause,
&::roctx::roctxProfilerResume};
&::rocprofiler::roctx::roctxProfilerPause,
&::rocprofiler::roctx::roctxProfilerResume};

tbl->name = roctxNameApiTable_t{sizeof(roctxNameApiTable_t),
&::roctx::roctxNameOsThread,
&::roctx::roctxNameHsaAgent,
&::roctx::roctxNameHipDevice,
&::roctx::roctxNameHipStream};
&::rocprofiler::roctx::roctxNameOsThread,
&::rocprofiler::roctx::roctxNameHsaAgent,
&::rocprofiler::roctx::roctxNameHipDevice,
&::rocprofiler::roctx::roctxNameHipStream};

auto table_array = std::array<void*, 3>{&tbl->core, &tbl->control, &tbl->name};
auto lib_id = rocprofiler_register_library_indentifier_t{};
Expand Down Expand Up @@ -236,79 +192,80 @@ get_table()
}
} // namespace
} // namespace roctx
} // namespace rocprofiler

ROCTX_EXTERN_C_INIT

void
roctxMarkA(const char* message)
{
::roctx::get_table()->core.roctxMarkA_fn(message);
::rocprofiler::roctx::get_table()->core.roctxMarkA_fn(message);
}

int
roctxRangePushA(const char* message)
{
return ::roctx::get_table()->core.roctxRangePushA_fn(message);
return ::rocprofiler::roctx::get_table()->core.roctxRangePushA_fn(message);
}

int
roctxRangePop()
{
return ::roctx::get_table()->core.roctxRangePop_fn();
return ::rocprofiler::roctx::get_table()->core.roctxRangePop_fn();
}

roctx_range_id_t
roctxRangeStartA(const char* message)
{
return ::roctx::get_table()->core.roctxRangeStartA_fn(message);
return ::rocprofiler::roctx::get_table()->core.roctxRangeStartA_fn(message);
}

void
roctxRangeStop(roctx_range_id_t id)
{
return ::roctx::get_table()->core.roctxRangeStop_fn(id);
return ::rocprofiler::roctx::get_table()->core.roctxRangeStop_fn(id);
}

int
roctxGetThreadId(roctx_thread_id_t* tid)
{
return ::roctx::get_table()->core.roctxGetThreadId_fn(tid);
return ::rocprofiler::roctx::get_table()->core.roctxGetThreadId_fn(tid);
}

int
roctxProfilerPause(roctx_thread_id_t tid)
{
return ::roctx::get_table()->control.roctxProfilerPause_fn(tid);
return ::rocprofiler::roctx::get_table()->control.roctxProfilerPause_fn(tid);
}

int
roctxProfilerResume(roctx_thread_id_t tid)
{
return ::roctx::get_table()->control.roctxProfilerResume_fn(tid);
return ::rocprofiler::roctx::get_table()->control.roctxProfilerResume_fn(tid);
}

int
roctxNameOsThread(const char* name)
{
return ::roctx::get_table()->name.roctxNameOsThread_fn(name);
return ::rocprofiler::roctx::get_table()->name.roctxNameOsThread_fn(name);
}

int
roctxNameHsaAgent(const char* name, const struct hsa_agent_s* agent)
{
return ::roctx::get_table()->name.roctxNameHsaAgent_fn(name, agent);
return ::rocprofiler::roctx::get_table()->name.roctxNameHsaAgent_fn(name, agent);
}

int
roctxNameHipDevice(const char* name, int device_id)
{
return ::roctx::get_table()->name.roctxNameHipDevice_fn(name, device_id);
return ::rocprofiler::roctx::get_table()->name.roctxNameHipDevice_fn(name, device_id);
}

int
roctxNameHipStream(const char* name, const struct ihipStream_t* stream)
{
return ::roctx::get_table()->name.roctxNameHipStream_fn(name, stream);
return ::rocprofiler::roctx::get_table()->name.roctxNameHipStream_fn(name, stream);
}

ROCTX_EXTERN_C_FINI

0 comments on commit 1b7ab08

Please sign in to comment.