diff options
Diffstat (limited to 'src/Stats.cxx')
-rw-r--r-- | src/Stats.cxx | 99 |
1 files changed, 67 insertions, 32 deletions
diff --git a/src/Stats.cxx b/src/Stats.cxx index f224bdf49..8fc626ecb 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,58 +20,84 @@ #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 "Partition.hxx" +#include "Instance.hxx" +#include "db/Selection.hxx" +#include "db/Interface.hxx" +#include "db/Stats.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 + +#ifdef ENABLE_DATABASE -static GTimer *uptime; static DatabaseStats stats; +enum class StatsValidity : uint8_t { + INVALID, VALID, FAILED, +}; + +static StatsValidity stats_validity = StatsValidity::INVALID; + +#endif + void stats_global_init(void) { - uptime = g_timer_new(); +#ifndef WIN32 + start_time = MonotonicClockS(); +#endif } -void stats_global_finish(void) +#ifdef ENABLE_DATABASE + +void +stats_invalidate() { - g_timer_destroy(uptime); + stats_validity = StatsValidity::INVALID; } -void stats_update(void) +static bool +stats_update(const Database &db) { - 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 (db.GetStats(selection, stats, error)) { + stats_validity = StatsValidity::VALID; + return true; } else { LogError(error); - stats.Clear(); + stats_validity = StatsValidity::FAILED; + return false; } } static void -db_stats_print(Client &client) +db_stats_print(Client &client, const Database &db) { - assert(GetDatabase() != nullptr); - - if (!db_is_simple()) - /* reload statistics if we're using the "proxy" - database plugin */ - /* TODO: move this into the "proxy" database plugin as - an "idle" handler */ - stats_update(); + if (!stats_update(db)) + return; client_printf(client, "artists: %u\n" @@ -83,22 +109,31 @@ db_stats_print(Client &client) stats.song_count, stats.total_duration); - const time_t update_stamp = GetDatabase()->GetUpdateStamp(); + const time_t update_stamp = db.GetUpdateStamp(); if (update_stamp > 0) client_printf(client, "db_update: %lu\n", (unsigned long)update_stamp); } +#endif + 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) - db_stats_print(client); +#ifdef ENABLE_DATABASE + const Database *db = client.partition.instance.database; + if (db != nullptr) + db_stats_print(client, *db); +#endif } |