[8ee106b] | 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 |
|
---|
[4805495] | 36 | #ifndef _LIBC_PERF_H_
|
---|
| 37 | #define _LIBC_PERF_H_
|
---|
[8ee106b] | 38 |
|
---|
| 39 | #include <time.h>
|
---|
| 40 |
|
---|
| 41 | /** Stopwatch is THE way to measure elapsed time on HelenOS. */
|
---|
| 42 | typedef 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 | */
|
---|
| 53 | static 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 | */
|
---|
[5fbc1f9] | 66 | static inline void stopwatch_set_nanos(stopwatch_t *stopwatch, nsec_t nanos)
|
---|
[8ee106b] | 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.
|
---|
[51a04bd] | 75 | *
|
---|
| 76 | * Note that repeated starts/stops do NOT aggregate the elapsed time.
|
---|
[8ee106b] | 77 | *
|
---|
| 78 | * @param stopwatch Stopwatch to start.
|
---|
| 79 | */
|
---|
| 80 | static inline void stopwatch_start(stopwatch_t *stopwatch)
|
---|
| 81 | {
|
---|
| 82 | getuptime(&stopwatch->start);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /** Stop the stopwatch.
|
---|
[51a04bd] | 86 | *
|
---|
| 87 | * Note that repeated starts/stops do NOT aggregate the elapsed time.
|
---|
[8ee106b] | 88 | *
|
---|
| 89 | * @param stopwatch Stopwatch to stop.
|
---|
| 90 | */
|
---|
| 91 | static inline void stopwatch_stop(stopwatch_t *stopwatch)
|
---|
| 92 | {
|
---|
| 93 | getuptime(&stopwatch->end);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /** Get elapsed time in nanoseconds.
|
---|
| 97 | *
|
---|
| 98 | * @param stopwatch Stopwatch to read from.
|
---|
| 99 | * @return Elapsed time in nanoseconds.
|
---|
| 100 | */
|
---|
| 101 | static inline nsec_t stopwatch_get_nanos(stopwatch_t *stopwatch)
|
---|
| 102 | {
|
---|
| 103 | return ts_sub_diff(&stopwatch->end, &stopwatch->start);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | #endif
|
---|
| 107 |
|
---|
| 108 | /** @}
|
---|
| 109 | */
|
---|