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