|
| 1 | + |
| 2 | +/* mbed Microcontroller Library |
| 3 | + * Copyright (c) 2018 ARM Limited |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "greentea-client/test_env.h" |
| 19 | +#include "unity/unity.h" |
| 20 | +#include "utest/utest.h" |
| 21 | + |
| 22 | +#include "mbed.h" |
| 23 | + |
| 24 | +#if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LOWPOWERTIMER) || !defined(DEVICE_SLEEP) |
| 25 | +#error [NOT_SUPPORTED] test not supported |
| 26 | +#endif |
| 27 | + |
| 28 | +using namespace utest::v1; |
| 29 | + |
| 30 | +DigitalOut led1(LED1); |
| 31 | + |
| 32 | +#define MAX_THREAD_STACK 384 |
| 33 | +#define SAMPLE_TIME 1000 // msec |
| 34 | +#define LOOP_TIME 2000 // msec |
| 35 | + |
| 36 | +static int32_t wait_time = 5000; |
| 37 | + |
| 38 | +static void busy_thread() |
| 39 | +{ |
| 40 | + volatile uint64_t i = ~0; |
| 41 | + |
| 42 | + while (i--) { |
| 43 | + led1 = !led1; |
| 44 | + wait_us(wait_time); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void get_cpu_usage() |
| 49 | +{ |
| 50 | + static uint64_t prev_idle_time = 0; |
| 51 | + mbed_stats_cpu_t stats; |
| 52 | + |
| 53 | + while (1) { |
| 54 | + mbed_stats_cpu_get(&stats); |
| 55 | + uint64_t diff = (stats.idle_time - prev_idle_time); |
| 56 | + uint8_t usage = 100 - ((diff * 100) / (SAMPLE_TIME * 1000)); |
| 57 | + prev_idle_time = stats.idle_time; |
| 58 | + TEST_ASSERT_NOT_EQUAL(0, usage); |
| 59 | + Thread::wait(SAMPLE_TIME); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void test_cpu_info(void) |
| 64 | +{ |
| 65 | + mbed_stats_cpu_t stats; |
| 66 | + // Additional read to make sure timer is initialized |
| 67 | + mbed_stats_cpu_get(&stats); |
| 68 | + Thread::wait(1); |
| 69 | + mbed_stats_cpu_get(&stats); |
| 70 | + TEST_ASSERT_NOT_EQUAL(0, stats.uptime); |
| 71 | + TEST_ASSERT_NOT_EQUAL(0, stats.idle_time); |
| 72 | + return; |
| 73 | +} |
| 74 | + |
| 75 | +void test_cpu_load(void) |
| 76 | +{ |
| 77 | + |
| 78 | + Thread thread(osPriorityNormal, MAX_THREAD_STACK); |
| 79 | + Thread thread_stats(osPriorityNormal, MAX_THREAD_STACK); |
| 80 | + |
| 81 | + thread.start(busy_thread); |
| 82 | + thread_stats.start(get_cpu_usage); |
| 83 | + |
| 84 | + // Steadily increase the system load |
| 85 | + for (int count = 1; ; count++) { |
| 86 | + Thread::wait(LOOP_TIME); |
| 87 | + if (wait_time <= 0) { |
| 88 | + break; |
| 89 | + } |
| 90 | + wait_time -= 1000; // usec |
| 91 | + } |
| 92 | + thread.terminate(); |
| 93 | + thread_stats.terminate(); |
| 94 | +} |
| 95 | + |
| 96 | +Case cases[] = { |
| 97 | + Case("Test CPU Info", test_cpu_info), |
| 98 | + Case("Test CPU load", test_cpu_load) |
| 99 | +}; |
| 100 | + |
| 101 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 102 | +{ |
| 103 | + GREENTEA_SETUP(20, "default_auto"); |
| 104 | + return greentea_test_setup_handler(number_of_cases); |
| 105 | +} |
| 106 | + |
| 107 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 108 | + |
| 109 | +int main() |
| 110 | +{ |
| 111 | + Harness::run(specification); |
| 112 | +} |
0 commit comments