2007-11-21 20:03:53 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2007-11-21 20:03:53 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-11-21 20:03:53 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-02-26 21:08:51 +00:00
|
|
|
* $Id$
|
2007-11-21 20:03:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "bencode.h"
|
2012-12-05 17:29:46 +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 */
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-20 18:48:52 +00:00
|
|
|
static const struct tr_session_stats STATS_INIT = { 0.0f, 0, 0, 0, 0, 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
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_session_stats single;
|
|
|
|
tr_session_stats old;
|
|
|
|
time_t startTime;
|
|
|
|
bool isDirty;
|
2007-11-26 18:55:36 +00:00
|
|
|
};
|
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
getOldFilename (const tr_session * session)
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_buildPath (tr_sessionGetConfigDir (session), "stats.benc", NULL);
|
2008-04-18 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static char*
|
2012-12-05 17:29:46 +00:00
|
|
|
getFilename (const tr_session * session)
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_buildPath (tr_sessionGetConfigDir (session), "stats.json", NULL);
|
2008-06-12 16:25:36 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
loadCumulativeStats (const tr_session * session, tr_session_stats * setme)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_benc top;
|
|
|
|
char * filename;
|
|
|
|
bool loaded = false;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
filename = getFilename (session);
|
|
|
|
loaded = !tr_bencLoadFile (&top, TR_FMT_JSON, filename);
|
|
|
|
tr_free (filename);
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!loaded)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
filename = getOldFilename (session);
|
|
|
|
loaded = !tr_bencLoadFile (&top, TR_FMT_BENC, filename);
|
|
|
|
tr_free (filename);
|
2008-06-12 16:25:36 +00:00
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (loaded)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int64_t i;
|
|
|
|
|
|
|
|
if (tr_bencDictFindInt (&top, "downloaded-bytes", &i))
|
|
|
|
setme->downloadedBytes = (uint64_t) i;
|
|
|
|
if (tr_bencDictFindInt (&top, "files-added", &i))
|
|
|
|
setme->filesAdded = (uint64_t) i;
|
|
|
|
if (tr_bencDictFindInt (&top, "seconds-active", &i))
|
|
|
|
setme->secondsActive = (uint64_t) i;
|
|
|
|
if (tr_bencDictFindInt (&top, "session-count", &i))
|
|
|
|
setme->sessionCount = (uint64_t) i;
|
|
|
|
if (tr_bencDictFindInt (&top, "uploaded-bytes", &i))
|
|
|
|
setme->uploadedBytes = (uint64_t) i;
|
|
|
|
|
|
|
|
tr_bencFree (&top);
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
saveCumulativeStats (const tr_session * session, const tr_session_stats * s)
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * filename;
|
|
|
|
tr_benc top;
|
|
|
|
|
|
|
|
tr_bencInitDict (&top, 5);
|
|
|
|
tr_bencDictAddInt (&top, "downloaded-bytes", s->downloadedBytes);
|
|
|
|
tr_bencDictAddInt (&top, "files-added", s->filesAdded);
|
|
|
|
tr_bencDictAddInt (&top, "seconds-active", s->secondsActive);
|
|
|
|
tr_bencDictAddInt (&top, "session-count", s->sessionCount);
|
|
|
|
tr_bencDictAddInt (&top, "uploaded-bytes", s->uploadedBytes);
|
|
|
|
|
|
|
|
filename = getFilename (session);
|
|
|
|
tr_deepLog (__FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename);
|
|
|
|
tr_bencToFile (&top, TR_FMT_JSON, filename);
|
|
|
|
|
|
|
|
tr_free (filename);
|
|
|
|
tr_bencFree (&top);
|
2007-11-26 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsInit (tr_session * session)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct tr_stats_handle * stats = tr_new0 (struct tr_stats_handle, 1);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +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
|
|
|
}
|
|
|
|
|
2009-12-08 20:51:45 +00:00
|
|
|
static struct tr_stats_handle *
|
2012-12-05 17:29:46 +00:00
|
|
|
getStats (const tr_session * session)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return session ? session->sessionStats : NULL;
|
2009-12-08 20:51:45 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsSaveDirty (tr_session * session)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct tr_stats_handle * h = getStats (session);
|
|
|
|
|
|
|
|
if ((h != NULL) && h->isDirty)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_session_stats cumulative = STATS_INIT;
|
|
|
|
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
|
|
|
|
2009-12-08 20:51:45 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsClose (tr_session * session)
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsSaveDirty (session);
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2012-12-05 17:29:46 +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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-22 03:01:14 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
updateRatio (tr_session_stats * setme)
|
2007-12-22 03:01:14 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
setme->ratio = tr_getRatio (setme->uploadedBytes, setme->downloadedBytes);
|
2008-02-04 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
addStats (tr_session_stats * setme,
|
2008-02-04 19:54:47 +00:00
|
|
|
const tr_session_stats * a,
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_session_stats * b)
|
2008-02-04 19:54:47 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionGetStats (const tr_session * session, tr_session_stats * setme)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct tr_stats_handle * stats = getStats (session);
|
|
|
|
|
|
|
|
if (stats)
|
2008-11-17 05:05:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionGetCumulativeStats (const tr_session * session,
|
|
|
|
tr_session_stats * setme)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct tr_stats_handle * stats = getStats (session);
|
|
|
|
tr_session_stats current = STATS_INIT;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (stats)
|
2008-11-17 05:05:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
}
|
|
|
|
|
2008-04-22 14:07:42 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sessionClearStats (tr_session * session)
|
2008-04-22 14:07:42 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_session_stats zero;
|
|
|
|
|
|
|
|
zero.uploadedBytes = 0;
|
|
|
|
zero.downloadedBytes = 0;
|
|
|
|
zero.ratio = TR_RATIO_NA;
|
|
|
|
zero.filesAdded = 0;
|
|
|
|
zero.sessionCount = 0;
|
|
|
|
zero.secondsActive = 0;
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsAddUploaded (tr_session * session, uint32_t bytes)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct tr_stats_handle * s;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((s = getStats (session)))
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsAddDownloaded (tr_session * session, uint32_t bytes)
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct tr_stats_handle * s;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((s = getStats (session)))
|
2009-12-08 20:51:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_statsFileCreated (tr_session * session)
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct tr_stats_handle * s;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((s = getStats (session)))
|
|
|
|
s->single.filesAdded++;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|