* xthr.c
@ 2018-07-31 8:22 Eric Wong
0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2018-07-31 8:22 UTC (permalink / raw)
To: spew
/*
* Cross-thread memory allocation test
* Copyright (C) 2014-2018, Eric Wong <e@80x24.org>
* License: LGPLv2.1 or later (depends on URCU)
*
* gcc -o xthr xthr.c -O3 -pthread -lurcu-cds -lurcu
* Usage: xthr [-a NALLOC] [-m BYTES] [-i ITERATIONS]
*
* -a number of threads doing allocation (default:1)
* -m malloc size in bytes (default:1 word (4-8 bytes))
* -i total number of malloc/free pairs (default:1024)
*/
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <urcu.h>
#include <urcu/wfcqueue.h>
#include <urcu/compiler.h>
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
static int memzero;
static size_t iter = 1024;
static size_t alloc_bytes = sizeof(struct rcu_head);
static void do_free(struct rcu_head *head)
{
free(head);
}
static void *do_alloc(void *arg)
{
const size_t max = (const size_t)arg;
const size_t bytes = alloc_bytes;
size_t i;
rcu_register_thread();
if (memzero) {
const size_t zbytes = bytes - CAA_CACHE_LINE_SIZE;
for (i = 0; i < max; i++) {
struct rcu_head *node = malloc(bytes);
if (!node) errx(1, "malloc failed");
memset(node, 0, zbytes);
call_rcu(node, do_free);
}
} else {
for (i = 0; i < max; i++) {
struct rcu_head *node = malloc(bytes);
if (!node) errx(1, "malloc failed");
call_rcu(node, do_free);
}
}
rcu_unregister_thread();
return NULL;
}
int main(int argc, char *argv[])
{
size_t i;
unsigned long nalloc = 1, nfree = 1;
pthread_t *th_alloc;
int err;
int opt;
unsigned long call_rcu_flags = 0;
rcu_init();
rcu_register_thread();
while ((opt = getopt(argc, argv, "a:m:i:zr")) != -1) {
char *end;
size_t c = 0;
switch (opt) {
case 'a': c = nalloc = strtoul(optarg, &end, 10); break;
case 'm': c = alloc_bytes = strtoul(optarg, &end, 10); break;
case 'i': c = iter = strtoul(optarg, &end, 10); break;
case 'r': call_rcu_flags = URCU_CALL_RCU_RT; continue;
case 'z': memzero = 1; continue;
default: errx(1, "bad arg: -%c", opt);
}
if (*end) errx(1, "trailing bytes: %s", end);
if (c == 0) errx(1, "-%c must be non-zero", opt);
}
if (alloc_bytes < sizeof(struct rcu_head)) {
alloc_bytes = sizeof(struct rcu_head);
fprintf(stderr, "-m clamped to %zu bytes\n", alloc_bytes);
}
if (memzero && alloc_bytes <= CAA_CACHE_LINE_SIZE)
memzero = 0;
th_alloc = malloc(sizeof(pthread_t) * nalloc);
if (!th_alloc)
errx(1, "malloc fails");
for (i = 0; i < nalloc; i++) {
size_t n = iter / nalloc;
if (i == 0) n += iter % nalloc;
err = pthread_create(&th_alloc[i], NULL, do_alloc, (void *)n);
if (err != 0)
errx(1, "failed to create thread: %s", strerror(err));
}
for (i = 0; i < nalloc; i++) {
err = pthread_join(th_alloc[i], NULL);
if (err != 0)
errx(1, "failed to join thread: %s", strerror(err));
}
rcu_barrier();
rcu_unregister_thread();
return 0;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-07-31 8:22 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-31 8:22 xthr.c Eric Wong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).