]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
4eb47d1386aa91fab2f5fbeac058dde7651fff94
[mirror_edk2.git] / MdeModulePkg / Library / PeiPerformanceLib / PeiPerformanceLib.c
1 /** @file
2 Performance library instance used in PEI phase.
3
4 This file implements all APIs in Performance Library class in MdePkg. It creates
5 performance logging GUIDed HOB on the first performance logging and then logs the
6 performance data to the GUIDed HOB. Due to the limitation of temporary RAM, the maximum
7 number of performance logging entry is specified by PcdMaxPeiPerformanceLogEntries.
8
9 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
10 This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20
21 #include <PiPei.h>
22
23 #include <Guid/Performance.h>
24
25 #include <Library/PerformanceLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/HobLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/TimerLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/BaseMemoryLib.h>
32
33
34 /**
35 Gets the GUID HOB for PEI performance.
36
37 This internal function searches for the GUID HOB for PEI performance.
38 If that GUID HOB is not found, it will build a new one.
39 It outputs the data area of that GUID HOB to record performance log.
40
41 @param PeiPerformanceLog Pointer to Pointer to PEI performance log header.
42 @param PeiPerformanceIdArray Pointer to Pointer to PEI performance identifier array.
43
44 **/
45 VOID
46 InternalGetPerformanceHobLog (
47 OUT PEI_PERFORMANCE_LOG_HEADER **PeiPerformanceLog,
48 OUT UINT32 **PeiPerformanceIdArray
49 )
50 {
51 EFI_HOB_GUID_TYPE *GuidHob;
52 UINTN PeiPerformanceSize;
53
54 ASSERT (PeiPerformanceLog != NULL);
55 ASSERT (PeiPerformanceIdArray != NULL);
56
57 GuidHob = GetFirstGuidHob (&gPerformanceProtocolGuid);
58
59 if (GuidHob != NULL) {
60 //
61 // PEI Performance HOB was found, then return the existing one.
62 //
63 *PeiPerformanceLog = GET_GUID_HOB_DATA (GuidHob);
64
65 GuidHob = GetFirstGuidHob (&gPerformanceExProtocolGuid);
66 ASSERT (GuidHob != NULL);
67 *PeiPerformanceIdArray = GET_GUID_HOB_DATA (GuidHob);
68 } else {
69 //
70 // PEI Performance HOB was not found, then build one.
71 //
72 PeiPerformanceSize = sizeof (PEI_PERFORMANCE_LOG_HEADER) +
73 sizeof (PEI_PERFORMANCE_LOG_ENTRY) * PcdGet8 (PcdMaxPeiPerformanceLogEntries);
74 *PeiPerformanceLog = BuildGuidHob (&gPerformanceProtocolGuid, PeiPerformanceSize);
75 *PeiPerformanceLog = ZeroMem (*PeiPerformanceLog, PeiPerformanceSize);
76
77 PeiPerformanceSize = sizeof (UINT32) * PcdGet8 (PcdMaxPeiPerformanceLogEntries);
78 *PeiPerformanceIdArray = BuildGuidHob (&gPerformanceExProtocolGuid, PeiPerformanceSize);
79 *PeiPerformanceIdArray = ZeroMem (*PeiPerformanceIdArray, PeiPerformanceSize);
80 }
81 }
82
83 /**
84 Searches in the log array with keyword Handle, Token, Module and Identifier.
85
86 This internal function searches for the log entry in the log array.
87 If there is an entry that exactly matches the given keywords
88 and its end time stamp is zero, then the index of that log entry is returned;
89 otherwise, the the number of log entries in the array is returned.
90
91 @param PeiPerformanceLog Pointer to the data structure containing PEI
92 performance data.
93 @param PeiPerformanceIdArray Pointer to PEI performance identifier array.
94 @param Handle Pointer to environment specific context used
95 to identify the component being measured.
96 @param Token Pointer to a Null-terminated ASCII string
97 that identifies the component being measured.
98 @param Module Pointer to a Null-terminated ASCII string
99 that identifies the module being measured.
100 @param Identifier 32-bit identifier.
101
102 @retval The index of log entry in the array.
103
104 **/
105 UINT32
106 InternalSearchForLogEntry (
107 IN PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog,
108 IN UINT32 *PeiPerformanceIdArray,
109 IN CONST VOID *Handle, OPTIONAL
110 IN CONST CHAR8 *Token, OPTIONAL
111 IN CONST CHAR8 *Module, OPTIONAL
112 IN UINT32 Identifier
113 )
114 {
115 UINT32 Index;
116 UINT32 Index2;
117 UINT32 NumberOfEntries;
118 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
119
120
121 if (Token == NULL) {
122 Token = "";
123 }
124 if (Module == NULL) {
125 Module = "";
126 }
127 NumberOfEntries = PeiPerformanceLog->NumberOfEntries;
128 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
129
130 Index2 = 0;
131
132 for (Index = 0; Index < NumberOfEntries; Index++) {
133 Index2 = NumberOfEntries - 1 - Index;
134 if (LogEntryArray[Index2].EndTimeStamp == 0 &&
135 (LogEntryArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&
136 AsciiStrnCmp (LogEntryArray[Index2].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
137 AsciiStrnCmp (LogEntryArray[Index2].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
138 (PeiPerformanceIdArray[Index2] == Identifier)) {
139 Index = Index2;
140 break;
141 }
142 }
143 return Index;
144 }
145
146 /**
147 Creates a record for the beginning of a performance measurement.
148
149 Creates a record that contains the Handle, Token, Module and Identifier.
150 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
151 If TimeStamp is zero, then this function reads the current time stamp
152 and adds that time stamp value to the record as the start time.
153
154 @param Handle Pointer to environment specific context used
155 to identify the component being measured.
156 @param Token Pointer to a Null-terminated ASCII string
157 that identifies the component being measured.
158 @param Module Pointer to a Null-terminated ASCII string
159 that identifies the module being measured.
160 @param TimeStamp 64-bit time stamp.
161 @param Identifier 32-bit identifier. If the value is 0, the created record
162 is same as the one created by StartPerformanceMeasurement.
163
164 @retval RETURN_SUCCESS The start of the measurement was recorded.
165 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
166
167 **/
168 RETURN_STATUS
169 EFIAPI
170 StartPerformanceMeasurementEx (
171 IN CONST VOID *Handle, OPTIONAL
172 IN CONST CHAR8 *Token, OPTIONAL
173 IN CONST CHAR8 *Module, OPTIONAL
174 IN UINT64 TimeStamp,
175 IN UINT32 Identifier
176 )
177 {
178 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
179 UINT32 *PeiPerformanceIdArray;
180 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
181 UINT32 Index;
182
183 InternalGetPerformanceHobLog (&PeiPerformanceLog, &PeiPerformanceIdArray);
184
185 if (PeiPerformanceLog->NumberOfEntries >= PcdGet8 (PcdMaxPeiPerformanceLogEntries)) {
186 return RETURN_OUT_OF_RESOURCES;
187 }
188 Index = PeiPerformanceLog->NumberOfEntries++;
189 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
190 LogEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
191
192 if (Token != NULL) {
193 AsciiStrCpyS (LogEntryArray[Index].Token, PEI_PERFORMANCE_STRING_SIZE, Token);
194 }
195 if (Module != NULL) {
196 AsciiStrCpyS (LogEntryArray[Index].Module, PEI_PERFORMANCE_STRING_SIZE, Module);
197 }
198
199 LogEntryArray[Index].EndTimeStamp = 0;
200 PeiPerformanceIdArray[Index] = Identifier;
201
202 if (TimeStamp == 0) {
203 TimeStamp = GetPerformanceCounter ();
204 }
205 LogEntryArray[Index].StartTimeStamp = TimeStamp;
206
207 return RETURN_SUCCESS;
208 }
209
210 /**
211 Fills in the end time of a performance measurement.
212
213 Looks up the record that matches Handle, Token, Module and Identifier.
214 If the record can not be found then return RETURN_NOT_FOUND.
215 If the record is found and TimeStamp is not zero,
216 then TimeStamp is added to the record as the end time.
217 If the record is found and TimeStamp is zero, then this function reads
218 the current time stamp and adds that time stamp value to the record as the end time.
219
220 @param Handle Pointer to environment specific context used
221 to identify the component being measured.
222 @param Token Pointer to a Null-terminated ASCII string
223 that identifies the component being measured.
224 @param Module Pointer to a Null-terminated ASCII string
225 that identifies the module being measured.
226 @param TimeStamp 64-bit time stamp.
227 @param Identifier 32-bit identifier. If the value is 0, the found record
228 is same as the one found by EndPerformanceMeasurement.
229
230 @retval RETURN_SUCCESS The end of the measurement was recorded.
231 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
232
233 **/
234 RETURN_STATUS
235 EFIAPI
236 EndPerformanceMeasurementEx (
237 IN CONST VOID *Handle, OPTIONAL
238 IN CONST CHAR8 *Token, OPTIONAL
239 IN CONST CHAR8 *Module, OPTIONAL
240 IN UINT64 TimeStamp,
241 IN UINT32 Identifier
242 )
243 {
244 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
245 UINT32 *PeiPerformanceIdArray;
246 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
247 UINT32 Index;
248
249 if (TimeStamp == 0) {
250 TimeStamp = GetPerformanceCounter ();
251 }
252
253 InternalGetPerformanceHobLog (&PeiPerformanceLog, &PeiPerformanceIdArray);
254 Index = InternalSearchForLogEntry (PeiPerformanceLog, PeiPerformanceIdArray, Handle, Token, Module, Identifier);
255 if (Index >= PeiPerformanceLog->NumberOfEntries) {
256 return RETURN_NOT_FOUND;
257 }
258 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
259 LogEntryArray[Index].EndTimeStamp = TimeStamp;
260
261 return RETURN_SUCCESS;
262 }
263
264 /**
265 Attempts to retrieve a performance measurement log entry from the performance measurement log.
266 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
267 and then assign the Identifier with 0.
268
269 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
270 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
271 and the key for the second entry in the log is returned. If the performance log is empty,
272 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
273 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
274 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
275 retrieved and an implementation specific non-zero key value that specifies the end of the performance
276 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
277 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
278 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
279 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
280 If Handle is NULL, then ASSERT().
281 If Token is NULL, then ASSERT().
282 If Module is NULL, then ASSERT().
283 If StartTimeStamp is NULL, then ASSERT().
284 If EndTimeStamp is NULL, then ASSERT().
285 If Identifier is NULL, then ASSERT().
286
287 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
288 0, then the first performance measurement log entry is retrieved.
289 On exit, the key of the next performance of entry entry.
290 @param Handle Pointer to environment specific context used to identify the component
291 being measured.
292 @param Token Pointer to a Null-terminated ASCII string that identifies the component
293 being measured.
294 @param Module Pointer to a Null-terminated ASCII string that identifies the module
295 being measured.
296 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
297 was started.
298 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
299 was ended.
300 @param Identifier Pointer to the 32-bit identifier that was recorded.
301
302 @return The key for the next performance log entry (in general case).
303
304 **/
305 UINTN
306 EFIAPI
307 GetPerformanceMeasurementEx (
308 IN UINTN LogEntryKey,
309 OUT CONST VOID **Handle,
310 OUT CONST CHAR8 **Token,
311 OUT CONST CHAR8 **Module,
312 OUT UINT64 *StartTimeStamp,
313 OUT UINT64 *EndTimeStamp,
314 OUT UINT32 *Identifier
315 )
316 {
317 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
318 UINT32 *PeiPerformanceIdArray;
319 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;
320 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
321 UINTN NumberOfEntries;
322
323 ASSERT (Handle != NULL);
324 ASSERT (Token != NULL);
325 ASSERT (Module != NULL);
326 ASSERT (StartTimeStamp != NULL);
327 ASSERT (EndTimeStamp != NULL);
328 ASSERT (Identifier != NULL);
329
330 InternalGetPerformanceHobLog (&PeiPerformanceLog, &PeiPerformanceIdArray);
331
332 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);
333 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
334 //
335 // Make sure that LogEntryKey is a valid log entry key.
336 //
337 ASSERT (LogEntryKey <= NumberOfEntries);
338
339 if (LogEntryKey == NumberOfEntries) {
340 return 0;
341 }
342
343 CurrentLogEntry = &(LogEntryArray[LogEntryKey]);
344
345 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);
346 *Token = CurrentLogEntry->Token;
347 *Module = CurrentLogEntry->Module;
348 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;
349 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;
350 *Identifier = PeiPerformanceIdArray[LogEntryKey++];
351
352 return LogEntryKey;
353 }
354
355 /**
356 Creates a record for the beginning of a performance measurement.
357
358 Creates a record that contains the Handle, Token, and Module.
359 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
360 If TimeStamp is zero, then this function reads the current time stamp
361 and adds that time stamp value to the record as the start time.
362
363 @param Handle Pointer to environment specific context used
364 to identify the component being measured.
365 @param Token Pointer to a Null-terminated ASCII string
366 that identifies the component being measured.
367 @param Module Pointer to a Null-terminated ASCII string
368 that identifies the module being measured.
369 @param TimeStamp 64-bit time stamp.
370
371 @retval RETURN_SUCCESS The start of the measurement was recorded.
372 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
373
374 **/
375 RETURN_STATUS
376 EFIAPI
377 StartPerformanceMeasurement (
378 IN CONST VOID *Handle, OPTIONAL
379 IN CONST CHAR8 *Token, OPTIONAL
380 IN CONST CHAR8 *Module, OPTIONAL
381 IN UINT64 TimeStamp
382 )
383 {
384 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
385 }
386
387 /**
388 Fills in the end time of a performance measurement.
389
390 Looks up the record that matches Handle, Token, and Module.
391 If the record can not be found then return RETURN_NOT_FOUND.
392 If the record is found and TimeStamp is not zero,
393 then TimeStamp is added to the record as the end time.
394 If the record is found and TimeStamp is zero, then this function reads
395 the current time stamp and adds that time stamp value to the record as the end time.
396
397 @param Handle Pointer to environment specific context used
398 to identify the component being measured.
399 @param Token Pointer to a Null-terminated ASCII string
400 that identifies the component being measured.
401 @param Module Pointer to a Null-terminated ASCII string
402 that identifies the module being measured.
403 @param TimeStamp 64-bit time stamp.
404
405 @retval RETURN_SUCCESS The end of the measurement was recorded.
406 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
407
408 **/
409 RETURN_STATUS
410 EFIAPI
411 EndPerformanceMeasurement (
412 IN CONST VOID *Handle, OPTIONAL
413 IN CONST CHAR8 *Token, OPTIONAL
414 IN CONST CHAR8 *Module, OPTIONAL
415 IN UINT64 TimeStamp
416 )
417 {
418 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
419 }
420
421 /**
422 Attempts to retrieve a performance measurement log entry from the performance measurement log.
423 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
424 and then eliminate the Identifier.
425
426 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
427 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
428 and the key for the second entry in the log is returned. If the performance log is empty,
429 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
430 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
431 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
432 retrieved and an implementation specific non-zero key value that specifies the end of the performance
433 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
434 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
435 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
436 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
437 If Handle is NULL, then ASSERT().
438 If Token is NULL, then ASSERT().
439 If Module is NULL, then ASSERT().
440 If StartTimeStamp is NULL, then ASSERT().
441 If EndTimeStamp is NULL, then ASSERT().
442
443 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
444 0, then the first performance measurement log entry is retrieved.
445 On exit, the key of the next performance of entry entry.
446 @param Handle Pointer to environment specific context used to identify the component
447 being measured.
448 @param Token Pointer to a Null-terminated ASCII string that identifies the component
449 being measured.
450 @param Module Pointer to a Null-terminated ASCII string that identifies the module
451 being measured.
452 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
453 was started.
454 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
455 was ended.
456
457 @return The key for the next performance log entry (in general case).
458
459 **/
460 UINTN
461 EFIAPI
462 GetPerformanceMeasurement (
463 IN UINTN LogEntryKey,
464 OUT CONST VOID **Handle,
465 OUT CONST CHAR8 **Token,
466 OUT CONST CHAR8 **Module,
467 OUT UINT64 *StartTimeStamp,
468 OUT UINT64 *EndTimeStamp
469 )
470 {
471 UINT32 Identifier;
472 return GetPerformanceMeasurementEx (LogEntryKey, Handle, Token, Module, StartTimeStamp, EndTimeStamp, &Identifier);
473 }
474
475 /**
476 Returns TRUE if the performance measurement macros are enabled.
477
478 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
479 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
480
481 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
482 PcdPerformanceLibraryPropertyMask is set.
483 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
484 PcdPerformanceLibraryPropertyMask is clear.
485
486 **/
487 BOOLEAN
488 EFIAPI
489 PerformanceMeasurementEnabled (
490 VOID
491 )
492 {
493 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
494 }