internal static void CheckValidCounterLayout(CounterCreationDataCollection counterData)
        {
            Hashtable hashtable = new Hashtable();

            for (int i = 0; i < counterData.Count; i++)
            {
                if ((counterData[i].CounterName == null) || (counterData[i].CounterName.Length == 0))
                {
                    throw new ArgumentException(SR.GetString("InvalidCounterName"));
                }
                int counterType = (int)counterData[i].CounterType;
                switch (counterType)
                {
                case 0x40020500:
                case 0x22510500:
                case 0x23510500:
                case 0x22410500:
                case 0x23410500:
                case 0x20020400:
                case 0x20c20400:
                case 0x30020400:
                    if (counterData.Count <= (i + 1))
                    {
                        throw new InvalidOperationException(SR.GetString("CounterLayout"));
                    }
                    if (!PerformanceCounterLib.IsBaseCounter((int)counterData[i + 1].CounterType))
                    {
                        throw new InvalidOperationException(SR.GetString("CounterLayout"));
                    }
                    break;

                default:
                    if (PerformanceCounterLib.IsBaseCounter(counterType))
                    {
                        if (i == 0)
                        {
                            throw new InvalidOperationException(SR.GetString("CounterLayout"));
                        }
                        counterType = (int)counterData[i - 1].CounterType;
                        if ((((counterType != 0x40020500) && (counterType != 0x22510500)) && ((counterType != 0x23510500) && (counterType != 0x22410500))) && (((counterType != 0x23410500) && (counterType != 0x20020400)) && ((counterType != 0x20c20400) && (counterType != 0x30020400))))
                        {
                            throw new InvalidOperationException(SR.GetString("CounterLayout"));
                        }
                    }
                    break;
                }
                if (hashtable.ContainsKey(counterData[i].CounterName))
                {
                    throw new ArgumentException(SR.GetString("DuplicateCounterName", new object[] { counterData[i].CounterName }));
                }
                hashtable.Add(counterData[i].CounterName, string.Empty);
                if ((counterData[i].CounterHelp == null) || (counterData[i].CounterHelp.Length == 0))
                {
                    counterData[i].CounterHelp = counterData[i].CounterName;
                }
            }
        }
Esempio n. 2
0
        internal static void CheckValidCounterLayout(CounterCreationDataCollection counterData)
        {
            // Ensure that there are no duplicate counter names being created
            Hashtable h = new Hashtable();

            for (int i = 0; i < counterData.Count; i++)
            {
                if (counterData[i].CounterName == null || counterData[i].CounterName.Length == 0)
                {
                    throw new ArgumentException(SR.InvalidCounterName);
                }

                int currentSampleType = (int)counterData[i].CounterType;
                if ((currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER))
                {
                    if (counterData.Count <= (i + 1))
                    {
                        throw new InvalidOperationException(SR.CounterLayout);
                    }
                    else
                    {
                        currentSampleType = (int)counterData[i + 1].CounterType;


                        if (!PerformanceCounterLib.IsBaseCounter(currentSampleType))
                        {
                            throw new InvalidOperationException(SR.CounterLayout);
                        }
                    }
                }
                else if (PerformanceCounterLib.IsBaseCounter(currentSampleType))
                {
                    if (i == 0)
                    {
                        throw new InvalidOperationException(SR.CounterLayout);
                    }
                    else
                    {
                        currentSampleType = (int)counterData[i - 1].CounterType;

                        if (
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER))
                        {
                            throw new InvalidOperationException(SR.CounterLayout);
                        }
                    }
                }

                if (h.ContainsKey(counterData[i].CounterName))
                {
                    throw new ArgumentException(SR.Format(SR.DuplicateCounterName, counterData[i].CounterName));
                }
                else
                {
                    h.Add(counterData[i].CounterName, string.Empty);

                    // Ensure that all counter help strings aren't null or empty
                    if (counterData[i].CounterHelp == null || counterData[i].CounterHelp.Length == 0)
                    {
                        counterData[i].CounterHelp = counterData[i].CounterName;
                    }
                }
            }
        }
        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));
                    }
                }
            }
        }