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