Example #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));
        }
Example #2
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();
            }
        }
Example #3
0
        // 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));
                }
            }
        }
 internal CounterDefinitionSample GetCounterDefinitionSample(string counter)
 {
     for (int i = 0; i < this.entry.CounterIndexes.Length; i++)
     {
         int    num2 = this.entry.CounterIndexes[i];
         string strA = (string)this.library.NameTable[num2];
         if ((strA != null) && (string.Compare(strA, counter, StringComparison.OrdinalIgnoreCase) == 0))
         {
             CounterDefinitionSample sample = (CounterDefinitionSample)this.CounterTable[num2];
             if (sample != null)
             {
                 return(sample);
             }
             foreach (CounterDefinitionSample sample2 in this.CounterTable.Values)
             {
                 if ((sample2.BaseCounterDefinitionSample != null) && (sample2.BaseCounterDefinitionSample.NameIndex == num2))
                 {
                     return(sample2.BaseCounterDefinitionSample);
                 }
             }
             throw new InvalidOperationException(SR.GetString("CounterLayout"));
         }
     }
     throw new InvalidOperationException(SR.GetString("CantReadCounter", new object[] { counter }));
 }
        // 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));
            }
        }
        internal unsafe string[] GetInstanceNamesFromIndex(int categoryIndex)
        {
            fixed(byte *numRef = this.library.GetPerformanceData(categoryIndex.ToString(CultureInfo.InvariantCulture)))
            {
                IntPtr ptr = new IntPtr((void *)numRef);

                Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
                Marshal.PtrToStructure(ptr, structure);
                ptr = (IntPtr)(((long)ptr) + structure.HeaderLength);
                int numObjectTypes = structure.NumObjectTypes;

                Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = null;
                bool flag = false;

                for (int i = 0; i < numObjectTypes; i++)
                {
                    perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
                    Marshal.PtrToStructure(ptr, perf_object_type);
                    if (perf_object_type.ObjectNameTitleIndex == categoryIndex)
                    {
                        flag = true;
                        break;
                    }
                    ptr = (IntPtr)(((long)ptr) + perf_object_type.TotalByteLength);
                }
                if (!flag)
                {
                    return(new string[0]);
                }
                int numCounters  = perf_object_type.NumCounters;
                int numInstances = perf_object_type.NumInstances;

                ptr = (IntPtr)(((long)ptr) + perf_object_type.HeaderLength);
                if (numInstances == -1)
                {
                    return(new string[0]);
                }
                CounterDefinitionSample[] sampleArray = new CounterDefinitionSample[numCounters];
                for (int j = 0; j < sampleArray.Length; j++)
                {
                    Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
                    Marshal.PtrToStructure(ptr, perf_counter_definition);
                    ptr = (IntPtr)(((long)ptr) + perf_counter_definition.ByteLength);
                }
                string[] strArray = new string[numInstances];
                for (int k = 0; k < numInstances; k++)
                {
                    Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
                    Marshal.PtrToStructure(ptr, perf_instance_definition);
                    strArray[k] = Marshal.PtrToStringUni((IntPtr)(((long)ptr) + perf_instance_definition.NameOffset));
                    ptr         = (IntPtr)(((long)ptr) + perf_instance_definition.ByteLength);
                    ptr         = (IntPtr)(((long)ptr) + Marshal.ReadInt32(ptr));
                }
                return(strArray);
            }
        }
        internal InstanceDataCollectionCollection ReadCategory()
        {
            InstanceDataCollectionCollection collections = new InstanceDataCollectionCollection();

            for (int i = 0; i < this.entry.CounterIndexes.Length; i++)
            {
                int    num2        = this.entry.CounterIndexes[i];
                string counterName = (string)this.library.NameTable[num2];
                if ((counterName != null) && (counterName != string.Empty))
                {
                    CounterDefinitionSample sample = (CounterDefinitionSample)this.CounterTable[num2];
                    if (sample != null)
                    {
                        collections.Add(counterName, sample.ReadInstanceData(counterName));
                    }
                }
            }
            return(collections);
        }
        internal unsafe string[] GetInstanceNamesFromIndex(int categoryIndex) {
            byte[] data = library.GetPerformanceData(categoryIndex.ToString(CultureInfo.InvariantCulture));
            fixed (byte* dataPtr = data) {
                IntPtr dataRef = new IntPtr((void*) dataPtr);

                NativeMethods.PERF_DATA_BLOCK dataBlock = new NativeMethods.PERF_DATA_BLOCK();
                Marshal.PtrToStructure(dataRef, dataBlock);
                dataRef = (IntPtr)((long)dataRef + dataBlock.HeaderLength);
                int numPerfObjects = dataBlock.NumObjectTypes;

                NativeMethods.PERF_OBJECT_TYPE perfObject = null;
                bool foundCategory = false;
                for (int index = 0; index < numPerfObjects; index++) {
                    perfObject = new NativeMethods.PERF_OBJECT_TYPE();
                    Marshal.PtrToStructure(dataRef, perfObject);

                    if (perfObject.ObjectNameTitleIndex == categoryIndex) {
                        foundCategory = true;
                        break;
                    }

                    dataRef = (IntPtr)((long)dataRef + perfObject.TotalByteLength);
                }

                if (!foundCategory)
                    return new string[0];

                int counterNumber = perfObject.NumCounters;
                int instanceNumber = perfObject.NumInstances;
                dataRef = (IntPtr)((long)dataRef + perfObject.HeaderLength);

                if (instanceNumber == -1)
                    return new string[0];

                CounterDefinitionSample[] samples = new CounterDefinitionSample[counterNumber];
                for (int index = 0; index < samples.Length; ++ index) {
                    NativeMethods.PERF_COUNTER_DEFINITION perfCounter = new NativeMethods.PERF_COUNTER_DEFINITION();
                    Marshal.PtrToStructure(dataRef, perfCounter);
                    dataRef = (IntPtr)((long)dataRef + perfCounter.ByteLength);
                }

                string[] instanceNames = new string[instanceNumber];
                for (int i = 0; i < instanceNumber; i++) {
                    NativeMethods.PERF_INSTANCE_DEFINITION perfInstance = new NativeMethods.PERF_INSTANCE_DEFINITION();
                    Marshal.PtrToStructure(dataRef, perfInstance);
                    instanceNames[i] =  Marshal.PtrToStringUni((IntPtr)((long)dataRef + perfInstance.NameOffset));
                    dataRef = (IntPtr)((long)dataRef + perfInstance.ByteLength);
                    dataRef = (IntPtr)((long)dataRef + Marshal.ReadInt32(dataRef));
                }

                return instanceNames;
            }
        }
        internal unsafe CategorySample(byte[] data, CategoryEntry entry, PerformanceCounterLib library) {
            this.entry = entry;
            this.library = library;
            int categoryIndex = entry.NameIndex;
            NativeMethods.PERF_DATA_BLOCK dataBlock = new NativeMethods.PERF_DATA_BLOCK();
            fixed (byte* dataPtr = data) {
                IntPtr dataRef = new IntPtr((void*) dataPtr);

                Marshal.PtrToStructure(dataRef, dataBlock);
                this.SystemFrequency = dataBlock.PerfFreq;
                this.TimeStamp = dataBlock.PerfTime;
                this.TimeStamp100nSec = dataBlock.PerfTime100nSec;
                dataRef = (IntPtr)((long)dataRef + dataBlock.HeaderLength);
                int numPerfObjects = dataBlock.NumObjectTypes;
                if (numPerfObjects == 0) {
                    this.CounterTable = new Hashtable();
                    this.InstanceNameTable = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    return;
                }

                //Need to find the right category, GetPerformanceData might return
                //several of them.
                NativeMethods.PERF_OBJECT_TYPE perfObject = null;
                bool foundCategory = false;
                for (int index = 0; index < numPerfObjects; index++) {
                    perfObject = new NativeMethods.PERF_OBJECT_TYPE();
                    Marshal.PtrToStructure(dataRef, perfObject);

                   if (perfObject.ObjectNameTitleIndex == categoryIndex) {
                        foundCategory = true;
                        break;
                    }

                    dataRef = (IntPtr)((long)dataRef + perfObject.TotalByteLength);
                }

                if (!foundCategory)
                    throw new InvalidOperationException(SR.GetString(SR.CantReadCategoryIndex, categoryIndex.ToString(CultureInfo.CurrentCulture)));

                this.CounterFrequency = perfObject.PerfFreq;
                this.CounterTimeStamp = perfObject.PerfTime;
                int counterNumber = perfObject.NumCounters;
                int instanceNumber = perfObject.NumInstances;

                if (instanceNumber == -1)
                    IsMultiInstance = false;
                else
                    IsMultiInstance = true;
                
                // Move pointer forward to end of PERF_OBJECT_TYPE
                dataRef = (IntPtr)((long)dataRef + perfObject.HeaderLength);

                CounterDefinitionSample[] samples = new CounterDefinitionSample[counterNumber];
                this.CounterTable = new Hashtable(counterNumber);
                for (int index = 0; index < samples.Length; ++ index) {
                    NativeMethods.PERF_COUNTER_DEFINITION perfCounter = new NativeMethods.PERF_COUNTER_DEFINITION();
                    Marshal.PtrToStructure(dataRef, perfCounter);
                    samples[index] = new CounterDefinitionSample(perfCounter, this, instanceNumber);
                    dataRef = (IntPtr)((long)dataRef + perfCounter.ByteLength);

                    int currentSampleType = samples[index].CounterType;
                    if (!PerformanceCounterLib.IsBaseCounter(currentSampleType)) {
                        // We'll put only non-base counters in the table. 
                        if (currentSampleType != NativeMethods.PERF_COUNTER_NODATA)
                            this.CounterTable[samples[index].NameIndex] = samples[index];
                    }
                    else {
                        // it's a base counter, try to hook it up to the main counter. 
                        Debug.Assert(index > 0, "Index > 0 because base counters should never be at index 0");
                        if (index > 0)
                            samples[index-1].BaseCounterDefinitionSample = samples[index];
                    }
                }

                // now set up the InstanceNameTable.  
                if (!IsMultiInstance) {
                    this.InstanceNameTable = new Hashtable(1, StringComparer.OrdinalIgnoreCase);
                    this.InstanceNameTable[PerformanceCounterLib.SingleInstanceName] = 0;

                    for (int index = 0; index < samples.Length; ++ index)  {
                        samples[index].SetInstanceValue(0, dataRef);
                    }
                }
                else {
                    string[] parentInstanceNames = null;
                    this.InstanceNameTable = new Hashtable(instanceNumber, StringComparer.OrdinalIgnoreCase);
                    for (int i = 0; i < instanceNumber; i++) {
                        NativeMethods.PERF_INSTANCE_DEFINITION perfInstance = new NativeMethods.PERF_INSTANCE_DEFINITION();
                        Marshal.PtrToStructure(dataRef, perfInstance);
                        if (perfInstance.ParentObjectTitleIndex > 0 && parentInstanceNames == null)
                            parentInstanceNames = GetInstanceNamesFromIndex(perfInstance.ParentObjectTitleIndex);

                        string instanceName;
                        if (parentInstanceNames != null && perfInstance.ParentObjectInstance >= 0 && perfInstance.ParentObjectInstance < parentInstanceNames.Length - 1)
                            instanceName = parentInstanceNames[perfInstance.ParentObjectInstance] + "/" + Marshal.PtrToStringUni((IntPtr)((long)dataRef + perfInstance.NameOffset));
                        else
                            instanceName = Marshal.PtrToStringUni((IntPtr)((long)dataRef + perfInstance.NameOffset));

                        //In some cases instance names are not unique (Process), same as perfmon
                        //generate a unique name.
                        string newInstanceName = instanceName;
                        int newInstanceNumber = 1;
                        while (true) {
                            if (!this.InstanceNameTable.ContainsKey(newInstanceName)) {
                                this.InstanceNameTable[newInstanceName] = i;
                                break;
                            }
                            else {
                                newInstanceName =  instanceName + "#" + newInstanceNumber.ToString(CultureInfo.InvariantCulture);
                                ++  newInstanceNumber;
                            }
                        }


                        dataRef = (IntPtr)((long)dataRef + perfInstance.ByteLength);
                        for (int index = 0; index < samples.Length; ++ index)
                            samples[index].SetInstanceValue(i, dataRef);

                        dataRef = (IntPtr)((long)dataRef + Marshal.ReadInt32(dataRef));
                    }
                }
            }
        }
 internal unsafe CategorySample(byte[] data, System.Diagnostics.CategoryEntry entry, PerformanceCounterLib library)
 {
     this.entry = entry;
     this.library = library;
     int nameIndex = entry.NameIndex;
     Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
     fixed (byte* numRef = data)
     {
         IntPtr ptr = new IntPtr((void*) numRef);
         Marshal.PtrToStructure(ptr, structure);
         this.SystemFrequency = structure.PerfFreq;
         this.TimeStamp = structure.PerfTime;
         this.TimeStamp100nSec = structure.PerfTime100nSec;
         ptr = (IntPtr) (((long) ptr) + structure.HeaderLength);
         int numObjectTypes = structure.NumObjectTypes;
         if (numObjectTypes == 0)
         {
             this.CounterTable = new Hashtable();
             this.InstanceNameTable = new Hashtable(StringComparer.OrdinalIgnoreCase);
             return;
         }
         Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = null;
         bool flag = false;
         for (int i = 0; i < numObjectTypes; i++)
         {
             perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
             Marshal.PtrToStructure(ptr, perf_object_type);
             if (perf_object_type.ObjectNameTitleIndex == nameIndex)
             {
                 flag = true;
                 break;
             }
             ptr = (IntPtr) (((long) ptr) + perf_object_type.TotalByteLength);
         }
         if (!flag)
         {
             throw new InvalidOperationException(SR.GetString("CantReadCategoryIndex", new object[] { nameIndex.ToString(CultureInfo.CurrentCulture) }));
         }
         this.CounterFrequency = perf_object_type.PerfFreq;
         this.CounterTimeStamp = perf_object_type.PerfTime;
         int numCounters = perf_object_type.NumCounters;
         int numInstances = perf_object_type.NumInstances;
         if (numInstances == -1)
         {
             this.IsMultiInstance = false;
         }
         else
         {
             this.IsMultiInstance = true;
         }
         ptr = (IntPtr) (((long) ptr) + perf_object_type.HeaderLength);
         CounterDefinitionSample[] sampleArray = new CounterDefinitionSample[numCounters];
         this.CounterTable = new Hashtable(numCounters);
         for (int j = 0; j < sampleArray.Length; j++)
         {
             Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
             Marshal.PtrToStructure(ptr, perf_counter_definition);
             sampleArray[j] = new CounterDefinitionSample(perf_counter_definition, this, numInstances);
             ptr = (IntPtr) (((long) ptr) + perf_counter_definition.ByteLength);
             int counterType = sampleArray[j].CounterType;
             if (!PerformanceCounterLib.IsBaseCounter(counterType))
             {
                 if (counterType != 0x40000200)
                 {
                     this.CounterTable[sampleArray[j].NameIndex] = sampleArray[j];
                 }
             }
             else if (j > 0)
             {
                 sampleArray[j - 1].BaseCounterDefinitionSample = sampleArray[j];
             }
         }
         if (!this.IsMultiInstance)
         {
             this.InstanceNameTable = new Hashtable(1, StringComparer.OrdinalIgnoreCase);
             this.InstanceNameTable["systemdiagnosticsperfcounterlibsingleinstance"] = 0;
             for (int k = 0; k < sampleArray.Length; k++)
             {
                 sampleArray[k].SetInstanceValue(0, ptr);
             }
         }
         else
         {
             string[] instanceNamesFromIndex = null;
             this.InstanceNameTable = new Hashtable(numInstances, StringComparer.OrdinalIgnoreCase);
             for (int m = 0; m < numInstances; m++)
             {
                 string str;
                 Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
                 Marshal.PtrToStructure(ptr, perf_instance_definition);
                 if ((perf_instance_definition.ParentObjectTitleIndex > 0) && (instanceNamesFromIndex == null))
                 {
                     instanceNamesFromIndex = this.GetInstanceNamesFromIndex(perf_instance_definition.ParentObjectTitleIndex);
                 }
                 if (((instanceNamesFromIndex != null) && (perf_instance_definition.ParentObjectInstance >= 0)) && (perf_instance_definition.ParentObjectInstance < (instanceNamesFromIndex.Length - 1)))
                 {
                     str = instanceNamesFromIndex[perf_instance_definition.ParentObjectInstance] + "/" + Marshal.PtrToStringUni((IntPtr) (((long) ptr) + perf_instance_definition.NameOffset));
                 }
                 else
                 {
                     str = Marshal.PtrToStringUni((IntPtr) (((long) ptr) + perf_instance_definition.NameOffset));
                 }
                 string key = str;
                 int num10 = 1;
                 while (true)
                 {
                     if (!this.InstanceNameTable.ContainsKey(key))
                     {
                         this.InstanceNameTable[key] = m;
                         break;
                     }
                     key = str + "#" + num10.ToString(CultureInfo.InvariantCulture);
                     num10++;
                 }
                 ptr = (IntPtr) (((long) ptr) + perf_instance_definition.ByteLength);
                 for (int n = 0; n < sampleArray.Length; n++)
                 {
                     sampleArray[n].SetInstanceValue(m, ptr);
                 }
                 ptr = (IntPtr) (((long) ptr) + Marshal.ReadInt32(ptr));
             }
         }
     }
 }
 internal unsafe string[] GetInstanceNamesFromIndex(int categoryIndex)
 {
     fixed (byte* numRef = this.library.GetPerformanceData(categoryIndex.ToString(CultureInfo.InvariantCulture)))
     {
         IntPtr ptr = new IntPtr((void*) numRef);
         Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
         Marshal.PtrToStructure(ptr, structure);
         ptr = (IntPtr) (((long) ptr) + structure.HeaderLength);
         int numObjectTypes = structure.NumObjectTypes;
         Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = null;
         bool flag = false;
         for (int i = 0; i < numObjectTypes; i++)
         {
             perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
             Marshal.PtrToStructure(ptr, perf_object_type);
             if (perf_object_type.ObjectNameTitleIndex == categoryIndex)
             {
                 flag = true;
                 break;
             }
             ptr = (IntPtr) (((long) ptr) + perf_object_type.TotalByteLength);
         }
         if (!flag)
         {
             return new string[0];
         }
         int numCounters = perf_object_type.NumCounters;
         int numInstances = perf_object_type.NumInstances;
         ptr = (IntPtr) (((long) ptr) + perf_object_type.HeaderLength);
         if (numInstances == -1)
         {
             return new string[0];
         }
         CounterDefinitionSample[] sampleArray = new CounterDefinitionSample[numCounters];
         for (int j = 0; j < sampleArray.Length; j++)
         {
             Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
             Marshal.PtrToStructure(ptr, perf_counter_definition);
             ptr = (IntPtr) (((long) ptr) + perf_counter_definition.ByteLength);
         }
         string[] strArray = new string[numInstances];
         for (int k = 0; k < numInstances; k++)
         {
             Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
             Marshal.PtrToStructure(ptr, perf_instance_definition);
             strArray[k] = Marshal.PtrToStringUni((IntPtr) (((long) ptr) + perf_instance_definition.NameOffset));
             ptr = (IntPtr) (((long) ptr) + perf_instance_definition.ByteLength);
             ptr = (IntPtr) (((long) ptr) + Marshal.ReadInt32(ptr));
         }
         return strArray;
     }
 }
        internal unsafe CategorySample(byte[] data, System.Diagnostics.CategoryEntry entry, PerformanceCounterLib library)
        {
            this.entry   = entry;
            this.library = library;
            int nameIndex = entry.NameIndex;

            Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
            fixed(byte *numRef = data)
            {
                IntPtr ptr = new IntPtr((void *)numRef);

                Marshal.PtrToStructure(ptr, structure);
                this.SystemFrequency  = structure.PerfFreq;
                this.TimeStamp        = structure.PerfTime;
                this.TimeStamp100nSec = structure.PerfTime100nSec;
                ptr = (IntPtr)(((long)ptr) + structure.HeaderLength);
                int numObjectTypes = structure.NumObjectTypes;

                if (numObjectTypes == 0)
                {
                    this.CounterTable      = new Hashtable();
                    this.InstanceNameTable = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    return;
                }
                Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = null;
                bool flag = false;

                for (int i = 0; i < numObjectTypes; i++)
                {
                    perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
                    Marshal.PtrToStructure(ptr, perf_object_type);
                    if (perf_object_type.ObjectNameTitleIndex == nameIndex)
                    {
                        flag = true;
                        break;
                    }
                    ptr = (IntPtr)(((long)ptr) + perf_object_type.TotalByteLength);
                }
                if (!flag)
                {
                    throw new InvalidOperationException(SR.GetString("CantReadCategoryIndex", new object[] { nameIndex.ToString(CultureInfo.CurrentCulture) }));
                }
                this.CounterFrequency = perf_object_type.PerfFreq;
                this.CounterTimeStamp = perf_object_type.PerfTime;
                int numCounters  = perf_object_type.NumCounters;
                int numInstances = perf_object_type.NumInstances;

                if (numInstances == -1)
                {
                    this.IsMultiInstance = false;
                }
                else
                {
                    this.IsMultiInstance = true;
                }
                ptr = (IntPtr)(((long)ptr) + perf_object_type.HeaderLength);
                CounterDefinitionSample[] sampleArray = new CounterDefinitionSample[numCounters];
                this.CounterTable = new Hashtable(numCounters);
                for (int j = 0; j < sampleArray.Length; j++)
                {
                    Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
                    Marshal.PtrToStructure(ptr, perf_counter_definition);
                    sampleArray[j] = new CounterDefinitionSample(perf_counter_definition, this, numInstances);
                    ptr            = (IntPtr)(((long)ptr) + perf_counter_definition.ByteLength);
                    int counterType = sampleArray[j].CounterType;
                    if (!PerformanceCounterLib.IsBaseCounter(counterType))
                    {
                        if (counterType != 0x40000200)
                        {
                            this.CounterTable[sampleArray[j].NameIndex] = sampleArray[j];
                        }
                    }
                    else if (j > 0)
                    {
                        sampleArray[j - 1].BaseCounterDefinitionSample = sampleArray[j];
                    }
                }
                if (!this.IsMultiInstance)
                {
                    this.InstanceNameTable = new Hashtable(1, StringComparer.OrdinalIgnoreCase);
                    this.InstanceNameTable["systemdiagnosticsperfcounterlibsingleinstance"] = 0;
                    for (int k = 0; k < sampleArray.Length; k++)
                    {
                        sampleArray[k].SetInstanceValue(0, ptr);
                    }
                }
                else
                {
                    string[] instanceNamesFromIndex = null;
                    this.InstanceNameTable = new Hashtable(numInstances, StringComparer.OrdinalIgnoreCase);
                    for (int m = 0; m < numInstances; m++)
                    {
                        string str;
                        Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
                        Marshal.PtrToStructure(ptr, perf_instance_definition);
                        if ((perf_instance_definition.ParentObjectTitleIndex > 0) && (instanceNamesFromIndex == null))
                        {
                            instanceNamesFromIndex = this.GetInstanceNamesFromIndex(perf_instance_definition.ParentObjectTitleIndex);
                        }
                        if (((instanceNamesFromIndex != null) && (perf_instance_definition.ParentObjectInstance >= 0)) && (perf_instance_definition.ParentObjectInstance < (instanceNamesFromIndex.Length - 1)))
                        {
                            str = instanceNamesFromIndex[perf_instance_definition.ParentObjectInstance] + "/" + Marshal.PtrToStringUni((IntPtr)(((long)ptr) + perf_instance_definition.NameOffset));
                        }
                        else
                        {
                            str = Marshal.PtrToStringUni((IntPtr)(((long)ptr) + perf_instance_definition.NameOffset));
                        }
                        string key   = str;
                        int    num10 = 1;
                        while (true)
                        {
                            if (!this.InstanceNameTable.ContainsKey(key))
                            {
                                this.InstanceNameTable[key] = m;
                                break;
                            }
                            key = str + "#" + num10.ToString(CultureInfo.InvariantCulture);
                            num10++;
                        }
                        ptr = (IntPtr)(((long)ptr) + perf_instance_definition.ByteLength);
                        for (int n = 0; n < sampleArray.Length; n++)
                        {
                            sampleArray[n].SetInstanceValue(m, ptr);
                        }
                        ptr = (IntPtr)(((long)ptr) + Marshal.ReadInt32(ptr));
                    }
                }
            }
        }