Name the RocksDB background threads

This commit is contained in:
Daniel Smith 2020-11-03 20:29:32 +00:00
parent f83423c968
commit 179dea5a1b
7 changed files with 38 additions and 26 deletions

View File

@ -58,8 +58,7 @@ struct Coroutine /*: IThreadlike*/ {
void start() { void start() {
int result = Coro_startCoro_( swapCoro(coro), coro, this, &entry ); int result = Coro_startCoro_( swapCoro(coro), coro, this, &entry );
if (result == ENOMEM) if (result == ENOMEM) platform::outOfMemory();
platform::outOfMemory();
} }
void unblock() { void unblock() {
@ -179,7 +178,7 @@ class WorkPool : public IThreadPool, public ReferenceCounted<WorkPool<Threadlike
ACTOR Future<Void> stopOnError( WorkPool* w ) { ACTOR Future<Void> stopOnError( WorkPool* w ) {
try { try {
wait( w->getError() ); wait(w->getError());
ASSERT(false); ASSERT(false);
} catch (Error& e) { } catch (Error& e) {
w->stop(e); w->stop(e);
@ -200,7 +199,7 @@ public:
} }
virtual Future<Void> getError() { return pool->anyError.getResult(); } virtual Future<Void> getError() { return pool->anyError.getResult(); }
virtual void addThread( IThreadPoolReceiver* userData ) { virtual void addThread(IThreadPoolReceiver* userData, const char*) {
checkError(); checkError();
auto w = new Worker(pool.getPtr(), userData); auto w = new Worker(pool.getPtr(), userData);
@ -294,8 +293,7 @@ void CoroThreadPool::init()
{ {
if (!current_coro) { if (!current_coro) {
current_coro = main_coro = Coro_new(); current_coro = main_coro = Coro_new();
if (main_coro == NULL) if (main_coro == NULL) platform::outOfMemory();
platform::outOfMemory();
Coro_initializeMainCoro(main_coro); Coro_initializeMainCoro(main_coro);
//printf("Main thread: %d bytes stack presumed available\n", Coro_bytesLeftOnStack(current_coro)); //printf("Main thread: %d bytes stack presumed available\n", Coro_bytesLeftOnStack(current_coro));

View File

@ -359,9 +359,9 @@ struct RocksDBKeyValueStore : IKeyValueStore {
{ {
writeThread = createGenericThreadPool(); writeThread = createGenericThreadPool();
readThreads = createGenericThreadPool(); readThreads = createGenericThreadPool();
writeThread->addThread(new Writer(db, id)); writeThread->addThread(new Writer(db, id), "fdb-rocksdb-wr");
for (unsigned i = 0; i < SERVER_KNOBS->ROCKSDB_READ_PARALLELISM; ++i) { for (unsigned i = 0; i < SERVER_KNOBS->ROCKSDB_READ_PARALLELISM; ++i) {
readThreads->addThread(new Reader(db)); readThreads->addThread(new Reader(db), "fdb-rocksdb-re");
} }
} }

View File

@ -95,9 +95,9 @@ public:
virtual Future<Void> getError() { return Never(); } // FIXME virtual Future<Void> getError() { return Never(); } // FIXME
virtual void addref() { ReferenceCounted<ThreadPool>::addref(); } virtual void addref() { ReferenceCounted<ThreadPool>::addref(); }
virtual void delref() { if (ReferenceCounted<ThreadPool>::delref_no_destroy()) stop(); } virtual void delref() { if (ReferenceCounted<ThreadPool>::delref_no_destroy()) stop(); }
void addThread( IThreadPoolReceiver* userData ) { void addThread(IThreadPoolReceiver* userData, const char* name) {
threads.push_back(new Thread(this, userData)); threads.push_back(new Thread(this, userData));
startThread(start, threads.back(), stackSize); startThread(start, threads.back(), stackSize, name);
} }
void post( PThreadAction action ) { void post( PThreadAction action ) {
ios.post( ActionWrapper( action ) ); ios.post( ActionWrapper( action ) );

View File

@ -22,6 +22,8 @@
#define FLOW_ITHREADPOOL_H #define FLOW_ITHREADPOOL_H
#pragma once #pragma once
#include <string_view>
#include "flow/flow.h" #include "flow/flow.h"
// The IThreadPool interface represents a thread pool suitable for doing blocking disk-intensive work // The IThreadPool interface represents a thread pool suitable for doing blocking disk-intensive work
@ -58,7 +60,7 @@ class IThreadPool {
public: public:
virtual ~IThreadPool() {} virtual ~IThreadPool() {}
virtual Future<Void> getError() = 0; // asynchronously throws an error if there is an internal error virtual Future<Void> getError() = 0; // asynchronously throws an error if there is an internal error
virtual void addThread( IThreadPoolReceiver* userData ) = 0; virtual void addThread(IThreadPoolReceiver* userData, const char* name = nullptr) = 0;
virtual void post( PThreadAction action ) = 0; virtual void post( PThreadAction action ) = 0;
virtual Future<Void> stop(Error const& e = success()) = 0; virtual Future<Void> stop(Error const& e = success()) = 0;
virtual bool isCoro() const { return false; } virtual bool isCoro() const { return false; }

View File

@ -1049,7 +1049,6 @@ void getDiskStatistics(std::string const& directory, uint64_t& currentIOs, uint6
writeSectors = total_blocks_read; writeSectors = total_blocks_read;
readSectors = total_blocks_write; readSectors = total_blocks_write;
} }
} }
dev_t getDeviceId(std::string path) { dev_t getDeviceId(std::string path) {
@ -2519,11 +2518,19 @@ void setCloseOnExec( int fd ) {
} // namespace platform } // namespace platform
#ifdef _WIN32 #ifdef _WIN32
THREAD_HANDLE startThread(void (*func) (void *), void *arg, int stackSize) { THREAD_HANDLE startThread(void (*func)(void*), void* arg, int stackSize, const char* name) {
return (void *)_beginthread(func, stackSize, arg); // Convert `const char*` to `const wchar*` because Windows.
size_t newsize = strlen(orig) + 1;
wchar_t* wcstring = new wchar_t[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, name, _TRUNCATE);
auto handle = _beginthread(func, stackSize, arg);
SetThreadDescription(handle, wcstring);
delete[] wcstring;
return (void*)handle;
} }
#elif (defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)) #elif (defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__))
THREAD_HANDLE startThread(void *(*func) (void *), void *arg, int stackSize) { THREAD_HANDLE startThread(void* (*func)(void*), void* arg, int stackSize, const char* name) {
pthread_t t; pthread_t t;
pthread_attr_t attr; pthread_attr_t attr;
@ -2542,6 +2549,11 @@ THREAD_HANDLE startThread(void *(*func) (void *), void *arg, int stackSize) {
pthread_create(&t, &attr, func, arg); pthread_create(&t, &attr, func, arg);
pthread_attr_destroy(&attr); pthread_attr_destroy(&attr);
if (name != nullptr) {
// TODO: Should this just truncate?
ASSERT_EQ(pthread_setname_np(t, name), 0);
}
return t; return t;
} }
#else #else
@ -3273,7 +3285,7 @@ int64_t getNumProfilesCaptured() {
void profileHandler(int sig) { void profileHandler(int sig) {
#ifdef __linux__ #ifdef __linux__
if(!profileThread) { if (!profileThread) {
return; return;
} }

View File

@ -153,13 +153,13 @@ inline static T& makeDependent(T& value) { return value; }
#define THREAD_FUNC static void __cdecl #define THREAD_FUNC static void __cdecl
#define THREAD_FUNC_RETURN void #define THREAD_FUNC_RETURN void
#define THREAD_HANDLE void * #define THREAD_HANDLE void *
THREAD_HANDLE startThread(void (func) (void *), void *arg, int stackSize = 0); THREAD_HANDLE startThread(void(func)(void*), void* arg, int stackSize = 0, const char* name = nullptr);
#define THREAD_RETURN return #define THREAD_RETURN return
#elif defined(__unixish__) #elif defined(__unixish__)
#define THREAD_FUNC static void * #define THREAD_FUNC static void *
#define THREAD_FUNC_RETURN void * #define THREAD_FUNC_RETURN void *
#define THREAD_HANDLE pthread_t #define THREAD_HANDLE pthread_t
THREAD_HANDLE startThread(void *(func) (void *), void *arg, int stackSize = 0); THREAD_HANDLE startThread(void*(func)(void*), void* arg, int stackSize = 0, const char* name = nullptr);
#define THREAD_RETURN return NULL #define THREAD_RETURN return NULL
#else #else
#error How do I start a new thread on this platform? #error How do I start a new thread on this platform?

View File

@ -61,7 +61,7 @@ public:
Future<Void> getError() { Future<Void> getError() {
return errors.getFuture(); return errors.getFuture();
} }
void addThread( IThreadPoolReceiver* userData ) { void addThread(IThreadPoolReceiver* userData, const char*) {
ASSERT( !thread ); ASSERT( !thread );
thread = userData; thread = userData;
} }