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