Commit 605e530a authored by Ben Walker's avatar Ben Walker
Browse files

thread: Use TLS to accelerate thread look up



Change-Id: I8136db265c9cb8d61de4845ce6eaff2351b5b597
Signed-off-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.gerrithub.io/435939


Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent 70642bcb
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@
#include "spdk/queue.h"
#include "spdk/util.h"

#include "spdk_internal/thread.h"

#include "config-host.h"
#include "fio.h"
#include "optgroup.h"
@@ -142,6 +144,8 @@ spdk_fio_cleanup_thread(struct spdk_fio_thread *fio_thread)

	while (spdk_fio_poll_thread(fio_thread) > 0) {}

	spdk_set_thread(fio_thread->thread);

	spdk_free_thread();
	free(fio_thread->iocq);
	free(fio_thread);
+47 −0
Original line number Diff line number Diff line
/*-
 *   BSD LICENSE
 *
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef SPDK_THREAD_INTERNAL_H_
#define SPDK_THREAD_INTERNAL_H_

#include "spdk/stdinc.h"

struct spdk_thread;

/**
 * Force the current system thread to act as if executing the given SPDK thread.
 * This is only used for unit testing.
 */
void spdk_set_thread(struct spdk_thread *thread);

#endif /* SPDK_THREAD_INTERNAL_H_ */
+10 −2
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@

#include "spdk_internal/event.h"
#include "spdk_internal/log.h"
#include "spdk_internal/thread.h"

#include "spdk/log.h"
#include "spdk/thread.h"
@@ -134,7 +135,7 @@ spdk_event_call(struct spdk_event *event)
}

static inline uint32_t
_spdk_event_queue_run_batch(struct spdk_reactor *reactor)
_spdk_event_queue_run_batch(struct spdk_reactor *reactor, struct spdk_thread *thread)
{
	unsigned count, i;
	void *events[SPDK_EVENT_BATCH_SIZE];
@@ -153,6 +154,8 @@ _spdk_event_queue_run_batch(struct spdk_reactor *reactor)
		return 0;
	}

	spdk_set_thread(thread);

	for (i = 0; i < count; i++) {
		struct spdk_event *event = events[i];

@@ -160,6 +163,8 @@ _spdk_event_queue_run_batch(struct spdk_reactor *reactor)
		event->fn(event->arg1, event->arg2);
	}

	spdk_set_thread(NULL);

	spdk_mempool_put_bulk(g_spdk_event_mempool, events, count);

	return count;
@@ -294,7 +299,9 @@ _spdk_reactor_run(void *arg)

	sleep_cycles = reactor->max_delay_us * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
	if (g_context_switch_monitor_enabled) {
		spdk_set_thread(thread);
		_spdk_reactor_context_switch_monitor_start(reactor, NULL);
		spdk_set_thread(NULL);
	}
	now = spdk_get_ticks();
	reactor->tsc_last = now;
@@ -302,7 +309,7 @@ _spdk_reactor_run(void *arg)
	while (1) {
		bool took_action = false;

		event_count = _spdk_event_queue_run_batch(reactor);
		event_count = _spdk_event_queue_run_batch(reactor, thread);
		if (event_count > 0) {
			rc = 1;
			now = spdk_get_ticks();
@@ -346,6 +353,7 @@ _spdk_reactor_run(void *arg)
		}
	}

	spdk_set_thread(thread);
	_spdk_reactor_context_switch_monitor_stop(reactor, NULL);
	spdk_free_thread();
	return 0;
+6 −1
Original line number Diff line number Diff line
@@ -45,6 +45,8 @@ extern "C" {
#include "spdk/log.h"
#include "spdk/thread.h"
#include "spdk/bdev.h"

#include "spdk_internal/thread.h"
}

namespace rocksdb
@@ -579,8 +581,11 @@ public:

void SpdkInitializeThread(void)
{
	struct spdk_thread *thread;

	if (g_fs != NULL) {
		spdk_allocate_thread(NULL, NULL, NULL, NULL, "spdk_rocksdb");
		thread = spdk_allocate_thread(NULL, NULL, NULL, NULL, "spdk_rocksdb");
		spdk_set_thread(thread);
		g_sync_args.channel = spdk_fs_alloc_io_channel_sync(g_fs);
	}
}
+19 −14
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@
#include "spdk/util.h"

#include "spdk_internal/log.h"
#include "spdk_internal/thread.h"

#ifdef __linux__
#include <sys/prctl.h>
@@ -130,22 +131,12 @@ struct spdk_thread {
static TAILQ_HEAD(, spdk_thread) g_threads = TAILQ_HEAD_INITIALIZER(g_threads);
static uint32_t g_thread_count = 0;

static struct spdk_thread *
static __thread struct spdk_thread *tls_thread = NULL;

static inline struct spdk_thread *
_get_thread(void)
{
	pthread_t thread_id;
	struct spdk_thread *thread;

	thread_id = pthread_self();

	thread = NULL;
	TAILQ_FOREACH(thread, &g_threads, tailq) {
		if (thread->thread_id == thread_id) {
			return thread;
		}
	}

	return NULL;
	return tls_thread;
}

static void
@@ -257,6 +248,12 @@ spdk_allocate_thread(spdk_thread_pass_msg msg_fn,
	return thread;
}

void
spdk_set_thread(struct spdk_thread *thread)
{
	tls_thread = thread;
}

void
spdk_free_thread(void)
{
@@ -290,6 +287,8 @@ spdk_free_thread(void)

	free(thread);

	tls_thread = NULL;

	pthread_mutex_unlock(&g_devlist_mutex);
}

@@ -360,6 +359,10 @@ spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs)
	struct spdk_poller *poller;
	int rc = 0;

	assert(_get_thread() == NULL);

	tls_thread = thread;

	msg_count = _spdk_msg_queue_run_batch(thread, max_msgs);
	if (msg_count) {
		rc = 1;
@@ -420,6 +423,8 @@ spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs)
		}
	}

	tls_thread = NULL;

	return rc;
}

Loading