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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/***************************************************************************
* 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 every application timer and system time service with event
tracing enabled.
Each service carries one in-line trace insert, and that insert behaves
differently according to the system state it runs in, whether its event is
filtered, whether it is the entry that wraps the trace buffer, and whether a
buffer-full notification is registered. The test walks the services once per
combination, so every service's insert is driven through all of them.
The timer and the time services sit in two different trace event classes, so
the filtered pass filters both.
One service is driven through its internal entry point in every pass rather
than only in the forced-state ones: tx_api.h remaps tx_time_get() to a direct
read of the system clock on SMP unless TX_MISRA_ENABLE is defined, so the
published macro reaches _tx_time_get in two configurations of the eight and
its insert cannot be driven from above in the other six.
The SMP insert reads the system state and the current thread of the core it
is running on rather than a single global, so the passes below that force a
system state write the entry of the core this test is running on, under the
SMP protection primitive. Every thread in this suite is created mapped to
core 0 alone, but the core is read rather than assumed.
Without event tracing compiled in the inserts expand to nothing, 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
/* The system state is per core on SMP, and tx_thread.h remaps the name to a
function call returning the running core's entry unless this macro says the
file wants the array itself. The forced-state passes below have to write it,
so they need the array. */
#define TX_THREAD_SMP_SOURCE_CODE
#include "tx_trace.h"
#include "tx_thread.h"
#include "tx_initialize.h"
/* Define the ISR dispatch. */
extern VOID (*test_isr_dispatch)(void);
/* Define the services, in the order the test drives them. The scratch timer is
created by the first and deleted by the last, and the resident timer is left
deactivated with a full tick count by the change that follows its
deactivation, so a pass over the whole list leaves the system as it found
it. */
#define TIMER_SERVICE_CREATE 0
#define TIMER_SERVICE_ACTIVATE 1
#define TIMER_SERVICE_INFO_GET 2
#define TIMER_SERVICE_DEACTIVATE 3
#define TIMER_SERVICE_CHANGE 4
#define TIMER_SERVICE_TIME_GET 5
#define TIMER_SERVICE_TIME_SET 6
#define TIMER_SERVICE_DELETE 7
#define TIMER_SERVICES 8
/* The services an ISR is allowed to call. Create is rejected by the error
checking shell from an interrupt, and delete from any caller that is not a
thread. */
#define TIMER_ISR_FIRST_SERVICE TIMER_SERVICE_ACTIVATE
#define TIMER_ISR_LAST_SERVICE TIMER_SERVICE_TIME_SET
/* The expiration the resident timer carries. It is long enough that a pass
cannot expire it, and the change service restores it after every
deactivation so that the next activation always has ticks to run. */
#define TIMER_TICKS 100
static unsigned long thread_0_counter = 0;
static TX_THREAD thread_0;
static TX_TIMER timer_0;
static TX_TIMER timer_1;
static unsigned long timer_0_counter = 0;
/* The system clock the time get service read, written back by the time set
service that follows it. Driving the set with a value the get has just
produced keeps the clock where the rest of the system expects it. */
static ULONG saved_time = 0;
static ULONG buffer_full_count = 0;
static ULONG isr_pass_done = 0;
static UINT isr_status = TX_SUCCESS;
/* The trace buffer is deliberately small. Several passes over the services fit
in it many times over, so the wrap path is reached by ordinary use as well as
by the positioned wrap below. */
static UCHAR trace_buffer[512];
#define TRACE_REGISTRY_ENTRIES 4
/* Define thread prototypes. */
static void thread_0_entry(ULONG thread_input);
static void timer_0_entry(ULONG timer_input);
/* Prototype for test control return. */
void test_control_return(UINT status);
/* The resident timer's expiration function. The timer is deactivated by every
pass long before it expires, so this only runs if a pass is interrupted for
longer than its expiration. */
static void timer_0_entry(ULONG timer_input)
{
timer_0_counter++;
}
/* Read the clock through the published macro and through the service, under one
SMP protection so that a tick between the two readings cannot make them
disagree. The macro is a direct read of the system clock unless
TX_MISRA_ENABLE is defined, so the two are the same value taken twice rather
than two calls into the same function. */
static UINT published_time_agrees(void)
{
TX_INTERRUPT_SAVE_AREA
ULONG published;
ULONG from_service;
TX_DISABLE
published = tx_time_get();
from_service = _tx_time_get();
TX_RESTORE
return((published == from_service) ? TX_TRUE : TX_FALSE);
}
/* Drive one service through its published entry point. */
static UINT timer_service(UINT service)
{
UINT status;
CHAR *name;
UINT active;
ULONG remaining_ticks;
ULONG reschedule_ticks;
TX_TIMER *next_timer;
switch (service)
{
case TIMER_SERVICE_CREATE:
status = tx_timer_create(&timer_1, "timer 1", timer_0_entry, 1,
TIMER_TICKS, TIMER_TICKS, TX_NO_ACTIVATE);
break;
case TIMER_SERVICE_ACTIVATE:
status = tx_timer_activate(&timer_0);
break;
case TIMER_SERVICE_INFO_GET:
status = tx_timer_info_get(&timer_0, &name, &active, &remaining_ticks,
&reschedule_ticks, &next_timer);
break;
case TIMER_SERVICE_DEACTIVATE:
status = tx_timer_deactivate(&timer_0);
break;
case TIMER_SERVICE_CHANGE:
status = tx_timer_change(&timer_0, TIMER_TICKS, TIMER_TICKS);
break;
case TIMER_SERVICE_TIME_GET:
/* The internal entry point, deliberately, and this is the one place in
the pattern where the published API is not what drives the insert.
tx_api.h remaps tx_time_get() to a direct read of the system clock on
SMP unless TX_MISRA_ENABLE is defined, so in six of the eight
configurations the published macro never enters _tx_time_get and the
insert inside it is unreachable from above. The remapping is checked
for itself below. */
saved_time = _tx_time_get();
status = TX_SUCCESS;
break;
case TIMER_SERVICE_TIME_SET:
tx_time_set(saved_time);
status = TX_SUCCESS;
break;
default:
status = tx_timer_delete(&timer_1);
break;
}
return(status);
}
#ifdef TX_ENABLE_EVENT_TRACE
/* Drive one service through its internal entry point. The error checking shell
rejects a create from an interrupt, a delete from any caller that is not a
thread, and a change from initialization, so the inserts inside those three
services cannot be reached from a forced system state through the published
entry points. The shell itself is covered by its own tests; what is under
test here is the insert behind it. */
static UINT timer_service_internal(UINT service)
{
UINT status;
CHAR *name;
UINT active;
ULONG remaining_ticks;
ULONG reschedule_ticks;
TX_TIMER *next_timer;
switch (service)
{
case TIMER_SERVICE_CREATE:
status = _tx_timer_create(&timer_1, "timer 1", timer_0_entry, 1,
TIMER_TICKS, TIMER_TICKS, TX_NO_ACTIVATE);
break;
case TIMER_SERVICE_ACTIVATE:
status = _tx_timer_activate(&timer_0);
break;
case TIMER_SERVICE_INFO_GET:
status = _tx_timer_info_get(&timer_0, &name, &active, &remaining_ticks,
&reschedule_ticks, &next_timer);
break;
case TIMER_SERVICE_DEACTIVATE:
status = _tx_timer_deactivate(&timer_0);
break;
case TIMER_SERVICE_CHANGE:
status = _tx_timer_change(&timer_0, TIMER_TICKS, TIMER_TICKS);
break;
case TIMER_SERVICE_TIME_GET:
saved_time = _tx_time_get();
status = TX_SUCCESS;
break;
case TIMER_SERVICE_TIME_SET:
_tx_time_set(saved_time);
status = TX_SUCCESS;
break;
default:
status = _tx_timer_delete(&timer_1);
break;
}
return(status);
}
/* Run a pass over the services with the system state forced to the value the
insert has to see. The SMP protection is held across the pass, so no other
core and no interrupt can observe the forced value, and the interrupt nesting
count the same array element carries is restored before anything else reads
it. The element is the running core's, read rather than assumed. */
static UINT pass_with_system_state(ULONG system_state)
{
TX_INTERRUPT_SAVE_AREA
UINT service;
UINT core;
UINT status = TX_SUCCESS;
ULONG saved_system_state;
TX_DISABLE
core = tx_thread_smp_core_get();
saved_system_state = _tx_thread_system_state[core];
_tx_thread_system_state[core] = system_state;
for (service = 0; service < TIMER_SERVICES; service++)
{
if (timer_service_internal(service) != TX_SUCCESS)
{
status = TX_NOT_DONE;
}
}
_tx_thread_system_state[core] = saved_system_state;
TX_RESTORE
return(status);
}
/* Position the trace buffer one entry short of its end, so that the next event
inserted is the one that wraps it. A wrap reached by filling the buffer lands
on whichever service happens to write the last entry; positioning puts each
service's own insert on the boundary in turn. No timer or time service writes
an event buffer entry ahead of its own insert -- the deactivation inside the
delete and the object registration inside the create write neither -- so one
entry is the right distance for every one of them. */
static void position_at_buffer_end(void)
{
_tx_trace_buffer_current_ptr = _tx_trace_buffer_end_ptr - 1;
_tx_trace_header_ptr -> tx_trace_header_buffer_current_pointer = (ULONG) _tx_trace_buffer_current_ptr;
}
/* Count the trace buffer wraps the kernel reports. */
static VOID trace_buffer_full(void *buffer)
{
buffer_full_count++;
}
/* Run a pass over the services, wrapping the trace buffer on every one of
them. The positioning and the call are held under one SMP protection, so the
boundary entry is the service's own by construction: every trace insert in
the kernel is written inside that same protection, so nothing else can take
the slot in between.
Once a notification is registered, every service in the pass has to report a
wrap of its own. Checking that per service rather than in total is what makes
the positioning prove itself: a service whose boundary entry was taken by
something else fails here instead of quietly losing coverage. */
static UINT pass_with_buffer_wrap(UINT check_wraps)
{
TX_INTERRUPT_SAVE_AREA
UINT service;
UINT status = TX_SUCCESS;
ULONG before;
for (service = 0; service < TIMER_SERVICES; service++)
{
before = buffer_full_count;
TX_DISABLE
position_at_buffer_end();
if (timer_service(service) != TX_SUCCESS)
{
status = TX_NOT_DONE;
}
TX_RESTORE
if ((check_wraps == TX_TRUE) && (buffer_full_count == before))
{
status = TX_NOT_DONE;
}
}
return(status);
}
/* Drive the services an ISR may call, once, from interrupt context. */
static void test_isr(void)
{
UINT service;
if (isr_pass_done == 0)
{
for (service = TIMER_ISR_FIRST_SERVICE; service <= TIMER_ISR_LAST_SERVICE; service++)
{
if (timer_service(service) != TX_SUCCESS)
{
isr_status = TX_NOT_DONE;
}
}
isr_pass_done = 1;
}
}
#endif
/* Define what the initial system looks like. */
#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void threadx_timer_trace_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 Timer Trace Test........................................... ERROR #1\n");
test_control_return(1);
}
/* Create the timer the activate, change, deactivate and information
services are driven against. It is left inactive, which is the state
every pass starts and finishes in. */
status = tx_timer_create(&timer_0, "timer 0", timer_0_entry, 0,
TIMER_TICKS, TIMER_TICKS, TX_NO_ACTIVATE);
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Timer Trace Test........................................... ERROR #2\n");
test_control_return(1);
}
}
/* Define the test thread. */
static void thread_0_entry(ULONG thread_input)
{
UINT status;
UINT service;
#ifdef TX_ENABLE_EVENT_TRACE
ULONG wait;
#endif
/* Increment thread 0 counter. */
thread_0_counter++;
/* Inform user of success getting to this test. */
printf("Running Timer Trace Test........................................... ");
/* The published tx_time_get() is a macro that reads the system clock
directly unless TX_MISRA_ENABLE is defined, so it agrees with the service
without entering it. Checking that here is what lets every pass below
drive the service itself. */
if (published_time_agrees() != TX_TRUE)
{
printf("ERROR #3\n");
test_control_return(1);
}
/* Enable event tracing. */
status = tx_trace_enable(trace_buffer, sizeof(trace_buffer), TRACE_REGISTRY_ENTRIES);
#ifndef TX_ENABLE_EVENT_TRACE
/* Without tracing compiled in the inserts expand to nothing, so there is
nothing here to drive. Check that the service says so and finish. */
if (status != TX_FEATURE_NOT_ENABLED)
{
printf("ERROR #4\n");
test_control_return(1);
}
/* One pass over the services, so the test still exercises them. */
for (service = 0; service < TIMER_SERVICES; service++)
{
if (timer_service(service) != TX_SUCCESS)
{
printf("ERROR #5\n");
test_control_return(1);
}
}
printf("SUCCESS!\n");
test_control_return(0);
#else
if (status != TX_SUCCESS)
{
printf("ERROR #6\n");
test_control_return(1);
}
/* Thread context, every event enabled: the insert runs and takes the
current thread's priority. */
for (service = 0; service < TIMER_SERVICES; service++)
{
if (timer_service(service) != TX_SUCCESS)
{
printf("ERROR #7\n");
test_control_return(1);
}
}
/* Timer and time events filtered out: the insert is reached and declines to
record anything. The two classes are distinct, so both are filtered. */
status = tx_trace_event_filter(TX_TRACE_TIMER_EVENTS | TX_TRACE_TIME_EVENTS);
if (status != TX_SUCCESS)
{
printf("ERROR #8\n");
test_control_return(1);
}
for (service = 0; service < TIMER_SERVICES; service++)
{
if (timer_service(service) != TX_SUCCESS)
{
printf("ERROR #9\n");
test_control_return(1);
}
}
status = tx_trace_event_unfilter(TX_TRACE_TIMER_EVENTS | TX_TRACE_TIME_EVENTS);
if (status != TX_SUCCESS)
{
printf("ERROR #10\n");
test_control_return(1);
}
/* Interrupt context: the insert records the interrupted thread instead of a
priority. Bounded, so a tick that never arrives fails the test rather than
hanging it. */
test_isr_dispatch = test_isr;
wait = 0;
while ((isr_pass_done == 0) && (wait < 100))
{
tx_thread_sleep(1);
wait++;
}
test_isr_dispatch = TX_NULL;
if (isr_pass_done == 0)
{
printf("ERROR #11\n");
test_control_return(1);
}
if (isr_status != TX_SUCCESS)
{
printf("ERROR #12\n");
test_control_return(1);
}
/* Initialization context: the insert has no thread to record at all. */
if (pass_with_system_state(TX_INITIALIZE_IN_PROGRESS) != TX_SUCCESS)
{
printf("ERROR #13\n");
test_control_return(1);
}
/* The same state an ISR presents, for the services an ISR may not call. */
if (pass_with_system_state((ULONG) 1) != TX_SUCCESS)
{
printf("ERROR #14\n");
test_control_return(1);
}
/* Wrap the trace buffer on every service with no notification registered,
then again with one. */
if (pass_with_buffer_wrap(TX_FALSE) != TX_SUCCESS)
{
printf("ERROR #15\n");
test_control_return(1);
}
status = tx_trace_buffer_full_notify(trace_buffer_full);
if (status != TX_SUCCESS)
{
printf("ERROR #16\n");
test_control_return(1);
}
if (pass_with_buffer_wrap(TX_TRUE) != TX_SUCCESS)
{
printf("ERROR #17\n");
test_control_return(1);
}
/* Every service in the last pass wrapped the buffer, so the notification
has to have been called once for each of them. */
if (buffer_full_count < TIMER_SERVICES)
{
printf("ERROR #18\n");
test_control_return(1);
}
status = tx_trace_buffer_full_notify(TX_NULL);
if (status != TX_SUCCESS)
{
printf("ERROR #19\n");
test_control_return(1);
}
status = tx_trace_disable();
if (status != TX_SUCCESS)
{
printf("ERROR #20\n");
test_control_return(1);
}
printf("SUCCESS!\n");
test_control_return(0);
#endif
}
|