forked from ARMmbed/mbed-os-example-cpu-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
84 lines (72 loc) · 2.33 KB
/
Copy pathmain.cpp
File metadata and controls
84 lines (72 loc) · 2.33 KB
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
/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "platform/mbed_thread.h"
#if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP)
#error [NOT_SUPPORTED] test not supported
#endif
// Initialise the digital pin LED1 as an output
DigitalOut led1(LED1);
#define MAX_THREAD_STACK 384
#define SAMPLE_TIME_MS 2000
#define LOOP_TIME_MS 3000
uint64_t prev_idle_time = 0;
int32_t wait_time_ms = 5000;
void busy_thread()
{
volatile uint64_t i = ~0;
while(i--) {
led1 = !led1;
thread_sleep_for(wait_time_ms);
}
}
void print_cpu_stats()
{
mbed_stats_cpu_t stats;
mbed_stats_cpu_get(&stats);
// Calculate the percentage of CPU usage
uint64_t diff_usec = (stats.idle_time - prev_idle_time);
uint8_t idle = (diff_usec * 100) / (SAMPLE_TIME_MS*1000);
uint8_t usage = 100 - ((diff_usec * 100) / (SAMPLE_TIME_MS*1000));
prev_idle_time = stats.idle_time;
printf("Time(us): Up: %lld", stats.uptime);
printf(" Idle: %lld", stats.idle_time);
printf(" Sleep: %lld", stats.sleep_time);
printf(" DeepSleep: %lld\n", stats.deep_sleep_time);
printf("Idle: %d%% Usage: %d%%\n\n", idle, usage);
}
int main()
{
// Request the shared queue
EventQueue *stats_queue = mbed_event_queue();
Thread *thread;
int id;
id = stats_queue->call_every(SAMPLE_TIME_MS, print_cpu_stats);
thread = new Thread(osPriorityNormal, MAX_THREAD_STACK);
thread->start(busy_thread);
// Steadily increase the system load
for (int count = 1; ; count++) {
thread_sleep_for(LOOP_TIME_MS);
if (wait_time_ms <= 0) {
break;
}
wait_time_ms -= 1000;
}
thread->terminate();
stats_queue->cancel(id);
return 0;
}