]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
Update the copyright notice format
[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 - 2008, 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 If this function is called multiple times for the same record, then the end time is overwritten.
201
202 @param Handle Pointer to environment specific context used
203 to identify the component being measured.
204 @param Token Pointer to a Null-terminated ASCII string
205 that identifies the component being measured.
206 @param Module Pointer to a Null-terminated ASCII string
207 that identifies the module being measured.
208 @param TimeStamp 64-bit time stamp.
209
210 @retval RETURN_SUCCESS The end of the measurement was recorded.
211 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
212
213 **/
214 RETURN_STATUS
215 EFIAPI
216 EndPerformanceMeasurement (
217 IN CONST VOID *Handle, OPTIONAL
218 IN CONST CHAR8 *Token, OPTIONAL
219 IN CONST CHAR8 *Module, OPTIONAL
220 IN UINT64 TimeStamp
221 )
222 {
223 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
224 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
225 UINT32 Index;
226
227 if (TimeStamp == 0) {
228 TimeStamp = GetPerformanceCounter ();
229 }
230
231 PeiPerformanceLog = InternalGetPerformanceHobLog ();
232 Index = InternalSearchForLogEntry (PeiPerformanceLog, Handle, Token, Module);
233 if (Index >= PeiPerformanceLog->NumberOfEntries) {
234 return RETURN_NOT_FOUND;
235 }
236 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
237 LogEntryArray[Index].EndTimeStamp = TimeStamp;
238
239 return RETURN_SUCCESS;
240 }
241
242 /**
243 Attempts to retrieve a performance measurement log entry from the performance measurement log.
244
245 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
246 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
247 and the key for the second entry in the log is returned. If the performance log is empty,
248 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
249 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
250 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
251 retrieved and an implementation specific non-zero key value that specifies the end of the performance
252 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
253 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
254 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
255 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
256 If Handle is NULL, then ASSERT().
257 If Token is NULL, then ASSERT().
258 If Module is NULL, then ASSERT().
259 If StartTimeStamp is NULL, then ASSERT().
260 If EndTimeStamp is NULL, then ASSERT().
261
262 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
263 0, then the first performance measurement log entry is retrieved.
264 On exit, the key of the next performance lof entry entry.
265 @param Handle Pointer to environment specific context used to identify the component
266 being measured.
267 @param Token Pointer to a Null-terminated ASCII string that identifies the component
268 being measured.
269 @param Module Pointer to a Null-terminated ASCII string that identifies the module
270 being measured.
271 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
272 was started.
273 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
274 was ended.
275
276 @return The key for the next performance log entry (in general case).
277
278 **/
279 UINTN
280 EFIAPI
281 GetPerformanceMeasurement (
282 IN UINTN LogEntryKey,
283 OUT CONST VOID **Handle,
284 OUT CONST CHAR8 **Token,
285 OUT CONST CHAR8 **Module,
286 OUT UINT64 *StartTimeStamp,
287 OUT UINT64 *EndTimeStamp
288 )
289 {
290 PEI_PERFORMANCE_LOG_HEADER *PeiPerformanceLog;
291 PEI_PERFORMANCE_LOG_ENTRY *CurrentLogEntry;
292 PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;
293 UINTN NumberOfEntries;
294
295 ASSERT (Handle != NULL);
296 ASSERT (Token != NULL);
297 ASSERT (Module != NULL);
298 ASSERT (StartTimeStamp != NULL);
299 ASSERT (EndTimeStamp != NULL);
300
301 PeiPerformanceLog = InternalGetPerformanceHobLog ();
302
303 NumberOfEntries = (UINTN) (PeiPerformanceLog->NumberOfEntries);
304 LogEntryArray = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);
305 //
306 // Make sure that LogEntryKey is a valid log entry key.
307 //
308 ASSERT (LogEntryKey <= NumberOfEntries);
309
310 if (LogEntryKey == NumberOfEntries) {
311 return 0;
312 }
313
314 CurrentLogEntry = &(LogEntryArray[LogEntryKey++]);
315
316 *Handle = (VOID *) (UINTN) (CurrentLogEntry->Handle);
317 *Token = CurrentLogEntry->Token;
318 *Module = CurrentLogEntry->Module;
319 *StartTimeStamp = CurrentLogEntry->StartTimeStamp;
320 *EndTimeStamp = CurrentLogEntry->EndTimeStamp;
321
322 return LogEntryKey;
323 }
324
325 /**
326 Returns TRUE if the performance measurement macros are enabled.
327
328 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
329 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
330
331 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
332 PcdPerformanceLibraryPropertyMask is set.
333 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
334 PcdPerformanceLibraryPropertyMask is clear.
335
336 **/
337 BOOLEAN
338 EFIAPI
339 PerformanceMeasurementEnabled (
340 VOID
341 )
342 {
343 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
344 }