Add mem_stat support on Windows.

GitOrigin-RevId: cf63e7fe53a00f2fa116472ed12f73492ad8bf29
This commit is contained in:
levlam 2020-06-14 02:31:57 +03:00
parent c022b1c5a1
commit 6b1c2f797d
5 changed files with 47 additions and 26 deletions

View File

@ -189,7 +189,7 @@ elseif (INTEL)
endif() endif()
if (WIN32) if (WIN32)
add_definitions(-DNTDDI_VERSION=0x06020000 -DWINVER=0x0602 -D_WIN32_WINNT=0x0602 -DNOMINMAX -DUNICODE -D_UNICODE -DWIN32_LEAN_AND_MEAN) add_definitions(-DNTDDI_VERSION=0x06020000 -DWINVER=0x0602 -D_WIN32_WINNT=0x0602 -DPSAPI_VERSION=1 -DNOMINMAX -DUNICODE -D_UNICODE -DWIN32_LEAN_AND_MEAN)
endif() endif()
if (CYGWIN) if (CYGWIN)
add_definitions(-D_DEFAULT_SOURCE=1 -DFD_SETSIZE=4096) add_definitions(-D_DEFAULT_SOURCE=1 -DFD_SETSIZE=4096)

View File

@ -4060,6 +4060,15 @@ class CliClient final : public Actor {
fd.write("a").ignore(); fd.write("a").ignore();
fd.seek(size).ignore(); fd.seek(size).ignore();
fd.truncate_to_current_position(size).ignore(); fd.truncate_to_current_position(size).ignore();
} else if (op == "mem") {
new int[1000000];
auto r_mem_stats = mem_stat();
if (r_mem_stats.is_error()) {
LOG(ERROR) << r_mem_stats.error();
} else {
auto stats = r_mem_stats.move_as_ok();
LOG(ERROR) << stats.resident_size_ << " " << stats.resident_size_peak_ << " " << stats.virtual_size_ << " " << stats.virtual_size_peak_;
}
} else if (op == "SetVerbosity" || op == "SV") { } else if (op == "SetVerbosity" || op == "SV") {
Log::set_verbosity_level(to_integer<int>(args)); Log::set_verbosity_level(to_integer<int>(args));
} else if (op[0] == 'v' && op[1] == 'v') { } else if (op[0] == 'v' && op[1] == 'v') {

View File

@ -291,7 +291,7 @@ if (WIN32)
# find_library(WS2_32_LIBRARY ws2_32) # find_library(WS2_32_LIBRARY ws2_32)
# find_library(MSWSOCK_LIBRARY Mswsock) # find_library(MSWSOCK_LIBRARY Mswsock)
# target_link_libraries(tdutils PRIVATE ${WS2_32_LIBRARY} ${MSWSOCK_LIBRARY}) # target_link_libraries(tdutils PRIVATE ${WS2_32_LIBRARY} ${MSWSOCK_LIBRARY})
target_link_libraries(tdutils PRIVATE ws2_32 Mswsock Normaliz) target_link_libraries(tdutils PRIVATE ws2_32 Mswsock Normaliz psapi)
endif() endif()
if (NOT CMAKE_CROSSCOMPILING) if (NOT CMAKE_CROSSCOMPILING)
add_dependencies(tdutils tdmime_auto) add_dependencies(tdutils tdmime_auto)

View File

@ -10,7 +10,6 @@
#include "td/utils/port/FileFd.h" #include "td/utils/port/FileFd.h"
#if TD_PORT_POSIX #if TD_PORT_POSIX
#include "td/utils/format.h" #include "td/utils/format.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
@ -38,8 +37,16 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#endif #endif
#elif TD_PORT_WINDOWS
#ifndef PSAPI_VERSION
#define PSAPI_VERSION 1
#endif
#include <psapi.h>
#endif
namespace td { namespace td {
#if TD_PORT_POSIX
namespace detail { namespace detail {
template <class...> template <class...>
@ -176,14 +183,20 @@ Status update_atime(CSlice path) {
}; };
return detail::update_atime(file.get_native_fd().fd()); return detail::update_atime(file.get_native_fd().fd());
} }
#endif
Result<Stat> stat(CSlice path) { Result<Stat> stat(CSlice path) {
#if TD_PORT_POSIX
struct ::stat buf; struct ::stat buf;
int err = detail::skip_eintr([&] { return ::stat(path.c_str(), &buf); }); int err = detail::skip_eintr([&] { return ::stat(path.c_str(), &buf); });
if (err < 0) { if (err < 0) {
return OS_ERROR(PSLICE() << "Stat for file \"" << path << "\" failed"); return OS_ERROR(PSLICE() << "Stat for file \"" << path << "\" failed");
} }
return detail::from_native_stat(buf); return detail::from_native_stat(buf);
#elif TD_PORT_WINDOWS
TRY_RESULT(fd, FileFd::open(path, FileFd::Flags::Read | FileFd::PrivateFlags::WinStat));
return fd.stat();
#endif
} }
Result<MemStat> mem_stat() { Result<MemStat> mem_stat() {
@ -260,6 +273,19 @@ Result<MemStat> mem_stat() {
s++; s++;
} }
return res;
#elif TD_WINDOWS
PROCESS_MEMORY_COUNTERS_EX counters;
if (!GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS *>(&counters),
sizeof(counters))) {
return Status::Error("Call to GetProcessMemoryInfo failed");
}
MemStat res;
res.resident_size_ = counters.WorkingSetSize;
res.resident_size_peak_ = counters.PeakWorkingSetSize;
res.virtual_size_ = counters.PrivateUsage;
res.virtual_size_peak_ = counters.PeakPagefileUsage;
return res; return res;
#else #else
return Status::Error("Not supported"); return Status::Error("Not supported");
@ -302,6 +328,7 @@ Status cpu_stat_self(CpuStat &stat) {
} }
return Status::OK(); return Status::OK();
} }
Status cpu_stat_total(CpuStat &stat) { Status cpu_stat_total(CpuStat &stat) {
TRY_RESULT(fd, FileFd::open("/proc/stat", FileFd::Read)); TRY_RESULT(fd, FileFd::open("/proc/stat", FileFd::Read));
SCOPE_EXIT { SCOPE_EXIT {
@ -343,20 +370,5 @@ Result<CpuStat> cpu_stat() {
return Status::Error("Not supported"); return Status::Error("Not supported");
#endif #endif
} }
} // namespace td
#endif
#if TD_PORT_WINDOWS
namespace td {
Result<Stat> stat(CSlice path) {
TRY_RESULT(fd, FileFd::open(path, FileFd::Flags::Read | FileFd::PrivateFlags::WinStat));
return fd.stat();
}
Result<CpuStat> cpu_stat() {
return Status::Error("Not supported");
}
} // namespace td } // namespace td
#endif

View File

@ -32,14 +32,6 @@ struct CpuStat {
}; };
Result<CpuStat> cpu_stat() TD_WARN_UNUSED_RESULT; Result<CpuStat> cpu_stat() TD_WARN_UNUSED_RESULT;
#if TD_PORT_POSIX
namespace detail {
Result<Stat> fstat(int native_fd);
} // namespace detail
Status update_atime(CSlice path) TD_WARN_UNUSED_RESULT;
struct MemStat { struct MemStat {
uint64 resident_size_ = 0; uint64 resident_size_ = 0;
uint64 resident_size_peak_ = 0; uint64 resident_size_peak_ = 0;
@ -49,6 +41,14 @@ struct MemStat {
Result<MemStat> mem_stat() TD_WARN_UNUSED_RESULT; Result<MemStat> mem_stat() TD_WARN_UNUSED_RESULT;
#if TD_PORT_POSIX
namespace detail {
Result<Stat> fstat(int native_fd);
} // namespace detail
Status update_atime(CSlice path) TD_WARN_UNUSED_RESULT;
#endif #endif
} // namespace td } // namespace td