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