1 // Created on: 1995-02-23
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
19 #include <Draw_Appli.hxx>
20 #include <Draw_Chronometer.hxx>
21 #include <Draw_Drawable3D.hxx>
22 #include <Draw_Printer.hxx>
23 #include <Draw_ProgressIndicator.hxx>
24 #include <Message.hxx>
25 #include <Message_Messenger.hxx>
26 #include <Message_PrinterOStream.hxx>
28 #include <OSD_Chronometer.hxx>
29 #include <OSD_Environment.hxx>
30 #include <OSD_Exception_CTRL_BREAK.hxx>
31 #include <OSD_MAllocHook.hxx>
32 #include <OSD_MemInfo.hxx>
33 #include <OSD_Parallel.hxx>
34 #include <OSD_ThreadPool.hxx>
35 #include <Standard_Macro.hxx>
36 #include <Standard_SStream.hxx>
37 #include <Standard_Stream.hxx>
38 #include <Standard_Version.hxx>
39 #include <TCollection_AsciiString.hxx>
41 #include <OSD_PerfMeter.h>
52 #define RLIM_INFINITY 0x7fffffff
54 static clock_t CPU_CURRENT; // cpu time already used at last
55 // cpulimit call. (sec.)
58 #include <sys/resource.h>
62 #if defined (__hpux) || defined ( HPUX )
63 #define RLIM_INFINITY 0x7fffffff
69 extern Standard_Boolean Draw_Batch;
71 static clock_t CPU_LIMIT; // Cpu_limit in Sec.
72 static OSD_Timer aTimer;
74 //=======================================================================
76 //=======================================================================
78 extern Standard_Boolean Draw_Chrono;
80 static Standard_Integer chronom(Draw_Interpretor& di,
81 Standard_Integer n,const char** a)
83 if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) {
85 Draw_Chrono = !Draw_Chrono;
87 Draw_Chrono = (*a[1] == '1');
89 if (Draw_Chrono) di << "Chronometers activated.\n";
90 else di << "Chronometers deactivated.\n";
93 Handle(Draw_Drawable3D) D = Draw::Get(a[1]);
94 Handle(Draw_Chronometer) C;
96 C = Handle(Draw_Chronometer)::DownCast(D);
99 C = new Draw_Chronometer();
100 Draw::Set(a[1],C,Standard_False);
106 for (Standard_Integer anIter = 2; anIter < n; ++anIter)
108 TCollection_AsciiString anArg (a[anIter]);
111 if (anArg == "reset")
115 else if (anArg == "restart")
117 C->Timer().Restart();
119 else if (anArg == "start")
123 else if (anArg == "stop")
127 else if (anArg == "show")
131 else if (anArg == "counter")
133 Standard_Real aSeconds,aCPUtime;
134 Standard_Integer aMinutes, aHours;
135 C->Timer().Show(aSeconds,aMinutes,aHours,aCPUtime);
136 std::cout << "COUNTER " << a[++anIter] << ": " << aCPUtime << "\n";
140 std::cerr << "Unknown argument '" << a[anIter] << "'!\n";
148 static Standard_Integer dchronom(Draw_Interpretor& theDI,
149 Standard_Integer n,const char** a)
151 if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) {
153 Draw_Chrono = !Draw_Chrono;
155 Draw_Chrono = (*a[1] == '1');
157 if (Draw_Chrono) theDI << "Chronometers activated.\n";
158 else theDI << "Chronometers deactivated.\n";
161 Handle(Draw_Drawable3D) D = Draw::Get(a[1]);
162 Handle(Draw_Chronometer) C;
164 C = Handle(Draw_Chronometer)::DownCast(D);
167 C = new Draw_Chronometer();
168 Draw::Set(a[1],C,Standard_False);
174 for (Standard_Integer anIter = 2; anIter < n; ++anIter)
176 TCollection_AsciiString anArg (a[anIter]);
179 if (anArg == "reset")
183 else if (anArg == "restart")
185 C->Timer().Restart();
187 else if (anArg == "start")
191 else if (anArg == "stop")
195 else if (anArg == "show")
201 else if (anArg == "counter")
203 Standard_Real aSeconds,aCPUtime;
204 Standard_Integer aMinutes, aHours;
205 C->Timer().Show(aSeconds,aMinutes,aHours,aCPUtime);
206 theDI << "COUNTER " << a[++anIter] << ": " << aCPUtime << "\n";
210 theDI << "Unknown argument '" << a[anIter] << "'!\n";
220 //=======================================================================
223 //=======================================================================
225 static Standard_Integer ifbatch(Draw_Interpretor& DI, Standard_Integer , const char** )
235 //=======================================================================
238 //=======================================================================
240 extern Standard_Boolean Draw_Spying;
241 extern std::filebuf Draw_Spyfile;
243 static Standard_Integer spy(Draw_Interpretor& di, Standard_Integer n, const char** a)
246 Draw_Spyfile.close();
247 Draw_Spying = Standard_False;
249 if (!Draw_Spyfile.open(a[1],std::ios::out)) {
250 di << "Cannot open "<<a[1]<<" for writing\n";
253 Draw_Spying = Standard_True;
258 static Standard_Integer dlog(Draw_Interpretor& di, Standard_Integer n, const char** a)
260 if (n != 2 && n != 3)
262 Message::SendFail() << "Enable or disable logging: " << a[0] << " {on|off}\n"
263 << "Reset log: " << a[0] << " reset\n"
264 << "Get log content: " << a[0] << " get";
268 if (! strcmp (a[1], "on") && n == 2)
270 di.SetDoLog (Standard_True);
271 // di.Log() << "dlog on" << std::endl; // for symmetry
273 else if (! strcmp (a[1], "off") && n == 2)
275 di.SetDoLog (Standard_False);
277 else if (! strcmp (a[1], "reset") && n == 2)
281 else if (! strcmp (a[1], "get") && n == 2)
285 else if (! strcmp (a[1], "add") && n == 3)
290 else if (! strcmp (a[1], "status") && n == 2)
292 di << (di.GetDoLog() ? "on" : "off");
295 Message::SendFail() << "Unrecognized option(s): " << a[1];
301 static Standard_Integer decho(Draw_Interpretor& di, Standard_Integer n, const char** a)
305 Message::SendFail() << "Enable or disable echoing: " << a[0] << " {on|off}";
309 if (! strcmp (a[1], "on"))
311 di.SetDoEcho (Standard_True);
313 else if (! strcmp (a[1], "off"))
315 di.SetDoEcho (Standard_False);
318 Message::SendFail() << "Unrecognized option: " << a[1];
324 static Standard_Integer dbreak(Draw_Interpretor& di, Standard_Integer, const char**)
329 catch (OSD_Exception_CTRL_BREAK const&) {
330 di << "User pressed Control-Break";
331 return 1; // Tcl exception
337 static Standard_Integer dversion(Draw_Interpretor& di, Standard_Integer, const char**)
339 // print OCCT version and OCCTY-specific macros used
340 di << "Open CASCADE Technology " << OCC_VERSION_STRING_EXT << "\n";
342 di << "Extended debug mode\n";
343 #elif defined(_DEBUG)
344 di << "Debug mode\n";
347 di << "Tk enabled (HAVE_TK)\n";
349 di << "Tk disabled\n";
352 di << "Xlib enabled (HAVE_XLIB)\n";
353 #elif !defined(_WIN32)
354 di << "Xlib disabled\n";
357 di << "TBB enabled (HAVE_TBB)\n";
359 di << "TBB disabled\n";
362 di << "FreeType enabled (HAVE_FREETYPE)\n";
364 di << "FreeType disabled\n";
366 #ifdef HAVE_FREEIMAGE
367 di << "FreeImage enabled (HAVE_FREEIMAGE)\n";
369 di << "FreeImage disabled\n";
372 di << "FFmpeg enabled (HAVE_FFMPEG)\n";
374 di << "FFmpeg disabled\n";
376 #ifdef HAVE_OPENGL_EXT
377 di << "OpenGL: enabled (HAVE_OPENGL_EXT)\n";
379 #ifdef HAVE_GLES2_EXT
380 di << "OpenGL ES: enabled (HAVE_GLES2_EXT)\n";
383 di << "OpenVR enabled (HAVE_OPENVR)\n";
385 di << "OpenVR disabled\n";
387 #ifdef HAVE_RAPIDJSON
388 di << "RapidJSON enabled (HAVE_RAPIDJSON)\n";
390 di << "RapidJSON disabled\n";
393 di << "Draco enabled (HAVE_DRACO)\n";
395 di << "Draco disabled\n";
398 di << "VTK enabled (HAVE_VTK)\n";
400 di << "VTK disabled\n";
403 di << "Exceptions disabled (No_Exception)\n";
405 di << "Exceptions enabled\n";
408 // check compiler, OS, etc. using pre-processor macros provided by compiler
409 // see "Pre-defined C/C++ Compiler Macros" http://sourceforge.net/p/predef/wiki/
410 // note that only modern compilers that are known to be used for OCCT are recognized
412 // compiler; note that GCC and MSVC are last as other compilers (e.g. Intel) can also define __GNUC__ and _MSC_VER
413 #if defined(__INTEL_COMPILER)
414 di << "Compiler: Intel " << __INTEL_COMPILER << "\n";
415 #elif defined(__BORLANDC__)
416 di << "Compiler: Borland C++ (__BORLANDC__ = " << __BORLANDC__ << ")\n";
417 #elif defined(__clang__)
418 di << "Compiler: Clang " << __clang_major__ << "." << __clang_minor__ << "." << __clang_patchlevel__ << "\n";
419 #elif defined(__SUNPRO_C)
420 di << "Compiler: Sun Studio (__SUNPRO_C = " << __SUNPROC_C << ")\n";
421 #elif defined(_MSC_VER)
423 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";
425 di << "Compiler: MS Visual C++ " << (int)(_MSC_VER/100-5) << "." << (int)((_MSC_VER/10)-50-10*(int)(_MSC_VER/100-5)) << " (_MSC_FULL_VER = " << _MSC_FULL_VER << ")\n";
427 #elif defined(__GNUC__)
428 di << "Compiler: GCC " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << "\n";
430 di << "Compiler: unrecognized\n";
433 // Cygwin and MinGW specifics
434 #if defined(__CYGWIN__)
437 #if defined(__MINGW64__)
438 di << "MinGW 64 " << __MINGW64_VERSION_MAJOR << "." << __MINGW64_VERSION_MINOR << "\n";
439 #elif defined(__MINGW32__)
440 di << "MinGW 32 " << __MINGW32_MAJOR_VERSION << "." << __MINGW32_MINOR_VERSION << "\n";
444 #if defined(__amd64) || defined(__x86_64) || defined(_M_AMD64)
445 di << "Architecture: AMD64\n";
446 #elif defined(__i386) || defined(_M_IX86) || defined(__X86__)|| defined(_X86_)
447 di << "Architecture: Intel x86\n";
448 #elif defined(_M_IA64) || defined(__ia64__)
449 di << "Architecture: Intel Itanium (IA 64)\n";
450 #elif defined(__sparc__) || defined(__sparc)
451 di << "Architecture: SPARC\n";
452 #elif defined(__aarch64__) && defined(__LP64__)
453 di << "Architecture: ARM 64-bit\n";
454 #elif defined(__arm__) || defined(__arm64__)
455 #if defined(__LP64__)
456 di << "Architecture: ARM 64-bit\n";
458 di << "Architecture: ARM 32-bit\n";
460 #elif defined(__EMSCRIPTEN__)
461 di << "Architecture: WASM "
462 #if defined(__LP64__)
467 #if defined(__wasm_simd128__)
472 di << "Architecture: unrecognized\n";
476 #if defined(_WIN32) || defined(__WINDOWS__) || defined(__WIN32__)
477 di << "OS: Windows\n";
478 #elif defined(__APPLE__) || defined(__MACH__)
479 di << "OS: Mac OS X\n";
481 di << "OS: SUN Solaris\n";
482 #elif defined(__ANDROID__) /* must be before Linux */
483 #include <android/api-level.h>
484 di << "OS: Android (__ANDROID_API__ = " << __ANDROID_API__ << ")\n";
485 #elif defined(__QNXNTO__)
486 di << "OS: QNX Neutrino\n";
487 #elif defined(__QNX__)
489 #elif defined(__linux__)
491 #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
492 #include <sys/param.h>
493 di << "OS: BSD (BSD = " << BSD << ")\n";
494 #elif defined(__EMSCRIPTEN__)
495 di << "OS: WebAssembly (Emscripten SDK " << __EMSCRIPTEN_major__ << "." << __EMSCRIPTEN_minor__ << "." << __EMSCRIPTEN_tiny__
496 #ifdef __EMSCRIPTEN_PTHREADS__
503 di << "OS: unrecognized\n";
509 //=======================================================================
512 //=======================================================================
514 static Standard_Integer Draw_wait(Draw_Interpretor& , Standard_Integer n, const char** a)
516 Standard_Integer w = 10;
518 w = Draw::Atoi(a[1]);
519 time_t ct = time(NULL) + w;
520 while (time(NULL) < ct) {};
524 //=======================================================================
525 //function : cpulimit
527 //=======================================================================
529 static unsigned int __stdcall CpuFunc (void * /*param*/)
531 clock_t anElapCurrent;
537 Standard_Real anUserSeconds, aSystemSeconds;
538 OSD_Chronometer::GetProcessCPU (anUserSeconds, aSystemSeconds);
539 aCurrent = clock_t(anUserSeconds + aSystemSeconds);
540 anElapCurrent = clock_t(aTimer.ElapsedTime());
542 if (CPU_LIMIT > 0 && (aCurrent - CPU_CURRENT) >= CPU_LIMIT)
545 if (IsDebuggerPresent())
547 std::cout << "Info: CPU limit (" << CPU_LIMIT << " sec) has been reached but ignored because of attached Debugger" << std::endl;
552 std::cout << "ERROR: Process killed by CPU limit (" << CPU_LIMIT << " sec)" << std::endl;
556 if (CPU_LIMIT > 0 && anElapCurrent >= CPU_LIMIT)
559 if (IsDebuggerPresent())
561 std::cout << "Info: Elapsed limit (" << CPU_LIMIT << " sec) has been reached but ignored because of attached Debugger" << std::endl;
566 std::cout << "ERROR: Process killed by elapsed limit (" << CPU_LIMIT << " sec)" << std::endl;
573 static void cpulimitSignalHandler (int)
575 std::cout << "Process killed by CPU limit (" << CPU_LIMIT << " sec)" << std::endl;
578 static void *CpuFunc(void* /*threadarg*/)
580 clock_t anElapCurrent;
584 anElapCurrent = clock_t(aTimer.ElapsedTime());
585 if (CPU_LIMIT >0 && (anElapCurrent) >= CPU_LIMIT) {
586 std::cout << "Process killed by elapsed limit (" << CPU_LIMIT << " sec)" << std::endl;
594 // Returns time in seconds defined by the argument string,
595 // multiplied by factor defined in environment variable
596 // CSF_CPULIMIT_FACTOR (if it exists, 1 otherwise)
597 static clock_t GetCpuLimit (const Standard_CString theParam)
599 clock_t aValue = Draw::Atoi (theParam);
601 OSD_Environment aEnv("CSF_CPULIMIT_FACTOR");
602 TCollection_AsciiString aEnvStr = aEnv.Value();
603 if (!aEnvStr.IsEmpty())
605 aValue *= Draw::Atoi (aEnvStr.ToCString());
610 static Standard_Integer cpulimit(Draw_Interpretor& di, Standard_Integer n, const char** a)
612 static int aFirst = 1;
614 // Windows specific code
615 unsigned int __stdcall CpuFunc (void *);
619 CPU_LIMIT = RLIM_INFINITY;
621 CPU_LIMIT = GetCpuLimit (a[1]);
622 Standard_Real anUserSeconds, aSystemSeconds;
623 OSD_Chronometer::GetProcessCPU (anUserSeconds, aSystemSeconds);
624 CPU_CURRENT = clock_t(anUserSeconds + aSystemSeconds);
627 if (aFirst) // Launch the thread only at the 1st call.
630 _beginthreadex (NULL, 0, CpuFunc, NULL, 0, &aThreadID);
637 rlp.rlim_max = RLIM_INFINITY;
639 rlp.rlim_cur = RLIM_INFINITY;
641 rlp.rlim_cur = GetCpuLimit (a[1]);
642 CPU_LIMIT = rlp.rlim_cur;
645 status=setrlimit(RLIMIT_CPU,&rlp);
647 di << "status cpulimit setrlimit : " << status << "\n";
649 // set signal handler to print a message before death
650 struct sigaction act, oact;
651 memset (&act, 0, sizeof(act));
652 act.sa_handler = cpulimitSignalHandler;
653 sigaction (SIGXCPU, &act, &oact);
655 // cpulimit for elapsed time
658 pthread_t cpulimitThread;
659 if (aFirst) // Launch the thread only at the 1st call.
662 pthread_create(&cpulimitThread, NULL, CpuFunc, NULL);
665 di << "CPU and elapsed time limit set to " << (double)CPU_LIMIT << " seconds";
669 //=======================================================================
670 //function : mallochook
672 //=======================================================================
674 static Standard_Integer mallochook(Draw_Interpretor& di, Standard_Integer n,
680 usage: mallochook cmd\n\
681 where cmd is one of:\n\
682 set [<op>] - set callback to malloc/free; op is one of the following:\n\
683 0 - set callback to NULL,\n\
684 1 - set callback OSD_MAllocHook::CollectBySize (default)\n\
685 2 - set callback OSD_MAllocHook::LogFileHandler\n\
686 reset - reset the CollectBySize handler\n\
687 report1 [<outfile>]\n\
688 - write report from CollectBySize handler in <outfile>\n\
690 - open file for writing the log with LogFileHandler\n\
691 close - close the log file with LogFileHandler\n\
692 report2 [<flag>] [<logfile>] [<outfile>]\n\
693 - scan <logfile> written with LogFileHandler\n\
694 and make synthesized report in <outfile>; <flag> can be:\n\
695 0 - simple stats by sizes (default),\n\
696 1 - with alive allocation numbers\n\
697 By default <logfile> is \"mem-log.txt\", <outfile> is \"mem-stat.txt\""
701 if (strcmp(a[1], "set") == 0)
703 int aType = (n > 2 ? Draw::Atoi(a[2]) : 1);
704 if (aType < 0 || aType > 2)
706 di << "unknown op of the command set\n";
711 OSD_MAllocHook::SetCallback(NULL);
712 di << "callback is unset\n";
716 OSD_MAllocHook::SetCallback(OSD_MAllocHook::GetCollectBySize());
717 di << "callback is set to CollectBySize\n";
719 else //if (aType == 2)
721 OSD_MAllocHook::SetCallback(OSD_MAllocHook::GetLogFileHandler());
722 di << "callback is set to LogFileHandler\n";
725 else if (strcmp(a[1], "reset") == 0)
727 OSD_MAllocHook::GetCollectBySize()->Reset();
728 di << "CollectBySize handler is reset\n";
730 else if (strcmp(a[1], "open") == 0)
732 const char* aFileName = (n > 2 ? a[2] : "mem-log.txt");
733 if (!OSD_MAllocHook::GetLogFileHandler()->Open(aFileName))
735 di << "cannot create file " << aFileName << " for writing\n";
738 di << "log file " << aFileName << " is opened for writing\n";
740 else if (strcmp(a[1], "close") == 0)
742 OSD_MAllocHook::GetLogFileHandler()->Close();
743 di << "log file is closed\n";
745 else if (strcmp(a[1], "report1") == 0)
747 const char* aOutFile = "mem-stat.txt";
750 if (OSD_MAllocHook::GetCollectBySize()->MakeReport(aOutFile))
752 di << "report " << aOutFile << " has been created\n";
756 di << "cannot create report " << aOutFile << "\n";
760 else if (strcmp(a[1], "report2") == 0)
762 Standard_Boolean includeAlive = Standard_False;
763 const char* aLogFile = "mem-log.txt";
764 const char* aOutFile = "mem-stat.txt";
767 includeAlive = (Draw::Atoi(a[2]) != 0);
775 if (OSD_MAllocHook::LogFileHandler::MakeReport(aLogFile, aOutFile, includeAlive))
777 di << "report " << aOutFile << " has been created\n";
781 di << "cannot create report " << aOutFile << " from the log file "
788 di << "unrecognized command " << a[1] << "\n";
794 //==============================================================================
797 //==============================================================================
799 static int dlocale (Draw_Interpretor& di, Standard_Integer n, const char** argv)
801 int category = LC_ALL;
804 const char *cat = argv[1];
805 if ( ! strcmp (cat, "LC_ALL") ) category = LC_ALL;
806 else if ( ! strcmp (cat, "LC_COLLATE") ) category = LC_COLLATE;
807 else if ( ! strcmp (cat, "LC_CTYPE") ) category = LC_CTYPE;
808 else if ( ! strcmp (cat, "LC_MONETARY") ) category = LC_MONETARY;
809 else if ( ! strcmp (cat, "LC_NUMERIC") ) category = LC_NUMERIC;
810 else if ( ! strcmp (cat, "LC_TIME") ) category = LC_TIME;
813 Message::SendFail() << "Error: cannot recognize argument " << cat << " as one of LC_ macros";
817 const char* locale = (n > 2 ? argv[2] : NULL);
818 const char* result = setlocale (category, locale);
822 std::cout << "Error: unsupported locale specification: " << locale << std::endl;
826 //==============================================================================
827 //function : dmeminfo
829 //==============================================================================
831 static int dmeminfo (Draw_Interpretor& theDI,
832 Standard_Integer theArgNb,
833 const char** theArgVec)
837 OSD_MemInfo aMemInfo;
838 theDI << aMemInfo.ToString();
842 NCollection_Map<OSD_MemInfo::Counter> aCounters;
843 for (Standard_Integer anIter = 1; anIter < theArgNb; ++anIter)
845 TCollection_AsciiString anArg (theArgVec[anIter]);
847 if (anArg == "virt" || anArg == "v")
849 aCounters.Add (OSD_MemInfo::MemVirtual);
851 else if (anArg == "heap" || anArg == "h")
853 aCounters.Add (OSD_MemInfo::MemHeapUsage);
855 else if (anArg == "wset" || anArg == "w")
857 aCounters.Add (OSD_MemInfo::MemWorkingSet);
859 else if (anArg == "wsetpeak")
861 aCounters.Add (OSD_MemInfo::MemWorkingSetPeak);
863 else if (anArg == "swap")
865 aCounters.Add (OSD_MemInfo::MemSwapUsage);
867 else if (anArg == "swappeak")
869 aCounters.Add (OSD_MemInfo::MemSwapUsagePeak);
871 else if (anArg == "private")
873 aCounters.Add (OSD_MemInfo::MemPrivate);
877 std::cerr << "Unknown argument '" << theArgVec[anIter] << "'!\n";
881 OSD_MemInfo aMemInfo (Standard_False);
882 aMemInfo.SetActive (Standard_False);
883 for (NCollection_Map<OSD_MemInfo::Counter>::Iterator aCountersIt (aCounters); aCountersIt.More(); aCountersIt.Next())
885 aMemInfo.SetActive (aCountersIt.Value(), Standard_True);
889 for (NCollection_Map<OSD_MemInfo::Counter>::Iterator aCountersIt (aCounters); aCountersIt.More(); aCountersIt.Next())
891 theDI << Standard_Real (aMemInfo.Value (aCountersIt.Value())) << " ";
897 //==============================================================================
898 //function : dparallel
900 //==============================================================================
901 static int dparallel (Draw_Interpretor& theDI,
902 Standard_Integer theArgNb,
903 const char** theArgVec)
905 const Handle(OSD_ThreadPool)& aDefPool = OSD_ThreadPool::DefaultPool();
908 theDI << "NbLogicalProcessors: " << OSD_Parallel::NbLogicalProcessors() << "\n"
909 << "NbThreads: " << aDefPool->NbThreads() << "\n"
910 << "NbDefThreads: " << aDefPool->NbDefaultThreadsToLaunch() << "\n"
911 << "UseOcct: " << (OSD_Parallel::ToUseOcctThreads() ? 1 : 0);
915 for (Standard_Integer anIter = 1; anIter < theArgNb; ++anIter)
917 TCollection_AsciiString anArg (theArgVec[anIter]);
919 if (anIter + 1 < theArgNb
920 && (anArg == "-nbthreads"
921 || anArg == "-threads"))
923 const Standard_Integer aVal = Draw::Atoi (theArgVec[++anIter]);
924 aDefPool->Init (aVal);
926 else if (anIter + 1 < theArgNb
927 && (anArg == "-nbdefthreads"
928 || anArg == "-defthreads"
929 || anArg == "-nbmaxdefthreads"
930 || anArg == "-maxdefthreads"))
932 const Standard_Integer aVal = Draw::Atoi (theArgVec[++anIter]);
933 if (aVal <= 0 || aVal > aDefPool->NbThreads())
935 Message::SendFail() << "Syntax error: maximum number of threads to use should be <= of threads in the pool";
938 aDefPool->SetNbDefaultThreadsToLaunch (aVal);
940 else if (anIter + 1 < theArgNb
941 && (anArg == "-useocct"
942 || anArg == "-touseocct"
943 || anArg == "-occt"))
945 const Standard_Integer aVal = Draw::Atoi (theArgVec[++anIter]);
946 OSD_Parallel::SetUseOcctThreads (aVal == 1);
947 if (OSD_Parallel::ToUseOcctThreads() != (aVal == 1))
949 std::cout << "Warning: unable to switch threads library - no options available\n";
952 else if (anIter + 1 < theArgNb
953 && (anArg == "-usetbb"
954 || anArg == "-tousetbb"
957 const Standard_Integer aVal = Draw::Atoi (theArgVec[++anIter]);
958 OSD_Parallel::SetUseOcctThreads (aVal == 0);
959 if (OSD_Parallel::ToUseOcctThreads() != (aVal == 0))
961 std::cout << "Warning: unable to switch threads library - no options available\n";
966 Message::SendFail() << "Syntax error: unknown argument '" << anArg << "'";
973 //==============================================================================
976 //==============================================================================
978 static int dperf (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
980 // reset if argument is provided and it is not '0'
981 int reset = (theArgNb > 1 ? theArgVec[1][0] != '0' && theArgVec[1][0] != '\0' : 0);
983 perf_sprint_all_meters (buffer, 25600 - 1, reset);
988 //==============================================================================
989 //function : dsetsignal
991 //==============================================================================
993 static int dsetsignal (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
995 OSD_SignalMode aMode = OSD_SignalMode_Set;
996 Standard_Boolean aSetFPE = OSD::ToCatchFloatingSignals();
997 Standard_Integer aStackLen = OSD::SignalStackTraceLength();
999 // default for FPE signal is defined by CSF_FPE variable, if set
1000 OSD_Environment aEnv("CSF_FPE");
1001 TCollection_AsciiString aEnvStr = aEnv.Value();
1002 if (!aEnvStr.IsEmpty())
1004 aSetFPE = (aEnvStr.Value(1) != '0');
1008 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
1010 TCollection_AsciiString anArg(theArgVec[anArgIter]);
1012 if (anArg == "asis")
1014 aMode = OSD_SignalMode_AsIs;
1016 else if (anArg == "set")
1018 aMode = OSD_SignalMode_Set;
1020 else if (anArg == "unhandled")
1022 aMode = OSD_SignalMode_SetUnhandled;
1024 else if (anArg == "unset")
1026 aMode = OSD_SignalMode_Unset;
1028 else if (anArg == "1" || anArg == "on")
1030 aSetFPE = Standard_True;
1032 else if (anArg == "0" || anArg == "off")
1034 aSetFPE = Standard_False;
1036 else if (anArg == "default")
1039 else if (anArgIter + 1 < theArgNb
1040 && (anArg == "-stracktracelength"
1041 || anArg == "-stracktracelen"
1042 || anArg == "-stracklength"
1043 || anArg == "-stracklen"))
1045 aStackLen = Draw::Atoi (theArgVec[++anArgIter]);
1049 Message::SendFail() << "Syntax error: unknown argument '" << anArg << "'";
1054 OSD::SetSignal(aMode, aSetFPE);
1055 OSD::SetSignalStackTraceLength (aStackLen);
1057 // report actual status in the end
1058 const char* aModeStr = 0;
1059 switch (OSD::SignalMode())
1062 case OSD_SignalMode_AsIs: aModeStr = "asis"; break;
1063 case OSD_SignalMode_Set: aModeStr = "set"; break;
1064 case OSD_SignalMode_SetUnhandled: aModeStr = "unhandled"; break;
1065 case OSD_SignalMode_Unset: aModeStr = "unset"; break;
1067 theDI << "Signal mode: " << aModeStr << "\n"
1068 << "Catch FPE: " << (OSD::ToCatchFloatingSignals() ? "1" : "0") << "\n"
1069 << "Stack Trace Length: " << aStackLen << "\n";
1073 //==============================================================================
1074 //function : dtracelevel
1076 //==============================================================================
1078 static int dtracelevel (Draw_Interpretor& theDI,
1079 Standard_Integer theArgNb,
1080 const char** theArgVec)
1082 Message_Gravity aLevel = Message_Info;
1083 if (theArgNb < 1 || theArgNb > 2)
1085 Message::SendFail() << "Error: wrong number of arguments! See usage:";
1086 theDI.PrintHelp (theArgVec[0]);
1089 else if (theArgNb == 2)
1091 TCollection_AsciiString aVal (theArgVec[1]);
1093 if (aVal == "trace")
1095 aLevel = Message_Trace;
1097 else if (aVal == "info")
1099 aLevel = Message_Info;
1101 else if (aVal == "warn"
1102 || aVal == "warning")
1104 aLevel = Message_Warning;
1106 else if (aVal == "alarm")
1108 aLevel = Message_Alarm;
1110 else if (aVal == "fail")
1112 aLevel = Message_Fail;
1116 Message::SendFail() << "Error: unknown gravity '" << theArgVec[1] << "'";
1121 Handle(Message_Messenger) aMessenger = Message::DefaultMessenger();
1122 if (aMessenger.IsNull())
1124 Message::SendFail() << "Error: default messenger is unavailable";
1128 Message_SequenceOfPrinters& aPrinters = aMessenger->ChangePrinters();
1129 if (aPrinters.Length() < 1)
1131 Message::SendFail() << "Error: no printers registered in default Messenger";
1135 for (Standard_Integer aPrinterIter = 1; aPrinterIter <= aPrinters.Length(); ++aPrinterIter)
1137 Handle(Message_Printer)& aPrinter = aPrinters.ChangeValue (aPrinterIter);
1140 if (aPrinterIter == 1)
1142 aLevel = aPrinter->GetTraceLevel();
1144 else if (aLevel == aPrinter->GetTraceLevel())
1149 switch (aPrinter->GetTraceLevel())
1151 case Message_Trace: theDI << "trace"; break;
1152 case Message_Info: theDI << "info"; break;
1153 case Message_Warning: theDI << "warn"; break;
1154 case Message_Alarm: theDI << "alarm"; break;
1155 case Message_Fail: theDI << "fail"; break;
1160 aPrinter->SetTraceLevel (aLevel);
1166 //==============================================================================
1167 //function : ddebugtraces
1169 //==============================================================================
1170 static int ddebugtraces (Draw_Interpretor& theDI, Standard_Integer theArgNb, const char** theArgVec)
1174 theDI << Standard_Failure::DefaultStackTraceLength();
1177 else if (theArgNb != 2)
1179 theDI << "Syntax error: wrong number of arguments";
1183 Standard_Failure::SetDefaultStackTraceLength (Draw::Atoi (theArgVec[1]));
1187 //==============================================================================
1190 //==============================================================================
1191 static int dputs (Draw_Interpretor& theDI,
1192 Standard_Integer theArgNb,
1193 const char** theArgVec)
1195 Standard_OStream* aStream = &std::cout;
1196 bool isNoNewline = false, toIntense = false;
1197 Message_ConsoleColor aColor = Message_ConsoleColor_Default;
1198 for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
1200 TCollection_AsciiString anArg (theArgVec[anArgIter]);
1202 if (anArg == "-nonewline")
1206 else if (anArg == "stdcout")
1208 aStream = &std::cout;
1210 else if (anArg == "stdcerr")
1212 aStream = &std::cerr;
1214 else if (anArg == "-intense")
1218 else if (anArg == "-black")
1220 aColor = Message_ConsoleColor_Black;
1222 else if (anArg == "-white")
1224 aColor = Message_ConsoleColor_White;
1226 else if (anArg == "-red")
1228 aColor = Message_ConsoleColor_Red;
1230 else if (anArg == "-blue")
1232 aColor = Message_ConsoleColor_Blue;
1234 else if (anArg == "-green")
1236 aColor = Message_ConsoleColor_Green;
1238 else if (anArg == "-yellow")
1240 aColor = Message_ConsoleColor_Yellow;
1242 else if (anArg == "-cyan")
1244 aColor = Message_ConsoleColor_Cyan;
1246 else if (anArg == "-magenta")
1248 aColor = Message_ConsoleColor_Magenta;
1250 else if (anArgIter + 1 == theArgNb)
1252 if (!theDI.ToColorize())
1255 aColor = Message_ConsoleColor_Default;
1257 if (toIntense || aColor != Message_ConsoleColor_Default)
1259 Message_PrinterOStream::SetConsoleTextColor (aStream, aColor, toIntense);
1262 *aStream << theArgVec[anArgIter];
1265 *aStream << std::endl;
1268 if (toIntense || aColor != Message_ConsoleColor_Default)
1270 Message_PrinterOStream::SetConsoleTextColor (aStream, Message_ConsoleColor_Default, false);
1276 Message::SendFail() << "Syntax error at '" << anArg << "'";
1281 Message::SendFail() << "Syntax error: wrong number of arguments";
1285 void Draw::BasicCommands(Draw_Interpretor& theCommands)
1287 static Standard_Boolean Done = Standard_False;
1289 Done = Standard_True;
1291 std::ios::sync_with_stdio();
1293 const char* g = "DRAW General Commands";
1295 theCommands.Add("batch", "returns 1 in batch mode",
1296 __FILE__,ifbatch,g);
1297 theCommands.Add("spy","spy [file], Save commands in file. no file close",
1299 theCommands.Add("wait","wait [time(10)], wait time seconds",
1300 __FILE__,Draw_wait,g);
1301 theCommands.Add("cpulimit","cpulimit [nbseconds], no args remove limits",
1302 __FILE__,cpulimit,g);
1303 theCommands.Add("chrono","chrono [name action [action...]] \n Operates named timer.\n"
1304 " Supported actions: reset, start, stop, restart, show, counter [text].\n"
1305 " Without arguments enables / disables global timer for all DRAW commands.",
1306 __FILE__,chronom,g);
1307 theCommands.Add("dchrono","see help of chrono command",
1308 __FILE__,dchronom,g);
1309 theCommands.Add("mallochook",
1310 "debug memory allocation/deallocation, w/o args for help",
1311 __FILE__, mallochook, g);
1312 theCommands.Add ("meminfo",
1313 "meminfo [virt|v] [heap|h] [wset|w] [wsetpeak] [swap] [swappeak] [private]"
1314 " : memory counters for this process",
1315 __FILE__, dmeminfo, g);
1316 theCommands.Add("dperf","dperf [reset] -- show performance counters, reset if argument is provided",
1318 theCommands.Add("dsetsignal",
1319 "dsetsignal [{asIs|set|unhandled|unset}=set] [{0|1|default=$CSF_FPE}]"
1320 "\n\t\t: [-strackTraceLength Length]"
1321 "\n\t\t: Sets OSD signal handler, with FPE option if argument is given."
1322 "\n\t\t: -strackTraceLength specifies length of stack trace to put into exceptions redirected from signals.",
1323 __FILE__,dsetsignal,g);
1325 theCommands.Add("dparallel",
1326 "dparallel [-occt {0|1}] [-nbThreads Count] [-nbDefThreads Count]"
1327 "\n\t\t: Manages global parallelization parameters:"
1328 "\n\t\t: -occt use OCCT implementation or external library (if available)"
1329 "\n\t\t: -nbThreads specify the number of threads in default thread pool"
1330 "\n\t\t: -nbDefThreads specify the upper limit of threads to be used for default thread pool"
1331 "\n\t\t: within single parallelization call (should be <= of overall number of threads),"
1332 "\n\t\t: so that nested algorithm can also use this pool",
1333 __FILE__,dparallel,g);
1335 // Logging commands; note that their names are hard-coded in the code
1336 // of Draw_Interpretor, thus should not be changed without update of that code!
1337 theCommands.Add("dlog", "manage logging of commands and output; run without args to get help",
1339 theCommands.Add("decho", "switch on / off echo of commands to cout; run without args to get help",
1341 theCommands.Add("dtracelevel", "dtracelevel [trace|info|warn|alarm|fail]",
1342 __FILE__, dtracelevel, g);
1343 theCommands.Add("ddebugtraces",
1344 "ddebugtraces nbTraces"
1345 "\n\t\t: Sets the number of lines for the stack trace within Standard_Failure constructor."
1346 "\n\t\t: Intended for debug purposes.",
1347 __FILE__, ddebugtraces, g);
1349 theCommands.Add("dbreak", "raises Tcl exception if user has pressed Control-Break key",
1351 theCommands.Add("dversion", "provides information on OCCT build configuration (version, compiler, OS, C library, etc.)",
1352 __FILE__,dversion,g);
1353 theCommands.Add("dlocale", "set and / or query locate of C subsystem (function setlocale())",
1354 __FILE__,dlocale,g);
1356 theCommands.Add("dputs",
1357 "dputs [-intense] [-black|-white|-red|-green|-blue|-yellow|-cyan|-magenta]"
1358 "\n\t\t: [-nonewline] [stdcout|stdcerr] text"
1359 "\n\t\t: Puts text into console output",