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