Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Management / AppDomainResourcePerfCounters.cs / 1305376 / AppDomainResourcePerfCounters.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Management { using System; using System.Configuration; using System.Web; using System.Threading; internal class AppDomainResourcePerfCounters { private const uint NUM_SECONDS_TO_POLL = 5; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// internal static void Init() { if (_fInit) return; lock (_InitLock) { if (_fInit) return; if (AppDomain.MonitoringIsEnabled) { PerfCounters.SetCounter(AppPerfCounter.APP_CPU_USED_BASE, 100); _Timer = new Timer((new AppDomainResourcePerfCounters()).TimerCallback, null, NUM_SECONDS_TO_POLL * 1000, NUM_SECONDS_TO_POLL * 1000); } _fInit = true; } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// internal static void Stop() { if (_Timer == null) return; // already stopped _StopRequested = true; lock (_InitLock) { if (_Timer != null) { ((IDisposable)_Timer).Dispose(); _Timer = null; } } // Wait for the _inProgressLock lock while (_inProgressLock != 0) { Thread.Sleep(100); } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // Static data private static bool _fInit = false; private static object _InitLock = new object(); private static Timer _Timer = null; private static int _inProgressLock = 0; private static bool _StopRequested = false; // Instance data private int _MemUsageLastReported = 0; private int _CPUUsageLastReported = 0; private TimeSpan _TotalCPUTime; private DateTime _LastCollectTime; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// private AppDomainResourcePerfCounters() { _TotalCPUTime = AppDomain.CurrentDomain.MonitoringTotalProcessorTime; _LastCollectTime = DateTime.UtcNow; } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// private void TimerCallback(Object state) { if ( _StopRequested || // Stop has been called -- exit immediately !AppDomain.MonitoringIsEnabled || // Monitoring APIs will throw NotSupportedException if not-enabled Interlocked.Exchange(ref _inProgressLock, 1) != 0) // Is some thread currently executing the callback { return; } try { SetPerfCounters(); } catch { // don't bubble up exceptions, since we are on a timer thread } finally { Interlocked.Exchange(ref _inProgressLock, 0); } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// private void SetPerfCounters() { //////////////////////////////////////////////////////////// // Calculate memory: Limited to 2TB (Int32.MaxValue * 1024 bytes) long memInKB = (AppDomain.CurrentDomain.MonitoringSurvivedMemorySize / 1024); // Mem used in KB _MemUsageLastReported = (int) Math.Min(Int32.MaxValue, Math.Max(0, memInKB)); // Make sure its within 0 and Int32.MaxValue PerfCounters.SetCounter(AppPerfCounter.APP_MEMORY_USED, _MemUsageLastReported); //////////////////////////////////////////////////////////// // Calculate CPU DateTime dtUtcNow = DateTime.UtcNow; TimeSpan newTotalCPUTime = AppDomain.CurrentDomain.MonitoringTotalProcessorTime; double timeElapsed = (dtUtcNow - _LastCollectTime).TotalMilliseconds; // Total time since last collect double cpuTimeUsed = (newTotalCPUTime - _TotalCPUTime).TotalMilliseconds; // Total CPU time used since last collect int cpuPercent = (int) ((cpuTimeUsed * 100) / timeElapsed); // Percent of CPU time used _CPUUsageLastReported = Math.Min(100, Math.Max(0, cpuPercent)); // Make sure it's within 0 and 100 PerfCounters.SetCounter(AppPerfCounter.APP_CPU_USED, _CPUUsageLastReported); // Update variables for next time _TotalCPUTime = newTotalCPUTime; _LastCollectTime = dtUtcNow; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OletxEnlistment.cs
- DynamicAttribute.cs
- SystemMulticastIPAddressInformation.cs
- BrowsableAttribute.cs
- Duration.cs
- LocationSectionRecord.cs
- ImageButton.cs
- ReadOnlyTernaryTree.cs
- WebBaseEventKeyComparer.cs
- ClipboardData.cs
- FixedElement.cs
- ByteViewer.cs
- TemplateControlCodeDomTreeGenerator.cs
- Expression.cs
- FlowDocumentReader.cs
- IntegerFacetDescriptionElement.cs
- safesecurityhelperavalon.cs
- CreateUserWizard.cs
- AspProxy.cs
- VersionedStreamOwner.cs
- HtmlLiteralTextAdapter.cs
- ResourceExpressionEditor.cs
- DataTableMappingCollection.cs
- ConfigXmlSignificantWhitespace.cs
- MailSettingsSection.cs
- QuaternionAnimationBase.cs
- ZoomPercentageConverter.cs
- DataProtection.cs
- XmlSchemaComplexContent.cs
- UnaryNode.cs
- MaskedTextProvider.cs
- DesignerPainter.cs
- JoinCqlBlock.cs
- dsa.cs
- AnnotationHighlightLayer.cs
- Signature.cs
- SecureStringHasher.cs
- SocketAddress.cs
- ScriptingScriptResourceHandlerSection.cs
- StrongNamePublicKeyBlob.cs
- CornerRadiusConverter.cs
- ByteStack.cs
- CompiledIdentityConstraint.cs
- Scanner.cs
- HtmlLink.cs
- TrimSurroundingWhitespaceAttribute.cs
- SecUtil.cs
- ServiceReference.cs
- UnsafeNativeMethods.cs
- BypassElementCollection.cs
- GradientBrush.cs
- RIPEMD160Managed.cs
- Faults.cs
- TextBoxAutoCompleteSourceConverter.cs
- ChangeBlockUndoRecord.cs
- SubclassTypeValidator.cs
- HitTestParameters3D.cs
- CodeGotoStatement.cs
- EmissiveMaterial.cs
- ExceptionValidationRule.cs
- StylusPlugInCollection.cs
- WinFormsComponentEditor.cs
- PresentationSource.cs
- VectorAnimationUsingKeyFrames.cs
- ProcessStartInfo.cs
- WizardForm.cs
- TimeManager.cs
- ValueTypeFixupInfo.cs
- FontTypeConverter.cs
- ToolStripRenderer.cs
- TextEditorCopyPaste.cs
- Oid.cs
- SmiEventSink_Default.cs
- BinaryObjectInfo.cs
- DbLambda.cs
- QueryCacheEntry.cs
- ConnectionStringsExpressionBuilder.cs
- XmlElementList.cs
- JsonDeserializer.cs
- ParallelLoopState.cs
- TableRowGroupCollection.cs
- MatrixCamera.cs
- DBAsyncResult.cs
- ByteKeyFrameCollection.cs
- BypassElementCollection.cs
- ProfileInfo.cs
- Int64AnimationUsingKeyFrames.cs
- CodeRegionDirective.cs
- FormatConvertedBitmap.cs
- DBSqlParserTableCollection.cs
- AsmxEndpointPickerExtension.cs
- ProcessProtocolHandler.cs
- TransactionScopeDesigner.cs
- SafeRightsManagementHandle.cs
- TracePayload.cs
- SoapFormatterSinks.cs
- TripleDESCryptoServiceProvider.cs
- PrinterSettings.cs
- RulePatternOps.cs
- CollectionViewSource.cs