/[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 1212 by schoenebeck, Tue May 29 23:59:36 2007 UTC revision 1644 by persson, Sat Jan 19 16:55:03 2008 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-2008 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;
# Line 49  unsigned short int lscp_port; Line 69  unsigned short int lscp_port;
69  void parse_options(int argc, char **argv);  void parse_options(int argc, char **argv);
70  void signal_handler(int signal);  void signal_handler(int signal);
71  void kill_app();  void kill_app();
72    static atomic_t running = ATOMIC_INIT(1);
73    
74  int main(int argc, char **argv) {  int main(int argc, char **argv) {
75    
76      // initialize the stack trace mechanism with our binary file      // initialize the stack trace mechanism with our binary file
77      StackTraceInit(argv[0], -1);      StackTraceInit(argv[0], -1);
78    
79        #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        #if !defined(WIN32)
128      main_pid = getpid();      main_pid = getpid();
129      main_thread = pthread_self();      #endif
130    
131      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)      // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>)
132      signal(SIGINT, signal_handler);      signal(SIGINT, signal_handler);
133    
134        #if defined(WIN32)
135        // FIXME: sigaction() not supported on WIN32, we ignore it for now
136        #else
137      // register signal handler for all unusual signals      // register signal handler for all unusual signals
138      // (we will print the stack trace and exit)      // (we will print the stack trace and exit)
139      struct sigaction sact;      struct sigaction sact;
# Line 73  int main(int argc, char **argv) { Line 146  int main(int argc, char **argv) {
146      sigaction(SIGFPE,  &sact, NULL);      sigaction(SIGFPE,  &sact, NULL);
147      sigaction(SIGUSR1, &sact, NULL);      sigaction(SIGUSR1, &sact, NULL);
148      sigaction(SIGUSR2, &sact, NULL);      sigaction(SIGUSR2, &sact, NULL);
149        #endif
150    
151      lscp_addr = htonl(LSCP_ADDR);      lscp_addr = htonl(LSCP_ADDR);
152      lscp_port = htons(LSCP_PORT);      lscp_port = htons(LSCP_PORT);
# Line 128  int main(int argc, char **argv) { Line 202  int main(int argc, char **argv) {
202      rtEvents.push_back(LSCPEvent::event_buffer_fill);      rtEvents.push_back(LSCPEvent::event_buffer_fill);
203      rtEvents.push_back(LSCPEvent::event_total_voice_count);      rtEvents.push_back(LSCPEvent::event_total_voice_count);
204    
205      while (true) {      while (atomic_read(&running)) {
206          if (bPrintStatistics) {          if (bPrintStatistics) {
207              const std::set<Engine*>& engines = EngineFactory::EngineInstances();              const std::set<Engine*>& engines = EngineFactory::EngineInstances();
208              std::set<Engine*>::iterator itEngine = engines.begin();              std::set<Engine*>::iterator itEngine = engines.begin();
# Line 142  int main(int argc, char **argv) { Line 216  int main(int argc, char **argv) {
216              }              }
217          }          }
218    
219        sleep(1);          sleep(1);
220        if (profile)          if (profile)
221        {          {
222            unsigned int samplingFreq = 48000; //FIXME: hardcoded for now              unsigned int samplingFreq = 48000; //FIXME: hardcoded for now
223            unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);              unsigned int bv = LinuxSampler::gig::Profiler::GetBogoVoices(samplingFreq);
224            if (bv != 0)              if (bv != 0)
225            {              {
226                printf("       BogoVoices: %i         \r", bv);                  printf("       BogoVoices: %i         \r", bv);
227                fflush(stdout);                  fflush(stdout);
228            }              }
229        }          }
   
       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, pEngine->VoiceCount());  
               pSampler->fireStreamCountChanged(iter->first, pEngine->DiskStreamCount());  
               pSampler->fireBufferFillChanged(iter->first, pEngine->DiskStreamBufferFillPercentage());  
               pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount());  
           }  
           LSCPServer::UnlockRTNotify();  
       }  
230    
231            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                    pSampler->fireTotalStreamCountChanged(pSampler->GetDiskStreamCount());
246                    pSampler->fireTotalVoiceCountChanged(pSampler->GetVoiceCount());
247                }
248                LSCPServer::UnlockRTNotify();
249            }
250      }      }
251        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        printf("LinuxSampler stopped due to SIGINT.\n");
257      return EXIT_SUCCESS;      return EXIT_SUCCESS;
258  }  }
259    
260  void signal_handler(int iSignal) {  void signal_handler(int iSignal) {
261      switch (iSignal) {      switch (iSignal) {
262          case SIGINT: {          case SIGINT:
263              if (pthread_equal(pthread_self(), main_thread)) {              atomic_set(&running, 0);
                 if (pLSCPServer) {  
                     pLSCPServer->StopThread();  
                     delete pLSCPServer;  
                 }  
                 if (pSampler) delete pSampler;  
 #if HAVE_SQLITE3  
                 InstrumentsDb::Destroy();  
 #endif  
                 printf("LinuxSampler stopped due to SIGINT.\n");  
                 exit(EXIT_SUCCESS);  
             }  
264              return;              return;
265          }          #if defined(WIN32)
266            // FIXME: under WIN32 we ignore the signals below due to the missing sigaction call
267            #else
268          case SIGSEGV:          case SIGSEGV:
269              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;              std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush;
270              break;              break;
# Line 213  void signal_handler(int iSignal) { Line 283  void signal_handler(int iSignal) {
283          case SIGUSR2:          case SIGUSR2:
284              std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;              std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush;
285              break;              break;
286            #endif
287          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
288              std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;              std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush;
289              break;              break;
# Line 227  void signal_handler(int iSignal) { Line 298  void signal_handler(int iSignal) {
298  }  }
299    
300  void kill_app() {  void kill_app() {
301        #if defined(WIN32)
302        // FIXME: do we need to do anything at this point under WIN32 ?  is exit(0) ok ?
303        exit(0);
304        #else
305      kill(main_pid, SIGKILL);      kill(main_pid, SIGKILL);
306        #endif
307  }  }
308    
309  void parse_options(int argc, char **argv) {  void parse_options(int argc, char **argv) {
# Line 291  void parse_options(int argc, char **argv Line 367  void parse_options(int argc, char **argv
367    
368                              if (res) {                              if (res) {
369                                  std::stringstream ss;                                  std::stringstream ss;
370                                  ss << "Fail to stat `" << optarg << "`: " << strerror(errno);                                  ss << "Failed to stat `" << optarg << "`: " << strerror(errno);
371                                  throw Exception(ss.str());                                  throw Exception(ss.str());
372                              }                              }
373    
# Line 304  void parse_options(int argc, char **argv Line 380  void parse_options(int argc, char **argv
380                              InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));                              InstrumentsDb::GetInstrumentsDb()->SetDbFile(String(optarg));
381                          }                          }
382                      } catch(Exception e) {                      } catch(Exception e) {
383                          std::cerr << e.Message() << std::endl << std::endl;                          std::cerr << "Could not open instruments DB file: "
384                          return;                                    << e.Message() << std::endl;
385                            exit(EXIT_FAILURE);
386                      }                      }
387                        break;
                     return;  
388  #else  #else
389                      std::cerr << "LinuxSampler was not build with ";                      std::cerr << "LinuxSampler was not build with ";
390                      std::cerr << "instruments database support." <<std::endl;                      std::cerr << "instruments database support!\n";
391                      return;                      exit(EXIT_FAILURE);
392  #endif                                    break;
393    #endif
394                  case 6: // --create-instruments-db                  case 6: // --create-instruments-db
395  #if HAVE_SQLITE3  #if HAVE_SQLITE3
396                      try {                      try {
397                          if (optarg) {                          if (optarg) {
398                              std::cout << "Creating instruments database..." << std::endl;                              std::cout << "Creating instruments database..." << std::endl;
399                              InstrumentsDb::CreateInstrumentsDb(String(optarg));                              InstrumentsDb::CreateInstrumentsDb(String(optarg));
                             InstrumentsDb::Destroy();  
400                              std::cout << "Done" << std::endl;                              std::cout << "Done" << std::endl;
401                          }                          }
402                      } catch(Exception e) {                      } catch(Exception e) {
# Line 333  void parse_options(int argc, char **argv Line 409  void parse_options(int argc, char **argv
409                      return;                      return;
410  #else  #else
411                      std::cerr << "Failed to create the database. LinuxSampler was ";                      std::cerr << "Failed to create the database. LinuxSampler was ";
412                      std::cerr << "not build with instruments database support." <<std::endl;                      std::cerr << "not build with instruments database support!\n";
413                      exit(EXIT_FAILURE);                      exit(EXIT_FAILURE);
414                      return;                      return;
415  #endif                #endif
416                  case 7: // --lscp-addr                  case 7: // --lscp-addr
417                      struct in_addr addr;                      struct in_addr addr;
418                      if (inet_aton(optarg, &addr) == 0)                      if (inet_aton(optarg, &addr) == 0)

Legend:
Removed from v.1212  
changed lines
  Added in v.1644

  ViewVC Help
Powered by ViewVC