~aleteoryx/muditaos

muditaos/module-sys/SystemManager/CpuStatistics.cpp -rw-r--r-- 2.2 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <SystemManager/CpuStatistics.hpp>
#include <log/log.hpp>
#include <FreeRTOS.h>
#include <task.h>
#include <Utils.hpp>

extern "C"
{
#include "prof.h"
}

namespace sys
{

    CpuStatistics::CpuStatistics()
    {

#if PROF_ON
        data_size = prof_pool_get_data().size;
        data      = new task_prof_data[data_size];
        printer   = std::make_unique<cpu::stats::PackPrinter>();
#else
        // to change printer change assignment
        // printer = std::make_unique<cpu::stats::LogPrinter>();
        printer = std::make_unique<cpu::stats::NullPrinter>();
#endif
    }

    CpuStatistics::~CpuStatistics()
    {
#if PROF_ON
        delete[] data;
#endif
    }

    void CpuStatistics::StoreSysUsage()
    {
#if PROF_ON
        vTaskSuspendAll();
        {
            if (auto ovf = prof_pool_flush(data, data_size); ovf != 0) {
                LOG_FATAL("prof pool flush overflow: %d", int(ovf));
            }
        }
        xTaskResumeAll();
#endif
    }

    void CpuStatistics::TrackChange(const cpu::UpdateResult &ret)
    {
        if (ret.changed != sys::cpu::algorithm::Change::NoChange) {
            printer->printCPUChange(ret);
#if PROF_ON
            printer->printSysUsage(data, data_size);
#endif
        }
        printer->printPowerConsumption();
        StoreSysUsage();
    }

    void CpuStatistics::UpdatePercentageCpuLoad()
    {
        uint32_t idleTickCount     = xTaskGetIdleRunTimeCounter();
        uint32_t totalTickCount    = ulHighFrequencyTimerTicks();
        uint32_t idleTickIncrease  = utils::computeIncrease(idleTickCount, lastIdleTickCount);
        uint32_t totalTickIncrease = utils::computeIncrease(totalTickCount, lastTotalTickCount);
        lastIdleTickCount          = idleTickCount;
        lastTotalTickCount         = totalTickCount;
        if (totalTickIncrease != 0u) {
            cpuLoad = 100 - ((idleTickIncrease * 100) / totalTickIncrease);
        }
        else {
            cpuLoad = 0;
        }
    }

    uint32_t CpuStatistics::GetPercentageCpuLoad()
    {
        UpdatePercentageCpuLoad();
        return cpuLoad;
    }

} // namespace sys