File size: 23,245 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.PerformanceData;
using System.Globalization;
using System.Management.Automation.Tracing;
namespace System.Management.Automation.PerformanceData
{
/// <summary>
/// An abstract class that forms the base class for any Counter Set type.
/// A Counter Set Instance is required to register a given performance counter category
/// with PSPerfCountersMgr.
/// </summary>
public abstract class CounterSetInstanceBase : IDisposable
{
private readonly PowerShellTraceSource _tracer = PowerShellTraceSourceFactory.GetTraceSource();
#region Protected Members
/// <summary>
/// An instance of counterSetRegistrarBase type encapsulates all the information
/// about a counter set and its associated counters.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
protected CounterSetRegistrarBase _counterSetRegistrarBase;
// NOTE: Check whether the following dictionaries need to be concurrent
// because there would be only 1 thread creating the instance,
// and that instance would then be shared by multiple threads for data access.
// Those threads won't modify/manipulate the dictionary, but they would only access it.
/// <summary>
/// Dictionary mapping counter name to id.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
protected ConcurrentDictionary<string, int> _counterNameToIdMapping;
/// <summary>
/// Dictionary mapping counter id to counter type.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
protected ConcurrentDictionary<int, CounterType> _counterIdToTypeMapping;
#region Constructors
/// <summary>
/// Constructor.
/// </summary>
protected CounterSetInstanceBase(CounterSetRegistrarBase counterSetRegistrarInst)
{
this._counterSetRegistrarBase = counterSetRegistrarInst;
_counterNameToIdMapping = new ConcurrentDictionary<string, int>();
_counterIdToTypeMapping = new ConcurrentDictionary<int, CounterType>();
CounterInfo[] counterInfoArray = this._counterSetRegistrarBase.CounterInfoArray;
for (int i = 0; i < counterInfoArray.Length; i++)
{
this._counterIdToTypeMapping.TryAdd(counterInfoArray[i].Id, counterInfoArray[i].Type);
if (!string.IsNullOrWhiteSpace(counterInfoArray[i].Name))
{
this._counterNameToIdMapping.TryAdd(counterInfoArray[i].Name, counterInfoArray[i].Id);
}
}
}
#endregion
/// <summary>
/// Method that retrieves the target counter id.
/// NOTE: If isNumerator is true, then input counter id is returned.
/// But, if isNumerator is false, then a check is made on the input
/// counter's type to ensure that denominator is indeed value for such a counter.
/// </summary>
protected bool RetrieveTargetCounterIdIfValid(int counterId, bool isNumerator, out int targetCounterId)
{
targetCounterId = counterId;
if (isNumerator == false)
{
bool isDenominatorValid = false;
CounterType counterType = this._counterIdToTypeMapping[counterId];
switch (counterType)
{
case CounterType.MultiTimerPercentageActive:
case CounterType.MultiTimerPercentageActive100Ns:
case CounterType.MultiTimerPercentageNotActive:
case CounterType.MultiTimerPercentageNotActive100Ns:
case CounterType.RawFraction32:
case CounterType.RawFraction64:
case CounterType.SampleFraction:
case CounterType.AverageCount64:
case CounterType.AverageTimer32:
isDenominatorValid = true;
break;
}
if (isDenominatorValid == false)
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Denominator for update not valid for the given counter id {0}",
counterId));
_tracer.TraceException(invalidOperationException);
return false;
}
targetCounterId = counterId + 1;
}
return true;
}
#endregion
#region Public Methods
/// <summary>
/// If isNumerator is true, then updates the numerator component
/// of target counter 'counterId' by a value given by 'stepAmount'.
/// Otherwise, updates the denominator component by 'stepAmount'.
/// </summary>
public abstract bool UpdateCounterByValue(
int counterId,
long stepAmount,
bool isNumerator);
/// <summary>
/// If isNumerator is true, then updates the numerator component
/// of target counter 'counterName' by a value given by 'stepAmount'.
/// Otherwise, updates the denominator component by 'stepAmount'.
/// </summary>
public abstract bool UpdateCounterByValue(
string counterName,
long stepAmount,
bool isNumerator);
/// <summary>
/// If isNumerator is true, then sets the numerator component of target
/// counter 'counterId' to 'counterValue'.
/// Otherwise, sets the denominator component to 'counterValue'.
/// </summary>
public abstract bool SetCounterValue(
int counterId,
long counterValue,
bool isNumerator);
/// <summary>
/// If isNumerator is true, then sets the numerator component of target
/// Counter 'counterName' to 'counterValue'.
/// Otherwise, sets the denominator component to 'counterValue'.
/// </summary>
public abstract bool SetCounterValue(
string counterName,
long counterValue,
bool isNumerator);
/// <summary>
/// This method retrieves the counter value associated with counter 'counterId'
/// based on isNumerator parameter.
/// </summary>
public abstract bool GetCounterValue(int counterId, bool isNumerator, out long counterValue);
/// <summary>
/// This method retrieves the counter value associated with counter 'counterName'
/// based on isNumerator parameter.
/// </summary>
public abstract bool GetCounterValue(string counterName, bool isNumerator, out long counterValue);
/// <summary>
/// An abstract method that will be implemented by the derived type
/// so as to dispose the appropriate counter set instance.
/// </summary>
public abstract void Dispose();
/*
/// <summary>
/// Resets the target counter 'counterId' to 0. If the given
/// counter has both numerator and denominator components, then
/// they both are set to 0.
/// </summary>
public bool ResetCounter(
int counterId)
{
this.SetCounterValue(counterId, 0, true);
this.SetCounterValue(counterId, 0, false);
}
/// <summary>
/// Resets the target counter 'counterName' to 0. If the given
/// counter has both numerator and denominator components, then
/// they both are set to 0.
/// </summary>
public void ResetCounter(string counterName)
{
this.SetCounterValue(counterName, 0, true);
this.SetCounterValue(counterName, 0, false);
}
*/
#endregion
}
/// <summary>
/// PSCounterSetInstance is a thin wrapper
/// on System.Diagnostics.PerformanceData.CounterSetInstance.
/// </summary>
public class PSCounterSetInstance : CounterSetInstanceBase
{
#region Private Members
private bool _Disposed;
private CounterSet _CounterSet;
private CounterSetInstance _CounterSetInstance;
private readonly PowerShellTraceSource _tracer = PowerShellTraceSourceFactory.GetTraceSource();
#region Private Methods
private void CreateCounterSetInstance()
{
_CounterSet =
new CounterSet(
base._counterSetRegistrarBase.ProviderId,
base._counterSetRegistrarBase.CounterSetId,
base._counterSetRegistrarBase.CounterSetInstType);
// Add the counters to the counter set definition.
foreach (CounterInfo counterInfo in base._counterSetRegistrarBase.CounterInfoArray)
{
if (counterInfo.Name == null)
{
_CounterSet.AddCounter(counterInfo.Id, counterInfo.Type);
}
else
{
_CounterSet.AddCounter(counterInfo.Id, counterInfo.Type, counterInfo.Name);
}
}
string instanceName = PSPerfCountersMgr.Instance.GetCounterSetInstanceName();
// Create an instance of the counter set (contains the counter data).
_CounterSetInstance = _CounterSet.CreateCounterSetInstance(instanceName);
}
private void UpdateCounterByValue(CounterData TargetCounterData, long stepAmount)
{
Debug.Assert(TargetCounterData != null);
if (stepAmount == -1)
{
TargetCounterData.Decrement();
}
else if (stepAmount == 1)
{
TargetCounterData.Increment();
}
else
{
TargetCounterData.IncrementBy(stepAmount);
}
}
#endregion
#endregion
#region Constructors
/// <summary>
/// Constructor for creating an instance of PSCounterSetInstance.
/// </summary>
public PSCounterSetInstance(CounterSetRegistrarBase counterSetRegBaseObj)
: base(counterSetRegBaseObj)
{
CreateCounterSetInstance();
}
#endregion
#region Destructor
/// <summary>
/// This destructor will run only if the Dispose method
/// does not get called.
/// It gives the base class opportunity to finalize.
/// </summary>
~PSCounterSetInstance()
{
Dispose(false);
}
#endregion
#region Protected Methods
/// <summary>
/// Dispose(bool disposing) executes in two distinct scenarios.
/// If disposing equals true, the method has been called directly
/// or indirectly by a user's code. Managed and unmanaged resources
/// can be disposed.
/// If disposing equals false, the method has been called by the
/// runtime from inside the finalizer and you should not reference
/// other objects. Only unmanaged resources can be disposed.
/// </summary>
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_Disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
_CounterSetInstance.Dispose();
_CounterSet.Dispose();
}
// Note disposing has been done.
_Disposed = true;
}
}
#endregion
#region IDisposable Overrides
/// <summary>
/// Dispose Method implementation for IDisposable interface.
/// </summary>
public override void Dispose()
{
this.Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SuppressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
#endregion
#region CounterSetInstanceBase Overrides
#region Public Methods
/// <summary>
/// If isNumerator is true, then updates the numerator component
/// of target counter 'counterId' by a value given by 'stepAmount'.
/// Otherwise, updates the denominator component by 'stepAmount'.
/// </summary>
public override bool UpdateCounterByValue(int counterId, long stepAmount, bool isNumerator)
{
if (_Disposed)
{
ObjectDisposedException objectDisposedException =
new ObjectDisposedException("PSCounterSetInstance");
_tracer.TraceException(objectDisposedException);
return false;
}
int targetCounterId;
if (base.RetrieveTargetCounterIdIfValid(counterId, isNumerator, out targetCounterId))
{
CounterData targetCounterData = _CounterSetInstance.Counters[targetCounterId];
if (targetCounterData != null)
{
this.UpdateCounterByValue(targetCounterData, stepAmount);
return true;
}
else
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Lookup for counter corresponding to counter id {0} failed",
counterId));
_tracer.TraceException(invalidOperationException);
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// If isNumerator is true, then updates the numerator component
/// of target counter 'counterName' by a value given by 'stepAmount'.
/// Otherwise, updates the denominator component by 'stepAmount'.
/// </summary>
public override bool UpdateCounterByValue(string counterName, long stepAmount, bool isNumerator)
{
if (_Disposed)
{
ObjectDisposedException objectDisposedException =
new ObjectDisposedException("PSCounterSetInstance");
_tracer.TraceException(objectDisposedException);
return false;
}
// retrieve counter id associated with the counter name
if (counterName == null)
{
ArgumentNullException argNullException = new ArgumentNullException(nameof(counterName));
_tracer.TraceException(argNullException);
return false;
}
try
{
int targetCounterId = this._counterNameToIdMapping[counterName];
return this.UpdateCounterByValue(targetCounterId, stepAmount, isNumerator);
}
catch (KeyNotFoundException)
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Lookup for counter corresponding to counter name {0} failed",
counterName));
_tracer.TraceException(invalidOperationException);
return false;
}
}
/// <summary>
/// If isNumerator is true, then sets the numerator component
/// of target counter 'counterId' to 'counterValue'.
/// Otherwise, sets the denominator component to 'counterValue'.
/// </summary>
public override bool SetCounterValue(int counterId, long counterValue, bool isNumerator)
{
if (_Disposed)
{
ObjectDisposedException objectDisposedException =
new ObjectDisposedException("PSCounterSetInstance");
_tracer.TraceException(objectDisposedException);
return false;
}
int targetCounterId;
if (base.RetrieveTargetCounterIdIfValid(counterId, isNumerator, out targetCounterId))
{
CounterData targetCounterData = _CounterSetInstance.Counters[targetCounterId];
if (targetCounterData != null)
{
targetCounterData.Value = counterValue;
return true;
}
else
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Lookup for counter corresponding to counter id {0} failed",
counterId));
_tracer.TraceException(invalidOperationException);
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// If isNumerator is true, then updates the numerator component
/// of target counter 'counterName' by a value given by 'counterValue'.
/// Otherwise, sets the denominator component to 'counterValue'.
/// </summary>
public override bool SetCounterValue(string counterName, long counterValue, bool isNumerator)
{
if (_Disposed)
{
ObjectDisposedException objectDisposedException =
new ObjectDisposedException("PSCounterSetInstance");
_tracer.TraceException(objectDisposedException);
return false;
}
// retrieve counter id associated with the counter name
if (counterName == null)
{
ArgumentNullException argNullException = new ArgumentNullException(nameof(counterName));
_tracer.TraceException(argNullException);
return false;
}
try
{
int targetCounterId = this._counterNameToIdMapping[counterName];
return this.SetCounterValue(targetCounterId, counterValue, isNumerator);
}
catch (KeyNotFoundException)
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Lookup for counter corresponding to counter name {0} failed",
counterName));
_tracer.TraceException(invalidOperationException);
return false;
}
}
/// <summary>
/// This method retrieves the counter value associated with counter 'counterId'
/// based on isNumerator parameter.
/// </summary>
public override bool GetCounterValue(int counterId, bool isNumerator, out long counterValue)
{
counterValue = -1;
if (_Disposed)
{
ObjectDisposedException objectDisposedException =
new ObjectDisposedException("PSCounterSetInstance");
_tracer.TraceException(objectDisposedException);
return false;
}
int targetCounterId;
if (base.RetrieveTargetCounterIdIfValid(counterId, isNumerator, out targetCounterId))
{
CounterData targetCounterData = _CounterSetInstance.Counters[targetCounterId];
if (targetCounterData != null)
{
counterValue = targetCounterData.Value;
return true;
}
else
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Lookup for counter corresponding to counter id {0} failed",
counterId));
_tracer.TraceException(invalidOperationException);
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// This method retrieves the counter value associated with counter 'counterName'
/// based on isNumerator parameter.
/// </summary>
public override bool GetCounterValue(string counterName, bool isNumerator, out long counterValue)
{
counterValue = -1;
if (_Disposed)
{
ObjectDisposedException objectDisposedException =
new ObjectDisposedException("PSCounterSetInstance");
_tracer.TraceException(objectDisposedException);
return false;
}
// retrieve counter id associated with the counter name
if (counterName == null)
{
ArgumentNullException argNullException = new ArgumentNullException(nameof(counterName));
_tracer.TraceException(argNullException);
return false;
}
try
{
int targetCounterId = this._counterNameToIdMapping[counterName];
return this.GetCounterValue(targetCounterId, isNumerator, out counterValue);
}
catch (KeyNotFoundException)
{
InvalidOperationException invalidOperationException =
new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Lookup for counter corresponding to counter name {0} failed",
counterName));
_tracer.TraceException(invalidOperationException);
return false;
}
}
#endregion
#endregion
}
}
|