OSDN Git Service

転送量カウンタのオーバーフロー修正
[peercast-im/PeerCastIM.git] / c: / Git / PeerCast.root / PeerCast / core / common / stats.cpp
1 // ------------------------------------------------
2 // File : stats.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //              Statistic logging
7 //              
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
20
21
22 #include "stats.h"
23 #include "common.h"
24 #include "sys.h"
25 #include "stream.h"
26 #ifdef _DEBUG
27 #include "chkMemoryLeak.h"
28 #define DEBUG_NEW new(__FILE__, __LINE__)
29 #define new DEBUG_NEW
30 #endif
31
32 Stats stats;
33 // ------------------------------------
34 void Stats::clear()
35 {
36         for(int i=0; i<Stats::MAX; i++)
37         {
38                 current[i] = 0;
39                 last[i] = 0;
40                 perSec[i] = 0;
41         }
42         lastUpdate = 0;
43 }
44 // ------------------------------------
45 void    Stats::update()
46 {
47         unsigned int ctime = sys->getTime();
48
49         unsigned int diff = ctime - lastUpdate;
50         if (diff >= /* 5 */ 1)
51         {
52                 
53                 for(int i=0; i<Stats::MAX; i++)
54                 {
55                         perSec[i] = (unsigned)(current[i]-last[i])/diff;
56                         last[i] = current[i];
57                 }
58
59                 lastUpdate = ctime;
60         }
61         
62 }
63 // ------------------------------------
64 bool Stats::writeVariable(Stream &out,const String &var)
65 {
66         char buf[1024];
67
68         if (var == "totalInPerSec")             
69                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESIN)));
70         else if (var == "totalOutPerSec")               
71                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESOUT)));
72         else if (var == "totalPerSec")          
73                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESIN)+getPerSecond(Stats::BYTESOUT)));
74         else if (var == "wanInPerSec")          
75                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESIN)-getPerSecond(Stats::LOCALBYTESIN)));
76         else if (var == "wanOutPerSec")         
77                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::BYTESOUT)-getPerSecond(Stats::LOCALBYTESOUT)));
78         else if (var == "wanTotalPerSec")               
79                 sprintf(buf,"%.1f",BYTES_TO_KBPS((getPerSecond(Stats::BYTESIN)-getPerSecond(Stats::LOCALBYTESIN))+(getPerSecond(Stats::BYTESOUT)-getPerSecond(Stats::LOCALBYTESOUT))));
80         else if (var == "netInPerSec")          
81                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::PACKETDATAIN)));
82         else if (var == "netOutPerSec")         
83                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::PACKETDATAOUT)));
84         else if (var == "netTotalPerSec")               
85                 sprintf(buf,"%.1f",BYTES_TO_KBPS(getPerSecond(Stats::PACKETDATAOUT)+getPerSecond(Stats::PACKETDATAIN)));
86         else if (var == "packInPerSec")         
87                 sprintf(buf,"%.1f",getPerSecond(Stats::NUMPACKETSIN));
88         else if (var == "packOutPerSec")                
89                 sprintf(buf,"%.1f",getPerSecond(Stats::NUMPACKETSOUT));
90         else if (var == "packTotalPerSec")              
91                 sprintf(buf,"%.1f",getPerSecond(Stats::NUMPACKETSOUT)+getPerSecond(Stats::NUMPACKETSIN));
92
93         else
94                 return false;
95
96         out.writeString(buf);
97
98         return true;
99 }
100
101
102