1 /*
2  * Copyright 2022-2023 NXP
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "NxpTimer.h"
18 
19 #include <android-base/logging.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <unistd.h>
23 
NxpTimer(std::string tag)24 NxpTimer::NxpTimer(std::string tag) {
25   logtag = std::move(tag);
26   is_running = false;
27   start_ts = end_ts = 0;
28 }
~NxpTimer()29 NxpTimer::~NxpTimer() {}
startTimer()30 void NxpTimer::startTimer() {
31   is_running = true;
32 
33   struct timespec tm;
34   clock_gettime(CLOCK_MONOTONIC, &tm);
35   start_ts = tm.tv_nsec * 1e-3 + tm.tv_sec * 1e+6;
36 
37   LOG(INFO) << logtag << " Timer started";
38 }
stopTimer()39 void NxpTimer::stopTimer() {
40   is_running = false;
41 
42   struct timespec tm;
43   clock_gettime(CLOCK_MONOTONIC, &tm);
44   end_ts = tm.tv_nsec * 1e-3 + tm.tv_sec * 1e+6;
45 
46   LOG(INFO) << logtag << " Timer stopped";
47 }
totalDuration()48 unsigned long NxpTimer::totalDuration() {
49   unsigned long duration = end_ts - start_ts;
50 
51   resetTimer();
52 
53   return duration;
54 }
resetTimer()55 void NxpTimer::resetTimer() {
56   is_running = false;
57   start_ts = end_ts = 0;
58 }
isRunning()59 bool NxpTimer::isRunning() { return is_running; }
60