
Example to demonstrate usage of `mbed_stats_cpu_get()` API usage
main.cpp@20:042d0f5536e8, 2019-02-13 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Feb 13 18:37:30 2019 +0000
- Revision:
- 20:042d0f5536e8
- Parent:
- 18:86a8fc778472
Merge pull request #20 from adbridge/master
Updating mbed-os to mbed-os-5.11.4
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-cpu-stats
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 15:86c93f5d67c4 | 1 | /* Copyright (c) 2018 Arm Limited |
mbed_official | 15:86c93f5d67c4 | 2 | * |
mbed_official | 15:86c93f5d67c4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 15:86c93f5d67c4 | 4 | * |
mbed_official | 15:86c93f5d67c4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 15:86c93f5d67c4 | 6 | * you may not use this file except in compliance with the License. |
mbed_official | 15:86c93f5d67c4 | 7 | * You may obtain a copy of the License at |
mbed_official | 15:86c93f5d67c4 | 8 | * |
mbed_official | 15:86c93f5d67c4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 15:86c93f5d67c4 | 10 | * |
mbed_official | 15:86c93f5d67c4 | 11 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 15:86c93f5d67c4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 15:86c93f5d67c4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 15:86c93f5d67c4 | 14 | * See the License for the specific language governing permissions and |
mbed_official | 15:86c93f5d67c4 | 15 | * limitations under the License. |
mbed_official | 15:86c93f5d67c4 | 16 | */ |
mbed_official | 0:3114f82feb68 | 17 | #include "mbed.h" |
mbed_official | 0:3114f82feb68 | 18 | |
mbed_official | 0:3114f82feb68 | 19 | #if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP) |
mbed_official | 0:3114f82feb68 | 20 | #error [NOT_SUPPORTED] test not supported |
mbed_official | 0:3114f82feb68 | 21 | #endif |
mbed_official | 0:3114f82feb68 | 22 | |
mbed_official | 0:3114f82feb68 | 23 | DigitalOut led1(LED1); |
mbed_official | 0:3114f82feb68 | 24 | |
mbed_official | 0:3114f82feb68 | 25 | #define MAX_THREAD_STACK 384 |
mbed_official | 0:3114f82feb68 | 26 | #define SAMPLE_TIME 1000 |
mbed_official | 0:3114f82feb68 | 27 | #define LOOP_TIME 3000 |
mbed_official | 0:3114f82feb68 | 28 | |
mbed_official | 0:3114f82feb68 | 29 | uint64_t prev_idle_time = 0; |
mbed_official | 0:3114f82feb68 | 30 | int32_t wait_time = 5000; |
mbed_official | 0:3114f82feb68 | 31 | |
mbed_official | 0:3114f82feb68 | 32 | void busy_thread() |
mbed_official | 0:3114f82feb68 | 33 | { |
mbed_official | 0:3114f82feb68 | 34 | volatile uint64_t i = ~0; |
mbed_official | 0:3114f82feb68 | 35 | |
mbed_official | 0:3114f82feb68 | 36 | while(i--) { |
mbed_official | 0:3114f82feb68 | 37 | led1 = !led1; |
mbed_official | 18:86a8fc778472 | 38 | wait_ms(wait_time); |
mbed_official | 0:3114f82feb68 | 39 | } |
mbed_official | 0:3114f82feb68 | 40 | } |
mbed_official | 0:3114f82feb68 | 41 | |
mbed_official | 0:3114f82feb68 | 42 | void print_stats() |
mbed_official | 0:3114f82feb68 | 43 | { |
mbed_official | 0:3114f82feb68 | 44 | mbed_stats_cpu_t stats; |
mbed_official | 0:3114f82feb68 | 45 | mbed_stats_cpu_get(&stats); |
mbed_official | 0:3114f82feb68 | 46 | |
mbed_official | 0:3114f82feb68 | 47 | printf("%-20lld", stats.uptime); |
mbed_official | 0:3114f82feb68 | 48 | printf("%-20lld", stats.idle_time); |
mbed_official | 0:3114f82feb68 | 49 | printf("%-20lld", stats.sleep_time); |
mbed_official | 0:3114f82feb68 | 50 | printf("%-20lld\n", stats.deep_sleep_time); |
mbed_official | 0:3114f82feb68 | 51 | } |
mbed_official | 0:3114f82feb68 | 52 | |
mbed_official | 0:3114f82feb68 | 53 | int main() |
mbed_official | 0:3114f82feb68 | 54 | { |
mbed_official | 0:3114f82feb68 | 55 | // Request the shared queue |
mbed_official | 0:3114f82feb68 | 56 | EventQueue *stats_queue = mbed_event_queue(); |
mbed_official | 0:3114f82feb68 | 57 | Thread *thread; |
mbed_official | 0:3114f82feb68 | 58 | int id; |
mbed_official | 0:3114f82feb68 | 59 | |
mbed_official | 0:3114f82feb68 | 60 | id = stats_queue->call_every(SAMPLE_TIME, print_stats); |
mbed_official | 0:3114f82feb68 | 61 | printf("%-20s%-20s%-20s%-20s\n", "Uptime", "Idle Time", "Sleep time", "DeepSleep time"); |
mbed_official | 0:3114f82feb68 | 62 | |
mbed_official | 0:3114f82feb68 | 63 | thread = new Thread(osPriorityNormal, MAX_THREAD_STACK); |
mbed_official | 0:3114f82feb68 | 64 | thread->start(busy_thread); |
mbed_official | 0:3114f82feb68 | 65 | |
mbed_official | 0:3114f82feb68 | 66 | // Steadily increase the system load |
mbed_official | 0:3114f82feb68 | 67 | for (int count = 1; ; count++) { |
mbed_official | 18:86a8fc778472 | 68 | ThisThread::sleep_for(LOOP_TIME); |
mbed_official | 0:3114f82feb68 | 69 | if (wait_time <= 0) { |
mbed_official | 0:3114f82feb68 | 70 | break; |
mbed_official | 0:3114f82feb68 | 71 | } |
mbed_official | 0:3114f82feb68 | 72 | wait_time -= 1000; |
mbed_official | 0:3114f82feb68 | 73 | } |
mbed_official | 0:3114f82feb68 | 74 | thread->terminate(); |
mbed_official | 0:3114f82feb68 | 75 | stats_queue->cancel(id); |
mbed_official | 0:3114f82feb68 | 76 | return 0; |
mbed_official | 0:3114f82feb68 | 77 | } |