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" |
#include "common/global_private.h" |
34 |
#include "engines/EngineFactory.h" |
#include "engines/EngineFactory.h" |
44 |
|
|
45 |
Sampler* pSampler = NULL; |
Sampler* pSampler = NULL; |
46 |
LSCPServer* pLSCPServer = NULL; |
LSCPServer* pLSCPServer = NULL; |
47 |
|
#if defined(WIN32) |
48 |
|
// inet_aton seems missing under WIN32 |
49 |
|
#ifndef INADDR_NONE |
50 |
|
#define INADDR_NONE 0xffffffff |
51 |
|
#endif |
52 |
|
|
53 |
|
int inet_aton(const char *cp, struct in_addr *addr) |
54 |
|
{ |
55 |
|
addr->s_addr = inet_addr(cp); |
56 |
|
return (addr->s_addr == INADDR_NONE) ? 0 : 1; |
57 |
|
} |
58 |
|
|
59 |
|
DWORD main_thread; |
60 |
|
#else |
61 |
pthread_t main_thread; |
pthread_t main_thread; |
62 |
pid_t main_pid; |
pid_t main_pid; |
63 |
|
#endif |
64 |
bool bPrintStatistics = false; |
bool bPrintStatistics = false; |
65 |
bool profile = false; |
bool profile = false; |
66 |
bool tune = true; |
bool tune = true; |
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_thread = GetCurrentThreadId(); |
129 |
|
#else |
130 |
main_pid = getpid(); |
main_pid = getpid(); |
131 |
main_thread = pthread_self(); |
main_thread = pthread_self(); |
132 |
|
#endif |
133 |
|
|
134 |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
// setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) |
135 |
signal(SIGINT, signal_handler); |
signal(SIGINT, signal_handler); |
136 |
|
|
137 |
|
#if defined(WIN32) |
138 |
|
// FIXME: sigaction() not supported on WIN32, we ignore it for now |
139 |
|
#else |
140 |
// register signal handler for all unusual signals |
// register signal handler for all unusual signals |
141 |
// (we will print the stack trace and exit) |
// (we will print the stack trace and exit) |
142 |
struct sigaction sact; |
struct sigaction sact; |
149 |
sigaction(SIGFPE, &sact, NULL); |
sigaction(SIGFPE, &sact, NULL); |
150 |
sigaction(SIGUSR1, &sact, NULL); |
sigaction(SIGUSR1, &sact, NULL); |
151 |
sigaction(SIGUSR2, &sact, NULL); |
sigaction(SIGUSR2, &sact, NULL); |
152 |
|
#endif |
153 |
|
|
154 |
lscp_addr = htonl(LSCP_ADDR); |
lscp_addr = htonl(LSCP_ADDR); |
155 |
lscp_port = htons(LSCP_PORT); |
lscp_port = htons(LSCP_PORT); |
258 |
void signal_handler(int iSignal) { |
void signal_handler(int iSignal) { |
259 |
switch (iSignal) { |
switch (iSignal) { |
260 |
case SIGINT: { |
case SIGINT: { |
261 |
|
#if defined(WIN32) |
262 |
|
if( GetCurrentThreadId() == main_thread ) { |
263 |
|
#else |
264 |
if (pthread_equal(pthread_self(), main_thread)) { |
if (pthread_equal(pthread_self(), main_thread)) { |
265 |
|
#endif |
266 |
if (pLSCPServer) pLSCPServer->StopThread(); |
if (pLSCPServer) pLSCPServer->StopThread(); |
267 |
// the delete order here is important: the Sampler |
// the delete order here is important: the Sampler |
268 |
// destructor sends notifications to the lscpserver |
// destructor sends notifications to the lscpserver |
276 |
} |
} |
277 |
return; |
return; |
278 |
} |
} |
279 |
|
#if defined(WIN32) |
280 |
|
// FIXME: under WIN32 we ignore the signals below due to the missing sigaction call |
281 |
|
#else |
282 |
case SIGSEGV: |
case SIGSEGV: |
283 |
std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; |
std::cerr << ">>> FATAL ERROR: Segmentation fault (SIGSEGV) occured! <<<\n" << std::flush; |
284 |
break; |
break; |
297 |
case SIGUSR2: |
case SIGUSR2: |
298 |
std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; |
std::cerr << ">>> User defined signal 2 (SIGUSR2) received <<<\n" << std::flush; |
299 |
break; |
break; |
300 |
|
#endif |
301 |
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 |
302 |
std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; |
std::cerr << ">>> FATAL ERROR: Unknown signal received! <<<\n" << std::flush; |
303 |
break; |
break; |
312 |
} |
} |
313 |
|
|
314 |
void kill_app() { |
void kill_app() { |
315 |
|
#if defined(WIN32) |
316 |
|
// FIXME: do we need to do anything at this point under WIN32 ? is exit(0) ok ? |
317 |
|
exit(0); |
318 |
|
#else |
319 |
kill(main_pid, SIGKILL); |
kill(main_pid, SIGKILL); |
320 |
|
#endif |
321 |
} |
} |
322 |
|
|
323 |
void parse_options(int argc, char **argv) { |
void parse_options(int argc, char **argv) { |