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()
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()
if (CYGWIN)
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.seek(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") {
Log::set_verbosity_level(to_integer<int>(args));
} 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(MSWSOCK_LIBRARY Mswsock)
# 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()
if (NOT CMAKE_CROSSCOMPILING)
add_dependencies(tdutils tdmime_auto)

View File

@ -10,7 +10,6 @@
#include "td/utils/port/FileFd.h"
#if TD_PORT_POSIX
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
@ -38,8 +37,16 @@
#include <sys/syscall.h>
#endif
#elif TD_PORT_WINDOWS
#ifndef PSAPI_VERSION
#define PSAPI_VERSION 1
#endif
#include <psapi.h>
#endif
namespace td {
#if TD_PORT_POSIX
namespace detail {
template <class...>
@ -176,14 +183,20 @@ Status update_atime(CSlice path) {
};
return detail::update_atime(file.get_native_fd().fd());
}
#endif
Result<Stat> stat(CSlice path) {
#if TD_PORT_POSIX
struct ::stat buf;
int err = detail::skip_eintr([&] { return ::stat(path.c_str(), &buf); });
if (err < 0) {
return OS_ERROR(PSLICE() << "Stat for file \"" << path << "\" failed");
}
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() {
@ -260,6 +273,19 @@ Result<MemStat> mem_stat() {
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;
#else
return Status::Error("Not supported");
@ -302,6 +328,7 @@ Status cpu_stat_self(CpuStat &stat) {
}
return Status::OK();
}
Status cpu_stat_total(CpuStat &stat) {
TRY_RESULT(fd, FileFd::open("/proc/stat", FileFd::Read));
SCOPE_EXIT {
@ -343,20 +370,5 @@ Result<CpuStat> cpu_stat() {
return Status::Error("Not supported");
#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
#endif

View File

@ -32,14 +32,6 @@ struct CpuStat {
};
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 {
uint64 resident_size_ = 0;
uint64 resident_size_peak_ = 0;
@ -49,6 +41,14 @@ struct MemStat {
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
} // namespace td