2019-09-09 18:40:24 +08:00
|
|
|
/*
|
2020-01-11 18:21:43 +08:00
|
|
|
* Copyright 2017-2020 Leonid Yuriev <leo@yuriev.ru>
|
2017-03-30 23:54:57 +08:00
|
|
|
* and other libmdbx authors: please see AUTHORS file.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
|
2020-04-15 22:09:37 +08:00
|
|
|
#if !(defined(_WIN32) || defined(_WIN64))
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
#include <atomic>
|
2017-03-30 23:54:57 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-06-26 08:33:53 +08:00
|
|
|
#include <atomic>
|
|
|
|
|
2019-11-12 18:28:24 +08:00
|
|
|
#ifndef MDBX_LOCKING
|
2020-07-08 04:14:01 +08:00
|
|
|
#error "Oops, MDBX_LOCKING is undefined!"
|
2019-08-13 07:12:13 +08:00
|
|
|
#endif
|
|
|
|
|
2019-11-12 18:28:24 +08:00
|
|
|
#if defined(__APPLE__) && (MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
|
|
|
|
MDBX_LOCKING == MDBX_LOCKING_POSIX2008)
|
|
|
|
#include "darwin/pthread_barrier.c"
|
|
|
|
#endif /* __APPLE__ && MDBX_LOCKING >= MDBX_LOCKING_POSIX2001 */
|
|
|
|
|
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/sem.h>
|
|
|
|
#endif /* MDBX_LOCKING == MDBX_LOCKING_SYSV */
|
|
|
|
|
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_POSIX1988
|
|
|
|
#include <semaphore.h>
|
|
|
|
|
2019-11-07 21:17:08 +08:00
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
#include <atomic>
|
|
|
|
static __inline __maybe_unused int atomic_decrement(std::atomic_int *p) {
|
|
|
|
return std::atomic_fetch_sub(p, 1) - 1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static __inline __maybe_unused int atomic_decrement(volatile int *p) {
|
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
|
|
return __sync_sub_and_fetch(p, 1);
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
STATIC_ASSERT(sizeof(volatile long) == sizeof(volatile int));
|
|
|
|
return _InterlockedDecrement((volatile long *)p);
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
return OSAtomicDecrement32Barrier((volatile int *)p);
|
|
|
|
#else
|
|
|
|
#error FIXME: Unsupported compiler
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* C++11 */
|
2019-11-12 18:28:24 +08:00
|
|
|
#endif /* MDBX_LOCKING == MDBX_LOCKING_POSIX1988 */
|
2019-11-07 21:17:08 +08:00
|
|
|
|
2019-11-12 20:14:07 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
|
|
|
|
static int ipc;
|
|
|
|
static pid_t ipc_overlord_pid;
|
|
|
|
static void ipc_remove(void) {
|
|
|
|
if (ipc_overlord_pid == getpid())
|
|
|
|
semctl(ipc, 0, IPC_RMID, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2017-03-30 23:54:57 +08:00
|
|
|
struct shared_t {
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
|
|
|
|
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
|
2017-03-30 23:54:57 +08:00
|
|
|
pthread_barrier_t barrier;
|
|
|
|
pthread_mutex_t mutex;
|
2019-11-07 21:17:08 +08:00
|
|
|
size_t count;
|
|
|
|
pthread_cond_t events[1];
|
2019-11-12 18:28:24 +08:00
|
|
|
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
|
2019-11-07 21:17:08 +08:00
|
|
|
struct {
|
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
std::atomic_int countdown;
|
|
|
|
#else
|
|
|
|
volatile int countdown;
|
|
|
|
#endif /* C++11 */
|
|
|
|
sem_t sema;
|
|
|
|
} barrier;
|
|
|
|
size_t count;
|
|
|
|
sem_t events[1];
|
2019-11-12 18:28:24 +08:00
|
|
|
#else
|
|
|
|
#error "FIXME"
|
|
|
|
#endif /* MDBX_LOCKING */
|
2017-03-30 23:54:57 +08:00
|
|
|
};
|
|
|
|
static shared_t *shared;
|
2019-11-12 18:28:24 +08:00
|
|
|
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
|
2017-03-30 23:54:57 +08:00
|
|
|
|
|
|
|
void osal_wait4barrier(void) {
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
|
2019-11-12 20:14:07 +08:00
|
|
|
struct sembuf op;
|
|
|
|
op.sem_num = 0;
|
|
|
|
op.sem_op = -1;
|
|
|
|
op.sem_flg = IPC_NOWAIT;
|
|
|
|
if (semop(ipc, &op, 1))
|
|
|
|
failure_perror("semop(dec)", errno);
|
|
|
|
op.sem_op = 0;
|
|
|
|
op.sem_flg = 0;
|
|
|
|
if (semop(ipc, &op, 1))
|
|
|
|
failure_perror("semop(wait)", errno);
|
2019-11-12 18:28:24 +08:00
|
|
|
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
|
|
|
|
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
|
|
|
|
assert(shared != nullptr && shared != MAP_FAILED);
|
|
|
|
int err = pthread_barrier_wait(&shared->barrier);
|
|
|
|
if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
|
|
|
|
failure_perror("pthread_barrier_wait(shared)", err);
|
|
|
|
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
|
2017-03-30 23:54:57 +08:00
|
|
|
assert(shared != nullptr && shared != MAP_FAILED);
|
2019-11-12 18:28:24 +08:00
|
|
|
int err = (atomic_decrement(&shared->barrier.countdown) > 0 &&
|
|
|
|
sem_wait(&shared->barrier.sema))
|
|
|
|
? errno
|
|
|
|
: 0;
|
|
|
|
if (err != 0)
|
|
|
|
failure_perror("sem_wait(shared)", err);
|
|
|
|
if (sem_post(&shared->barrier.sema))
|
|
|
|
failure_perror("sem_post(shared)", errno);
|
2019-11-07 21:17:08 +08:00
|
|
|
#else
|
2019-11-12 18:28:24 +08:00
|
|
|
#error "FIXME"
|
|
|
|
#endif /* MDBX_LOCKING */
|
2017-03-30 23:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void osal_setup(const std::vector<actor_config> &actors) {
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
|
2019-11-12 20:14:07 +08:00
|
|
|
if (ipc_overlord_pid)
|
|
|
|
failure("ipc already created by %ld pid", (long)ipc_overlord_pid);
|
|
|
|
ipc_overlord_pid = getpid();
|
2019-11-28 07:19:36 +08:00
|
|
|
#ifndef SEM_A
|
|
|
|
#define SEM_A S_IRUSR
|
|
|
|
#endif
|
|
|
|
#ifndef SEM_R
|
|
|
|
#define SEM_R S_IWUSR
|
|
|
|
#endif
|
2019-11-12 20:14:07 +08:00
|
|
|
ipc = semget(IPC_PRIVATE, actors.size() + 2, IPC_CREAT | SEM_A | SEM_R);
|
|
|
|
if (ipc < 0)
|
|
|
|
failure_perror("semget(IPC_PRIVATE, shared_sems)", errno);
|
|
|
|
if (atexit(ipc_remove))
|
|
|
|
failure_perror("atexit(ipc_remove)", errno);
|
|
|
|
if (semctl(ipc, 0, SETVAL, (int)(actors.size() + 1)))
|
|
|
|
failure_perror("semctl(SETVAL.0, shared_sems)", errno);
|
|
|
|
for (size_t i = 1; i < actors.size() + 2; ++i)
|
|
|
|
if (semctl(ipc, i, SETVAL, 1))
|
|
|
|
failure_perror("semctl(SETVAL.N, shared_sems)", errno);
|
2019-11-12 18:28:24 +08:00
|
|
|
#else
|
2017-03-30 23:54:57 +08:00
|
|
|
assert(shared == nullptr);
|
2019-11-07 21:17:08 +08:00
|
|
|
shared = (shared_t *)mmap(
|
|
|
|
nullptr, sizeof(shared_t) + actors.size() * sizeof(shared->events[0]),
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED | MAP_ANONYMOUS
|
|
|
|
#ifdef MAP_HASSEMAPHORE
|
|
|
|
| MAP_HASSEMAPHORE
|
|
|
|
#endif
|
|
|
|
,
|
|
|
|
-1, 0);
|
|
|
|
if (MAP_FAILED == (void *)shared)
|
2020-10-20 20:42:50 +08:00
|
|
|
failure_perror("mmap(shared)", errno);
|
2019-11-07 21:17:08 +08:00
|
|
|
|
|
|
|
shared->count = actors.size() + 1;
|
|
|
|
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
|
|
|
|
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
|
2017-03-30 23:54:57 +08:00
|
|
|
pthread_barrierattr_t barrierattr;
|
2019-11-12 18:28:24 +08:00
|
|
|
int err = pthread_barrierattr_init(&barrierattr);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_barrierattr_init()", err);
|
|
|
|
err = pthread_barrierattr_setpshared(&barrierattr, PTHREAD_PROCESS_SHARED);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_barrierattr_setpshared()", err);
|
|
|
|
|
|
|
|
err = pthread_barrier_init(&shared->barrier, &barrierattr, shared->count);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_barrier_init(shared)", err);
|
2019-11-07 21:17:08 +08:00
|
|
|
pthread_barrierattr_destroy(&barrierattr);
|
|
|
|
|
|
|
|
pthread_mutexattr_t mutexattr;
|
2019-11-12 18:28:24 +08:00
|
|
|
err = pthread_mutexattr_init(&mutexattr);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_mutexattr_init()", err);
|
|
|
|
err = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_mutexattr_setpshared()", err);
|
2019-11-07 21:17:08 +08:00
|
|
|
|
2017-03-30 23:54:57 +08:00
|
|
|
pthread_condattr_t condattr;
|
2019-11-12 18:28:24 +08:00
|
|
|
err = pthread_condattr_init(&condattr);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_condattr_init()", err);
|
|
|
|
err = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_condattr_setpshared()", err);
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2019-11-12 18:28:24 +08:00
|
|
|
err = pthread_mutex_init(&shared->mutex, &mutexattr);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_mutex_init(shared)", err);
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2019-11-07 21:17:08 +08:00
|
|
|
for (size_t i = 0; i < shared->count; ++i) {
|
|
|
|
pthread_cond_t *event = &shared->events[i];
|
2019-11-12 18:28:24 +08:00
|
|
|
err = pthread_cond_init(event, &condattr);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_cond_init(shared)", err);
|
2017-05-11 00:16:14 +08:00
|
|
|
log_trace("osal_setup: event(shared pthread_cond) %" PRIuPTR " -> %p", i,
|
2019-10-26 05:02:28 +08:00
|
|
|
__Wpedantic_format_voidptr(event));
|
2017-03-30 23:54:57 +08:00
|
|
|
}
|
|
|
|
pthread_condattr_destroy(&condattr);
|
|
|
|
pthread_mutexattr_destroy(&mutexattr);
|
2019-11-12 18:28:24 +08:00
|
|
|
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
|
2019-11-07 21:17:08 +08:00
|
|
|
shared->barrier.countdown = shared->count;
|
2019-11-12 18:28:24 +08:00
|
|
|
if (sem_init(&shared->barrier.sema, true, 1))
|
|
|
|
failure_perror("sem_init(shared.barrier)", errno);
|
2019-11-07 21:17:08 +08:00
|
|
|
for (size_t i = 0; i < shared->count; ++i) {
|
|
|
|
sem_t *event = &shared->events[i];
|
2019-11-12 18:28:24 +08:00
|
|
|
if (sem_init(event, true, 0))
|
|
|
|
failure_perror("sem_init(shared.event)", errno);
|
2019-11-07 21:17:08 +08:00
|
|
|
log_trace("osal_setup: event(shared sem_init) %" PRIuPTR " -> %p", i,
|
|
|
|
__Wpedantic_format_voidptr(event));
|
|
|
|
}
|
2019-11-12 18:28:24 +08:00
|
|
|
#else
|
|
|
|
#error "FIXME"
|
|
|
|
#endif /* MDBX_LOCKING */
|
|
|
|
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
|
2017-03-30 23:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void osal_broadcast(unsigned id) {
|
2017-04-21 23:41:11 +08:00
|
|
|
log_trace("osal_broadcast: event %u", id);
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
|
2019-11-12 20:14:07 +08:00
|
|
|
if (semctl(ipc, id + 1, SETVAL, 0))
|
|
|
|
failure_perror("semctl(SETVAL)", errno);
|
2019-11-12 18:28:24 +08:00
|
|
|
#else
|
|
|
|
assert(shared != nullptr && shared != MAP_FAILED);
|
2019-11-07 21:17:08 +08:00
|
|
|
if (id >= shared->count)
|
2017-04-21 23:41:11 +08:00
|
|
|
failure("osal_broadcast: id > limit");
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
|
|
|
|
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
|
|
|
|
int err = pthread_cond_broadcast(shared->events + id);
|
|
|
|
if (err)
|
|
|
|
failure_perror("pthread_cond_broadcast(shared)", err);
|
|
|
|
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
|
|
|
|
if (sem_post(shared->events + id))
|
|
|
|
failure_perror("sem_post(shared)", errno);
|
2019-11-07 21:17:08 +08:00
|
|
|
#else
|
2019-11-12 18:28:24 +08:00
|
|
|
#error "FIXME"
|
|
|
|
#endif /* MDBX_LOCKING */
|
|
|
|
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
|
2017-03-30 23:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int osal_waitfor(unsigned id) {
|
2017-04-21 23:41:11 +08:00
|
|
|
log_trace("osal_waitfor: event %u", id);
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
|
2019-11-12 20:14:07 +08:00
|
|
|
struct sembuf op;
|
|
|
|
memset(&op, 0, sizeof(op));
|
|
|
|
op.sem_num = (short)(id + 1);
|
|
|
|
int rc = semop(ipc, &op, 1) ? errno : MDBX_SUCCESS;
|
2019-11-12 18:28:24 +08:00
|
|
|
#else
|
|
|
|
assert(shared != nullptr && shared != MAP_FAILED);
|
2019-11-07 21:17:08 +08:00
|
|
|
if (id >= shared->count)
|
2017-04-21 23:41:11 +08:00
|
|
|
failure("osal_waitfor: id > limit");
|
|
|
|
|
2019-11-12 18:28:24 +08:00
|
|
|
#if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
|
|
|
|
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
|
2017-03-30 23:54:57 +08:00
|
|
|
int rc = pthread_mutex_lock(&shared->mutex);
|
|
|
|
if (rc != 0)
|
|
|
|
failure_perror("pthread_mutex_lock(shared)", rc);
|
|
|
|
|
2019-11-07 21:17:08 +08:00
|
|
|
rc = pthread_cond_wait(shared->events + id, &shared->mutex);
|
2017-03-30 23:54:57 +08:00
|
|
|
if (rc && rc != EINTR)
|
|
|
|
failure_perror("pthread_cond_wait(shared)", rc);
|
|
|
|
|
|
|
|
rc = pthread_mutex_unlock(&shared->mutex);
|
|
|
|
if (rc != 0)
|
|
|
|
failure_perror("pthread_mutex_unlock(shared)", rc);
|
2019-11-12 18:28:24 +08:00
|
|
|
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
|
2019-11-07 21:17:08 +08:00
|
|
|
int rc = sem_wait(shared->events + id) ? errno : 0;
|
2019-11-12 18:28:24 +08:00
|
|
|
if (rc == 0 && sem_post(shared->events + id))
|
|
|
|
failure_perror("sem_post(shared)", errno);
|
|
|
|
#else
|
|
|
|
#error "FIXME"
|
|
|
|
#endif /* MDBX_LOCKING */
|
|
|
|
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
|
2017-03-30 23:54:57 +08:00
|
|
|
|
|
|
|
return (rc == 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const std::string
|
|
|
|
actor_config::osal_serialize(simple_checksum &checksum) const {
|
|
|
|
(void)checksum;
|
|
|
|
/* not used in workload, but just for testing */
|
|
|
|
return "unix.fork";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool actor_config::osal_deserialize(const char *str, const char *end,
|
|
|
|
simple_checksum &checksum) {
|
|
|
|
(void)checksum;
|
|
|
|
/* not used in workload, but just for testing */
|
|
|
|
return strncmp(str, "unix.fork", 9) == 0 && str + 9 == end;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-10-02 06:17:09 +08:00
|
|
|
static pid_t overlord_pid;
|
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
static std::atomic_int sigusr1_head, sigusr2_head;
|
2019-10-02 06:17:09 +08:00
|
|
|
static void handler_SIGUSR(int signum) {
|
|
|
|
switch (signum) {
|
|
|
|
case SIGUSR1:
|
2020-06-26 08:33:53 +08:00
|
|
|
++sigusr1_head;
|
2019-10-02 06:17:09 +08:00
|
|
|
return;
|
|
|
|
case SIGUSR2:
|
2020-06-26 08:33:53 +08:00
|
|
|
++sigusr2_head;
|
2019-10-02 06:17:09 +08:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool osal_progress_push(bool active) {
|
|
|
|
if (overlord_pid) {
|
|
|
|
if (kill(overlord_pid, active ? SIGUSR1 : SIGUSR2))
|
|
|
|
failure_perror("osal_progress_push: kill(overload)", errno);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-09-22 11:51:47 +08:00
|
|
|
static std::unordered_map<pid_t, actor_status> children;
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2020-08-23 01:19:46 +08:00
|
|
|
static std::atomic_int sigalarm_head;
|
2019-10-12 15:24:18 +08:00
|
|
|
static void handler_SIGCHLD(int signum) {
|
|
|
|
if (signum == SIGALRM)
|
2020-06-26 08:33:53 +08:00
|
|
|
++sigalarm_head;
|
2019-10-12 15:24:18 +08:00
|
|
|
}
|
2017-03-30 23:54:57 +08:00
|
|
|
|
|
|
|
mdbx_pid_t osal_getpid(void) { return getpid(); }
|
|
|
|
|
2017-04-21 23:33:35 +08:00
|
|
|
int osal_delay(unsigned seconds) { return sleep(seconds) ? errno : 0; }
|
|
|
|
|
2017-03-30 23:54:57 +08:00
|
|
|
int osal_actor_start(const actor_config &config, mdbx_pid_t &pid) {
|
2020-09-22 11:51:47 +08:00
|
|
|
if (children.empty()) {
|
2019-10-02 06:17:09 +08:00
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
act.sa_handler = handler_SIGCHLD;
|
|
|
|
sigaction(SIGCHLD, &act, nullptr);
|
2019-10-12 15:24:18 +08:00
|
|
|
sigaction(SIGALRM, &act, nullptr);
|
2019-10-02 06:17:09 +08:00
|
|
|
act.sa_handler = handler_SIGUSR;
|
|
|
|
sigaction(SIGUSR1, &act, nullptr);
|
|
|
|
sigaction(SIGUSR2, &act, nullptr);
|
|
|
|
|
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGCHLD);
|
|
|
|
sigaddset(&mask, SIGUSR1);
|
|
|
|
sigaddset(&mask, SIGUSR2);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &mask, nullptr);
|
|
|
|
}
|
2017-03-30 23:54:57 +08:00
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if (pid == 0) {
|
2019-10-02 06:17:09 +08:00
|
|
|
overlord_pid = getppid();
|
2017-03-30 23:54:57 +08:00
|
|
|
const bool result = test_execute(config);
|
|
|
|
exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
return errno;
|
|
|
|
|
2019-11-09 15:50:43 +08:00
|
|
|
log_trace("osal_actor_start: fork pid %ld for %u", (long)pid,
|
|
|
|
config.actor_id);
|
2020-09-22 11:51:47 +08:00
|
|
|
children[pid] = as_running;
|
2017-03-30 23:54:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-22 11:51:47 +08:00
|
|
|
actor_status osal_actor_info(const mdbx_pid_t pid) { return children.at(pid); }
|
2017-03-30 23:54:57 +08:00
|
|
|
|
|
|
|
void osal_killall_actors(void) {
|
2020-09-22 11:51:47 +08:00
|
|
|
for (auto &pair : children) {
|
2017-03-30 23:54:57 +08:00
|
|
|
kill(pair.first, SIGKILL);
|
|
|
|
pair.second = as_killed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int osal_actor_poll(mdbx_pid_t &pid, unsigned timeout) {
|
2019-10-12 15:24:18 +08:00
|
|
|
static sig_atomic_t sigalarm_tail;
|
|
|
|
alarm(0) /* cancel prev timeout */;
|
|
|
|
sigalarm_tail = sigalarm_head /* reset timeout flag */;
|
|
|
|
|
|
|
|
int options = WNOHANG;
|
|
|
|
if (timeout) {
|
|
|
|
alarm((timeout > INT_MAX) ? INT_MAX : timeout);
|
|
|
|
options = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-30 23:54:57 +08:00
|
|
|
#ifdef WUNTRACED
|
|
|
|
options |= WUNTRACED;
|
|
|
|
#endif
|
|
|
|
#ifdef WCONTINUED
|
|
|
|
options |= WCONTINUED;
|
|
|
|
#endif
|
2019-10-02 06:17:09 +08:00
|
|
|
|
2019-10-12 15:24:18 +08:00
|
|
|
while (sigalarm_tail == sigalarm_head) {
|
|
|
|
int status;
|
|
|
|
pid = waitpid(0, &status, options);
|
2020-11-17 20:28:52 +08:00
|
|
|
const int err = errno;
|
2019-10-12 15:24:18 +08:00
|
|
|
|
|
|
|
if (pid > 0) {
|
|
|
|
if (WIFEXITED(status))
|
2020-09-22 11:51:47 +08:00
|
|
|
children[pid] =
|
2019-10-12 15:24:18 +08:00
|
|
|
(WEXITSTATUS(status) == EXIT_SUCCESS) ? as_successful : as_failed;
|
|
|
|
else if (WCOREDUMP(status))
|
2020-09-22 11:51:47 +08:00
|
|
|
children[pid] = as_coredump;
|
2019-10-12 15:24:18 +08:00
|
|
|
else if (WIFSIGNALED(status))
|
2020-09-22 11:51:47 +08:00
|
|
|
children[pid] = as_killed;
|
2019-10-12 15:24:18 +08:00
|
|
|
else if (WIFSTOPPED(status))
|
2020-09-22 11:51:47 +08:00
|
|
|
children[pid] = as_debugging;
|
2019-10-12 15:24:18 +08:00
|
|
|
else if (WIFCONTINUED(status))
|
2020-09-22 11:51:47 +08:00
|
|
|
children[pid] = as_running;
|
2019-10-12 15:24:18 +08:00
|
|
|
else {
|
|
|
|
assert(false);
|
|
|
|
}
|
2018-09-17 21:57:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2019-10-12 15:24:18 +08:00
|
|
|
static sig_atomic_t sigusr1_tail, sigusr2_tail;
|
|
|
|
if (sigusr1_tail != sigusr1_head) {
|
|
|
|
sigusr1_tail = sigusr1_head;
|
|
|
|
logging::progress_canary(true);
|
2020-11-17 20:28:52 +08:00
|
|
|
if (pid < 0 && err == EINTR)
|
2019-10-12 15:24:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sigusr2_tail != sigusr2_head) {
|
|
|
|
sigusr2_tail = sigusr2_head;
|
|
|
|
logging::progress_canary(false);
|
2020-11-17 20:28:52 +08:00
|
|
|
if (pid < 0 && err == EINTR)
|
2019-10-12 15:24:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
break;
|
2017-03-30 23:54:57 +08:00
|
|
|
|
2019-10-12 15:24:18 +08:00
|
|
|
if (err != EINTR)
|
|
|
|
return err;
|
2017-03-30 23:54:57 +08:00
|
|
|
}
|
2019-10-12 15:24:18 +08:00
|
|
|
return 0 /* timeout */;
|
2017-03-30 23:54:57 +08:00
|
|
|
}
|
2017-04-21 23:33:35 +08:00
|
|
|
|
|
|
|
void osal_yield(void) {
|
|
|
|
if (sched_yield())
|
|
|
|
failure_perror("sched_yield()", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void osal_udelay(unsigned us) {
|
2020-09-22 11:51:47 +08:00
|
|
|
chrono::time until, now = chrono::now_monotonic();
|
2017-04-21 23:33:35 +08:00
|
|
|
until.fixedpoint = now.fixedpoint + chrono::from_us(us).fixedpoint;
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
static unsigned threshold_us;
|
|
|
|
if (threshold_us == 0) {
|
2019-11-08 21:57:54 +08:00
|
|
|
#if defined(_POSIX_CPUTIME) && _POSIX_CPUTIME > -1 && \
|
|
|
|
defined(CLOCK_PROCESS_CPUTIME_ID)
|
2017-04-21 23:33:35 +08:00
|
|
|
if (clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)) {
|
|
|
|
int rc = errno;
|
2019-11-07 21:17:08 +08:00
|
|
|
log_warning("clock_getres(CLOCK_PROCESS_CPUTIME_ID), failed errno %d",
|
|
|
|
rc);
|
|
|
|
}
|
|
|
|
#endif /* CLOCK_PROCESS_CPUTIME_ID */
|
|
|
|
if (threshold_us == 0 && clock_getres(CLOCK_MONOTONIC, &ts)) {
|
|
|
|
int rc = errno;
|
|
|
|
failure_perror("clock_getres(CLOCK_MONOTONIC)", rc);
|
2017-04-21 23:33:35 +08:00
|
|
|
}
|
|
|
|
chrono::time threshold = chrono::from_timespec(ts);
|
|
|
|
assert(threshold.seconds() == 0);
|
|
|
|
|
|
|
|
threshold_us = chrono::fractional2us(threshold.fractional);
|
|
|
|
if (threshold_us < 1000)
|
|
|
|
threshold_us = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.tv_sec = ts.tv_nsec = 0;
|
|
|
|
if (us > threshold_us) {
|
|
|
|
ts.tv_sec = us / 1000000u;
|
|
|
|
ts.tv_nsec = (us % 1000000u) * 1000u;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (us > threshold_us) {
|
|
|
|
if (nanosleep(&ts, &ts)) {
|
|
|
|
int rc = errno;
|
|
|
|
/* if (rc == EINTR) { ... } ? */
|
|
|
|
failure_perror("usleep()", rc);
|
|
|
|
}
|
|
|
|
us = ts.tv_sec * 1000000u + ts.tv_nsec / 1000u;
|
|
|
|
}
|
|
|
|
cpu_relax();
|
|
|
|
|
2020-09-22 11:51:47 +08:00
|
|
|
now = chrono::now_monotonic();
|
2017-04-21 23:33:35 +08:00
|
|
|
} while (until.fixedpoint > now.fixedpoint);
|
|
|
|
}
|
2017-05-24 07:16:25 +08:00
|
|
|
|
|
|
|
bool osal_istty(int fd) { return isatty(fd) == 1; }
|
2018-03-20 01:40:10 +08:00
|
|
|
|
|
|
|
std::string osal_tempdir(void) {
|
2018-03-20 01:57:55 +08:00
|
|
|
const char *tempdir = getenv("TMPDIR");
|
|
|
|
if (!tempdir)
|
|
|
|
tempdir = getenv("TMP");
|
|
|
|
if (!tempdir)
|
|
|
|
tempdir = getenv("TEMPDIR");
|
|
|
|
if (!tempdir)
|
|
|
|
tempdir = getenv("TEMP");
|
2020-05-01 05:22:51 +08:00
|
|
|
if (tempdir && *tempdir) {
|
2018-03-20 01:57:55 +08:00
|
|
|
std::string dir(tempdir);
|
2020-05-01 05:22:51 +08:00
|
|
|
if (dir.back() != '/')
|
2018-03-20 01:57:55 +08:00
|
|
|
dir.append("/");
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
if (access("/dev/shm/", R_OK | W_OK | X_OK) == 0)
|
|
|
|
return "/dev/shm/";
|
|
|
|
return "";
|
2018-03-20 01:40:10 +08:00
|
|
|
}
|
2018-11-04 23:57:24 +08:00
|
|
|
|
2020-04-15 22:09:37 +08:00
|
|
|
#endif /* !Windows */
|