/[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 1297 by iliev, Thu Aug 16 15:55:21 2007 UTC revision 1908 by persson, Mon Jun 1 18:50:06 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 23  Line 23 
23    
24  #include <getopt.h>  #include <getopt.h>
25  #include <signal.h>  #include <signal.h>
26    #include <sys/stat.h>
27    
28    #if defined(WIN32)
29    // require at least Windows 2000 for the GlobalMemoryStatusEx() call
30    #if _WIN32_WINNT < 0x0500
31    #ifdef _WIN32_WINNT
32    #undef _WIN32_WINNT
33    #endif
34    #define _WIN32_WINNT 0x0500
35    #endif
36    #endif
37    
38  #include "Sampler.h"  #include "Sampler.h"
39    #include "common/global_private.h"
40  #include "engines/EngineFactory.h"  #include "engines/EngineFactory.h"
41  #include "engines/InstrumentEditorFactory.h"  #include "plugins/InstrumentEditorFactory.h"
42  #include "drivers/midi/MidiInputDeviceFactory.h"  #include "drivers/midi/MidiInputDeviceFactory.h"
43  #include "drivers/audio/AudioOutputDeviceFactory.h"  #include "drivers/audio/AudioOutputDeviceFactory.h"
44  #include "engines/gig/Profiler.h"  #include "engines/gig/Profiler.h"
45  #include "network/lscpserver.h"  #include "network/lscpserver.h"
46  #include "common/stacktrace.h"  #include "common/stacktrace.h"
47  #include "common/Features.h"  #include "common/Features.h"
48    #include "common/atomic.h"
49    
50  using namespace LinuxSampler;  using namespace LinuxSampler;
51    
52  Sampler*    pSampler    = NULL;  Sampler*    pSampler    = NULL;
53  LSCPServer* pLSCPServer = NULL;  LSCPServer* pLSCPServer = NULL;
54  pthread_t   main_thread;  #if defined(WIN32)
55    // inet_aton seems missing under WIN32
56    #ifndef INADDR_NONE
57    #define INADDR_NONE 0xffffffff
58    #endif
59    
60    int inet_aton(const char *cp, struct in_addr *addr)
61    {
62        addr->s_addr = inet_addr(cp);
63        return (addr->s_addr == INADDR_NONE) ? 0 : 1;
64    }
65    
66    #else
67  pid_t       main_pid;  pid_t       main_pid;
68    #endif
69  bool bPrintStatistics = false;  bool bPrintStatistics = false;
70  bool profile = false;  bool profile = false;
71  bool tune = true;  bool tune = true;
72    static bool bShowStackTrace = false;
73  unsigned long int lscp_addr;  unsigned long int lscp_addr;
74  unsigned short int lscp_port;  unsigned short int lscp_port;
75    
76  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
77  void signal_handler(int signal);  void signal_handler(int signal);
78  void kill_app();  void kill_app();
79    static atomic_t running = ATOMIC_INIT(1);
80    
81  int main(int argc, char **argv) {  int main(int argc, char **argv) {
82    
83      // initialize the stack trace mechanism with our binary file      lscp_addr = htonl(LSCP_ADDR);
84      StackTraceInit(argv[0], -1);      lscp_port = htons(LSCP_PORT);
85    
86        #if !defined(WIN32)
87      main_pid = getpid();      main_pid = getpid();
88      main_thread = pthread_self();      #endif
89    
90        // parse and assign command line options
91        parse_options(argc, argv);
92    
93      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
94      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
95    
96      // register signal handler for all unusual signals      // initialize the stack trace mechanism with our binary file
97      // (we will print the stack trace and exit)      // (if requested by command line option)
98      struct sigaction sact;      if (bShowStackTrace) {
99      sigemptyset(&sact.sa_mask);          #if defined(WIN32)
100      sact.sa_flags   = 0;          // FIXME: sigaction() not supported on WIN32, we ignore it for now
101      sact.sa_handler = signal_handler;          #else
102      sigaction(SIGSEGV, &sact, NULL);          StackTraceInit(argv[0], -1);
103      sigaction(SIGBUS,  &sact, NULL);          // register signal handler for all unusual signals
104      sigaction(SIGILL,  &sact, NULL);          // (we will print the stack trace and exit)
105      sigaction(SIGFPE,  &sact, NULL);          struct sigaction sact;
106      sigaction(SIGUSR1, &sact, NULL);          sigemptyset(&sact.sa_mask);
107      sigaction(SIGUSR2, &sact, NULL);          sact.sa_flags   = 0;
108            sact.sa_handler = signal_handler;
109      lscp_addr = htonl(LSCP_ADDR);          sigaction(SIGSEGV, &sact, NULL);
110      lscp_port = htons(LSCP_PORT);          sigaction(SIGBUS,  &sact, NULL);
111            sigaction(SIGILL,  &sact, NULL);
112      // parse and assign command line options          sigaction(SIGFPE,  &sact, NULL);
113      parse_options(argc, argv);          sigaction(SIGUSR1, &sact, NULL);
114            sigaction(SIGUSR2, &sact, NULL);
115            #endif
116        }
117    
118      dmsg(1,("LinuxSampler %s\n", VERSION));      dmsg(1,("LinuxSampler %s\n", VERSION));
119      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"));
120      dmsg(1,("Copyright (C) 2005-2007 Christian Schoenebeck\n"));      dmsg(1,("Copyright (C) 2005-2009 Christian Schoenebeck\n"));
121    
122        #if defined(WIN32)
123        #if 0
124        // some WIN32 memory info code which tries to determine the maximum lockable amount of memory (for debug purposes)
125        SYSTEM_INFO siSysInfo;
126        long physical_memory;
127        GetSystemInfo(&siSysInfo);
128        dmsg(2,("page size=%d\n", siSysInfo.dwPageSize));
129    
130        MEMORYSTATUSEX statex;
131            statex.dwLength = sizeof (statex);
132        GlobalMemoryStatusEx (&statex);
133        dmsg(2, ("There are %*I64d total Kbytes of physical memory.\n",
134              8, statex.ullTotalPhys));
135        dmsg(2, ("There are %*I64d free Kbytes of physical memory.\n",
136              8, statex.ullAvailPhys));
137        physical_memory = statex.ullTotalPhys;
138    
139        HANDLE hProcess = GetCurrentProcess();
140    
141        unsigned long MinimumWorkingSetSize, MaximumWorkingSetSize;
142        unsigned long DefaultMinimumWorkingSetSize, DefaultMaximumWorkingSetSize;
143        unsigned long RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize;
144        int res;
145    
146        res = GetProcessWorkingSetSize(hProcess, &DefaultMinimumWorkingSetSize, &DefaultMaximumWorkingSetSize);
147    
148        RequestedMaximumWorkingSetSize = physical_memory - 2*1024*1024;
149        RequestedMinimumWorkingSetSize = RequestedMaximumWorkingSetSize;
150    
151        for(;;) {
152            dmsg(2,("TRYING VALUES  RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
153            res = SetProcessWorkingSetSize(hProcess, RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize);
154            dmsg(2,("AFTER SET: res = %d  RequestedMinimumWorkingSetSize=%d, RequestedMaximumWorkingSetSize=%d\n", res,RequestedMinimumWorkingSetSize, RequestedMaximumWorkingSetSize));
155    
156            res = GetProcessWorkingSetSize(hProcess, &MinimumWorkingSetSize, &MaximumWorkingSetSize);
157            dmsg(2,("AFTER GET: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
158    
159            if( RequestedMinimumWorkingSetSize == MinimumWorkingSetSize ) {
160                dmsg(2,("RequestedMinimumWorkingSetSize == MinimumWorkingSetSize. OK !\n"));
161                break;
162            }
163    
164            RequestedMinimumWorkingSetSize -=  10*1024*1024;
165            if(RequestedMinimumWorkingSetSize < DefaultMinimumWorkingSetSize) break;
166        }
167    
168        dmsg(2,("AFTER GetProcessWorkingSetSize: res = %d  MinimumWorkingSetSize=%d, MaximumWorkingSetSize=%d\n", res,MinimumWorkingSetSize, MaximumWorkingSetSize));
169        #endif
170        #endif // WIN32
171    
172      if (tune) {      if (tune) {
173          // detect and print system / CPU specific features          // detect and print system / CPU specific features
# Line 92  int main(int argc, char **argv) { Line 177  int main(int argc, char **argv) {
177          Features::enableDenormalsAreZeroMode();          Features::enableDenormalsAreZeroMode();
178      }      }
179    
180        dmsg(1,("Automatic Stacktrace: %s\n", (bShowStackTrace) ? "On" : "Off"));
181    
182      // create LinuxSampler instance      // create LinuxSampler instance
183      dmsg(1,("Creating Sampler..."));      dmsg(1,("Creating Sampler..."));
184      pSampler = new Sampler;      pSampler = new Sampler;
# Line 122  int main(int argc, char **argv) { Line 209  int main(int argc, char **argv) {
209    
210      printf("LinuxSampler initialization completed. :-)\n\n");      printf("LinuxSampler initialization completed. :-)\n\n");
211    
212      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) {  
213          if (bPrintStatistics) {          if (bPrintStatistics) {
214              const std::set<Engine*>& engines = EngineFactory::EngineInstances();              const std::set<Engine*>& engines = EngineFactory::EngineInstances();
215              std::set<Engine*>::iterator itEngine = engines.begin();              std::set<Engine*>::iterator itEngine = engines.begin();
# Line 142  int main(int argc, char **argv) { Line 223  int main(int argc, char **argv) {
223              }              }
224          }          }
225    
226        sleep(1);          sleep(1);
227        if (profile)          if (profile)
228        {          {
229            unsigned int samplingFreq = 48000; //FIXME: hardcoded for now              unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
230            unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);              unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
231            if (bv != 0)              if (bv != 0)
232            {              {
233                printf("       BogoVoices: %i         \r", bv);                  printf("       BogoVoices: %i         \r", bv);
234                fflush(stdout);                  fflush(stdout);
235            }              }
236        }          }
   
       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();  
       }  
237    
238            pSampler->fireStatistics();
239      }      }
240        if (pLSCPServer) pLSCPServer->StopThread();
241        // the delete order here is important: the Sampler
242        // destructor sends notifications to the lscpserver
243        if (pSampler) delete pSampler;
244        if (pLSCPServer) delete pLSCPServer;
245        printf("LinuxSampler stopped due to SIGINT.\n");
246      return EXIT_SUCCESS;      return EXIT_SUCCESS;
247  }  }
248    
249  void signal_handler(int iSignal) {  void signal_handler(int iSignal) {
250      switch (iSignal) {      switch (iSignal) {
251          case SIGINT: {          case SIGINT:
252              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);  
             }  
253              return;              return;
254          }          #if defined(WIN32)
255            // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
256            #else
257          case SIGSEGV:          case SIGSEGV:
258              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
259              break;              break;
# Line 213  void signal_handler(int iSignal) { Line 272  void signal_handler(int iSignal) {
272          case SIGUSR2:          case SIGUSR2:
273              std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;              std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
274              break;              break;
275            #endif
276          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
277              std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;              std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
278              break;              break;
279          }          }
280      }      }
281      signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions      signal(iSignal, SIG_DFL); // Reinstall default handler to prevent race conditions
282      std::cerr << "Showing stack trace...\n" << std::flush;      if (bShowStackTrace) {
283      StackTrace();          std::cerr << "Showing stack trace...\n" << std::flush;
284      sleep(2);          StackTrace();
285            sleep(2);
286        }
287      std::cerr << "Killing LinuxSampler...\n" << std::flush;      std::cerr << "Killing LinuxSampler...\n" << std::flush;
288      kill_app(); // Use abort() if we want to generate a core dump.      kill_app(); // Use abort() if we want to generate a core dump.
289  }  }
290    
291  void kill_app() {  void kill_app() {
292        #if defined(WIN32)
293        // FIXME: do we need to do anything at this point under WIN32 ?  is exit(0) ok ?
294        exit(0);
295        #else
296      kill(main_pid, SIGKILL);      kill(main_pid, SIGKILL);
297        #endif
298  }  }
299    
300  void parse_options(int argc, char **argv) {  void parse_options(int argc, char **argv) {
# Line 244  void parse_options(int argc, char **argv Line 311  void parse_options(int argc, char **argv
311              {"create-instruments-db",1,0,0},              {"create-instruments-db",1,0,0},
312              {"lscp-addr",1,0,0},              {"lscp-addr",1,0,0},
313              {"lscp-port",1,0,0},              {"lscp-port",1,0,0},
314                {"stacktrace",0,0,0},
315              {0,0,0,0}              {0,0,0,0}
316          };          };
317    
# Line 267  void parse_options(int argc, char **argv Line 335  void parse_options(int argc, char **argv
335                      printf("--lscp-port                 set LSCP port (default: 8888)\n");                      printf("--lscp-port                 set LSCP port (default: 8888)\n");
336                      printf("--create-instruments-db     creates an instruments DB\n");                      printf("--create-instruments-db     creates an instruments DB\n");
337                      printf("--instruments-db-location   specifies the instruments DB file\n");                      printf("--instruments-db-location   specifies the instruments DB file\n");
338                        printf("--stacktrace                automatically shows stacktrace if crashes\n");
339                        printf("                            (broken on most systems at the moment)\n");
340                      exit(EXIT_SUCCESS);                      exit(EXIT_SUCCESS);
341                      break;                      break;
342                  case 1: // --version                  case 1: // --version
# Line 291  void parse_options(int argc, char **argv Line 361  void parse_options(int argc, char **argv
361    
362                              if (res) {                              if (res) {
363                                  std::stringstream ss;                                  std::stringstream ss;
364                                  ss << "Fail to stat `" << optarg << "`: " << strerror(errno);                                  ss << "Failed to stat `" << optarg << "`: " << strerror(errno);
365                                  throw Exception(ss.str());                                  throw Exception(ss.str());
366                              }                              }
367    
# Line 304  void parse_options(int argc, char **argv Line 374  void parse_options(int argc, char **argv
374                              InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));                              InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));
375                          }                          }
376                      } catch(Exception e) {                      } catch(Exception e) {
377                          std::cerr << e.Message() << std::endl << std::endl;                          std::cerr << "Could not open instruments DB file: "
378                          return;                                    << e.Message() << std::endl;
379                            exit(EXIT_FAILURE);
380                      }                      }
381                        break;
                     return;  
382  #else  #else
383                      std::cerr << "LinuxSampler was not build with ";                      std::cerr << "LinuxSampler was not build with ";
384                      std::cerr << "instruments database support." <<std::endl;                      std::cerr << "instruments database support!\n";
385                      return;                      exit(EXIT_FAILURE);
386  #endif                                    break;
387    #endif
388                  case 6: // --create-instruments-db                  case 6: // --create-instruments-db
389  #if HAVE_SQLITE3  #if HAVE_SQLITE3
390                      try {                      try {
391                          if (optarg) {                          if (optarg) {
392                              std::cout << "Creating instruments database..." << std::endl;                              std::cout << "Creating instruments database..." << std::endl;
393                              InstrumentsDb::CreateInstrumentsDb(String(optarg));                              InstrumentsDb::CreateInstrumentsDb(String(optarg));
                             InstrumentsDb::Destroy();  
394                              std::cout << "Done" << std::endl;                              std::cout << "Done" << std::endl;
395                          }                          }
396                      } catch(Exception e) {                      } catch(Exception e) {
# Line 333  void parse_options(int argc, char **argv Line 403  void parse_options(int argc, char **argv
403                      return;                      return;
404  #else  #else
405                      std::cerr << "Failed to create the database. LinuxSampler was ";                      std::cerr << "Failed to create the database. LinuxSampler was ";
406                      std::cerr << "not build with instruments database support." <<std::endl;                      std::cerr << "not build with instruments database support!\n";
407                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
408                      return;                      return;
409  #endif                #endif
410                  case 7: // --lscp-addr                  case 7: { // --lscp-addr
411                      struct in_addr addr;                      struct in_addr addr;
412                      if (inet_aton(optarg, &addr) == 0)                      if (inet_aton(optarg, &addr) == 0)
413                          printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");                          printf("WARNING: Failed to parse lscp-addr argument, ignoring!\n");
414                      else                      else
415                          lscp_addr = addr.s_addr;                          lscp_addr = addr.s_addr;
416                      break;                      break;
417                  case 8: // --lscp-port                  }
418                    case 8: {// --lscp-port
419                      long unsigned int port = 0;                      long unsigned int port = 0;
420                      if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))                      if ((sscanf(optarg, "%u", &port) != 1) || (port == 0) || (port > 65535))
421                          printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");                          printf("WARNING: Failed to parse lscp-port argument, ignoring!\n");
422                      else                      else
423                          lscp_port = htons(port);                          lscp_port = htons(port);
424                      break;                      break;
425                    }
426                    case 9: // --stacktrace
427                        bShowStackTrace = true;
428                        break;
429              }              }
430          }          }
431      }      }

Legend:
Removed from v.1297  
changed lines
  Added in v.1908

  ViewVC Help
Powered by ViewVC