]> git.proxmox.com Git - mirror_edk2.git/blame - PerformancePkg/Dp_App/DpUtilities.c
Move some define code to the uni file. Also fixed some memory leak.
[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
88359546 209 FreePool (StringPtr);\r
c06ad33e 210 return ;\r
211 }\r
212\r
213 // Get handle name from image protocol\r
214 //\r
215 Status = gBS->HandleProtocol (\r
216 DriverBinding->ImageHandle,\r
217 &gEfiLoadedImageProtocolGuid,\r
16d5d168 218 (VOID**) &Image\r
c06ad33e 219 );\r
220 }\r
221\r
222 PdbFileName = PeCoffLoaderGetPdbPointer (Image->ImageBase);\r
223\r
224 if (PdbFileName != NULL) {\r
225 GetShortPdbFileName (PdbFileName, mGaugeString);\r
226 } else {\r
227 StrCpy (mGaugeString, StringPtr);\r
228 }\r
88359546 229 FreePool (StringPtr);\r
c06ad33e 230 return ;\r
231}\r
232\r
9dd74618
ED
233/** \r
234 Calculate the Duration in microseconds.\r
235 \r
236 Duration is multiplied by 1000, instead of Frequency being divided by 1000 or\r
237 multiplying the result by 1000, in order to maintain precision. Since Duration is\r
238 a 64-bit value, multiplying it by 1000 is unlikely to produce an overflow.\r
239 \r
240 The time is calculated as (Duration * 1000) / Timer_Frequency.\r
241 \r
242 @param[in] Duration The event duration in timer ticks.\r
243 \r
244 @return A 64-bit value which is the Elapsed time in microseconds.\r
c06ad33e 245**/\r
246UINT64\r
247DurationInMicroSeconds (\r
248 IN UINT64 Duration\r
249 )\r
250{\r
251 UINT64 Temp;\r
252\r
253 Temp = MultU64x32 (Duration, 1000);\r
254 return DivU64x32 (Temp, TimerInfo.Frequency);\r
255}\r
256\r
9dd74618
ED
257/** \r
258 Formatted Print using a Hii Token to reference the localized format string.\r
259 \r
260 @param[in] Token A HII token associated with a localized Unicode string.\r
261 @param[in] ... The variable argument list.\r
262 \r
263 @return The number of characters converted by UnicodeVSPrint().\r
264 \r
c06ad33e 265**/\r
266UINTN\r
267PrintToken (\r
268 IN UINT16 Token,\r
269 ...\r
270 )\r
271{\r
272 VA_LIST Marker;\r
273 EFI_STRING StringPtr;\r
274 UINTN Return;\r
275 UINTN BufferSize;\r
276\r
277 StringPtr = HiiGetString (gHiiHandle, Token, NULL);\r
278 ASSERT (StringPtr != NULL);\r
279\r
280 VA_START (Marker, Token);\r
281\r
282 BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);\r
283\r
284 if (mPrintTokenBuffer == NULL) {\r
285 mPrintTokenBuffer = AllocatePool (BufferSize);\r
286 ASSERT (mPrintTokenBuffer != NULL);\r
287 }\r
288 SetMem( mPrintTokenBuffer, BufferSize, 0);\r
289\r
290 Return = UnicodeVSPrint (mPrintTokenBuffer, BufferSize, StringPtr, Marker);\r
291 if (Return > 0 && gST->ConOut != NULL) {\r
292 gST->ConOut->OutputString (gST->ConOut, mPrintTokenBuffer);\r
293 }\r
88359546 294 FreePool (StringPtr);\r
c06ad33e 295 return Return;\r
296}\r
297\r
9dd74618
ED
298/** \r
299 Get index of Measurement Record's match in the CumData array.\r
300 \r
301 If the Measurement's Token value matches a Token in one of the CumData\r
302 records, the index of the matching record is returned. The returned\r
303 index is a signed value so that negative values can indicate that\r
304 the Measurement didn't match any entry in the CumData array.\r
305 \r
306 @param[in] Measurement A pointer to a Measurement Record to match against the CumData array.\r
307 \r
308 @retval <0 Token is not in the CumData array.\r
309 @retval >=0 Return value is the index into CumData where Token is found.\r
c06ad33e 310**/\r
311INTN\r
312GetCumulativeItem(\r
313 IN MEASUREMENT_RECORD *Measurement\r
314 )\r
315{\r
316 INTN Index;\r
317\r
318 for( Index = 0; Index < (INTN)NumCum; ++Index) {\r
319 if (AsciiStrnCmp (Measurement->Token, CumData[Index].Name, PERF_TOKEN_LENGTH) == 0) {\r
320 return Index; // Exit, we found a match\r
321 }\r
322 }\r
323 // If the for loop exits, Token was not found.\r
324 return -1; // Indicate failure\r
325}\r