mbed-os-examples / Mbed OS mbed-os-example-mbed5-cpu-stats
Committer:
mbed_official
Date:
Wed Aug 01 15:00:27 2018 +0100
Revision:
5:2c41d8ba7f30
Parent:
0:3114f82feb68
Child:
15:86c93f5d67c4
Merge pull request #6 from cmonr/master

Updating mbed-os to mbed-os-5.9.4
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-cpu-stats

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:3114f82feb68 1 #include "mbed.h"
mbed_official 0:3114f82feb68 2
mbed_official 0:3114f82feb68 3 #if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP)
mbed_official 0:3114f82feb68 4 #error [NOT_SUPPORTED] test not supported
mbed_official 0:3114f82feb68 5 #endif
mbed_official 0:3114f82feb68 6
mbed_official 0:3114f82feb68 7 DigitalOut led1(LED1);
mbed_official 0:3114f82feb68 8
mbed_official 0:3114f82feb68 9 #define MAX_THREAD_STACK 384
mbed_official 0:3114f82feb68 10 #define SAMPLE_TIME 1000
mbed_official 0:3114f82feb68 11 #define LOOP_TIME 3000
mbed_official 0:3114f82feb68 12
mbed_official 0:3114f82feb68 13 uint64_t prev_idle_time = 0;
mbed_official 0:3114f82feb68 14 int32_t wait_time = 5000;
mbed_official 0:3114f82feb68 15
mbed_official 0:3114f82feb68 16 void busy_thread()
mbed_official 0:3114f82feb68 17 {
mbed_official 0:3114f82feb68 18 volatile uint64_t i = ~0;
mbed_official 0:3114f82feb68 19
mbed_official 0:3114f82feb68 20 while(i--) {
mbed_official 0:3114f82feb68 21 led1 = !led1;
mbed_official 0:3114f82feb68 22 wait_us(wait_time);
mbed_official 0:3114f82feb68 23 }
mbed_official 0:3114f82feb68 24 }
mbed_official 0:3114f82feb68 25
mbed_official 0:3114f82feb68 26 void print_stats()
mbed_official 0:3114f82feb68 27 {
mbed_official 0:3114f82feb68 28 mbed_stats_cpu_t stats;
mbed_official 0:3114f82feb68 29 mbed_stats_cpu_get(&stats);
mbed_official 0:3114f82feb68 30
mbed_official 0:3114f82feb68 31 printf("%-20lld", stats.uptime);
mbed_official 0:3114f82feb68 32 printf("%-20lld", stats.idle_time);
mbed_official 0:3114f82feb68 33 printf("%-20lld", stats.sleep_time);
mbed_official 0:3114f82feb68 34 printf("%-20lld\n", stats.deep_sleep_time);
mbed_official 0:3114f82feb68 35 }
mbed_official 0:3114f82feb68 36
mbed_official 0:3114f82feb68 37 int main()
mbed_official 0:3114f82feb68 38 {
mbed_official 0:3114f82feb68 39 // Request the shared queue
mbed_official 0:3114f82feb68 40 EventQueue *stats_queue = mbed_event_queue();
mbed_official 0:3114f82feb68 41 Thread *thread;
mbed_official 0:3114f82feb68 42 int id;
mbed_official 0:3114f82feb68 43
mbed_official 0:3114f82feb68 44 id = stats_queue->call_every(SAMPLE_TIME, print_stats);
mbed_official 0:3114f82feb68 45 printf("%-20s%-20s%-20s%-20s\n", "Uptime", "Idle Time", "Sleep time", "DeepSleep time");
mbed_official 0:3114f82feb68 46
mbed_official 0:3114f82feb68 47 thread = new Thread(osPriorityNormal, MAX_THREAD_STACK);
mbed_official 0:3114f82feb68 48 thread->start(busy_thread);
mbed_official 0:3114f82feb68 49
mbed_official 0:3114f82feb68 50 // Steadily increase the system load
mbed_official 0:3114f82feb68 51 for (int count = 1; ; count++) {
mbed_official 0:3114f82feb68 52 Thread::wait(LOOP_TIME);
mbed_official 0:3114f82feb68 53 if (wait_time <= 0) {
mbed_official 0:3114f82feb68 54 break;
mbed_official 0:3114f82feb68 55 }
mbed_official 0:3114f82feb68 56 wait_time -= 1000;
mbed_official 0:3114f82feb68 57 }
mbed_official 0:3114f82feb68 58 thread->terminate();
mbed_official 0:3114f82feb68 59 stats_queue->cancel(id);
mbed_official 0:3114f82feb68 60 return 0;
mbed_official 0:3114f82feb68 61 }