1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 
28 /*
29  * Utility functions required to gather run time statistics.  See:
30  * https://www.FreeRTOS.org/rtos-run-time-stats.html
31  *
32  * Note that this is a simulated port, where simulated time is a lot slower than
33  * real time, therefore the run time counter values have no real meaningful
34  * units.
35  *
36  * Also note that it is assumed this demo is going to be used for short periods
37  * of time only, and therefore timer overflows are not handled.
38  */
39 
40 /* FreeRTOS includes. */
41 #include <FreeRTOS.h>
42 
43 /* Variables used in the creation of the run time stats time base.  Run time
44  * stats record how much time each task spends in the Running state. */
45 static long long llInitialRunTimeCounterValue = 0LL, llTicksPerHundredthMillisecond = 0LL;
46 
47 /*-----------------------------------------------------------*/
48 
vConfigureTimerForRunTimeStats(void)49 void vConfigureTimerForRunTimeStats( void )
50 {
51     LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;
52 
53     /* Initialise the variables used to create the run time stats time base.
54      * Run time stats record how much time each task spends in the Running
55      * state. */
56 
57     if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )
58     {
59         llTicksPerHundredthMillisecond = 1;
60     }
61     else
62     {
63         /* How many times does the performance counter increment in 1/100th
64          * millisecond. */
65         llTicksPerHundredthMillisecond = liPerformanceCounterFrequency.QuadPart / 100000LL;
66 
67         /* What is the performance counter value now, this will be subtracted
68          * from readings taken at run time. */
69         QueryPerformanceCounter( &liInitialRunTimeValue );
70         llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;
71     }
72 }
73 /*-----------------------------------------------------------*/
74 
ulGetRunTimeCounterValue(void)75 unsigned long ulGetRunTimeCounterValue( void )
76 {
77     LARGE_INTEGER liCurrentCount;
78     unsigned long ulReturn;
79 
80     /* What is the performance counter value now? */
81     QueryPerformanceCounter( &liCurrentCount );
82 
83     /* Subtract the performance counter value reading taken when the
84      * application started to get a count from that reference point, then
85      * scale to (simulated) 1/100ths of a millisecond. */
86     if( llTicksPerHundredthMillisecond == 0 )
87     {
88         /* The trace macros can call this function before the kernel has been
89          * started, in which case llTicksPerHundredthMillisecond will not have been
90          * initialised. */
91         ulReturn = 0;
92     }
93     else
94     {
95         ulReturn = ( unsigned long )
96                    ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) /
97                      llTicksPerHundredthMillisecond );
98     }
99 
100     return ulReturn;
101 }
102 /*-----------------------------------------------------------*/
103