Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace Kontur.Core.Metrics
- {
- internal static unsafe class InstanceNameUtility
- {
- public static string ObtainInstanceNameForProcessSlow(int pid, string processName)
- {
- var category = new PerformanceCounterCategory("Process");
- foreach (var instance in category.GetInstanceNames().Where(name => name.StartsWith(processName)))
- {
- using (var counter = new System.Diagnostics.PerformanceCounter("Process", "ID Process", instance, true))
- {
- var counterProcessId = (int)counter.RawValue;
- if (counterProcessId == pid)
- return instance;
- }
- }
- return processName;
- }
- public static string ObtainInstanceNameForProcess(int pid, string processName)
- {
- const uint pdhMoreData = 0x800007d2;
- uint result;
- IntPtr query;
- if ((result = PdhOpenQuery(null, IntPtr.Zero, out query)) != 0)
- FailWithError(result, "PdhOpenQuery");
- try
- {
- IntPtr counter;
- if ((result = PdhAddCounter(query, @"\Process(" + processName + @"*)\ID Process", IntPtr.Zero, out counter)) != 0)
- FailWithError(result, "PdhAddCounter");
- if ((result = PdhCollectQueryData(query)) != 0)
- FailWithError(result, "PdhCollectQueryData");
- while (true)
- {
- int bufferSize = 0;
- int itemCount;
- if ((result = PdhGetRawCounterArray(counter, ref bufferSize, out itemCount, null)) != pdhMoreData)
- FailWithError(result, "PdhGetRawCounterArray");
- var buffer = new byte[bufferSize];
- fixed (byte* ptr = buffer)
- {
- result = PdhGetRawCounterArray(counter, ref bufferSize, out itemCount, ptr);
- if (result == pdhMoreData)
- continue;
- if (result != 0)
- FailWithError(result, "PdhGetRawCounterArray");
- var instanceIndex = 0;
- for (int i = 0; i < itemCount; i++)
- {
- var currentPid = ((PdhRawCounterItem*)ptr)[i].RawValue.FirstValue;
- var currentName = new string(((PdhRawCounterItem*)ptr)[i].Name);
- if (currentName != processName)
- continue;
- if (currentPid == pid)
- return instanceIndex == 0 ? processName : processName + "#" + instanceIndex;
- instanceIndex++;
- }
- }
- throw new ArgumentException("Failed to find instance name for PID " + pid);
- }
- }
- finally
- {
- PdhCloseQuery(query);
- }
- }
- private static void FailWithError(uint error, string function)
- {
- throw new Exception(String.Format("{0} failed with code {1:x8}", function, error));
- }
- [DllImport("pdh.dll")]
- private static extern uint PdhOpenQuery(string dataSource, IntPtr userData, out IntPtr query);
- [DllImport("pdh.dll")]
- private static extern uint PdhCloseQuery(IntPtr query);
- [DllImport("pdh.dll")]
- private static extern uint PdhAddCounter(IntPtr query, string path, IntPtr userData, out IntPtr counter);
- [DllImport("pdh.dll")]
- private static extern uint PdhCollectQueryData(IntPtr query);
- [DllImport("pdh.dll")]
- private static extern uint PdhGetRawCounterArray(IntPtr counter, ref int bufferSize, out int itemCount, byte* buffer);
- [StructLayout(LayoutKind.Sequential)]
- private struct PdhRawCounterItem
- {
- public readonly sbyte* Name;
- public readonly PdhRawCounter RawValue;
- }
- [StructLayout(LayoutKind.Sequential)]
- private struct PdhRawCounter
- {
- public readonly int CStatus;
- public readonly long TimeStamp;
- public readonly long FirstValue;
- public readonly long SecondValue;
- public readonly int MultiCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement