summaryrefslogtreecommitdiff
path: root/utility/benchmarks/thread_metric/tm_interrupt_processing_test.c
blob: 69d156d99777a50fb5d58fba643a52d84e33d266 (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
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present 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.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** Thread-Metric Component                                               */
/**                                                                       */
/**   Interrupt Processing Test                                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    tm_interrupt_processing_test                        PORTABLE C      */
/*                                                           6.1.7        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This file defines the No-preemption interrupt processing test.      */
/*                                                                        */
/**************************************************************************/
#include "tm_api.h"


/* Define the counters used in the demo application...  */

unsigned long   tm_interrupt_thread_0_counter;
unsigned long   tm_interrupt_handler_counter;


/* Define the test thread prototypes.  */

void            tm_interrupt_thread_0_entry(void);
void            tm_interrupt_handler_entry(void);


/* Define the reporting thread prototype.  */

void            tm_interrupt_thread_report(void);


/* Define the interrupt handler.  This must be called from the RTOS.  */

void            tm_interrupt_handler(void);


/* Define the initialization prototype.  */

void            tm_interrupt_processing_initialize(void);


/* Define main entry point.  */

void tm_main()
{

    /* Initialize the test.  */
    tm_initialize(tm_interrupt_processing_initialize);
}


/* Define the interrupt processing test initialization.  */

void  tm_interrupt_processing_initialize(void)
{

    /* Create thread that generates the interrupt at priority 10.  */
    tm_thread_create(0, 10, tm_interrupt_thread_0_entry);

    /* Create a semaphore that will be posted from the interrupt
       handler.  */
    tm_semaphore_create(0);

    /* Resume just thread 0.  */
    tm_thread_resume(0);

    /* Create the reporting thread. It will preempt the other
       threads and print out the test results.  */
    tm_thread_create(5, 2, tm_interrupt_thread_report);
    tm_thread_resume(5);
}


/* Define the thread that generates the interrupt.  */
void  tm_interrupt_thread_0_entry(void)
{

int status;


    /* Pickup the semaphore since it is initialized to 1 by default. */
    status = tm_semaphore_get(0);

    /* Check for good status.  */
    if (status != TM_SUCCESS)
        return;

    while(1)
    {

        /* Force an interrupt. The underlying RTOS must see that the
           the interrupt handler is called from the appropriate software
           interrupt or trap. */
       TM_CAUSE_INTERRUPT

        /* We won't get back here until the interrupt processing is complete,
           including the setting of the semaphore from the interrupt
           handler.  */

        /* Pickup the semaphore set by the interrupt handler. */
        status = tm_semaphore_get(0);

        /* Check for good status.  */
        if (status != TM_SUCCESS)
            return;

        /* Increment this thread's counter.  */
        tm_interrupt_thread_0_counter++;
    }
}


/* Define the interrupt handler.  This must be called from the RTOS trap handler.
   To be fair, it must behave just like a processor interrupt, i.e. it must save
   the full context of the interrupted thread during the preemption processing. */
void  tm_interrupt_handler(void)
{

    /* Increment the interrupt count.  */
    tm_interrupt_handler_counter++;

    /* Put the semaphore from the interrupt handler.  */
    tm_semaphore_put(0);
}


/* Define the interrupt test reporting thread.  */
void  tm_interrupt_thread_report(void)
{

unsigned long   total;
unsigned long   last_total;
unsigned long   relative_time;
unsigned long   average;


    /* Initialize the last total.  */
    last_total =  0;

    /* Initialize the relative time.  */
    relative_time =  0;

    while(1)
    {

        /* Sleep to allow the test to run.  */
        tm_thread_sleep(TM_TEST_DURATION);

        /* Increment the relative time.  */
        relative_time =  relative_time + TM_TEST_DURATION;

        /* Print results to the stdio window.  */
        printf("**** Thread-Metric Interrupt Processing Test **** Relative Time: %lu\n", relative_time);

        /* Calculate the total of all the counters.  */
        total =  tm_interrupt_thread_0_counter + tm_interrupt_handler_counter;

        /* Calculate the average of all the counters.  */
        average =  total/2;

        /* See if there are any errors.  */
        if ((tm_interrupt_thread_0_counter < (average - 1)) ||
            (tm_interrupt_thread_0_counter > (average + 1)) ||
            (tm_interrupt_handler_counter < (average - 1)) ||
            (tm_interrupt_handler_counter > (average + 1)))
        {

            printf("ERROR: Invalid counter value(s). Interrupt processing test has failed!\n");
        }

        /* Show the total interrupts for the time period.  */
        printf("Time Period Total:  %lu\n\n", tm_interrupt_handler_counter - last_total);

        /* Save the last total number of interrupts.  */
        last_total =  tm_interrupt_handler_counter;
    }
}