public void CounterDefinitionSampleReadsInstanceValueCorrectlyInt64()
        {
            // ARRANGE
            var perfCounter = new NativeMethods.PERF_COUNTER_DEFINITION()
            {
                CounterNameTitleIndex = 0,
                CounterType           = 0,
                CounterOffset         = 6,
                CounterSize           = 8
            };

            var data = new byte[]
            {
                0, 0, 0, 0,
                0, 0, 5, 0,
                0, 0, 0, 0
            };

            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            IntPtr dataRef = handle.AddrOfPinnedObject();

            var sample = new CounterDefinitionSample(perfCounter, -1);

            // ACT
            sample.SetInstanceValue(0, dataRef);

            // ASSERT
            Assert.AreEqual(5, sample.GetInstanceValue(0));

            handle.Free();
        }
        public CategorySampleMock(IList <Tuple <string, long> > procValues) : base(null, 0, 0, null)
        {
            var counterDefinitionSample = new CounterDefinitionSample(new NativeMethods.PERF_COUNTER_DEFINITION(), -1)
            {
                InstanceValues = procValues.Select(v => v.Item2).ToArray()
            };

            this.CounterTable = new Dictionary <int, CounterDefinitionSample>()
            {
                [6] = counterDefinitionSample
            };

            this.InstanceNameTable = new Dictionary <string, int>();

            for (int i = 0; i < procValues.Count; i++)
            {
                this.InstanceNameTable.Add(procValues[i].Item1, i);
            }
        }
        /// <summary>
        /// Gets a collection of <see cref="QuickPulseProcess"/> objects - each corresponding to a system process and containing
        /// information about the amount of time the process has occupied CPU cores.
        /// </summary>
        /// <param name="totalTime">If available, contains the value of the _Total instance of the counter, which indicates the overall
        /// amount of time spent by CPU cores executing system processes.</param>
        public IEnumerable <QuickPulseProcess> GetProcesses(out TimeSpan?totalTime)
        {
            // "Process" object
            const int CategoryIndex = 230;

            // "% Processor Time" counter
            const int CounterIndex = 6;

            const string TotalInstanceName = "_Total";
            const string IdleInstanceName  = "Idle";

            CategorySample          categorySample = this.perfLib.GetCategorySample(CategoryIndex, CounterIndex);
            CounterDefinitionSample counterSample  = categorySample.CounterTable[CounterIndex];

            var procValues = new Dictionary <string, long>();

            foreach (var pair in categorySample.InstanceNameTable)
            {
                string instanceName = pair.Key;
                int    valueIndex   = pair.Value;

                long instanceValue = counterSample.GetInstanceValue(valueIndex);

                procValues.Add(instanceName, instanceValue);
            }

            long overallTime;

            totalTime = procValues.TryGetValue(TotalInstanceName, out overallTime) ? TimeSpan.FromTicks(overallTime) : (TimeSpan?)null;

            return
                (procValues.Where(
                     pv =>
                     !string.Equals(pv.Key, TotalInstanceName, StringComparison.Ordinal) &&
                     !string.Equals(pv.Key, IdleInstanceName, StringComparison.Ordinal))
                 .Select(pv => new QuickPulseProcess(pv.Key, TimeSpan.FromTicks(pv.Value))));
        }