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