From 8d16971a6fcd3534c9dd34d625c151d2a5ce3ed9 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Sun, 17 Jul 2022 19:46:34 -0700 Subject: LogManager: use own timestamp function --- Source/Core/Common/Logging/LogManager.cpp | 17 +++++++++++++++-- Source/Core/Common/Logging/LogManager.h | 2 ++ 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 2fa86a11b2..80e7db895e 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -4,6 +4,7 @@ #include "Common/Logging/LogManager.h" #include +#include #include #include #include @@ -11,6 +12,7 @@ #include #include +#include #include #include "Common/CommonPaths.h" @@ -19,7 +21,6 @@ #include "Common/Logging/ConsoleListener.h" #include "Common/Logging/Log.h" #include "Common/StringUtil.h" -#include "Common/Timer.h" namespace Common::Log { @@ -204,11 +205,23 @@ void LogManager::Log(LogLevel level, LogType type, const char* file, int line, c LogWithFullPath(level, type, file + m_path_cutoff_point, line, message); } +std::string LogManager::GetTimestamp() +{ + // NOTE: the Qt LogWidget hardcodes the expected length of the timestamp portion of the log line, + // so ensure they stay in sync + + // We want milliseconds *and not hours*, so can't directly use STL formatters + const auto now = std::chrono::system_clock::now(); + const auto now_s = std::chrono::floor(now); + const auto now_ms = std::chrono::floor(now); + return fmt::format("{:%M:%S}:{:03}", now_s, (now_ms - now_s).count()); +} + void LogManager::LogWithFullPath(LogLevel level, LogType type, const char* file, int line, const char* message) { const std::string msg = - fmt::format("{} {}:{} {}[{}]: {}\n", Common::Timer::GetTimeFormatted(), file, line, + fmt::format("{} {}:{} {}[{}]: {}\n", GetTimestamp(), file, line, LOG_LEVEL_TO_CHAR[static_cast(level)], GetShortName(type), message); for (const auto listener_id : m_listener_ids) diff --git a/Source/Core/Common/Logging/LogManager.h b/Source/Core/Common/Logging/LogManager.h index 070edd82b4..093d31e01d 100644 --- a/Source/Core/Common/Logging/LogManager.h +++ b/Source/Core/Common/Logging/LogManager.h @@ -75,6 +75,8 @@ private: LogManager(LogManager&&) = delete; LogManager& operator=(LogManager&&) = delete; + static std::string GetTimestamp(); + LogLevel m_level; EnumMap m_log{}; std::array m_listeners{}; -- cgit v1.2.3 From 09089eeee0bf6022cc7681214e51546d134402ea Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Sun, 17 Jul 2022 20:43:47 -0700 Subject: Common::Timer: use chrono::steady_clock internally --- Source/Core/Common/Image.cpp | 2 +- Source/Core/Common/Profiler.cpp | 8 +- Source/Core/Common/Timer.cpp | 216 ++++++++++------------------------------ Source/Core/Common/Timer.h | 41 ++++---- 4 files changed, 75 insertions(+), 192 deletions(-) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Image.cpp b/Source/Core/Common/Image.cpp index adca8c1aa7..94e65b9619 100644 --- a/Source/Core/Common/Image.cpp +++ b/Source/Core/Common/Image.cpp @@ -116,7 +116,7 @@ bool SavePNG(const std::string& path, const u8* input, ImageByteFormat format, u size_t image_len = 0; spng_decoded_image_size(ctx.get(), SPNG_FMT_PNG, &image_len); INFO_LOG_FMT(FRAMEDUMP, "{} byte {} by {} image saved to {} at level {} in {}", image_len, width, - height, path, level, timer.GetTimeElapsedFormatted()); + height, path, level, timer.ElapsedMs()); return true; } diff --git a/Source/Core/Common/Profiler.cpp b/Source/Core/Common/Profiler.cpp index 6e35318668..f3903629e6 100644 --- a/Source/Core/Common/Profiler.cpp +++ b/Source/Core/Common/Profiler.cpp @@ -32,7 +32,7 @@ Profiler::Profiler(const std::string& name) : m_name(name), m_usecs(0), m_usecs_min(UINT64_MAX), m_usecs_max(0), m_usecs_quad(0), m_calls(0), m_depth(0) { - m_time = Common::Timer::GetTimeUs(); + m_time = Common::Timer::NowUs(); s_max_length = std::max(s_max_length, u32(m_name.length())); std::lock_guard lk(s_mutex); @@ -64,7 +64,7 @@ std::string Profiler::ToString() if (s_all_profilers.empty()) return ""; - u64 end = Common::Timer::GetTimeUs(); + u64 end = Common::Timer::NowUs(); s_usecs_frame = end - s_frame_time; s_frame_time = end; @@ -101,7 +101,7 @@ void Profiler::Start() { if (!m_depth++) { - m_time = Common::Timer::GetTimeUs(); + m_time = Common::Timer::NowUs(); } } @@ -109,7 +109,7 @@ void Profiler::Stop() { if (!--m_depth) { - u64 end = Common::Timer::GetTimeUs(); + u64 end = Common::Timer::NowUs(); u64 diff = end - m_time; diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index f6d4c983cb..9ac3645841 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -9,13 +9,14 @@ #ifdef _WIN32 #include -#include +#include #include #include #else #include #endif +#include #include #include "Common/CommonTypes.h" @@ -23,157 +24,67 @@ namespace Common { -u32 Timer::GetTimeMs() +template +static typename Clock::rep time_now() { -#ifdef _WIN32 - return timeGetTime(); -#elif defined __APPLE__ - struct timeval t; - (void)gettimeofday(&t, nullptr); - return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000)); -#else - struct timespec t; - (void)clock_gettime(CLOCK_MONOTONIC, &t); - return ((u32)(t.tv_sec * 1000 + t.tv_nsec / 1000000)); -#endif + return std::chrono::time_point_cast(Clock::now()).time_since_epoch().count(); } -#ifdef _WIN32 -double GetFreq() +template +static auto steady_time_now() { - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - return 1000000.0 / double(freq.QuadPart); + return time_now(); } -#endif -u64 Timer::GetTimeUs() +u64 Timer::NowUs() { -#ifdef _WIN32 - LARGE_INTEGER time; - static double freq = GetFreq(); - QueryPerformanceCounter(&time); - return u64(double(time.QuadPart) * freq); -#elif defined __APPLE__ - struct timeval t; - (void)gettimeofday(&t, nullptr); - return ((u64)(t.tv_sec * 1000000 + t.tv_usec)); -#else - struct timespec t; - (void)clock_gettime(CLOCK_MONOTONIC, &t); - return ((u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000)); -#endif + return steady_time_now(); } -// -------------------------------------------- -// Initiate, Start, Stop, and Update the time -// -------------------------------------------- - -// Set initial values for the class -Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false) +u64 Timer::NowMs() { - Update(); + return steady_time_now(); } -// Write the starting time void Timer::Start() { - m_StartTime = GetTimeMs(); - m_Running = true; + m_start_ms = NowMs(); + m_end_ms = 0; + m_running = true; } -// Stop the timer -void Timer::Stop() +void Timer::StartWithOffset(u64 offset) { - // Write the final time - m_LastTime = GetTimeMs(); - m_Running = false; + Start(); + if (m_start_ms > offset) + m_start_ms -= offset; } -// Update the last time variable -void Timer::Update() -{ - m_LastTime = GetTimeMs(); - // TODO(ector) - QPF -} - -// ------------------------------------- -// Get time difference and elapsed time -// ------------------------------------- - -// Get the number of milliseconds since the last Update() -u64 Timer::GetTimeDifference() -{ - return GetTimeMs() - m_LastTime; -} - -// Add the time difference since the last Update() to the starting time. -// This is used to compensate for a paused game. -void Timer::AddTimeDifference() +void Timer::Stop() { - m_StartTime += GetTimeDifference(); + m_end_ms = NowMs(); + m_running = false; } -// Get the time elapsed since the Start() -u64 Timer::GetTimeElapsed() +u64 Timer::ElapsedMs() const { // If we have not started yet, return zero - if (m_StartTime == 0) + if (m_start_ms == 0) return 0; - // Return the final timer time if the timer is stopped - if (!m_Running) - return (m_LastTime - m_StartTime); - - return (GetTimeMs() - m_StartTime); -} - -// Get the formatted time elapsed since the Start() -std::string Timer::GetTimeElapsedFormatted() const -{ - // If we have not started yet, return zero - if (m_StartTime == 0) - return "00:00:00:000"; - - // The number of milliseconds since the start. - // Use a different value if the timer is stopped. - u64 Milliseconds; - if (m_Running) - Milliseconds = GetTimeMs() - m_StartTime; + if (m_running) + { + u64 now = NowMs(); + if (m_start_ms >= now) + return 0; + return now - m_start_ms; + } else - Milliseconds = m_LastTime - m_StartTime; - // Seconds - u32 Seconds = (u32)(Milliseconds / 1000); - // Minutes - u32 Minutes = Seconds / 60; - // Hours - u32 Hours = Minutes / 60; - - return fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60, - Milliseconds % 1000); -} - -// Get current time -void Timer::IncreaseResolution() -{ -#ifdef _WIN32 - timeBeginPeriod(1); -#endif -} - -void Timer::RestoreResolution() -{ -#ifdef _WIN32 - timeEndPeriod(1); -#endif -} - -// Get the number of seconds since January 1 1970 -u64 Timer::GetTimeSinceJan1970() -{ - time_t ltime; - time(<ime); - return ((u64)ltime); + { + if (m_start_ms >= m_end_ms) + return 0; + return m_end_ms - m_start_ms; + } } u64 Timer::GetLocalTimeSinceJan1970() @@ -195,41 +106,7 @@ u64 Timer::GetLocalTimeSinceJan1970() return static_cast(sysTime + tzDiff + tzDST); } -// Return the current time formatted as Minutes:Seconds:Milliseconds -// in the form 00:00:000. -std::string Timer::GetTimeFormatted() -{ - time_t sysTime; - time(&sysTime); - - struct tm* gmTime = localtime(&sysTime); - -#ifdef _WIN32 - wchar_t tmp[13]; - wcsftime(tmp, 6, L"%M:%S", gmTime); -#else - char tmp[13]; - strftime(tmp, 6, "%M:%S", gmTime); -#endif - -// Now tack on the milliseconds -#ifdef _WIN32 - struct timeb tp; - (void)::ftime(&tp); - return WStringToUTF8(tmp) + fmt::format(":{:03}", tp.millitm); -#elif defined __APPLE__ - struct timeval t; - (void)gettimeofday(&t, nullptr); - return fmt::format("{}:{:03}", tmp, t.tv_usec / 1000); -#else - struct timespec t; - (void)clock_gettime(CLOCK_MONOTONIC, &t); - return fmt::format("{}:{:03}", tmp, t.tv_nsec / 1000000); -#endif -} - -// Returns a timestamp with decimals for precise time comparisons -double Timer::GetDoubleTime() +double Timer::GetSystemTimeAsDouble() { // FYI: std::chrono::system_clock epoch is not required to be 1970 until c++20. // We will however assume time_t IS unix time. @@ -244,10 +121,9 @@ double Timer::GetDoubleTime() return std::chrono::duration_cast>(since_double_time_epoch).count(); } -// Formats a timestamp from GetDoubleTime() into a date and time string -std::string Timer::GetDateTimeFormatted(double time) +std::string Timer::SystemTimeAsDoubleToString(double time) { - // revert adjustments from GetDoubleTime() to get a normal Unix timestamp again + // revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET; tm* localTime = localtime(&seconds); @@ -262,4 +138,18 @@ std::string Timer::GetDateTimeFormatted(double time) #endif } +void Timer::IncreaseResolution() +{ +#ifdef _WIN32 + timeBeginPeriod(1); +#endif +} + +void Timer::RestoreResolution() +{ +#ifdef _WIN32 + timeEndPeriod(1); +#endif +} + } // Namespace Common diff --git a/Source/Core/Common/Timer.h b/Source/Core/Common/Timer.h index 614e7b3dfe..160f3a23c9 100644 --- a/Source/Core/Common/Timer.h +++ b/Source/Core/Common/Timer.h @@ -11,43 +11,36 @@ namespace Common class Timer { public: - Timer(); + static u64 NowUs(); + static u64 NowMs(); void Start(); + // Start(), then decrement start time by the offset. + // Effectively "resumes" a timer + void StartWithOffset(u64 offset); void Stop(); - void Update(); + bool IsRunning() const { return m_running; } + u64 ElapsedMs() const; - // The time difference is always returned in milliseconds, regardless of alternative internal - // representation - u64 GetTimeDifference(); - void AddTimeDifference(); + static u64 GetLocalTimeSinceJan1970(); - bool IsRunning() const { return m_Running; } + // Returns biased system timestamp as double + // It is very unlikely you want to use this in new code; ideally we can remove it completely. + static double GetSystemTimeAsDouble(); + // Formats a timestamp from GetSystemTimeAsDouble() into a date and time string + static std::string SystemTimeAsDoubleToString(double time); static void IncreaseResolution(); static void RestoreResolution(); - static u64 GetTimeSinceJan1970(); - static u64 GetLocalTimeSinceJan1970(); - // Returns a timestamp with decimals for precise time comparisons - static double GetDoubleTime(); - - static std::string GetTimeFormatted(); - // Formats a timestamp from GetDoubleTime() into a date and time string - static std::string GetDateTimeFormatted(double time); - std::string GetTimeElapsedFormatted() const; - u64 GetTimeElapsed(); - - static u32 GetTimeMs(); - static u64 GetTimeUs(); - // Arbitrarily chosen value (38 years) that is subtracted in GetDoubleTime() + // Arbitrarily chosen value (38 years) that is subtracted in GetSystemTimeAsDouble() // to increase sub-second precision of the resulting double timestamp static constexpr int DOUBLE_TIME_OFFSET = (38 * 365 * 24 * 60 * 60); private: - u64 m_LastTime; - u64 m_StartTime; - bool m_Running; + u64 m_start_ms{0}; + u64 m_end_ms{0}; + bool m_running{false}; }; } // Namespace Common -- cgit v1.2.3 From 3384b1385edba826f56dec3c92c01426e1fcd48d Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Sun, 17 Jul 2022 20:59:31 -0700 Subject: move "double time" code into State from Timer Ideally the statesave format could be changed to just store a u64 in the future --- Source/Core/Common/Timer.cpp | 43 ++----------------------------------------- Source/Core/Common/Timer.h | 11 ----------- 2 files changed, 2 insertions(+), 52 deletions(-) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index 9ac3645841..ef910f6c27 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -4,23 +4,16 @@ #include "Common/Timer.h" #include -#include #ifdef _WIN32 -#include - #include -#include -#include +#include +#include #else #include #endif -#include -#include - #include "Common/CommonTypes.h" -#include "Common/StringUtil.h" namespace Common { @@ -106,38 +99,6 @@ u64 Timer::GetLocalTimeSinceJan1970() return static_cast(sysTime + tzDiff + tzDST); } -double Timer::GetSystemTimeAsDouble() -{ - // FYI: std::chrono::system_clock epoch is not required to be 1970 until c++20. - // We will however assume time_t IS unix time. - using Clock = std::chrono::system_clock; - - // TODO: Use this on switch to c++20: - // const auto since_epoch = Clock::now().time_since_epoch(); - const auto unix_epoch = Clock::from_time_t({}); - const auto since_epoch = Clock::now() - unix_epoch; - - const auto since_double_time_epoch = since_epoch - std::chrono::seconds(DOUBLE_TIME_OFFSET); - return std::chrono::duration_cast>(since_double_time_epoch).count(); -} - -std::string Timer::SystemTimeAsDoubleToString(double time) -{ - // revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again - time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET; - tm* localTime = localtime(&seconds); - -#ifdef _WIN32 - wchar_t tmp[32] = {}; - wcsftime(tmp, std::size(tmp), L"%x %X", localTime); - return WStringToUTF8(tmp); -#else - char tmp[32] = {}; - strftime(tmp, sizeof(tmp), "%x %X", localTime); - return tmp; -#endif -} - void Timer::IncreaseResolution() { #ifdef _WIN32 diff --git a/Source/Core/Common/Timer.h b/Source/Core/Common/Timer.h index 160f3a23c9..0d357aff2b 100644 --- a/Source/Core/Common/Timer.h +++ b/Source/Core/Common/Timer.h @@ -3,7 +3,6 @@ #pragma once -#include #include "Common/CommonTypes.h" namespace Common @@ -24,19 +23,9 @@ public: static u64 GetLocalTimeSinceJan1970(); - // Returns biased system timestamp as double - // It is very unlikely you want to use this in new code; ideally we can remove it completely. - static double GetSystemTimeAsDouble(); - // Formats a timestamp from GetSystemTimeAsDouble() into a date and time string - static std::string SystemTimeAsDoubleToString(double time); - static void IncreaseResolution(); static void RestoreResolution(); - // Arbitrarily chosen value (38 years) that is subtracted in GetSystemTimeAsDouble() - // to increase sub-second precision of the resulting double timestamp - static constexpr int DOUBLE_TIME_OFFSET = (38 * 365 * 24 * 60 * 60); - private: u64 m_start_ms{0}; u64 m_end_ms{0}; -- cgit v1.2.3 From b473c3587388e06823c153c4fecc2104bcafedff Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 18 Jul 2022 00:20:26 -0700 Subject: windows: mark as HighQoS, ensure timer resolution is honored --- Source/Core/Common/Timer.cpp | 16 ++++++++++++++++ Source/Core/Common/Timer.h | 1 + 2 files changed, 17 insertions(+) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index ef910f6c27..3d2a2ed777 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -102,6 +102,22 @@ u64 Timer::GetLocalTimeSinceJan1970() void Timer::IncreaseResolution() { #ifdef _WIN32 + // Disable execution speed and timer resolution throttling process-wide. + // This mainly will keep Dolphin marked as high performance if it's in the background. The OS + // should make it high performance if it's in the foreground anyway (or for some specific + // threads e.g. audio). + // This is best-effort (i.e. the call may fail on older versions of Windows, where such throttling + // doesn't exist, anyway), and we don't bother reverting once set. + // This adjusts behavior on CPUs with "performance" and "efficiency" cores + PROCESS_POWER_THROTTLING_STATE PowerThrottling{}; + PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION; + PowerThrottling.ControlMask = + PROCESS_POWER_THROTTLING_EXECUTION_SPEED | PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION; + PowerThrottling.StateMask = 0; + SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &PowerThrottling, + sizeof(PowerThrottling)); + + // Not actually sure how useful this is these days.. :') timeBeginPeriod(1); #endif } diff --git a/Source/Core/Common/Timer.h b/Source/Core/Common/Timer.h index 0d357aff2b..cf194e773e 100644 --- a/Source/Core/Common/Timer.h +++ b/Source/Core/Common/Timer.h @@ -21,6 +21,7 @@ public: bool IsRunning() const { return m_running; } u64 ElapsedMs() const; + // The rest of these functions probably belong somewhere else static u64 GetLocalTimeSinceJan1970(); static void IncreaseResolution(); -- cgit v1.2.3 From 86da6c98fbd229d87de7cf8b9c7eb94c16923829 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 18 Jul 2022 01:10:22 -0700 Subject: msvc: use std::chrono for GetLocalTimeSinceJan1970 --- Source/Core/Common/Timer.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index 3d2a2ed777..1b80e02880 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -7,7 +7,6 @@ #ifdef _WIN32 #include -#include #include #else #include @@ -82,6 +81,12 @@ u64 Timer::ElapsedMs() const u64 Timer::GetLocalTimeSinceJan1970() { +#ifdef _MSC_VER + std::chrono::zoned_seconds seconds( + std::chrono::current_zone(), + std::chrono::time_point_cast(std::chrono::system_clock::now())); + return seconds.get_local_time().time_since_epoch().count(); +#else time_t sysTime, tzDiff, tzDST; time(&sysTime); tm* gmTime = localtime(&sysTime); @@ -97,6 +102,7 @@ u64 Timer::GetLocalTimeSinceJan1970() tzDiff = sysTime - mktime(gmTime); return static_cast(sysTime + tzDiff + tzDST); +#endif } void Timer::IncreaseResolution() -- cgit v1.2.3 From fec61f89a3bce9bf857a15ed4789629624e66584 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Mon, 18 Jul 2022 11:48:20 -0700 Subject: Timer: protect usages of ms timers from rollover --- Source/Core/Common/Timer.cpp | 24 +++++------------------- Source/Core/Common/Timer.h | 1 - 2 files changed, 5 insertions(+), 20 deletions(-) (limited to 'Source/Core/Common') diff --git a/Source/Core/Common/Timer.cpp b/Source/Core/Common/Timer.cpp index 1b80e02880..fb81164390 100644 --- a/Source/Core/Common/Timer.cpp +++ b/Source/Core/Common/Timer.cpp @@ -48,8 +48,7 @@ void Timer::Start() void Timer::StartWithOffset(u64 offset) { Start(); - if (m_start_ms > offset) - m_start_ms -= offset; + m_start_ms -= offset; } void Timer::Stop() @@ -60,23 +59,10 @@ void Timer::Stop() u64 Timer::ElapsedMs() const { - // If we have not started yet, return zero - if (m_start_ms == 0) - return 0; - - if (m_running) - { - u64 now = NowMs(); - if (m_start_ms >= now) - return 0; - return now - m_start_ms; - } - else - { - if (m_start_ms >= m_end_ms) - return 0; - return m_end_ms - m_start_ms; - } + const u64 end = m_running ? NowMs() : m_end_ms; + // Can handle up to 1 rollover event (underflow produces correct result) + // If Start() has never been called, will return 0 + return end - m_start_ms; } u64 Timer::GetLocalTimeSinceJan1970() diff --git a/Source/Core/Common/Timer.h b/Source/Core/Common/Timer.h index cf194e773e..7298f4a368 100644 --- a/Source/Core/Common/Timer.h +++ b/Source/Core/Common/Timer.h @@ -18,7 +18,6 @@ public: // Effectively "resumes" a timer void StartWithOffset(u64 offset); void Stop(); - bool IsRunning() const { return m_running; } u64 ElapsedMs() const; // The rest of these functions probably belong somewhere else -- cgit v1.2.3