diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/mem_info.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/mem_info.cpp')
-rw-r--r-- | util/system/mem_info.cpp | 208 |
1 files changed, 104 insertions, 104 deletions
diff --git a/util/system/mem_info.cpp b/util/system/mem_info.cpp index aa51ae3b16..3b2c5197cb 100644 --- a/util/system/mem_info.cpp +++ b/util/system/mem_info.cpp @@ -5,41 +5,41 @@ #include <util/generic/yexception.h> #include <util/stream/file.h> #include <util/string/cast.h> -#include <util/string/builder.h> -#include "error.h" -#include "info.h" +#include <util/string/builder.h> +#include "error.h" +#include "info.h" #if defined(_unix_) - #include <errno.h> - #include <unistd.h> - #if defined(_freebsd_) - #include <sys/sysctl.h> - #include <sys/types.h> - #include <sys/user.h> - #elif defined(_darwin_) && !defined(_arm_) && !defined(__IOS__) - #include <libproc.h> - #elif defined(__MACH__) && defined(__APPLE__) - #include <mach/mach.h> - #endif + #include <errno.h> + #include <unistd.h> + #if defined(_freebsd_) + #include <sys/sysctl.h> + #include <sys/types.h> + #include <sys/user.h> + #elif defined(_darwin_) && !defined(_arm_) && !defined(__IOS__) + #include <libproc.h> + #elif defined(__MACH__) && defined(__APPLE__) + #include <mach/mach.h> + #endif #elif defined(_win_) - #include <Windows.h> - #include <util/generic/ptr.h> + #include <Windows.h> + #include <util/generic/ptr.h> using NTSTATUS = LONG; - #define STATUS_INFO_LENGTH_MISMATCH 0xC0000004 - #define STATUS_BUFFER_TOO_SMALL 0xC0000023 + #define STATUS_INFO_LENGTH_MISMATCH 0xC0000004 + #define STATUS_BUFFER_TOO_SMALL 0xC0000023 typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING; -typedef struct _CLIENT_ID { +typedef struct _CLIENT_ID { HANDLE UniqueProcess; HANDLE UniqueThread; } CLIENT_ID, *PCLIENT_ID; using KWAIT_REASON = ULONG; -typedef struct _SYSTEM_THREAD_INFORMATION { +typedef struct _SYSTEM_THREAD_INFORMATION { LARGE_INTEGER KernelTime; LARGE_INTEGER UserTime; LARGE_INTEGER CreateTime; @@ -52,7 +52,7 @@ typedef struct _SYSTEM_THREAD_INFORMATION { ULONG ThreadState; KWAIT_REASON WaitReason; } SYSTEM_THREAD_INFORMATION, *PSYSTEM_THREAD_INFORMATION; -typedef struct _SYSTEM_PROCESS_INFORMATION { +typedef struct _SYSTEM_PROCESS_INFORMATION { ULONG NextEntryOffset; ULONG NumberOfThreads; LARGE_INTEGER SpareLi1; @@ -99,16 +99,16 @@ typedef enum _SYSTEM_INFORMATION_CLASS { #endif namespace NMemInfo { - TMemInfo GetMemInfo(pid_t pid) { - TMemInfo result; + TMemInfo GetMemInfo(pid_t pid) { + TMemInfo result; #if defined(_unix_) - #if defined(_linux_) || defined(_freebsd_) || defined(_cygwin_) - const ui32 pagesize = NSystemInfo::GetPageSize(); - #endif + #if defined(_linux_) || defined(_freebsd_) || defined(_cygwin_) + const ui32 pagesize = NSystemInfo::GetPageSize(); + #endif - #if defined(_linux_) || defined(_cygwin_) + #if defined(_linux_) || defined(_cygwin_) TString path; if (!pid) { path = "/proc/self/statm"; @@ -116,47 +116,47 @@ namespace NMemInfo { path = TStringBuilder() << TStringBuf("/proc/") << pid << TStringBuf("/statm"); } const TString stats = TUnbufferedFileInput(path).ReadAll(); - - TStringBuf statsiter(stats); - - result.VMS = FromString<ui64>(statsiter.NextTok(' ')) * pagesize; - result.RSS = FromString<ui64>(statsiter.NextTok(' ')) * pagesize; - - #if defined(_cygwin_) - //cygwin not very accurate - result.VMS = Max(result.VMS, result.RSS); - #endif - #elif defined(_freebsd_) - int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; - size_t size = sizeof(struct kinfo_proc); - - struct kinfo_proc proc; - Zero(proc); - - errno = 0; + + TStringBuf statsiter(stats); + + result.VMS = FromString<ui64>(statsiter.NextTok(' ')) * pagesize; + result.RSS = FromString<ui64>(statsiter.NextTok(' ')) * pagesize; + + #if defined(_cygwin_) + //cygwin not very accurate + result.VMS = Max(result.VMS, result.RSS); + #endif + #elif defined(_freebsd_) + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; + size_t size = sizeof(struct kinfo_proc); + + struct kinfo_proc proc; + Zero(proc); + + errno = 0; if (sysctl((int*)mib, 4, &proc, &size, nullptr, 0) == -1) { - int err = errno; + int err = errno; TString errtxt = LastSystemErrorText(err); - ythrow yexception() << "sysctl({CTL_KERN,KERN_PROC,KERN_PROC_PID,pid},4,proc,&size,NULL,0) returned -1, errno: " << err << " (" << errtxt << ")" << Endl; - } + ythrow yexception() << "sysctl({CTL_KERN,KERN_PROC,KERN_PROC_PID,pid},4,proc,&size,NULL,0) returned -1, errno: " << err << " (" << errtxt << ")" << Endl; + } - result.VMS = proc.ki_size; - result.RSS = proc.ki_rssize * pagesize; - #elif defined(_darwin_) && !defined(_arm_) && !defined(__IOS__) + result.VMS = proc.ki_size; + result.RSS = proc.ki_rssize * pagesize; + #elif defined(_darwin_) && !defined(_arm_) && !defined(__IOS__) if (!pid) { pid = getpid(); } - struct proc_taskinfo taskInfo; - const int r = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo)); + struct proc_taskinfo taskInfo; + const int r = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo)); - if (r != sizeof(taskInfo)) { - int err = errno; + if (r != sizeof(taskInfo)) { + int err = errno; TString errtxt = LastSystemErrorText(err); - ythrow yexception() << "proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo)) returned " << r << ", errno: " << err << " (" << errtxt << ")" << Endl; - } - result.VMS = taskInfo.pti_virtual_size; - result.RSS = taskInfo.pti_resident_size; - #elif defined(__MACH__) && defined(__APPLE__) + ythrow yexception() << "proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskInfo, sizeof(taskInfo)) returned " << r << ", errno: " << err << " (" << errtxt << ")" << Endl; + } + result.VMS = taskInfo.pti_virtual_size; + result.RSS = taskInfo.pti_resident_size; + #elif defined(__MACH__) && defined(__APPLE__) Y_UNUSED(pid); struct mach_task_basic_info taskInfo; mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT; @@ -169,51 +169,51 @@ namespace NMemInfo { } result.VMS = taskInfo.virtual_size; result.RSS = taskInfo.resident_size; - #elif defined(_arm_) + #elif defined(_arm_) Y_UNUSED(pid); - ythrow yexception() << "arm is not supported"; - #endif + ythrow yexception() << "arm is not supported"; + #endif #elif defined(_win_) - if (!pid) { - pid = GetCurrentProcessId(); - } - - NTSTATUS status; - TArrayHolder<char> buffer; - ULONG bufferSize; - - // Query data for all processes and threads in the system. - // This is probably an overkill if the target process is normal not-privileged one, - // but allows to obtain information even about system processes that are not open-able directly. - typedef NTSTATUS(_stdcall * NTQSI_PROC)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG); - NTQSI_PROC NtQuerySystemInformation = (NTQSI_PROC)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "NtQuerySystemInformation"); - bufferSize = 0x4000; - - for (;;) { - buffer.Reset(new char[bufferSize]); - status = NtQuerySystemInformation(SystemProcessInformation, buffer.Get(), bufferSize, &bufferSize); - - if (!status) { - break; - } - - if (status != STATUS_BUFFER_TOO_SMALL && status != STATUS_INFO_LENGTH_MISMATCH) { - ythrow yexception() << "NtQuerySystemInformation failed with status code " << status; - } - } - - SYSTEM_PROCESS_INFORMATION* process = (SYSTEM_PROCESS_INFORMATION*)buffer.Get(); - while (process->UniqueProcessId != (HANDLE)(size_t)(pid)) { - if (!process->NextEntryOffset) { - ythrow yexception() << "GetMemInfo: invalid PID"; - } - - process = (SYSTEM_PROCESS_INFORMATION*)((char*)process + process->NextEntryOffset); - } - - result.VMS = process->VirtualSize; - result.RSS = process->WorkingSetSize; + if (!pid) { + pid = GetCurrentProcessId(); + } + + NTSTATUS status; + TArrayHolder<char> buffer; + ULONG bufferSize; + + // Query data for all processes and threads in the system. + // This is probably an overkill if the target process is normal not-privileged one, + // but allows to obtain information even about system processes that are not open-able directly. + typedef NTSTATUS(_stdcall * NTQSI_PROC)(SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG); + NTQSI_PROC NtQuerySystemInformation = (NTQSI_PROC)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "NtQuerySystemInformation"); + bufferSize = 0x4000; + + for (;;) { + buffer.Reset(new char[bufferSize]); + status = NtQuerySystemInformation(SystemProcessInformation, buffer.Get(), bufferSize, &bufferSize); + + if (!status) { + break; + } + + if (status != STATUS_BUFFER_TOO_SMALL && status != STATUS_INFO_LENGTH_MISMATCH) { + ythrow yexception() << "NtQuerySystemInformation failed with status code " << status; + } + } + + SYSTEM_PROCESS_INFORMATION* process = (SYSTEM_PROCESS_INFORMATION*)buffer.Get(); + while (process->UniqueProcessId != (HANDLE)(size_t)(pid)) { + if (!process->NextEntryOffset) { + ythrow yexception() << "GetMemInfo: invalid PID"; + } + + process = (SYSTEM_PROCESS_INFORMATION*)((char*)process + process->NextEntryOffset); + } + + result.VMS = process->VirtualSize; + result.RSS = process->WorkingSetSize; #endif - return result; - } + return result; + } } |