PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/MSBuild/Microsoft/Internal/Performance/CodeMarkers.cs

#
C# | 128 lines | 118 code | 10 blank | 0 comment | 13 complexity | 698bfcf1d42b003e850f2d94f9b5cb5c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.Internal.Performance
  2. {
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. internal sealed class CodeMarkers
  7. {
  8. private const string AtomName = "VSCodeMarkersEnabled";
  9. private const string DllName = "Microsoft.Internal.Performance.CodeMarkers.dll";
  10. private bool fUseCodeMarkers = (NativeMethods.FindAtom("VSCodeMarkersEnabled") != 0);
  11. public static readonly Microsoft.Internal.Performance.CodeMarkers Instance = new Microsoft.Internal.Performance.CodeMarkers();
  12. private CodeMarkers()
  13. {
  14. }
  15. public void CodeMarker(Microsoft.Internal.Performance.CodeMarkerEvent nTimerID)
  16. {
  17. if (this.fUseCodeMarkers)
  18. {
  19. try
  20. {
  21. NativeMethods.DllPerfCodeMarker((int) nTimerID, null, 0);
  22. }
  23. catch (DllNotFoundException)
  24. {
  25. this.fUseCodeMarkers = false;
  26. }
  27. }
  28. }
  29. public void CodeMarkerEx(Microsoft.Internal.Performance.CodeMarkerEvent nTimerID, byte[] aBuff)
  30. {
  31. if (aBuff == null)
  32. {
  33. throw new ArgumentNullException("aBuff");
  34. }
  35. if (this.fUseCodeMarkers)
  36. {
  37. try
  38. {
  39. NativeMethods.DllPerfCodeMarker((int) nTimerID, aBuff, aBuff.Length);
  40. }
  41. catch (DllNotFoundException)
  42. {
  43. this.fUseCodeMarkers = false;
  44. }
  45. }
  46. }
  47. private static string GetPerformanceSubKey(RegistryKey hKey, string strRegRoot)
  48. {
  49. if (hKey == null)
  50. {
  51. return null;
  52. }
  53. string str = null;
  54. using (RegistryKey key = hKey.OpenSubKey(strRegRoot + @"\Performance"))
  55. {
  56. if (key != null)
  57. {
  58. str = key.GetValue("").ToString();
  59. }
  60. }
  61. return str;
  62. }
  63. public void InitPerformanceDll(CodeMarkerApp iApp, string strRegRoot)
  64. {
  65. this.fUseCodeMarkers = false;
  66. if (UseCodeMarkers(strRegRoot))
  67. {
  68. try
  69. {
  70. NativeMethods.AddAtom("VSCodeMarkersEnabled");
  71. NativeMethods.DllInitPerf((int) iApp);
  72. this.fUseCodeMarkers = true;
  73. }
  74. catch (DllNotFoundException)
  75. {
  76. }
  77. }
  78. }
  79. public void UninitializePerformanceDLL(CodeMarkerApp iApp)
  80. {
  81. if (this.fUseCodeMarkers)
  82. {
  83. this.fUseCodeMarkers = false;
  84. ushort atom = NativeMethods.FindAtom("VSCodeMarkersEnabled");
  85. if (atom != 0)
  86. {
  87. NativeMethods.DeleteAtom(atom);
  88. }
  89. try
  90. {
  91. NativeMethods.DllUnInitPerf((int) iApp);
  92. }
  93. catch (DllNotFoundException)
  94. {
  95. }
  96. }
  97. }
  98. private static bool UseCodeMarkers(string strRegRoot)
  99. {
  100. return !string.IsNullOrEmpty(GetPerformanceSubKey(Registry.LocalMachine, strRegRoot));
  101. }
  102. private static class NativeMethods
  103. {
  104. [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
  105. public static extern ushort AddAtom(string lpString);
  106. [DllImport("kernel32.dll")]
  107. public static extern ushort DeleteAtom(ushort atom);
  108. [DllImport("Microsoft.Internal.Performance.CodeMarkers.dll", EntryPoint="InitPerf")]
  109. public static extern void DllInitPerf(int iApp);
  110. [DllImport("Microsoft.Internal.Performance.CodeMarkers.dll", EntryPoint="PerfCodeMarker")]
  111. public static extern void DllPerfCodeMarker(int nTimerID, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] byte[] aUserParams, int cbParams);
  112. [DllImport("Microsoft.Internal.Performance.CodeMarkers.dll", EntryPoint="UnInitPerf")]
  113. public static extern void DllUnInitPerf(int iApp);
  114. [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
  115. public static extern ushort FindAtom(string lpString);
  116. }
  117. }
  118. }