~aleteoryx/muditaos

muditaos/module-sys/SystemManager/CpuPackPrinter.cpp -rw-r--r-- 3.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/CpuPrinter.hpp"
#include "SystemManager/SysCpuUpdateResult.hpp"
#include "SystemManager/SystemManagerCommon.hpp"
#include <battery_charger/battery_charger.hpp>
#include <time/time_conversion.hpp>
#include <RTOSWrapper/include/ticks.hpp>
#include <sink/RTTSink.hpp>
#include <msgpack11/msgpack11.hpp>

constexpr auto rtt_config = sink::rtt_traits{.chanel_no = 1, .size = 1024};

namespace sys::cpu::stats
{

    using namespace msgpack11;

    static auto getTimestamp()
    {
        return cpp_freertos::Ticks::TicksToMs(cpp_freertos::Ticks::GetTicks());
    }

    enum class PackID
    {
        Usage   = 1,
        ProcEnd = 2,
        Proc    = 3,
        Power   = 4,
    };

    PackPrinter::PackPrinter()
    {
        sink = std::make_unique<sink::RTT>(rtt_config);
    }

    PackPrinter::~PackPrinter()
    {}

    void PackPrinter::printSysUsage(struct task_prof_data *data, size_t size)
    {
        MsgPack procEnd = MsgPack::object({{"id", uint8_t(PackID::ProcEnd)}});
        auto ts         = getTimestamp();
        vTaskSuspendAll();
        {
            for (size_t i = 0; i < size; ++i) {
                if (data[i].exec_time == 0 && data[i].switches == 0) {
                    continue;
                }
                MsgPack obj = MsgPack::object{{"name", SystemManagerCommon::ServiceProcessor(data[i].task_TCB_id)},
                                              {"tcb", uint16_t(data[i].task_TCB_id)},
                                              {"time", data[i].exec_time},
                                              {"ts", ts},
                                              {"id", uint8_t(PackID::Proc)}};

                sink->put(obj.dump().c_str(), obj.dump().size());
            }
            sink->put(procEnd.dump().c_str(), procEnd.dump().size());
        }
        xTaskResumeAll();
    }

    void PackPrinter::printCPUChange(const cpu::UpdateResult &ret)
    {
        MsgPack obj = MsgPack::object{{"id", uint8_t(PackID::Usage)},
                                      {"freq", uint32_t(ret.frequencySet)},
                                      {"name", ret.data.name},
                                      {"reason", ret.data.reason},
                                      {"requested", uint32_t(ret.data.minFrequency)},
                                      {"avgA", int32_t(bsp::battery_charger::getAvgCurrent())},
                                      {"nowA", int32_t(bsp::battery_charger::getCurrentMeasurement())},
                                      {"ts", getTimestamp()}};

        sink->put(obj.dump().c_str(), obj.dump().size());
    }

    void PackPrinter::printPowerConsumption()
    {
        MsgPack obj = MsgPack::object{{"id", uint8_t(PackID::Power)},
                                      {"avgA", int32_t(bsp::battery_charger::getAvgCurrent())},
                                      {"nowA", int32_t(bsp::battery_charger::getCurrentMeasurement())},
                                      {"ts", getTimestamp()}};
        sink->put(obj.dump().c_str(), obj.dump().size());
    }
} // namespace sys::cpu::stats