]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
Code scrub performance library instances in MdeModulePkg
[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 - 2008, Intel Corporation. <BR>
10 All rights reserved. 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/PeiPerformanceHob.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 PEI 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 returns the data area of that GUID HOB to record performance log.
40
41 @param Handle Pointer to environment specific context used
42 to identify the component being measured.
43 @param Token Pointer to a Null-terminated ASCII string
44 that identifies the component being measured.
45 @param Module Pointer to a Null-terminated ASCII string
46 that identifies the module being measured.
47
48 @retval The index of log entry in the array.
49
50 **/
51 PEI_PERFORMANCE_LOG_HEADER *
52 InternalGetPerformanceHobLog (
53 VOID
54 )
55 {
56 EFI_HOB_GUID_TYPE *GuidHob;
57 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
58 UINTN PeiPerformanceLogSize;
59
60 GuidHob = GetFirstGuidHob (&gPeiPerformanceHobGuid);
61
62 if (GuidHob != NULL) {
63 //
64 // PEI Performance HOB was found, then return the existing one.
65 //
66 PeiPerformanceLog = GET_GUID_HOB_DATA (GuidHob);
67 } else {
68 //
69 // PEI Performance HOB was not found, then build one.
70 //
71 PeiPerformanceLogSize = sizeof (PEI_PERFORMANCE_LOG_HEADER) +
72 sizeof (PEI_PERFORMANCE_LOG_ENTRY) * PcdGet8 (PcdMaxPeiPerformanceLogEntries);
73 PeiPerformanceLog = BuildGuidHob (&gPeiPerformanceHobGuid, PeiPerformanceLogSize);
74 PeiPerformanceLog = ZeroMem (PeiPerformanceLog, PeiPerformanceLogSize);
75 }
76
77 return PeiPerformanceLog;
78 }
79
80 /**
81 Searches in the log array with keyword Handle, Token and Module.
82
83 This internal function searches for the log entry in the log array.
84 If there is an entry that exactly matches the given key word triple
85 and its end time stamp is zero, then the index of that log entry is returned;
86 otherwise, the the number of log entries in the array is returned.
87
88 @param Handle Pointer to environment specific context used
89 to identify the component being measured.
90 @param Token Pointer to a Null-terminated ASCII string
91 that identifies the component being measured.
92 @param Module Pointer to a Null-terminated ASCII string
93 that identifies the module being measured.
94
95 @retval The index of log entry in the array.
96
97 **/
98 UINT32
99 InternalSearchForLogEntry (
100 IN PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog,
101 IN CONST VOID *Handle, OPTIONAL
102 IN CONST CHAR8 *Token, OPTIONAL
103 IN CONST CHAR8 *Module OPTIONAL
104 )
105 {
106 UINT32 Index;
107 UINT32 NumberOfEntries;
108 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
109
110
111 if (Token == NULL) {
112 Token = "";
113 }
114 if (Module == NULL) {
115 Module = "";
116 }
117 NumberOfEntries = PeiPerformanceLog->NumberOfEntries;
118 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
119
120 for (Index = 0; Index < NumberOfEntries; Index++) {
121 if ((LogEntryArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&
122 AsciiStrnCmp (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
123 AsciiStrnCmp (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
124 LogEntryArray[Index].EndTimeStamp == 0
125 ) {
126 break;
127 }
128 }
129 return Index;
130 }
131
132 /**
133 Creates a record for the beginning of a performance measurement.
134
135 Creates a record that contains the Handle, Token, and Module.
136 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
137 If TimeStamp is zero, then this function reads the current time stamp
138 and adds that time stamp value to the record as the start time.
139
140 @param Handle Pointer to environment specific context used
141 to identify the component being measured.
142 @param Token Pointer to a Null-terminated ASCII string
143 that identifies the component being measured.
144 @param Module Pointer to a Null-terminated ASCII string
145 that identifies the module being measured.
146 @param TimeStamp 64-bit time stamp.
147
148 @retval RETURN_SUCCESS The start of the measurement was recorded.
149 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
150
151 **/
152 RETURN_STATUS
153 EFIAPI
154 StartPerformanceMeasurement (
155 IN CONST VOID *Handle, OPTIONAL
156 IN CONST CHAR8 *Token, OPTIONAL
157 IN CONST CHAR8 *Module, OPTIONAL
158 IN UINT64 TimeStamp
159 )
160 {
161 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
162 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
163 UINT32 Index;
164
165 PeiPerformanceLog = InternalGetPerformanceHobLog ();
166
167 if (PeiPerformanceLog->NumberOfEntries >= PcdGet8 (PcdMaxPeiPerformanceLogEntries)) {
168 return RETURN_OUT_OF_RESOURCES;
169 }
170 Index = PeiPerformanceLog->NumberOfEntries++;
171 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
172 LogEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
173
174 if (Token != NULL) {
175 AsciiStrnCpy (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH);
176 }
177 if (Module != NULL) {
178 AsciiStrnCpy (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH);
179 }
180
181 if (TimeStamp == 0) {
182 TimeStamp = GetPerformanceCounter ();
183 }
184 LogEntryArray[Index].StartTimeStamp = TimeStamp;
185
186 return RETURN_SUCCESS;
187 }
188
189 /**
190 Fills in the end time of a performance measurement.
191
192 Looks up the record that matches Handle, Token, and Module.
193 If the record can not be found then return RETURN_NOT_FOUND.
194 If the record is found and TimeStamp is not zero,
195 then TimeStamp is added to the record as the end time.
196 If the record is found and TimeStamp is zero, then this function reads
197 the current time stamp and adds that time stamp value to the record as the end time.
198 If this function is called multiple times for the same record, then the end time is overwritten.
199
200 @param Handle Pointer to environment specific context used
201 to identify the component being measured.
202 @param Token Pointer to a Null-terminated ASCII string
203 that identifies the component being measured.
204 @param Module Pointer to a Null-terminated ASCII string
205 that identifies the module being measured.
206 @param TimeStamp 64-bit time stamp.
207
208 @retval RETURN_SUCCESS The end of the measurement was recorded.
209 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
210
211 **/
212 RETURN_STATUS
213 EFIAPI
214 EndPerformanceMeasurement (
215 IN CONST VOID *Handle, OPTIONAL
216 IN CONST CHAR8 *Token, OPTIONAL
217 IN CONST CHAR8 *Module, OPTIONAL
218 IN UINT64 TimeStamp
219 )
220 {
221 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
222 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
223 UINT32 Index;
224
225 if (TimeStamp == 0) {
226 TimeStamp = GetPerformanceCounter ();
227 }
228
229 PeiPerformanceLog = InternalGetPerformanceHobLog ();
230 Index = InternalSearchForLogEntry (PeiPerformanceLog, Handle, Token, Module);
231 if (Index >= PeiPerformanceLog->NumberOfEntries) {
232 return RETURN_NOT_FOUND;
233 }
234 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
235 LogEntryArray[Index].EndTimeStamp = TimeStamp;
236
237 return RETURN_SUCCESS;
238 }
239
240 /**
241 Attempts to retrieve a performance measurement log entry from the performance measurement log.
242
243 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
244 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
245 and the key for the second entry in the log is returned. If the performance log is empty,
246 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
247 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
248 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
249 retrieved and an implementation specific non-zero key value that specifies the end of the performance
250 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
251 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
252 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
253 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
254 If Handle is NULL, then ASSERT().
255 If Token is NULL, then ASSERT().
256 If Module is NULL, then ASSERT().
257 If StartTimeStamp is NULL, then ASSERT().
258 If EndTimeStamp is NULL, then ASSERT().
259
260 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
261 0, then the first performance measurement log entry is retrieved.
262 On exit, the key of the next performance lof entry entry.
263 @param Handle Pointer to environment specific context used to identify the component
264 being measured.
265 @param Token Pointer to a Null-terminated ASCII string that identifies the component
266 being measured.
267 @param Module Pointer to a Null-terminated ASCII string that identifies the module
268 being measured.
269 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
270 was started.
271 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
272 was ended.
273
274 @return The key for the next performance log entry (in general case).
275
276 **/
277 UINTN
278 EFIAPI
279 GetPerformanceMeasurement (
280 IN UINTN LogEntryKey,
281 OUT CONST VOID **Handle,
282 OUT CONST CHAR8 **Token,
283 OUT CONST CHAR8 **Module,
284 OUT UINT64 *StartTimeStamp,
285 OUT UINT64 *EndTimeStamp
286 )
287 {
288 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
289 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;
290 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
291 UINTN NumberOfEntries;
292
293 ASSERT (Handle != NULL);
294 ASSERT (Token != NULL);
295 ASSERT (Module != NULL);
296 ASSERT (StartTimeStamp != NULL);
297 ASSERT (EndTimeStamp != NULL);
298
299 PeiPerformanceLog = InternalGetPerformanceHobLog ();
300
301 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);
302 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
303 //
304 // Make sure that LogEntryKey is a valid log entry key.
305 //
306 ASSERT (LogEntryKey <= NumberOfEntries);
307
308 if (LogEntryKey == NumberOfEntries) {
309 return 0;
310 }
311
312 CurrentLogEntry = &(LogEntryArray[LogEntryKey++]);
313
314 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);
315 *Token = CurrentLogEntry->Token;
316 *Module = CurrentLogEntry->Module;
317 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;
318 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;
319
320 return LogEntryKey;
321 }
322
323 /**
324 Returns TRUE if the performance measurement macros are enabled.
325
326 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
327 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
328
329 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
330 PcdPerformanceLibraryPropertyMask is set.
331 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
332 PcdPerformanceLibraryPropertyMask is clear.
333
334 **/
335 BOOLEAN
336 EFIAPI
337 PerformanceMeasurementEnabled (
338 VOID
339 )
340 {
341 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
342 }