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