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