]> git.proxmox.com Git - mirror_edk2.git/blob - PerformancePkg/Dp_App/DpUtilities.c
Clean up DEC files:
[mirror_edk2.git] / PerformancePkg / Dp_App / DpUtilities.c
1 /** @file
2 * Utility functions used by the Dp application.
3 *
4 * Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
9 *
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/TimerLib.h>
20 #include <Library/PeCoffGetEntryPointLib.h>
21 #include <Library/PrintLib.h>
22 #include <Library/HiiLib.h>
23 #include <Library/PcdLib.h>
24
25 #include <Protocol/LoadedImage.h>
26 #include <Protocol/DriverBinding.h>
27
28 #include <Guid/Performance.h>
29
30 #include "Dp.h"
31 #include "Literals.h"
32 #include "DpInternal.h"
33
34 /**
35 Calculate an event's duration in timer ticks.
36
37 Given the count direction and the event's start and end timer values,
38 calculate the duration of the event in timer ticks. Information for
39 the current measurement is pointed to by the parameter.
40
41 If the measurement's start time is 1, it indicates that the developer
42 is indicating that the measurement began at the release of reset.
43 The start time is adjusted to the timer's starting count before performing
44 the elapsed time calculation.
45
46 The calculated duration, in ticks, is the absolute difference between
47 the measurement's ending and starting counts.
48
49 @param Measurement Pointer to a MEASUREMENT_RECORD structure containing
50 data for the current measurement.
51
52 @return The 64-bit duration of the event.
53 **/
54 UINT64
55 GetDuration (
56 IN OUT MEASUREMENT_RECORD *Measurement
57 )
58 {
59 UINT64 Duration;
60 BOOLEAN Error;
61
62 // PERF_START macros are called with a value of 1 to indicate
63 // the beginning of time. So, adjust the start ticker value
64 // to the real beginning of time.
65 // Assumes no wraparound. Even then, there is a very low probability
66 // of having a valid StartTicker value of 1.
67 if (Measurement->StartTimeStamp == 1) {
68 Measurement->StartTimeStamp = TimerInfo.StartCount;
69 }
70 if (TimerInfo.CountUp) {
71 Duration = Measurement->EndTimeStamp - Measurement->StartTimeStamp;
72 Error = (BOOLEAN)(Duration > Measurement->EndTimeStamp);
73 }
74 else {
75 Duration = Measurement->StartTimeStamp - Measurement->EndTimeStamp;
76 Error = (BOOLEAN)(Duration > Measurement->StartTimeStamp);
77 }
78
79 if (Error) {
80 DEBUG ((EFI_D_ERROR, ALit_TimerLibError));
81 Duration = 0;
82 }
83 return Duration;
84 }
85
86 /**
87 Determine whether the Measurement record is for an EFI Phase.
88
89 The Token and Module members of the measurement record are checked.
90 Module must be empty and Token must be one of SEC, PEI, DXE, BDS, or SHELL.
91
92 @param[in] Measurement A pointer to the Measurement record to test.
93
94 @retval TRUE The measurement record is for an EFI Phase.
95 @retval FALSE The measurement record is NOT for an EFI Phase.
96 **/
97 BOOLEAN
98 IsPhase(
99 IN MEASUREMENT_RECORD *Measurement
100 )
101 {
102 BOOLEAN RetVal;
103
104 RetVal = (BOOLEAN)( ( *Measurement->Module == '\0') &&
105 ((AsciiStrnCmp (Measurement->Token, ALit_SEC, PERF_TOKEN_LENGTH) == 0) ||
106 (AsciiStrnCmp (Measurement->Token, ALit_PEI, PERF_TOKEN_LENGTH) == 0) ||
107 (AsciiStrnCmp (Measurement->Token, ALit_DXE, PERF_TOKEN_LENGTH) == 0) ||
108 (AsciiStrnCmp (Measurement->Token, ALit_BDS, PERF_TOKEN_LENGTH) == 0))
109 );
110 return RetVal;
111 }
112
113 /**
114 Get the file name portion of the Pdb File Name.
115
116 The portion of the Pdb File Name between the last backslash and
117 either a following period or the end of the string is converted
118 to Unicode and copied into UnicodeBuffer. The name is truncated,
119 if necessary, to ensure that UnicodeBuffer is not overrun.
120
121 @param[in] PdbFileName Pdb file name.
122 @param[out] UnicodeBuffer The resultant Unicode File Name.
123
124 **/
125 VOID
126 GetShortPdbFileName (
127 IN CHAR8 *PdbFileName,
128 OUT CHAR16 *UnicodeBuffer
129 )
130 {
131 UINTN IndexA; // Current work location within an ASCII string.
132 UINTN IndexU; // Current work location within a Unicode string.
133 UINTN StartIndex;
134 UINTN EndIndex;
135
136 ZeroMem (UnicodeBuffer, DXE_PERFORMANCE_STRING_LENGTH * sizeof (CHAR16));
137
138 if (PdbFileName == NULL) {
139 StrCpy (UnicodeBuffer, L" ");
140 } else {
141 StartIndex = 0;
142 for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++)
143 ;
144 for (IndexA = 0; PdbFileName[IndexA] != 0; IndexA++) {
145 if (PdbFileName[IndexA] == '\\') {
146 StartIndex = IndexA + 1;
147 }
148
149 if (PdbFileName[IndexA] == '.') {
150 EndIndex = IndexA;
151 }
152 }
153
154 IndexU = 0;
155 for (IndexA = StartIndex; IndexA < EndIndex; IndexA++) {
156 UnicodeBuffer[IndexU] = (CHAR16) PdbFileName[IndexA];
157 IndexU++;
158 if (IndexU >= DXE_PERFORMANCE_STRING_LENGTH) {
159 UnicodeBuffer[DXE_PERFORMANCE_STRING_LENGTH] = 0;
160 break;
161 }
162 }
163 }
164 }
165
166 /**
167 Get a human readable name for an image handle.
168
169 @param[in] Handle
170
171 @post The resulting Unicode name string is stored in the
172 mGaugeString global array.
173
174 **/
175 VOID
176 GetNameFromHandle (
177 IN EFI_HANDLE Handle
178 )
179 {
180 EFI_STATUS Status;
181 EFI_LOADED_IMAGE_PROTOCOL *Image;
182 CHAR8 *PdbFileName;
183 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;
184 EFI_STRING StringPtr;
185
186 // Proactively get the error message so it will be ready if needed
187 StringPtr = HiiGetString (gHiiHandle, STRING_TOKEN (STR_DP_ERROR_NAME), NULL);
188 ASSERT (StringPtr != NULL);
189
190 // Get handle name from image protocol
191 //
192 Status = gBS->HandleProtocol (
193 Handle,
194 &gEfiLoadedImageProtocolGuid,
195 (VOID**) &Image
196 );
197
198 if (EFI_ERROR (Status)) {
199 Status = gBS->OpenProtocol (
200 Handle,
201 &gEfiDriverBindingProtocolGuid,
202 (VOID **) &DriverBinding,
203 NULL,
204 NULL,
205 EFI_OPEN_PROTOCOL_GET_PROTOCOL
206 );
207 if (EFI_ERROR (Status)) {
208 StrCpy (mGaugeString, StringPtr);
209 FreePool (StringPtr);
210 return ;
211 }
212
213 // Get handle name from image protocol
214 //
215 Status = gBS->HandleProtocol (
216 DriverBinding->ImageHandle,
217 &gEfiLoadedImageProtocolGuid,
218 (VOID**) &Image
219 );
220 }
221
222 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);
223
224 if (PdbFileName != NULL) {
225 GetShortPdbFileName (PdbFileName, mGaugeString);
226 } else {
227 StrCpy (mGaugeString, StringPtr);
228 }
229 FreePool (StringPtr);
230 return ;
231 }
232
233 /**
234 Calculate the Duration in microseconds.
235
236 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or
237 multiplying the result by 1000, in order to maintain precision. Since Duration is
238 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.
239
240 The time is calculated as (Duration * 1000) / Timer_Frequency.
241
242 @param[in] Duration The event duration in timer ticks.
243
244 @return A 64-bit value which is the Elapsed time in microseconds.
245 **/
246 UINT64
247 DurationInMicroSeconds (
248 IN UINT64 Duration
249 )
250 {
251 UINT64 Temp;
252
253 Temp = MultU64x32 (Duration, 1000);
254 return DivU64x32 (Temp, TimerInfo.Frequency);
255 }
256
257 /**
258 Formatted Print using a Hii Token to reference the localized format string.
259
260 @param[in] Token A HII token associated with a localized Unicode string.
261 @param[in] ... The variable argument list.
262
263 @return The number of characters converted by UnicodeVSPrint().
264
265 **/
266 UINTN
267 PrintToken (
268 IN UINT16 Token,
269 ...
270 )
271 {
272 VA_LIST Marker;
273 EFI_STRING StringPtr;
274 UINTN Return;
275 UINTN BufferSize;
276
277 StringPtr = HiiGetString (gHiiHandle, Token, NULL);
278 ASSERT (StringPtr != NULL);
279
280 VA_START (Marker, Token);
281
282 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);
283
284 if (mPrintTokenBuffer == NULL) {
285 mPrintTokenBuffer = AllocatePool (BufferSize);
286 ASSERT (mPrintTokenBuffer != NULL);
287 }
288 SetMem( mPrintTokenBuffer, BufferSize, 0);
289
290 Return = UnicodeVSPrint (mPrintTokenBuffer, BufferSize, StringPtr, Marker);
291 if (Return > 0 && gST->ConOut != NULL) {
292 gST->ConOut->OutputString (gST->ConOut, mPrintTokenBuffer);
293 }
294 FreePool (StringPtr);
295 return Return;
296 }
297
298 /**
299 Get index of Measurement Record's match in the CumData array.
300
301 If the Measurement's Token value matches a Token in one of the CumData
302 records, the index of the matching record is returned. The returned
303 index is a signed value so that negative values can indicate that
304 the Measurement didn't match any entry in the CumData array.
305
306 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.
307
308 @retval <0 Token is not in the CumData array.
309 @retval >=0 Return value is the index into CumData where Token is found.
310 **/
311 INTN
312 GetCumulativeItem(
313 IN MEASUREMENT_RECORD *Measurement
314 )
315 {
316 INTN Index;
317
318 for( Index = 0; Index < (INTN)NumCum; ++Index) {
319 if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {
320 return Index; // Exit, we found a match
321 }
322 }
323 // If the for loop exits, Token was not found.
324 return -1; // Indicate failure
325 }