Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 1995-02-23 |
2 | // Created by: Remi LEQUETTE | |
3 | // Copyright (c) 1995-1999 Matra Datavision | |
973c2be1 | 4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e | 5 | // |
973c2be1 | 6 | // This file is part of Open CASCADE Technology software library. |
b311480e | 7 | // |
d5f74e42 | 8 | // This library is free software; you can redistribute it and/or modify it under |
9 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 10 | // by the Free Software Foundation, with special exception defined in the file |
11 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
12 | // distribution for complete text of the license and disclaimer of any warranty. | |
b311480e | 13 | // |
973c2be1 | 14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. | |
b311480e | 16 | |
7fd59977 | 17 | |
42cf5bc1 | 18 | #include <Draw.hxx> |
7fd59977 | 19 | #include <Draw_Appli.hxx> |
a60b9727 | 20 | #include <Draw_Chronometer.hxx> |
42cf5bc1 | 21 | #include <Draw_Drawable3D.hxx> |
7fd59977 | 22 | #include <Draw_Printer.hxx> |
42cf5bc1 | 23 | #include <Draw_ProgressIndicator.hxx> |
7fd59977 | 24 | #include <Message.hxx> |
25 | #include <Message_Messenger.hxx> | |
8a262fa1 | 26 | #include <OSD.hxx> |
42cf5bc1 | 27 | #include <OSD_Chronometer.hxx> |
8a262fa1 | 28 | #include <OSD_Exception_CTRL_BREAK.hxx> |
42cf5bc1 | 29 | #include <OSD_MAllocHook.hxx> |
30 | #include <OSD_MemInfo.hxx> | |
31 | #include <Standard_Macro.hxx> | |
32 | #include <Standard_SStream.hxx> | |
33 | #include <Standard_Stream.hxx> | |
34 | #include <Standard_Version.hxx> | |
35 | #include <TCollection_AsciiString.hxx> | |
7fd59977 | 36 | |
42cf5bc1 | 37 | #include <OSD_PerfMeter.h> |
a60b9727 | 38 | #ifdef _WIN32 |
7fd59977 | 39 | |
7fd59977 | 40 | #include <windows.h> |
41 | #include <winbase.h> | |
42 | #include <process.h> | |
43 | #include <stdio.h> | |
44 | #include <stdlib.h> | |
a60b9727 | 45 | #include <time.h> |
46 | #include <limits> | |
47 | ||
48 | #define RLIM_INFINITY 0x7fffffff | |
49 | ||
50 | static clock_t CPU_CURRENT; // cpu time already used at last | |
51 | // cpulimit call. (sec.) | |
a60b9727 | 52 | #else /* _WIN32 */ |
53 | ||
54 | #include <sys/resource.h> | |
03155c18 | 55 | #include <signal.h> |
0b309a75 | 56 | #include <unistd.h> |
7fd59977 | 57 | |
58 | #if defined (__hpux) || defined ( HPUX ) | |
59 | #define RLIM_INFINITY 0x7fffffff | |
60 | #define RLIMIT_CPU 0 | |
61 | #endif | |
62 | ||
a60b9727 | 63 | #endif /* _WIN32 */ |
7fd59977 | 64 | |
a60b9727 | 65 | extern Standard_Boolean Draw_Batch; |
66 | ||
67 | static clock_t CPU_LIMIT; // Cpu_limit in Sec. | |
0b309a75 | 68 | static OSD_Timer aTimer; |
7fd59977 | 69 | |
70 | //======================================================================= | |
71 | // chronom | |
72 | //======================================================================= | |
73 | ||
74 | extern Standard_Boolean Draw_Chrono; | |
75 | ||
76 | static Standard_Integer chronom(Draw_Interpretor& di, | |
77 | Standard_Integer n,const char** a) | |
78 | { | |
79 | if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) { | |
80 | if (n == 1) | |
81 | Draw_Chrono = !Draw_Chrono; | |
82 | else | |
83 | Draw_Chrono = (*a[1] == '1'); | |
84 | ||
586db386 | 85 | if (Draw_Chrono) di << "Chronometers activated.\n"; |
86 | else di << "Chronometers desactivated.\n"; | |
7fd59977 | 87 | } |
88 | else { | |
89 | Handle(Draw_Drawable3D) D = Draw::Get(a[1]); | |
90 | Handle(Draw_Chronometer) C; | |
91 | if (!D.IsNull()) { | |
92 | C = Handle(Draw_Chronometer)::DownCast(D); | |
93 | } | |
94 | if (C.IsNull()) { | |
95 | C = new Draw_Chronometer(); | |
96 | Draw::Set(a[1],C,Standard_False); | |
97 | } | |
98 | if (n <= 2) { | |
99 | C->Timer().Reset(); | |
100 | } | |
101 | else { | |
102 | if (!strcasecmp(a[2],"reset")) | |
103 | C->Timer().Reset(); | |
104 | if (!strcasecmp(a[2],"start")) | |
105 | C->Timer().Start(); | |
106 | if (!strcasecmp(a[2],"stop")) | |
107 | C->Timer().Stop(); | |
108 | if (!strcasecmp(a[2],"show")) | |
109 | C->Timer().Show(); | |
110 | } | |
111 | } | |
112 | return 0; | |
113 | } | |
114 | ||
115 | static Standard_Integer dchronom(Draw_Interpretor& I, | |
116 | Standard_Integer n,const char** a) | |
117 | { | |
118 | if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) { | |
119 | if (n == 1) | |
120 | Draw_Chrono = !Draw_Chrono; | |
121 | else | |
122 | Draw_Chrono = (*a[1] == '1'); | |
123 | ||
586db386 | 124 | if (Draw_Chrono) I << "Chronometers activated.\n"; |
125 | else I << "Chronometers desactivated.\n"; | |
7fd59977 | 126 | } |
127 | else { | |
128 | Handle(Draw_Drawable3D) D = Draw::Get(a[1]); | |
129 | Handle(Draw_Chronometer) C; | |
130 | if (!D.IsNull()) { | |
131 | C = Handle(Draw_Chronometer)::DownCast(D); | |
132 | } | |
133 | if (C.IsNull()) { | |
134 | C = new Draw_Chronometer(); | |
135 | Draw::Set(a[1],C,Standard_False); | |
136 | } | |
137 | if (n <= 2) { | |
138 | C->Timer().Reset(); | |
139 | } | |
140 | else { | |
141 | if (!strcasecmp(a[2],"reset")) | |
142 | C->Timer().Reset(); | |
143 | if (!strcasecmp(a[2],"start")) | |
144 | C->Timer().Start(); | |
145 | if (!strcasecmp(a[2],"stop")) | |
146 | C->Timer().Stop(); | |
147 | if (!strcasecmp(a[2],"show")) { | |
148 | Standard_SStream ss; | |
149 | C->Timer().Show(ss); | |
150 | I << ss; | |
151 | } | |
152 | } | |
153 | } | |
154 | return 0; | |
155 | } | |
156 | ||
157 | ||
158 | ||
159 | //======================================================================= | |
160 | //function : ifbatch | |
161 | //purpose : | |
162 | //======================================================================= | |
163 | ||
164 | static Standard_Integer ifbatch(Draw_Interpretor& DI, Standard_Integer , const char** ) | |
165 | { | |
166 | if (Draw_Batch) | |
167 | DI << "1"; | |
168 | else | |
169 | DI << "0"; | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | //======================================================================= | |
175 | //function : spy | |
176 | //purpose : | |
177 | //======================================================================= | |
178 | ||
179 | extern Standard_Boolean Draw_Spying; | |
180 | extern filebuf Draw_Spyfile; | |
181 | ||
182 | static Standard_Integer spy(Draw_Interpretor& di, Standard_Integer n, const char** a) | |
183 | { | |
184 | if (Draw_Spying) | |
185 | Draw_Spyfile.close(); | |
186 | Draw_Spying = Standard_False; | |
187 | if (n > 1) { | |
188 | if (!Draw_Spyfile.open(a[1],ios::out)) { | |
586db386 | 189 | di << "Cannot open "<<a[1]<<" for writing\n"; |
7fd59977 | 190 | return 1; |
191 | } | |
192 | Draw_Spying = Standard_True; | |
193 | } | |
194 | return 0; | |
195 | } | |
196 | ||
aa02980d | 197 | static Standard_Integer dlog(Draw_Interpretor& di, Standard_Integer n, const char** a) |
198 | { | |
199 | if (n != 2 && n != 3) | |
200 | { | |
201 | cout << "Enable or disable logging: " << a[0] << " {on|off}" << endl; | |
202 | cout << "Reset log: " << a[0] << " reset" << endl; | |
203 | cout << "Get log content: " << a[0] << " get" << endl; | |
204 | return 1; | |
205 | } | |
206 | ||
207 | if (! strcmp (a[1], "on") && n == 2) | |
208 | { | |
209 | di.SetDoLog (Standard_True); | |
210 | // di.Log() << "dlog on" << endl; // for symmetry | |
211 | } | |
212 | else if (! strcmp (a[1], "off") && n == 2) | |
213 | { | |
214 | di.SetDoLog (Standard_False); | |
215 | } | |
216 | else if (! strcmp (a[1], "reset") && n == 2) | |
217 | { | |
218 | di.Log().str(""); | |
219 | } | |
220 | else if (! strcmp (a[1], "get") && n == 2) | |
221 | { | |
222 | di << di.Log().str().c_str(); | |
223 | } | |
224 | else if (! strcmp (a[1], "add") && n == 3) | |
225 | { | |
226 | di.Log() << a[2] << "\n"; | |
227 | } | |
228 | else { | |
229 | cout << "Unrecognized option(s): " << a[1] << endl; | |
230 | return 1; | |
231 | } | |
232 | return 0; | |
233 | } | |
234 | ||
235 | static Standard_Integer decho(Draw_Interpretor& di, Standard_Integer n, const char** a) | |
236 | { | |
237 | if (n != 2) | |
238 | { | |
239 | cout << "Enable or disable echoing: " << a[0] << " {on|off}" << endl; | |
240 | return 1; | |
241 | } | |
242 | ||
243 | if (! strcmp (a[1], "on")) | |
244 | { | |
245 | di.SetDoEcho (Standard_True); | |
246 | } | |
247 | else if (! strcmp (a[1], "off")) | |
248 | { | |
249 | di.SetDoEcho (Standard_False); | |
250 | } | |
251 | else { | |
252 | cout << "Unrecognized option: " << a[1] << endl; | |
253 | return 1; | |
254 | } | |
255 | return 0; | |
256 | } | |
257 | ||
8a262fa1 | 258 | static Standard_Integer dbreak(Draw_Interpretor& di, Standard_Integer, const char**) |
259 | { | |
260 | try { | |
261 | OSD::ControlBreak(); | |
262 | } | |
263 | catch (OSD_Exception_CTRL_BREAK) { | |
264 | di << "User pressed Control-Break"; | |
265 | return 1; // Tcl exception | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | static Standard_Integer dversion(Draw_Interpretor& di, Standard_Integer, const char**) | |
272 | { | |
273 | // print OCCT version and OCCTY-specific macros used | |
274 | di << "Open CASCADE Technology " << OCC_VERSION_STRING_EXT << "\n"; | |
0797d9d3 | 275 | #ifdef OCCT_DEBUG |
276 | di << "Extended debug mode\n"; | |
277 | #elif defined(_DEBUG) | |
8a262fa1 | 278 | di << "Debug mode\n"; |
279 | #endif | |
280 | #ifdef HAVE_TBB | |
281 | di << "TBB enabled (HAVE_TBB)\n"; | |
282 | #else | |
283 | di << "TBB disabled\n"; | |
284 | #endif | |
285 | #ifdef HAVE_GL2PS | |
286 | di << "GL2PS enabled (HAVE_GL2PS)\n"; | |
287 | #else | |
288 | di << "GL2PS disabled\n"; | |
289 | #endif | |
290 | #ifdef HAVE_FREEIMAGE | |
291 | di << "FreeImage enabled (HAVE_FREEIMAGE)\n"; | |
292 | #else | |
293 | di << "FreeImage disabled\n"; | |
294 | #endif | |
7ae4a307 | 295 | #ifdef HAVE_OPENCL |
296 | di << "OpenCL enabled (HAVE_OPENCL)\n"; | |
297 | #else | |
298 | di << "OpenCL disabled\n"; | |
299 | #endif | |
476e84b1 | 300 | #ifdef HAVE_VTK |
301 | di << "VTK enabled (HAVE_VTK)\n"; | |
302 | #else | |
303 | di << "VTK disabled\n"; | |
304 | #endif | |
8a262fa1 | 305 | #ifdef No_Exception |
306 | di << "Exceptions disabled (No_Exception)\n"; | |
307 | #else | |
308 | di << "Exceptions enabled\n"; | |
309 | #endif | |
310 | ||
311 | // check compiler, OS, etc. using pre-processor macros provided by compiler | |
312 | // see "Pre-defined C/C++ Compiler Macros" http://sourceforge.net/p/predef/wiki/ | |
313 | // note that only modern compilers that are known to be used for OCCT are recognized | |
314 | ||
315 | // compiler; note that GCC and MSVC are last as other compilers (e.g. Intel) can also define __GNUC__ and _MSC_VER | |
316 | #if defined(__INTEL_COMPILER) | |
317 | di << "Compiler: Intel " << __INTEL_COMPILER << "\n"; | |
318 | #elif defined(__BORLANDC__) | |
319 | di << "Compiler: Borland C++ (__BORLANDC__ = " << __BORLANDC__ << ")\n"; | |
320 | #elif defined(__clang__) | |
321 | di << "Compiler: Clang " << __clang_major__ << "." << __clang_minor__ << "." << __clang_patchlevel__ << "\n"; | |
322 | #elif defined(__SUNPRO_C) | |
323 | di << "Compiler: Sun Studio (__SUNPRO_C = " << __SUNPROC_C << ")\n"; | |
324 | #elif defined(_MSC_VER) | |
325 | di << "Compiler: MS Visual C++ " << (int)(_MSC_VER/100-6) << "." << (int)((_MSC_VER/10)-60-10*(int)(_MSC_VER/100-6)) << " (_MSC_FULL_VER = " << _MSC_FULL_VER << ")\n"; | |
326 | #elif defined(__GNUC__) | |
327 | di << "Compiler: GCC " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << "\n"; | |
328 | #else | |
329 | di << "Compiler: unrecognized\n"; | |
330 | #endif | |
331 | ||
332 | // Cygwin and MinGW specifics | |
333 | #if defined(__CYGWIN__) | |
334 | di << "Cygwin\n"; | |
335 | #endif | |
336 | #if defined(__MINGW64__) | |
7c65581d | 337 | di << "MinGW 64 " << __MINGW64_VERSION_MAJOR << "." << __MINGW64_VERSION_MINOR << "\n"; |
8a262fa1 | 338 | #elif defined(__MINGW32__) |
339 | di << "MinGW 32 " << __MINGW32_MAJOR_VERSION << "." << __MINGW32_MINOR_VERSION << "\n"; | |
340 | #endif | |
341 | ||
342 | // architecture | |
343 | #if defined(__amd64) || defined(__x86_64) || defined(_M_AMD64) | |
344 | di << "Architecture: AMD64\n"; | |
345 | #elif defined(__i386) || defined(_M_IX86) || defined(__X86__)|| defined(_X86_) | |
346 | di << "Architecture: Intel x86\n"; | |
347 | #elif defined(_M_IA64) || defined(__ia64__) | |
348 | di << "Architecture: Intel Itanium (IA 64)\n"; | |
349 | #elif defined(__sparc__) || defined(__sparc) | |
350 | di << "Architecture: SPARC\n"; | |
351 | #else | |
352 | di << "Architecture: unrecognized\n"; | |
353 | #endif | |
354 | ||
355 | // OS | |
356 | #if defined(_WIN32) || defined(__WINDOWS__) || defined(__WIN32__) | |
357 | di << "OS: Windows\n"; | |
358 | #elif defined(__APPLE__) || defined(__MACH__) | |
359 | di << "OS: Mac OS X\n"; | |
360 | #elif defined(__sun) | |
361 | di << "OS: SUN Solaris\n"; | |
362 | #elif defined(__ANDROID__) /* must be before Linux */ | |
363 | #include <android/api-level.h> | |
364 | di << "OS: Android (__ANDROID_API__ = " << __ANDROID_API__ << ")\n"; | |
d8d01f6e | 365 | #elif defined(__QNXNTO__) |
366 | di << "OS: QNX Neutrino\n"; | |
367 | #elif defined(__QNX__) | |
368 | di << "OS: QNX\n"; | |
8a262fa1 | 369 | #elif defined(__linux__) |
370 | di << "OS: Linux\n"; | |
371 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) | |
372 | #include <sys/param.h> | |
373 | di << "OS: BSD (BSD = " << BSD << ")\n"; | |
374 | #else | |
375 | di << "OS: unrecognized\n"; | |
376 | #endif | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
7fd59977 | 381 | //======================================================================= |
382 | //function : wait | |
383 | //purpose : | |
384 | //======================================================================= | |
385 | ||
386 | static Standard_Integer Draw_wait(Draw_Interpretor& , Standard_Integer n, const char** a) | |
387 | { | |
388 | Standard_Integer w = 10; | |
389 | if (n > 1) | |
91322f44 | 390 | w = Draw::Atoi(a[1]); |
7fd59977 | 391 | time_t ct = time(NULL) + w; |
392 | while (time(NULL) < ct) {}; | |
393 | return 0; | |
394 | } | |
395 | ||
47cbc555 A |
396 | //======================================================================= |
397 | //function : cpulimit | |
398 | //purpose : | |
399 | //======================================================================= | |
a60b9727 | 400 | #ifdef _WIN32 |
35e08fe8 | 401 | static unsigned int __stdcall CpuFunc (void * /*param*/) |
47cbc555 | 402 | { |
0b309a75 | 403 | clock_t anElapCurrent; |
47cbc555 | 404 | clock_t aCurrent; |
0b309a75 | 405 | |
302f96fb | 406 | for(;;) |
47cbc555 A |
407 | { |
408 | Sleep (5); | |
409 | Standard_Real anUserSeconds, aSystemSeconds; | |
410 | OSD_Chronometer::GetProcessCPU (anUserSeconds, aSystemSeconds); | |
411 | aCurrent = clock_t(anUserSeconds + aSystemSeconds); | |
0b309a75 | 412 | anElapCurrent = clock_t(aTimer.ElapsedTime()); |
47cbc555 | 413 | |
d48df25d | 414 | if (CPU_LIMIT > 0 && (aCurrent - CPU_CURRENT) >= CPU_LIMIT) |
47cbc555 | 415 | { |
a60b9727 | 416 | cout << "Process killed by CPU limit (" << CPU_LIMIT << " sec)" << endl; |
0b309a75 | 417 | aTimer.Stop(); |
418 | ExitProcess (2); | |
419 | } | |
d48df25d | 420 | if (CPU_LIMIT > 0 && anElapCurrent >= CPU_LIMIT) |
0b309a75 | 421 | { |
422 | cout << "Process killed by elapsed limit (" << CPU_LIMIT << " sec)" << endl; | |
423 | aTimer.Stop(); | |
47cbc555 | 424 | ExitProcess (2); |
47cbc555 A |
425 | } |
426 | } | |
47cbc555 | 427 | } |
a60b9727 | 428 | #else |
0b309a75 | 429 | static void cpulimitSignalHandler (int) |
a60b9727 | 430 | { |
0b309a75 | 431 | cout << "Process killed by CPU limit (" << CPU_LIMIT << " sec)" << endl; |
a60b9727 | 432 | exit(2); |
433 | } | |
0b309a75 | 434 | static void *CpuFunc(void* /*threadarg*/) |
435 | { | |
436 | clock_t anElapCurrent; | |
437 | for(;;) | |
438 | { | |
439 | sleep (5); | |
440 | anElapCurrent = clock_t(aTimer.ElapsedTime()); | |
d48df25d | 441 | if (CPU_LIMIT >0 && (anElapCurrent) >= CPU_LIMIT) { |
0b309a75 | 442 | cout << "Process killed by elapsed limit (" << CPU_LIMIT << " sec)" << endl; |
443 | exit(2); | |
444 | } | |
445 | } | |
446 | return NULL; | |
447 | } | |
47cbc555 | 448 | #endif |
7fd59977 | 449 | |
35e08fe8 | 450 | #ifdef _WIN32 |
451 | static Standard_Integer cpulimit(Draw_Interpretor&, Standard_Integer n, const char** a) | |
452 | { | |
453 | #else | |
7fd59977 | 454 | static Standard_Integer cpulimit(Draw_Interpretor& di, Standard_Integer n, const char** a) |
455 | { | |
35e08fe8 | 456 | #endif |
0b309a75 | 457 | static int aFirst = 1; |
a60b9727 | 458 | #ifdef _WIN32 |
459 | // Windows specific code | |
47cbc555 A |
460 | unsigned int __stdcall CpuFunc (void *); |
461 | unsigned aThreadID; | |
462 | ||
0b309a75 | 463 | if (n <= 1){ |
a60b9727 | 464 | CPU_LIMIT = RLIM_INFINITY; |
0b309a75 | 465 | } else { |
91322f44 | 466 | CPU_LIMIT = Draw::Atoi (a[1]); |
47cbc555 A |
467 | Standard_Real anUserSeconds, aSystemSeconds; |
468 | OSD_Chronometer::GetProcessCPU (anUserSeconds, aSystemSeconds); | |
a60b9727 | 469 | CPU_CURRENT = clock_t(anUserSeconds + aSystemSeconds); |
0b309a75 | 470 | aTimer.Reset(); |
471 | aTimer.Start(); | |
47cbc555 A |
472 | if (aFirst) // Launch the thread only at the 1st call. |
473 | { | |
474 | aFirst = 0; | |
475 | _beginthreadex (NULL, 0, CpuFunc, NULL, 0, &aThreadID); | |
476 | } | |
7fd59977 | 477 | } |
478 | ||
a60b9727 | 479 | #else |
a60b9727 | 480 | // Unix & Linux |
a60b9727 | 481 | rlimit rlp; |
482 | rlp.rlim_max = RLIM_INFINITY; | |
483 | if (n <= 1) | |
484 | rlp.rlim_cur = RLIM_INFINITY; | |
485 | else | |
91322f44 | 486 | rlp.rlim_cur = Draw::Atoi(a[1]); |
a60b9727 | 487 | CPU_LIMIT = rlp.rlim_cur; |
488 | ||
489 | int status; | |
490 | status=setrlimit(RLIMIT_CPU,&rlp); | |
491 | if (status !=0) | |
492 | di << "status cpulimit setrlimit : " << status << "\n"; | |
493 | ||
494 | // set signal handler to print a message before death | |
495 | struct sigaction act, oact; | |
496 | memset (&act, 0, sizeof(act)); | |
0b309a75 | 497 | act.sa_handler = cpulimitSignalHandler; |
a60b9727 | 498 | sigaction (SIGXCPU, &act, &oact); |
499 | ||
0b309a75 | 500 | // cpulimit for elapsed time |
501 | aTimer.Reset(); | |
502 | aTimer.Start(); | |
503 | pthread_t cpulimitThread; | |
504 | if (aFirst) // Launch the thread only at the 1st call. | |
505 | { | |
506 | aFirst = 0; | |
507 | pthread_create(&cpulimitThread, NULL, CpuFunc, NULL); | |
508 | } | |
7fd59977 | 509 | #endif |
7fd59977 | 510 | return 0; |
511 | } | |
512 | ||
0b309a75 | 513 | |
7af17f1e MA |
514 | //======================================================================= |
515 | //function : mallochook | |
516 | //purpose : | |
517 | //======================================================================= | |
518 | ||
519 | static Standard_Integer mallochook(Draw_Interpretor& di, Standard_Integer n, | |
520 | const char** a) | |
521 | { | |
522 | if (n < 2) | |
523 | { | |
524 | di << "\ | |
525 | usage: mallochook cmd\n\ | |
526 | where cmd is one of:\n\ | |
527 | set [<op>] - set callback to malloc/free; op is one of the following:\n\ | |
528 | 0 - set callback to NULL,\n\ | |
529 | 1 - set callback OSD_MAllocHook::CollectBySize (default)\n\ | |
530 | 2 - set callback OSD_MAllocHook::LogFileHandler\n\ | |
531 | reset - reset the CollectBySize handler\n\ | |
532 | report1 [<outfile>]\n\ | |
533 | - write report from CollectBySize handler in <outfile>\n\ | |
534 | open [<logfile>]\n\ | |
535 | - open file for writing the log with LogFileHandler\n\ | |
536 | close - close the log file with LogFileHandler\n\ | |
537 | report2 [<flag>] [<logfile>] [<outfile>]\n\ | |
538 | - scan <logfile> written with LogFileHandler\n\ | |
539 | and make synthesized report in <outfile>; <flag> can be:\n\ | |
540 | 0 - simple stats by sizes (default),\n\ | |
541 | 1 - with alive allocation numbers\n\ | |
542 | By default <logfile> is \"mem-log.txt\", <outfile> is \"mem-stat.txt\"" | |
543 | << "\n"; | |
544 | return 0; | |
545 | } | |
546 | if (strcmp(a[1], "set") == 0) | |
547 | { | |
91322f44 | 548 | int aType = (n > 2 ? Draw::Atoi(a[2]) : 1); |
7af17f1e MA |
549 | if (aType < 0 || aType > 2) |
550 | { | |
586db386 | 551 | di << "unknown op of the command set\n"; |
7af17f1e MA |
552 | return 1; |
553 | } | |
554 | else if (aType == 0) | |
555 | { | |
556 | OSD_MAllocHook::SetCallback(NULL); | |
586db386 | 557 | di << "callback is unset\n"; |
7af17f1e MA |
558 | } |
559 | else if (aType == 1) | |
560 | { | |
561 | OSD_MAllocHook::SetCallback(OSD_MAllocHook::GetCollectBySize()); | |
586db386 | 562 | di << "callback is set to CollectBySize\n"; |
7af17f1e MA |
563 | } |
564 | else //if (aType == 2) | |
565 | { | |
566 | OSD_MAllocHook::SetCallback(OSD_MAllocHook::GetLogFileHandler()); | |
586db386 | 567 | di << "callback is set to LogFileHandler\n"; |
7af17f1e MA |
568 | } |
569 | } | |
570 | else if (strcmp(a[1], "reset") == 0) | |
571 | { | |
572 | OSD_MAllocHook::GetCollectBySize()->Reset(); | |
586db386 | 573 | di << "CollectBySize handler is reset\n"; |
7af17f1e MA |
574 | } |
575 | else if (strcmp(a[1], "open") == 0) | |
576 | { | |
577 | const char* aFileName = (n > 2 ? a[2] : "mem-log.txt"); | |
578 | if (!OSD_MAllocHook::GetLogFileHandler()->Open(aFileName)) | |
579 | { | |
586db386 | 580 | di << "cannot create file " << aFileName << " for writing\n"; |
7af17f1e MA |
581 | return 1; |
582 | } | |
586db386 | 583 | di << "log file " << aFileName << " is opened for writing\n"; |
7af17f1e MA |
584 | } |
585 | else if (strcmp(a[1], "close") == 0) | |
586 | { | |
587 | OSD_MAllocHook::GetLogFileHandler()->Close(); | |
586db386 | 588 | di << "log file is closed\n"; |
7af17f1e MA |
589 | } |
590 | else if (strcmp(a[1], "report1") == 0) | |
591 | { | |
592 | const char* aOutFile = "mem-stat.txt"; | |
593 | if (n > 2) | |
594 | aOutFile = a[2]; | |
595 | if (OSD_MAllocHook::GetCollectBySize()->MakeReport(aOutFile)) | |
596 | { | |
586db386 | 597 | di << "report " << aOutFile << " has been created\n"; |
7af17f1e MA |
598 | } |
599 | else | |
600 | { | |
601 | di << "cannot create report " << aOutFile << "\n"; | |
602 | return 1; | |
603 | } | |
604 | } | |
605 | else if (strcmp(a[1], "report2") == 0) | |
606 | { | |
607 | Standard_Boolean includeAlive = Standard_False; | |
608 | const char* aLogFile = "mem-log.txt"; | |
609 | const char* aOutFile = "mem-stat.txt"; | |
610 | if (n > 2) | |
611 | { | |
91322f44 | 612 | includeAlive = (Draw::Atoi(a[2]) != 0); |
7af17f1e MA |
613 | if (n > 3) |
614 | { | |
615 | aLogFile = a[3]; | |
616 | if (n > 4) | |
617 | aOutFile = a[4]; | |
618 | } | |
619 | } | |
620 | if (OSD_MAllocHook::LogFileHandler::MakeReport(aLogFile, aOutFile, includeAlive)) | |
621 | { | |
586db386 | 622 | di << "report " << aOutFile << " has been created\n"; |
7af17f1e MA |
623 | } |
624 | else | |
625 | { | |
626 | di << "cannot create report " << aOutFile << " from the log file " | |
627 | << aLogFile << "\n"; | |
628 | return 1; | |
629 | } | |
630 | } | |
631 | else | |
632 | { | |
633 | di << "unrecognized command " << a[1] << "\n"; | |
634 | return 1; | |
635 | } | |
636 | return 0; | |
637 | } | |
638 | ||
91322f44 | 639 | //============================================================================== |
640 | //function : dlocale | |
641 | //purpose : | |
642 | //============================================================================== | |
643 | ||
644 | static int dlocale (Draw_Interpretor& di, Standard_Integer n, const char** argv) | |
645 | { | |
646 | int category = LC_ALL; | |
647 | if (n > 1) | |
648 | { | |
649 | const char *cat = argv[1]; | |
650 | if ( ! strcmp (cat, "LC_ALL") ) category = LC_ALL; | |
651 | else if ( ! strcmp (cat, "LC_COLLATE") ) category = LC_COLLATE; | |
652 | else if ( ! strcmp (cat, "LC_CTYPE") ) category = LC_CTYPE; | |
653 | else if ( ! strcmp (cat, "LC_MONETARY") ) category = LC_MONETARY; | |
654 | else if ( ! strcmp (cat, "LC_NUMERIC") ) category = LC_NUMERIC; | |
655 | else if ( ! strcmp (cat, "LC_TIME") ) category = LC_TIME; | |
656 | else | |
657 | { | |
658 | cout << "Error: cannot recognize argument " << cat << " as one of LC_ macros" << endl; | |
659 | return 1; | |
660 | } | |
661 | } | |
662 | const char* locale = (n > 2 ? argv[2] : NULL); | |
663 | const char* result = setlocale (category, locale); | |
664 | if (result) | |
665 | di << result; | |
666 | else | |
667 | cout << "Error: unsupported locale specification: " << locale << endl; | |
668 | return 0; | |
669 | } | |
670 | ||
f0430952 | 671 | //============================================================================== |
672 | //function : dmeminfo | |
673 | //purpose : | |
674 | //============================================================================== | |
675 | ||
676 | static int dmeminfo (Draw_Interpretor& theDI, | |
677 | Standard_Integer theArgNb, | |
678 | const char** theArgVec) | |
679 | { | |
680 | OSD_MemInfo aMemInfo; | |
681 | if (theArgNb <= 1) | |
682 | { | |
683 | theDI << aMemInfo.ToString(); | |
684 | return 0; | |
685 | } | |
686 | ||
687 | for (Standard_Integer anIter = 1; anIter < theArgNb; ++anIter) | |
688 | { | |
689 | TCollection_AsciiString anArg (theArgVec[anIter]); | |
690 | anArg.LowerCase(); | |
691 | if (anArg == "virt" || anArg == "v") | |
692 | { | |
693 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemVirtual)) << " "; | |
694 | } | |
67a1064e | 695 | else if (anArg == "heap" || anArg == "h") |
696 | { | |
697 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemHeapUsage)) << " "; | |
698 | } | |
f0430952 | 699 | else if (anArg == "wset" || anArg == "w") |
700 | { | |
701 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemWorkingSet)) << " "; | |
702 | } | |
703 | else if (anArg == "wsetpeak") | |
704 | { | |
705 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemWorkingSetPeak)) << " "; | |
706 | } | |
707 | else if (anArg == "swap") | |
708 | { | |
709 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemSwapUsage)) << " "; | |
710 | } | |
711 | else if (anArg == "swappeak") | |
712 | { | |
713 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemSwapUsagePeak)) << " "; | |
714 | } | |
715 | else if (anArg == "private") | |
716 | { | |
717 | theDI << Standard_Real (aMemInfo.Value (OSD_MemInfo::MemPrivate)) << " "; | |
718 | } | |
719 | else | |
720 | { | |
721 | std::cerr << "Unknown argument '" << theArgVec[anIter] << "'!\n"; | |
722 | } | |
723 | } | |
724 | theDI << "\n"; | |
725 | return 0; | |
726 | } | |
7fd59977 | 727 | |
618617fe | 728 | //============================================================================== |
729 | //function : dperf | |
730 | //purpose : | |
731 | //============================================================================== | |
732 | ||
733 | static int dperf (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec) | |
734 | { | |
735 | // reset if argument is provided and it is not '0' | |
736 | int reset = (theArgNb > 1 ? theArgVec[1][0] != '0' && theArgVec[1][0] != '\0' : 0); | |
737 | char buffer[25600]; | |
738 | perf_sprint_all_meters (buffer, 25600 - 1, reset); | |
739 | theDI << buffer; | |
740 | return 0; | |
741 | } | |
742 | ||
785a9540 | 743 | //============================================================================== |
744 | //function : dtracelevel | |
745 | //purpose : | |
746 | //============================================================================== | |
747 | ||
748 | static int dtracelevel (Draw_Interpretor& theDI, | |
749 | Standard_Integer theArgNb, | |
750 | const char** theArgVec) | |
751 | { | |
752 | Message_Gravity aLevel = Message_Info; | |
753 | if (theArgNb < 1 || theArgNb > 2) | |
754 | { | |
755 | std::cout << "Error: wrong number of arguments! See usage:\n"; | |
756 | theDI.PrintHelp (theArgVec[0]); | |
757 | return 1; | |
758 | } | |
759 | else if (theArgNb == 2) | |
760 | { | |
761 | TCollection_AsciiString aVal (theArgVec[1]); | |
762 | aVal.LowerCase(); | |
763 | if (aVal == "trace") | |
764 | { | |
765 | aLevel = Message_Trace; | |
766 | } | |
767 | else if (aVal == "info") | |
768 | { | |
769 | aLevel = Message_Info; | |
770 | } | |
771 | else if (aVal == "warn" | |
772 | || aVal == "warning") | |
773 | { | |
774 | aLevel = Message_Warning; | |
775 | } | |
776 | else if (aVal == "alarm") | |
777 | { | |
778 | aLevel = Message_Alarm; | |
779 | } | |
780 | else if (aVal == "fail") | |
781 | { | |
782 | aLevel = Message_Fail; | |
783 | } | |
784 | else | |
785 | { | |
786 | std::cout << "Error: unknown gravity '" << theArgVec[1] << "'!\n"; | |
787 | return 1; | |
788 | } | |
789 | } | |
790 | ||
791 | Handle(Message_Messenger) aMessenger = Message::DefaultMessenger(); | |
792 | if (aMessenger.IsNull()) | |
793 | { | |
794 | std::cout << "Error: default messenger is unavailable!\n"; | |
795 | return 1; | |
796 | } | |
797 | ||
798 | Message_SequenceOfPrinters& aPrinters = aMessenger->ChangePrinters(); | |
799 | if (aPrinters.Length() < 1) | |
800 | { | |
801 | std::cout << "Error: no printers registered in default Messenger!\n"; | |
802 | return 0; | |
803 | } | |
804 | ||
805 | for (Standard_Integer aPrinterIter = 1; aPrinterIter <= aPrinters.Length(); ++aPrinterIter) | |
806 | { | |
807 | Handle(Message_Printer)& aPrinter = aPrinters.ChangeValue (aPrinterIter); | |
808 | if (theArgNb == 1) | |
809 | { | |
810 | if (aPrinterIter == 1) | |
811 | { | |
812 | aLevel = aPrinter->GetTraceLevel(); | |
813 | } | |
814 | else if (aLevel == aPrinter->GetTraceLevel()) | |
815 | { | |
816 | continue; | |
817 | } | |
818 | ||
819 | switch (aPrinter->GetTraceLevel()) | |
820 | { | |
821 | case Message_Trace: theDI << "trace"; break; | |
822 | case Message_Info: theDI << "info"; break; | |
823 | case Message_Warning: theDI << "warn"; break; | |
824 | case Message_Alarm: theDI << "alarm"; break; | |
825 | case Message_Fail: theDI << "fail"; break; | |
826 | } | |
827 | continue; | |
828 | } | |
829 | ||
830 | aPrinter->SetTraceLevel (aLevel); | |
831 | } | |
832 | ||
833 | return 0; | |
834 | } | |
835 | ||
7fd59977 | 836 | void Draw::BasicCommands(Draw_Interpretor& theCommands) |
837 | { | |
838 | static Standard_Boolean Done = Standard_False; | |
839 | if (Done) return; | |
840 | Done = Standard_True; | |
841 | ||
aa02980d | 842 | ios::sync_with_stdio(); |
843 | ||
7fd59977 | 844 | const char* g = "DRAW General Commands"; |
785a9540 | 845 | |
7fd59977 | 846 | theCommands.Add("batch", "returns 1 in batch mode", |
847 | __FILE__,ifbatch,g); | |
848 | theCommands.Add("spy","spy [file], Save commands in file. no file close", | |
849 | __FILE__,spy,g); | |
850 | theCommands.Add("wait","wait [time(10)], wait time seconds", | |
851 | __FILE__,Draw_wait,g); | |
852 | theCommands.Add("cpulimit","cpulimit [nbseconds], no args remove limits", | |
853 | __FILE__,cpulimit,g); | |
854 | theCommands.Add("chrono","chrono [ name start/stop/reset/show]", | |
855 | __FILE__,chronom,g); | |
856 | theCommands.Add("dchrono","dchrono [ name start/stop/reset/show]", | |
857 | __FILE__,dchronom,g); | |
7af17f1e MA |
858 | theCommands.Add("mallochook", |
859 | "debug memory allocation/deallocation, w/o args for help", | |
860 | __FILE__, mallochook, g); | |
f0430952 | 861 | theCommands.Add ("meminfo", |
67a1064e | 862 | "meminfo [virt|v] [heap|h] [wset|w] [wsetpeak] [swap] [swappeak] [private]" |
f0430952 | 863 | " : memory counters for this process", |
864 | __FILE__, dmeminfo, g); | |
618617fe | 865 | theCommands.Add("dperf","dperf [reset] -- show performance counters, reset if argument is provided", |
866 | __FILE__,dperf,g); | |
aa02980d | 867 | |
785a9540 | 868 | // Logging commands; note that their names are hard-coded in the code |
aa02980d | 869 | // of Draw_Interpretor, thus should not be changed without update of that code! |
870 | theCommands.Add("dlog", "manage logging of commands and output; run without args to get help", | |
871 | __FILE__,dlog,g); | |
872 | theCommands.Add("decho", "switch on / off echo of commands to cout; run without args to get help", | |
873 | __FILE__,decho,g); | |
785a9540 | 874 | theCommands.Add("dtracelevel", "dtracelevel [trace|info|warn|alarm|fail]", |
875 | __FILE__, dtracelevel, g); | |
876 | ||
8a262fa1 | 877 | theCommands.Add("dbreak", "raises Tcl exception if user has pressed Control-Break key", |
878 | __FILE__,dbreak,g); | |
879 | theCommands.Add("dversion", "provides information on OCCT build configuration (version, compiler, OS, C library, etc.)", | |
880 | __FILE__,dversion,g); | |
91322f44 | 881 | theCommands.Add("dlocale", "set and / or query locate of C subsystem (function setlocale())", |
882 | __FILE__,dlocale,g); | |
7fd59977 | 883 | } |