YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
task_monitor.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// A task monitor which prints a task table to
15// stdout.
16//
17#ifndef _TASK_MONITOR_H
18#define _TASK_MONITOR_H
19
20#include <cstdio>
21#include "yahal_config.h"
22#include "task.h"
23#include "task_timer.h"
24#include "malloc.h"
25
26#define MONITOR_WAIT 5 // in seconds !!
27
28#define CLEAR_SCREEN "%c[H%c[J",27,27
29#define VT100_COLOR "%c[%dm",27
30#define BLACK 30
31#define RED 31
32#define GREEN 32
33#define YELLOW 33
34#define BLUE 34
35#define MAGENTA 35
36#define CYAN 36
37#define WHITE 37
38
39extern uint32_t __data_start__;
40extern uint32_t __data_end__;
41extern uint32_t __bss_start__;
42extern uint32_t __bss_end__;
43
45{
46public:
47 task_monitor() : task_timer("Monitor", DEFAULT_STACK_SIZE) {
48 setPeriod(MONITOR_WAIT * 1000000, TIMER::PERIODIC);
49 setCallback([this]() {
50 task * p = _list[_core].getHead();
51 uint32_t millis = task::millis();
52 printf(CLEAR_SCREEN);
53 printf(VT100_COLOR, BLUE);
54 printf("\n ---< YAHAL Task Monitor (core:%d uptime: %ldh %ldm %ld.%03lds) >--- \n\n",
55 _core,
56 millis/3600000,
57 (millis/60000) % 60,
58 (millis/1000) % 60,
59 millis%1000);
60 printf(VT100_COLOR, BLACK);
61
62 auto data_size = (uint32_t)(&__data_end__) - (uint32_t)(&__data_start__);
63 auto bss_size = (uint32_t)(&__bss_end__) - (uint32_t)(&__bss_start__);
64 uint32_t heap_used = mallinfo().uordblks;
65 printf(" RAM usage: data:%ld, bss:%ld, heap:%ld, total:%ld bytes\n",
66 data_size, bss_size, heap_used, data_size + bss_size + heap_used);
67
68 puts("+------------------+-----+------+-----------+-------------+--------+");
69 puts("| Task Name | Flg | Prio | State | Stack usage | CPU % |");
70 puts("+------------------+-----+------+-----------+-------------+--------+");
71 do {
72 uint32_t t = p->getDeltaTicks();
73 printf("| %-16s | %c/%c | %4d | %-9s | %4d / %4d |%3ld.%1ld %% |\n",
74 p->getName(),
75 p->isPrivileged() ? 'P' : 'U', p->isUsingFloat() ? 'F' : 'I',
76 p->getPriority(),
77 state_to_str(p->getState()),
78 p->getUsedStack(), p->getStackSize(),
79 t * 100 / MONITOR_WAIT / TICK_FREQUENCY,
80 (t * 1000 / MONITOR_WAIT / TICK_FREQUENCY) % 10
81 );
82 p = p->_next;
83 } while(p != _list[_core].getHead());
84 puts("+------------------+-----+------+-----------+-------------+--------+");
85 });
86 start();
87 }
88
89 ~task_monitor() override = default;
90
91 // No copy, no assignment
92 task_monitor (const task_monitor &) = delete;
93 task_monitor & operator= (const task_monitor &) = delete;
94};
95
96#endif // _TASK_MONITOR_H
Definition task.h:39