Events.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // ---------------------------------------------------------------------
  2. // Copyright (c) 2015 Microsoft
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. // ---------------------------------------------------------------------
  22. namespace Microsoft.IO
  23. {
  24. using System;
  25. using System.Diagnostics.Tracing;
  26. public sealed partial class RecyclableMemoryStreamManager
  27. {
  28. [EventSource(Name = "Microsoft-IO-RecyclableMemoryStream", Guid = "{B80CD4E4-890E-468D-9CBA-90EB7C82DFC7}")]
  29. public sealed class Events : EventSource
  30. {
  31. public static Events Writer = new Events();
  32. public enum MemoryStreamBufferType
  33. {
  34. Small,
  35. Large
  36. }
  37. public enum MemoryStreamDiscardReason
  38. {
  39. TooLarge,
  40. EnoughFree
  41. }
  42. [Event(1, Level = EventLevel.Verbose)]
  43. public void MemoryStreamCreated(Guid guid, string tag, int requestedSize)
  44. {
  45. if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
  46. {
  47. WriteEvent(1, guid, tag ?? string.Empty, requestedSize);
  48. }
  49. }
  50. [Event(2, Level = EventLevel.Verbose)]
  51. public void MemoryStreamDisposed(Guid guid, string tag)
  52. {
  53. if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
  54. {
  55. WriteEvent(2, guid, tag ?? string.Empty);
  56. }
  57. }
  58. [Event(3, Level = EventLevel.Critical)]
  59. public void MemoryStreamDoubleDispose(Guid guid, string tag, string allocationStack, string disposeStack1,
  60. string disposeStack2)
  61. {
  62. if (this.IsEnabled())
  63. {
  64. this.WriteEvent(3, guid, tag ?? string.Empty, allocationStack ?? string.Empty,
  65. disposeStack1 ?? string.Empty, disposeStack2 ?? string.Empty);
  66. }
  67. }
  68. [Event(4, Level = EventLevel.Error)]
  69. public void MemoryStreamFinalized(Guid guid, string tag, string allocationStack)
  70. {
  71. if (this.IsEnabled())
  72. {
  73. WriteEvent(4, guid, tag ?? string.Empty, allocationStack ?? string.Empty);
  74. }
  75. }
  76. [Event(5, Level = EventLevel.Verbose)]
  77. public void MemoryStreamToArray(Guid guid, string tag, string stack, int size)
  78. {
  79. if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
  80. {
  81. WriteEvent(5, guid, tag ?? string.Empty, stack ?? string.Empty, size);
  82. }
  83. }
  84. [Event(6, Level = EventLevel.Informational)]
  85. public void MemoryStreamManagerInitialized(int blockSize, int largeBufferMultiple, int maximumBufferSize)
  86. {
  87. if (this.IsEnabled())
  88. {
  89. WriteEvent(6, blockSize, largeBufferMultiple, maximumBufferSize);
  90. }
  91. }
  92. [Event(7, Level = EventLevel.Verbose)]
  93. public void MemoryStreamNewBlockCreated(long smallPoolInUseBytes)
  94. {
  95. if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
  96. {
  97. WriteEvent(7, smallPoolInUseBytes);
  98. }
  99. }
  100. [Event(8, Level = EventLevel.Verbose)]
  101. public void MemoryStreamNewLargeBufferCreated(int requiredSize, long largePoolInUseBytes)
  102. {
  103. if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
  104. {
  105. WriteEvent(8, requiredSize, largePoolInUseBytes);
  106. }
  107. }
  108. [Event(9, Level = EventLevel.Verbose)]
  109. public void MemoryStreamNonPooledLargeBufferCreated(int requiredSize, string tag, string allocationStack)
  110. {
  111. if (this.IsEnabled(EventLevel.Verbose, EventKeywords.None))
  112. {
  113. WriteEvent(9, requiredSize, tag ?? string.Empty, allocationStack ?? string.Empty);
  114. }
  115. }
  116. [Event(10, Level = EventLevel.Warning)]
  117. public void MemoryStreamDiscardBuffer(MemoryStreamBufferType bufferType, string tag,
  118. MemoryStreamDiscardReason reason)
  119. {
  120. if (this.IsEnabled())
  121. {
  122. WriteEvent(10, bufferType, tag ?? string.Empty, reason);
  123. }
  124. }
  125. [Event(11, Level = EventLevel.Error)]
  126. public void MemoryStreamOverCapacity(int requestedCapacity, long maxCapacity, string tag,
  127. string allocationStack)
  128. {
  129. if (this.IsEnabled())
  130. {
  131. WriteEvent(11, requestedCapacity, maxCapacity, tag ?? string.Empty, allocationStack ?? string.Empty);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. // This is here for .NET frameworks which don't support EventSource. We basically shim bare functionality used above to
  138. #if NET40
  139. namespace System.Diagnostics.Tracing
  140. {
  141. public enum EventLevel
  142. {
  143. LogAlways = 0,
  144. Critical,
  145. Error,
  146. Warning,
  147. Informational,
  148. Verbose,
  149. }
  150. public enum EventKeywords : long
  151. {
  152. None = 0x0,
  153. }
  154. [AttributeUsage(AttributeTargets.Class)]
  155. public sealed class EventSourceAttribute : Attribute
  156. {
  157. public string Name { get; set; }
  158. public string Guid { get; set; }
  159. }
  160. [AttributeUsage(AttributeTargets.Method)]
  161. public sealed class EventAttribute : Attribute
  162. {
  163. public EventAttribute(int id) { }
  164. public EventLevel Level { get; set; }
  165. }
  166. public class EventSource
  167. {
  168. public void WriteEvent(params object[] unused)
  169. {
  170. return;
  171. }
  172. public bool IsEnabled()
  173. {
  174. return false;
  175. }
  176. public bool IsEnabled(EventLevel level, EventKeywords keywords)
  177. {
  178. return false;
  179. }
  180. }
  181. }
  182. #endif