diff options
author | Rob Barnes <robbarnes@google.com> | 2023-02-01 12:37:32 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-02-15 20:28:09 +0000 |
commit | a5ee4e01225eed03fc0141b1a7b9ca015b8a5b3c (patch) | |
tree | 2fd653dfb0cb8d66ce5d12e0ed032d723d8859ea /zephyr/test | |
parent | b05c46a23916b727ef2c357ffe4a28c7b2bb34c4 (diff) | |
download | chrome-ec-a5ee4e01225eed03fc0141b1a7b9ca015b8a5b3c.tar.gz |
tasks: Add task_id_to_thread_id and thread_id_to_task_id
Add thread_id_to_task_id and thread_id_to_task_id for mapping between
Zephyer thread ids and EC task ids.
This change allows for looking up threads without the need of
CONFIG_THREAD_MONITOR which costs ~1.5KB of additional flash.
Add new tests for checking the mapping between tasks and threads.
BUG=b:267470086
BRANCH=None
TEST=Unit tests
Change-Id: I9c7ef4d14248bfac5aede9f14adc302342fbe9a6
Signed-off-by: Rob Barnes <robbarnes@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4214561
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
Diffstat (limited to 'zephyr/test')
-rw-r--r-- | zephyr/test/tasks/extra_tasks.c | 89 | ||||
-rw-r--r-- | zephyr/test/tasks/main.c | 21 | ||||
-rw-r--r-- | zephyr/test/tasks/prj.conf | 1 | ||||
-rw-r--r-- | zephyr/test/tasks/testcase.yaml | 14 |
4 files changed, 120 insertions, 5 deletions
diff --git a/zephyr/test/tasks/extra_tasks.c b/zephyr/test/tasks/extra_tasks.c index ac11c20940..d9c75f4cfe 100644 --- a/zephyr/test/tasks/extra_tasks.c +++ b/zephyr/test/tasks/extra_tasks.c @@ -3,6 +3,7 @@ * found in the LICENSE file. */ +#include "ec_tasks.h" #include "host_command.h" #include "task.h" @@ -13,15 +14,16 @@ k_tid_t get_sysworkq_thread(void); k_tid_t get_idle_thread(void); -/* Utility functions for finding a Zephyr thread by name */ +/* Utilities for finding a Zephyr thread by name */ static k_tid_t found_thread; static void find_thread_by_name_cb(const struct k_thread *thread, void *user_data) { const char *name = (const char *)user_data; - if (strcmp(k_thread_name_get((k_tid_t)thread), name) == 0) + if (strcmp(k_thread_name_get((k_tid_t)thread), name) == 0) { found_thread = (k_tid_t)thread; + } } static k_tid_t find_thread_by_name(const char *name) @@ -31,20 +33,65 @@ static k_tid_t find_thread_by_name(const char *name) return found_thread; } -ZTEST_USER(extra_tasks, test_main_thread_mapping) +/* Utilities for checking asserts */ +static bool expect_assert; +static int num_asserts; +void assert_post_action(const char *file, unsigned int line) +{ + num_asserts += 1; + if (!expect_assert) { + ztest_test_fail(); + } +} + +#define EXPECT_ASSERT(test) \ + do { \ + expect_assert = true; \ + num_asserts = 0; \ + (test); \ + expect_assert = false; \ + zassert_equal(num_asserts, 1); \ + } while (0) + +ZTEST_USER(extra_tasks, test_hostcmd_thread_mapping) { k_tid_t hostcmd_thread; k_tid_t main_thread; +#if IS_ENABLED(HAS_TASK_HOSTCMD) +#ifdef CONFIG_TASK_HOSTCMD_THREAD_MAIN + k_thread_name_set(get_main_thread(), "HOSTCMD"); +#endif /* CONFIG_TASK_HOSTCMD_THREAD_MAIN */ + hostcmd_thread = find_thread_by_name("HOSTCMD"); zassert_not_null(hostcmd_thread); zassert_equal(hostcmd_thread, get_hostcmd_thread()); + zassert_equal(TASK_ID_HOSTCMD, thread_id_to_task_id(hostcmd_thread)); + zassert_equal(task_id_to_thread_id(TASK_ID_HOSTCMD), hostcmd_thread); +#ifdef CONFIG_TASK_HOSTCMD_THREAD_DEDICATED main_thread = find_thread_by_name("main"); zassert_not_null(main_thread); zassert_equal(main_thread, get_main_thread()); - /* Not equal when CONFIG_TASK_HOSTCMD_THREAD_DEDICATED is set */ zassert_not_equal(main_thread, hostcmd_thread); + zassert_equal(TASK_ID_MAIN, thread_id_to_task_id(main_thread)); + zassert_equal(task_id_to_thread_id(TASK_ID_MAIN), main_thread); +#else + main_thread = get_main_thread(); + zassert_not_null(main_thread); + zassert_equal(main_thread, hostcmd_thread); +#endif /* CONFIG_TASK_HOSTCMD_THREAD_DEDICATED */ + +#else /* !HAS_TASK_HOSTCMD */ + hostcmd_thread = find_thread_by_name("HOSTCMD"); + zassert_is_null(hostcmd_thread); + EXPECT_ASSERT(hostcmd_thread = get_hostcmd_thread()); + zassert_is_null(hostcmd_thread); + + main_thread = find_thread_by_name("main"); + zassert_not_null(main_thread); + zassert_equal(main_thread, get_main_thread()); +#endif /* HAS_TASK_HOSTCMD */ } ZTEST_USER(extra_tasks, test_sysworkq_thread_mapping) @@ -54,6 +101,8 @@ ZTEST_USER(extra_tasks, test_sysworkq_thread_mapping) sysworkq_thread = find_thread_by_name("sysworkq"); zassert_not_null(sysworkq_thread); zassert_equal(sysworkq_thread, get_sysworkq_thread()); + zassert_equal(TASK_ID_SYSWORKQ, thread_id_to_task_id(sysworkq_thread)); + zassert_equal(task_id_to_thread_id(TASK_ID_SYSWORKQ), sysworkq_thread); } ZTEST_USER(extra_tasks, test_idle_thread_mapping) @@ -63,6 +112,38 @@ ZTEST_USER(extra_tasks, test_idle_thread_mapping) idle_thread = find_thread_by_name("idle"); zassert_not_null(idle_thread); zassert_equal(idle_thread, get_idle_thread()); + zassert_equal(TASK_ID_IDLE, thread_id_to_task_id(idle_thread)); + zassert_equal(task_id_to_thread_id(TASK_ID_IDLE), idle_thread); +} + +ZTEST_USER(extra_tasks, test_invalid_task_id) +{ + k_tid_t thread_id; + + EXPECT_ASSERT(thread_id = task_id_to_thread_id(TASK_ID_INVALID)); + zassert_is_null(thread_id); + + EXPECT_ASSERT(thread_id = task_id_to_thread_id(-1)); + zassert_is_null(thread_id); +} + +ZTEST_USER(extra_tasks, test_invalid_thread_id) +{ + task_id_t task_id; + + EXPECT_ASSERT(task_id = thread_id_to_task_id(NULL)); + zassert_equal(task_id, TASK_ID_INVALID); + + EXPECT_ASSERT(task_id = thread_id_to_task_id((k_tid_t)0x1234)); + zassert_equal(task_id, TASK_ID_INVALID); +} + +ZTEST_USER(extra_tasks, test_extra_task_enumeration) +{ + for (task_id_t task_id = 0; task_id < TASK_ID_COUNT + EXTRA_TASK_COUNT; + task_id++) { + zassert_not_null(task_id_to_thread_id(task_id)); + } } ZTEST_SUITE(extra_tasks, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/tasks/main.c b/zephyr/test/tasks/main.c index bbf752c656..dc3df59ca7 100644 --- a/zephyr/test/tasks/main.c +++ b/zephyr/test/tasks/main.c @@ -10,6 +10,7 @@ #include <stdbool.h> #include <zephyr/kernel.h> +#include <zephyr/kernel/thread.h> #include <zephyr/ztest.h> /* Second for platform/ec task API (in microseconds). */ @@ -273,6 +274,23 @@ static void empty_set_mask2(void) zassert_within(end_ms - start_ms, 2000, 100, "Timeout for 2 seconds"); } +static void check_task_1_mapping(void) +{ + zassert_equal(TASK_ID_TASK_1, thread_id_to_task_id(k_current_get())); + zassert_equal(k_current_get(), task_id_to_thread_id(TASK_ID_TASK_1)); +} + +static void check_task_2_mapping(void) +{ + zassert_equal(TASK_ID_TASK_2, thread_id_to_task_id(k_current_get())); + zassert_equal(k_current_get(), task_id_to_thread_id(TASK_ID_TASK_2)); +} + +static void test_thread_to_task_mapping(void) +{ + run_test(&check_task_1_mapping, &check_task_2_mapping); +} + static void test_empty_set_mask(void) { run_test(&empty_set_mask1, &empty_set_mask2); @@ -289,6 +307,7 @@ void test_main(void) ztest_unit_test(test_event_delivered), ztest_unit_test(test_event_mask_not_delivered), ztest_unit_test(test_event_mask_extra), - ztest_unit_test(test_empty_set_mask)); + ztest_unit_test(test_empty_set_mask), + ztest_unit_test(test_thread_to_task_mapping)); ztest_run_test_suite(test_task_shim); } diff --git a/zephyr/test/tasks/prj.conf b/zephyr/test/tasks/prj.conf index deb8253bde..67deb38144 100644 --- a/zephyr/test/tasks/prj.conf +++ b/zephyr/test/tasks/prj.conf @@ -5,6 +5,7 @@ CONFIG_ZTEST=y CONFIG_PLATFORM_EC=y CONFIG_CROS_EC=y +CONFIG_TASKS_SET_TEST_RUNNER_TID_RULE=y CONFIG_PLATFORM_EC_VBOOT_HASH=n CONFIG_PLATFORM_EC_BACKLIGHT_LID=n CONFIG_PLATFORM_EC_SWITCH=n diff --git a/zephyr/test/tasks/testcase.yaml b/zephyr/test/tasks/testcase.yaml index 4604e966e9..c079930c6c 100644 --- a/zephyr/test/tasks/testcase.yaml +++ b/zephyr/test/tasks/testcase.yaml @@ -12,5 +12,19 @@ tests: extra_configs: - CONFIG_ZTEST_NEW_API=y - CONFIG_SHIMMED_TASKS=y + - CONFIG_ASSERT_TEST=y + - CONFIG_PLATFORM_EC_HOSTCMD=n + tasks.extra_tasks.hostcmd_main: + extra_configs: + - CONFIG_ZTEST_NEW_API=y + - CONFIG_SHIMMED_TASKS=y + - CONFIG_ASSERT_TEST=y + - CONFIG_PLATFORM_EC_HOSTCMD=y + - CONFIG_TASK_HOSTCMD_THREAD_MAIN=y + tasks.extra_tasks.hostcmd_dedicated: + extra_configs: + - CONFIG_ZTEST_NEW_API=y + - CONFIG_SHIMMED_TASKS=y + - CONFIG_ASSERT_TEST=y - CONFIG_PLATFORM_EC_HOSTCMD=y - CONFIG_TASK_HOSTCMD_THREAD_DEDICATED=y |