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