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