summaryrefslogtreecommitdiff
path: root/utility/execution_profile_kit/smp_version/tx_execution_profile.c
blob: 735dbf147df0dc11273cd3c4fa2a8c5a644f57fd (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
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
/***************************************************************************
 * 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
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** ThreadX Component                                                     */
/**                                                                       */
/**   Execution Profile Kit                                               */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define TX_SOURCE_CODE


/* Include necessary system files.  */

#include "tx_api.h"
#include "tx_execution_profile.h"

/* The thread execution profile kit is designed to track thread execution time
   based on the hardware timer defined by TX_EXECUTION_TIME_SOURCE and
   TX_EXECUTION_MAX_TIME_SOURCE below. When the thread's total time reaches
   the maximum value, it remains there until the time is reset to 0 via a call
   to tx_thread_execution_time_reset. There are several assumptions to the
   operation of this kit, as follows:

   1. In tx_port.h replace:

        #define TX_THREAD_EXTENSION_3"

      with:

        #define TX_THREAD_EXTENSION_3           unsigned long long  tx_thread_execution_time_total; \
                                                unsigned long       tx_thread_execution_time_last_start;

        Note: if 64-bit time source is present, the tx_thread_execution_time_last_start type should be unsigned long long.

   2. The TX_EXECUTION_TIME_SOURCE and TX_EXECUTION_MAX_TIME_SOURCE macros are
      defined to utilize a local hardware time source.

   3. ThreadX 5.4 (or later) is being used, with the assembly code enabled to
      call the following routines from assembly code:

            VOID  _tx_execution_thread_enter(void);
            VOID  _tx_execution_thread_exit(void);
            VOID  _tx_execution_isr_enter(void);
            VOID  _tx_execution_isr_exit(void);

    4. The ThreadX library assembly code must be rebuilt with TX_ENABLE_EXECUTION_CHANGE_NOTIFY so
       that these macros are expanded in the TX_THREAD structure and so the assembly code macros
       are enabled to call the execution profile routines.

    5. Add tx_execution_profile.c to the application build.  */


/* Externally reference several internal ThreadX variables.  */

extern ULONG                            _tx_thread_system_state[TX_THREAD_SMP_MAX_CORES];
extern UINT                             _tx_thread_preempt_disable;
extern TX_THREAD                        *_tx_thread_current_ptr[TX_THREAD_SMP_MAX_CORES];
extern TX_THREAD                        *_tx_thread_execute_ptr[TX_THREAD_SMP_MAX_CORES];
extern TX_THREAD                        *_tx_thread_created_ptr;
extern ULONG                            _tx_thread_created_count;


/* Define the total time for all threads.  This is accumulated as each thread's total time is accumulated.  */

EXECUTION_TIME                      _tx_execution_thread_time_total[TX_THREAD_SMP_MAX_CORES];


/* Define the ISR time gathering information. This is setup to track total ISR time presently, but
   could easily be expanded to track different ISRs. Also, only ISRs that utilize _tx_thread_context_save
   and _tx_thread_context_restore are tracked by this utility.  */

EXECUTION_TIME                      _tx_execution_isr_time_total[TX_THREAD_SMP_MAX_CORES];
EXECUTION_TIME_SOURCE_TYPE          _tx_execution_isr_time_last_start[TX_THREAD_SMP_MAX_CORES];


/* Define the system idle time gathering information. For idle time that exceeds the range of the timer
   source, another timer source may be needed. In addition, the total thread execution time added to the
   total ISR time, less the total system time is also a measure of idle time.  */

EXECUTION_TIME                      _tx_execution_idle_time_total[TX_THREAD_SMP_MAX_CORES];
EXECUTION_TIME_SOURCE_TYPE          _tx_execution_idle_time_last_start[TX_THREAD_SMP_MAX_CORES];


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_thread_enter                         PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is called whenever thread execution starts.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _tx_thread_schedule               Thread scheduling                 */
/*                                                                        */
/**************************************************************************/
VOID  _tx_execution_thread_enter(void)
{

TX_THREAD                   *thread_ptr;
EXECUTION_TIME_SOURCE_TYPE  last_start_time;
EXECUTION_TIME_SOURCE_TYPE  current_time;
EXECUTION_TIME              delta_time;
EXECUTION_TIME              total_time;
EXECUTION_TIME              new_total_time;
UINT                        core;


    /* Pickup the current time.  */
    current_time =  TX_EXECUTION_TIME_SOURCE;

    /* Pickup the core index.  */
    core =  TX_SMP_CORE_ID;

    /* Pickup the current thread control block.  */
    thread_ptr =  _tx_thread_current_ptr[core];

    /* This thread is being scheduled.  Simply setup the last start time in the
       thread control block.  */
    thread_ptr -> tx_thread_execution_time_last_start =  current_time;

    /* Pickup the last idle start time.  */
    last_start_time =  _tx_execution_idle_time_last_start[core];

    /* Determine if idle time is being measured.  */
    if (last_start_time)
    {

        /* Determine how to calculate the difference.  */
        if (current_time >= last_start_time)
        {

            /* Simply subtract.  */
            delta_time =  (EXECUTION_TIME) (current_time - last_start_time);
        }
        else
        {

            /* Timer wrapped, compute the delta assuming incrementing time counter.  */
            delta_time =  (EXECUTION_TIME) (current_time + (((EXECUTION_TIME_SOURCE_TYPE) TX_EXECUTION_MAX_TIME_SOURCE) - last_start_time));
        }

        /* Pickup the total time.  */
        total_time =  _tx_execution_idle_time_total[core];

        /* Now compute the new total time.  */
        new_total_time =  total_time + delta_time;

        /* Determine if a rollover on the total time is present.  */
        if (new_total_time < total_time)
        {

            /* Rollover. Set the total time to max value.  */
            new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
        }

        /* Now store back the total idle time.  */
        _tx_execution_idle_time_total[core] =  new_total_time;

        /* Disable the idle time measurement.  */
        _tx_execution_idle_time_last_start[core] =  0;
    }
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_thread_exit                          PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is called whenever a thread execution ends.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _tx_thread_system_return          Thread exiting                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
VOID  _tx_execution_thread_exit(void)
{

TX_THREAD                   *thread_ptr;
EXECUTION_TIME              total_time;
EXECUTION_TIME              new_total_time;
EXECUTION_TIME_SOURCE_TYPE  last_start_time;
EXECUTION_TIME_SOURCE_TYPE  current_time;
EXECUTION_TIME              delta_time;
UINT                        core;


    /* Pickup the core index.  */
    core =  TX_SMP_CORE_ID;

    /* Pickup the current thread control block.  */
    thread_ptr =  _tx_thread_current_ptr[core];

    /* Determine if there is a thread.  */
    if (thread_ptr)
    {

        /* Pickup the current time.  */
        current_time =  TX_EXECUTION_TIME_SOURCE;

        /* Pickup the last start time.  */
        last_start_time =  thread_ptr -> tx_thread_execution_time_last_start;

        /* Determine if there is an actual start time.  */
        if (last_start_time)
        {

            /* Clear the last start time.  */
            thread_ptr -> tx_thread_execution_time_last_start =  0;

            /* Determine how to calculate the difference.  */
            if (current_time >= last_start_time)
            {

                /* Simply subtract.  */
                delta_time =  (EXECUTION_TIME) (current_time - last_start_time);
            }
            else
            {

                /* Timer wrapped, compute the delta assuming incrementing time counter.  */
                delta_time =  (EXECUTION_TIME) (current_time + (((EXECUTION_TIME_SOURCE_TYPE) TX_EXECUTION_MAX_TIME_SOURCE) - last_start_time));
            }

            /* Pickup the total time.  */
            total_time =  thread_ptr -> tx_thread_execution_time_total;

            /* Now compute the new total time.  */
            new_total_time =  total_time + delta_time;

            /* Determine if a rollover on the total time is present.  */
            if (new_total_time < total_time)
            {

                /* Rollover. Set the total time to max value.  */
                new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
            }

            /* Store back the new total time.  */
            thread_ptr -> tx_thread_execution_time_total =  new_total_time;

            /* Now accumulate this thread's execution time into the total thread execution time.   */
            new_total_time =  _tx_execution_thread_time_total[core] + delta_time;

            /* Determine if a rollover on the total time is present.  */
            if (new_total_time < _tx_execution_thread_time_total[core])
            {

                /* Rollover. Set the total time to max value.  */
                new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
            }

            /* Store back the new total time.  */
            _tx_execution_thread_time_total[core] =  new_total_time;
        }

        /* Is the system now idle?  */
        if (_tx_thread_execute_ptr[core] == TX_NULL)
        {

            /* Yes, idle system. Pickup the start of idle time.  */
            _tx_execution_idle_time_last_start[core] =  TX_EXECUTION_TIME_SOURCE;
        }
    }
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_isr_enter                            PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is called whenever ISR processing starts.             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _tx_thread_context_save           ISR context save                  */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
VOID  _tx_execution_isr_enter(void)
{

TX_THREAD                   *thread_ptr;
EXECUTION_TIME_SOURCE_TYPE  current_time;
EXECUTION_TIME              total_time;
EXECUTION_TIME              new_total_time;
EXECUTION_TIME_SOURCE_TYPE  last_start_time;
EXECUTION_TIME              delta_time;
UINT                        core;


    /* Pickup the core index.  */
    core =  TX_SMP_CORE_ID;

    /* Determine if this is the first interrupt. Nested interrupts are all treated as
       general interrupt processing.  */
    if (_tx_thread_system_state[core] == 1)
    {
        /* Pickup the current time.  */
        current_time =  TX_EXECUTION_TIME_SOURCE;

        /* Pickup the current thread control block.  */
        thread_ptr =  _tx_thread_current_ptr[core];

        /* Determine if a thread was interrupted.  */
        if (thread_ptr)
        {

            /* Pickup the last start time.  */
            last_start_time =  thread_ptr -> tx_thread_execution_time_last_start;

            /* Determine if there is an actual start time.  */
            if (last_start_time)
            {

                /* Clear the last start time.  */
                thread_ptr -> tx_thread_execution_time_last_start =  0;

                /* Determine how to calculate the difference.  */
                if (current_time >= last_start_time)
                {

                    /* Simply subtract.  */
                    delta_time =  (EXECUTION_TIME) (current_time - last_start_time);
                }
                else
                {

                    /* Timer wrapped, compute the delta assuming incrementing time counter.  */
                    delta_time =  (EXECUTION_TIME) (current_time + (((EXECUTION_TIME_SOURCE_TYPE) TX_EXECUTION_MAX_TIME_SOURCE) - last_start_time));
                }

                /* Pickup the total time.  */
                total_time =  thread_ptr -> tx_thread_execution_time_total;

                /* Now compute the new total time.  */
                new_total_time =  total_time + delta_time;

                /* Determine if a rollover on the total time is present.  */
                if (new_total_time < total_time)
                {

                    /* Rollover. Set the total time to max value.  */
                    new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
                }

                /* Store back the new total time.  */
                thread_ptr -> tx_thread_execution_time_total =  new_total_time;

                /* Now accumulate this thread's execution time into the total thread execution time.   */
                new_total_time =  _tx_execution_thread_time_total[core] + delta_time;

                /* Determine if a rollover on the total time is present.  */
                if (new_total_time < _tx_execution_thread_time_total[core])
                {

                    /* Rollover. Set the total time to max value.  */
                    new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
                }

                /* Store back the new total time.  */
                _tx_execution_thread_time_total[core] =  new_total_time;
            }
        }

        /* Has idle time started?  */
        else if (_tx_execution_idle_time_last_start[core])
        {

            /* Pickup the last idle start time.  */
            last_start_time =  _tx_execution_idle_time_last_start[core];

            /* Determine how to calculate the difference.  */
            if (current_time >= last_start_time)
            {

                /* Simply subtract.  */
                delta_time =  (EXECUTION_TIME) (current_time - last_start_time);
            }
            else
            {

                /* Timer wrapped, compute the delta assuming incrementing time counter.  */
                delta_time =  (EXECUTION_TIME) (current_time + (((EXECUTION_TIME_SOURCE_TYPE) TX_EXECUTION_MAX_TIME_SOURCE) - last_start_time));
            }

            /* Pickup the total time.  */
            total_time =  _tx_execution_idle_time_total[core];

            /* Now compute the new total time.  */
            new_total_time =  total_time + delta_time;

            /* Determine if a rollover on the total time is present.  */
            if (new_total_time < total_time)
            {

                /* Rollover. Set the total time to max value.  */
                new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
            }

            /* Now store back the total idle time.  */
            _tx_execution_idle_time_total[core] =  new_total_time;

            /* Disable the idle time measurement.  */
            _tx_execution_idle_time_last_start[core] =  0;
        }

        /* Save the ISR start time.  */
        _tx_execution_isr_time_last_start[core] =  current_time;
    }
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_isr_exit                             PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is called whenever ISR processing ends.               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _tx_thread_context_restore        Thread de-scheduling              */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
VOID  _tx_execution_isr_exit(void)
{

TX_THREAD                   *thread_ptr;
EXECUTION_TIME              total_time;
EXECUTION_TIME              new_total_time;
EXECUTION_TIME_SOURCE_TYPE  last_start_time;
EXECUTION_TIME_SOURCE_TYPE  current_time;
EXECUTION_TIME              delta_time;
UINT                        core;


    /* Pickup the core index.  */
    core =  TX_SMP_CORE_ID;

    /* Determine if this is the first interrupt. Nested interrupts are all treated as
       general interrupt processing.  */
    if (_tx_thread_system_state[core] == 1)
    {

        /* Pickup the current time.  */
        current_time =  TX_EXECUTION_TIME_SOURCE;

        /* Pickup the last start time.  */
        last_start_time =  _tx_execution_isr_time_last_start[core];

        /* Determine how to calculate the difference.  */
        if (current_time >= last_start_time)
        {

           /* Simply subtract.  */
           delta_time =  (EXECUTION_TIME) (current_time - last_start_time);
        }
        else
        {

            /* Timer wrapped, compute the delta assuming incrementing time counter.  */
            delta_time =  (EXECUTION_TIME) (current_time + (((EXECUTION_TIME_SOURCE_TYPE) TX_EXECUTION_MAX_TIME_SOURCE) - last_start_time));
        }

        /* Pickup the total time.  */
        total_time =  _tx_execution_isr_time_total[core];

        /* Now compute the new total time.  */
        new_total_time =  total_time + delta_time;

        /* Determine if a rollover on the total time is present.  */
        if (new_total_time < total_time)
        {

            /* Rollover. Set the total time to max value.  */
            new_total_time =  (EXECUTION_TIME) TX_EXECUTION_MAX_TIME_SOURCE;
        }

        /* Store back the new total time.  */
        _tx_execution_isr_time_total[core] =  new_total_time;

        /* Pickup the current thread control block.  */
        thread_ptr =  _tx_thread_current_ptr[core];

        /* Was a thread interrupted?  */
        if (thread_ptr)
        {

            /* Now determine if the thread will execution is going to occur immediately.  */
            if ((thread_ptr == _tx_thread_execute_ptr[core]) || (_tx_thread_preempt_disable))
            {

                /* Yes, setup the thread last start time in the thread control block.  */
                thread_ptr -> tx_thread_execution_time_last_start =  current_time;
            }
        }

        /* Determine if the system is now idle.  */
        if (_tx_thread_execute_ptr[core] == TX_NULL)
        {

            /* Yes, idle system. Pickup the start of idle time.  */
            _tx_execution_idle_time_last_start[core] =  TX_EXECUTION_TIME_SOURCE;
        }
    }
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_thread_time_reset                    PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function resets the execution time of the specified thread.    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    thread_ptr                        Pointer to thread                 */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_thread_time_reset(TX_THREAD *thread_ptr)
{

    /* Reset the total time to 0.  */
    thread_ptr -> tx_thread_execution_time_total =  0;

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_thread_total_time_reset              PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function resets the total thread execution time.               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_thread_total_time_reset(void)
{

TX_INTERRUPT_SAVE_AREA

TX_THREAD       *thread_ptr;
UINT            total_threads;
UINT            core;


    /* Disable interrupts.  */
    TX_DISABLE

    /* Reset the total time for all cores.  */
    for (core = 0; core < TX_THREAD_SMP_MAX_CORES; core++)
    {

        _tx_execution_thread_time_total[core] =  0;
    }

    /* Loop through threads to clear their accumulated time.  */
    total_threads =      _tx_thread_created_count;
    thread_ptr =         _tx_thread_created_ptr;
    while (total_threads--)
    {
        thread_ptr -> tx_thread_execution_time_total =  0;
        thread_ptr =  thread_ptr -> tx_thread_created_next;
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_isr_time_reset                       PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function resets the execution time of the ISR calculation.     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_isr_time_reset(void)
{

TX_INTERRUPT_SAVE_AREA

UINT            core;

    /* Disable interrupts.  */
    TX_DISABLE

    /* Reset the ISR total time to 0.  */
    for (core = 0; core < TX_THREAD_SMP_MAX_CORES; core++)
    {

        _tx_execution_isr_time_total[core] =  0;
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_idle_time_reset                      PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function resets the idle execution time calculation.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_idle_time_reset(void)
{

TX_INTERRUPT_SAVE_AREA

UINT            core;

    /* Disable interrupts.  */
    TX_DISABLE

    /* Reset the idle total time to 0.  */
    for (core = 0; core < TX_THREAD_SMP_MAX_CORES; core++)
    {

        _tx_execution_idle_time_total[core] =  0;
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_thread_time_get                      PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the execution time of the specified thread.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    thread_ptr                        Pointer to the thread             */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_thread_time_get(TX_THREAD *thread_ptr, EXECUTION_TIME *total_time)
{

    /* Return the total time.  */
    *total_time =  thread_ptr -> tx_thread_execution_time_total;

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_thread_total_time_get                PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the execution time of all threads.               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_thread_total_time_get(EXECUTION_TIME *total_time)
{

TX_INTERRUPT_SAVE_AREA

UINT            core;

    /* Disable interrupts.  */
    TX_DISABLE

    /* Return the total thread time.  */
    for (core = 0; core < TX_THREAD_SMP_MAX_CORES; core++)
    {

        *total_time =  _tx_execution_thread_time_total[core];
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_isr_time_get                         PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the execution time of all ISRs.                  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_isr_time_get(EXECUTION_TIME *total_time)
{

TX_INTERRUPT_SAVE_AREA

UINT            core;

    /* Disable interrupts.  */
    TX_DISABLE

    /* Return the total ISR time.  */
    for (core = 0; core < TX_THREAD_SMP_MAX_CORES; core++)
    {

        /* Return the total time.  */
        *total_time =  _tx_execution_isr_time_total[core];
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_idle_time_get                        PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the total system idle time.                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_idle_time_get(EXECUTION_TIME *total_time)
{

TX_INTERRUPT_SAVE_AREA

UINT            core;

    /* Disable interrupts.  */
    TX_DISABLE

    /* Return the total time.  */
    for (core = 0; core < TX_THREAD_SMP_MAX_CORES; core++)
    {

        /* Return the total time.  */
        *total_time =  _tx_execution_idle_time_total[core];
    }

    /* Restore interrupts.  */
    TX_RESTORE

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_core_thread_total_time_get           PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the total thread execution time of the           */
/*    specified core.                                                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    core                              Specified core                    */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_core_thread_total_time_get(UINT core, EXECUTION_TIME *total_time)
{

    /* Determine if the core is valid.  */
    if (core >= TX_THREAD_SMP_MAX_CORES)
    {

        /* Invalid core, return an error.  */
        return(TX_NOT_DONE);
    }

    /* Return the total thread time for the core.  */
    *total_time =  _tx_execution_thread_time_total[core];

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_core_isr_time_get                    PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the execution time of ISRs on the specified      */
/*    core.                                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    core                              Specified core                    */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_core_isr_time_get(UINT core, EXECUTION_TIME *total_time)
{

    /* Determine if the core is valid.  */
    if (core >= TX_THREAD_SMP_MAX_CORES)
    {

        /* Invalid core, return an error.  */
        return(TX_NOT_DONE);
    }

    /* Return the total ISR time for this core.  */
    *total_time =  _tx_execution_isr_time_total[core];

    /* Return success.  */
    return(TX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _tx_execution_core_idle_time_get                   PORTABLE SMP     */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    William E. Lamie, Microsoft Corporation                             */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function gets the idle time of the specified core.             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    core                              Specified core                    */
/*    total_time                        Destination for total time        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                            Completion status                 */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  07-29-2022      William E. Lamie        Initial Version 6.1.12        */
/*                                                                        */
/**************************************************************************/
UINT  _tx_execution_core_idle_time_get(UINT core, EXECUTION_TIME *total_time)
{

    /* Determine if the core is valid.  */
    if (core >= TX_THREAD_SMP_MAX_CORES)
    {

        /* Invalid core, return an error.  */
        return(TX_NOT_DONE);
    }

    /* Return the total idle time for this core.  */
    *total_time =  _tx_execution_idle_time_total[core];

    /* Return success.  */
    return(TX_SUCCESS);
}