Skip to content

Commit fe7c078

Browse files
author
Marcus Chang
committed
Added optional uptime and duty-cycle statistics. Default is off. Can be enabled with YOTTA_CFG_MINAR_STATS = true.
1 parent 5b273d8 commit fe7c078

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* 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, WITHOUT
13+
* 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+
18+
#ifndef MINAR_PLATFORM_STATS_H
19+
#define MINAR_PLATFORM_STATS_H
20+
21+
#include <stdint.h>
22+
23+
namespace minar {
24+
namespace stats {
25+
26+
/**
27+
* @brief Get uptime in seconds.
28+
* @details Uptime is seconds since last reboot.
29+
* @return uint64_t uptime in seconds.
30+
*/
31+
uint64_t getUptime();
32+
33+
/**
34+
* @brief Get seconds spent in active (non-sleep) mode.
35+
* @details The active time includes time used by interrupt handlers.
36+
* @return uint64_t active time in seconds.
37+
*/
38+
uint64_t getActive();
39+
40+
};
41+
};
42+
43+
#endif // MINAR_PLATFORM_STATS_H

source/mbed_platform.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ static bool timeIsInPeriod(minar::platform::tick_t start, minar::platform::tick_
2929

3030
///////////////////////////////////////////////////////////////////////////////
3131

32+
// functions and variables for keeping track of uptime and duty-cycle
33+
static uint64_t total_sleep = 0;
34+
static uint64_t total_active = 0;
35+
static minar::platform::tick_t last_wakeup = 0;
36+
static minar::platform::tick_t last_gotosleep = 0;
37+
38+
namespace minar {
39+
namespace stats {
40+
41+
/* Return uptime in seconds. */
42+
uint64_t getUptime() {
43+
return (total_active + total_sleep) / MINAR_PLATFORM_TIME_BASE;
44+
}
45+
46+
/* Return seconds spent in non-sleep mode. */
47+
uint64_t getActive() {
48+
return total_active / MINAR_PLATFORM_TIME_BASE;
49+
}
50+
51+
};
52+
};
53+
54+
///////////////////////////////////////////////////////////////////////////////
55+
3256
namespace minar {
3357
namespace platform {
3458

@@ -69,12 +93,25 @@ void sleepFromUntil(tick_t now, tick_t until){
6993
} else {
7094
const uint32_t next_int = lp_ticker_get_compare_match();
7195

96+
#if YOTTA_CFG_MINAR_STATS
97+
// update stats
98+
tick_t now = lp_ticker_read();
99+
total_active += now - last_wakeup;
100+
total_sleep += last_wakeup - last_gotosleep;
101+
last_gotosleep = now;
102+
#endif
103+
72104
if(timeIsInPeriod(now, until, next_int)){
73105
lp_ticker_sleep_until(now, until);
74106
} else {
75107
// existing compare match is sooner, go to sleep
76108
sleep();
77109
}
110+
111+
#if YOTTA_CFG_MINAR_STATS
112+
// update stats
113+
last_wakeup = lp_ticker_read();
114+
#endif
78115
}
79116
}
80117

0 commit comments

Comments
 (0)