Skip to content

Commit

Permalink
Alpine compatibility for pony
Browse files Browse the repository at this point in the history
Changes related to getting pony compiling and running on Alpine
Linux Edge.

* Define `_GNU_SOURCE` so musl glibc compatibility is enabled.
* Makefile changes to detect Alpine Linux and link in `libexecinfo`.
* Makefile changes to define PONY_NO_ASSERT for libponyrt-pic.
* Changes to stop using glibc specific `__GNUC_PREREQ` macro.
* Changes to move Linux cpu affinity setting outside of thread
  creation function and into same set affinity function as for
  other platforms. This also includes enabling asio thread affinity
  setting for non-linux platforms.
* Rename `SCHED_BATCH` to `PONY_SCHED_BATCH` to avoid conflict with
  musl.
* Set `this_scheduler` thread-local variable to `NULL` on
  `ponyint_sched_init` to avoid segfault in running codegen test
  suite.
* Change `#if` for `pony_assert` to use normal `assert`
  for builds that define `PONY_NO_ASSERT` (`libponyrt` and
  `libponyrt-pic` only as of this commit) instead of the custom
  `pony_assert_fail` that prints backtraces.

Resolves #1729
  • Loading branch information
dipinhora authored and SeanTAllen committed Jul 20, 2017
1 parent 405394d commit 1e1c484
Showing 13 changed files with 56 additions and 9 deletions.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ else
AR = gcc-ar
endif
endif

ALPINE=$(wildcard /etc/alpine-release)
endif

ifeq ($(UNAME_S),Darwin)
@@ -396,9 +398,14 @@ endif

# target specific build options
libponyrt.buildoptions = -DPONY_NO_ASSERT
libponyrt-pic.buildoptions = -DPONY_NO_ASSERT

libponyrt.tests.linkoptions += -rdynamic

ifneq ($(ALPINE),)
libponyrt.tests.linkoptions += -lexecinfo
endif

libponyc.buildoptions = -D__STDC_CONSTANT_MACROS
libponyc.buildoptions += -D__STDC_FORMAT_MACROS
libponyc.buildoptions += -D__STDC_LIMIT_MACROS
@@ -410,16 +417,29 @@ libponyc.tests.buildoptions += -DPONY_PACKAGES_DIR=\"$(packages_abs_src)\"

libponyc.tests.linkoptions += -rdynamic

ifneq ($(ALPINE),)
libponyc.tests.linkoptions += -lexecinfo
endif

libponyc.benchmarks.buildoptions = -D__STDC_CONSTANT_MACROS
libponyc.benchmarks.buildoptions += -D__STDC_FORMAT_MACROS
libponyc.benchmarks.buildoptions += -D__STDC_LIMIT_MACROS

libgbenchmark.buildoptions := -DHAVE_POSIX_REGEX

ifneq ($(ALPINE),)
libponyc.benchmarks.linkoptions += -lexecinfo
libponyrt.benchmarks.linkoptions += -lexecinfo
endif

ponyc.buildoptions = $(libponyc.buildoptions)

ponyc.linkoptions += -rdynamic

ifneq ($(ALPINE),)
ponyc.linkoptions += -lexecinfo
endif

ifeq ($(OSTYPE), linux)
libponyrt-pic.buildoptions += -fpic
endif
6 changes: 6 additions & 0 deletions src/common/platform.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#ifndef PLATFORM_H
#define PLATFORM_H

#ifdef __linux__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#endif

#include <stdbool.h>
#include <stdint.h>

4 changes: 2 additions & 2 deletions src/common/pony/detail/atomics.h
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ using std::atomic_thread_fence;
# endif
#elif defined(__GNUC__) && !defined(__clang__)
# include <features.h>
# if __GNUC_PREREQ(4, 9)
# if ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((4) << 16) + (9))
# ifdef __cplusplus
// g++ doesn't like C11 atomics. We never need atomic ops in C++ files so
// we only define the atomic types.
@@ -50,7 +50,7 @@ using std::atomic_thread_fence;
# define PONY_ATOMIC(T) T _Atomic
# define PONY_ATOMIC_RVALUE(T) T _Atomic
# endif
# elif __GNUC_PREREQ(4, 7)
# elif ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((4) << 16) + (7))
# define PONY_ATOMIC(T) alignas(sizeof(T)) T
# define PONY_ATOMIC_RVALUE(T) T
# define PONY_ATOMIC_BUILTINS
5 changes: 3 additions & 2 deletions src/common/ponyassert.h
Original file line number Diff line number Diff line change
@@ -2,11 +2,12 @@
#define PLATFORM_PONYASSERT_H

#include "platform.h"
#include <assert.h>

PONY_EXTERN_C_BEGIN

#if defined(NDEBUG) && defined(PONY_NO_ASSERT)
# define pony_assert(expr) ((void)0)
#if defined(PONY_NO_ASSERT)
# define pony_assert(expr) assert(expr)
#else
# define pony_assert(expr) \
((expr) ? (void)0 : \
1 change: 1 addition & 0 deletions src/libponyc/codegen/genexe.c
Original file line number Diff line number Diff line change
@@ -261,6 +261,7 @@ static bool link_exe(compile_t* c, ast_t* program,
"Warning: environment variable $CC undefined, using %s as the linker\n",
PONY_COMPILER);
}

const char* mcx16_arg = target_is_ilp32(c->opt->triple) ? "" : "-mcx16";
const char* fuseld = target_is_linux(c->opt->triple) ? "-fuse-ld=gold" : "";
const char* ldl = target_is_linux(c->opt->triple) ? "-ldl" : "";
5 changes: 5 additions & 0 deletions src/libponyrt/asio/asio.c
Original file line number Diff line number Diff line change
@@ -33,6 +33,11 @@ asio_backend_t* ponyint_asio_get_backend()
return running_base.backend;
}

uint32_t ponyint_asio_get_cpu()
{
return asio_cpu;
}

void ponyint_asio_init(uint32_t cpu)
{
asio_cpu = cpu;
5 changes: 5 additions & 0 deletions src/libponyrt/asio/asio.h
Original file line number Diff line number Diff line change
@@ -68,6 +68,11 @@ bool ponyint_asio_start();
*/
asio_backend_t* ponyint_asio_get_backend();

/** Returns the cpu assigned for the ASIO thread.
*
*/
uint32_t ponyint_asio_get_cpu();

/** Attempts to stop an asynchronous event mechanism.
*
* Stopping an event mechanism is only possible if there are no pending "noisy"
2 changes: 2 additions & 0 deletions src/libponyrt/asio/epoll.c
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

#include "../actor/messageq.h"
#include "../mem/pool.h"
#include "../sched/cpu.h"
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
@@ -163,6 +164,7 @@ PONY_API void pony_asio_event_resubscribe_read(asio_event_t* ev)

DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)
{
ponyint_cpu_affinity(ponyint_asio_get_cpu());
pony_register_thread();
asio_backend_t* b = arg;

2 changes: 2 additions & 0 deletions src/libponyrt/asio/iocp.c
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

#include "../actor/messageq.h"
#include "../mem/pool.h"
#include "../sched/cpu.h"
#include <string.h>
#include <signal.h>
#include <stdbool.h>
@@ -101,6 +102,7 @@ void ponyint_asio_backend_final(asio_backend_t* b)

DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)
{
ponyint_cpu_affinity(ponyint_asio_get_cpu());
pony_register_thread();
asio_backend_t* b = (asio_backend_t*)arg;
asio_event_t* stdin_event = NULL;
2 changes: 2 additions & 0 deletions src/libponyrt/asio/kqueue.c
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

#include "../actor/messageq.h"
#include "../mem/pool.h"
#include "../sched/cpu.h"
#include "ponyassert.h"
#include <sys/event.h>
#include <string.h>
@@ -120,6 +121,7 @@ PONY_API void pony_asio_event_resubscribe_write(asio_event_t* ev)

DECLARE_THREAD_FN(ponyint_asio_backend_dispatch)
{
ponyint_cpu_affinity(ponyint_asio_get_cpu());
pony_register_thread();
asio_backend_t* b = arg;
struct kevent fired[MAX_EVENTS];
2 changes: 0 additions & 2 deletions src/libponyrt/platform/threads.c
Original file line number Diff line number Diff line change
@@ -176,8 +176,6 @@ bool ponyint_thread_create(pony_thread_id_t* thread, thread_fn start,
CPU_ZERO(&set);
CPU_SET(cpu, &set);

pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &set);

if(use_numa)
{
struct rlimit limit;
6 changes: 5 additions & 1 deletion src/libponyrt/sched/cpu.c
Original file line number Diff line number Diff line change
@@ -256,7 +256,11 @@ void ponyint_cpu_affinity(uint32_t cpu)
return;

#if defined(PLATFORM_IS_LINUX)
// Affinity is handled when spawning the thread.
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);

sched_setaffinity(0, sizeof(cpu_set_t), &set);
#elif defined(PLATFORM_IS_FREEBSD)
// No pinning, since we cannot yet determine hyperthreads vs physical cores.
#elif defined(PLATFORM_IS_MACOSX)
5 changes: 3 additions & 2 deletions src/libponyrt/sched/scheduler.c
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
#include <string.h>
#include <stdio.h>

#define SCHED_BATCH 100
#define PONY_SCHED_BATCH 100

static DECLARE_THREAD_FN(run_thread);

@@ -284,7 +284,7 @@ static void run(scheduler_t* sched)
}

// Run the current actor and get the next actor.
bool reschedule = ponyint_actor_run(&sched->ctx, actor, SCHED_BATCH);
bool reschedule = ponyint_actor_run(&sched->ctx, actor, PONY_SCHED_BATCH);
pony_actor_t* next = pop_global(sched);

if(reschedule)
@@ -370,6 +370,7 @@ static void ponyint_sched_shutdown()
pony_ctx_t* ponyint_sched_init(uint32_t threads, bool noyield, bool nopin,
bool pinasio)
{
this_scheduler = NULL;
pony_register_thread();
this_scheduler->ctx.current = NULL;

0 comments on commit 1e1c484

Please sign in to comment.