2013-08-27 17:38:45 +02:00
|
|
|
/*
|
|
|
|
* rcuq_test.c
|
|
|
|
*
|
|
|
|
* usage: rcuq_test <readers> <duration>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Mike D. Day, IBM Corporation.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-08-27 17:38:45 +02:00
|
|
|
#include "qemu/atomic.h"
|
|
|
|
#include "qemu/rcu.h"
|
|
|
|
#include "qemu/thread.h"
|
|
|
|
#include "qemu/rcu_queue.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test variables.
|
|
|
|
*/
|
|
|
|
|
2015-03-21 16:32:05 +01:00
|
|
|
static QemuMutex counts_mutex;
|
|
|
|
static long long n_reads = 0LL;
|
|
|
|
static long long n_updates = 0LL;
|
2018-09-11 01:27:45 +02:00
|
|
|
static int64_t n_reclaims;
|
|
|
|
static int64_t n_nodes_removed;
|
2015-03-21 16:32:05 +01:00
|
|
|
static long long n_nodes = 0LL;
|
|
|
|
static int g_test_in_charge = 0;
|
2013-08-27 17:38:45 +02:00
|
|
|
|
2015-03-21 16:32:05 +01:00
|
|
|
static int nthreadsrunning;
|
2013-08-27 17:38:45 +02:00
|
|
|
|
|
|
|
#define GOFLAG_INIT 0
|
|
|
|
#define GOFLAG_RUN 1
|
|
|
|
#define GOFLAG_STOP 2
|
|
|
|
|
2018-08-19 11:13:29 +02:00
|
|
|
static int goflag = GOFLAG_INIT;
|
2013-08-27 17:38:45 +02:00
|
|
|
|
|
|
|
#define RCU_READ_RUN 1000
|
|
|
|
#define RCU_UPDATE_RUN 10
|
|
|
|
#define NR_THREADS 100
|
|
|
|
#define RCU_Q_LEN 100
|
|
|
|
|
|
|
|
static QemuThread threads[NR_THREADS];
|
|
|
|
static struct rcu_reader_data *data[NR_THREADS];
|
|
|
|
static int n_threads;
|
|
|
|
|
|
|
|
static int select_random_el(int max)
|
|
|
|
{
|
|
|
|
return (rand() % max);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void create_thread(void *(*func)(void *))
|
|
|
|
{
|
|
|
|
if (n_threads >= NR_THREADS) {
|
|
|
|
fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
|
|
|
|
QEMU_THREAD_JOINABLE);
|
|
|
|
n_threads++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_all_threads(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_threads; i++) {
|
|
|
|
qemu_thread_join(&threads[i]);
|
|
|
|
}
|
|
|
|
n_threads = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:13:31 +02:00
|
|
|
#ifndef TEST_LIST_TYPE
|
|
|
|
#define TEST_LIST_TYPE 1
|
|
|
|
#endif
|
2013-08-27 17:38:45 +02:00
|
|
|
|
|
|
|
struct list_element {
|
2018-08-19 11:13:31 +02:00
|
|
|
#if TEST_LIST_TYPE == 1
|
2013-08-27 17:38:45 +02:00
|
|
|
QLIST_ENTRY(list_element) entry;
|
2018-08-19 11:13:32 +02:00
|
|
|
#elif TEST_LIST_TYPE == 2
|
|
|
|
QSIMPLEQ_ENTRY(list_element) entry;
|
2018-08-19 11:13:33 +02:00
|
|
|
#elif TEST_LIST_TYPE == 3
|
|
|
|
QTAILQ_ENTRY(list_element) entry;
|
2018-08-19 11:13:31 +02:00
|
|
|
#else
|
|
|
|
#error Invalid TEST_LIST_TYPE
|
|
|
|
#endif
|
2013-08-27 17:38:45 +02:00
|
|
|
struct rcu_head rcu;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void reclaim_list_el(struct rcu_head *prcu)
|
|
|
|
{
|
|
|
|
struct list_element *el = container_of(prcu, struct list_element, rcu);
|
|
|
|
g_free(el);
|
2015-03-21 16:32:05 +01:00
|
|
|
/* Accessed only from call_rcu thread. */
|
2018-09-11 01:27:45 +02:00
|
|
|
atomic_set_i64(&n_reclaims, n_reclaims + 1);
|
2013-08-27 17:38:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 11:13:31 +02:00
|
|
|
#if TEST_LIST_TYPE == 1
|
2018-12-06 11:58:10 +01:00
|
|
|
static QLIST_HEAD(, list_element) Q_list_head;
|
2013-08-27 17:38:45 +02:00
|
|
|
|
2018-08-19 11:13:31 +02:00
|
|
|
#define TEST_NAME "qlist"
|
|
|
|
#define TEST_LIST_REMOVE_RCU QLIST_REMOVE_RCU
|
|
|
|
#define TEST_LIST_INSERT_AFTER_RCU QLIST_INSERT_AFTER_RCU
|
|
|
|
#define TEST_LIST_INSERT_HEAD_RCU QLIST_INSERT_HEAD_RCU
|
|
|
|
#define TEST_LIST_FOREACH_RCU QLIST_FOREACH_RCU
|
|
|
|
#define TEST_LIST_FOREACH_SAFE_RCU QLIST_FOREACH_SAFE_RCU
|
2018-08-19 11:13:32 +02:00
|
|
|
|
|
|
|
#elif TEST_LIST_TYPE == 2
|
|
|
|
static QSIMPLEQ_HEAD(, list_element) Q_list_head =
|
|
|
|
QSIMPLEQ_HEAD_INITIALIZER(Q_list_head);
|
|
|
|
|
|
|
|
#define TEST_NAME "qsimpleq"
|
|
|
|
#define TEST_LIST_REMOVE_RCU(el, f) \
|
|
|
|
QSIMPLEQ_REMOVE_RCU(&Q_list_head, el, list_element, f)
|
|
|
|
|
|
|
|
#define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
|
|
|
|
QSIMPLEQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
|
|
|
|
|
|
|
|
#define TEST_LIST_INSERT_HEAD_RCU QSIMPLEQ_INSERT_HEAD_RCU
|
|
|
|
#define TEST_LIST_FOREACH_RCU QSIMPLEQ_FOREACH_RCU
|
|
|
|
#define TEST_LIST_FOREACH_SAFE_RCU QSIMPLEQ_FOREACH_SAFE_RCU
|
2018-08-19 11:13:33 +02:00
|
|
|
|
|
|
|
#elif TEST_LIST_TYPE == 3
|
|
|
|
static QTAILQ_HEAD(, list_element) Q_list_head;
|
|
|
|
|
|
|
|
#define TEST_NAME "qtailq"
|
|
|
|
#define TEST_LIST_REMOVE_RCU(el, f) QTAILQ_REMOVE_RCU(&Q_list_head, el, f)
|
|
|
|
|
|
|
|
#define TEST_LIST_INSERT_AFTER_RCU(list_el, el, f) \
|
|
|
|
QTAILQ_INSERT_AFTER_RCU(&Q_list_head, list_el, el, f)
|
|
|
|
|
|
|
|
#define TEST_LIST_INSERT_HEAD_RCU QTAILQ_INSERT_HEAD_RCU
|
|
|
|
#define TEST_LIST_FOREACH_RCU QTAILQ_FOREACH_RCU
|
|
|
|
#define TEST_LIST_FOREACH_SAFE_RCU QTAILQ_FOREACH_SAFE_RCU
|
2018-08-19 11:13:31 +02:00
|
|
|
#else
|
|
|
|
#error Invalid TEST_LIST_TYPE
|
|
|
|
#endif
|
|
|
|
|
2013-08-27 17:38:45 +02:00
|
|
|
static void *rcu_q_reader(void *arg)
|
|
|
|
{
|
2015-03-21 16:32:05 +01:00
|
|
|
long long n_reads_local = 0;
|
2013-08-27 17:38:45 +02:00
|
|
|
struct list_element *el;
|
|
|
|
|
2015-07-09 08:55:38 +02:00
|
|
|
rcu_register_thread();
|
|
|
|
|
2013-08-27 17:38:45 +02:00
|
|
|
*(struct rcu_reader_data **)arg = &rcu_reader;
|
|
|
|
atomic_inc(&nthreadsrunning);
|
2018-08-19 11:13:29 +02:00
|
|
|
while (atomic_read(&goflag) == GOFLAG_INIT) {
|
2013-08-27 17:38:45 +02:00
|
|
|
g_usleep(1000);
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:13:29 +02:00
|
|
|
while (atomic_read(&goflag) == GOFLAG_RUN) {
|
2013-08-27 17:38:45 +02:00
|
|
|
rcu_read_lock();
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
|
2013-08-27 17:38:45 +02:00
|
|
|
n_reads_local++;
|
2018-08-19 11:13:29 +02:00
|
|
|
if (atomic_read(&goflag) == GOFLAG_STOP) {
|
2013-08-27 17:38:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
g_usleep(100);
|
|
|
|
}
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_lock(&counts_mutex);
|
|
|
|
n_reads += n_reads_local;
|
|
|
|
qemu_mutex_unlock(&counts_mutex);
|
2015-07-09 08:55:38 +02:00
|
|
|
|
|
|
|
rcu_unregister_thread();
|
2013-08-27 17:38:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *rcu_q_updater(void *arg)
|
|
|
|
{
|
|
|
|
int j, target_el;
|
2015-03-21 16:32:05 +01:00
|
|
|
long long n_nodes_local = 0;
|
2013-08-27 17:38:45 +02:00
|
|
|
long long n_updates_local = 0;
|
|
|
|
long long n_removed_local = 0;
|
|
|
|
struct list_element *el, *prev_el;
|
|
|
|
|
|
|
|
*(struct rcu_reader_data **)arg = &rcu_reader;
|
|
|
|
atomic_inc(&nthreadsrunning);
|
2018-08-19 11:13:29 +02:00
|
|
|
while (atomic_read(&goflag) == GOFLAG_INIT) {
|
2013-08-27 17:38:45 +02:00
|
|
|
g_usleep(1000);
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:13:29 +02:00
|
|
|
while (atomic_read(&goflag) == GOFLAG_RUN) {
|
2013-08-27 17:38:45 +02:00
|
|
|
target_el = select_random_el(RCU_Q_LEN);
|
|
|
|
j = 0;
|
|
|
|
/* FOREACH_RCU could work here but let's use both macros */
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
|
2013-08-27 17:38:45 +02:00
|
|
|
j++;
|
|
|
|
if (target_el == j) {
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_REMOVE_RCU(prev_el, entry);
|
2013-08-27 17:38:45 +02:00
|
|
|
/* may be more than one updater in the future */
|
|
|
|
call_rcu1(&prev_el->rcu, reclaim_list_el);
|
|
|
|
n_removed_local++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-08-19 11:13:29 +02:00
|
|
|
if (atomic_read(&goflag) == GOFLAG_STOP) {
|
2013-08-27 17:38:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
target_el = select_random_el(RCU_Q_LEN);
|
|
|
|
j = 0;
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
|
2013-08-27 17:38:45 +02:00
|
|
|
j++;
|
|
|
|
if (target_el == j) {
|
2018-08-19 11:13:31 +02:00
|
|
|
struct list_element *new_el = g_new(struct list_element, 1);
|
2015-03-21 16:32:05 +01:00
|
|
|
n_nodes += n_nodes_local;
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
|
2013-08-27 17:38:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n_updates_local += 2;
|
|
|
|
synchronize_rcu();
|
|
|
|
}
|
|
|
|
synchronize_rcu();
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_lock(&counts_mutex);
|
|
|
|
n_nodes += n_nodes_local;
|
|
|
|
n_updates += n_updates_local;
|
2018-09-11 01:27:45 +02:00
|
|
|
atomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_unlock(&counts_mutex);
|
2013-08-27 17:38:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rcu_qtest_init(void)
|
|
|
|
{
|
|
|
|
struct list_element *new_el;
|
|
|
|
int i;
|
|
|
|
nthreadsrunning = 0;
|
|
|
|
srand(time(0));
|
|
|
|
for (i = 0; i < RCU_Q_LEN; i++) {
|
|
|
|
new_el = g_new(struct list_element, 1);
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
|
2013-08-27 17:38:45 +02:00
|
|
|
}
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_lock(&counts_mutex);
|
|
|
|
n_nodes += RCU_Q_LEN;
|
|
|
|
qemu_mutex_unlock(&counts_mutex);
|
2013-08-27 17:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rcu_qtest_run(int duration, int nreaders)
|
|
|
|
{
|
|
|
|
int nthreads = nreaders + 1;
|
|
|
|
while (atomic_read(&nthreadsrunning) < nthreads) {
|
|
|
|
g_usleep(1000);
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:13:29 +02:00
|
|
|
atomic_set(&goflag, GOFLAG_RUN);
|
2013-08-27 17:38:45 +02:00
|
|
|
sleep(duration);
|
2018-08-19 11:13:29 +02:00
|
|
|
atomic_set(&goflag, GOFLAG_STOP);
|
2013-08-27 17:38:45 +02:00
|
|
|
wait_all_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void rcu_qtest(const char *test, int duration, int nreaders)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
long long n_removed_local = 0;
|
|
|
|
|
|
|
|
struct list_element *el, *prev_el;
|
|
|
|
|
|
|
|
rcu_qtest_init();
|
|
|
|
for (i = 0; i < nreaders; i++) {
|
|
|
|
create_thread(rcu_q_reader);
|
|
|
|
}
|
|
|
|
create_thread(rcu_q_updater);
|
|
|
|
rcu_qtest_run(duration, nreaders);
|
|
|
|
|
2018-08-19 11:13:31 +02:00
|
|
|
TEST_LIST_FOREACH_SAFE_RCU(prev_el, &Q_list_head, entry, el) {
|
|
|
|
TEST_LIST_REMOVE_RCU(prev_el, entry);
|
2013-08-27 17:38:45 +02:00
|
|
|
call_rcu1(&prev_el->rcu, reclaim_list_el);
|
|
|
|
n_removed_local++;
|
|
|
|
}
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_lock(&counts_mutex);
|
2018-09-11 01:27:45 +02:00
|
|
|
atomic_set_i64(&n_nodes_removed, n_nodes_removed + n_removed_local);
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_unlock(&counts_mutex);
|
2013-08-27 17:38:45 +02:00
|
|
|
synchronize_rcu();
|
2018-09-11 01:27:45 +02:00
|
|
|
while (atomic_read_i64(&n_nodes_removed) > atomic_read_i64(&n_reclaims)) {
|
2013-08-27 17:38:45 +02:00
|
|
|
g_usleep(100);
|
|
|
|
synchronize_rcu();
|
|
|
|
}
|
|
|
|
if (g_test_in_charge) {
|
2018-09-11 01:27:45 +02:00
|
|
|
g_assert_cmpint(atomic_read_i64(&n_nodes_removed), ==,
|
|
|
|
atomic_read_i64(&n_reclaims));
|
2013-08-27 17:38:45 +02:00
|
|
|
} else {
|
|
|
|
printf("%s: %d readers; 1 updater; nodes read: " \
|
2018-09-11 01:27:45 +02:00
|
|
|
"%lld, nodes removed: %"PRIi64"; nodes reclaimed: %"PRIi64"\n",
|
|
|
|
test, nthreadsrunning - 1, n_reads,
|
|
|
|
atomic_read_i64(&n_nodes_removed), atomic_read_i64(&n_reclaims));
|
2013-08-27 17:38:45 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s duration nreaders\n", argv[0]);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gtest_seconds;
|
|
|
|
|
|
|
|
static void gtest_rcuq_one(void)
|
|
|
|
{
|
|
|
|
rcu_qtest("rcuqtest", gtest_seconds / 4, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gtest_rcuq_few(void)
|
|
|
|
{
|
|
|
|
rcu_qtest("rcuqtest", gtest_seconds / 4, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gtest_rcuq_many(void)
|
|
|
|
{
|
|
|
|
rcu_qtest("rcuqtest", gtest_seconds / 2, 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int duration = 0, readers = 0;
|
|
|
|
|
2015-03-21 16:32:05 +01:00
|
|
|
qemu_mutex_init(&counts_mutex);
|
2013-08-27 17:38:45 +02:00
|
|
|
if (argc >= 2) {
|
|
|
|
if (argv[1][0] == '-') {
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
if (g_test_quick()) {
|
|
|
|
gtest_seconds = 4;
|
|
|
|
} else {
|
|
|
|
gtest_seconds = 20;
|
|
|
|
}
|
2018-08-19 11:13:31 +02:00
|
|
|
g_test_add_func("/rcu/"TEST_NAME"/single-threaded", gtest_rcuq_one);
|
|
|
|
g_test_add_func("/rcu/"TEST_NAME"/short-few", gtest_rcuq_few);
|
|
|
|
g_test_add_func("/rcu/"TEST_NAME"/long-many", gtest_rcuq_many);
|
2013-08-27 17:38:45 +02:00
|
|
|
g_test_in_charge = 1;
|
|
|
|
return g_test_run();
|
|
|
|
}
|
|
|
|
duration = strtoul(argv[1], NULL, 0);
|
|
|
|
}
|
|
|
|
if (argc >= 3) {
|
|
|
|
readers = strtoul(argv[2], NULL, 0);
|
|
|
|
}
|
|
|
|
if (duration && readers) {
|
|
|
|
rcu_qtest(argv[0], duration, readers);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usage(argc, argv);
|
|
|
|
return -1;
|
|
|
|
}
|