Skip to content

Commit

Permalink
[OpenMP] Implements __kmp_is_address_mapped for Solaris/Illumos. (#82930
Browse files Browse the repository at this point in the history
)

Also fixing OpenMP build itself for this platform.
  • Loading branch information
devnexen authored Mar 8, 2024
1 parent 08ddd2c commit 05280b5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
5 changes: 4 additions & 1 deletion openmp/runtime/cmake/LibompHandleFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ function(libomp_get_libflags libflags)
if (${CMAKE_SYSTEM_NAME} STREQUAL "DragonFly")
libomp_append(libflags_local "-lkvm")
endif()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux|NetBSD|Solaris")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux|NetBSD|SunOS")
libomp_append(libflags_local -lm)
if (${CMAKE_SYSTEM_NAME} STREQUAL "SunOS")
libomp_append(libflags_local "-lproc")
endif()
endif()
set(libflags_local ${libflags_local} ${LIBOMP_LIBFLAGS})
libomp_setup_flags(libflags_local)
Expand Down
72 changes: 68 additions & 4 deletions openmp/runtime/src/z_Linux_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
#include <sys/types.h>
#include <sys/sysctl.h>
#elif KMP_OS_SOLARIS
#include <libproc.h>
#include <procfs.h>
#include <thread.h>
#include <sys/loadavg.h>
#endif

Expand Down Expand Up @@ -418,15 +421,14 @@ void __kmp_terminate_thread(int gtid) {
KMP_YIELD(TRUE);
} //

/* Set thread stack info according to values returned by pthread_getattr_np().
/* Set thread stack info.
If values are unreasonable, assume call failed and use incremental stack
refinement method instead. Returns TRUE if the stack parameters could be
determined exactly, FALSE if incremental refinement is necessary. */
static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
int stack_data;
#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
KMP_OS_HURD || KMP_OS_SOLARIS || KMP_OS_AIX
pthread_attr_t attr;
int status;
size_t size = 0;
void *addr = 0;
Expand All @@ -436,6 +438,19 @@ static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
pthread_attr_getstack may cause thread gtid aliasing */
if (!KMP_UBER_GTID(gtid)) {

#if KMP_OS_SOLARIS
stack_t s;
if ((status = thr_stksegment(&s)) < 0) {
KMP_CHECK_SYSFAIL("thr_stksegment", status);
}

addr = s.ss_sp;
size = s.ss_size;
KA_TRACE(60, ("__kmp_set_stack_info: T#%d thr_stksegment returned size:"
" %lu, low addr: %p\n",
gtid, size, addr));
#else
pthread_attr_t attr;
/* Fetch the real thread attributes */
status = pthread_attr_init(&attr);
KMP_CHECK_SYSFAIL("pthread_attr_init", status);
Expand All @@ -454,6 +469,7 @@ static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
gtid, size, addr));
status = pthread_attr_destroy(&attr);
KMP_CHECK_SYSFAIL("pthread_attr_destroy", status);
#endif
}

if (size != 0 && addr != 0) { // was stack parameter determination successful?
Expand Down Expand Up @@ -2175,6 +2191,54 @@ int __kmp_is_address_mapped(void *addr) {
}

kvm_close(fd);
#elif KMP_OS_SOLARIS
prmap_t *cur, *map;
void *buf;
uintptr_t uaddr;
ssize_t rd;
int err;
int file;

pid_t pid = getpid();
struct ps_prochandle *fd = Pgrab(pid, PGRAB_RDONLY, &err);
;

if (!fd) {
return 0;
}

char *name = __kmp_str_format("/proc/%d/map", pid);
size_t sz = (1 << 20);
file = open(name, O_RDONLY);
if (file == -1) {
KMP_INTERNAL_FREE(name);
return 0;
}

buf = kmpc_malloc(sz);

while (sz > 0 && (rd = pread(file, buf, sz, 0)) == sz) {
void *newbuf;
sz <<= 1;
newbuf = kmpc_realloc(buf, sz);
buf = newbuf;
}

map = reinterpret_cast<prmap_t *>(buf);
uaddr = reinterpret_cast<uintptr_t>(addr);

for (cur = map; rd > 0; cur++, rd = -sizeof(*map)) {
if ((uaddr >= cur->pr_vaddr) && (uaddr < cur->pr_vaddr)) {
if ((cur->pr_mflags & MA_READ) != 0 && (cur->pr_mflags & MA_WRITE) != 0) {
found = 1;
break;
}
}
}

kmpc_free(map);
close(file);
KMP_INTERNAL_FREE(name);
#elif KMP_OS_DARWIN

/* On OS X*, /proc pseudo filesystem is not available. Try to read memory
Expand Down Expand Up @@ -2253,9 +2317,9 @@ int __kmp_is_address_mapped(void *addr) {
}
#elif KMP_OS_WASI
found = (int)addr < (__builtin_wasm_memory_size(0) * PAGESIZE);
#elif KMP_OS_SOLARIS || KMP_OS_AIX
#elif KMP_OS_AIX

// FIXME(Solaris, AIX): Implement this
// FIXME(AIX): Implement this
found = 1;

#else
Expand Down

0 comments on commit 05280b5

Please sign in to comment.