Skip to content

Commit

Permalink
Add mem_stat support on Windows.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: cf63e7fe53a00f2fa116472ed12f73492ad8bf29
  • Loading branch information
levlam committed Jun 13, 2020
1 parent c022b1c commit 6b1c2f7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions td/telegram/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion tdutils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 28 additions & 16 deletions tdutils/td/utils/port/Stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...>
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
16 changes: 8 additions & 8 deletions tdutils/td/utils/port/Stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

0 comments on commit 6b1c2f7

Please sign in to comment.