|
| 1 | +/* object_monitor.c - object monitor */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2016 Intel Corporation. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#include <zephyr.h> |
| 10 | +#include <tc_util.h> |
| 11 | +#include <util_test_common.h> |
| 12 | +#include <debug/object_tracing.h> |
| 13 | +#include "phil.h" |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * @brief Thread that traverses, counts and reports |
| 18 | + * the kernel objects in the philosophers application. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#define TOTAL_TEST_NUMBER 2 |
| 23 | + |
| 24 | +/* 1 IPM console fiber if enabled */ |
| 25 | +#if defined(CONFIG_IPM_CONSOLE_RECEIVER) && defined(CONFIG_PRINTK) |
| 26 | +#define IPM_THREAD 1 |
| 27 | +#else |
| 28 | +#define IPM_THREAD 0 |
| 29 | +#endif /* CONFIG_IPM_CONSOLE_RECEIVER && CONFIG_PRINTK*/ |
| 30 | + |
| 31 | +/* Must account for: |
| 32 | + * N Philosopher threads |
| 33 | + * 1 Object monitor thread |
| 34 | + * 1 System idle thread |
| 35 | + * 1 System workqueue thread |
| 36 | + * 1 IPM console thread |
| 37 | + */ |
| 38 | + |
| 39 | +void *force_sys_work_q_in = (void *)&k_sys_work_q; |
| 40 | + |
| 41 | +#define TOTAL_THREADS (N_PHILOSOPHERS + 3 + IPM_THREAD) |
| 42 | + |
| 43 | +#define OBJ_LIST_NAME k_sem |
| 44 | +#define OBJ_LIST_TYPE struct k_sem |
| 45 | + |
| 46 | +static inline int test_thread_monitor(void) |
| 47 | +{ |
| 48 | + int obj_counter = 0; |
| 49 | + struct k_thread *thread_list = NULL; |
| 50 | + |
| 51 | + /* wait a bit to allow any initialization-only threads to terminate */ |
| 52 | + k_sleep(100); |
| 53 | + |
| 54 | + thread_list = (struct k_thread *)SYS_THREAD_MONITOR_HEAD; |
| 55 | + while (thread_list != NULL) { |
| 56 | + if (thread_list->base.prio == -1) { |
| 57 | + TC_PRINT("PREMPT: %p OPTIONS: 0x%02x, STATE: 0x%02x\n", |
| 58 | + thread_list, |
| 59 | + thread_list->base.user_options, |
| 60 | + thread_list->base.thread_state); |
| 61 | + } else { |
| 62 | + TC_PRINT("COOP: %p OPTIONS: 0x%02x, STATE: 0x%02x\n", |
| 63 | + thread_list, |
| 64 | + thread_list->base.user_options, |
| 65 | + thread_list->base.thread_state); |
| 66 | + } |
| 67 | + thread_list = |
| 68 | + (struct k_thread *)SYS_THREAD_MONITOR_NEXT(thread_list); |
| 69 | + obj_counter++; |
| 70 | + } |
| 71 | + TC_PRINT("THREAD QUANTITY: %d\n", obj_counter); |
| 72 | + |
| 73 | + if (obj_counter == TOTAL_THREADS) { |
| 74 | + TC_END_RESULT(TC_PASS); |
| 75 | + return 1; |
| 76 | + } |
| 77 | + |
| 78 | + TC_END_RESULT(TC_FAIL); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +void object_monitor(void) |
| 83 | +{ |
| 84 | + int obj_counter; |
| 85 | + int test_counter = 0; |
| 86 | + void *obj_list = NULL; |
| 87 | + |
| 88 | + TC_START("OBJECT TRACING TEST"); |
| 89 | + |
| 90 | + obj_counter = 0; |
| 91 | + obj_list = SYS_TRACING_HEAD(OBJ_LIST_TYPE, OBJ_LIST_NAME); |
| 92 | + while (obj_list != NULL) { |
| 93 | + TC_PRINT("SEMAPHORE REF: %p\n", obj_list); |
| 94 | + obj_list = SYS_TRACING_NEXT(OBJ_LIST_TYPE, OBJ_LIST_NAME, |
| 95 | + obj_list); |
| 96 | + obj_counter++; |
| 97 | + } |
| 98 | + TC_PRINT("SEMAPHORE QUANTITY: %d\n", obj_counter); |
| 99 | + |
| 100 | + if (obj_counter == N_PHILOSOPHERS) { |
| 101 | + TC_END_RESULT(TC_PASS); |
| 102 | + test_counter++; |
| 103 | + } else { |
| 104 | + TC_END_RESULT(TC_FAIL); |
| 105 | + } |
| 106 | + |
| 107 | + test_counter += test_thread_monitor(); |
| 108 | + |
| 109 | + if (test_counter == TOTAL_TEST_NUMBER) { |
| 110 | + TC_END_REPORT(TC_PASS); |
| 111 | + } else { |
| 112 | + TC_END_REPORT(TC_FAIL); |
| 113 | + } |
| 114 | +} |
0 commit comments