diff options
Diffstat (limited to '')
-rw-r--r-- | src/Stats.cxx | 76 |
1 files changed, 55 insertions, 21 deletions
diff --git a/src/Stats.cxx b/src/Stats.cxx index f224bdf49..940a984da 100644 --- a/src/Stats.cxx +++ b/src/Stats.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,44 +20,71 @@ #include "config.h" #include "Stats.hxx" #include "PlayerControl.hxx" -#include "Client.hxx" -#include "DatabaseSelection.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseSimple.hxx" +#include "client/Client.hxx" +#include "db/Selection.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/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) |