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