Debian Codegraph @

cmake/tribits/core/utils/TimingUtils.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # TriBITS: Tribal Build, Integrate, and Test System
4 #
5 # Copyright 2013-2016 NTESS and the TriBITS contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10 #
11 # Timing utilities
12 #
13 # Warning: Depends on calling 'date' program so will not be portable to all
14 # platforms so call with care.
15 #
16
17 include("${CMAKE_CURRENT_LIST_DIR}/Split.cmake")
18
19
20 # @FUNCTION: timer_get_raw_seconds()
21 #
22 # Return the raw time in seconds (nano-second accuracy) since epoch, i.e.,
23 # since 1970-01-01 00:00:00 UTC.
24 #
25 # Usage::
26 #
27 # timer_get_raw_seconds(<rawSecondsVar>)
28 #
29 # This function is used along with `timer_get_rel_seconds()`_, and
30 # `timer_print_rel_time()`_ to time big chunks of CMake code for timing and
31 # profiling purposes. See `timer_print_rel_time()`_ for more details and an
32 # example.
33 #
34 # NOTE: This function runs an external process with ``execute_process()`` to
35 # run the ``date`` command. Therefore, it only works on Unix/Linux and other
36 # systems that have a standard ``date`` command. Since this uses
37 # ``execute_process()``, this function should only be used to time very
38 # course-grained operations (i.e. that take longer than a second). If the
39 # `date` command does not exist, then ${<rawSecondsVar>} will be empty on
40 # output!
41 #
42 function(timer_get_raw_seconds SECONDS_DEC_OUT)
43 execute_process(COMMAND date "+%s.%N" OUTPUT_STRIP_TRAILING_WHITESPACE
44 OUTPUT_VARIABLE SECONDS_DEC)
45 set(${SECONDS_DEC_OUT} ${SECONDS_DEC} PARENT_SCOPE)
46 endfunction()
47
48
49 # @FUNCTION: timer_get_rel_seconds()
50 #
51 # Return the relative time between start and stop seconds.
52 #
53 # Usage::
54 #
55 # timer_get_rel_seconds(<startSeconds> <endSeconds> <relSecondsOutVar>)
56 #
57 # This simple function computes the relative number of seconds between
58 # ``<startSeconds>`` and ``<endSeconds>`` (returned from
59 # `timer_get_raw_seconds()`_) and sets the result in the local variable
60 # ``<relSecondsOutVar>``.
61 #
62 function(timer_get_rel_seconds ABS_SECONDS_DEC_START
63 ABS_SECONDS_DEC_END SECONDS_REL_OUT
64 )
65 set(DECIMAL_PLACES 3)
66 timer_get_truncated_combined_int_from_decimal_num(
67 ${ABS_SECONDS_DEC_START} ${DECIMAL_PLACES} SECONDS_START_INT)
68 #print_var(SECONDS_START_INT)
69 timer_get_truncated_combined_int_from_decimal_num(
70 ${ABS_SECONDS_DEC_END} ${DECIMAL_PLACES} SECONDS_END_INT)
71 #print_var(SECONDS_END_INT)
72
73 math(EXPR SECONDS_REL_INT "${SECONDS_END_INT} - ${SECONDS_START_INT}")
74
75 # ToDo: Check that SECONDS_REL_INT is positive and if not, then raise an
76 # error and exit!
77
78 timer_get_decimal_num_frum_truncated_combined_int(
79 ${SECONDS_REL_INT} ${DECIMAL_PLACES} SECONDS_REL )
80
81 set(${SECONDS_REL_OUT} "${SECONDS_REL}" PARENT_SCOPE)
82
83 endfunction()
84
85
86 # @FUNCTION: timer_print_rel_time()
87 #
88 # Print the relative time between start and stop timers in ``<min>m<sec>s``
89 # format.
90 #
91 # Usage::
92 #
93 # timer_print_rel_time(<startSeconds> <endSeconds> "<messageStr>")
94 #
95 # Differences the raw times ``<startSeconds>`` and ``<endSeconds>``
96 # (i.e. gotten from `timer_get_raw_seconds()`_) and prints the time in
97 # ``<min>m<sec>s`` format.
98 #
99 # This is meant to be used with `timer_get_raw_seconds()`_ to time expensive
100 # blocks of CMake code like::
101 #
102 # timer_get_raw_seconds(REAL_EXPENSIVE_START)
103 #
104 # real_expensive(...)
105 #
106 # timer_get_raw_seconds(REAL_EXPENSIVE_END)
107 #
108 # timer_print_rel_time(${REAL_EXPENSIVE_START} ${REAL_EXPENSIVE_END}
109 # "real_expensive() time")
110 #
111 # This will print something like::
112 #
113 # real_expensive() time: 0m5.235s
114 #
115 function(timer_print_rel_time ABS_SECONDS_DEC_START ABS_SECONDS_DEC_END MESSAGE_STR )
116
117 timer_get_rel_seconds(${ABS_SECONDS_DEC_START} ${ABS_SECONDS_DEC_END} SECONDS_REL)
118 #print_var(SECONDS_REL)
119
120 split(${SECONDS_REL} "[.]" SECONDS_REL_ARRAY)
121 list(GET SECONDS_REL_ARRAY 0 SECONDS_REL_S)
122 list(GET SECONDS_REL_ARRAY 1 SECONDS_REL_NS)
123
124 # CMake does not support floating point so I need to do this manually
125 math(EXPR MINUTES_REL "${SECONDS_REL_S}/60")
126 math(EXPR SECONDS_REL_REMAINING "${SECONDS_REL_S} - 60*${MINUTES_REL}")
127 message("${MESSAGE_STR}: ${MINUTES_REL}m${SECONDS_REL_REMAINING}.${SECONDS_REL_NS}s")
128 endfunction()
129 # ToDo: Unit test the above function by calling message_wrapper().
130
131
132 #
133 # Helper functions
134 #
135
136
137 #
138 # Create a combined number of seconds as a combined integer given input
139 # decimal form <sec>.<nanosec>. This single int form can be subtracted by
140 # CMake (the decimal form can't).
141 #
142 function(timer_get_truncated_combined_int_from_decimal_num
143 ABS_SECONDS_DEC_IN DECIMAL_PLACES SECONDS_NS_INT_OUT
144 )
145 #print_var(ABS_SECONDS_DEC_IN)
146
147 split(${ABS_SECONDS_DEC_IN} "[.]" ABS_SECONDS_DEC_IN_ARRAY)
148 list(GET ABS_SECONDS_DEC_IN_ARRAY 0 ABS_SECONDS_DEC_IN_S)
149 list(GET ABS_SECONDS_DEC_IN_ARRAY 1 ABS_SECONDS_DEC_IN_NS)
150 #print_var(ABS_SECONDS_DEC_IN_S)
151 #print_var(ABS_SECONDS_DEC_IN_NS)
152
153 string(SUBSTRING ${ABS_SECONDS_DEC_IN_S} ${DECIMAL_PLACES} -1 SECONDS_S_TRUNCATED)
154 string(SUBSTRING ${ABS_SECONDS_DEC_IN_NS} 0 ${DECIMAL_PLACES} SECONDS_NS_TRUNCATED)
155 #print_var(SECONDS_S_TRUNCATED)
156 #print_var(SECONDS_NS_TRUNCATED)
157
158 set(${SECONDS_NS_INT_OUT} "${SECONDS_S_TRUNCATED}${SECONDS_NS_TRUNCATED}"
159 PARENT_SCOPE)
160
161 endfunction()
162
163
164 #
165 # Reform the formatted seconds in the form <sec>.<nanasec> from the combined
166 # int form.
167 #
168 function(timer_get_decimal_num_frum_truncated_combined_int
169 SECONDS_NS_INT_IN DECIMAL_PLACES
170 SECONDS_DEC_IN_OUT
171 )
172 #print_var(SECONDS_NS_INT_IN)
173
174 string(LENGTH ${SECONDS_NS_INT_IN} COMBINED_INT_LEN)
175 math(EXPR SEC_DIGITS "${COMBINED_INT_LEN}-${DECIMAL_PLACES}")
176 #print_var(COMBINED_INT_LEN)
177 #print_var(SEC_DIGITS)
178
179 if (SEC_DIGITS GREATER 0)
180 # There is at least one digit to the right of the decimal place
181 string(SUBSTRING ${SECONDS_NS_INT_IN} 0 ${SEC_DIGITS} SECONDS_S_TRUNCATED)
182 string(SUBSTRING ${SECONDS_NS_INT_IN} ${SEC_DIGITS} -1 SECONDS_NS_TRUNCATED)
183 else()
184 # There are no digits to the right of th decimal place. Therefore, there
185 # is 0 sec and we need to pad the decimal places with zeros.
186 set(SECONDS_S_TRUNCATED 0)
187
188 math(EXPR NUM_ZEROS "${DECIMAL_PLACES}-${COMBINED_INT_LEN}")
189 #print_var(NUM_ZEROS)
190 set(SECONDS_NS_TRUNCATED ${SECONDS_NS_INT_IN})
191 set(LOOP_IDX 0)
192 while (LOOP_IDX LESS ${NUM_ZEROS})
193 set(SECONDS_NS_TRUNCATED "0${SECONDS_NS_TRUNCATED}")
194 math(EXPR LOOP_IDX "${LOOP_IDX}+1")
195 endwhile()
196 endif()
197
198 #print_var(SECONDS_S_TRUNCATED)
199 #print_var(SECONDS_NS_TRUNCATED)
200
201 set(${SECONDS_DEC_IN_OUT} "${SECONDS_S_TRUNCATED}.${SECONDS_NS_TRUNCATED}"
202 PARENT_SCOPE)
203
204 endfunction()
205
206 # ToDo: Combine this basic code with code for scaling TIMEOUT into a simple
207 # DecimalMath.cmake module. We need to be able to add, subtract, and multiply
208 # to begin with.