2 * Copyright (c) 2018 Vojtech Horky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * System performance measurement utilities.
41 /** Stopwatch is THE way to measure elapsed time on HelenOS. */
43 struct timespec start
;
47 #define STOPWATCH_INITIALIZE_STATIC { { 0, 0 }, { 0, 0 } }
49 /** Initialize system stopwatch.
51 * @param stopwatch Stopwatch to initialize.
53 static inline void stopwatch_init(stopwatch_t
*stopwatch
)
55 stopwatch
->start
.tv_sec
= 0;
56 stopwatch
->start
.tv_nsec
= 0;
57 stopwatch
->end
.tv_sec
= 0;
58 stopwatch
->end
.tv_nsec
= 0;
61 /** Emulate elapsed time for use in tests.
63 * @param stopwatch Stopwatch to use.
64 * @param nanos Elapsed time in nanoseconds to set.
66 static inline void stopwatch_set_nanos(stopwatch_t
*stopwatch
, nsec_t nanos
)
68 stopwatch
->start
.tv_sec
= 0;
69 stopwatch
->start
.tv_nsec
= 0;
70 stopwatch
->end
.tv_sec
= NSEC2SEC(nanos
);
71 stopwatch
->end
.tv_nsec
= nanos
- SEC2NSEC(stopwatch
->end
.tv_sec
);
74 /** Start the stopwatch.
76 * Note that repeated starts/stops do NOT aggregate the elapsed time.
78 * @param stopwatch Stopwatch to start.
80 static inline void stopwatch_start(stopwatch_t
*stopwatch
)
82 getuptime(&stopwatch
->start
);
85 /** Stop the stopwatch.
87 * Note that repeated starts/stops do NOT aggregate the elapsed time.
89 * @param stopwatch Stopwatch to stop.
91 static inline void stopwatch_stop(stopwatch_t
*stopwatch
)
93 getuptime(&stopwatch
->end
);
96 /** Get elapsed time in nanoseconds.
98 * @param stopwatch Stopwatch to read from.
99 * @return Elapsed time in nanoseconds.
101 static inline nsec_t
stopwatch_get_nanos(stopwatch_t
*stopwatch
)
103 return ts_sub_diff(&stopwatch
->end
, &stopwatch
->start
);