Exemple #1
0
        public static unsafe void ReadRawValues(
            this PdhCounter counter,
            ArrayHolder arrayHolder,
            InstancesCounter instancesCounter,
            List <Sample> samples,
            out int size)
        {
            samples.Clear();
            instancesCounter.Clear();
            while (true)
            {
                size = counter.EstimateRawCounterArraySize();
                if (size == 0)
                {
                    return;
                }
                var buffer = arrayHolder.Get(size);
                fixed(byte *b = buffer)
                {
                    var ptr    = (PDH_RAW_COUNTER_ITEM *)b;
                    var status = counter.GetRawCounterArray(ref size, out var items, ptr);

                    if (status == PdhStatus.PDH_CSTATUS_INVALID_DATA)
                    {
                        return;
                    }
                    if (status == PdhStatus.PDH_MORE_DATA)
                    {
                        continue;
                    }

                    status.EnsureSuccess(nameof(PdhExports.PdhGetFormattedCounterArray));

                    if (items * sizeof(PDH_RAW_COUNTER_ITEM) > buffer.Length)
                    {
                        throw new InvalidOperationException($"Buffer overflow check failed. ItemCount: {items}, ItemSize: {sizeof(PDH_FMT_COUNTERVALUE_ITEM)}, BufferSize: {buffer.Length}");
                    }

                    for (var i = 0; i < items; ++i)
                    {
                        var current      = ptr + i;
                        var instanceName = new string(current->Name); // TODO: avoid allocation here
                        samples.Add(
                            new Sample
                        {
                            Instance = instanceName,
                            Value    = current->RawValue.FirstValue,
                            Id       = instancesCounter.GetAndIncrement(instanceName)
                        });
                    }

                    break;
                }
            }
        }
Exemple #2
0
 public static extern PdhStatus PdhCalculateCounterFromRawValue(
     PdhCounter hCounter,
     PdhFmt dwFormat,
     ref PDH_RAW_COUNTER rawValue1,
     ref PDH_RAW_COUNTER rawValue2,
     out PDH_FMT_COUNTERVALUE fmtValue);
Exemple #3
0
 public static extern PdhStatus PdhGetRawCounterValue(
     PdhCounter counter,
     out int lpdwType,
     out PDH_RAW_COUNTER pValue);
Exemple #4
0
 public static extern unsafe PdhStatus PdhGetFormattedCounterArray(
     PdhCounter counter,
     PdhFmt dwFormat,
     ref int bufferSize,
     out int itemCount,
     PDH_FMT_COUNTERVALUE_ITEM *buffer);
Exemple #5
0
 public static extern PdhStatus PdhGetFormattedCounterValue(
     PdhCounter counter,
     PdhFmt dwFormat,
     out int lpdwType,
     out PDH_FMT_COUNTERVALUE pValue);
Exemple #6
0
 public static extern unsafe PdhStatus PdhGetRawCounterArray(
     PdhCounter counter,
     ref int bufferSize,
     out int itemCount,
     PDH_RAW_COUNTER_ITEM *buffer);
Exemple #7
0
 public static extern PdhStatus PdhAddEnglishCounter(
     PdhQueryHandle query,
     string path,
     IntPtr userData,
     out PdhCounter counter);