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