--- linuxsampler/trunk/src/linuxsampler.cpp 2007/06/22 10:10:06 1248 +++ linuxsampler/trunk/src/linuxsampler.cpp 2008/09/06 16:44:42 1765 @@ -2,8 +2,8 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005-2007 Christian Schoenebeck * + * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005-2008 Christian Schoenebeck * * * * 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 * @@ -24,22 +24,42 @@ #include #include +#if defined(WIN32) +// require at least Windows 2000 for the GlobalMemoryStatusEx() call +#define _WIN32_WINNT 0x0500 +#endif + #include "Sampler.h" +#include "common/global_private.h" #include "engines/EngineFactory.h" -#include "engines/InstrumentEditorFactory.h" +#include "plugins/InstrumentEditorFactory.h" #include "drivers/midi/MidiInputDeviceFactory.h" #include "drivers/audio/AudioOutputDeviceFactory.h" #include "engines/gig/Profiler.h" #include "network/lscpserver.h" #include "common/stacktrace.h" #include "common/Features.h" +#include "common/atomic.h" using namespace LinuxSampler; Sampler* pSampler = NULL; LSCPServer* pLSCPServer = NULL; -pthread_t main_thread; +#if defined(WIN32) +// inet_aton seems missing under WIN32 +#ifndef INADDR_NONE +#define INADDR_NONE 0xffffffff +#endif + +int inet_aton(const char *cp, struct in_addr *addr) +{ + addr->s_addr = inet_addr(cp); + return (addr->s_addr == INADDR_NONE) ? 0 : 1; +} + +#else pid_t main_pid; +#endif bool bPrintStatistics = false; bool profile = false; bool tune = true; @@ -49,18 +69,71 @@ void parse_options(int argc, char **argv); void signal_handler(int signal); void kill_app(); +static atomic_t running = ATOMIC_INIT(1); int main(int argc, char **argv) { // initialize the stack trace mechanism with our binary file StackTraceInit(argv[0], -1); + #if defined(WIN32) + // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes) + SYSTEM_INFO siSysInfo; + long physical_memory; + GetSystemInfo(&siSysInfo); + dmsg(1,("page size=%d\n", siSysInfo.dwPageSize)); + + MEMORYSTATUSEX statex; + statex.dwLength = sizeof (statex); + GlobalMemoryStatusEx (&statex); + dmsg(1, ("There are %*I64d total Kbytes of physical memory.\n", + 8, statex.ullTotalPhys)); + dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n", + 8, statex.ullAvailPhys)); + physical_memory = statex.ullTotalPhys; + + HANDLE hProcess = GetCurrentProcess(); + + unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize; + unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize; + unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize; + int res; + + res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize); + + RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024; + RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize; + + for(;;) { + dmsg(2,("TRYING VALUES RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize)); + res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize); + dmsg(2,("AFTER SET: res = %d RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize)); + + res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize); + dmsg(2,("AFTER GET: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); + + if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) { + dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n")); + break; + } + + RequestedMinimumWorkingSetSize -= 10*1024*1024; + if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break; + } + + dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); + #endif + + #if !defined(WIN32) main_pid = getpid(); - main_thread = pthread_self(); + #endif // setting signal handler for catching SIGINT (thus e.g. ) signal(SIGINT, signal_handler); + #if defined(WIN32) + // FIXME: sigaction() not supported on WIN32, we ignore it for now + #else // register signal handler for all unusual signals // (we will print the stack trace and exit) struct sigaction sact; @@ -73,6 +146,7 @@ sigaction(SIGFPE, &sact, NULL); sigaction(SIGUSR1, &sact, NULL); sigaction(SIGUSR2, &sact, NULL); + #endif lscp_addr = htonl(LSCP_ADDR); lscp_port = htons(LSCP_PORT); @@ -82,7 +156,7 @@ dmsg(1,("LinuxSampler %s\n", VERSION)); dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n")); - dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n")); + dmsg(1,("Copyright (C) 2005-2008 Christian Schoenebeck\n")); if (tune) { // detect and print system / CPU specific features @@ -122,13 +196,7 @@ printf("LinuxSampler initialization completed. :-)\n\n"); - std::list rtEvents; - rtEvents.push_back(LSCPEvent::event_voice_count); - rtEvents.push_back(LSCPEvent::event_stream_count); - rtEvents.push_back(LSCPEvent::event_buffer_fill); - rtEvents.push_back(LSCPEvent::event_total_voice_count); - - while (true) { + while (atomic_read(&running)) { if (bPrintStatistics) { const std::set& engines = EngineFactory::EngineInstances(); std::set::iterator itEngine = engines.begin(); @@ -142,59 +210,37 @@ } } - sleep(1); - if (profile) - { - unsigned int samplingFreq = 48000; //FIXME: hardcoded for now - unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq); - if (bv != 0) - { - printf(" BogoVoices: %i \r", bv); - fflush(stdout); - } - } - - if (LSCPServer::EventSubscribers(rtEvents)) - { - LSCPServer::LockRTNotify(); - std::map channels = pSampler->GetSamplerChannels(); - std::map::iterator iter = channels.begin(); - for (; iter != channels.end(); iter++) { - SamplerChannel* pSamplerChannel = iter->second; - EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel(); - if (!pEngineChannel) continue; - Engine* pEngine = pEngineChannel->GetEngine(); - if (!pEngine) continue; - pSampler->fireVoiceCountChanged(iter->first, pEngine->VoiceCount()); - pSampler->fireStreamCountChanged(iter->first, pEngine->DiskStreamCount()); - pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); - pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount()); - } - LSCPServer::UnlockRTNotify(); - } + sleep(1); + if (profile) + { + unsigned int samplingFreq = 48000; //FIXME: hardcoded for now + unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq); + if (bv != 0) + { + printf(" BogoVoices: %i \r", bv); + fflush(stdout); + } + } + pSampler->fireStatistics(); } - + if (pLSCPServer) pLSCPServer->StopThread(); + // the delete order here is important: the Sampler + // destructor sends notifications to the lscpserver + if (pSampler) delete pSampler; + if (pLSCPServer) delete pLSCPServer; + printf("LinuxSampler stopped due to SIGINT.\n"); return EXIT_SUCCESS; } void signal_handler(int iSignal) { switch (iSignal) { - case SIGINT: { - if (pthread_equal(pthread_self(), main_thread)) { - if (pLSCPServer) pLSCPServer->StopThread(); - // the delete order here is important: the Sampler - // destructor sends notifications to the lscpserver - if (pSampler) delete pSampler; - if (pLSCPServer) delete pLSCPServer; -#if HAVE_SQLITE3 - InstrumentsDb::Destroy(); -#endif - printf("LinuxSampler stopped due to SIGINT.\n"); - exit(EXIT_SUCCESS); - } + case SIGINT: + atomic_set(&running, 0); return; - } + #if defined(WIN32) + // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call + #else case SIGSEGV: std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; break; @@ -213,6 +259,7 @@ case SIGUSR2: std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; break; + #endif default: { // this should never happen, as we register for the signals we want std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; break; @@ -227,7 +274,12 @@ } void kill_app() { + #if defined(WIN32) + // FIXME: do we need to do anything at this point under WIN32 ? is exit(0) ok ? + exit(0); + #else kill(main_pid, SIGKILL); + #endif } void parse_options(int argc, char **argv) { @@ -291,7 +343,7 @@ if (res) { std::stringstream ss; - ss << "Fail to stat `" << optarg << "`: " << strerror(errno); + ss << "Failed to stat `" << optarg << "`: " << strerror(errno); throw Exception(ss.str()); } @@ -304,23 +356,23 @@ InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg)); } } catch(Exception e) { - std::cerr << e.Message() << std::endl << std::endl; - return; + std::cerr << "Could not open instruments DB file: " + << e.Message() << std::endl; + exit(EXIT_FAILURE); } - - return; + break; #else std::cerr << "LinuxSampler was not build with "; - std::cerr << "instruments database support." <