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