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