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);
            }
        }
 private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int processIndex, int threadIndex, byte[] data)
 {
     Hashtable hashtable = new Hashtable();
     ArrayList list = new ArrayList();
     GCHandle handle = new GCHandle();
     try
     {
         handle = GCHandle.Alloc(data, GCHandleType.Pinned);
         IntPtr ptr = handle.AddrOfPinnedObject();
         Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
         Marshal.PtrToStructure(ptr, structure);
         IntPtr ptr2 = (IntPtr) (((long) ptr) + structure.HeaderLength);
         Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
         Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK perf_counter_block = new Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK();
         for (int j = 0; j < structure.NumObjectTypes; j++)
         {
             Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
             Marshal.PtrToStructure(ptr2, perf_object_type);
             IntPtr ptr3 = (IntPtr) (((long) ptr2) + perf_object_type.DefinitionLength);
             IntPtr ptr4 = (IntPtr) (((long) ptr2) + perf_object_type.HeaderLength);
             ArrayList list2 = new ArrayList();
             for (int k = 0; k < perf_object_type.NumCounters; k++)
             {
                 Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
                 Marshal.PtrToStructure(ptr4, perf_counter_definition);
                 string counterName = library.GetCounterName(perf_counter_definition.CounterNameTitleIndex);
                 if (perf_object_type.ObjectNameTitleIndex == processIndex)
                 {
                     perf_counter_definition.CounterNameTitlePtr = (int) GetValueId(counterName);
                 }
                 else if (perf_object_type.ObjectNameTitleIndex == threadIndex)
                 {
                     perf_counter_definition.CounterNameTitlePtr = (int) GetValueId(counterName);
                 }
                 list2.Add(perf_counter_definition);
                 ptr4 = (IntPtr) (((long) ptr4) + perf_counter_definition.ByteLength);
             }
             Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[] perf_counter_definitionArray = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[list2.Count];
             list2.CopyTo(perf_counter_definitionArray, 0);
             for (int m = 0; m < perf_object_type.NumInstances; m++)
             {
                 Marshal.PtrToStructure(ptr3, perf_instance_definition);
                 IntPtr ptr5 = (IntPtr) (((long) ptr3) + perf_instance_definition.NameOffset);
                 string strA = Marshal.PtrToStringUni(ptr5);
                 if (!strA.Equals("_Total"))
                 {
                     IntPtr ptr6 = (IntPtr) (((long) ptr3) + perf_instance_definition.ByteLength);
                     Marshal.PtrToStructure(ptr6, perf_counter_block);
                     if (perf_object_type.ObjectNameTitleIndex == processIndex)
                     {
                         ProcessInfo info = GetProcessInfo(perf_object_type, (IntPtr) (((long) ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray);
                         if (((info.processId != 0) || (string.Compare(strA, "Idle", StringComparison.OrdinalIgnoreCase) == 0)) && (hashtable[info.processId] == null))
                         {
                             string str3 = strA;
                             if (str3.Length == 15)
                             {
                                 if (strA.EndsWith(".", StringComparison.Ordinal))
                                 {
                                     str3 = strA.Substring(0, 14);
                                 }
                                 else if (strA.EndsWith(".e", StringComparison.Ordinal))
                                 {
                                     str3 = strA.Substring(0, 13);
                                 }
                                 else if (strA.EndsWith(".ex", StringComparison.Ordinal))
                                 {
                                     str3 = strA.Substring(0, 12);
                                 }
                             }
                             info.processName = str3;
                             hashtable.Add(info.processId, info);
                         }
                     }
                     else if (perf_object_type.ObjectNameTitleIndex == threadIndex)
                     {
                         ThreadInfo info2 = GetThreadInfo(perf_object_type, (IntPtr) (((long) ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray);
                         if (info2.threadId != 0)
                         {
                             list.Add(info2);
                         }
                     }
                     ptr3 = (IntPtr) ((((long) ptr3) + perf_instance_definition.ByteLength) + perf_counter_block.ByteLength);
                 }
             }
             ptr2 = (IntPtr) (((long) ptr2) + perf_object_type.TotalByteLength);
         }
     }
     finally
     {
         if (handle.IsAllocated)
         {
             handle.Free();
         }
     }
     for (int i = 0; i < list.Count; i++)
     {
         ThreadInfo info3 = (ThreadInfo) list[i];
         ProcessInfo info4 = (ProcessInfo) hashtable[info3.processId];
         if (info4 != null)
         {
             info4.threadInfoList.Add(info3);
         }
     }
     ProcessInfo[] array = new ProcessInfo[hashtable.Values.Count];
     hashtable.Values.CopyTo(array, 0);
     return array;
 }
Exemple #3
0
        private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int processIndex, int threadIndex, byte[] data)
        {
            Hashtable hashtable = new Hashtable();
            ArrayList list      = new ArrayList();
            GCHandle  handle    = new GCHandle();

            try
            {
                handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                IntPtr ptr = handle.AddrOfPinnedObject();
                Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK();
                Marshal.PtrToStructure(ptr, structure);
                IntPtr ptr2 = (IntPtr)(((long)ptr) + structure.HeaderLength);
                Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION();
                Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK       perf_counter_block       = new Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK();
                for (int j = 0; j < structure.NumObjectTypes; j++)
                {
                    Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE();
                    Marshal.PtrToStructure(ptr2, perf_object_type);
                    IntPtr    ptr3  = (IntPtr)(((long)ptr2) + perf_object_type.DefinitionLength);
                    IntPtr    ptr4  = (IntPtr)(((long)ptr2) + perf_object_type.HeaderLength);
                    ArrayList list2 = new ArrayList();
                    for (int k = 0; k < perf_object_type.NumCounters; k++)
                    {
                        Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION();
                        Marshal.PtrToStructure(ptr4, perf_counter_definition);
                        string counterName = library.GetCounterName(perf_counter_definition.CounterNameTitleIndex);
                        if (perf_object_type.ObjectNameTitleIndex == processIndex)
                        {
                            perf_counter_definition.CounterNameTitlePtr = (int)GetValueId(counterName);
                        }
                        else if (perf_object_type.ObjectNameTitleIndex == threadIndex)
                        {
                            perf_counter_definition.CounterNameTitlePtr = (int)GetValueId(counterName);
                        }
                        list2.Add(perf_counter_definition);
                        ptr4 = (IntPtr)(((long)ptr4) + perf_counter_definition.ByteLength);
                    }
                    Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[] perf_counter_definitionArray = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[list2.Count];
                    list2.CopyTo(perf_counter_definitionArray, 0);
                    for (int m = 0; m < perf_object_type.NumInstances; m++)
                    {
                        Marshal.PtrToStructure(ptr3, perf_instance_definition);
                        IntPtr ptr5 = (IntPtr)(((long)ptr3) + perf_instance_definition.NameOffset);
                        string strA = Marshal.PtrToStringUni(ptr5);
                        if (!strA.Equals("_Total"))
                        {
                            IntPtr ptr6 = (IntPtr)(((long)ptr3) + perf_instance_definition.ByteLength);
                            Marshal.PtrToStructure(ptr6, perf_counter_block);
                            if (perf_object_type.ObjectNameTitleIndex == processIndex)
                            {
                                ProcessInfo info = GetProcessInfo(perf_object_type, (IntPtr)(((long)ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray);
                                if (((info.processId != 0) || (string.Compare(strA, "Idle", StringComparison.OrdinalIgnoreCase) == 0)) && (hashtable[info.processId] == null))
                                {
                                    string str3 = strA;
                                    if (str3.Length == 15)
                                    {
                                        if (strA.EndsWith(".", StringComparison.Ordinal))
                                        {
                                            str3 = strA.Substring(0, 14);
                                        }
                                        else if (strA.EndsWith(".e", StringComparison.Ordinal))
                                        {
                                            str3 = strA.Substring(0, 13);
                                        }
                                        else if (strA.EndsWith(".ex", StringComparison.Ordinal))
                                        {
                                            str3 = strA.Substring(0, 12);
                                        }
                                    }
                                    info.processName = str3;
                                    hashtable.Add(info.processId, info);
                                }
                            }
                            else if (perf_object_type.ObjectNameTitleIndex == threadIndex)
                            {
                                ThreadInfo info2 = GetThreadInfo(perf_object_type, (IntPtr)(((long)ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray);
                                if (info2.threadId != 0)
                                {
                                    list.Add(info2);
                                }
                            }
                            ptr3 = (IntPtr)((((long)ptr3) + perf_instance_definition.ByteLength) + perf_counter_block.ByteLength);
                        }
                    }
                    ptr2 = (IntPtr)(((long)ptr2) + perf_object_type.TotalByteLength);
                }
            }
            finally
            {
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
            for (int i = 0; i < list.Count; i++)
            {
                ThreadInfo  info3 = (ThreadInfo)list[i];
                ProcessInfo info4 = (ProcessInfo)hashtable[info3.processId];
                if (info4 != null)
                {
                    info4.threadInfoList.Add(info3);
                }
            }
            ProcessInfo[] array = new ProcessInfo[hashtable.Values.Count];
            hashtable.Values.CopyTo(array, 0);
            return(array);
        }
 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));
                    }
                }
            }
        }