/// <summary>
        /// Estimates memory consumption
        /// </summary>
        /// <exception cref="InvalidOperationException">Process has exited</exception>
        /// <returns></returns>
        public ProcessMemoryInfo GetMemoryInfo(int pid)
        {
            var handle = IntPtr.Zero;

            try
            {
                handle = ProcessUtility.OpenLimitedQueryInformationProcessHandle(pid);
                EnsureProcessIsRunning(handle, pid);

                if (!PsApi.GetProcessMemoryInfo(handle, out var counters))
                {
                    Win32ExceptionUtility.Throw();
                }

                return(new ProcessMemoryInfo
                {
                    // use PrivateUsage instead of PagefileUsage because PagefileUsage is always zero on Windows 7, Windows Server 2008 R2 and earlier systems
                    PrivateBytes = (long)counters.PrivateUsage,
                    WorkingSetBytes = (long)counters.WorkingSetSize
                });
            }
            finally
            {
                Kernel32.CloseHandle(handle);
            }
        }
コード例 #2
0
        private PERFORMANCE_INFORMATION GetPerformanceInformation()
        {
            if (!PsApi.GetPerformanceInfo(out var info))
            {
                Win32ExceptionUtility.Throw();
            }

            return(info);
        }
コード例 #3
0
        public static ulong GetSystemTime()
        {
            if (Kernel32.GetSystemTimes(out var _, out var kernelTime, out var userTime))
            {
                return(kernelTime.ToULong() + userTime.ToULong());
            }

            Win32ExceptionUtility.Throw();
            // unreachable
            return(0);
        }
コード例 #4
0
        public static ulong GetProcessTime(IntPtr hProcess)
        {
            if (Kernel32.GetProcessTimes(hProcess, out var _, out var _, out var kernelTime, out var userTime))
            {
                return(kernelTime.ToULong() + userTime.ToULong());
            }

            Win32ExceptionUtility.Throw();
            // unreachable
            return(0);
        }
        private static void EnsureProcessIsRunning(IntPtr handle, int pid)
        {
            if (!Kernel32.GetExitCodeProcess(handle, out var exitCode))
            {
                Win32ExceptionUtility.Throw();
            }

            if (exitCode != ProcessExitCodes.STILL_ALIVE)
            {
                ThrowOnProcessExit(pid);
            }
        }
コード例 #6
0
        private MEMORYSTATUSEX GetMemoryStatusEx()
        {
            var memoryStatusExSize = checked ((uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)));
            var result             = new MEMORYSTATUSEX
            {
                dwLength = memoryStatusExSize
            };

            if (!Kernel32.GlobalMemoryStatusEx(ref result))
            {
                Win32ExceptionUtility.Throw();
            }

            return(result);
        }
コード例 #7
0
        /// <summary>
        /// Estimates memory consumption
        /// </summary>
        /// <exception cref="InvalidOperationException">Process has exited</exception>
        /// <returns></returns>
        public ProcessMemoryInfo GetMemoryInfo()
        {
            ProcessUtility.EnsureProcessIsRunning(handle, pid);

            if (!PsApi.GetProcessMemoryInfo(handle, out var counters))
            {
                Win32ExceptionUtility.Throw();
            }

            return(new ProcessMemoryInfo
            {
                // use PrivateUsage instead of PagefileUsage because PagefileUsage is always zero on Windows 7, Windows Server 2008 R2 and earlier systems
                PrivateBytes = (long)counters.PrivateUsage,
                WorkingSetBytes = (long)counters.WorkingSetSize,
            });
        }