aboutsummaryrefslogtreecommitdiffstats
path: root/src/Stats.cxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Stats.cxx64
1 files changed, 49 insertions, 15 deletions
diff --git a/src/Stats.cxx b/src/Stats.cxx
index f224bdf49..1764516ef 100644
--- a/src/Stats.cxx
+++ b/src/Stats.cxx
@@ -26,38 +26,65 @@
#include "DatabasePlugin.hxx"
#include "DatabaseSimple.hxx"
#include "util/Error.hxx"
+#include "system/Clock.hxx"
#include "Log.hxx"
-#include <glib.h>
+#ifndef WIN32
+/**
+ * The monotonic time stamp when MPD was started. It is used to
+ * calculate the uptime.
+ */
+static unsigned start_time;
+#endif
-static GTimer *uptime;
static DatabaseStats stats;
+enum class StatsValidity : uint8_t {
+ INVALID, VALID, FAILED,
+};
+
+static StatsValidity stats_validity = StatsValidity::INVALID;
+
void stats_global_init(void)
{
- uptime = g_timer_new();
+#ifndef WIN32
+ start_time = MonotonicClockS();
+#endif
}
-void stats_global_finish(void)
+void
+stats_invalidate()
{
- g_timer_destroy(uptime);
+ assert(GetDatabase() != nullptr);
+
+ stats_validity = StatsValidity::INVALID;
}
-void stats_update(void)
+static bool
+stats_update()
{
- assert(GetDatabase() != nullptr);
+ switch (stats_validity) {
+ case StatsValidity::INVALID:
+ break;
- Error error;
+ case StatsValidity::VALID:
+ return true;
+
+ case StatsValidity::FAILED:
+ return false;
+ }
- DatabaseStats stats2;
+ Error error;
const DatabaseSelection selection("", true);
- if (GetDatabase()->GetStats(selection, stats2, error)) {
- stats = stats2;
+ if (GetDatabase()->GetStats(selection, stats, error)) {
+ stats_validity = StatsValidity::VALID;
+ return true;
} else {
LogError(error);
- stats.Clear();
+ stats_validity = StatsValidity::FAILED;
+ return true;
}
}
@@ -71,7 +98,10 @@ db_stats_print(Client &client)
database plugin */
/* TODO: move this into the "proxy" database plugin as
an "idle" handler */
- stats_update();
+ stats_invalidate();
+
+ if (!stats_update())
+ return;
client_printf(client,
"artists: %u\n"
@@ -94,9 +124,13 @@ void
stats_print(Client &client)
{
client_printf(client,
- "uptime: %lu\n"
+ "uptime: %u\n"
"playtime: %lu\n",
- (unsigned long)g_timer_elapsed(uptime, NULL),
+#ifdef WIN32
+ GetProcessUptimeS(),
+#else
+ MonotonicClockS() - start_time,
+#endif
(unsigned long)(client.player_control.GetTotalPlayTime() + 0.5));
if (GetDatabase() != nullptr)