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

Diff of /linuxsampler/trunk/src/linuxsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1364 by schoenebeck, Mon Oct 1 13:38:37 2007 UTC revision 1888 by persson, Sat Apr 18 09:26:45 2009 UTC
# Line 2  Line 2 
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  *
# Line 24  Line 24 
24  #include <getopt.h>  #include <getopt.h>
25  #include <signal.h>  #include <signal.h>
26    
27    #if defined(WIN32)
28    // require at least Windows 2000 for the GlobalMemoryStatusEx() call
29    #define _WIN32_WINNT 0x0500
30    #endif
31    
32  #include "Sampler.h"  #include "Sampler.h"
33    #include "common/global_private.h"
34  #include "engines/EngineFactory.h"  #include "engines/EngineFactory.h"
35  #include "engines/InstrumentEditorFactory.h"  #include "plugins/InstrumentEditorFactory.h"
36  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
37  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
38  #include "engines/gig/Profiler.h"  #include "engines/gig/Profiler.h"
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    
46  Sampler*    pSampler    = NULL;  Sampler*    pSampler    = NULL;
47  LSCPServer* pLSCPServer = NULL;  LSCPServer* pLSCPServer = NULL;
48  pthread_t   main_thread;  #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  pid_t       main_pid;  pid_t       main_pid;
62    #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      // initialize the stack trace mechanism with our binary file      lscp_addr = htonl(LSCP_ADDR);
78      StackTraceInit(argv[0], -1);      lscp_port = htons(LSCP_PORT);
79    
80        #if !defined(WIN32)
81      main_pid = getpid();      main_pid = getpid();
82      main_thread = pthread_self();      #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>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
88      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
89    
90      // register signal handler for all unusual signals      // initialize the stack trace mechanism with our binary file
91      // (we will print the stack trace and exit)      // (if requested by command line option)
92      struct sigaction sact;      if (bShowStackTrace) {
93      sigemptyset(&sact.sa_mask);          #if defined(WIN32)
94      sact.sa_flags   = 0;          // FIXME: sigaction() not supported on WIN32, we ignore it for now
95      sact.sa_handler = signal_handler;          #else
96      sigaction(SIGSEGV, &sact, NULL);          StackTraceInit(argv[0], -1);
97      sigaction(SIGBUS,  &sact, NULL);          // register signal handler for all unusual signals
98      sigaction(SIGILL,  &sact, NULL);          // (we will print the stack trace and exit)
99      sigaction(SIGFPE,  &sact, NULL);          struct sigaction sact;
100      sigaction(SIGUSR1, &sact, NULL);          sigemptyset(&sact.sa_mask);
101      sigaction(SIGUSR2, &sact, NULL);          sact.sa_flags   = 0;
102            sact.sa_handler = signal_handler;
103      lscp_addr = htonl(LSCP_ADDR);          sigaction(SIGSEGV, &sact, NULL);
104      lscp_port = htons(LSCP_PORT);          sigaction(SIGBUS,  &sact, NULL);
105            sigaction(SIGILL,  &sact, NULL);
106      // parse and assign command line options          sigaction(SIGFPE,  &sact, NULL);
107      parse_options(argc, argv);          sigaction(SIGUSR1, &sact, NULL);
108            sigaction(SIGUSR2, &sact, NULL);
109            #endif
110        }
111    
112      dmsg(1,("LinuxSampler %s\n", VERSION));      dmsg(1,("LinuxSampler %s\n", VERSION));
113      dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));      dmsg(1,("Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck\n"));
114      dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n"));      dmsg(1,("Copyright (C) 2005-2009 Christian Schoenebeck\n"));
115    
116        #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)
119        SYSTEM_INFO siSysInfo;
120        long physical_memory;
121        GetSystemInfo(&siSysInfo);
122        dmsg(2,("page size=%d\n", siSysInfo.dwPageSize));
123    
124        MEMORYSTATUSEX statex;
125            statex.dwLength = sizeof (statex);
126        GlobalMemoryStatusEx (&statex);
127        dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n",
128              8, statex.ullTotalPhys));
129        dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n",
130              8, statex.ullAvailPhys));
131        physical_memory = statex.ullTotalPhys;
132    
133        HANDLE hProcess = GetCurrentProcess();
134    
135        unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize;
136        unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize;
137        unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize;
138        int res;
139    
140        res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize);
141    
142        RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024;
143        RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize;
144    
145        for(;;) {
146            dmsg(2,("TRYING VALUES  RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
147            res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize);
148            dmsg(2,("AFTER SET: res = %d  RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
149    
150            res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize);
151            dmsg(2,("AFTER GET: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
152    
153            if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) {
154                dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n"));
155                break;
156            }
157    
158            RequestedMinimumWorkingSetSize -=  10*1024*1024;
159            if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;
160        }
161    
162        dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
163        #endif
164        #endif // WIN32
165    
166      if (tune) {      if (tune) {
167          // detect and print system / CPU specific features          // detect and print system / CPU specific features
# Line 92  int main(int argc, char **argv) { Line 171  int main(int argc, char **argv) {
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;
# Line 122  int main(int argc, char **argv) { Line 203  int main(int argc, char **argv) {
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();
# Line 142  int main(int argc, char **argv) { Line 217  int main(int argc, char **argv) {
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 (pthread_equal(pthread_self(), main_thread)) {              atomic_set(&running, 0);
                 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)
249            // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
250            #else
251          case SIGSEGV:          case SIGSEGV:
252              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
253              break;              break;
# Line 213  void signal_handler(int iSignal) { Line 266  void signal_handler(int iSignal) {
266          case SIGUSR2:          case SIGUSR2:
267              std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;              std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
268              break;              break;
269            #endif
270          default: { // this should never happen, as we register for the signals we want          default: { // this should never happen, as we register for the signals we want
271              std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;              std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
272              break;              break;
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  }  }
284    
285  void kill_app() {  void kill_app() {
286        #if defined(WIN32)
287        // FIXME: do we need to do anything at this point under WIN32 ?  is exit(0) ok ?
288        exit(0);
289        #else
290      kill(main_pid, SIGKILL);      kill(main_pid, SIGKILL);
291        #endif
292  }  }
293    
294  void parse_options(int argc, char **argv) {  void parse_options(int argc, char **argv) {
# Line 244  void parse_options(int argc, char **argv Line 305  void parse_options(int argc, char **argv
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    
# Line 267  void parse_options(int argc, char **argv Line 329  void parse_options(int argc, char **argv
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
# Line 321  void parse_options(int argc, char **argv Line 385  void parse_options(int argc, char **argv
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) {
# Line 338  void parse_options(int argc, char **argv Line 401  void parse_options(int argc, char **argv
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      }      }

Legend:
Removed from v.1364  
changed lines
  Added in v.1888

  ViewVC Help
Powered by ViewVC