mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
Fix issues with previous commit
This commit is contained in:
parent
2f48a02b44
commit
a0a404a431
8 changed files with 122 additions and 57 deletions
|
@ -86,19 +86,28 @@ static ssize_t GetRandomBsd(char *p, size_t n, void impl(char *, size_t)) {
|
|||
}
|
||||
}
|
||||
|
||||
static ssize_t GetDevUrandom(char *p, size_t n) {
|
||||
static ssize_t GetDevUrandom(char *p, size_t n, unsigned f) {
|
||||
int fd;
|
||||
int oflags;
|
||||
ssize_t rc;
|
||||
BLOCK_SIGNALS;
|
||||
BLOCK_CANCELATION;
|
||||
fd = sys_openat(AT_FDCWD, "/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
|
||||
const char *dev;
|
||||
BEGIN_CANCELATION_POINT;
|
||||
if (f & GRND_RANDOM) {
|
||||
dev = "/dev/random";
|
||||
} else {
|
||||
dev = "/dev/urandom";
|
||||
}
|
||||
oflags = O_RDONLY | O_CLOEXEC;
|
||||
if (f & GRND_NONBLOCK)
|
||||
oflags |= O_NONBLOCK;
|
||||
fd = sys_openat(AT_FDCWD, dev, oflags, 0);
|
||||
if (fd != -1) {
|
||||
rc = sys_read(fd, p, n);
|
||||
sys_close(fd);
|
||||
} else {
|
||||
rc = -1;
|
||||
}
|
||||
ALLOW_CANCELATION;
|
||||
ALLOW_SIGNALS;
|
||||
END_CANCELATION_POINT;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
@ -122,7 +131,7 @@ ssize_t __getrandom(void *p, size_t n, unsigned f) {
|
|||
#endif
|
||||
} else {
|
||||
BEGIN_CANCELATION_POINT;
|
||||
rc = GetDevUrandom(p, n);
|
||||
rc = GetDevUrandom(p, n, f);
|
||||
END_CANCELATION_POINT;
|
||||
}
|
||||
return rc;
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
static int _rand64_pid;
|
||||
static unsigned __int128 _rand64_pool;
|
||||
pthread_mutex_t _rand64_lock_obj = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
||||
pthread_mutex_t _rand64_lock_obj = PTHREAD_SIGNAL_SAFE_MUTEX_INITIALIZER_NP;
|
||||
|
||||
/**
|
||||
* Returns nondeterministic random data.
|
||||
|
|
|
@ -85,7 +85,7 @@ static void _onfork_child(void) {
|
|||
if (IsWindows())
|
||||
__proc_wipe();
|
||||
__fds_lock_obj = (pthread_mutex_t)PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
||||
_rand64_lock_obj = (pthread_mutex_t)PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
||||
_rand64_lock_obj = (pthread_mutex_t)PTHREAD_SIGNAL_SAFE_MUTEX_INITIALIZER_NP;
|
||||
_pthread_lock_obj = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
|
||||
atomic_store_explicit(&__maps.lock, 0, memory_order_relaxed);
|
||||
if (_weaken(_pthread_onfork_child))
|
||||
|
|
|
@ -4,23 +4,76 @@
|
|||
#include "libc/stdio/stdio.h"
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define BENCHMARK(ITERATIONS, WORK_PER_RUN, CODE) \
|
||||
do { \
|
||||
struct timespec start = timespec_mono(); \
|
||||
for (int __i = 0; __i < ITERATIONS; ++__i) { \
|
||||
asm volatile("" ::: "memory"); \
|
||||
CODE; \
|
||||
} \
|
||||
long long work = ((WORK_PER_RUN) ? (WORK_PER_RUN) : 1) * (ITERATIONS); \
|
||||
double nanos = \
|
||||
(timespec_tonanos(timespec_sub(timespec_mono(), start)) + work - 1) / \
|
||||
(double)work; \
|
||||
if (nanos < 1000) { \
|
||||
printf("%10g ns %2dx %s\n", nanos, (ITERATIONS), #CODE); \
|
||||
} else { \
|
||||
printf("%10lld ns %2dx %s\n", (long long)nanos, (ITERATIONS), #CODE); \
|
||||
} \
|
||||
#define X(x) __expropriate(x)
|
||||
#define V(x) __veil("r", x)
|
||||
|
||||
#define BENCHMARK(ITERATIONS, WORK_PER_RUN, CODE) \
|
||||
do { \
|
||||
struct timespec start = timespec_mono(); \
|
||||
for (int __i = 0; __i < ITERATIONS; ++__i) { \
|
||||
asm volatile("" ::: "memory"); \
|
||||
CODE; \
|
||||
} \
|
||||
double total_nanos = \
|
||||
(double)timespec_tonanos(timespec_sub(timespec_mono(), start)); \
|
||||
double total_work = \
|
||||
(double)((WORK_PER_RUN) ? (WORK_PER_RUN) : 1) * (ITERATIONS); \
|
||||
_print_benchmark_result(total_nanos, total_work, ITERATIONS, #CODE); \
|
||||
} while (0)
|
||||
|
||||
static void _print_benchmark_result(double total_nanos, double total_work,
|
||||
int iterations, const char* code) {
|
||||
double time_per_op = total_nanos / iterations;
|
||||
double throughput = total_work / (total_nanos * 1e-9);
|
||||
const char* throughput_unit;
|
||||
const char* time_unit;
|
||||
double time_value;
|
||||
|
||||
// Determine throughput unit
|
||||
if (throughput >= 1e9) {
|
||||
throughput /= 1e9;
|
||||
throughput_unit = "G";
|
||||
} else if (throughput >= 1e6) {
|
||||
throughput /= 1e6;
|
||||
throughput_unit = "M";
|
||||
} else if (throughput >= 1e3) {
|
||||
throughput /= 1e3;
|
||||
throughput_unit = "K";
|
||||
} else {
|
||||
throughput_unit = " ";
|
||||
}
|
||||
|
||||
// Determine time unit
|
||||
if (time_per_op >= 1e6) {
|
||||
time_value = time_per_op / 1e6;
|
||||
time_unit = "ms";
|
||||
} else if (time_per_op >= 1e3) {
|
||||
time_value = time_per_op / 1e3;
|
||||
time_unit = "µs";
|
||||
} else {
|
||||
time_value = time_per_op;
|
||||
time_unit = "ns";
|
||||
}
|
||||
|
||||
// Determine work unit
|
||||
const char* work_unit;
|
||||
double work_value = total_work / iterations;
|
||||
if (work_value >= 1e9) {
|
||||
work_value /= 1e9;
|
||||
work_unit = "G";
|
||||
} else if (work_value >= 1e6) {
|
||||
work_value /= 1e6;
|
||||
work_unit = "M";
|
||||
} else if (work_value >= 1e3) {
|
||||
work_value /= 1e3;
|
||||
work_unit = "K";
|
||||
} else {
|
||||
work_unit = " ";
|
||||
}
|
||||
|
||||
printf("%8.2f %-2s %6.2f %s/s %6.2f %s %2dx %s\n", time_value, time_unit,
|
||||
throughput, throughput_unit, work_value, work_unit, iterations, code);
|
||||
}
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_LIBC_TESTLIB_BENCHMARK_H_ */
|
||||
|
|
|
@ -47,6 +47,9 @@ COSMOPOLITAN_C_START_
|
|||
|
||||
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP {0, {}, PTHREAD_MUTEX_RECURSIVE}
|
||||
|
||||
#define PTHREAD_SIGNAL_SAFE_MUTEX_INITIALIZER_NP \
|
||||
{0, {}, PTHREAD_MUTEX_RECURSIVE | PTHREAD_PROCESS_SHARED}
|
||||
|
||||
#ifndef __cplusplus
|
||||
#define _PTHREAD_ATOMIC(x) _Atomic(x)
|
||||
#else
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue