--- linuxsampler/trunk/src/linuxsampler.cpp 2007/12/02 19:07:17 1534 +++ linuxsampler/trunk/src/linuxsampler.cpp 2009/06/01 18:50:06 1908 @@ -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-2009 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 * @@ -23,11 +23,17 @@ #include #include +#include #if defined(WIN32) // require at least Windows 2000 for the GlobalMemoryStatusEx() call +#if _WIN32_WINNT < 0x0500 +#ifdef _WIN32_WINNT +#undef _WIN32_WINNT +#endif #define _WIN32_WINNT 0x0500 #endif +#endif #include "Sampler.h" #include "common/global_private.h" @@ -63,6 +69,7 @@ bool bPrintStatistics = false; bool profile = false; bool tune = true; +static bool bShowStackTrace = false; unsigned long int lscp_addr; unsigned short int lscp_port; @@ -73,22 +80,59 @@ int main(int argc, char **argv) { + lscp_addr = htonl(LSCP_ADDR); + lscp_port = htons(LSCP_PORT); + + #if !defined(WIN32) + main_pid = getpid(); + #endif + + // parse and assign command line options + parse_options(argc, argv); + + // setting signal handler for catching SIGINT (thus e.g. ) + signal(SIGINT, signal_handler); + // initialize the stack trace mechanism with our binary file - StackTraceInit(argv[0], -1); + // (if requested by command line option) + if (bShowStackTrace) { + #if defined(WIN32) + // FIXME: sigaction() not supported on WIN32, we ignore it for now + #else + StackTraceInit(argv[0], -1); + // register signal handler for all unusual signals + // (we will print the stack trace and exit) + struct sigaction sact; + sigemptyset(&sact.sa_mask); + sact.sa_flags = 0; + sact.sa_handler = signal_handler; + sigaction(SIGSEGV, &sact, NULL); + sigaction(SIGBUS, &sact, NULL); + sigaction(SIGILL, &sact, NULL); + sigaction(SIGFPE, &sact, NULL); + sigaction(SIGUSR1, &sact, NULL); + sigaction(SIGUSR2, &sact, NULL); + #endif + } + + dmsg(1,("LinuxSampler %s\n", VERSION)); + dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n")); + dmsg(1,("Copyright (C) 2005-2009 Christian Schoenebeck\n")); #if defined(WIN32) + #if 0 // 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)); + dmsg(2,("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", + dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n", 8, statex.ullTotalPhys)); - dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n", + dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n", 8, statex.ullAvailPhys)); physical_memory = statex.ullTotalPhys; @@ -121,42 +165,9 @@ 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(); + dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); #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; - sigemptyset(&sact.sa_mask); - sact.sa_flags = 0; - sact.sa_handler = signal_handler; - sigaction(SIGSEGV, &sact, NULL); - sigaction(SIGBUS, &sact, NULL); - sigaction(SIGILL, &sact, NULL); - sigaction(SIGFPE, &sact, NULL); - sigaction(SIGUSR1, &sact, NULL); - sigaction(SIGUSR2, &sact, NULL); - #endif - - lscp_addr = htonl(LSCP_ADDR); - lscp_port = htons(LSCP_PORT); - - // parse and assign command line options - parse_options(argc, argv); - - 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")); + #endif // WIN32 if (tune) { // detect and print system / CPU specific features @@ -166,6 +177,8 @@ Features::enableDenormalsAreZeroMode(); } + dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off")); + // create LinuxSampler instance dmsg(1,("Creating Sampler...")); pSampler = new Sampler; @@ -196,12 +209,6 @@ 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 (atomic_read(&running)) { if (bPrintStatistics) { const std::set& engines = EngineFactory::EngineInstances(); @@ -228,33 +235,13 @@ } } - 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, pEngineChannel->GetVoiceCount()); - pSampler->fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount()); - pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage()); - pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount()); - } - LSCPServer::UnlockRTNotify(); - } + 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; -#if HAVE_SQLITE3 - InstrumentsDb::Destroy(); -#endif printf("LinuxSampler stopped due to SIGINT.\n"); return EXIT_SUCCESS; } @@ -292,9 +279,11 @@ } } signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions - std::cerr << "Showing stack trace...\n" << std::flush; - StackTrace(); - sleep(2); + if (bShowStackTrace) { + std::cerr << "Showing stack trace...\n" << std::flush; + StackTrace(); + sleep(2); + } std::cerr << "Killing LinuxSampler...\n" << std::flush; kill_app(); // Use abort() if we want to generate a core dump. } @@ -322,6 +311,7 @@ {"create-instruments-db",1,0,0}, {"lscp-addr",1,0,0}, {"lscp-port",1,0,0}, + {"stacktrace",0,0,0}, {0,0,0,0} }; @@ -345,6 +335,8 @@ printf("--lscp-port set LSCP port (default: 8888)\n"); printf("--create-instruments-db creates an instruments DB\n"); printf("--instruments-db-location specifies the instruments DB file\n"); + printf("--stacktrace automatically shows stacktrace if crashes\n"); + printf(" (broken on most systems at the moment)\n"); exit(EXIT_SUCCESS); break; case 1: // --version @@ -399,7 +391,6 @@ if (optarg) { std::cout << "Creating instruments database..." << std::endl; InstrumentsDb::CreateInstrumentsDb(String(optarg)); - InstrumentsDb::Destroy(); std::cout << "Done" << std::endl; } } catch(Exception e) { @@ -416,20 +407,25 @@ exit(EXIT_FAILURE); return; #endif - case 7: // --lscp-addr + case 7: { // --lscp-addr struct in_addr addr; if (inet_aton(optarg, &addr) == 0) printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n"); else lscp_addr = addr.s_addr; break; - case 8: // --lscp-port + } + case 8: {// --lscp-port long unsigned int port = 0; if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535)) printf("WARNING: Failed to parse lscp-port argument, ignoring!\n"); else lscp_port = htons(port); break; + } + case 9: // --stacktrace + bShowStackTrace = true; + break; } } }