]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxePerformanceLib/DxePerformanceLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / DxePerformanceLib / DxePerformanceLib.c
1 /** @file
2 Performance Library
3
4 This library instance provides infrastructure for DXE phase drivers to log performance
5 data. It consumes PerformanceEx or Performance Protocol published by DxeCorePerformanceLib
6 to log performance data. If both PerformanceEx and Performance Protocol is not available, it does not log any
7 performance information.
8
9 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
11
12 **/
13
14 #include <PiDxe.h>
15
16 #include <Guid/PerformanceMeasurement.h>
17
18 #include <Library/PerformanceLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/PcdLib.h>
22
23 //
24 // The cached Performance Protocol and PerformanceEx Protocol interface.
25 //
26 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL *mPerformanceMeasurement = NULL;
27
28 /**
29 The function caches the pointers to PerformanceEx protocol and Performance Protocol.
30
31 The function locates PerformanceEx protocol and Performance Protocol from protocol database.
32
33 @retval EFI_SUCCESS PerformanceEx protocol or Performance Protocol is successfully located.
34 @retval EFI_NOT_FOUND Both PerformanceEx protocol and Performance Protocol are not located to log performance.
35
36 **/
37 EFI_STATUS
38 GetPerformanceMeasurementProtocol (
39 VOID
40 )
41 {
42 EFI_STATUS Status;
43 EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL *PerformanceMeasurement;
44
45 if (mPerformanceMeasurement != NULL) {
46 return EFI_SUCCESS;
47 }
48
49 Status = gBS->LocateProtocol (&gEdkiiPerformanceMeasurementProtocolGuid, NULL, (VOID **)&PerformanceMeasurement);
50 if (!EFI_ERROR (Status)) {
51 ASSERT (PerformanceMeasurement != NULL);
52 //
53 // Cache PerformanceMeasurement Protocol.
54 //
55 mPerformanceMeasurement = PerformanceMeasurement;
56 return EFI_SUCCESS;
57 }
58
59 return EFI_NOT_FOUND;
60 }
61
62 /**
63 Creates a record for the beginning of a performance measurement.
64
65 Creates a record that contains the Handle, Token, Module and Identifier.
66 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
67 If TimeStamp is zero, then this function reads the current time stamp
68 and adds that time stamp value to the record as the start time.
69
70 @param Handle Pointer to environment specific context used
71 to identify the component being measured.
72 @param Token Pointer to a Null-terminated ASCII string
73 that identifies the component being measured.
74 @param Module Pointer to a Null-terminated ASCII string
75 that identifies the module being measured.
76 @param TimeStamp 64-bit time stamp.
77 @param Identifier 32-bit identifier. If the value is 0, the created record
78 is same as the one created by StartPerformanceMeasurement.
79
80 @retval RETURN_SUCCESS The start of the measurement was recorded.
81 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
82
83 **/
84 RETURN_STATUS
85 EFIAPI
86 StartPerformanceMeasurementEx (
87 IN CONST VOID *Handle OPTIONAL,
88 IN CONST CHAR8 *Token OPTIONAL,
89 IN CONST CHAR8 *Module OPTIONAL,
90 IN UINT64 TimeStamp,
91 IN UINT32 Identifier
92 )
93 {
94 EFI_STATUS Status;
95 CONST CHAR8 *String;
96
97 Status = GetPerformanceMeasurementProtocol ();
98 if (EFI_ERROR (Status)) {
99 return RETURN_NOT_FOUND;
100 }
101
102 if (Token != NULL) {
103 String = Token;
104 } else if (Module != NULL) {
105 String = Module;
106 } else {
107 String = NULL;
108 }
109
110 if (mPerformanceMeasurement != NULL) {
111 Status = mPerformanceMeasurement->CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);
112 } else {
113 ASSERT (FALSE);
114 }
115
116 return (RETURN_STATUS)Status;
117 }
118
119 /**
120 Fills in the end time of a performance measurement.
121
122 Looks up the record that matches Handle, Token and Module.
123 If the record can not be found then return RETURN_NOT_FOUND.
124 If the record is found and TimeStamp is not zero,
125 then TimeStamp is added to the record as the end time.
126 If the record is found and TimeStamp is zero, then this function reads
127 the current time stamp and adds that time stamp value to the record as the end time.
128
129 @param Handle Pointer to environment specific context used
130 to identify the component being measured.
131 @param Token Pointer to a Null-terminated ASCII string
132 that identifies the component being measured.
133 @param Module Pointer to a Null-terminated ASCII string
134 that identifies the module being measured.
135 @param TimeStamp 64-bit time stamp.
136 @param Identifier 32-bit identifier. If the value is 0, the found record
137 is same as the one found by EndPerformanceMeasurement.
138
139 @retval RETURN_SUCCESS The end of the measurement was recorded.
140 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
141
142 **/
143 RETURN_STATUS
144 EFIAPI
145 EndPerformanceMeasurementEx (
146 IN CONST VOID *Handle OPTIONAL,
147 IN CONST CHAR8 *Token OPTIONAL,
148 IN CONST CHAR8 *Module OPTIONAL,
149 IN UINT64 TimeStamp,
150 IN UINT32 Identifier
151 )
152 {
153 EFI_STATUS Status;
154 CONST CHAR8 *String;
155
156 Status = GetPerformanceMeasurementProtocol ();
157 if (EFI_ERROR (Status)) {
158 return RETURN_NOT_FOUND;
159 }
160
161 if (Token != NULL) {
162 String = Token;
163 } else if (Module != NULL) {
164 String = Module;
165 } else {
166 String = NULL;
167 }
168
169 if (mPerformanceMeasurement != NULL) {
170 Status = mPerformanceMeasurement->CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);
171 } else {
172 ASSERT (FALSE);
173 }
174
175 return (RETURN_STATUS)Status;
176 }
177
178 /**
179 Attempts to retrieve a performance measurement log entry from the performance measurement log.
180 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,
181 and then assign the Identifier with 0.
182
183 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
184 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
185 and the key for the second entry in the log is returned. If the performance log is empty,
186 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
187 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
188 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
189 retrieved and an implementation specific non-zero key value that specifies the end of the performance
190 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
191 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
192 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.
193 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
194 If Handle is NULL, then ASSERT().
195 If Token is NULL, then ASSERT().
196 If Module is NULL, then ASSERT().
197 If StartTimeStamp is NULL, then ASSERT().
198 If EndTimeStamp is NULL, then ASSERT().
199 If Identifier is NULL, then ASSERT().
200
201 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
202 0, then the first performance measurement log entry is retrieved.
203 On exit, the key of the next performance log entry.
204 @param Handle Pointer to environment specific context used to identify the component
205 being measured.
206 @param Token Pointer to a Null-terminated ASCII string that identifies the component
207 being measured.
208 @param Module Pointer to a Null-terminated ASCII string that identifies the module
209 being measured.
210 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
211 was started.
212 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
213 was ended.
214 @param Identifier Pointer to the 32-bit identifier that was recorded.
215
216 @return The key for the next performance log entry (in general case).
217
218 **/
219 UINTN
220 EFIAPI
221 GetPerformanceMeasurementEx (
222 IN UINTN LogEntryKey,
223 OUT CONST VOID **Handle,
224 OUT CONST CHAR8 **Token,
225 OUT CONST CHAR8 **Module,
226 OUT UINT64 *StartTimeStamp,
227 OUT UINT64 *EndTimeStamp,
228 OUT UINT32 *Identifier
229 )
230 {
231 return 0;
232 }
233
234 /**
235 Creates a record for the beginning of a performance measurement.
236
237 Creates a record that contains the Handle, Token, and Module.
238 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
239 If TimeStamp is zero, then this function reads the current time stamp
240 and adds that time stamp value to the record as the start time.
241
242 @param Handle Pointer to environment specific context used
243 to identify the component being measured.
244 @param Token Pointer to a Null-terminated ASCII string
245 that identifies the component being measured.
246 @param Module Pointer to a Null-terminated ASCII string
247 that identifies the module being measured.
248 @param TimeStamp 64-bit time stamp.
249
250 @retval RETURN_SUCCESS The start of the measurement was recorded.
251 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
252
253 **/
254 RETURN_STATUS
255 EFIAPI
256 StartPerformanceMeasurement (
257 IN CONST VOID *Handle OPTIONAL,
258 IN CONST CHAR8 *Token OPTIONAL,
259 IN CONST CHAR8 *Module OPTIONAL,
260 IN UINT64 TimeStamp
261 )
262 {
263 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
264 }
265
266 /**
267 Fills in the end time of a performance measurement.
268
269 Looks up the record that matches Handle, Token, and Module.
270 If the record can not be found then return RETURN_NOT_FOUND.
271 If the record is found and TimeStamp is not zero,
272 then TimeStamp is added to the record as the end time.
273 If the record is found and TimeStamp is zero, then this function reads
274 the current time stamp and adds that time stamp value to the record as the end time.
275
276 @param Handle Pointer to environment specific context used
277 to identify the component being measured.
278 @param Token Pointer to a Null-terminated ASCII string
279 that identifies the component being measured.
280 @param Module Pointer to a Null-terminated ASCII string
281 that identifies the module being measured.
282 @param TimeStamp 64-bit time stamp.
283
284 @retval RETURN_SUCCESS The end of the measurement was recorded.
285 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
286
287 **/
288 RETURN_STATUS
289 EFIAPI
290 EndPerformanceMeasurement (
291 IN CONST VOID *Handle OPTIONAL,
292 IN CONST CHAR8 *Token OPTIONAL,
293 IN CONST CHAR8 *Module OPTIONAL,
294 IN UINT64 TimeStamp
295 )
296 {
297 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);
298 }
299
300 /**
301 Attempts to retrieve a performance measurement log entry from the performance measurement log.
302 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,
303 and then eliminate the Identifier.
304
305 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
306 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
307 and the key for the second entry in the log is returned. If the performance log is empty,
308 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
309 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
310 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
311 retrieved and an implementation specific non-zero key value that specifies the end of the performance
312 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
313 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
314 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
315 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
316 If Handle is NULL, then ASSERT().
317 If Token is NULL, then ASSERT().
318 If Module is NULL, then ASSERT().
319 If StartTimeStamp is NULL, then ASSERT().
320 If EndTimeStamp is NULL, then ASSERT().
321
322 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
323 0, then the first performance measurement log entry is retrieved.
324 On exit, the key of the next performance log entry.
325 @param Handle Pointer to environment specific context used to identify the component
326 being measured.
327 @param Token Pointer to a Null-terminated ASCII string that identifies the component
328 being measured.
329 @param Module Pointer to a Null-terminated ASCII string that identifies the module
330 being measured.
331 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
332 was started.
333 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
334 was ended.
335
336 @return The key for the next performance log entry (in general case).
337
338 **/
339 UINTN
340 EFIAPI
341 GetPerformanceMeasurement (
342 IN UINTN LogEntryKey,
343 OUT CONST VOID **Handle,
344 OUT CONST CHAR8 **Token,
345 OUT CONST CHAR8 **Module,
346 OUT UINT64 *StartTimeStamp,
347 OUT UINT64 *EndTimeStamp
348 )
349 {
350 return 0;
351 }
352
353 /**
354 Returns TRUE if the performance measurement macros are enabled.
355
356 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
357 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
358
359 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
360 PcdPerformanceLibraryPropertyMask is set.
361 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
362 PcdPerformanceLibraryPropertyMask is clear.
363
364 **/
365 BOOLEAN
366 EFIAPI
367 PerformanceMeasurementEnabled (
368 VOID
369 )
370 {
371 return (BOOLEAN)((PcdGet8 (PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
372 }
373
374 /**
375 Create performance record with event description and a timestamp.
376
377 @param CallerIdentifier - Image handle or pointer to caller ID GUID
378 @param Guid - Pointer to a GUID
379 @param String - Pointer to a string describing the measurement
380 @param Address - Pointer to a location in memory relevant to the measurement
381 @param Identifier - Performance identifier describing the type of measurement
382
383 @retval RETURN_SUCCESS - Successfully created performance record
384 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records
385 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL
386 pointer or invalid PerfId
387
388 **/
389 RETURN_STATUS
390 EFIAPI
391 LogPerformanceMeasurement (
392 IN CONST VOID *CallerIdentifier,
393 IN CONST VOID *Guid OPTIONAL,
394 IN CONST CHAR8 *String OPTIONAL,
395 IN UINT64 Address OPTIONAL,
396 IN UINT32 Identifier
397 )
398 {
399 EFI_STATUS Status;
400
401 Status = GetPerformanceMeasurementProtocol ();
402 if (EFI_ERROR (Status)) {
403 return RETURN_OUT_OF_RESOURCES;
404 }
405
406 if (mPerformanceMeasurement != NULL) {
407 Status = mPerformanceMeasurement->CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);
408 } else {
409 ASSERT (FALSE);
410 }
411
412 return (RETURN_STATUS)Status;
413 }
414
415 /**
416 Check whether the specified performance measurement can be logged.
417
418 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set
419 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.
420
421 @param Type - Type of the performance measurement entry.
422
423 @retval TRUE The performance measurement can be logged.
424 @retval FALSE The performance measurement can NOT be logged.
425
426 **/
427 BOOLEAN
428 EFIAPI
429 LogPerformanceMeasurementEnabled (
430 IN CONST UINTN Type
431 )
432 {
433 //
434 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.
435 //
436 if (PerformanceMeasurementEnabled () && ((PcdGet8 (PcdPerformanceLibraryPropertyMask) & Type) == 0)) {
437 return TRUE;
438 }
439
440 return FALSE;
441 }