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