Tune queue limits, some other adjustments

This commit is contained in:
kwolekr 2013-02-21 17:44:14 -05:00
parent 67c59645ec
commit 60e6284f30
4 changed files with 174 additions and 29 deletions

@ -185,8 +185,8 @@ void set_default_settings(Settings *settings)
settings->setDefault("remote_media", ""); settings->setDefault("remote_media", "");
settings->setDefault("debug_log_level", "0"); settings->setDefault("debug_log_level", "0");
settings->setDefault("emergequeue_limit_total", "256"); settings->setDefault("emergequeue_limit_total", "256");
settings->setDefault("emergequeue_limit_diskonly", "5"); settings->setDefault("emergequeue_limit_diskonly", "");
settings->setDefault("emergequeue_limit_generate", "1"); settings->setDefault("emergequeue_limit_generate", "");
settings->setDefault("num_emerge_threads", ""); settings->setDefault("num_emerge_threads", "");
// physics stuff // physics stuff

@ -50,17 +50,28 @@ EmergeManager::EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef) {
mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info"); mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
qlimit_total = g_settings->getU16("emergequeue_limit_total");
qlimit_diskonly = g_settings->getU16("emergequeue_limit_diskonly");
qlimit_generate = g_settings->getU16("emergequeue_limit_generate");
queuemutex.Init(); queuemutex.Init();
int nthreads = g_settings->get("num_emerge_threads").empty() ?
porting::getNumberOfProcessors() : int nthreads;
g_settings->getU16("num_emerge_threads"); if (g_settings->get("num_emerge_threads").empty()) {
int nprocs = porting::getNumberOfProcessors();
// leave a proc for the main thread and one for some other misc threads
if (nprocs > 2)
nthreads = nprocs - 2;
} else {
nthreads = g_settings->getU16("num_emerge_threads");
}
if (nthreads < 1) if (nthreads < 1)
nthreads = 1; nthreads = 1;
qlimit_total = g_settings->getU16("emergequeue_limit_total");
qlimit_diskonly = g_settings->get("emergequeue_limit_diskonly").empty() ?
nthreads * 5 + 1 :
g_settings->getU16("emergequeue_limit_diskonly");
qlimit_generate = g_settings->get("emergequeue_limit_generate").empty() ?
nthreads + 1 :
g_settings->getU16("emergequeue_limit_generate");
for (int i = 0; i != nthreads; i++) for (int i = 0; i != nthreads; i++)
emergethread.push_back(new EmergeThread((Server *)gamedef, i)); emergethread.push_back(new EmergeThread((Server *)gamedef, i));
@ -138,6 +149,7 @@ bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate
peer_queue_count[peer_id] = count + 1; peer_queue_count[peer_id] = count + 1;
// insert into the EmergeThread queue with the least items
int lowestitems = emergethread[0]->blockqueue.size(); int lowestitems = emergethread[0]->blockqueue.size();
for (int i = 1; i != emergethread.size(); i++) { for (int i = 1; i != emergethread.size(); i++) {
int nitems = emergethread[i]->blockqueue.size(); int nitems = emergethread[i]->blockqueue.size();
@ -183,7 +195,7 @@ bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
int EmergeManager::getGroundLevelAtPoint(v2s16 p) { int EmergeManager::getGroundLevelAtPoint(v2s16 p) {
if (!mapgen[0]) { if (mapgen.size() == 0 || !mapgen[0]) {
errorstream << "EmergeManager: getGroundLevelAtPoint() called" errorstream << "EmergeManager: getGroundLevelAtPoint() called"
" before mapgen initialized" << std::endl; " before mapgen initialized" << std::endl;
return 0; return 0;
@ -365,7 +377,7 @@ bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
void *EmergeThread::Thread() { void *EmergeThread::Thread() {
ThreadStarted(); ThreadStarted();
log_register_thread("EmergeThread"); log_register_thread("EmergeThread" + id);
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER BEGIN_DEBUG_EXCEPTION_HANDLER

@ -131,29 +131,127 @@ void signal_handler_init(void)
#endif #endif
/* /*
Multithreading support Multithreading support
*/ */
int getNumberOfProcessors() { int getNumberOfProcessors() {
#if defined(_SC_NPROCESSORS_ONLN) #if defined(_SC_NPROCESSORS_ONLN)
return sysconf(_SC_NPROCESSORS_ONLN); return sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(__FreeBSD__) || defined(__APPLE__)
#elif defined(__FreeBSD__) || defined(__APPLE__)
unsigned int len, count; unsigned int len, count;
len = sizeof(count); len = sizeof(count);
return sysctlbyname("hw.ncpu", &count, &len, NULL, 0); return sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
#elif defined(_GNU_SOURCE)
#elif defined(_GNU_SOURCE)
return get_nprocs(); return get_nprocs();
#elif defined(_WIN32)
#elif defined(_WIN32)
SYSTEM_INFO sysinfo; SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo); GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors; return sysinfo.dwNumberOfProcessors;
#elif defined(PTW32_VERSION) || defined(__hpux)
#elif defined(PTW32_VERSION) || defined(__hpux)
return pthread_num_processors_np(); return pthread_num_processors_np();
#else
#else
return 1; return 1;
#endif
#endif
} }
bool threadBindToProcessor(threadid_t tid, int pnumber) {
#if defined(_WIN32)
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
if (!hThread)
return false;
bool success = SetThreadAffinityMask(hThread, 1 << pnumber) != 0;
CloseHandle(hThread);
return success;
#elif (defined(__FreeBSD__) && (__FreeBSD_version >= 702106)) \
|| defined(__linux) || defined(linux)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(pnumber, &cpuset);
return pthread_setaffinity_np(tid, sizeof(cpuset), &cpuset) == 0;
#elif defined(__sun) || defined(sun)
return processor_bind(P_LWPID, MAKE_LWPID_PTHREAD(tid),
pnumber, NULL) == 0;
#elif defined(_AIX)
return bindprocessor(BINDTHREAD, (tid_t)tid, pnumber) == 0;
#elif defined(__hpux) || defined(hpux)
pthread_spu_t answer;
return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP,
&answer, pnumber, tid) == 0;
#elif defined(__APPLE__)
struct thread_affinity_policy tapol;
thread_port_t threadport = pthread_mach_thread_np(tid);
tapol.affinity_tag = pnumber + 1;
return thread_policy_set(threadport, THREAD_AFFINITY_POLICY,
(thread_policy_t)&tapol, THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS;
#else
return false;
#endif
}
bool threadSetPriority(threadid_t tid, int prio) {
#if defined(_WIN32)
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, 0, tid);
if (!hThread)
return false;
bool success = SetThreadPriority(hThread, prio) != 0;
CloseHandle(hThread);
return success;
#else
struct sched_param sparam;
int policy;
if (pthread_getschedparam(tid, &policy, &sparam) != 0)
return false;
int min = sched_get_priority_min(policy);
int max = sched_get_priority_max(policy);
sparam.sched_priority = min + prio * (max - min) / THREAD_PRIORITY_HIGHEST;
return pthread_setschedparam(tid, policy, &sparam) == 0;
#endif
}
/* /*
Path mangler Path mangler
*/ */

@ -47,7 +47,32 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <unistd.h> #include <unistd.h>
#include <stdint.h> //for uintptr_t #include <stdint.h> //for uintptr_t
#if defined(linux) || defined(__linux)
#define _GNU_SOURCE
#endif
#include <sched.h>
#ifdef __FreeBSD__
#include <pthread_np.h>
typedef cpuset_t cpu_set_t;
#elif defined(__sun) || defined(sun)
#include <sys/types.h>
#include <sys/processor.h>
#elif defined(_AIX)
#include <sys/processor.h>
#elif __APPLE__
#include <mach/mach_init.h>
#include <mach/thread_policy.h>
#endif
#define sleep_ms(x) usleep(x*1000) #define sleep_ms(x) usleep(x*1000)
#define THREAD_PRIORITY_LOWEST 0
#define THREAD_PRIORITY_BELOW_NORMAL 1
#define THREAD_PRIORITY_NORMAL 2
#define THREAD_PRIORITY_ABOVE_NORMAL 3
#define THREAD_PRIORITY_HIGHEST 4
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
@ -108,6 +133,16 @@ void initializePaths();
*/ */
int getNumberOfProcessors(); int getNumberOfProcessors();
/*
Set a thread's affinity to a particular processor.
*/
bool threadBindToProcessor(threadid_t tid, int pnumber);
/*
Set a thread's priority.
*/
bool threadSetPriority(threadid_t tid, int prio);
/* /*
Resolution is 10-20ms. Resolution is 10-20ms.
Remember to check for overflows. Remember to check for overflows.