0032969: Coding - get rid of unused headers [IMeshData to PLib]
[occt.git] / src / Message / Message_AttributeMeter.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <Message_AttributeMeter.hxx>
15
16 #include <Message_Report.hxx>
17 #include <OSD_Timer.hxx>
18
19 #include <Precision.hxx>
20 #include <Standard_Dump.hxx>
21
22 IMPLEMENT_STANDARD_RTTIEXT(Message_AttributeMeter, Message_Attribute)
23
24 //=======================================================================
25 //function : Constructor
26 //purpose  :
27 //=======================================================================
28 Message_AttributeMeter::Message_AttributeMeter (const TCollection_AsciiString& theName)
29 : Message_Attribute(theName)
30 {
31 }
32
33 //=======================================================================
34 //function : HasMetric
35 //purpose  : 
36 //=======================================================================
37 Standard_Boolean Message_AttributeMeter::HasMetric (const Message_MetricType& theMetric) const
38 {
39   return myMetrics.Contains (theMetric);
40 }
41
42 //=======================================================================
43 //function : IsMetricValid
44 //purpose  : 
45 //=======================================================================
46 Standard_Boolean Message_AttributeMeter::IsMetricValid (const Message_MetricType& theMetric) const
47 {
48   return Abs (StartValue(theMetric) - UndefinedMetricValue()) > Precision::Confusion() &&
49          Abs (StopValue (theMetric) - UndefinedMetricValue()) > Precision::Confusion();
50 }
51
52 //=======================================================================
53 //function : StartValue
54 //purpose  :
55 //=======================================================================
56 Standard_Real Message_AttributeMeter::StartValue (const Message_MetricType& theMetric) const
57 {
58   if (!HasMetric (theMetric))
59   {
60     return UndefinedMetricValue();
61   }
62
63   return myMetrics.Seek (theMetric)->first;
64 }
65
66 //=======================================================================
67 //function : SetStartValue
68 //purpose  :
69 //=======================================================================
70 void Message_AttributeMeter::SetStartValue (const Message_MetricType& theMetric, const Standard_Real theValue)
71 {
72   if (StartToStopValue* aValPtr = myMetrics.ChangeSeek (theMetric))
73   {
74     aValPtr->first = theValue;
75   }
76   else
77   {
78     myMetrics.Add (theMetric, std::make_pair (theValue, UndefinedMetricValue()));
79   }
80 }
81
82 //=======================================================================
83 //function : StopValue
84 //purpose  :
85 //=======================================================================
86 Standard_Real Message_AttributeMeter::StopValue (const Message_MetricType& theMetric) const
87 {
88   if (!HasMetric (theMetric))
89   {
90     return UndefinedMetricValue();
91   }
92   return myMetrics.Seek (theMetric)->second;
93 }
94
95 //=======================================================================
96 //function : SetStopValue
97 //purpose  :
98 //=======================================================================
99 void Message_AttributeMeter::SetStopValue (const Message_MetricType& theMetric, const Standard_Real theValue)
100 {
101   if (StartToStopValue* aValPtr = myMetrics.ChangeSeek (theMetric))
102   {
103     aValPtr->second = theValue;
104   }
105 }
106
107 //=======================================================================
108 //function : SetAlertMetrics
109 //purpose  :
110 //=======================================================================
111 void Message_AttributeMeter::SetAlertMetrics (const Handle(Message_AlertExtended)& theAlert,
112                                               const Standard_Boolean theStartValue)
113 {
114   if (theAlert.IsNull())
115   {
116     return;
117   }
118
119   Handle(Message_AttributeMeter) aMeterAttribute = Handle(Message_AttributeMeter)::DownCast (theAlert->Attribute());
120   if (aMeterAttribute.IsNull())
121   {
122     return;
123   }
124
125   Handle(Message_Report) aReport = Message::DefaultReport (Standard_True);
126   const NCollection_IndexedMap<Message_MetricType>& anActiveMetrics = aReport->ActiveMetrics();
127
128   // time metrics
129   if (anActiveMetrics.Contains (Message_MetricType_WallClock))
130   {
131     OSD_Timer aTimer;
132     aTimer.Start();
133     Standard_Real aTime = OSD_Timer::GetWallClockTime();
134     if (theStartValue)
135       aMeterAttribute->SetStartValue (Message_MetricType_WallClock, aTime);
136     else
137       aMeterAttribute->SetStopValue (Message_MetricType_WallClock, aTime);
138   }
139   if (anActiveMetrics.Contains (Message_MetricType_ProcessCPUUserTime) ||
140       anActiveMetrics.Contains (Message_MetricType_ProcessCPUSystemTime) ||
141       anActiveMetrics.Contains (Message_MetricType_ThreadCPUUserTime) ||
142       anActiveMetrics.Contains (Message_MetricType_ThreadCPUSystemTime))
143   {
144     if (anActiveMetrics.Contains (Message_MetricType_ProcessCPUUserTime) ||
145         anActiveMetrics.Contains (Message_MetricType_ProcessCPUSystemTime))
146     {
147       Standard_Real aProcessUserTime, aProcessSystemTime;
148       OSD_Chronometer::GetProcessCPU (aProcessUserTime, aProcessSystemTime);
149       if (anActiveMetrics.Contains (Message_MetricType_ProcessCPUUserTime))
150       {
151         if (theStartValue)
152         {
153           aMeterAttribute->SetStartValue (Message_MetricType_ProcessCPUUserTime, aProcessUserTime);
154         }
155         else
156         {
157           aMeterAttribute->SetStopValue (Message_MetricType_ProcessCPUUserTime, aProcessUserTime);
158         }
159       }
160       if (anActiveMetrics.Contains (Message_MetricType_ProcessCPUSystemTime))
161       {
162         if (theStartValue)
163         {
164           aMeterAttribute->SetStartValue (Message_MetricType_ProcessCPUSystemTime, aProcessSystemTime);
165         }
166         else
167         {
168           aMeterAttribute->SetStopValue (Message_MetricType_ProcessCPUSystemTime, aProcessSystemTime);
169         }
170       }
171     }
172     if (anActiveMetrics.Contains (Message_MetricType_ThreadCPUUserTime) ||
173         anActiveMetrics.Contains (Message_MetricType_ThreadCPUSystemTime))
174     {
175       Standard_Real aThreadUserTime, aThreadSystemTime;
176       OSD_Chronometer::GetThreadCPU (aThreadUserTime, aThreadSystemTime);
177       if (anActiveMetrics.Contains (Message_MetricType_ThreadCPUUserTime))
178       {
179         if (theStartValue)
180         {
181           aMeterAttribute->SetStartValue (Message_MetricType_ThreadCPUUserTime, aThreadUserTime);
182         }
183         else
184         {
185           aMeterAttribute->SetStopValue (Message_MetricType_ThreadCPUUserTime, aThreadUserTime);
186         }
187       }
188       if (anActiveMetrics.Contains (Message_MetricType_ThreadCPUSystemTime))
189       {
190         if (theStartValue)
191         {
192           aMeterAttribute->SetStartValue (Message_MetricType_ThreadCPUSystemTime, aThreadSystemTime);
193         }
194         else
195         {
196           aMeterAttribute->SetStopValue (Message_MetricType_ThreadCPUSystemTime, aThreadSystemTime);
197         }
198       }
199     }
200   }
201
202   // memory metrics
203   OSD_MemInfo aMemInfo (Standard_False);
204   aMemInfo.SetActive (Standard_False);
205   NCollection_IndexedMap<OSD_MemInfo::Counter> aCounters;
206   for (NCollection_IndexedMap<Message_MetricType>::Iterator anIterator (anActiveMetrics); anIterator.More(); anIterator.Next())
207   {
208     OSD_MemInfo::Counter anInfoCounter;
209     if (!Message::ToOSDMetric (anIterator.Value(), anInfoCounter))
210     {
211       continue;
212     }
213
214     aCounters.Add (anInfoCounter);
215     aMemInfo.SetActive (anInfoCounter, Standard_True);
216   }
217   if (aCounters.IsEmpty())
218   {
219     return;
220   }
221
222   aMemInfo.Update();
223   Message_MetricType aMetricType;
224   for (NCollection_IndexedMap<OSD_MemInfo::Counter>::Iterator anIterator (aCounters); anIterator.More(); anIterator.Next())
225   {
226     if (!Message::ToMessageMetric (anIterator.Value(), aMetricType))
227     {
228       continue;
229     }
230
231     if (theStartValue)
232     {
233       aMeterAttribute->SetStartValue (aMetricType, (Standard_Real)aMemInfo.ValuePreciseMiB (anIterator.Value()));
234     }
235     else
236     {
237       aMeterAttribute->SetStopValue (aMetricType, (Standard_Real)aMemInfo.ValuePreciseMiB (anIterator.Value()));
238     }
239   }
240 }
241
242 //=======================================================================
243 //function : DumpJson
244 //purpose  :
245 //=======================================================================
246 void Message_AttributeMeter::DumpJson (Standard_OStream& theOStream,
247                                        Standard_Integer theDepth) const
248 {
249   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
250   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Message_Attribute)
251
252   for (NCollection_IndexedDataMap<Message_MetricType, StartToStopValue>::Iterator anIterator (myMetrics);
253        anIterator.More(); anIterator.Next())
254   {
255     OCCT_DUMP_VECTOR_CLASS (theOStream, Message::MetricToString (anIterator.Key()),
256                             2, anIterator.Value().first, anIterator.Value().second)
257   }
258 }