Module: check_mk
Branch: master
Commit: 34d27892aa22919399d7e4de80e7708e4f99a26d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=34d27892aa2291…
Author: Jukka Aro <ja(a)mathias-kettner.de>
Date: Mon Mar 19 15:11:03 2018 +0100
Win-agent: replace WinAPI GetSystemTime
Use std::chrono::system_clock and other utilities in std::chrono
instead.
---
agents/windows/build_version | 2 +-
agents/windows/sections/Section.cc | 8 --------
agents/windows/sections/Section.h | 8 +++++++-
agents/windows/sections/SectionFileinfo.cc | 6 ++----
agents/windows/sections/SectionPS.cc | 7 ++++---
agents/windows/sections/SectionSystemtime.cc | 3 +--
agents/windows/sections/SectionWinperf.cc | 5 +++--
7 files changed, 18 insertions(+), 21 deletions(-)
diff --git a/agents/windows/build_version b/agents/windows/build_version
index 02c434d..19d718c 100644
--- a/agents/windows/build_version
+++ b/agents/windows/build_version
@@ -1 +1 @@
-3190
+3192
diff --git a/agents/windows/sections/Section.cc b/agents/windows/sections/Section.cc
index 41eadd4..ea2380e 100644
--- a/agents/windows/sections/Section.cc
+++ b/agents/windows/sections/Section.cc
@@ -40,14 +40,6 @@ double file_time(const FILETIME *filetime) {
return (double(uli.QuadPart) / WINDOWS_TICK) - SEC_TO_UNIX_EPOCH;
}
-double current_time(const WinApiAdaptor &winapi) {
- SYSTEMTIME systime{0};
- FILETIME filetime{0};
- winapi.GetSystemTime(&systime);
- winapi.SystemTimeToFileTime(&systime, &filetime);
- return file_time(&filetime);
-}
-
} // namespace section_helpers
Section::Section(const std::string &outputName, const std::string &configName,
diff --git a/agents/windows/sections/Section.h b/agents/windows/sections/Section.h
index 92484fa..4e9d273 100644
--- a/agents/windows/sections/Section.h
+++ b/agents/windows/sections/Section.h
@@ -42,7 +42,13 @@ namespace section_helpers {
double file_time(const FILETIME *filetime);
-double current_time(const WinApiAdaptor &winapi);
+template <class ToDuration = std::chrono::seconds, class Rep = long long>
+inline Rep current_time() {
+ const std::chrono::duration<Rep> now =
+ std::chrono::duration_cast<ToDuration>(
+ std::chrono::system_clock::now().time_since_epoch());
+ return now.count();
+}
} // namespace section_helpers
diff --git a/agents/windows/sections/SectionFileinfo.cc
b/agents/windows/sections/SectionFileinfo.cc
index 2f7e5e2..a3a51a1 100644
--- a/agents/windows/sections/SectionFileinfo.cc
+++ b/agents/windows/sections/SectionFileinfo.cc
@@ -36,8 +36,7 @@ SectionFileinfo::SectionFileinfo(Configuration &config, Logger
*logger,
bool SectionFileinfo::produceOutputInner(std::ostream &out,
const std::optional<std::string> &) {
Debug(_logger) << "SectionFileinfo::produceOutputInner";
- out << std::fixed << std::setprecision(0)
- << section_helpers::current_time(_winapi) << "\n";
+ out << std::fixed << section_helpers::current_time() <<
"\n";
for (const std::string &path : *_fileinfo_paths) {
outputFileinfos(out, path.c_str());
@@ -133,8 +132,7 @@ void SectionFileinfo::outputFileinfos(std::ostream &out, const
char *path) {
}
if (!found_file) {
- out << path << "|missing|" <<
section_helpers::current_time(_winapi)
- << "\n";
+ out << path << "|missing|" <<
section_helpers::current_time() << "\n";
}
}
diff --git a/agents/windows/sections/SectionPS.cc b/agents/windows/sections/SectionPS.cc
index 70129dc..df0598c 100644
--- a/agents/windows/sections/SectionPS.cc
+++ b/agents/windows/sections/SectionPS.cc
@@ -199,8 +199,9 @@ bool SectionPS::outputWMI(std::ostream &out) {
std::tm t;
ss >> std::get_time(&t, L"%Y%m%d%H%M%S");
time_t creation_time = mktime(&t);
- auto uptime = (ULONGLONG)(
- (time_t)section_helpers::current_time(_winapi) - creation_time);
+ auto uptime = static_cast<ULONGLONG>(
+ static_cast<time_t>(section_helpers::current_time()) -
+ creation_time);
outputProcess(
out,
std::stoull(result.get<std::string>(L"VirtualSize")),
@@ -309,7 +310,7 @@ bool SectionPS::outputNative(std::ostream &out) {
// Uptime
double ft = section_helpers::file_time(&createTime);
ULONGLONG uptime =
- (ULONGLONG)(section_helpers::current_time(_winapi) - ft);
+ static_cast<ULONGLONG>(section_helpers::current_time() - ft);
// Note: CPU utilization is determined out of usermodetime and
// kernelmodetime
diff --git a/agents/windows/sections/SectionSystemtime.cc
b/agents/windows/sections/SectionSystemtime.cc
index 99d7c81..bbb59c3 100644
--- a/agents/windows/sections/SectionSystemtime.cc
+++ b/agents/windows/sections/SectionSystemtime.cc
@@ -33,7 +33,6 @@ SectionSystemtime::SectionSystemtime(const Environment &env, Logger
*logger,
bool SectionSystemtime::produceOutputInner(std::ostream &out,
const std::optional<std::string> &)
{
Debug(_logger) << "SectionSystemtime::produceOutputInner";
- out << std::fixed << std::setprecision(0)
- << section_helpers::current_time(_winapi);
+ out << std::fixed << section_helpers::current_time();
return true;
}
diff --git a/agents/windows/sections/SectionWinperf.cc
b/agents/windows/sections/SectionWinperf.cc
index c2ca010..cc4d72e 100644
--- a/agents/windows/sections/SectionWinperf.cc
+++ b/agents/windows/sections/SectionWinperf.cc
@@ -50,8 +50,9 @@ bool SectionWinperf::produceOutputInner(std::ostream &out,
LARGE_INTEGER Frequency;
_winapi.QueryPerformanceFrequency(&Frequency);
out << std::fixed << std::setprecision(2)
- << section_helpers::current_time(_winapi) << " "
<< _base << " "
- << Frequency.QuadPart << "\n";
+ << section_helpers::current_time<std::chrono::milliseconds,
+ double>()
+ << " " << _base << " " <<
Frequency.QuadPart << "\n";
std::vector<PERF_INSTANCE_DEFINITION *> instances =
counterObject.instances();