summaryrefslogtreecommitdiff
path: root/test/smp/regression/threadx_trace_object_registry_test.c
blob: 08933f54921835b0247df02de93b82120307eb7a (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/***************************************************************************
 * Copyright (c) 2026 Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5).
 * The AI-generated portions may be considered public domain (CC0-1.0)
 * and not subject to the project's licence. The human contributor has
 * reviewed and verified that the code is correct.
 *
 * SPDX-License-Identifier: MIT and CC0-1.0
 **************************************************************************/

/* This test drives the three decisions in the trace object registry that
   ordinary use never reaches.

   The first is the name copy. It stops at the string's terminator, so an object
   whose name is at least as long as a registry entry's name field leaves the
   loop by its own count instead. The second is the same copy handed no name at
   all, which it recognises and stops on. The third is the choice the registry
   search makes when it has already found one free entry and comes across a
   second: it prefers the later one unless the earlier one has never held a
   valid object, and nothing the kernel registers produces an entry that is both
   in use and of no valid type.

   The third is therefore driven by registering an object of the type the
   registry treats as not valid, giving the search start back to that entry, and
   registering again - so the search meets a free entry that has never held a
   valid object, and keeps it.

   The registry is walked and rearranged under one SMP protection, which on this
   port is what keeps the other cores and the tick out of it. The registry is
   global state the kernel writes from every service that creates an object, so
   the arrangement the third decision needs has to be built and used without
   anything else registering in between.

   Without event tracing compiled in the registry does not exist, so the test
   only checks that the trace services report the feature as disabled.  */


#include   <stdio.h>
#include   "tx_api.h"
#define    TX_SOURCE_CODE
#include   "tx_trace.h"


/* A name at least as long as a registry entry's name field, so the copy runs to
   its own limit rather than to a terminator.  */

static CHAR            long_name[] =  "an object name longer than the registry keeps room for";

static unsigned long   thread_0_counter =  0;
static TX_THREAD       thread_0;

static TX_QUEUE        queue_0;
static TX_QUEUE        queue_1;
static ULONG           queue_0_area[4];
static ULONG           queue_1_area[4];

static ULONG           trace_buffer[512];

#define TRACE_REGISTRY_ENTRIES      16

/* Stand-in objects, used only as registry keys. The registry stores the pointer
   it is given and never follows it.  */

#define FILLER_OBJECTS              (TRACE_REGISTRY_ENTRIES + 1)

static ULONG           filler_object[FILLER_OBJECTS];
static ULONG           probe_object;


/* Define thread prototypes.  */

static void    thread_0_entry(ULONG thread_input);


/* Prototype for test control return.  */

void  test_control_return(UINT status);


#ifdef TX_ENABLE_EVENT_TRACE

/* The registry index holding the given object, or the total number of entries
   if it holds none.  */

static ULONG  registry_index_of(VOID *object_ptr)
{

ULONG   index;


    for (index = 0; index < _tx_trace_total_registry_entries; index++)
    {

        if (_tx_trace_registry_start_ptr[index].tx_trace_object_entry_thread_pointer == (ULONG) object_ptr)
        {
            break;
        }
    }

    return(index);
}

#endif


/* Define what the initial system looks like.  */

#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void    threadx_trace_object_registry_application_define(void *first_unused_memory)
#endif
{

UINT    status;
CHAR    *pointer;


    /* Put first available memory address into a character pointer.  */
    pointer =  (CHAR *) first_unused_memory;

    /* Put system definition stuff in here, e.g. thread creates and other assorted
       create information.  */
    status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
            pointer, TEST_STACK_SIZE_PRINTF,
            16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
    pointer =  pointer + TEST_STACK_SIZE_PRINTF;

    /* Check for status.  */
    if (status != TX_SUCCESS)
    {

        printf("Running Trace Object Registry Test................................. ERROR #1\n");
        test_control_return(1);
    }
}


/* Define the test thread.  */

static void    thread_0_entry(ULONG thread_input)
{

UINT    status;
#ifdef TX_ENABLE_EVENT_TRACE
TX_INTERRUPT_SAVE_AREA
ULONG   fillers_used;
ULONG   not_valid_index;
ULONG   second_free_index;
VOID   *second_free_object;
#endif


    /* Increment thread 0 counter.  */
    thread_0_counter++;

    /* Inform user of success getting to this test.  */
    printf("Running Trace Object Registry Test................................. ");

    /* Enable event tracing, so that every create below registers.  */
    status =  tx_trace_enable(trace_buffer, sizeof(trace_buffer), TRACE_REGISTRY_ENTRIES);

#ifndef TX_ENABLE_EVENT_TRACE

    if (status != TX_FEATURE_NOT_ENABLED)
    {

        printf("ERROR #2\n");
        test_control_return(1);
    }

    printf("SUCCESS!\n");
    test_control_return(0);

#else

    if (status != TX_SUCCESS)
    {

        printf("ERROR #3\n");
        test_control_return(1);
    }

    /* An object whose name is longer than a registry entry keeps room for. The
       copy runs out of room rather than reaching a terminator.  */
    status =  tx_queue_create(&queue_0, long_name, TX_1_ULONG, queue_0_area, sizeof(queue_0_area));

    if (status != TX_SUCCESS)
    {

        printf("ERROR #4\n");
        test_control_return(1);
    }

    /* The name field is filled to its last byte and terminated there.  */
    if (_tx_trace_registry_start_ptr[registry_index_of(&queue_0)].tx_trace_object_entry_name[TX_TRACE_OBJECT_REGISTRY_NAME - 1] != ((UCHAR) 0))
    {

        printf("ERROR #5\n");
        test_control_return(1);
    }

    /* An object with no name at all. The copy recognises it and stops before
       reading anything.  */
    status =  tx_queue_create(&queue_1, TX_NULL, TX_1_ULONG, queue_1_area, sizeof(queue_1_area));

    if (status != TX_SUCCESS)
    {

        printf("ERROR #6\n");
        test_control_return(1);
    }

    if (_tx_trace_registry_start_ptr[registry_index_of(&queue_1)].tx_trace_object_entry_name[0] != ((UCHAR) 0))
    {

        printf("ERROR #7\n");
        test_control_return(1);
    }

    /* Fill every remaining registry entry, the first of them with the object
       type the registry treats as not valid. The search below has to walk the
       whole list, and it stops at the first entry that has never been used at
       all, so no entry may be left unused.  */
    TX_DISABLE

    fillers_used =  0;

    while ((_tx_trace_available_registry_entries != ((ULONG) 0)) && (fillers_used < FILLER_OBJECTS))
    {

        _tx_trace_object_register((fillers_used == ((ULONG) 0)) ? TX_TRACE_OBJECT_TYPE_NOT_VALID : TX_TRACE_OBJECT_TYPE_QUEUE,
                                  &filler_object[fillers_used], "filler", ((ULONG) 0), ((ULONG) 0));
        fillers_used++;
    }

    TX_RESTORE

    if ((_tx_trace_available_registry_entries != ((ULONG) 0)) || (fillers_used == ((ULONG) 0)))
    {

        printf("ERROR #8\n");
        test_control_return(1);
    }

    not_valid_index =  registry_index_of(&filler_object[0]);

    if (not_valid_index >= _tx_trace_total_registry_entries)
    {

        printf("ERROR #9\n");
        test_control_return(1);
    }

    /* Free two entries, the one holding no valid type last, so the search
       starts there. The search then finds it first and meets the other one
       afterwards, which is the arrangement the choice below is made in.  */
    second_free_index =   (not_valid_index + ((ULONG) 1)) % _tx_trace_total_registry_entries;
    second_free_object =  (VOID *) _tx_trace_registry_start_ptr[second_free_index].tx_trace_object_entry_thread_pointer;

    TX_DISABLE

    _tx_trace_object_unregister(second_free_object);
    _tx_trace_object_unregister(&filler_object[0]);

    /* The search keeps the entry that has never held a valid object rather than
       moving on to the later free one.  */
    _tx_trace_object_register(TX_TRACE_OBJECT_TYPE_QUEUE, &probe_object, "probe", ((ULONG) 0), ((ULONG) 0));

    TX_RESTORE

    if (registry_index_of(&probe_object) != not_valid_index)
    {

        printf("ERROR #10\n");
        test_control_return(1);
    }

    status =  tx_queue_delete(&queue_0);

    if (status != TX_SUCCESS)
    {

        printf("ERROR #11\n");
        test_control_return(1);
    }

    status =  tx_queue_delete(&queue_1);

    if (status != TX_SUCCESS)
    {

        printf("ERROR #12\n");
        test_control_return(1);
    }

    status =  tx_trace_disable();

    if (status != TX_SUCCESS)
    {

        printf("ERROR #13\n");
        test_control_return(1);
    }

    printf("SUCCESS!\n");
    test_control_return(0);

#endif
}