summaryrefslogtreecommitdiff
path: root/components/drivers/smp_call/utest/smp_002_tc.c
blob: e59d3059edff1894ed9568b44636db2b354bf3aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2006-2024 RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024/10/28     Shell        Added smp.smoke
 * 2025/12/3      ChuanN-sudo  add standardized utest documentation block
 */

/**
 * Test Case Name: SMP Call Smoke 002 Test
 *
 * Test Objectives:
 * - Validate thread-safety of rt_smp_call_cpu_mask() under concurrent calls from multiple CPUs.
 * - Verify system stability with simultaneous blocking SMP calls.
 * - Test core APIs: rt_smp_call_cpu_mask().
 *
 * Test Scenarios:
 * - One worker thread pinned to each CPU core.
 * - All threads simultaneously call rt_smp_call_cpu_mask() with random targets.
 * - Atomic counter tracks callbacks executed per thread.
 *
 * Verification Metrics:
 * - Callback count must match requested target count.
 * - Callbacks execute in interrupt-disabled context.
 * - All threads successfully created and pinned.
 * - Each thread tests all available CPUs.
 *
 * Dependencies:
 * - Hardware requirements:  QEMU emulator or any multi-core hardware platform that supports RT-Thread.
 * - Software configuration:
 *     - RT_USING_UTEST must be enabled (select "RT-Thread Utestcases" in menuconfig).
 *     - RT_UTEST_SMP_CALL_FUNC must be enabled(enable via:  RT-Thread Utestcases -> Kernel Components -> Drivers -> SMP-Call Test -> SMP-Call Smoke Test).
 * - Environmental Assumptions: System scheduler and SMP services working normally.
 *
 * Expected Results:
 * - Progress logs: A series of '#' characters indicating callback executions.
 * - Final output: "[ PASSED ] [ result ] testcase (components.drivers.smp_call.smoke_002)"
 */

#include <rtdevice.h>
#include <utest.h>
#include <utest_assert.h>
#include <smp_call.h>

#define PERCPU_TEST_COUNT 10000
#define NEWLINE_ON 80

static struct rt_semaphore _utestd_exited;
static rt_thread_t _utestd[RT_CPUS_NR];
static rt_atomic_t _entry_counts[RT_CPUS_NR];

static void _logging_progress(void)
{
    static rt_atomic_t counts;
    rt_ubase_t old;

    rt_kputs("#");
    old = rt_atomic_add(&counts, 1);
    if (old % NEWLINE_ON == 0)
    {
        rt_kputs("\n");
    }
}

static void _test_smp_cb(void *param)
{
    rt_ubase_t req_cpuid = (rt_ubase_t)param;

    if (!rt_hw_interrupt_is_disabled())
    {
        /* SYNC.004 */
        uassert_true(0);
    }

    _logging_progress();
    rt_atomic_add(&_entry_counts[req_cpuid], 1);
}

static void _utestd_entry(void *oncpu_param)
{
    rt_ubase_t oncpu = (rt_ubase_t)oncpu_param;
    volatile int cpu_mask;
    volatile int popcount = 0;
    rt_ubase_t tested_cpus = 0;

    if (rt_hw_cpu_id() != oncpu)
    {
        /* SYNC.004 */
        uassert_true(0);
    }

    for (size_t i = 0; i < PERCPU_TEST_COUNT; i++)
    {
        cpu_mask = rand() % RT_ALL_CPU;
        tested_cpus |= cpu_mask;

        rt_smp_call_cpu_mask(cpu_mask, _test_smp_cb, oncpu_param, SMP_CALL_WAIT_ALL);
        popcount += __builtin_popcount(cpu_mask);
    }

    LOG_D("popcount %d, _entry_counts[%d] %d", popcount, oncpu, _entry_counts[oncpu]);

    /* TARG.001 */
    uassert_true(popcount == rt_atomic_load(&_entry_counts[oncpu]));

    /* TOP.001, TOP.002 */
    uassert_true(tested_cpus == RT_ALL_CPU);

    rt_sem_release(&_utestd_exited);
}

static void _blocking_mtsafe_call(void)
{
    rt_err_t error;
    for (size_t i = 0; i < RT_CPUS_NR; i++)
    {
        error = rt_thread_startup(_utestd[i]);

        /* SYNC.001, SYNC.002, SYNC.003 */
        uassert_true(!error);
    }

    for (size_t i = 0; i < RT_CPUS_NR; i++)
    {
        rt_sem_take(&_utestd_exited, RT_WAITING_FOREVER);
    }
}

static rt_err_t utest_tc_init(void)
{
    for (size_t i = 0; i < RT_CPUS_NR; i++)
    {
        rt_atomic_store(&_entry_counts[i], 0);
        _utestd[i] = rt_thread_create("utestd", _utestd_entry, (void *)i,
                                      UTEST_THR_STACK_SIZE, UTEST_THR_PRIORITY,
                                      20);
        rt_thread_control(_utestd[i], RT_THREAD_CTRL_BIND_CPU, (void *)i);

        /* SYNC.001, SYNC.002, SYNC.003 */
        uassert_true(_utestd[i] != RT_NULL);
    }

    rt_sem_init(&_utestd_exited, "utestd", 0, RT_IPC_FLAG_PRIO);
    srand(rt_tick_get());

    return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
    rt_sem_detach(&_utestd_exited);

    return RT_EOK;
}

static void _testcase(void)
{
    UTEST_UNIT_RUN(_blocking_mtsafe_call);
}

UTEST_TC_EXPORT(_testcase, "components.drivers.smp_call.smoke_002", utest_tc_init, utest_tc_cleanup, 10);