blob: c311125543b2e5d95bd7cfefa6eebf13a99d301b [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include "timer.h"
22
alexander31ae9f02022-02-10 16:15:54 +000023#include <assert.h>
24#include <time.h>
25#include <string.h>
alexander3c798932021-03-26 21:42:19 +000026
27#define MILLISECONDS_IN_SECOND 1000
28#define MICROSECONDS_IN_SECOND 1000000
29#define NANOSECONDS_IN_MILLISECOND 1000000
30#define NANOSECONDS_IN_MICROSECOND 1000
31
32/**
33 * @brief Gets the current time counter value.
34 * @return Counter value expressed in terms of time_counter struct.
35 **/
36static time_counter get_time_counter(void)
37{
alexander31ae9f02022-02-10 16:15:54 +000038 struct timespec current_time;
alexander3c798932021-03-26 21:42:19 +000039 clock_gettime(1, &current_time);
40 time_counter t = {
41 .current_secs = current_time.tv_sec,
42 .current_nsecs = current_time.tv_nsec
43 };
44
45 return t;
46}
47
48/**
49 * @brief Gets the time duration elapsed between start and end.
50 * @param[in] start Pointer to time_counter value at start time.
51 * @param[in] end Pointer to time_counter value at end.
52 * @return Difference in milliseconds between the arguments expressed
53 * as unsigned 32 bit integer.
54 **/
55static time_t get_duration_milliseconds(time_counter *start, time_counter *end)
56{
57 /* Convert both parts of time struct to ms then add for complete time. */
58 time_t seconds_part =
59 (end->current_secs - start->current_secs) * MILLISECONDS_IN_SECOND;
60 time_t nanoseconds_part =
61 (end->current_nsecs - start->current_nsecs) / NANOSECONDS_IN_MILLISECOND;
62
63 return seconds_part + nanoseconds_part;
64}
65
66/**
67 * @brief Gets the time duration elapsed between start and end.
68 * @param[in] start Pointer to time_counter value at start time.
69 * @param[in] end Pointer to time_counter value at end.
70 * @return Difference in microseconds between the arguments expressed
71 * as unsigned 32 bit integer.
72 **/
73static time_t get_duration_microseconds(time_counter *start, time_counter *end)
74{
75 /* Convert both parts of time struct to us then add for complete time. */
76 time_t seconds_part =
77 (end->current_secs - start->current_secs) * MICROSECONDS_IN_SECOND;
78 time_t nanoseconds_part =
79 (end->current_nsecs - start->current_nsecs) / NANOSECONDS_IN_MICROSECOND;
80
81 return seconds_part + nanoseconds_part;
82}
83
84/**
85 * @brief Stub for timer reset.
86 **/
87void reset_timer() {}
88
89/**
90 * @brief Initialise the timer for this platform.
91 **/
92void init_timer(platform_timer *timer)
93{
94 assert(timer);
95 memset(timer, 0, sizeof(*timer));
96
97 timer->get_time_counter = get_time_counter;
98 timer->start_profiling = get_time_counter;
99 timer->stop_profiling = get_time_counter;
100 timer->get_duration_ms = get_duration_milliseconds;
101 timer->cap.duration_ms = 1;
102 timer->get_duration_us = get_duration_microseconds;
103 timer->cap.duration_us = 1;
104 timer->reset = reset_timer;
105 timer->inited = 1;
106}
107
108#ifdef __cplusplus
109}
110#endif