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