[vcpkg] Trap Ctrl-C, enable thread safety for global data structures
MODIFY
include/metrics.h
include/metrics.h
#include <string>
+#include "vcpkg_Util.h"
+
namespace vcpkg::Metrics
+{
+ struct Metrics : Util::ResourceBase
{
void set_send_metrics(bool should_send_metrics);
void set_print_metrics(bool should_print_metrics);
@@ -12,9 +16,13 @@ namespace vcpkg::Metrics
void track_metric(const std::string& name, double value);
void track_property(const std::string& name, const std::string& value);
void track_property(const std::string& name, const std::wstring& value);
- bool get_compiled_metrics_enabled();
- std::wstring get_SQM_user();
void upload(const std::string& payload);
void flush();
+ };
+
+ extern Util::LockGuarded<Metrics> g_metrics;
+
+ std::wstring get_SQM_user();
+ bool get_compiled_metrics_enabled();
}

MODIFY
include/pch.h
include/pch.h
#include <algorithm>
#include <array>
+#include <atomic>
#include <cassert>
#include <cctype>
#include <chrono>
#include <iterator>
#include <map>
#include <memory>
+#include <mutex>
#include <process.h>
#include <regex>
#include <set>

MODIFY
include/vcpkg_GlobalState.h
include/vcpkg_GlobalState.h
#pragma once
-#include <vcpkg_Chrono.h>
+#include <atomic>
+
+#include "vcpkg_Chrono.h"
+#include "vcpkg_Util.h"
namespace vcpkg
{
struct GlobalState
{
- static ElapsedTime timer;
- static bool debugging;
- static bool feature_packages;
+ static Util::LockGuarded<ElapsedTime> timer;
+ static std::atomic<bool> debugging;
+ static std::atomic<bool> feature_packages;
};
}
\ No newline at end of file

MODIFY
include/vcpkg_Util.h
include/vcpkg_Util.h
#pragma once
#include <map>
+#include <mutex>
#include <utility>
#include <vector>
@@ -95,4 +96,34 @@ namespace vcpkg::Util
ResourceBase& operator=(const ResourceBase&) = delete;
ResourceBase& operator=(ResourceBase&&) = delete;
};
+
+ template<class T>
+ struct LockGuardPtr;
+
+ template<class T>
+ struct LockGuarded
+ {
+ friend struct LockGuardPtr<T>;
+
+ LockGuardPtr<T> lock() { return *this; }
+
+ private:
+ std::mutex m_mutex;
+ T m_t;
+ };
+
+ template<class T>
+ struct LockGuardPtr
+ {
+ T& operator*() { return m_ptr; }
+ T* operator->() { return &m_ptr; }
+
+ T* get() { return &m_ptr; }
+
+ LockGuardPtr(LockGuarded<T>& sync) : m_lock(sync.m_mutex), m_ptr(sync.m_t) {}
+
+ private:
+ std::unique_lock<std::mutex> m_lock;
+ T& m_ptr;
+ };
}
\ No newline at end of file

MODIFY
src/VcpkgCmdArguments.cpp
src/VcpkgCmdArguments.cpp
@@ -16,7 +16,7 @@ namespace vcpkg
if (arg_begin == arg_end)
{
System::println(System::Color::error, "Error: expected value after %s", option_name);
- Metrics::track_property("error", "error option name");
+ Metrics::g_metrics.lock()->track_property("error", "error option name");
Commands::Help::print_usage();
Checks::exit_fail(VCPKG_LINE_INFO);
}
@@ -24,7 +24,7 @@ namespace vcpkg
if (option_field != nullptr)
{
System::println(System::Color::error, "Error: %s specified multiple times", option_name);
- Metrics::track_property("error", "error option specified multiple times");
+ Metrics::g_metrics.lock()->track_property("error", "error option specified multiple times");
Commands::Help::print_usage();
Checks::exit_fail(VCPKG_LINE_INFO);
}
@@ -37,7 +37,7 @@ namespace vcpkg
if (option_field && option_field != new_setting)
{
System::println(System::Color::error, "Error: conflicting values specified for --%s", option_name);
- Metrics::track_property("error", "error conflicting switches");
+ Metrics::g_metrics.lock()->track_property("error", "error conflicting switches");
Commands::Help::print_usage();
Checks::exit_fail(VCPKG_LINE_INFO);
}
@@ -71,7 +71,7 @@ namespace vcpkg
if (arg[0] == '-' && arg[1] != '-')
{
- Metrics::track_property("error", "error short options are not supported");
+ Metrics::g_metrics.lock()->track_property("error", "error short options are not supported");
Checks::exit_with_message(VCPKG_LINE_INFO, "Error: short options are not supported: %s", arg);
}

MODIFY
src/VcpkgPaths.cpp
src/VcpkgPaths.cpp
@@ -87,8 +87,11 @@ namespace vcpkg
"(No sufficient installed version was found)",
Strings::to_utf8(tool_name),
version_as_string);
- Metrics::track_property("error", "powershell install failed");
- Metrics::track_property("installcmd", install_cmd);
+ {
+ auto locked_metrics = Metrics::g_metrics.lock();
+ locked_metrics->track_property("error", "powershell install failed");
+ locked_metrics->track_property("installcmd", install_cmd);
+ }
Checks::exit_with_code(VCPKG_LINE_INFO, rc.exit_code);
}
@@ -186,7 +189,7 @@ namespace vcpkg
if (paths.root.empty())
{
- Metrics::track_property("error", "Invalid vcpkg root directory");
+ Metrics::g_metrics.lock()->track_property("error", "Invalid vcpkg root directory");
Checks::exit_with_message(VCPKG_LINE_INFO, "Invalid vcpkg root directory: %s", paths.root.string());
}

MODIFY
src/commands_install.cpp
src/commands_install.cpp
@@ -514,7 +514,8 @@ namespace vcpkg::Commands::Install
return "R$" + raction->spec.to_string();
Checks::unreachable(VCPKG_LINE_INFO);
});
- Metrics::track_property("installplan", specs_string);
+
+ Metrics::g_metrics.lock()->track_property("installplan", specs_string);
print_plan(action_plan, is_recursive);

MODIFY
src/metrics.cpp
src/metrics.cpp
namespace vcpkg::Metrics
{
+ Util::LockGuarded<Metrics> g_metrics;
+
static std::string get_current_date_time()
{
struct tm newtime;
@@ -223,25 +225,25 @@ namespace vcpkg::Metrics
return hkcu_sqmclient.value_or(L"{}");
}
- void set_user_information(const std::string& user_id, const std::string& first_use_time)
+ void Metrics::set_user_information(const std::string& user_id, const std::string& first_use_time)
{
g_metricmessage.user_id = user_id;
g_metricmessage.user_timestamp = first_use_time;
}
- void init_user_information(std::string& user_id, std::string& first_use_time)
+ void Metrics::init_user_information(std::string& user_id, std::string& first_use_time)
{
user_id = generate_random_UUID();
first_use_time = get_current_date_time();
}
- void set_send_metrics(bool should_send_metrics) { g_should_send_metrics = should_send_metrics; }
+ void Metrics::set_send_metrics(bool should_send_metrics) { g_should_send_metrics = should_send_metrics; }
- void set_print_metrics(bool should_print_metrics) { g_should_print_metrics = should_print_metrics; }
+ void Metrics::set_print_metrics(bool should_print_metrics) { g_should_print_metrics = should_print_metrics; }
- void track_metric(const std::string& name, double value) { g_metricmessage.TrackMetric(name, value); }
+ void Metrics::track_metric(const std::string& name, double value) { g_metricmessage.TrackMetric(name, value); }
- void track_property(const std::string& name, const std::wstring& value)
+ void Metrics::track_property(const std::string& name, const std::wstring& value)
{
// Note: this is not valid UTF-16 -> UTF-8, it just yields a close enough approximation for our purposes.
std::string converted_value;
@@ -252,12 +254,12 @@ namespace vcpkg::Metrics
g_metricmessage.TrackProperty(name, converted_value);
}
- void track_property(const std::string& name, const std::string& value)
+ void Metrics::track_property(const std::string& name, const std::string& value)
{
g_metricmessage.TrackProperty(name, value);
}
- void upload(const std::string& payload)
+ void Metrics::upload(const std::string& payload)
{
HINTERNET hSession = nullptr, hConnect = nullptr, hRequest = nullptr;
BOOL bResults = FALSE;
@@ -349,7 +351,7 @@ namespace vcpkg::Metrics
return fs::path(buf, buf + bytes);
}
- void flush()
+ void Metrics::flush()
{
std::string payload = g_metricmessage.format_event_data_template();
if (g_should_print_metrics) std::cerr << payload << "\n";

MODIFY
src/vcpkg.cpp
src/vcpkg.cpp
@@ -27,7 +27,7 @@ void invalid_command(const std::string& cmd)
static void inner(const VcpkgCmdArguments& args)
{
- Metrics::track_property("command", args.command);
+ Metrics::g_metrics.lock()->track_property("command", args.command);
if (args.command.empty())
{
Commands::Help::print_usage();
@@ -135,7 +135,7 @@ static void loadConfig()
auto user_time = keys["User-Since"];
if (!user_id.empty() && !user_time.empty())
{
- Metrics::set_user_information(user_id, user_time);
+ Metrics::g_metrics.lock()->set_user_information(user_id, user_time);
return;
}
}
@@ -146,8 +146,11 @@ static void loadConfig()
// config file not found, could not be read, or invalid
std::string user_id, user_time;
- Metrics::init_user_information(user_id, user_time);
- Metrics::set_user_information(user_id, user_time);
+ {
+ auto locked_metrics = Metrics::g_metrics.lock();
+ locked_metrics->init_user_information(user_id, user_time);
+ locked_metrics->set_user_information(user_id, user_time);
+ }
try
{
std::error_code ec;
@@ -189,23 +192,28 @@ int wmain(const int argc, const wchar_t* const* const argv)
{
if (argc == 0) std::abort();
- GlobalState::timer = ElapsedTime::create_started();
+ *GlobalState::timer.lock() = ElapsedTime::create_started();
// Checks::register_console_ctrl_handler();
- Metrics::track_property("version", Commands::Version::version());
-
const std::string trimmed_command_line = trim_path_from_command_line(Strings::to_utf8(GetCommandLineW()));
- Metrics::track_property("cmdline", trimmed_command_line);
+
+ {
+ auto locked_metrics = Metrics::g_metrics.lock();
+ locked_metrics->track_property("version", Commands::Version::version());
+ locked_metrics->track_property("cmdline", trimmed_command_line);
+ }
loadConfig();
- Metrics::track_property("sqmuser", Metrics::get_SQM_user());
+ Metrics::g_metrics.lock()->track_property("sqmuser", Metrics::get_SQM_user());
const VcpkgCmdArguments args = VcpkgCmdArguments::create_from_command_line(argc, argv);
- if (auto p = args.printmetrics.get()) Metrics::set_print_metrics(*p);
- if (auto p = args.sendmetrics.get()) Metrics::set_send_metrics(*p);
+ if (auto p = args.printmetrics.get()) Metrics::g_metrics.lock()->set_print_metrics(*p);
+ if (auto p = args.sendmetrics.get()) Metrics::g_metrics.lock()->set_send_metrics(*p);
if (auto p = args.debug.get()) GlobalState::debugging = *p;
+ vcpkg::Checks::register_console_ctrl_handler();
+
if (GlobalState::debugging)
{
inner(args);
@@ -226,7 +234,7 @@ int wmain(const int argc, const wchar_t* const* const argv)
{
exc_msg = "unknown error(...)";
}
- Metrics::track_property("error", exc_msg);
+ Metrics::g_metrics.lock()->track_property("error", exc_msg);
fflush(stdout);
System::print("vcpkg.exe has crashed.\n"

MODIFY
src/vcpkg_Build.cpp
src/vcpkg_Build.cpp
@@ -193,14 +193,17 @@ namespace vcpkg::Build
int return_code = System::cmd_execute_clean(command);
auto buildtimeus = timer.microseconds();
const auto spec_string = spec.to_string();
- Metrics::track_metric("buildtimeus-" + spec_string, buildtimeus);
+ {
+ auto locked_metrics = Metrics::g_metrics.lock();
+ locked_metrics->track_metric("buildtimeus-" + spec_string, buildtimeus);
if (return_code != 0)
{
- Metrics::track_property("error", "build failed");
- Metrics::track_property("build_error", spec_string);
+ locked_metrics->track_property("error", "build failed");
+ locked_metrics->track_property("build_error", spec_string);
return {BuildResult::BUILD_FAILED, {}};
}
+ }
auto build_info = read_build_info(paths.get_filesystem(), paths.build_info_file_path(spec));
const size_t error_count = PostBuildLint::perform_all_checks(spec, paths, pre_build_info, build_info);

MODIFY
src/vcpkg_Checks.cpp
src/vcpkg_Checks.cpp
@@ -10,17 +10,23 @@ namespace vcpkg::Checks
{
[[noreturn]] static void cleanup_and_exit(const int exit_code)
{
- auto elapsed_us = GlobalState::timer.microseconds();
- Metrics::track_metric("elapsed_us", elapsed_us);
+ auto elapsed_us = GlobalState::timer.lock()->microseconds();
+
+ auto metrics = Metrics::g_metrics.lock();
+ metrics->track_metric("elapsed_us", elapsed_us);
GlobalState::debugging = false;
- Metrics::flush();
+ metrics->flush();
::exit(exit_code);
}
static BOOL CtrlHandler(DWORD fdwCtrlType)
{
- Metrics::track_metric("SignalCaptured", fdwCtrlType);
+ {
+ auto locked_metrics = Metrics::g_metrics.lock();
+ locked_metrics->track_property("CtrlHandler", std::to_string(fdwCtrlType));
+ locked_metrics->track_property("error", "CtrlHandler was fired.");
+ }
cleanup_and_exit(EXIT_FAILURE);
}

MODIFY
src/vcpkg_GlobalState.cpp
src/vcpkg_GlobalState.cpp
namespace vcpkg
{
- ElapsedTime GlobalState::timer;
- bool GlobalState::debugging = false;
- bool GlobalState::feature_packages = false;
+ Util::LockGuarded<ElapsedTime> GlobalState::timer;
+ std::atomic<bool> GlobalState::debugging = false;
+ std::atomic<bool> GlobalState::feature_packages = false;
}

MODIFY
src/vcpkg_Input.cpp
src/vcpkg_Input.cpp
@@ -29,7 +29,7 @@ namespace vcpkg::Input
if (!paths.is_valid_triplet(t))
{
System::println(System::Color::error, "Error: invalid triplet: %s", t);
- Metrics::track_property("error", "invalid triplet: " + t.to_string());
+ Metrics::g_metrics.lock()->track_property("error", "invalid triplet: " + t.to_string());
Commands::Help::help_topic_valid_triplet(paths);
Checks::exit_fail(VCPKG_LINE_INFO);
}

MODIFY
src/vcpkg_metrics_uploader.cpp
src/vcpkg_metrics_uploader.cpp
@@ -11,5 +11,6 @@ int WINAPI WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int)
LPWSTR* szArgList = CommandLineToArgvW(GetCommandLineW(), &argCount);
Checks::check_exit(VCPKG_LINE_INFO, argCount == 2, "Requires exactly one argument, the path to the payload file");
- Metrics::upload(Files::get_real_filesystem().read_contents(szArgList[1]).value_or_exit(VCPKG_LINE_INFO));
+ auto v = Files::get_real_filesystem().read_contents(szArgList[1]).value_or_exit(VCPKG_LINE_INFO);
+ Metrics::g_metrics.lock()->upload(v);
}

MODIFY
src/vcpkglib.cpp
src/vcpkglib.cpp
@@ -117,7 +117,7 @@ namespace vcpkg
if (!was_tracked)
{
was_tracked = true;
- Metrics::track_property("listfile", "update to new format");
+ Metrics::g_metrics.lock()->track_property("listfile", "update to new format");
}
// The files are sorted such that directories are placed just before the files they contain

[vcpkg] Trap Ctrl-C, enable thread safety for global data structures