Skyward boardcore
Loading...
Searching...
No Matches
CpuMeter.cpp
Go to the documentation of this file.
1/* Copyright (c) 2017 Skyward Experimental Rocketry
2 * Author: Federico Terraneo
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "CpuMeter.h"
24
28
29using namespace miosix;
30
31namespace Boardcore
32{
33
34namespace CpuMeter
35{
36
37const int period = 100;
38const int gap = 100;
39const int watchdogPeriod = 20 * period;
40
41static FastMutex utilizationMutex;
42static Stats utilization;
43static volatile unsigned int update = 0;
44
46{
47 Lock<FastMutex> l(utilizationMutex);
48 return CpuMeterData(TimestampTimer::getTimestamp(), utilization.getStats(),
49 MemoryProfiling::getAbsoluteFreeHeap(),
50 MemoryProfiling::getCurrentFreeHeap(),
51 MemoryProfiling::getAbsoluteFreeStack(),
52 MemoryProfiling::getCurrentFreeStack());
53}
54
55void resetCpuStats() { utilization.reset(); }
56
57#ifdef ENABLE_CPU_METER
58
59static void cpuMeterThread(void*)
60{
61 for (;;)
62 {
63 long long t1 = Kernel::getOldTick();
64 delayMs(period);
65 long long t2 = Kernel::getOldTick();
66
67 update++;
68 float delta = t2 - t1;
69 {
70 Lock<FastMutex> l(utilizationMutex);
71 utilization.add(100.f * (1.f - static_cast<float>(period) / delta));
72 }
73
74 Thread::sleep(gap);
75
77 }
78}
79
80static void watchdogThread(void*)
81{
82 for (unsigned int previous = update;; previous = update)
83 {
84 Thread::sleep(watchdogPeriod);
85 if (previous == update)
86 {
87 Lock<FastMutex> l(utilizationMutex);
88 utilization.add(100.0);
89 }
90
92 }
93}
94
96{
97public:
99 {
100 // Create the cpu meter thread with minimum priority
101 Thread::create(cpuMeterThread, skywardStack(STACK_MIN), 0, nullptr);
102 Thread::create(watchdogThread, skywardStack(STACK_MIN), MAIN_PRIORITY,
103 nullptr);
104 }
105};
106
107static CpuMeterLauncher launcher;
108
109#endif // ENABLE_CPU_METER
110
111} // namespace CpuMeter
112
113} // namespace Boardcore
static StackLogger & getInstance()
Definition Singleton.h:52
Computes on-line statistics of a dataset.
Definition Stats.h:54
PrintLogger l
Definition CanDriver.cpp:41
const int watchdogPeriod
Definition CpuMeter.cpp:39
void resetCpuStats()
Resets the cpu utilization statistics.
Definition CpuMeter.cpp:55
CpuMeterData getCpuStats()
Definition CpuMeter.cpp:45
long long getOldTick()
Get the current time in milliseconds.
Definition KernelTime.h:43
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
unsigned int skywardStack(unsigned int stack)
@ THID_CPU_WD
Definition StackData.h:43
@ THID_CPU_METER
Definition StackData.h:42