source: trunk/libtransmission/stats.c @ 3989

Last change on this file since 3989 was 3989, checked in by charles, 15 years ago

fix crash-on-exit introduced by stats code

File size: 5.1 KB
Line 
1/*
2 * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: peer-msgs.c 3906 2007-11-20 17:29:56Z charles $
11 */
12
13#include <string.h> /* memset */
14
15#include "transmission.h"
16#include "bencode.h"
17#include "platform.h" /* tr_getPrefsDirectory */
18#include "utils.h" /* tr_buildPath */
19
20/***
21****
22***/
23
24struct tr_stats_handle
25{
26    tr_session_stats single;
27    tr_session_stats cumulative;
28    time_t startTime;
29};
30
31static void
32parseCumulativeStats( tr_session_stats  * setme,
33                      const uint8_t     * content,
34                      size_t              len )
35{
36    benc_val_t top;
37
38    if( !tr_bencLoad( content, len, &top, NULL ) )
39    {
40        const benc_val_t * val;
41
42        if(( val = tr_bencDictFindType( &top, "uploaded-bytes", TYPE_INT )))
43            setme->uploadedBytes = (uint64_t) tr_bencGetInt( val );
44
45        if(( val = tr_bencDictFindType( &top, "downloaded-bytes", TYPE_INT )))
46            setme->downloadedBytes = (uint64_t) tr_bencGetInt( val );
47
48        if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT )))
49            setme->filesAdded = (uint64_t) tr_bencGetInt( val );
50
51        if(( val = tr_bencDictFindType( &top, "session-count", TYPE_INT )))
52            setme->sessionCount = (uint64_t) tr_bencGetInt( val );
53
54        if(( val = tr_bencDictFindType( &top, "seconds-active", TYPE_INT )))
55            setme->secondsActive = (uint64_t) tr_bencGetInt( val );
56
57        tr_bencFree( &top );
58    }
59}
60
61static char*
62getFilename( char * buf, size_t buflen )
63{
64    tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "stats.benc", NULL );
65    return buf;
66}
67
68static void
69loadCumulativeStats( tr_session_stats * setme )
70{
71    size_t len;
72    uint8_t * content;
73    char filename[MAX_PATH_LENGTH];
74
75    getFilename( filename, sizeof(filename) );
76    content = tr_loadFile( filename, &len );
77    if( content != NULL )
78        parseCumulativeStats( setme, content, len );
79
80    tr_free( content );
81}
82
83static void
84saveCumulativeStats( const tr_session_stats * stats )
85{
86    FILE * fp;
87    char * str;
88    char filename[MAX_PATH_LENGTH];
89    int len;
90    benc_val_t top;
91
92    tr_bencInit( &top, TYPE_DICT );
93    tr_bencDictReserve( &top, 5 );
94    tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), stats->uploadedBytes );
95    tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), stats->downloadedBytes );
96    tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), stats->filesAdded );
97    tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), stats->sessionCount );
98    tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), stats->secondsActive );
99
100    str = tr_bencSave( &top, &len );
101    getFilename( filename, sizeof(filename) );
102    fp = fopen( filename, "wb+" );
103    fwrite( str, 1, len, fp );
104    fclose( fp );
105    tr_free( str );
106
107    tr_bencFree( &top );
108}
109
110/***
111****
112***/
113
114void
115tr_statsInit( tr_handle * handle )
116{
117    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
118    loadCumulativeStats( &stats->cumulative );
119    stats->cumulative.sessionCount++;
120    stats->startTime = time(NULL);
121    handle->sessionStats = stats;
122fprintf( stderr, "handle %p sessionStats is %p\n", handle, handle->sessionStats );
123}
124
125void
126tr_statsClose( tr_handle * handle )
127{
128    tr_session_stats tmp;
129fprintf( stderr, "handle %p sessionStats is %p\n", handle, handle->sessionStats );
130    tr_getCumulativeSessionStats( handle, &tmp );
131    saveCumulativeStats( &tmp );
132
133    tr_free( handle->sessionStats );
134    handle->sessionStats = NULL;
135}
136
137void
138tr_getSessionStats( const tr_handle   * handle,
139                    tr_session_stats  * setme )
140{
141    const struct tr_stats_handle * stats = handle->sessionStats;
142    *setme = stats->single;
143    setme->ratio = setme->downloadedBytes ? (double)setme->uploadedBytes / (double)setme->downloadedBytes : TR_RATIO_NA;
144    setme->secondsActive += ( time(NULL) - stats->startTime );
145}
146
147void
148tr_getCumulativeSessionStats( const tr_handle   * handle,
149                              tr_session_stats  * setme )
150{
151    const struct tr_stats_handle * stats = handle->sessionStats;
152    *setme = stats->cumulative;
153    setme->ratio = setme->downloadedBytes ? (double)setme->uploadedBytes / (double)setme->downloadedBytes : TR_RATIO_NA;
154    setme->secondsActive += ( time(NULL) - stats->startTime );
155}
156
157/**
158***
159**/
160
161void
162tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
163{
164    struct tr_stats_handle * stats = handle->sessionStats;
165    stats->single.uploadedBytes += bytes;
166    stats->cumulative.uploadedBytes += bytes;
167}
168
169void
170tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
171{
172    struct tr_stats_handle * stats = handle->sessionStats;
173    stats->single.downloadedBytes += bytes;
174    stats->cumulative.downloadedBytes += bytes;
175}
176
177void
178tr_statsFileCreated( tr_handle * handle )
179{
180    struct tr_stats_handle * stats = handle->sessionStats;
181    ++stats->cumulative.filesAdded;
182    ++stats->single.filesAdded;
183}
Note: See TracBrowser for help on using the repository browser.