ServicePerformanceCounters.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Diagnostics / ServicePerformanceCounters.cs / 1 / ServicePerformanceCounters.cs

                             
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Diagnostics 
{
    using System.Collections.Generic; 
    using System.ServiceModel.Channels; 
    using System.ServiceModel;
    using System.ComponentModel; 
    using System.Diagnostics;
    using System.IO;
    using System.ServiceModel.Administration;
    using System.ServiceModel.Diagnostics; 
    using System.Globalization;
    using System.Runtime.InteropServices; 
 
    internal class ServicePerformanceCounters : PerformanceCountersBase
    { 
        string instanceName;

        PerformanceCounter[] counters;
 
        enum PerfCounters : int
        { 
            Calls = 0, 
            CallsPerSecond,
            CallsOutstanding, 
            CallsFailed,
            CallsFailedPerSecond,
            CallsFaulted,
            CallsFaultedPerSecond, 
            CallDuration,
            CallDurationBase, 
            SecurityValidationAuthenticationFailures, 
            SecurityValidationAuthenticationFailuresPerSecond,
            CallsNotAuthorized, 
            CallsNotAuthorizedPerSecond,
            Instances,
            InstancesRate,
            RMSessionsFaulted, 
            RMSessionsFaultedPerSecond,
            RMMessagesDropped, 
            RMMessagesDroppedPerSecond, 
            TxFlowed,
            TxFlowedPerSecond, 
            TxCommitted,
            TxCommittedPerSecond,
            TxAborted,
            TxAbortedPerSecond, 
            TxInDoubt,
            TxInDoubtPerSecond, 
            MsmqPoisonMessages, 
            MsmqPoisonMessagesPerSecond,
            MsmqRejectedMessages, 
            MsmqRejectedMessagesPerSecond,
            MsmqDroppedMessages,
            MsmqDroppedMessagesPerSecond,
            TotalCounters = MsmqDroppedMessagesPerSecond + 1 
        }
 
        string[] perfCounterNames = 
        {
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCalls, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallsPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallsOutstanding,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallsFailed,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallsFailedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallsFaulted,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallsFaultedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallDuration, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SCallDurationBase,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SSecurityValidationAuthenticationFailures, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SSecurityValidationAuthenticationFailuresPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SSecurityCallsNotAuthorized,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SSecurityCallsNotAuthorizedPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SInstances, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SInstancesPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SRMSessionsFaulted, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SRMSessionsFaultedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.SRMMessagesDropped,
            PerformanceCounterStrings.SERVICEMODELSERVICE.SRMMessagesDroppedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxFlowed,
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxFlowedPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxCommitted,
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxCommittedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxAborted,
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxAbortedPerSecond, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxInDoubt, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.STxInDoubtPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.MsmqPoisonMessages, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.MsmqPoisonMessagesPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.MsmqRejectedMessages,
            PerformanceCounterStrings.SERVICEMODELSERVICE.MsmqRejectedMessagesPerSecond,
            PerformanceCounterStrings.SERVICEMODELSERVICE.MsmqDroppedMessages, 
            PerformanceCounterStrings.SERVICEMODELSERVICE.MsmqDroppedMessagesPerSecond,
        }; 
 
        const int maxCounterLength = 64;
        const int hashLength = 2; 
        [Flags]
        enum truncOptions : uint
        {
            NoBits = 0, 
            service32 = 0x01,
            uri31 = 0x04 
        } 

        internal override PerformanceCounter[] Counters 
        {
            get
            {
                return this.counters; 
            }
            set 
            { 
                this.counters = value;
            } 
        }

        internal override string InstanceName
        { 
            get
            { 
                return this.instanceName; 
            }
        } 

        internal override string[] CounterNames
        {
            get 
            {
                return this.perfCounterNames; 
            } 
        }
 
        internal override int PerfCounterStart
        {
            get { return (int)PerfCounters.Calls; }
        } 

        internal override int PerfCounterEnd 
        { 
            get { return (int)PerfCounters.TotalCounters; }
        } 

        internal ServicePerformanceCounters(ServiceHostBase serviceHost)
        {
            this.instanceName = ServicePerformanceCounters.CreateFriendlyInstanceName(serviceHost); 

            this.counters = new PerformanceCounter[(int)PerfCounters.TotalCounters]; 
            for (int i = 0; i < (int)PerfCounters.TotalCounters; i++) 
            {
                PerformanceCounter counter = PerformanceCounters.GetServicePerformanceCounter(this.perfCounterNames[i], this.instanceName); 
                if (counter != null)
                {
                    try
                    { 
                        counter.RawValue = 0;
                        this.counters[i] = counter; 
                    } 
#pragma warning suppress 56500 // covered by FxCOP
                    catch (Exception e) 
                    {
                        if (ExceptionUtility.IsFatal(e))
                            throw;
                        if (DiagnosticUtility.ShouldTraceError) 
                            TraceUtility.TraceEvent(TraceEventType.Error,
                                TraceCode.PerformanceCountersFailedForService, 
                                null, e); 
                        break;
                    } 
                }
                else
                {
                    break; 
                }
            } 
        } 

 
        internal static string CreateFriendlyInstanceName(ServiceHostBase serviceHost)
        {
            // instance name is: serviceName.interfaceName.operationName@uri
            ServiceInfo serviceInfo = new ServiceInfo(serviceHost); 
            string serviceName = serviceInfo.ServiceName;
            string uri = serviceInfo.FirstAddress; 
            int length = serviceName.Length + uri.Length + 2; 

            if (length > maxCounterLength) 
            {
                int count = 0;

                truncOptions tasks = ServicePerformanceCounters.GetCompressionTasks( 
                    length, serviceName.Length, uri.Length);
 
                //if necessary, compress service name to 8 chars with a 2 char hash code 
                if ((tasks & truncOptions.service32) > 0)
                { 
                    count = 32;
                    serviceName = GetHashedString(serviceName, count - hashLength, serviceName.Length - count + hashLength, true);
                }
 
                //if necessary,  compress uri to 36 chars with a 2 char hash code
                if ((tasks & truncOptions.uri31) > 0) 
                { 
                    count = 31;
                    uri = GetHashedString(uri, 0, uri.Length - count + hashLength, false); 
                }
            }

            // replace '/' with '|' because perfmon fails when '/' is in perfcounter instance name 
            return serviceName + "@" + uri.Replace('/', '|');
        } 
 
        private static truncOptions GetCompressionTasks(int totalLen, int serviceLen, int uriLen)
        { 
            truncOptions bitmask = 0;

            if (totalLen > maxCounterLength)
            { 
                int workingLen = totalLen;
 
                //note: order of if statements important (see spec)! 
                if (workingLen > maxCounterLength && serviceLen > 32)
                { 
                    bitmask |= truncOptions.service32; //compress service name to 16 chars
                    workingLen -= serviceLen - 32;
                }
                if (workingLen > maxCounterLength && uriLen > 31) 
                {
                    bitmask |= truncOptions.uri31; //compress uri to 31 chars 
                } 
            }
 
            return bitmask;
        }

        internal void MethodCalled() 
        {
            Increment((int)PerfCounters.Calls); 
            Increment((int)PerfCounters.CallsPerSecond); 
            Increment((int)PerfCounters.CallsOutstanding);
        } 

        internal void MethodReturnedSuccess()
        {
            Decrement((int)PerfCounters.CallsOutstanding); 
        }
 
        internal void MethodReturnedError() 
        {
            Increment((int)PerfCounters.CallsFailed); 
            Increment((int)PerfCounters.CallsFailedPerSecond);
            Decrement((int)PerfCounters.CallsOutstanding);
    }
 
        internal void MethodReturnedFault()
        { 
            Increment((int)PerfCounters.CallsFaulted); 
            Increment((int)PerfCounters.CallsFaultedPerSecond);
            Decrement((int)PerfCounters.CallsOutstanding); 
        }


        internal void SaveCallDuration(long time) 
        {
            IncrementBy((int)PerfCounters.CallDuration, time); 
            Increment((int)PerfCounters.CallDurationBase); 
        }
 
        internal void AuthenticationFailed()
        {
            Increment((int)PerfCounters.SecurityValidationAuthenticationFailures);
            Increment((int)PerfCounters.SecurityValidationAuthenticationFailuresPerSecond); 
        }
 
        internal void AuthorizationFailed() 
        {
            Increment((int)PerfCounters.CallsNotAuthorized); 
            Increment((int)PerfCounters.CallsNotAuthorizedPerSecond);
        }

        internal void ServiceInstanceCreated() 
        {
            Increment((int)PerfCounters.Instances); 
            Increment((int)PerfCounters.InstancesRate); 
        }
 
        internal void ServiceInstanceRemoved()
        {
            Decrement((int)PerfCounters.Instances);
        } 

        internal void SessionFaulted() 
        { 
            Increment((int)PerfCounters.RMSessionsFaulted);
            Increment((int)PerfCounters.RMSessionsFaultedPerSecond); 
        }

        internal void MessageDropped()
        { 
            Increment((int)PerfCounters.RMMessagesDropped);
            Increment((int)PerfCounters.RMMessagesDroppedPerSecond); 
        } 

        internal void TxCommitted(long count) 
        {
            IncrementBy((int)PerfCounters.TxCommitted, count);
            IncrementBy((int)PerfCounters.TxCommittedPerSecond, count);
        } 

        internal void TxInDoubt(long count) 
        { 
            IncrementBy((int)PerfCounters.TxInDoubt, count);
            IncrementBy((int)PerfCounters.TxInDoubtPerSecond, count); 
        }

        internal void TxAborted(long count)
        { 
            IncrementBy((int)PerfCounters.TxAborted, count);
            IncrementBy((int)PerfCounters.TxAbortedPerSecond, count); 
        } 

        internal void TxFlowed() 
        {
            Increment((int)PerfCounters.TxFlowed);
            Increment((int)PerfCounters.TxFlowedPerSecond);
        } 

        internal void MsmqDroppedMessage() 
        { 
            Increment((int)PerfCounters.MsmqDroppedMessages);
            Increment((int)PerfCounters.MsmqDroppedMessagesPerSecond); 
        }

        internal void MsmqPoisonMessage()
        { 
            Increment((int)PerfCounters.MsmqPoisonMessages);
            Increment((int)PerfCounters.MsmqPoisonMessagesPerSecond); 
        } 

        internal void MsmqRejectedMessage() 
        {
            Increment((int)PerfCounters.MsmqRejectedMessages);
            Increment((int)PerfCounters.MsmqRejectedMessagesPerSecond);
        } 

        internal bool Initialized 
        { 
            get { return this.counters != null; }
        } 

    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK