source: mainline/uspace/lib/c/include/perf.h@ b4a4ad94

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b4a4ad94 was 8ee106b, checked in by Vojtech Horky <vojtech.horky@…>, 7 years ago

Add stopwatch_t for precise wallclock measurement

The idea is to introduce system API for precise wall-clock measurement
early on even if the implementation is suboptimal. This API will always
be only a tiny wrapper around the best-possible time source but it
would hide the implementation details.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * Copyright (c) 2018 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 * System performance measurement utilities.
34 */
35
36#ifndef LIBC_PERF_H_
37#define LIBC_PERF_H_
38
39#include <time.h>
40
41/** Stopwatch is THE way to measure elapsed time on HelenOS. */
42typedef struct {
43 struct timespec start;
44 struct timespec end;
45} stopwatch_t;
46
47#define STOPWATCH_INITIALIZE_STATIC { { 0, 0 }, { 0, 0 } }
48
49/** Initialize system stopwatch.
50 *
51 * @param stopwatch Stopwatch to initialize.
52 */
53static inline void stopwatch_init(stopwatch_t *stopwatch)
54{
55 stopwatch->start.tv_sec = 0;
56 stopwatch->start.tv_nsec = 0;
57 stopwatch->end.tv_sec = 0;
58 stopwatch->end.tv_nsec = 0;
59}
60
61/** Emulate elapsed time for use in tests.
62 *
63 * @param stopwatch Stopwatch to use.
64 * @param nanos Elapsed time in nanoseconds to set.
65 */
66static inline void stopwatch_set_elapsed(stopwatch_t *stopwatch, nsec_t nanos)
67{
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);
72}
73
74/** Start the stopwatch.
75 *
76 * @param stopwatch Stopwatch to start.
77 */
78static inline void stopwatch_start(stopwatch_t *stopwatch)
79{
80 getuptime(&stopwatch->start);
81}
82
83/** Stop the stopwatch.
84 *
85 * @param stopwatch Stopwatch to stop.
86 */
87static inline void stopwatch_stop(stopwatch_t *stopwatch)
88{
89 getuptime(&stopwatch->end);
90}
91
92/** Get elapsed time in nanoseconds.
93 *
94 * @param stopwatch Stopwatch to read from.
95 * @return Elapsed time in nanoseconds.
96 */
97static inline nsec_t stopwatch_get_nanos(stopwatch_t *stopwatch)
98{
99 return ts_sub_diff(&stopwatch->end, &stopwatch->start);
100}
101
102#endif
103
104/** @}
105 */
Note: See TracBrowser for help on using the repository browser.