diff --git a/toolsrc/include/vcpkg_Chrono.h b/toolsrc/include/vcpkg_Chrono.h index e4ae121b3..a9d1bbbed 100644 --- a/toolsrc/include/vcpkg_Chrono.h +++ b/toolsrc/include/vcpkg_Chrono.h @@ -10,7 +10,7 @@ namespace vcpkg public: static ElapsedTime createStarted(); - constexpr ElapsedTime() :m_startTick() {} + constexpr ElapsedTime() : m_startTick() {} template TimeUnit elapsed() const @@ -18,42 +18,14 @@ namespace vcpkg return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - this->m_startTick); } - std::string toString() const; - - private: - std::chrono::steady_clock::time_point m_startTick; - }; - - class Stopwatch - { - public: - static Stopwatch createUnstarted(); - - static Stopwatch createStarted(); - - bool isRunning() const; - - const Stopwatch& start(); - - const Stopwatch& stop(); - - Stopwatch& reset(); - - template - TimeUnit elapsed() const + double microseconds() const { - return std::chrono::duration_cast(elapsedNanos()); + return elapsed>().count(); } std::string toString() const; private: - Stopwatch(); - - std::chrono::nanoseconds elapsedNanos() const; - - bool m_isRunning; - std::chrono::nanoseconds m_elapsedNanos; - std::chrono::steady_clock::time_point m_startTick; + std::chrono::high_resolution_clock::time_point m_startTick; }; } diff --git a/toolsrc/include/vcpkg_System.h b/toolsrc/include/vcpkg_System.h index 7634034ab..a414ba0d4 100644 --- a/toolsrc/include/vcpkg_System.h +++ b/toolsrc/include/vcpkg_System.h @@ -84,15 +84,6 @@ namespace vcpkg::System return println(c, Strings::format(messageTemplate, messageArgs...).c_str()); } - struct Stopwatch2 - { - int64_t start_time, end_time, freq; - - void start(); - void stop(); - double microseconds() const; - }; - optional get_environmental_variable(const wchar_t* varname) noexcept; void set_environmental_variable(const wchar_t* varname, const wchar_t* varvalue) noexcept; diff --git a/toolsrc/src/commands_build.cpp b/toolsrc/src/commands_build.cpp index e51d519f8..02b98e28d 100644 --- a/toolsrc/src/commands_build.cpp +++ b/toolsrc/src/commands_build.cpp @@ -6,6 +6,7 @@ #include "PostBuildLint.h" #include "vcpkg_Dependencies.h" #include "vcpkg_System.h" +#include "vcpkg_Chrono.h" #include "vcpkg_Environment.h" #include "metrics.h" #include "vcpkg_Enums.h" @@ -53,11 +54,11 @@ namespace vcpkg::Commands::Build port_dir.generic_wstring(), ports_cmake_script_path.generic_wstring()); - System::Stopwatch2 timer; - timer.start(); + ElapsedTime timer = ElapsedTime::createStarted(); + int return_code = System::cmd_execute(command); - timer.stop(); - TrackMetric("buildtimeus-" + spec.toString(), timer.microseconds()); + auto buildtimeus = timer.microseconds(); + TrackMetric("buildtimeus-" + spec.toString(), buildtimeus); if (return_code != 0) { diff --git a/toolsrc/src/vcpkg.cpp b/toolsrc/src/vcpkg.cpp index a263be8bc..e94d2538b 100644 --- a/toolsrc/src/vcpkg.cpp +++ b/toolsrc/src/vcpkg.cpp @@ -13,6 +13,7 @@ #include "vcpkg_Input.h" #include "Paragraphs.h" #include "vcpkg_Strings.h" +#include "vcpkg_Chrono.h" using namespace vcpkg; @@ -153,8 +154,6 @@ static void loadConfig() } } -static System::Stopwatch2 g_timer; - static std::string trim_path_from_command_line(const std::string& full_command_line) { Checks::check_exit(full_command_line.size() > 0, "Internal failure - cannot have empty command line"); @@ -175,16 +174,18 @@ static std::string trim_path_from_command_line(const std::string& full_command_l return std::string(it, full_command_line.cend()); } +static ElapsedTime g_timer; + int wmain(const int argc, const wchar_t* const* const argv) { if (argc == 0) std::abort(); - g_timer.start(); + g_timer = ElapsedTime::createStarted(); atexit([]() { - g_timer.stop(); - TrackMetric("elapsed_us", g_timer.microseconds()); + auto elapsed_us = g_timer.microseconds(); + TrackMetric("elapsed_us", elapsed_us); Flush(); }); diff --git a/toolsrc/src/vcpkg_Chrono.cpp b/toolsrc/src/vcpkg_Chrono.cpp index 1bcb439be..e39842df9 100644 --- a/toolsrc/src/vcpkg_Chrono.cpp +++ b/toolsrc/src/vcpkg_Chrono.cpp @@ -60,60 +60,4 @@ namespace vcpkg { return format_time_userfriendly(elapsed()); } - - Stopwatch Stopwatch::createUnstarted() - { - return Stopwatch(); - } - - Stopwatch Stopwatch::createStarted() - { - return Stopwatch().start(); - } - - bool Stopwatch::isRunning() const - { - return this->m_isRunning; - } - - const Stopwatch& Stopwatch::start() - { - Checks::check_exit(!this->m_isRunning, "This stopwatch is already running."); - this->m_isRunning = true; - this->m_startTick = std::chrono::high_resolution_clock::now(); - return *this; - } - - const Stopwatch& Stopwatch::stop() - { - auto tick = std::chrono::high_resolution_clock::now(); - Checks::check_exit(this->m_isRunning, "This stopwatch is already stopped."); - this->m_isRunning = false; - this->m_elapsedNanos += tick - this->m_startTick; - return *this; - } - - Stopwatch& Stopwatch::reset() - { - this->m_elapsedNanos = std::chrono::nanoseconds(); - this->m_isRunning = false; - return *this; - } - - std::string Stopwatch::toString() const - { - return format_time_userfriendly(this->elapsedNanos()); - } - - Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(0), m_startTick() { } - - std::chrono::nanoseconds Stopwatch::elapsedNanos() const - { - if (this->m_isRunning) - { - return std::chrono::high_resolution_clock::now() - this->m_startTick + this->m_elapsedNanos; - } - - return this->m_elapsedNanos; - } } diff --git a/toolsrc/src/vcpkg_System.cpp b/toolsrc/src/vcpkg_System.cpp index 9c849e8d8..679318768 100644 --- a/toolsrc/src/vcpkg_System.cpp +++ b/toolsrc/src/vcpkg_System.cpp @@ -96,23 +96,4 @@ namespace vcpkg::System { _wputenv_s(varname, varvalue); } - - void Stopwatch2::start() - { - static_assert(sizeof(start_time) == sizeof(LARGE_INTEGER), ""); - - QueryPerformanceCounter(reinterpret_cast(&start_time)); - } - - void Stopwatch2::stop() - { - QueryPerformanceCounter(reinterpret_cast(&end_time)); - QueryPerformanceFrequency(reinterpret_cast(&freq)); - } - - double Stopwatch2::microseconds() const - { - return (reinterpret_cast(&end_time)->QuadPart - - reinterpret_cast(&start_time)->QuadPart) * 1000000.0 / reinterpret_cast(&freq)->QuadPart; - } }