Import DxeCorePerformanceLib DxePerformanceLib PeiPerformanceLib and EdkDxePrintLib
[mirror_edk2.git] / MdeModulePkg / Library / PeiPerformanceLib / PeiPerformanceLib.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 PeiPerformanceLib.c
15
16 Abstract:
17
18 Performance Library
19
20 --*/
21
22 //
23 // The package level header files this module uses
24 //
25 #include <PiPei.h>
26 //
27 // The protocols, PPI and GUID defintions for this module
28 //
29 #include <Guid/PeiPerformanceHob.h>
30 //
31 // The Library classes this module consumes
32 //
33 #include <Library/PerformanceLib.h>
34 #include <Library/DebugLib.h>
35 #include <Library/HobLib.h>
36 #include <Library/BaseLib.h>
37 #include <Library/TimerLib.h>
38 #include <Library/PcdLib.h>
39 #include <Library/BaseMemoryLib.h>
40
41
42 /**
43 Gets PEI the GUID HOB for PEI performance.
44
45 This internal function searches for the GUID HOB for PEI performance.
46 If that GUID HOB is not found, it will build a new one.
47 It returns the data area of that GUID HOB to record performance log.
48
49 @param Handle Pointer to environment specific context used
50 to identify the component being measured.
51 @param Token Pointer to a Null-terminated ASCII string
52 that identifies the component being measured.
53 @param Module Pointer to a Null-terminated ASCII string
54 that identifies the module being measured.
55
56 @retval The index of log entry in the array.
57
58 **/
59 STATIC
60 PEI_PERFORMANCE_LOG_HEADER *
61 InternalGetPerformanceHobLog (
62 VOID
63 )
64 {
65 EFI_HOB_GUID_TYPE *GuidHob;
66 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
67 UINTN PeiPerformanceLogSize;
68
69 GuidHob = GetFirstGuidHob (&gPeiPerformanceHobGuid);
70
71 if (GuidHob != NULL) {
72 //
73 // PEI Performance HOB was found, then return the existing one.
74 //
75 PeiPerformanceLog = GET_GUID_HOB_DATA (GuidHob);
76 } else {
77 //
78 // PEI Performance HOB was not found, then build one.
79 //
80 PeiPerformanceLogSize = sizeof (PEI_PERFORMANCE_LOG_HEADER) +
81 sizeof (PEI_PERFORMANCE_LOG_ENTRY) * PcdGet8 (PcdMaxPeiPerformanceLogEntries);
82 PeiPerformanceLog = BuildGuidHob (&gPeiPerformanceHobGuid, PeiPerformanceLogSize);
83 PeiPerformanceLog = ZeroMem (PeiPerformanceLog, PeiPerformanceLogSize);
84 }
85
86 return PeiPerformanceLog;
87 }
88
89 /**
90 Searches in the log array with keyword Handle, Token and Module.
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 key word triple
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 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
104 @retval The index of log entry in the array.
105
106 **/
107 STATIC
108 UINT32
109 InternalSearchForLogEntry (
110 IN PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog,
111 IN CONST VOID *Handle, OPTIONAL
112 IN CONST CHAR8 *Token, OPTIONAL
113 IN CONST CHAR8 *Module OPTIONAL
114 )
115 {
116 UINT32 Index;
117 UINT32 NumberOfEntries;
118 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
119
120
121 if (Token == NULL) {
122 Token = "";
123 }
124 if (Module == NULL) {
125 Module = "";
126 }
127 NumberOfEntries = PeiPerformanceLog->NumberOfEntries;
128 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
129
130 for (Index = 0; Index < NumberOfEntries; Index++) {
131 if ((LogEntryArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&
132 AsciiStrnCmp (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
133 AsciiStrnCmp (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&
134 LogEntryArray[Index].EndTimeStamp == 0
135 ) {
136 break;
137 }
138 }
139 return Index;
140 }
141
142 /**
143 Creates a record for the beginning of a performance measurement.
144
145 Creates a record that contains the Handle, Token, and Module.
146 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.
147 If TimeStamp is zero, then this function reads the current time stamp
148 and adds that time stamp value to the record as the start time.
149
150 @param Handle Pointer to environment specific context used
151 to identify the component being measured.
152 @param Token Pointer to a Null-terminated ASCII string
153 that identifies the component being measured.
154 @param Module Pointer to a Null-terminated ASCII string
155 that identifies the module being measured.
156 @param TimeStamp 64-bit time stamp.
157
158 @retval RETURN_SUCCESS The start of the measurement was recorded.
159 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.
160
161 **/
162 RETURN_STATUS
163 EFIAPI
164 StartPerformanceMeasurement (
165 IN CONST VOID *Handle, OPTIONAL
166 IN CONST CHAR8 *Token, OPTIONAL
167 IN CONST CHAR8 *Module, OPTIONAL
168 IN UINT64 TimeStamp
169 )
170 {
171 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
172 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
173 UINT32 Index;
174
175 PeiPerformanceLog = InternalGetPerformanceHobLog ();
176
177 if (PeiPerformanceLog->NumberOfEntries >= PcdGet8 (PcdMaxPeiPerformanceLogEntries)) {
178 return RETURN_OUT_OF_RESOURCES;
179 }
180 Index = PeiPerformanceLog->NumberOfEntries++;
181 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
182 LogEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;
183
184 if (Token != NULL) {
185 AsciiStrnCpy (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH);
186 }
187 if (Module != NULL) {
188 AsciiStrnCpy (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH);
189 }
190
191 if (TimeStamp == 0) {
192 TimeStamp = GetPerformanceCounter ();
193 }
194 LogEntryArray[Index].StartTimeStamp = TimeStamp;
195
196 return RETURN_SUCCESS;
197 }
198
199 /**
200 Fills in the end time of a performance measurement.
201
202 Looks up the record that matches Handle, Token, and Module.
203 If the record can not be found then return RETURN_NOT_FOUND.
204 If the record is found and TimeStamp is not zero,
205 then TimeStamp is added to the record as the end time.
206 If the record is found and TimeStamp is zero, then this function reads
207 the current time stamp and adds that time stamp value to the record as the end time.
208 If this function is called multiple times for the same record, then the end time is overwritten.
209
210 @param Handle Pointer to environment specific context used
211 to identify the component being measured.
212 @param Token Pointer to a Null-terminated ASCII string
213 that identifies the component being measured.
214 @param Module Pointer to a Null-terminated ASCII string
215 that identifies the module being measured.
216 @param TimeStamp 64-bit time stamp.
217
218 @retval RETURN_SUCCESS The end of the measurement was recorded.
219 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
220
221 **/
222 RETURN_STATUS
223 EFIAPI
224 EndPerformanceMeasurement (
225 IN CONST VOID *Handle, OPTIONAL
226 IN CONST CHAR8 *Token, OPTIONAL
227 IN CONST CHAR8 *Module, OPTIONAL
228 IN UINT64 TimeStamp
229 )
230 {
231 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
232 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
233 UINT32 Index;
234
235 if (TimeStamp == 0) {
236 TimeStamp = GetPerformanceCounter ();
237 }
238
239 PeiPerformanceLog = InternalGetPerformanceHobLog ();
240 Index = InternalSearchForLogEntry (PeiPerformanceLog, Handle, Token, Module);
241 if (Index >= PeiPerformanceLog->NumberOfEntries) {
242 return RETURN_NOT_FOUND;
243 }
244 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
245 LogEntryArray[Index].EndTimeStamp = TimeStamp;
246
247 return RETURN_SUCCESS;
248 }
249
250 /**
251 Attempts to retrieve a performance measurement log entry from the performance measurement log.
252
253 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
254 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
255 and the key for the second entry in the log is returned. If the performance log is empty,
256 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
257 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
258 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
259 retrieved and an implementation specific non-zero key value that specifies the end of the performance
260 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
261 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
262 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
263 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
264 If Handle is NULL, then ASSERT().
265 If Token is NULL, then ASSERT().
266 If Module is NULL, then ASSERT().
267 If StartTimeStamp is NULL, then ASSERT().
268 If EndTimeStamp is NULL, then ASSERT().
269
270 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
271 0, then the first performance measurement log entry is retrieved.
272 On exit, the key of the next performance lof entry entry.
273 @param Handle Pointer to environment specific context used to identify the component
274 being measured.
275 @param Token Pointer to a Null-terminated ASCII string that identifies the component
276 being measured.
277 @param Module Pointer to a Null-terminated ASCII string that identifies the module
278 being measured.
279 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
280 was started.
281 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
282 was ended.
283
284 @return The key for the next performance log entry (in general case).
285
286 **/
287 UINTN
288 EFIAPI
289 GetPerformanceMeasurement (
290 IN UINTN LogEntryKey,
291 OUT CONST VOID **Handle,
292 OUT CONST CHAR8 **Token,
293 OUT CONST CHAR8 **Module,
294 OUT UINT64 *StartTimeStamp,
295 OUT UINT64 *EndTimeStamp
296 )
297 {
298 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
299 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;
300 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
301 UINTN NumberOfEntries;
302
303 ASSERT (Handle != NULL);
304 ASSERT (Token != NULL);
305 ASSERT (Module != NULL);
306 ASSERT (StartTimeStamp != NULL);
307 ASSERT (EndTimeStamp != NULL);
308
309 PeiPerformanceLog = InternalGetPerformanceHobLog ();
310
311 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);
312 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
313 //
314 // Make sure that LogEntryKey is a valid log entry key.
315 //
316 ASSERT (LogEntryKey <= NumberOfEntries);
317
318 if (LogEntryKey == NumberOfEntries) {
319 return 0;
320 }
321
322 CurrentLogEntry = &(LogEntryArray[LogEntryKey++]);
323
324 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);
325 *Token = CurrentLogEntry->Token;
326 *Module = CurrentLogEntry->Module;
327 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;
328 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;
329
330 return LogEntryKey;
331 }
332
333 /**
334 Returns TRUE if the performance measurement macros are enabled.
335
336 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
337 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
338
339 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
340 PcdPerformanceLibraryPropertyMask is set.
341 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
342 PcdPerformanceLibraryPropertyMask is clear.
343
344 **/
345 BOOLEAN
346 EFIAPI
347 PerformanceMeasurementEnabled (
348 VOID
349 )
350 {
351 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
352 }