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
25
#include <
diagnostic/SkywardStack.h
>
26
#include <
diagnostic/StackLogger.h
>
27
#include <
drivers/timer/TimestampTimer.h
>
28
29
using namespace
miosix
;
30
31
namespace
Boardcore
32
{
33
34
namespace
CpuMeter
35
{
36
37
const
int
period
= 100;
38
const
int
gap
= 100;
39
const
int
watchdogPeriod
= 20 *
period
;
40
41
static
FastMutex utilizationMutex;
42
static
Stats
utilization;
43
static
volatile
unsigned
int
update = 0;
44
45
CpuMeterData
getCpuStats
()
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
55
void
resetCpuStats
() { utilization.reset(); }
56
57
#ifdef ENABLE_CPU_METER
58
59
static
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
76
StackLogger::getInstance
().updateStack(
THID_CPU_METER
);
77
}
78
}
79
80
static
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
91
StackLogger::getInstance
().updateStack(
THID_CPU_WD
);
92
}
93
}
94
95
class
CpuMeterLauncher
96
{
97
public
:
98
CpuMeterLauncher
()
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
107
static
CpuMeterLauncher launcher;
108
109
#endif
// ENABLE_CPU_METER
110
111
}
// namespace CpuMeter
112
113
}
// namespace Boardcore
CpuMeter.h
SkywardStack.h
StackLogger.h
TimestampTimer.h
Boardcore::CpuMeter::CpuMeterLauncher
Definition
CpuMeter.cpp:96
Boardcore::CpuMeter::CpuMeterLauncher::CpuMeterLauncher
CpuMeterLauncher()
Definition
CpuMeter.cpp:98
Boardcore::Singleton< StackLogger >::getInstance
static StackLogger & getInstance()
Definition
Singleton.h:52
Boardcore::Stats
Computes on-line statistics of a dataset.
Definition
Stats.h:54
Boardcore::Canbus::l
PrintLogger l
Definition
CanDriver.cpp:41
Boardcore::CpuMeter::watchdogPeriod
const int watchdogPeriod
Definition
CpuMeter.cpp:39
Boardcore::CpuMeter::period
const int period
Definition
CpuMeter.cpp:37
Boardcore::CpuMeter::resetCpuStats
void resetCpuStats()
Resets the cpu utilization statistics.
Definition
CpuMeter.cpp:55
Boardcore::CpuMeter::getCpuStats
CpuMeterData getCpuStats()
Definition
CpuMeter.cpp:45
Boardcore::CpuMeter::gap
const int gap
Definition
CpuMeter.cpp:38
Boardcore::Kernel::getOldTick
long long getOldTick()
Get the current time in milliseconds.
Definition
KernelTime.h:43
Boardcore::TimestampTimer::getTimestamp
uint64_t getTimestamp()
Returns the current timer value in microseconds.
Definition
TimestampTimer.h:57
Boardcore
This file includes all the types the logdecoder script will decode.
Definition
ActiveObject.h:31
Boardcore::skywardStack
unsigned int skywardStack(unsigned int stack)
Definition
SkywardStack.h:36
Boardcore::THID_CPU_WD
@ THID_CPU_WD
Definition
StackData.h:43
Boardcore::THID_CPU_METER
@ THID_CPU_METER
Definition
StackData.h:42
miosix
Definition
stm32f2_f4_i2c.cpp:189
Boardcore::CpuMeterData
Definition
CpuMeterData.h:31
src
shared
diagnostic
CpuMeter
CpuMeter.cpp
Generated by
1.10.0