42cf5bc1 |
1 | // Created on: 2018-03-15 |
2 | // Created by: Stephan GARNAUD (ARM) |
3 | // Copyright (c) 1998-1999 Matra Datavision |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
5 | // |
6 | // This file is part of Open CASCADE Technology software library. |
7 | // |
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. |
13 | // |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
16 | |
17 | #ifndef _OSD_Chronometer_HeaderFile |
18 | #define _OSD_Chronometer_HeaderFile |
19 | |
20 | #include <Standard.hxx> |
21 | #include <Standard_DefineAlloc.hxx> |
22 | #include <Standard_Handle.hxx> |
42cf5bc1 |
23 | #include <Standard_Real.hxx> |
24 | #include <Standard_OStream.hxx> |
25 | |
42cf5bc1 |
26 | //! This class measures CPU time (both user and system) consumed |
27 | //! by current process or thread. The chronometer can be started |
28 | //! and stopped multiple times, and measures cumulative time. |
29 | //! |
30 | //! If only the thread is measured, calls to Stop() and Show() |
31 | //! must occur from the same thread where Start() was called |
32 | //! (unless chronometer is stopped); otherwise measurement will |
33 | //! yield false values. |
34 | class OSD_Chronometer |
35 | { |
36 | public: |
37 | |
38 | DEFINE_STANDARD_ALLOC |
39 | |
42cf5bc1 |
40 | //! Initializes a stopped Chronometer. |
41 | //! |
42 | //! If ThisThreadOnly is True, measured CPU time will account |
43 | //! time of the current thread only; otherwise CPU of the |
44 | //! process (all threads, and completed children) is measured. |
2da51263 |
45 | Standard_EXPORT OSD_Chronometer (Standard_Boolean theThisThreadOnly = Standard_False); |
46 | |
47 | //! Destructor. |
42cf5bc1 |
48 | Standard_EXPORT virtual ~OSD_Chronometer(); |
1beb58d7 |
49 | |
50 | //! Return true if timer has been started. |
2da51263 |
51 | Standard_Boolean IsStarted() const { return !myIsStopped; } |
1beb58d7 |
52 | |
42cf5bc1 |
53 | //! Stops and Reinitializes the Chronometer. |
54 | Standard_EXPORT virtual void Reset(); |
44fae8b1 |
55 | |
56 | //! Restarts the Chronometer. |
57 | Standard_EXPORT virtual void Restart(); |
58 | |
42cf5bc1 |
59 | //! Stops the Chronometer. |
60 | Standard_EXPORT virtual void Stop(); |
61 | |
62 | //! Starts (after Create or Reset) or restarts (after Stop) |
63 | //! the chronometer. |
64 | Standard_EXPORT virtual void Start(); |
65 | |
66 | //! Shows the current CPU user and system time on the |
67 | //! standard output stream <cout>. |
68 | //! The chronometer can be running (laps Time) or stopped. |
69 | Standard_EXPORT virtual void Show() const; |
70 | |
71 | //! Shows the current CPU user and system time on the output |
72 | //! stream <os>. |
73 | //! The chronometer can be running (laps Time) or stopped. |
2da51263 |
74 | Standard_EXPORT virtual void Show (Standard_OStream& theOStream) const; |
75 | |
76 | //! Returns the current CPU user time in seconds. |
77 | //! The chronometer can be running (laps Time) or stopped. |
78 | Standard_Real UserTimeCPU() const |
79 | { |
80 | Standard_Real aUserTime = 0.0, aSysTime = 0.0; |
81 | Show (aUserTime, aSysTime); |
82 | return aUserTime; |
83 | } |
84 | |
85 | //! Returns the current CPU system time in seconds. |
86 | //! The chronometer can be running (laps Time) or stopped. |
87 | Standard_Real SystemTimeCPU() const |
88 | { |
89 | Standard_Real aUserTime = 0.0, aSysTime = 0.0; |
90 | Show (aUserTime, aSysTime); |
91 | return aSysTime; |
92 | } |
93 | |
42cf5bc1 |
94 | //! Returns the current CPU user time in a variable. |
95 | //! The chronometer can be running (laps Time) or stopped. |
2da51263 |
96 | void Show (Standard_Real& theUserSeconds) const { theUserSeconds = UserTimeCPU(); } |
42cf5bc1 |
97 | |
98 | //! Returns the current CPU user and system time in variables. |
99 | //! The chronometer can be running (laps Time) or stopped. |
2da51263 |
100 | Standard_EXPORT void Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const; |
101 | |
102 | public: |
103 | |
42cf5bc1 |
104 | //! Returns CPU time (user and system) consumed by the current |
105 | //! process since its start, in seconds. The actual precision of |
106 | //! the measurement depends on granularity provided by the system, |
107 | //! and is platform-specific. |
108 | Standard_EXPORT static void GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds); |
109 | |
110 | //! Returns CPU time (user and system) consumed by the current |
111 | //! thread since its start. Note that this measurement is |
112 | //! platform-specific, as threads are implemented and managed |
113 | //! differently on different platforms and CPUs. |
114 | Standard_EXPORT static void GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds); |
115 | |
42cf5bc1 |
116 | protected: |
117 | |
2da51263 |
118 | Standard_Real myStartCpuUser; |
119 | Standard_Real myStartCpuSys; |
120 | Standard_Real myCumulCpuUser; |
121 | Standard_Real myCumulCpuSys; |
122 | Standard_Boolean myIsStopped; |
123 | Standard_Boolean myIsThreadOnly; |
42cf5bc1 |
124 | |
125 | }; |
126 | |
42cf5bc1 |
127 | #endif // _OSD_Chronometer_HeaderFile |