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