Skip to content

Commit

Permalink
Implementation of OPMD callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
cvonelm committed Oct 8, 2024
1 parent b044d65 commit 6452986
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ find_package(Veosinfo)
find_package(Libpfm)
find_package(PkgConfig)

find_package(OpenMP REQUIRED)

if(PkgConfig_FOUND)
pkg_check_modules(Audit audit)
pkg_check_modules(Radare IMPORTED_TARGET r_main>=5.8.0)
Expand Down Expand Up @@ -198,6 +200,7 @@ set(SOURCE_FILES
src/perf/util.cpp
src/syscalls.cpp
src/summary.cpp
src/ompd/callbacks.cpp
)

# define lo2s target
Expand All @@ -215,6 +218,7 @@ target_link_libraries(lo2s
Binutils::Binutils
fmt::fmt
std::filesystem
OpenMP::OpenMP_CXX
)

# old glibc versions require -lrt for clock_gettime()
Expand Down
1 change: 1 addition & 0 deletions include/lo2s/bfd_resolve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Lib

LineInfo lookup(Address addr) const;

Address reverse_lookup(const char* symbol_name) const;

Check failure on line 144 in include/lo2s/bfd_resolve.hpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

include/lo2s/bfd_resolve.hpp#L144

+
const std::string& name() const
{
return name_;
Expand Down
14 changes: 14 additions & 0 deletions src/bfd_resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ Lib::Lib(const std::string& name)
read_sections();
}

Address Lib::reverse_lookup(const char* symbol_name) const
{
for (const auto& sym : symbols_)
{
if (sym == nullptr)
{
continue;
}
if (strcmp(sym->name, symbol_name) == 0)
{
}
}
}

LineInfo Lib::lookup(Address addr) const
{
const char* file = nullptr;
Expand Down
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

#include <system_error>

extern "C"
{

void call_ompd_initialize();
}

Check failure on line 35 in src/main.cpp

View check run for this annotation

Code Style Turtle / Code Formatting Test

src/main.cpp#L35

+
int main(int argc, const char** argv)
{
// The resource limit for file descriptors (which lo2s uses a lot of, especially in
Expand All @@ -38,6 +43,7 @@ int main(int argc, const char** argv)
lo2s::initial_rlimit_fd();
lo2s::bump_rlimit_fd();

call_ompd_initialize();
try
{
lo2s::parse_program_options(argc, argv);
Expand Down
196 changes: 196 additions & 0 deletions src/ompd/callbacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@

#include <cstring>
#include <fmt/core.h>
#include <lo2s/bfd_resolve.hpp>
#include <map>
extern "C"
{
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <omp-tools.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
}

extern "C"
{
struct _ompd_aspace_cont
{
int id;
};

struct _ompd_thread_cont
{
int id;
};

static pid_t ompd_pid;
static std::map<pid_t, ompd_thread_context_t> ompd_threads;

ompd_address_space_context_t acontext = { 42 };
void* ompd_library = NULL;

ompd_rc_t ompd_callback_alloc(ompd_size_t nbytes, void** ptr)
{
*ptr = malloc(nbytes);
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_free(void* ptr)
{
free(ptr);
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_get_thread_context(ompd_address_space_context_t* address_space_context,
ompd_thread_id_t kind, ompd_size_t sizeof_thread_id,
const void* thread_id,
ompd_thread_context_t** thread_context)
{
if (kind != 0 && kind != 1)
{
return ompd_rc_unsupported;
}
long int tid;

tid = *(const uint64_t*)thread_id;

ompd_threads[tid] = { .id = (int)tid };

*thread_context = &ompd_threads[tid];

return ompd_rc_ok;
}

ompd_rc_t ompd_callback_sizeof(ompd_address_space_context_t* address_space_context,
ompd_device_type_sizes_t* sizes)
{

*sizes = { .sizeof_char = sizeof(char),
.sizeof_short = sizeof(short),
.sizeof_int = sizeof(int),
.sizeof_long = sizeof(long),
.sizeof_long_long = sizeof(long long),
.sizeof_pointer = sizeof(void*) };
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_symbol_addr(ompd_address_space_context_t* address_space_contxt,
ompd_thread_context_t* thread_context,
const char* symbol_name, ompd_address_t* symbol_addr,
const char* file_name)
{
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_print_string(const char* string, int category)
{
printf("%s", string);
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_sym_addr(ompd_address_space_context_t* context,
ompd_thread_context_t* tcontext, const char* symbol_name,
ompd_address_t* symbol_addr, const char* file_name)
{
symbol_addr->address = 0;
printf("%s:%s\n", symbol_name, file_name);
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_read(ompd_address_space_context_t* context,
ompd_thread_context_t* tcontext, const ompd_address_t* addr,
ompd_size_t nbytes, void* buffer)
{
uint64_t readMem = (uint64_t)addr->address;
int fd = open(fmt::format("/proc/{}/mem", ompd_pid).c_str(), 0, O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "Error: %s\n", strerror(errno));
return ompd_rc_error;
}
if (read(fd, buffer, nbytes) != nbytes)
{
fprintf(stderr, "Error: %s\n", strerror(errno));
}
close(fd);
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_read_string(ompd_address_space_context_t* context,
ompd_thread_context_t* tcontext, const ompd_address_t* addr,
ompd_size_t nbytes, void* buffer)
{
ompd_callback_read(context, tcontext, addr, nbytes, buffer);
((char*)buffer)[nbytes - 1] = '\0';
return ompd_rc_ok;
}

ompd_rc_t ompd_callback_endianess(ompd_address_space_context_t* address_space_context,
const void* input, ompd_size_t unit_size, ompd_size_t count,
void* output)
{
memmove(output, input, count * unit_size);
return ompd_rc_ok;
}

void call_ompd_initialize()
{
ompd_library = dlopen("/usr/lib/libompd.so", RTLD_LAZY);

if (ompd_library == NULL)
{
fprintf(stderr, "Could not load libompd.so!");
}

static ompd_callbacks_t table = { ompd_callback_alloc,
ompd_callback_free,
ompd_callback_print_string,
ompd_callback_sizeof,
ompd_callback_sym_addr,
ompd_callback_read,
NULL,
ompd_callback_read_string,
ompd_callback_endianess,
ompd_callback_endianess,
ompd_callback_get_thread_context };

ompd_rc_t (*my_ompd_init)(ompd_word_t version, ompd_callbacks_t*) =
(ompd_rc_t(*)(ompd_word_t, ompd_callbacks_t*))dlsym(ompd_library, "ompd_initialize");
ompd_rc_t returnInit = my_ompd_init(201811, &table);

if (returnInit != ompd_rc_ok)
{
fprintf(stderr, "Error: %d\n", returnInit);
}

ompd_rc_t (*my_proc_init)(ompd_address_space_context_t*, ompd_address_space_handle_t**) =
(ompd_rc_t(*)(ompd_address_space_context_t*, ompd_address_space_handle_t**))dlsym(
ompd_library, "ompd_process_initialize");

ompd_address_space_handle_t* addr_space = NULL;

ompd_rc_t retProcInit = my_proc_init(&acontext, &addr_space);

if (retProcInit != ompd_rc_ok)
{
fprintf(stderr, "Error (my_proc_init): %d\n", retProcInit);
}
}

ompd_rc_t call_ompd_finalize()
{
static ompd_rc_t (*my_ompd_finalize)(void) = NULL;
if (!my_ompd_finalize)
{
my_ompd_finalize = (ompd_rc_t(*)(void))dlsym(ompd_library, "ompd_finalize");
if (dlerror())
{
return ompd_rc_error;
}
}
return my_ompd_finalize();
}
}

0 comments on commit 6452986

Please sign in to comment.