]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Library/EdkPeiPerformanceLib/PeiPerformanceLib.c
Initial import.
[mirror_edk2.git] / EdkModulePkg / Library / EdkPeiPerformanceLib / PeiPerformanceLib.c
CommitLineData
878ddf1f 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
60 sizeof (PEI_PERFORMANCE_LOG_ENTRY) * MAX_PEI_PERFORMANCE_LOG_ENTRIES;\r
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
155 if (PeiPerformanceLog->NumberOfEntries >= MAX_PEI_PERFORMANCE_LOG_ENTRIES) {\r
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 Retrieves a previously logged performance measurement. \r
230 \r
231 Looks up the record that matches Handle, Token, and Module.\r
232 If the record can not be found then return RETURN_NOT_FOUND.\r
233 If the record is found then the start of the measurement is returned in StartTimeStamp,\r
234 and the end of the measurement is returned in EndTimeStamp.\r
235\r
236 @param LogEntryKey The key for the previous performance measurement log entry.\r
237 If 0, then the first performance measurement log entry is retrieved.\r
238 @param Handle Pointer to environment specific context used\r
239 to identify the component being measured.\r
240 @param Token Pointer to a Null-terminated ASCII string\r
241 that identifies the component being measured.\r
242 @param Module Pointer to a Null-terminated ASCII string\r
243 that identifies the module being measured.\r
244 @param StartTimeStamp The 64-bit time stamp that was recorded when the measurement was started.\r
245 @param EndTimeStamp The 64-bit time stamp that was recorded when the measurement was ended.\r
246\r
247 @return The key for the current performance log entry.\r
248\r
249**/\r
250UINTN\r
251EFIAPI\r
252GetPerformanceMeasurement (\r
253 UINTN LogEntryKey, \r
254 OUT CONST VOID **Handle,\r
255 OUT CONST CHAR8 **Token,\r
256 OUT CONST CHAR8 **Module,\r
257 OUT UINT64 *StartTimeStamp,\r
258 OUT UINT64 *EndTimeStamp\r
259 )\r
260{\r
261 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;\r
262 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;\r
263 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;\r
264 UINTN NumberOfEntries;\r
265\r
266 ASSERT (Handle != NULL);\r
267 ASSERT (Token != NULL);\r
268 ASSERT (Module != NULL);\r
269 ASSERT (StartTimeStamp != NULL);\r
270 ASSERT (EndTimeStamp != NULL);\r
271 \r
272 PeiPerformanceLog = InternalGetPerformanceHobLog ();\r
273 \r
274 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);\r
275 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);\r
276 //\r
277 // Make sure that LogEntryKey is a valid log entry key.\r
278 //\r
279 ASSERT (LogEntryKey <= NumberOfEntries);\r
280 \r
281 if (LogEntryKey == NumberOfEntries) {\r
282 return 0;\r
283 }\r
284\r
285 CurrentLogEntry = &(LogEntryArray[LogEntryKey++]);\r
286\r
287 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);\r
288 *Token = CurrentLogEntry->Token;\r
289 *Module = CurrentLogEntry->Module;\r
290 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;\r
291 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;\r
292\r
293 return LogEntryKey; \r
294}\r
295\r
296/**\r
297 Returns TRUE if the performance measurement macros are enabled. \r
298 \r
299 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
300 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
301\r
302 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
303 PcdPerformanceLibraryPropertyMask is set.\r
304 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
305 PcdPerformanceLibraryPropertyMask is clear.\r
306\r
307**/\r
308BOOLEAN\r
309EFIAPI\r
310PerformanceMeasurementEnabled (\r
311 VOID\r
312 )\r
313{\r
314 return ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
315}\r