]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/DxePerformanceLib/DxePerformanceLib.c
Update function comments to remove confused description on PerformanceLib EndPerforma...
[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 - 2012, Intel Corporation. All rights reserved.<BR>
10 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
120 @param Handle Pointer to environment specific context used
121 to identify the component being measured.
122 @param Token Pointer to a Null-terminated ASCII string
123 that identifies the component being measured.
124 @param Module Pointer to a Null-terminated ASCII string
125 that identifies the module being measured.
126 @param TimeStamp 64-bit time stamp.
127
128 @retval RETURN_SUCCESS The end of the measurement was recorded.
129 @retval RETURN_NOT_FOUND The specified measurement record could not be found.
130
131 **/
132 RETURN_STATUS
133 EFIAPI
134 EndPerformanceMeasurement (
135 IN CONST VOID *Handle, OPTIONAL
136 IN CONST CHAR8 *Token, OPTIONAL
137 IN CONST CHAR8 *Module, OPTIONAL
138 IN UINT64 TimeStamp
139 )
140 {
141 EFI_STATUS Status;
142
143 Status = GetPerformanceProtocol ();
144 if (EFI_ERROR (Status)) {
145 return RETURN_NOT_FOUND;
146 }
147
148 Status = mPerformance->EndGauge (Handle, Token, Module, TimeStamp);
149
150 return (RETURN_STATUS) Status;
151 }
152
153 /**
154 Attempts to retrieve a performance measurement log entry from the performance measurement log.
155
156 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is
157 zero on entry, then an attempt is made to retrieve the first entry from the performance log,
158 and the key for the second entry in the log is returned. If the performance log is empty,
159 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance
160 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is
161 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is
162 retrieved and an implementation specific non-zero key value that specifies the end of the performance
163 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry
164 is retrieved and zero is returned. In the cases where a performance log entry can be returned,
165 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.
166 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().
167 If Handle is NULL, then ASSERT().
168 If Token is NULL, then ASSERT().
169 If Module is NULL, then ASSERT().
170 If StartTimeStamp is NULL, then ASSERT().
171 If EndTimeStamp is NULL, then ASSERT().
172
173 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.
174 0, then the first performance measurement log entry is retrieved.
175 On exit, the key of the next performance log entry.
176 @param Handle Pointer to environment specific context used to identify the component
177 being measured.
178 @param Token Pointer to a Null-terminated ASCII string that identifies the component
179 being measured.
180 @param Module Pointer to a Null-terminated ASCII string that identifies the module
181 being measured.
182 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
183 was started.
184 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement
185 was ended.
186
187 @return The key for the next performance log entry (in general case).
188
189 **/
190 UINTN
191 EFIAPI
192 GetPerformanceMeasurement (
193 IN UINTN LogEntryKey,
194 OUT CONST VOID **Handle,
195 OUT CONST CHAR8 **Token,
196 OUT CONST CHAR8 **Module,
197 OUT UINT64 *StartTimeStamp,
198 OUT UINT64 *EndTimeStamp
199 )
200 {
201 EFI_STATUS Status;
202 GAUGE_DATA_ENTRY *GaugeData;
203
204 ASSERT (Handle != NULL);
205 ASSERT (Token != NULL);
206 ASSERT (Module != NULL);
207 ASSERT (StartTimeStamp != NULL);
208 ASSERT (EndTimeStamp != NULL);
209
210 Status = GetPerformanceProtocol ();
211 if (EFI_ERROR (Status)) {
212 return 0;
213 }
214
215 Status = mPerformance->GetGauge (LogEntryKey++, &GaugeData);
216
217 //
218 // Make sure that LogEntryKey is a valid log entry key,
219 //
220 ASSERT (Status != EFI_INVALID_PARAMETER);
221
222 if (EFI_ERROR (Status)) {
223 //
224 // The LogEntryKey is the last entry (equals to the total entry number).
225 //
226 return 0;
227 }
228
229 ASSERT (GaugeData != NULL);
230
231 *Handle = (VOID *) (UINTN) GaugeData->Handle;
232 *Token = GaugeData->Token;
233 *Module = GaugeData->Module;
234 *StartTimeStamp = GaugeData->StartTimeStamp;
235 *EndTimeStamp = GaugeData->EndTimeStamp;
236
237 return LogEntryKey;
238 }
239
240 /**
241 Returns TRUE if the performance measurement macros are enabled.
242
243 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
244 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.
245
246 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
247 PcdPerformanceLibraryPropertyMask is set.
248 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of
249 PcdPerformanceLibraryPropertyMask is clear.
250
251 **/
252 BOOLEAN
253 EFIAPI
254 PerformanceMeasurementEnabled (
255 VOID
256 )
257 {
258 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);
259 }