Import DxeCorePerformanceLib DxePerformanceLib PeiPerformanceLib and EdkDxePrintLib
[mirror_edk2.git] / MdeModulePkg / Library / DxePerformanceLib / DxePerformanceLib.c
CommitLineData
a0afd019 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 DxePerformanceLib.c\r
15\r
16Abstract:\r
17\r
18 Performance Library\r
19\r
20--*/\r
21\r
22//\r
23// The package level header files this module uses\r
24//\r
25#include <PiDxe.h>\r
26//\r
27// The protocols, PPI and GUID defintions for this module\r
28//\r
29#include <Protocol/Performance.h>\r
30//\r
31// The Library classes this module consumes\r
32//\r
33#include <Library/PerformanceLib.h>\r
34#include <Library/DebugLib.h>\r
35#include <Library/UefiBootServicesTableLib.h>\r
36#include <Library/PcdLib.h>\r
37\r
38STATIC PERFORMANCE_PROTOCOL *mPerformance = NULL;\r
39\r
40/**\r
41 The constructor function caches the pointer to Performance protocol.\r
42\r
43 The constructor function locates Performance protocol from protocol database.\r
44 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
45\r
46 @retval EFI_SUCCESS Performance protocol is successfully located.\r
47 @retval Other Performance protocol is not located to log performance.\r
48\r
49**/\r
50STATIC\r
51EFI_STATUS\r
52GetPerformanceProtocol (\r
53 VOID\r
54 )\r
55{\r
56 EFI_STATUS Status;\r
57 PERFORMANCE_PROTOCOL *Performance;\r
58\r
59 if (mPerformance != NULL) {\r
60 return EFI_SUCCESS;\r
61 }\r
62\r
63 Status = gBS->LocateProtocol (&gPerformanceProtocolGuid, NULL, (VOID **) &Performance);\r
64 if (!EFI_ERROR (Status)) {\r
65 ASSERT (Performance != NULL);\r
66 //\r
67 // Cache performance protocol.\r
68 //\r
69 mPerformance = Performance;\r
70 }\r
71\r
72 return Status;\r
73}\r
74\r
75/**\r
76 Creates a record for the beginning of a performance measurement.\r
77\r
78 Creates a record that contains the Handle, Token, and Module.\r
79 If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
80 If TimeStamp is zero, then this function reads the current time stamp\r
81 and adds that time stamp value to the record as the start time.\r
82\r
83 @param Handle Pointer to environment specific context used\r
84 to identify the component being measured.\r
85 @param Token Pointer to a Null-terminated ASCII string\r
86 that identifies the component being measured.\r
87 @param Module Pointer to a Null-terminated ASCII string\r
88 that identifies the module being measured.\r
89 @param TimeStamp 64-bit time stamp.\r
90\r
91 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
92 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
93\r
94**/\r
95RETURN_STATUS\r
96EFIAPI\r
97StartPerformanceMeasurement (\r
98 IN CONST VOID *Handle, OPTIONAL\r
99 IN CONST CHAR8 *Token, OPTIONAL\r
100 IN CONST CHAR8 *Module, OPTIONAL\r
101 IN UINT64 TimeStamp\r
102 )\r
103{\r
104 EFI_STATUS Status;\r
105\r
106 Status = GetPerformanceProtocol ();\r
107 if (EFI_ERROR (Status)) {\r
108 return RETURN_OUT_OF_RESOURCES;\r
109 }\r
110\r
111 Status = mPerformance->StartGauge (Handle, Token, Module, TimeStamp);\r
112\r
113 return (RETURN_STATUS) Status;\r
114}\r
115\r
116/**\r
117 Fills in the end time of a performance measurement.\r
118\r
119 Looks up the record that matches Handle, Token, and Module.\r
120 If the record can not be found then return RETURN_NOT_FOUND.\r
121 If the record is found and TimeStamp is not zero,\r
122 then TimeStamp is added to the record as the end time.\r
123 If the record is found and TimeStamp is zero, then this function reads\r
124 the current time stamp and adds that time stamp value to the record as the end time.\r
125 If this function is called multiple times for the same record, then the end time is overwritten.\r
126\r
127 @param Handle Pointer to environment specific context used\r
128 to identify the component being measured.\r
129 @param Token Pointer to a Null-terminated ASCII string\r
130 that identifies the component being measured.\r
131 @param Module Pointer to a Null-terminated ASCII string\r
132 that identifies the module being measured.\r
133 @param TimeStamp 64-bit time stamp.\r
134\r
135 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
136 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
137\r
138**/\r
139RETURN_STATUS\r
140EFIAPI\r
141EndPerformanceMeasurement (\r
142 IN CONST VOID *Handle, OPTIONAL\r
143 IN CONST CHAR8 *Token, OPTIONAL\r
144 IN CONST CHAR8 *Module, OPTIONAL\r
145 IN UINT64 TimeStamp\r
146 )\r
147{\r
148 EFI_STATUS Status;\r
149\r
150 Status = GetPerformanceProtocol ();\r
151 if (EFI_ERROR (Status)) {\r
152 return RETURN_NOT_FOUND;\r
153 }\r
154\r
155 Status = mPerformance->EndGauge (Handle, Token, Module, TimeStamp);\r
156\r
157 return (RETURN_STATUS) Status;\r
158}\r
159\r
160/**\r
161 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
162\r
163 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
164 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
165 and the key for the second entry in the log is returned. If the performance log is empty,\r
166 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
167 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
168 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
169 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
170 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
171 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
172 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
173 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
174 If Handle is NULL, then ASSERT().\r
175 If Token is NULL, then ASSERT().\r
176 If Module is NULL, then ASSERT().\r
177 If StartTimeStamp is NULL, then ASSERT().\r
178 If EndTimeStamp is NULL, then ASSERT().\r
179\r
180 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
181 0, then the first performance measurement log entry is retrieved.\r
182 On exit, the key of the next performance lof entry entry.\r
183 @param Handle Pointer to environment specific context used to identify the component\r
184 being measured.\r
185 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
186 being measured.\r
187 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
188 being measured.\r
189 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
190 was started.\r
191 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
192 was ended.\r
193\r
194 @return The key for the next performance log entry (in general case).\r
195\r
196**/\r
197UINTN\r
198EFIAPI\r
199GetPerformanceMeasurement (\r
200 IN UINTN LogEntryKey,\r
201 OUT CONST VOID **Handle,\r
202 OUT CONST CHAR8 **Token,\r
203 OUT CONST CHAR8 **Module,\r
204 OUT UINT64 *StartTimeStamp,\r
205 OUT UINT64 *EndTimeStamp\r
206 )\r
207{\r
208 EFI_STATUS Status;\r
209 GAUGE_DATA_ENTRY *GaugeData;\r
210\r
211 ASSERT (Handle != NULL);\r
212 ASSERT (Token != NULL);\r
213 ASSERT (Module != NULL);\r
214 ASSERT (StartTimeStamp != NULL);\r
215 ASSERT (EndTimeStamp != NULL);\r
216\r
217 Status = GetPerformanceProtocol ();\r
218 if (EFI_ERROR (Status)) {\r
219 return 0;\r
220 }\r
221\r
222 Status = mPerformance->GetGauge (LogEntryKey++, &GaugeData);\r
223\r
224 //\r
225 // Make sure that LogEntryKey is a valid log entry key,\r
226 //\r
227 ASSERT (Status != EFI_INVALID_PARAMETER);\r
228\r
229 if (EFI_ERROR (Status)) {\r
230 //\r
231 // The LogEntryKey is the last entry (equals to the total entry number).\r
232 //\r
233 return 0;\r
234 }\r
235\r
236 ASSERT (GaugeData != NULL);\r
237\r
238 *Handle = (VOID *) (UINTN) GaugeData->Handle;\r
239 *Token = GaugeData->Token;\r
240 *Module = GaugeData->Module;\r
241 *StartTimeStamp = GaugeData->StartTimeStamp;\r
242 *EndTimeStamp = GaugeData->EndTimeStamp;\r
243\r
244 return LogEntryKey;\r
245}\r
246\r
247/**\r
248 Returns TRUE if the performance measurement macros are enabled.\r
249\r
250 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
251 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
252\r
253 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
254 PcdPerformanceLibraryPropertyMask is set.\r
255 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
256 PcdPerformanceLibraryPropertyMask is clear.\r
257\r
258**/\r
259BOOLEAN\r
260EFIAPI\r
261PerformanceMeasurementEnabled (\r
262 VOID\r
263 )\r
264{\r
265 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
266}\r