2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005-2007 Christian Schoenebeck * |
* Copyright (C) 2005-2009 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
39 |
#include "network/lscpserver.h" |
#include "network/lscpserver.h" |
40 |
#include "common/stacktrace.h" |
#include "common/stacktrace.h" |
41 |
#include "common/Features.h" |
#include "common/Features.h" |
42 |
|
#include "common/atomic.h" |
43 |
|
|
44 |
using namespace LinuxSampler; |
using namespace LinuxSampler; |
45 |
|
|
57 |
return (addr->s_addr == INADDR_NONE) ? 0 : 1; |
return (addr->s_addr == INADDR_NONE) ? 0 : 1; |
58 |
} |
} |
59 |
|
|
|
DWORD main_thread; |
|
60 |
#else |
#else |
|
pthread_t main_thread; |
|
61 |
pid_t main_pid; |
pid_t main_pid; |
62 |
#endif |
#endif |
63 |
bool bPrintStatistics = false; |
bool bPrintStatistics = false; |
64 |
bool profile = false; |
bool profile = false; |
65 |
bool tune = true; |
bool tune = true; |
66 |
|
static bool bShowStackTrace = false; |
67 |
unsigned long int lscp_addr; |
unsigned long int lscp_addr; |
68 |
unsigned short int lscp_port; |
unsigned short int lscp_port; |
69 |
|
|
70 |
void parse_options(int argc, char **argv); |
void parse_options(int argc, char **argv); |
71 |
void signal_handler(int signal); |
void signal_handler(int signal); |
72 |
void kill_app(); |
void kill_app(); |
73 |
|
static atomic_t running = ATOMIC_INIT(1); |
74 |
|
|
75 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
76 |
|
|
77 |
|
lscp_addr = htonl(LSCP_ADDR); |
78 |
|
lscp_port = htons(LSCP_PORT); |
79 |
|
|
80 |
|
#if !defined(WIN32) |
81 |
|
main_pid = getpid(); |
82 |
|
#endif |
83 |
|
|
84 |
|
// parse and assign command line options |
85 |
|
parse_options(argc, argv); |
86 |
|
|
87 |
|
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
88 |
|
signal(SIGINT, signal_handler); |
89 |
|
|
90 |
// initialize the stack trace mechanism with our binary file |
// initialize the stack trace mechanism with our binary file |
91 |
StackTraceInit(argv[0], -1); |
// (if requested by command line option) |
92 |
|
if (bShowStackTrace) { |
93 |
|
#if defined(WIN32) |
94 |
|
// FIXME: sigaction() not supported on WIN32, we ignore it for now |
95 |
|
#else |
96 |
|
StackTraceInit(argv[0], -1); |
97 |
|
// register signal handler for all unusual signals |
98 |
|
// (we will print the stack trace and exit) |
99 |
|
struct sigaction sact; |
100 |
|
sigemptyset(&sact.sa_mask); |
101 |
|
sact.sa_flags = 0; |
102 |
|
sact.sa_handler = signal_handler; |
103 |
|
sigaction(SIGSEGV, &sact, NULL); |
104 |
|
sigaction(SIGBUS, &sact, NULL); |
105 |
|
sigaction(SIGILL, &sact, NULL); |
106 |
|
sigaction(SIGFPE, &sact, NULL); |
107 |
|
sigaction(SIGUSR1, &sact, NULL); |
108 |
|
sigaction(SIGUSR2, &sact, NULL); |
109 |
|
#endif |
110 |
|
} |
111 |
|
|
112 |
|
dmsg(1,("LinuxSampler %s\n", VERSION)); |
113 |
|
dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n")); |
114 |
|
dmsg(1,("Copyright (C) 2005-2009 Christian Schoenebeck\n")); |
115 |
|
|
116 |
#if defined(WIN32) |
#if defined(WIN32) |
117 |
|
#if 0 |
118 |
// some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes) |
// some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes) |
119 |
SYSTEM_INFO siSysInfo; |
SYSTEM_INFO siSysInfo; |
120 |
long physical_memory; |
long physical_memory; |
121 |
GetSystemInfo(&siSysInfo); |
GetSystemInfo(&siSysInfo); |
122 |
dmsg(1,("page size=%d\n", siSysInfo.dwPageSize)); |
dmsg(2,("page size=%d\n", siSysInfo.dwPageSize)); |
123 |
|
|
124 |
MEMORYSTATUSEX statex; |
MEMORYSTATUSEX statex; |
125 |
statex.dwLength = sizeof (statex); |
statex.dwLength = sizeof (statex); |
126 |
GlobalMemoryStatusEx (&statex); |
GlobalMemoryStatusEx (&statex); |
127 |
dmsg(1, ("There are %*I64d total Kbytes of physical memory.\n", |
dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n", |
128 |
8, statex.ullTotalPhys)); |
8, statex.ullTotalPhys)); |
129 |
dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n", |
dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n", |
130 |
8, statex.ullAvailPhys)); |
8, statex.ullAvailPhys)); |
131 |
physical_memory = statex.ullTotalPhys; |
physical_memory = statex.ullTotalPhys; |
132 |
|
|
159 |
if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break; |
if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break; |
160 |
} |
} |
161 |
|
|
162 |
dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); |
dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize)); |
|
#endif |
|
|
|
|
|
#if defined(WIN32) |
|
|
main_thread = GetCurrentThreadId(); |
|
|
#else |
|
|
main_pid = getpid(); |
|
|
main_thread = pthread_self(); |
|
|
#endif |
|
|
|
|
|
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
|
|
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); |
|
163 |
#endif |
#endif |
164 |
|
#endif // WIN32 |
|
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")); |
|
165 |
|
|
166 |
if (tune) { |
if (tune) { |
167 |
// detect and print system / CPU specific features |
// detect and print system / CPU specific features |
171 |
Features::enableDenormalsAreZeroMode(); |
Features::enableDenormalsAreZeroMode(); |
172 |
} |
} |
173 |
|
|
174 |
|
dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off")); |
175 |
|
|
176 |
// create LinuxSampler instance |
// create LinuxSampler instance |
177 |
dmsg(1,("Creating Sampler...")); |
dmsg(1,("Creating Sampler...")); |
178 |
pSampler = new Sampler; |
pSampler = new Sampler; |
203 |
|
|
204 |
printf("LinuxSampler initialization completed. :-)\n\n"); |
printf("LinuxSampler initialization completed. :-)\n\n"); |
205 |
|
|
206 |
std::list<LSCPEvent::event_t> rtEvents; |
while (atomic_read(&running)) { |
|
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) { |
|
207 |
if (bPrintStatistics) { |
if (bPrintStatistics) { |
208 |
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
const std::set<Engine*>& engines = EngineFactory::EngineInstances(); |
209 |
std::set<Engine*>::iterator itEngine = engines.begin(); |
std::set<Engine*>::iterator itEngine = engines.begin(); |
217 |
} |
} |
218 |
} |
} |
219 |
|
|
220 |
sleep(1); |
sleep(1); |
221 |
if (profile) |
if (profile) |
222 |
{ |
{ |
223 |
unsigned int samplingFreq = 48000; //FIXME: hardcoded for now |
unsigned int samplingFreq = 48000; //FIXME: hardcoded for now |
224 |
unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq); |
unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq); |
225 |
if (bv != 0) |
if (bv != 0) |
226 |
{ |
{ |
227 |
printf(" BogoVoices: %i \r", bv); |
printf(" BogoVoices: %i \r", bv); |
228 |
fflush(stdout); |
fflush(stdout); |
229 |
} |
} |
230 |
} |
} |
|
|
|
|
if (LSCPServer::EventSubscribers(rtEvents)) |
|
|
{ |
|
|
LSCPServer::LockRTNotify(); |
|
|
std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels(); |
|
|
std::map<uint,SamplerChannel*>::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(); |
|
|
} |
|
231 |
|
|
232 |
|
pSampler->fireStatistics(); |
233 |
} |
} |
234 |
|
if (pLSCPServer) pLSCPServer->StopThread(); |
235 |
|
// the delete order here is important: the Sampler |
236 |
|
// destructor sends notifications to the lscpserver |
237 |
|
if (pSampler) delete pSampler; |
238 |
|
if (pLSCPServer) delete pLSCPServer; |
239 |
|
printf("LinuxSampler stopped due to SIGINT.\n"); |
240 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
241 |
} |
} |
242 |
|
|
243 |
void signal_handler(int iSignal) { |
void signal_handler(int iSignal) { |
244 |
switch (iSignal) { |
switch (iSignal) { |
245 |
case SIGINT: { |
case SIGINT: |
246 |
#if defined(WIN32) |
atomic_set(&running, 0); |
|
if( GetCurrentThreadId() == main_thread ) { |
|
|
#else |
|
|
if (pthread_equal(pthread_self(), main_thread)) { |
|
|
#endif |
|
|
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); |
|
|
} |
|
247 |
return; |
return; |
|
} |
|
248 |
#if defined(WIN32) |
#if defined(WIN32) |
249 |
// FIXME: under WIN32 we ignore the signals below due to the missing sigaction call |
// FIXME: under WIN32 we ignore the signals below due to the missing sigaction call |
250 |
#else |
#else |
273 |
} |
} |
274 |
} |
} |
275 |
signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions |
signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions |
276 |
std::cerr << "Showing stack trace...\n" << std::flush; |
if (bShowStackTrace) { |
277 |
StackTrace(); |
std::cerr << "Showing stack trace...\n" << std::flush; |
278 |
sleep(2); |
StackTrace(); |
279 |
|
sleep(2); |
280 |
|
} |
281 |
std::cerr << "Killing LinuxSampler...\n" << std::flush; |
std::cerr << "Killing LinuxSampler...\n" << std::flush; |
282 |
kill_app(); // Use abort() if we want to generate a core dump. |
kill_app(); // Use abort() if we want to generate a core dump. |
283 |
} |
} |
305 |
{"create-instruments-db",1,0,0}, |
{"create-instruments-db",1,0,0}, |
306 |
{"lscp-addr",1,0,0}, |
{"lscp-addr",1,0,0}, |
307 |
{"lscp-port",1,0,0}, |
{"lscp-port",1,0,0}, |
308 |
|
{"stacktrace",0,0,0}, |
309 |
{0,0,0,0} |
{0,0,0,0} |
310 |
}; |
}; |
311 |
|
|
329 |
printf("--lscp-port set LSCP port (default: 8888)\n"); |
printf("--lscp-port set LSCP port (default: 8888)\n"); |
330 |
printf("--create-instruments-db creates an instruments DB\n"); |
printf("--create-instruments-db creates an instruments DB\n"); |
331 |
printf("--instruments-db-location specifies the instruments DB file\n"); |
printf("--instruments-db-location specifies the instruments DB file\n"); |
332 |
|
printf("--stacktrace automatically shows stacktrace if crashes\n"); |
333 |
|
printf(" (broken on most systems at the moment)\n"); |
334 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
335 |
break; |
break; |
336 |
case 1: // --version |
case 1: // --version |
385 |
if (optarg) { |
if (optarg) { |
386 |
std::cout << "Creating instruments database..." << std::endl; |
std::cout << "Creating instruments database..." << std::endl; |
387 |
InstrumentsDb::CreateInstrumentsDb(String(optarg)); |
InstrumentsDb::CreateInstrumentsDb(String(optarg)); |
|
InstrumentsDb::Destroy(); |
|
388 |
std::cout << "Done" << std::endl; |
std::cout << "Done" << std::endl; |
389 |
} |
} |
390 |
} catch(Exception e) { |
} catch(Exception e) { |
401 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
402 |
return; |
return; |
403 |
#endif |
#endif |
404 |
case 7: // --lscp-addr |
case 7: { // --lscp-addr |
405 |
struct in_addr addr; |
struct in_addr addr; |
406 |
if (inet_aton(optarg, &addr) == 0) |
if (inet_aton(optarg, &addr) == 0) |
407 |
printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n"); |
printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n"); |
408 |
else |
else |
409 |
lscp_addr = addr.s_addr; |
lscp_addr = addr.s_addr; |
410 |
break; |
break; |
411 |
case 8: // --lscp-port |
} |
412 |
|
case 8: {// --lscp-port |
413 |
long unsigned int port = 0; |
long unsigned int port = 0; |
414 |
if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535)) |
if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535)) |
415 |
printf("WARNING: Failed to parse lscp-port argument, ignoring!\n"); |
printf("WARNING: Failed to parse lscp-port argument, ignoring!\n"); |
416 |
else |
else |
417 |
lscp_port = htons(port); |
lscp_port = htons(port); |
418 |
break; |
break; |
419 |
|
} |
420 |
|
case 9: // --stacktrace |
421 |
|
bShowStackTrace = true; |
422 |
|
break; |
423 |
} |
} |
424 |
} |
} |
425 |
} |
} |