1 /*
2  Copyright (c) 1999-2014 OPEN CASCADE SAS
3 
4  This file is part of Open CASCADE Technology software library.
5 
6  This library is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License version 2.1 as published
8  by the Free Software Foundation, with special exception defined in the file
9  OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10  distribution for complete text of the license and disclaimer of any warranty.
11 
12  Alternatively, this file may be used under the terms of Open CASCADE
13  commercial license or contractual agreement.
14 */
15 
16 #ifndef _OSD_PERFMETER_H
17 #define _OSD_PERFMETER_H
18 
19 /**
20  * Macros for convenient and fast usage of meters.
21  * Define PERF_ENABLE_METERS to make them available.
22  */
23 #ifdef PERF_ENABLE_METERS
24 
25 /**
26  * @def PERF_START_METER(theMeterName)
27  * Forces meter MeterName to begin to count by remembering the current data of timer.
28  * Creates new meter if there is no such meter.
29  */
30 #define PERF_START_METER(_m_name) { \
31   static int __iMeter = -1; \
32   if  (__iMeter >= 0)  perf_start_imeter (__iMeter); \
33   else      __iMeter = perf_start_meter (_m_name);   \
34 }
35 
36 /**
37  * @def PERF_STOP_METER(theMeterName)
38  * Forces meter MeterName to stop and cumulate the time elapsed since the start.
39  */
40 #define PERF_STOP_METER(_m_name) { \
41   static int __iMeter = -1; \
42   if  (__iMeter >= 0)  perf_stop_imeter (__iMeter); \
43   else      __iMeter = perf_stop_meter (_m_name); \
44 }
45 
46 /**
47  * @def PERF_TICK_METER(theMeterName)
48  * Increments the counter of meter MeterName without changing its state with respect to measurement of time.
49  * Creates new meter if there is no such meter.
50  * It is useful to count the number of enters to a part of code without wasting a time to measure CPU time.
51  */
52 #define PERF_TICK_METER(_m_name) { \
53   static int __iMeter = -1; \
54   if  (__iMeter >= 0)  perf_tick_imeter (__iMeter); \
55   else      __iMeter = perf_tick_meter (_m_name); \
56 }
57 
58 /**
59  * @def PERF_CLOSE_METER(theMeterName)
60  * Prints out and resets the given meter.
61  */
62 #define PERF_CLOSE_METER(_m_name) perf_close_meter (_m_name);
63 
64 /**
65  * @def PERF_PRINT_ALL
66  * Prints all existing meters which have been entered at least once and resets them.
67  */
68 #define PERF_PRINT_ALL() { \
69   perf_print_all_meters(1); \
70 }
71 
72 #else
73   #define PERF_TICK_METER(_m_name)
74   #define PERF_START_METER(_m_name)
75   #define PERF_STOP_METER(_m_name)
76   #define PERF_CLOSE_METER(_m_name)
77   #define PERF_PRINT_ALL()
78 #endif
79 
80 /**
81  * Creates new counter (if it is absent) identified by theMeterName and resets its cumulative value
82  * @return meter global identifier if OK, -1 if alloc problem
83  */
84 Standard_EXPORTEXTERNC int perf_init_meter (const char* const theMeterName);
85 
86 /**
87  * Forces meter theMeterName to begin to count by remembering the current data of timer.
88  * Creates new meter if there is no such meter.
89  * @return meter global identifier if OK, -1 if no such meter and cannot create a new one
90  */
91 Standard_EXPORTEXTERNC int perf_start_meter (const char* const theMeterName);
92 
93 /**
94  * Forces meter with number theMeterId to begin count by remembering the current data of timer.
95  * @return meter global identifier if OK, -1 if no such meter
96  */
97 Standard_EXPORTEXTERNC int perf_start_imeter (const int theMeterId);
98 
99 /**
100  * Forces meter theMeterName to stop and cumulate the time elapsed since the start.
101  * @return meter global identifier if OK, -1 if no such meter or it is has not been started
102  */
103 Standard_EXPORTEXTERNC int perf_stop_meter (const char* const theMeterName);
104 
105 /**
106  * Forces meter with number theMeterId to stop and cumulate the time elapsed since the start.
107  * @return meter global identifier if OK, -1 if no such meter or it is has not been started
108  */
109 Standard_EXPORTEXTERNC int perf_stop_imeter (const int theMeterId);
110 
111 /**
112  * Increments the counter of meter theMeterName without changing its state with respect to measurement of time.
113  * Creates new meter if there is no such meter.
114  * @return meter global identifier if OK, -1 if no such meter and cannot create a new one
115  */
116 Standard_EXPORTEXTERNC int perf_tick_meter (const char* const theMeterName);
117 
118 /**
119  * Increments the counter of meter theMeterId without changing its state with respect to measurement of time.
120  * @return meter global identifier if OK, -1 if no such meter
121  */
122 Standard_EXPORTEXTERNC int perf_tick_imeter (const int theMeterId);
123 
124 /**
125  * Tells the time cumulated by meter theMeterName and the number of enters to this meter.
126  * @param theNbEnter [OUT] number of enters if the pointer != NULL
127  * @param theSeconds [OUT] seconds if the pointer != NULL
128  * @return meter global identifier if OK, -1 if no such meter
129 */
130 Standard_EXPORTEXTERNC int perf_get_meter (const char* const theMeterName,
131                                            int*              theNbEnter,
132                                            double*           theSeconds);
133 
134 /**
135  * Prints on stdout the cumulated time and the number of enters for the specified meter.
136  */
137 Standard_EXPORTEXTERNC void perf_close_meter (const char* const theMeterName);
138 
139 /**
140  * Prints on stdout the cumulated time and the number of enters for the specified meter.
141  */
142 Standard_EXPORTEXTERNC void perf_close_imeter (const int theMeterId);
143 
144 /**
145  * Prints on stdout the cumulated time and the number of enters for each alive meter which have the number of enters > 0.
146  * Resets all meters if reset is non-null.
147  */
148 Standard_EXPORTEXTERNC void perf_print_all_meters (int reset);
149 
150 /**
151  * Prints to supplied string buffer the cumulated time and the number of enters
152  * for each alive meter with the number of enters > 0.
153  * If buffer length is not sufficient, data of some meters may be lost.
154  * It is recommended to reserve 256 bytes per meter, 25600 bytes should fit all.
155  * Resets all meters.
156  */
157 Standard_EXPORTEXTERNC void perf_sprint_all_meters (char *buffer, int length, int reset);
158 
159 /**
160  * Deletes all meters and frees memory.
161  */
162 Standard_EXPORTEXTERNC void perf_destroy_all_meters (void);
163 
164 /**
165  * ATTENTION!!!
166  * This func calls perf_print_all_meters() and perf_destroy_all_meters()
167  * and is called automatically at the end of a program via system call atexit().
168  */
169 Standard_EXPORTEXTERNC void perf_print_and_destroy (void);
170 
171 #endif
172