Exemple #1
0
        public CounterSample NextSample()
        {
            string categoryName = this.categoryName;
            string machineName  = this.machineName;

            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
            this.Initialize();
            CategorySample          categorySample          = PerformanceCounterLib.GetCategorySample(machineName, categoryName);
            CounterDefinitionSample counterDefinitionSample = categorySample.GetCounterDefinitionSample(this.counterName);

            this.counterType = counterDefinitionSample.CounterType;
            if (!categorySample.IsMultiInstance)
            {
                if ((this.instanceName != null) && (this.instanceName.Length != 0))
                {
                    throw new InvalidOperationException(SR.GetString("InstanceNameProhibited", new object[] { this.instanceName }));
                }
                return(counterDefinitionSample.GetSingleValue());
            }
            if ((this.instanceName == null) || (this.instanceName.Length == 0))
            {
                throw new InvalidOperationException(SR.GetString("InstanceNameRequired"));
            }
            return(counterDefinitionSample.GetInstanceValue(this.instanceName));
        }
        // Will cause an update, raw value
        /// <devdoc>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </devdoc>
        public CounterSample NextSample()
        {
            string currentCategoryName = categoryName;
            string currentMachineName  = machineName;

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, currentMachineName, currentCategoryName);

            permission.Demand();

            Initialize();
            CategorySample          categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName);
            CounterDefinitionSample counterSample  = categorySample.GetCounterDefinitionSample(this.counterName);

            this.counterType = counterSample.CounterType;
            if (!categorySample.IsMultiInstance)
            {
                if (instanceName != null && instanceName.Length != 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.InstanceNameProhibited, this.instanceName));
                }

                return(counterSample.GetSingleValue());
            }
            else
            {
                if (instanceName == null || instanceName.Length == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.InstanceNameRequired));
                }

                return(counterSample.GetInstanceValue(this.instanceName));
            }
        }
        // Will cause an update, raw value
        /// <summary>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </summary>
        public CounterSample NextSample()
        {
            string currentCategoryName = _categoryName;
            string currentMachineName  = _machineName;

            Initialize();


            using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(currentMachineName, currentCategoryName))
            {
                CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(_counterName);
                _counterType = counterSample._counterType;
                if (!categorySample._isMultiInstance)
                {
                    if (_instanceName != null && _instanceName.Length != 0)
                    {
                        throw new InvalidOperationException(SR.Format(SR.InstanceNameProhibited, _instanceName));
                    }

                    return(counterSample.GetSingleValue());
                }
                else
                {
                    if (_instanceName == null || _instanceName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.InstanceNameRequired);
                    }

                    return(counterSample.GetInstanceValue(_instanceName));
                }
            }
        }
Exemple #4
0
        // Will cause an update, raw value
        /// <include file='doc\PerformanceCounter.uex' path='docs/doc[@for="PerformanceCounter.NextSample"]/*' />
        /// <devdoc>
        ///     Obtains a counter sample and returns the raw value for it.
        /// </devdoc>
        public CounterSample NextSample()
        {
            Initialize();
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName);

            try {
                CounterDefinitionSample counterSample = categorySample.GetCounterDefinitionSample(this.counterName);
                this.counterType = counterSample.CounterType;
                if (counterSample.IsSingleInstance)
                {
                    if (instanceName != null && instanceName.Length != 0)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.InstanceNameProhibited, this.instanceName));
                    }

                    return(counterSample.GetSingleValue());
                }
                else
                {
                    if (instanceName == null || instanceName.Length == 0)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.InstanceNameRequired));
                    }

                    return(counterSample.GetInstanceValue(this.instanceName));
                }
            }
            finally {
                categorySample.Dispose();
            }
        }
        /// <devdoc>
        ///     Reads all the counter and instance data of this performance category.  Note that reading the entire category
        ///     at once can be as efficient as reading a single counter because of the way the system provides the data.
        /// </devdoc>
        public InstanceDataCollectionCollection ReadCategory()
        {
            if (this.categoryName == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.CategoryNameNotSet));
            }

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(this.machineName, this.categoryName);

            return(categorySample.ReadCategory());
        }
Exemple #6
0
        /// <summary>
        ///     Reads all the counter and instance data of this performance category.  Note that reading the entire category
        ///     at once can be as efficient as reading a single counter because of the way the system provides the data.
        /// </summary>
        public InstanceDataCollectionCollection ReadCategory()
        {
            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.CategoryNameNotSet);
            }

            using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(_machineName, _categoryName))
            {
                return(categorySample.ReadCategory());
            }
        }
        /// <summary>
        ///     Returns true if the instance already exists for this category.
        /// </summary>
        public bool InstanceExists(string instanceName)
        {
            ArgumentNullException.ThrowIfNull(instanceName);

            if (_categoryName == null)
            {
                throw new InvalidOperationException(SR.CategoryNameNotSet);
            }

            using (CategorySample categorySample = PerformanceCounterLib.GetCategorySample(_machineName, _categoryName))
            {
                return(categorySample._instanceNameTable.ContainsKey(instanceName));
            }
        }
Exemple #8
0
 internal CounterDefinitionSample(Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perfCounter, CategorySample categorySample, int instanceNumber)
 {
     this.NameIndex   = perfCounter.CounterNameTitleIndex;
     this.CounterType = perfCounter.CounterType;
     this.offset      = perfCounter.CounterOffset;
     this.size        = perfCounter.CounterSize;
     if (instanceNumber == -1)
     {
         this.instanceValues = new long[1];
     }
     else
     {
         this.instanceValues = new long[instanceNumber];
     }
     this.categorySample = categorySample;
 }
        /// <devdoc>
        ///     Returns true if the instance already exists for this category.
        /// </devdoc>
        public bool InstanceExists(string instanceName)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException("instanceName");
            }

            if (this.categoryName == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.CategoryNameNotSet));
            }

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            return(categorySample.InstanceNameTable.ContainsKey(instanceName));
        }
 internal CounterDefinitionSample(Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perfCounter, CategorySample categorySample, int instanceNumber)
 {
     this.NameIndex = perfCounter.CounterNameTitleIndex;
     this.CounterType = perfCounter.CounterType;
     this.offset = perfCounter.CounterOffset;
     this.size = perfCounter.CounterSize;
     if (instanceNumber == -1)
     {
         this.instanceValues = new long[1];
     }
     else
     {
         this.instanceValues = new long[instanceNumber];
     }
     this.categorySample = categorySample;
 }
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName).Demand();
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            if (categorySample.InstanceNameTable.Count == 0)
            {
                return(new string[0]);
            }
            string[] array = new string[categorySample.InstanceNameTable.Count];
            categorySample.InstanceNameTable.Keys.CopyTo(array, 0);
            if ((array.Length == 1) && (array[0].CompareTo("systemdiagnosticsperfcounterlibsingleinstance") == 0))
            {
                return(new string[0]);
            }
            return(array);
        }
Exemple #12
0
        /// <summary>
        ///     Returns the instance names for a given category
        /// </summary>
        /// <internalonly/>
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            if (categorySample._instanceNameTable.Count == 0)
            {
                return(Array.Empty <string>());
            }

            string[] instanceNames = new string[categorySample._instanceNameTable.Count];
            categorySample._instanceNameTable.Keys.CopyTo(instanceNames, 0);
            if (instanceNames.Length == 1 && instanceNames[0] == PerformanceCounterLib.SingleInstanceName)
            {
                return(Array.Empty <string>());
            }

            return(instanceNames);
        }
Exemple #13
0
        /// <include file='doc\PerformanceCounterCategory.uex' path='docs/doc[@for="PerformanceCounterCategory.GetCounterInstances"]/*' />
        /// <devdoc>
        ///     Returns the instance names for a given category
        /// </devdoc>
        /// <internalonly/>
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            if (categoryName == null)
            {
                throw new ArgumentNullException("categoryName");
            }

            if (categoryName.Length == 0)
            {
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "categoryName", categoryName));
            }

            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "machineName", machineName));
            }

            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Browse, machineName, categoryName);

            permission.Demand();

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            try {
                if (categorySample.InstanceNameTable.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.NoInstanceInformation, categoryName));
                }

                string[] instanceNames = new string[categorySample.InstanceNameTable.Count];
                categorySample.InstanceNameTable.Keys.CopyTo(instanceNames, 0);
                if (instanceNames.Length == 1 && instanceNames[0].CompareTo(PerformanceCounterLib.SingleInstanceName) == 0)
                {
                    return(new string[0]);
                }

                return(instanceNames);
            }
            finally {
                categorySample.Dispose();
            }
        }
        internal static CategorySample GetCategorySample(string machine, string category)
        {
            CategorySample categorySample = GetPerformanceCounterLib(machine, new CultureInfo(9)).GetCategorySample(category);

            if ((categorySample == null) && (CultureInfo.CurrentCulture.Parent.LCID != 9))
            {
                for (CultureInfo info = CultureInfo.CurrentCulture; info != CultureInfo.InvariantCulture; info = info.Parent)
                {
                    categorySample = GetPerformanceCounterLib(machine, info).GetCategorySample(category);
                    if (categorySample != null)
                    {
                        return(categorySample);
                    }
                }
            }
            if (categorySample == null)
            {
                throw new InvalidOperationException(SR.GetString("MissingCategory"));
            }
            return(categorySample);
        }
        /// <devdoc>
        ///     Returns the instance names for a given category
        /// </devdoc>
        /// <internalonly/>
        internal static string[] GetCounterInstances(string categoryName, string machineName)
        {
            PerformanceCounterPermission permission = new PerformanceCounterPermission(PerformanceCounterPermissionAccess.Read, machineName, categoryName);

            permission.Demand();

            CategorySample categorySample = PerformanceCounterLib.GetCategorySample(machineName, categoryName);

            if (categorySample.InstanceNameTable.Count == 0)
            {
                return(new string[0]);
            }

            string[] instanceNames = new string[categorySample.InstanceNameTable.Count];
            categorySample.InstanceNameTable.Keys.CopyTo(instanceNames, 0);
            if (instanceNames.Length == 1 && instanceNames[0].CompareTo(PerformanceCounterLib.SingleInstanceName) == 0)
            {
                return(new string[0]);
            }

            return(instanceNames);
        }
        private CategorySample GetCategorySample(string category) {
            CategoryEntry entry = (CategoryEntry)this.CategoryTable[category];
            if (entry == null)
                return null;

            CategorySample sample = null;
            byte[] dataRef = GetPerformanceData(entry.NameIndex.ToString(CultureInfo.InvariantCulture));
            if (dataRef == null)
                throw new InvalidOperationException(SR.GetString(SR.CantReadCategory, category));

            sample = new CategorySample(dataRef, entry, this);
            return sample;
        }