From 1a610ff5311bc98042bb118de7e290cdb52bd6cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Sat, 13 Sep 2014 19:59:51 +0000 Subject: [PATCH] more sdlwork.c and winwork.c consolidation / SDL now supports envrionment variable OSDWORKQUEUEMAXTHREADS (nw) --- .gitattributes | 2 ++ src/osd/sdl/sdlos_win32.c | 26 ++------------------------ src/osd/sdl/sdlwork.c | 22 ++++++++++++++++------ src/osd/windows/windows.mak | 1 + src/osd/windows/winos.c | 35 +++++++++++++++++++++++++++++++++++ src/osd/windows/winos.h | 32 ++++++++++++++++++++++++++++++++ src/osd/windows/winwork.c | 31 ++++++++++++++++++------------- 7 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 src/osd/windows/winos.c create mode 100644 src/osd/windows/winos.h diff --git a/.gitattributes b/.gitattributes index 19f6453d3d9..ea8fa087c20 100644 --- a/.gitattributes +++ b/.gitattributes @@ -9552,6 +9552,8 @@ src/osd/windows/winmain.c svneol=native#text/plain src/osd/windows/winmain.h svneol=native#text/plain src/osd/windows/winmenu.c svneol=native#text/plain src/osd/windows/winmisc.c svneol=native#text/plain +src/osd/windows/winos.c svneol=native#text/plain +src/osd/windows/winos.h svneol=native#text/plain src/osd/windows/winprefix.h svneol=native#text/plain src/osd/windows/winptty.c svneol=native#text/plain src/osd/windows/winsocket.c svneol=native#text/plain diff --git a/src/osd/sdl/sdlos_win32.c b/src/osd/sdl/sdlos_win32.c index dfc61f0204a..75917803681 100644 --- a/src/osd/sdl/sdlos_win32.c +++ b/src/osd/sdl/sdlos_win32.c @@ -20,6 +20,8 @@ #include "osdcore.h" #include "strconv.h" +#include "../windows/winos.c" + //============================================================ // PROTOTYPES //============================================================ @@ -157,21 +159,6 @@ void osd_sleep(osd_ticks_t duration) } } -//============================================================ -// osd_num_processors -//============================================================ - -int osd_get_num_processors(void) -{ - SYSTEM_INFO info; - - // otherwise, fetch the info from the system - GetSystemInfo(&info); - - // max out at 4 for now since scaling above that seems to do poorly - return MIN(info.dwNumberOfProcessors, 4); -} - //============================================================ // osd_malloc //============================================================ @@ -253,15 +240,6 @@ void osd_free(void *ptr) #endif } -//============================================================ -// osd_getenv -//============================================================ - -char *osd_getenv(const char *name) -{ - return getenv(name); -} - //============================================================ // osd_setenv //============================================================ diff --git a/src/osd/sdl/sdlwork.c b/src/osd/sdl/sdlwork.c index dca494720ab..46597db4112 100644 --- a/src/osd/sdl/sdlwork.c +++ b/src/osd/sdl/sdlwork.c @@ -44,8 +44,9 @@ int osd_num_processors = 0; // PARAMETERS //============================================================ -#define SDLENV_PROCESSORS "OSDPROCESSORS" -#define SDLENV_CPUMASKS "OSDCPUMASKS" +#define ENV_PROCESSORS "OSDPROCESSORS" +#define ENV_CPUMASKS "OSDCPUMASKS" +#define ENV_WORKQUEUEMAXTHREADS "OSDWORKQUEUEMAXTHREADS" #define INFINITE (osd_ticks_per_second() * (osd_ticks_t) 10000) #define SPIN_LOOP_TIME (osd_ticks_per_second() / 10000) @@ -151,6 +152,7 @@ osd_work_queue *osd_work_queue_alloc(int flags) int numprocs = effective_num_processors(); osd_work_queue *queue; int threadnum; + char *osdworkqueuemaxthreads = osd_getenv("OSDWORKQUEUEMAXTHREADS"); // allocate a new queue queue = (osd_work_queue *)osd_malloc(sizeof(*queue)); @@ -181,6 +183,10 @@ osd_work_queue *osd_work_queue_alloc(int flags) else queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? (numprocs - 1) : 1; + if (osdworkqueuemaxthreads != NULL && sscanf(osdworkqueuemaxthreads, "%d", &threadnum) == 1 && queue->threads > threadnum) + queue->threads = threadnum; + + // clamp to the maximum queue->threads = MIN(queue->threads, WORK_MAX_THREADS); @@ -564,17 +570,21 @@ void osd_work_item_release(osd_work_item *item) static int effective_num_processors(void) { - char *procsoverride; - int numprocs = 0; int physprocs = osd_get_num_processors(); // osd_num_processors == 0 for 'auto' if (osd_num_processors > 0) + { return MIN(4 * physprocs, osd_num_processors); + } else { + char *procsoverride; + int numprocs = 0; + // if the OSDPROCESSORS environment variable is set, use that value if valid - procsoverride = osd_getenv(SDLENV_PROCESSORS); + // note that we permit more than the real number of processors for testing + procsoverride = osd_getenv(ENV_PROCESSORS); if (procsoverride != NULL && sscanf(procsoverride, "%d", &numprocs) == 1 && numprocs > 0) return MIN(4 * physprocs, numprocs); @@ -593,7 +603,7 @@ static UINT32 effective_cpu_mask(int index) char buf[5]; UINT32 mask = 0xFFFF; - s = osd_getenv(SDLENV_CPUMASKS); + s = osd_getenv(ENV_CPUMASKS); if (s != NULL && strcmp(s,"none")) { if (!strcmp(s,"auto")) diff --git a/src/osd/windows/windows.mak b/src/osd/windows/windows.mak index 9495e40f144..bceed636679 100644 --- a/src/osd/windows/windows.mak +++ b/src/osd/windows/windows.mak @@ -340,6 +340,7 @@ OSDCOREOBJS = \ $(WINOBJ)/winsocket.o \ $(WINOBJ)/winwork.o \ $(WINOBJ)/winptty.o \ + $(WINOBJ)/winos.o \ #------------------------------------------------- diff --git a/src/osd/windows/winos.c b/src/osd/windows/winos.c new file mode 100644 index 00000000000..8d32c5ec62b --- /dev/null +++ b/src/osd/windows/winos.c @@ -0,0 +1,35 @@ +//============================================================ +// +// winos.c - Win32 OS specific low level code +// +//============================================================ + +#define WIN32_LEAN_AND_MEAN +#include + +// MAME headers +#include "osdcore.h" + +//============================================================ +// osd_num_processors +//============================================================ + +int osd_get_num_processors(void) +{ + SYSTEM_INFO info; + + // otherwise, fetch the info from the system + GetSystemInfo(&info); + + // max out at 4 for now since scaling above that seems to do poorly + return MIN(info.dwNumberOfProcessors, 4); +} + +//============================================================ +// osd_getenv +//============================================================ + +char *osd_getenv(const char *name) +{ + return getenv(name); +} \ No newline at end of file diff --git a/src/osd/windows/winos.h b/src/osd/windows/winos.h new file mode 100644 index 00000000000..bef3d799760 --- /dev/null +++ b/src/osd/windows/winos.h @@ -0,0 +1,32 @@ +//============================================================ +// +// winos.h - Win32 OS specific low level code +// +//============================================================ + +/*----------------------------------------------------------------------------- + osd_num_processors: return the number of processors + + Parameters: + + None. + + Return value: + + Number of processors +-----------------------------------------------------------------------------*/ +int osd_get_num_processors(void); + + +/*----------------------------------------------------------------------------- + osd_getenv: return pointer to environment variable + + Parameters: + + name - name of environment variable + + Return value: + + pointer to value +-----------------------------------------------------------------------------*/ +char *osd_getenv(const char *name); \ No newline at end of file diff --git a/src/osd/windows/winwork.c b/src/osd/windows/winwork.c index af237351453..ab13a1eaa67 100644 --- a/src/osd/windows/winwork.c +++ b/src/osd/windows/winwork.c @@ -21,6 +21,7 @@ #include "osdcore.h" #include "winsync.h" +#include "winos.h" #include "eminline.h" @@ -35,6 +36,9 @@ // PARAMETERS //============================================================ +#define ENV_PROCESSORS "OSDPROCESSORS" +#define ENV_WORKQUEUEMAXTHREADS "OSDWORKQUEUEMAXTHREADS" + #define SPIN_LOOP_TIME (osd_ticks_per_second() / 1000) @@ -156,7 +160,7 @@ osd_work_queue *osd_work_queue_alloc(int flags) int numprocs = effective_num_processors(); osd_work_queue *queue; int threadnum; - TCHAR *osdworkqueuemaxthreads = _tgetenv(_T("OSDWORKQUEUEMAXTHREADS")); + char *osdworkqueuemaxthreads = osd_getenv("OSDWORKQUEUEMAXTHREADS"); // allocate a new queue queue = (osd_work_queue *)osd_malloc(sizeof(*queue)); @@ -187,7 +191,7 @@ osd_work_queue *osd_work_queue_alloc(int flags) else queue->threads = (flags & WORK_QUEUE_FLAG_MULTI) ? numprocs : 1; - if (osdworkqueuemaxthreads != NULL && _stscanf(osdworkqueuemaxthreads, _T("%d"), &threadnum) == 1 && queue->threads > threadnum) + if (osdworkqueuemaxthreads != NULL && sscanf(osdworkqueuemaxthreads, "%d", &threadnum) == 1 && queue->threads > threadnum) queue->threads = threadnum; // multi-queues with high frequency items should top out at 4 for now @@ -349,12 +353,13 @@ void osd_work_queue_free(osd_work_queue *queue) { work_thread_info *thread = &queue->thread[threadnum]; osd_ticks_t total = thread->runtime + thread->waittime + thread->spintime; - printf("Thread %d: items=%9d run=%5.2f%% (%5.2f%%) spin=%5.2f%% wait/other=%5.2f%%\n", + printf("Thread %d: items=%9d run=%5.2f%% (%5.2f%%) spin=%5.2f%% wait/other=%5.2f%% total=%9d\n", threadnum, thread->itemsdone, (double)thread->runtime * 100.0 / (double)total, (double)thread->actruntime * 100.0 / (double)total, (double)thread->spintime * 100.0 / (double)total, - (double)thread->waittime * 100.0 / (double)total); + (double)thread->waittime * 100.0 / (double)total, + (UINT32) total); } #endif } @@ -559,26 +564,26 @@ void osd_work_item_release(osd_work_item *item) static int effective_num_processors(void) { - SYSTEM_INFO info; - // fetch the info from the system - GetSystemInfo(&info); + int physprocs = osd_get_num_processors(); + // osd_num_processors == 0 for 'auto' if (osd_num_processors > 0) { - return MIN(info.dwNumberOfProcessors * 4, osd_num_processors); + return MIN(4 * physprocs, osd_num_processors); } else { - TCHAR *procsoverride; + char *procsoverride; int numprocs = 0; // if the OSDPROCESSORS environment variable is set, use that value if valid // note that we permit more than the real number of processors for testing - procsoverride = _tgetenv(_T("OSDPROCESSORS")); - if (procsoverride != NULL && _stscanf(procsoverride, _T("%d"), &numprocs) == 1 && numprocs > 0) - return MIN(info.dwNumberOfProcessors * 4, numprocs); + procsoverride = osd_getenv(ENV_PROCESSORS); + if (procsoverride != NULL && sscanf(procsoverride, "%d", &numprocs) == 1 && numprocs > 0) + return MIN(4 * physprocs, numprocs); - return info.dwNumberOfProcessors; + // otherwise, return the info from the system + return physprocs; } }