2007-11-21 20:03:53 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2007-11-21 20:03:53 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-11-21 20:03:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "platform.h" /* tr_sessionGetConfigDir() */
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "stats.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "utils.h" /* tr_buildPath */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-06-24 10:17:04 +00:00
|
|
|
struct tr_session_stats const TR_SESSION_STATS_INIT =
|
|
|
|
{
|
2019-03-17 04:07:48 +00:00
|
|
|
.ratio = 0.0F,
|
2017-06-24 10:17:04 +00:00
|
|
|
.uploadedBytes = 0,
|
|
|
|
.downloadedBytes = 0,
|
|
|
|
.filesAdded = 0,
|
|
|
|
.sessionCount = 0,
|
|
|
|
.secondsActive = 0
|
|
|
|
};
|
2008-11-21 16:53:58 +00:00
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Opaque, per-session data structure for bandwidth use statistics */
|
2007-11-26 18:55:36 +00:00
|
|
|
struct tr_stats_handle
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session_stats single;
|
|
|
|
tr_session_stats old;
|
|
|
|
time_t startTime;
|
|
|
|
bool isDirty;
|
2007-11-26 18:55:36 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* getOldFilename(tr_session const* session)
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_buildPath(tr_sessionGetConfigDir(session), "stats.benc", NULL);
|
2008-04-18 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* getFilename(tr_session const* session)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_buildPath(tr_sessionGetConfigDir(session), "stats.json", NULL);
|
2008-06-12 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void loadCumulativeStats(tr_session const* session, tr_session_stats* setme)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
char* filename;
|
|
|
|
bool loaded = false;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
filename = getFilename(session);
|
|
|
|
loaded = tr_variantFromFile(&top, TR_VARIANT_FMT_JSON, filename, NULL);
|
|
|
|
tr_free(filename);
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!loaded)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
filename = getOldFilename(session);
|
|
|
|
loaded = tr_variantFromFile(&top, TR_VARIANT_FMT_BENC, filename, NULL);
|
|
|
|
tr_free(filename);
|
2008-06-12 16:25:36 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (loaded)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t i;
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_downloaded_bytes, &i))
|
|
|
|
{
|
|
|
|
setme->downloadedBytes = (uint64_t)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_files_added, &i))
|
|
|
|
{
|
|
|
|
setme->filesAdded = (uint64_t)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_seconds_active, &i))
|
|
|
|
{
|
|
|
|
setme->secondsActive = (uint64_t)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_session_count, &i))
|
|
|
|
{
|
|
|
|
setme->sessionCount = (uint64_t)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(&top, TR_KEY_uploaded_bytes, &i))
|
|
|
|
{
|
|
|
|
setme->uploadedBytes = (uint64_t)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variantFree(&top);
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveCumulativeStats(tr_session const* session, tr_session_stats const* s)
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* filename;
|
|
|
|
tr_variant top;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(&top, 5);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_downloaded_bytes, s->downloadedBytes);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_files_added, s->filesAdded);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_seconds_active, s->secondsActive);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_session_count, s->sessionCount);
|
|
|
|
tr_variantDictAddInt(&top, TR_KEY_uploaded_bytes, s->uploadedBytes);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
filename = getFilename(session);
|
2020-07-28 17:31:36 +00:00
|
|
|
if (tr_logGetDeepEnabled())
|
|
|
|
{
|
|
|
|
tr_logAddDeep(__FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename);
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantToFile(&top, TR_VARIANT_FMT_JSON, filename);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(filename);
|
|
|
|
tr_variantFree(&top);
|
2007-11-26 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_statsInit(tr_session* session)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_stats_handle* stats = tr_new0(struct tr_stats_handle, 1);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
loadCumulativeStats(session, &stats->old);
|
|
|
|
stats->single.sessionCount = 1;
|
|
|
|
stats->startTime = tr_time();
|
|
|
|
session->sessionStats = stats;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static struct tr_stats_handle* getStats(tr_session const* session)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2019-07-14 12:40:41 +00:00
|
|
|
return session != NULL ? session->sessionStats : NULL;
|
2009-12-08 20:51:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_statsSaveDirty(tr_session* session)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_stats_handle* h = getStats(session);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (h != NULL && h->isDirty)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2017-06-24 10:17:04 +00:00
|
|
|
tr_session_stats cumulative = TR_SESSION_STATS_INIT;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionGetCumulativeStats(session, &cumulative);
|
|
|
|
saveCumulativeStats(session, &cumulative);
|
|
|
|
h->isDirty = false;
|
2009-12-08 20:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_statsClose(tr_session* session)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_statsSaveDirty(session);
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(session->sessionStats);
|
|
|
|
session->sessionStats = NULL;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-02-04 19:54:47 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void updateRatio(tr_session_stats* setme)
|
2007-12-22 03:01:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setme->ratio = tr_getRatio(setme->uploadedBytes, setme->downloadedBytes);
|
2008-02-04 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void addStats(tr_session_stats* setme, tr_session_stats const* a, tr_session_stats const* b)
|
2008-02-04 19:54:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setme->uploadedBytes = a->uploadedBytes + b->uploadedBytes;
|
|
|
|
setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
|
|
|
|
setme->filesAdded = a->filesAdded + b->filesAdded;
|
|
|
|
setme->sessionCount = a->sessionCount + b->sessionCount;
|
|
|
|
setme->secondsActive = a->secondsActive + b->secondsActive;
|
|
|
|
updateRatio(setme);
|
2007-12-22 03:01:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionGetStats(tr_session const* session, tr_session_stats* setme)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct tr_stats_handle const* stats = getStats(session);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (stats != NULL)
|
2008-11-17 05:05:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme = stats->single;
|
|
|
|
setme->secondsActive = tr_time() - stats->startTime;
|
|
|
|
updateRatio(setme);
|
2008-11-17 05:05:11 +00:00
|
|
|
}
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_sessionGetCumulativeStats(tr_session const* session, tr_session_stats* setme)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct tr_stats_handle const* stats = getStats(session);
|
2017-06-24 10:17:04 +00:00
|
|
|
tr_session_stats current = TR_SESSION_STATS_INIT;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (stats != NULL)
|
2008-11-17 05:05:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionGetStats(session, ¤t);
|
|
|
|
addStats(setme, &stats->old, ¤t);
|
2008-11-17 05:05:11 +00:00
|
|
|
}
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_sessionClearStats(tr_session* session)
|
2008-04-22 14:07:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session_stats zero;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
zero.uploadedBytes = 0;
|
|
|
|
zero.downloadedBytes = 0;
|
|
|
|
zero.ratio = TR_RATIO_NA;
|
|
|
|
zero.filesAdded = 0;
|
|
|
|
zero.sessionCount = 0;
|
|
|
|
zero.secondsActive = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
session->sessionStats->isDirty = true;
|
|
|
|
session->sessionStats->single = session->sessionStats->old = zero;
|
|
|
|
session->sessionStats->startTime = tr_time();
|
2008-04-22 14:07:42 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_statsAddUploaded(tr_session* session, uint32_t bytes)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_stats_handle* s;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((s = getStats(session)) != NULL)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s->single.uploadedBytes += bytes;
|
|
|
|
s->isDirty = true;
|
2009-12-08 20:51:45 +00:00
|
|
|
}
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_statsAddDownloaded(tr_session* session, uint32_t bytes)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_stats_handle* s;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((s = getStats(session)) != NULL)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
s->single.downloadedBytes += bytes;
|
|
|
|
s->isDirty = true;
|
2009-12-08 20:51:45 +00:00
|
|
|
}
|
2007-11-26 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_statsFileCreated(tr_session* session)
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_stats_handle* s;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((s = getStats(session)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
s->single.filesAdded++;
|
|
|
|
}
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|