MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Diagnostics.CodeAnalysis;5using System.Diagnostics.PerformanceData;6 7namespace System.Management.Automation.PerformanceData8{9 /// <summary>10 /// A struct that encapsulates the information pertaining to a given counter11 /// like name,type and id.12 /// </summary>13 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]14 public struct CounterInfo15 {16 #region Private Members17 18 #endregion19 20 #region Constructors21 /// <summary>22 /// Constructor.23 /// </summary>24 public CounterInfo(int id, CounterType type, string name)25 {26 Id = id;27 Type = type;28 Name = name;29 }30 31 /// <summary>32 /// Constructor.33 /// </summary>34 public CounterInfo(int id, CounterType type)35 {36 Id = id;37 Type = type;38 Name = null;39 }40 #endregion41 42 #region Properties43 /// <summary>44 /// Getter for Counter Name property.45 /// </summary>46 public string Name { get; }47 48 /// <summary>49 /// Getter for Counter Id property.50 /// </summary>51 public int Id { get; }52 53 /// <summary>54 /// Getter for Counter Type property.55 /// </summary>56 [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]57 public CounterType Type { get; }58 59 #endregion60 }61 62 /// <summary>63 /// An abstract class that forms the base class for any CounterSetRegistrar type.64 /// Any client that needs to register a new type of perf counter category with the65 /// PSPerfCountersMgr, should create an instance of CounterSetRegistrarBase's66 /// derived non-abstract type.67 /// The created instance is then passed to PSPerfCounterMgr's AddCounterSetInstance()68 /// method.69 /// </summary>70 public abstract class CounterSetRegistrarBase71 {72 #region Private Members73 74 #endregion75 76 #region Protected Members77 /// <summary>78 /// A reference to the encapsulated counter set instance.79 /// </summary>80 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]81 protected CounterSetInstanceBase _counterSetInstanceBase;82 83 /// <summary>84 /// Method that creates an instance of the CounterSetInstanceBase's derived type.85 /// This method is invoked by the PSPerfCountersMgr to retrieve the appropriate86 /// instance of CounterSet to register with its internal datastructure.87 /// </summary>88 protected abstract CounterSetInstanceBase CreateCounterSetInstance();89 90 #endregion91 92 #region Constructors93 /// <summary>94 /// Constructor that creates an instance of CounterSetRegistrarBase derived type95 /// based on Provider Id, counterSetId, counterSetInstanceType, a collection96 /// with counters information and an optional counterSetName.97 /// </summary>98 protected CounterSetRegistrarBase(99 Guid providerId,100 Guid counterSetId,101 CounterSetInstanceType counterSetInstType,102 CounterInfo[] counterInfoArray,103 string counterSetName = null)104 {105 ProviderId = providerId;106 CounterSetId = counterSetId;107 CounterSetInstType = counterSetInstType;108 CounterSetName = counterSetName;109 110 if (counterInfoArray is null || counterInfoArray.Length == 0)111 {112 throw new ArgumentNullException(nameof(counterInfoArray));113 }114 115 CounterInfoArray = new CounterInfo[counterInfoArray.Length];116 117 for (int i = 0; i < counterInfoArray.Length; i++)118 {119 CounterInfoArray[i] =120 new CounterInfo(121 counterInfoArray[i].Id,122 counterInfoArray[i].Type,123 counterInfoArray[i].Name124 );125 }126 127 this._counterSetInstanceBase = null;128 }129 130 /// <summary>131 /// Copy constructor.132 /// </summary>133 protected CounterSetRegistrarBase(134 CounterSetRegistrarBase srcCounterSetRegistrarBase)135 {136 ArgumentNullException.ThrowIfNull(srcCounterSetRegistrarBase);137 138 ProviderId = srcCounterSetRegistrarBase.ProviderId;139 CounterSetId = srcCounterSetRegistrarBase.CounterSetId;140 CounterSetInstType = srcCounterSetRegistrarBase.CounterSetInstType;141 CounterSetName = srcCounterSetRegistrarBase.CounterSetName;142 143 CounterInfo[] counterInfoArrayRef = srcCounterSetRegistrarBase.CounterInfoArray;144 CounterInfoArray = new CounterInfo[counterInfoArrayRef.Length];145 146 for (int i = 0; i < counterInfoArrayRef.Length; i++)147 {148 CounterInfoArray[i] =149 new CounterInfo(150 counterInfoArrayRef[i].Id,151 counterInfoArrayRef[i].Type,152 counterInfoArrayRef[i].Name);153 }154 }155 #endregion156 157 #region Properties158 159 /// <summary>160 /// Getter method for ProviderId property.161 /// </summary>162 public Guid ProviderId { get; }163 164 /// <summary>165 /// Getter method for CounterSetId property.166 /// </summary>167 public Guid CounterSetId { get; }168 169 /// <summary>170 /// Getter method for CounterSetName property.171 /// </summary>172 public string CounterSetName { get; }173 174 /// <summary>175 /// Getter method for CounterSetInstanceType property.176 /// </summary>177 public CounterSetInstanceType CounterSetInstType { get; }178 179 /// <summary>180 /// Getter method for array of counters information property.181 /// </summary>182 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]183 public CounterInfo[] CounterInfoArray { get; }184 185 /// <summary>186 /// Getter method that returns an instance of the CounterSetInstanceBase's187 /// derived type.188 /// </summary>189 public CounterSetInstanceBase CounterSetInstance190 {191 get { return _counterSetInstanceBase ?? (_counterSetInstanceBase = CreateCounterSetInstance()); }192 }193 194 #endregion195 196 #region Public Methods197 198 /// <summary>199 /// Method that disposes the referenced instance of the CounterSetInstanceBase's derived type.200 /// This method is invoked by the PSPerfCountersMgr to dispose the appropriate201 /// instance of CounterSet from its internal datastructure as part of PSPerfCountersMgr202 /// cleanup procedure.203 /// </summary>204 public abstract void DisposeCounterSetInstance();205 206 #endregion207 }208 209 /// <summary>210 /// PSCounterSetRegistrar implements the abstract methods of CounterSetRegistrarBase.211 /// Any client that needs to register a new type of perf counter category with the212 /// PSPerfCountersMgr, should create an instance of PSCounterSetRegistrar.213 /// The created instance is then passed to PSPerfCounterMgr's AddCounterSetInstance()214 /// method.215 /// </summary>216 public class PSCounterSetRegistrar : CounterSetRegistrarBase217 {218 #region Constructors219 /// <summary>220 /// Constructor that creates an instance of PSCounterSetRegistrar.221 /// </summary>222 public PSCounterSetRegistrar(223 Guid providerId,224 Guid counterSetId,225 CounterSetInstanceType counterSetInstType,226 CounterInfo[] counterInfoArray,227 string counterSetName = null)228 : base(providerId, counterSetId, counterSetInstType, counterInfoArray, counterSetName)229 {230 }231 232 /// <summary>233 /// Copy Constructor.234 /// </summary>235 public PSCounterSetRegistrar(236 PSCounterSetRegistrar srcPSCounterSetRegistrar)237 : base(srcPSCounterSetRegistrar)238 {239 ArgumentNullException.ThrowIfNull(srcPSCounterSetRegistrar);240 }241 242 #endregion243 244 #region CounterSetRegistrarBase Overrides245 246 #region Protected Methods247 248 /// <summary>249 /// Method that creates an instance of the CounterSetInstanceBase's derived type.250 /// </summary>251 protected override CounterSetInstanceBase CreateCounterSetInstance()252 {253 return new PSCounterSetInstance(this);254 }255 256 #endregion257 258 #region Public Methods259 /// <summary>260 /// Method that disposes the referenced instance of the CounterSetInstanceBase's derived type.261 /// </summary>262 public override void DisposeCounterSetInstance()263 {264 base._counterSetInstanceBase.Dispose();265 }266 267 #endregion268 269 #endregion270 }271}272 