/[svn]/linuxsampler/trunk/src/linuxsampler.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/linuxsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1541 - (hide annotations) (download)
Tue Dec 4 18:09:26 2007 UTC (16 years, 4 months ago) by iliev
File size: 18085 byte(s)
- Added support for monitoring the total number of active disk streams
  (new LSCP commands: GET TOTAL_STREAM_COUNT,
  SUBSCRIBE TOTAL_STREAM_COUNT, UNSUBSCRIBE TOTAL_STREAM_COUNT)

1 schoenebeck 9 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 capela 1012 * Copyright (C) 2003-2004 by Benno Senoner and Christian Schoenebeck *
6     * Copyright (C) 2005-2007 Christian Schoenebeck *
7 schoenebeck 9 * *
8     * 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 *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #include <getopt.h>
25     #include <signal.h>
26    
27 senoner 1502 #if defined(WIN32)
28     // require at least Windows 2000 for the GlobalMemoryStatusEx() call
29     #define _WIN32_WINNT 0x0500
30     #endif
31    
32 schoenebeck 53 #include "Sampler.h"
33 schoenebeck 1424 #include "common/global_private.h"
34 schoenebeck 420 #include "engines/EngineFactory.h"
35 schoenebeck 1375 #include "plugins/InstrumentEditorFactory.h"
36 schoenebeck 207 #include "drivers/midi/MidiInputDeviceFactory.h"
37 schoenebeck 203 #include "drivers/audio/AudioOutputDeviceFactory.h"
38 senkov 325 #include "engines/gig/Profiler.h"
39 schoenebeck 35 #include "network/lscpserver.h"
40 schoenebeck 271 #include "common/stacktrace.h"
41 schoenebeck 319 #include "common/Features.h"
42 persson 1534 #include "common/atomic.h"
43 schoenebeck 9
44 schoenebeck 53 using namespace LinuxSampler;
45    
46 schoenebeck 211 Sampler* pSampler = NULL;
47     LSCPServer* pLSCPServer = NULL;
48 senoner 1502 #if defined(WIN32)
49     // inet_aton seems missing under WIN32
50     #ifndef INADDR_NONE
51     #define INADDR_NONE 0xffffffff
52     #endif
53    
54     int inet_aton(const char *cp, struct in_addr *addr)
55     {
56     addr->s_addr = inet_addr(cp);
57     return (addr->s_addr == INADDR_NONE) ? 0 : 1;
58     }
59    
60     #else
61 wylder 816 pid_t main_pid;
62 senoner 1502 #endif
63 schoenebeck 420 bool bPrintStatistics = false;
64 senkov 325 bool profile = false;
65     bool tune = true;
66 senkov 667 unsigned long int lscp_addr;
67     unsigned short int lscp_port;
68 schoenebeck 9
69     void parse_options(int argc, char **argv);
70     void signal_handler(int signal);
71 schoenebeck 271 void kill_app();
72 persson 1534 static atomic_t running = ATOMIC_INIT(1);
73 schoenebeck 9
74     int main(int argc, char **argv) {
75    
76 schoenebeck 271 // initialize the stack trace mechanism with our binary file
77     StackTraceInit(argv[0], -1);
78    
79 senoner 1502 #if defined(WIN32)
80     // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes)
81     SYSTEM_INFO siSysInfo;
82     long physical_memory;
83     GetSystemInfo(&siSysInfo);
84     dmsg(1,("page size=%d\n", siSysInfo.dwPageSize));
85    
86     MEMORYSTATUSEX statex;
87     statex.dwLength = sizeof (statex);
88     GlobalMemoryStatusEx (&statex);
89     dmsg(1, ("There are %*I64d total Kbytes of physical memory.\n",
90     8, statex.ullTotalPhys));
91     dmsg(1, ("There are %*I64d free Kbytes of physical memory.\n",
92     8, statex.ullAvailPhys));
93     physical_memory = statex.ullTotalPhys;
94    
95     HANDLE hProcess = GetCurrentProcess();
96    
97     unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize;
98     unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize;
99     unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize;
100     int res;
101    
102     res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize);
103    
104     RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024;
105     RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize;
106    
107     for(;;) {
108     dmsg(2,("TRYING VALUES RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
109     res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize);
110     dmsg(2,("AFTER SET: res = %d RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
111    
112     res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize);
113     dmsg(2,("AFTER GET: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
114    
115     if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) {
116     dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n"));
117     break;
118     }
119    
120     RequestedMinimumWorkingSetSize -= 10*1024*1024;
121     if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;
122     }
123    
124     dmsg(1,("AFTER GetProcessWorkingSetSize: res = %d MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
125     #endif
126    
127 persson 1534 #if !defined(WIN32)
128 wylder 816 main_pid = getpid();
129 senoner 1502 #endif
130 schoenebeck 271
131 schoenebeck 9 // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
132     signal(SIGINT, signal_handler);
133    
134 senoner 1502 #if defined(WIN32)
135     // FIXME: sigaction() not supported on WIN32, we ignore it for now
136     #else
137 schoenebeck 271 // register signal handler for all unusual signals
138     // (we will print the stack trace and exit)
139     struct sigaction sact;
140     sigemptyset(&sact.sa_mask);
141     sact.sa_flags = 0;
142     sact.sa_handler = signal_handler;
143     sigaction(SIGSEGV, &sact, NULL);
144     sigaction(SIGBUS, &sact, NULL);
145     sigaction(SIGILL, &sact, NULL);
146     sigaction(SIGFPE, &sact, NULL);
147     sigaction(SIGUSR1, &sact, NULL);
148     sigaction(SIGUSR2, &sact, NULL);
149 senoner 1502 #endif
150 schoenebeck 271
151 senkov 667 lscp_addr = htonl(LSCP_ADDR);
152     lscp_port = htons(LSCP_PORT);
153    
154 schoenebeck 9 // parse and assign command line options
155 senkov 325 parse_options(argc, argv);
156 schoenebeck 9
157 schoenebeck 207 dmsg(1,("LinuxSampler %s\n", VERSION));
158 capela 1012 dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));
159     dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n"));
160 schoenebeck 123
161 schoenebeck 579 if (tune) {
162 persson 425 // detect and print system / CPU specific features
163     Features::detect();
164 schoenebeck 579 dmsg(1,("Detected features: %s\n", Features::featuresAsString().c_str()));
165     // prevent slow denormal FPU modes
166     Features::enableDenormalsAreZeroMode();
167 senkov 325 }
168 schoenebeck 319
169 schoenebeck 53 // create LinuxSampler instance
170 schoenebeck 123 dmsg(1,("Creating Sampler..."));
171 schoenebeck 53 pSampler = new Sampler;
172 schoenebeck 123 dmsg(1,("OK\n"));
173 schoenebeck 53
174 schoenebeck 900 dmsg(1,("Registered sampler engines: %s\n", EngineFactory::AvailableEngineTypesAsString().c_str()));
175 schoenebeck 207 dmsg(1,("Registered MIDI input drivers: %s\n", MidiInputDeviceFactory::AvailableDriversAsString().c_str()));
176 schoenebeck 123 dmsg(1,("Registered audio output drivers: %s\n", AudioOutputDeviceFactory::AvailableDriversAsString().c_str()));
177 schoenebeck 1212 dmsg(1,("Registered instrument editors: %s\n", InstrumentEditorFactory::AvailableEditorsAsString().c_str()));
178 schoenebeck 123
179 schoenebeck 53 // start LSCP network server
180 senkov 667 struct in_addr addr;
181     addr.s_addr = lscp_addr;
182     dmsg(1,("Starting LSCP network server (%s:%d)...", inet_ntoa(addr), ntohs(lscp_port)));
183     pLSCPServer = new LSCPServer(pSampler, lscp_addr, lscp_port);
184 schoenebeck 53 pLSCPServer->StartThread();
185 schoenebeck 211 pLSCPServer->WaitUntilInitialized();
186 schoenebeck 12 dmsg(1,("OK\n"));
187 schoenebeck 9
188 senkov 325 if (profile)
189     {
190 persson 425 dmsg(1,("Calibrating profiler..."));
191 iliev 1161 LinuxSampler::gig::Profiler::Calibrate();
192     LinuxSampler::gig::Profiler::Reset();
193     LinuxSampler::gig::Profiler::enable();
194 persson 425 dmsg(1,("OK\n"));
195 senkov 325 }
196    
197 schoenebeck 563 printf("LinuxSampler initialization completed. :-)\n\n");
198 schoenebeck 9
199 senkov 360 std::list<LSCPEvent::event_t> rtEvents;
200     rtEvents.push_back(LSCPEvent::event_voice_count);
201     rtEvents.push_back(LSCPEvent::event_stream_count);
202     rtEvents.push_back(LSCPEvent::event_buffer_fill);
203 iliev 778 rtEvents.push_back(LSCPEvent::event_total_voice_count);
204 senkov 360
205 persson 1534 while (atomic_read(&running)) {
206 schoenebeck 420 if (bPrintStatistics) {
207 persson 840 const std::set<Engine*>& engines = EngineFactory::EngineInstances();
208 schoenebeck 420 std::set<Engine*>::iterator itEngine = engines.begin();
209     for (int i = 0; itEngine != engines.end(); itEngine++, i++) {
210     Engine* pEngine = *itEngine;
211     printf("Engine %d) Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d)\n", i,
212     pEngine->VoiceCount(), pEngine->VoiceCountMax(),
213     pEngine->DiskStreamCount(), pEngine->DiskStreamCountMax()
214     );
215     fflush(stdout);
216     }
217     }
218 persson 425
219 persson 1534 sleep(1);
220     if (profile)
221     {
222     unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
223     unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
224     if (bv != 0)
225     {
226     printf(" BogoVoices: %i \r", bv);
227     fflush(stdout);
228     }
229     }
230 persson 425
231 persson 1534 if (LSCPServer::EventSubscribers(rtEvents))
232     {
233     LSCPServer::LockRTNotify();
234     std::map<uint,SamplerChannel*> channels = pSampler->GetSamplerChannels();
235     std::map<uint,SamplerChannel*>::iterator iter = channels.begin();
236     for (; iter != channels.end(); iter++) {
237     SamplerChannel* pSamplerChannel = iter->second;
238     EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
239     if (!pEngineChannel) continue;
240     Engine* pEngine = pEngineChannel->GetEngine();
241     if (!pEngine) continue;
242     pSampler->fireVoiceCountChanged(iter->first, pEngineChannel->GetVoiceCount());
243     pSampler->fireStreamCountChanged(iter->first, pEngineChannel->GetDiskStreamCount());
244     pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());
245 iliev 1541 pSampler->fireTotalStreamCountChanged(pSampler->GetDiskStreamCount());
246 persson 1534 pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount());
247     }
248     LSCPServer::UnlockRTNotify();
249     }
250 senoner 10 }
251 persson 1534 if (pLSCPServer) pLSCPServer->StopThread();
252     // the delete order here is important: the Sampler
253     // destructor sends notifications to the lscpserver
254     if (pSampler) delete pSampler;
255     if (pLSCPServer) delete pLSCPServer;
256     #if HAVE_SQLITE3
257     InstrumentsDb::Destroy();
258     #endif
259     printf("LinuxSampler stopped due to SIGINT.\n");
260 schoenebeck 9 return EXIT_SUCCESS;
261     }
262    
263 schoenebeck 271 void signal_handler(int iSignal) {
264     switch (iSignal) {
265 persson 1534 case SIGINT:
266     atomic_set(&running, 0);
267 schoenebeck 271 return;
268 senoner 1502 #if defined(WIN32)
269     // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
270     #else
271 schoenebeck 271 case SIGSEGV:
272     std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
273     break;
274     case SIGBUS:
275     std::cerr << ">>> FATAL ERROR: Access to undefined portion of a memory object (SIGBUS) occured! <<<\n" << std::flush;
276     break;
277     case SIGILL:
278     std::cerr << ">>> FATAL ERROR: Illegal instruction (SIGILL) occured! <<<\n" << std::flush;
279     break;
280     case SIGFPE:
281     std::cerr << ">>> FATAL ERROR: Erroneous arithmetic operation (SIGFPE) occured! <<<\n" << std::flush;
282     break;
283     case SIGUSR1:
284     std::cerr << ">>> User defined signal 1 (SIGUSR1) received <<<\n" << std::flush;
285     break;
286     case SIGUSR2:
287     std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
288     break;
289 senoner 1502 #endif
290 schoenebeck 271 default: { // this should never happen, as we register for the signals we want
291     std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
292     break;
293     }
294 schoenebeck 9 }
295 schoenebeck 271 signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
296     std::cerr << "Showing stack trace...\n" << std::flush;
297     StackTrace();
298     sleep(2);
299     std::cerr << "Killing LinuxSampler...\n" << std::flush;
300     kill_app(); // Use abort() if we want to generate a core dump.
301 schoenebeck 9 }
302    
303 schoenebeck 271 void kill_app() {
304 senoner 1502 #if defined(WIN32)
305     // FIXME: do we need to do anything at this point under WIN32 ? is exit(0) ok ?
306     exit(0);
307     #else
308 wylder 816 kill(main_pid, SIGKILL);
309 senoner 1502 #endif
310 schoenebeck 271 }
311    
312 senkov 325 void parse_options(int argc, char **argv) {
313 schoenebeck 9 int res;
314     int option_index = 0;
315     static struct option long_options[] =
316     {
317     {"help",0,0,0},
318 senkov 325 {"version",0,0,0},
319     {"profile",0,0,0},
320     {"no-tune",0,0,0},
321 schoenebeck 420 {"statistics",0,0,0},
322 iliev 1208 {"instruments-db-location",1,0,0},
323 iliev 1187 {"create-instruments-db",1,0,0},
324 senkov 667 {"lscp-addr",1,0,0},
325     {"lscp-port",1,0,0},
326 schoenebeck 9 {0,0,0,0}
327     };
328    
329     while (true) {
330 schoenebeck 361 /*
331     Stephane Letz : letz@grame.fr
332     getopt_long_only does not exist on OSX : replaced by getopt_long for now.
333     */
334     res = getopt_long(argc, argv, "", long_options, &option_index);
335 schoenebeck 9 if(res == -1) break;
336     if (res == 0) {
337     switch(option_index) {
338 senkov 325 case 0: // --help
339     printf("usage: linuxsampler [OPTIONS]\n\n");
340 iliev 1208 printf("--help prints this message\n");
341     printf("--version prints version information\n");
342     printf("--profile profile synthesis algorithms\n");
343     printf("--no-tune disable assembly optimization\n");
344     printf("--statistics periodically prints statistics\n");
345     printf("--lscp-addr set LSCP address (default: any)\n");
346     printf("--lscp-port set LSCP port (default: 8888)\n");
347     printf("--create-instruments-db creates an instruments DB\n");
348     printf("--instruments-db-location specifies the instruments DB file\n");
349 senkov 325 exit(EXIT_SUCCESS);
350 schoenebeck 9 break;
351 senkov 325 case 1: // --version
352 schoenebeck 328 printf("LinuxSampler %s\n", VERSION);
353     exit(EXIT_SUCCESS);
354 schoenebeck 9 break;
355 senkov 325 case 2: // --profile
356 persson 425 profile = true;
357 schoenebeck 20 break;
358 senkov 325 case 3: // --no-tune
359 persson 425 tune = false;
360 schoenebeck 9 break;
361 schoenebeck 420 case 4: // --statistics
362     bPrintStatistics = true;
363     break;
364 iliev 1208 case 5: // --instruments-db-location
365 iliev 1187 #if HAVE_SQLITE3
366     try {
367     if (optarg) {
368 iliev 1208 struct stat statBuf;
369     int res = stat(optarg, &statBuf);
370    
371     if (res) {
372     std::stringstream ss;
373 schoenebeck 1364 ss << "Failed to stat `" << optarg << "`: " << strerror(errno);
374 iliev 1208 throw Exception(ss.str());
375     }
376    
377     if (!S_ISREG(statBuf.st_mode)) {
378     std::stringstream ss;
379     ss << "`" << optarg << "` is not a regular file";
380     throw Exception(ss.str());
381     }
382    
383     InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));
384     }
385     } catch(Exception e) {
386 schoenebeck 1364 std::cerr << "Could not open instruments DB file: "
387     << e.Message() << std::endl;
388     exit(EXIT_FAILURE);
389 iliev 1208 }
390 schoenebeck 1364 break;
391 iliev 1208 #else
392     std::cerr << "LinuxSampler was not build with ";
393 schoenebeck 1364 std::cerr << "instruments database support!\n";
394     exit(EXIT_FAILURE);
395     break;
396     #endif
397 iliev 1208 case 6: // --create-instruments-db
398     #if HAVE_SQLITE3
399     try {
400     if (optarg) {
401 iliev 1187 std::cout << "Creating instruments database..." << std::endl;
402     InstrumentsDb::CreateInstrumentsDb(String(optarg));
403     InstrumentsDb::Destroy();
404     std::cout << "Done" << std::endl;
405     }
406     } catch(Exception e) {
407     std::cerr << e.Message() << std::endl;
408     exit(EXIT_FAILURE);
409     return;
410     }
411    
412     exit(EXIT_SUCCESS);
413     return;
414     #else
415     std::cerr << "Failed to create the database. LinuxSampler was ";
416 schoenebeck 1364 std::cerr << "not build with instruments database support!\n";
417 iliev 1187 exit(EXIT_FAILURE);
418     return;
419 schoenebeck 1364 #endif
420 iliev 1208 case 7: // --lscp-addr
421 iliev 1187 struct in_addr addr;
422     if (inet_aton(optarg, &addr) == 0)
423     printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");
424     else
425     lscp_addr = addr.s_addr;
426 senkov 667 break;
427 iliev 1208 case 8: // --lscp-port
428 iliev 1187 long unsigned int port = 0;
429     if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))
430     printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");
431     else
432     lscp_port = htons(port);
433 senkov 667 break;
434 schoenebeck 9 }
435     }
436     }
437 senkov 325 }

  ViewVC Help
Powered by ViewVC