| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Vojtech Horky
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup libc
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /** @file
|
|---|
| 11 | * System performance measurement utilities.
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | #ifndef _LIBC_PERF_H_
|
|---|
| 15 | #define _LIBC_PERF_H_
|
|---|
| 16 |
|
|---|
| 17 | #include <time.h>
|
|---|
| 18 |
|
|---|
| 19 | /** Stopwatch is THE way to measure elapsed time on HelenOS. */
|
|---|
| 20 | typedef struct {
|
|---|
| 21 | struct timespec start;
|
|---|
| 22 | struct timespec end;
|
|---|
| 23 | } stopwatch_t;
|
|---|
| 24 |
|
|---|
| 25 | #define STOPWATCH_INITIALIZE_STATIC { { 0, 0 }, { 0, 0 } }
|
|---|
| 26 |
|
|---|
| 27 | /** Initialize system stopwatch.
|
|---|
| 28 | *
|
|---|
| 29 | * @param stopwatch Stopwatch to initialize.
|
|---|
| 30 | */
|
|---|
| 31 | static inline void stopwatch_init(stopwatch_t *stopwatch)
|
|---|
| 32 | {
|
|---|
| 33 | stopwatch->start.tv_sec = 0;
|
|---|
| 34 | stopwatch->start.tv_nsec = 0;
|
|---|
| 35 | stopwatch->end.tv_sec = 0;
|
|---|
| 36 | stopwatch->end.tv_nsec = 0;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /** Emulate elapsed time for use in tests.
|
|---|
| 40 | *
|
|---|
| 41 | * @param stopwatch Stopwatch to use.
|
|---|
| 42 | * @param nanos Elapsed time in nanoseconds to set.
|
|---|
| 43 | */
|
|---|
| 44 | static inline void stopwatch_set_nanos(stopwatch_t *stopwatch, nsec_t nanos)
|
|---|
| 45 | {
|
|---|
| 46 | stopwatch->start.tv_sec = 0;
|
|---|
| 47 | stopwatch->start.tv_nsec = 0;
|
|---|
| 48 | stopwatch->end.tv_sec = NSEC2SEC(nanos);
|
|---|
| 49 | stopwatch->end.tv_nsec = nanos - SEC2NSEC(stopwatch->end.tv_sec);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /** Start the stopwatch.
|
|---|
| 53 | *
|
|---|
| 54 | * Note that repeated starts/stops do NOT aggregate the elapsed time.
|
|---|
| 55 | *
|
|---|
| 56 | * @param stopwatch Stopwatch to start.
|
|---|
| 57 | */
|
|---|
| 58 | static inline void stopwatch_start(stopwatch_t *stopwatch)
|
|---|
| 59 | {
|
|---|
| 60 | getuptime(&stopwatch->start);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /** Stop the stopwatch.
|
|---|
| 64 | *
|
|---|
| 65 | * Note that repeated starts/stops do NOT aggregate the elapsed time.
|
|---|
| 66 | *
|
|---|
| 67 | * @param stopwatch Stopwatch to stop.
|
|---|
| 68 | */
|
|---|
| 69 | static inline void stopwatch_stop(stopwatch_t *stopwatch)
|
|---|
| 70 | {
|
|---|
| 71 | getuptime(&stopwatch->end);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /** Get elapsed time in nanoseconds.
|
|---|
| 75 | *
|
|---|
| 76 | * @param stopwatch Stopwatch to read from.
|
|---|
| 77 | * @return Elapsed time in nanoseconds.
|
|---|
| 78 | */
|
|---|
| 79 | static inline nsec_t stopwatch_get_nanos(stopwatch_t *stopwatch)
|
|---|
| 80 | {
|
|---|
| 81 | return ts_sub_diff(&stopwatch->end, &stopwatch->start);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | #endif
|
|---|
| 85 |
|
|---|
| 86 | /** @}
|
|---|
| 87 | */
|
|---|