]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Library/EdkPeiPerformanceLib/PeiPerformanceLib.c
1. UINTN & INTN issue for EBC architecture:
[mirror_edk2.git] / EdkModulePkg / Library / EdkPeiPerformanceLib / PeiPerformanceLib.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 PeiPerformanceLib.c
15
16 Abstract:
17
18 Performance Library
19
20 --*/
21
22 /**
23 Gets PEI the GUID HOB for PEI performance.
24
25 This internal function searches for the GUID HOB for PEI performance.
26 If that GUID HOB is not found, it will build a new one.
27 It returns the data area of that GUID HOB to record performance log.
28
29 @param Handle Pointer to environment specific context used
30 to identify the component being measured.
31 @param Token Pointer to a Null-terminated ASCII string
32 that identifies the component being measured.
33 @param Module Pointer to a Null-terminated ASCII string
34 that identifies the module being measured.
35
36 @retval The index of log entry in the array.
37
38 **/
39 PEI_PERFORMANCE_LOG_HEADER *
40 InternalGetPerformanceHobLog (
41 VOID
42 )
43 {
44 EFI_HOB_GUID_TYPE *GuidHob;
45 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
46 UINTN PeiPerformanceLogSize;
47
48 GuidHob = GetFirstGuidHob (&gPeiPerformanceHobGuid);
49
50 if (GuidHob != NULL) {
51 //
52 // PEI Performance HOB was found, then return the existing one.
53 //
54 PeiPerformanceLog = GET_GUID_HOB_DATA (GuidHob);
55 } else {
56 //
57 // PEI Performance HOB was not found, then build one.
58 //
59 PeiPerformanceLogSize = sizeof (PEI_PERFORMANCE_LOG_HEADER) +
60 sizeof (PEI_PERFORMANCE_LOG_ENTRY) * MAX_PEI_PERFORMANCE_LOG_ENTRIES;
61 PeiPerformanceLog = BuildGuidHob (&gPeiPerformanceHobGuid, PeiPerformanceLogSize);
62 PeiPerformanceLog = ZeroMem (PeiPerformanceLog, PeiPerformanceLogSize);
63 }
64
65 return PeiPerformanceLog;
66 }
67
68 /**
69 Searches in the log array with keyword Handle, Token and Module.
70
71 This internal function searches for the log entry in the log array.
72 If there is an entry that exactly matches the given key word triple
73 and its end time stamp is zero, then the index of that log entry is returned;
74 otherwise, the the number of log entries in the array is returned.
75
76 @param Handle Pointer to environment specific context used
77 to identify the component being measured.
78 @param Token Pointer to a Null-terminated ASCII string
79 that identifies the component being measured.
80 @param Module Pointer to a Null-terminated ASCII string
81 that identifies the module being measured.
82
83 @retval The index of log entry in the array.
84
85 **/
86 UINT32
87 InternalSearchForLogEntry (
88 IN PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog,
89 IN CONST VOID *Handle, OPTIONAL
90 IN CONST CHAR8 *Token, OPTIONAL
91 IN CONST CHAR8 *Module OPTIONAL
92 )
93 {
94 UINT32 Index;
95 UINT32 NumberOfEntries;
96 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
97
98
99 if (Token == NULL) {
100 Token = "";
101 }
102 if (Module == NULL) {
103 Module = "";
104 }
105 NumberOfEntries = PeiPerformanceLog->NumberOfEntries;
106 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
107
108 for (Index = 0; Index < NumberOfEntries; Index++) {
109 if ((LogEntryArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&
110 AsciiStrnCmp (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
111 AsciiStrnCmp (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
112 LogEntryArray[Index].EndTimeStamp == 0
113 ) {
114 break;
115 }
116 }
117 return Index;
118 }
119
120 /**
121 Creates a record for the beginning of a performance measurement.
122
123 Creates a record that contains the Handle, Token, and Module.
124 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
125 If TimeStamp is zero, then this function reads the current time stamp
126 and adds that time stamp value to the record as the start time.
127
128 @param Handle Pointer to environment specific context used
129 to identify the component being measured.
130 @param Token Pointer to a Null-terminated ASCII string
131 that identifies the component being measured.
132 @param Module Pointer to a Null-terminated ASCII string
133 that identifies the module being measured.
134 @param TimeStamp 64-bit time stamp.
135
136 @retval RETURN_SUCCESS The start of the measurement was recorded.
137 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
138
139 **/
140 RETURN_STATUS
141 EFIAPI
142 StartPerformanceMeasurement (
143 IN CONST VOID *Handle, OPTIONAL
144 IN CONST CHAR8 *Token, OPTIONAL
145 IN CONST CHAR8 *Module, OPTIONAL
146 IN UINT64 TimeStamp
147 )
148 {
149 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
150 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
151 UINT32 Index;
152
153 PeiPerformanceLog = InternalGetPerformanceHobLog ();
154
155 if (PeiPerformanceLog->NumberOfEntries >= MAX_PEI_PERFORMANCE_LOG_ENTRIES) {
156 return RETURN_OUT_OF_RESOURCES;
157 }
158 Index = PeiPerformanceLog->NumberOfEntries++;
159 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
160 LogEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
161
162 if (Token != NULL) {
163 AsciiStrnCpy (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH);
164 }
165 if (Module != NULL) {
166 AsciiStrnCpy (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH);
167 }
168
169 if (TimeStamp == 0) {
170 TimeStamp = GetPerformanceCounter ();
171 }
172 LogEntryArray[Index].StartTimeStamp = TimeStamp;
173
174 return RETURN_SUCCESS;
175 }
176
177 /**
178 Fills in the end time of a performance measurement.
179
180 Looks up the record that matches Handle, Token, and Module.
181 If the record can not be found then return RETURN_NOT_FOUND.
182 If the record is found and TimeStamp is not zero,
183 then TimeStamp is added to the record as the end time.
184 If the record is found and TimeStamp is zero, then this function reads
185 the current time stamp and adds that time stamp value to the record as the end time.
186 If this function is called multiple times for the same record, then the end time is overwritten.
187
188 @param Handle Pointer to environment specific context used
189 to identify the component being measured.
190 @param Token Pointer to a Null-terminated ASCII string
191 that identifies the component being measured.
192 @param Module Pointer to a Null-terminated ASCII string
193 that identifies the module being measured.
194 @param TimeStamp 64-bit time stamp.
195
196 @retval RETURN_SUCCESS The end of the measurement was recorded.
197 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
198
199 **/
200 RETURN_STATUS
201 EFIAPI
202 EndPerformanceMeasurement (
203 IN CONST VOID *Handle, OPTIONAL
204 IN CONST CHAR8 *Token, OPTIONAL
205 IN CONST CHAR8 *Module, OPTIONAL
206 IN UINT64 TimeStamp
207 )
208 {
209 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
210 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
211 UINT32 Index;
212
213 if (TimeStamp == 0) {
214 TimeStamp = GetPerformanceCounter ();
215 }
216
217 PeiPerformanceLog = InternalGetPerformanceHobLog ();
218 Index = InternalSearchForLogEntry (PeiPerformanceLog, Handle, Token, Module);
219 if (Index >= PeiPerformanceLog->NumberOfEntries) {
220 return RETURN_NOT_FOUND;
221 }
222 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
223 LogEntryArray[Index].EndTimeStamp = TimeStamp;
224
225 return RETURN_SUCCESS;
226 }
227
228 /**
229 Retrieves a previously logged performance measurement.
230
231 Looks up the record that matches Handle, Token, and Module.
232 If the record can not be found then return RETURN_NOT_FOUND.
233 If the record is found then the start of the measurement is returned in StartTimeStamp,
234 and the end of the measurement is returned in EndTimeStamp.
235
236 @param LogEntryKey The key for the previous performance measurement log entry.
237 If 0, then the first performance measurement log entry is retrieved.
238 @param Handle Pointer to environment specific context used
239 to identify the component being measured.
240 @param Token Pointer to a Null-terminated ASCII string
241 that identifies the component being measured.
242 @param Module Pointer to a Null-terminated ASCII string
243 that identifies the module being measured.
244 @param StartTimeStamp The 64-bit time stamp that was recorded when the measurement was started.
245 @param EndTimeStamp The 64-bit time stamp that was recorded when the measurement was ended.
246
247 @return The key for the current performance log entry.
248
249 **/
250 UINTN
251 EFIAPI
252 GetPerformanceMeasurement (
253 UINTN LogEntryKey,
254 OUT CONST VOID **Handle,
255 OUT CONST CHAR8 **Token,
256 OUT CONST CHAR8 **Module,
257 OUT UINT64 *StartTimeStamp,
258 OUT UINT64 *EndTimeStamp
259 )
260 {
261 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
262 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;
263 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
264 UINTN NumberOfEntries;
265
266 ASSERT (Handle != NULL);
267 ASSERT (Token != NULL);
268 ASSERT (Module != NULL);
269 ASSERT (StartTimeStamp != NULL);
270 ASSERT (EndTimeStamp != NULL);
271
272 PeiPerformanceLog = InternalGetPerformanceHobLog ();
273
274 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);
275 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
276 //
277 // Make sure that LogEntryKey is a valid log entry key.
278 //
279 ASSERT (LogEntryKey <= NumberOfEntries);
280
281 if (LogEntryKey == NumberOfEntries) {
282 return 0;
283 }
284
285 CurrentLogEntry = &(LogEntryArray[LogEntryKey++]);
286
287 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);
288 *Token = CurrentLogEntry->Token;
289 *Module = CurrentLogEntry->Module;
290 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;
291 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;
292
293 return LogEntryKey;
294 }
295
296 /**
297 Returns TRUE if the performance measurement macros are enabled.
298
299 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
300 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
301
302 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
303 PcdPerformanceLibraryPropertyMask is set.
304 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
305 PcdPerformanceLibraryPropertyMask is clear.
306
307 **/
308 BOOLEAN
309 EFIAPI
310 PerformanceMeasurementEnabled (
311 VOID
312 )
313 {
314 return ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
315 }