premiere-libtorrent/include/libtorrent/stat.hpp

190 lines
5.9 KiB
C++
Raw Normal View History

/*
Copyright (c) 2003, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TORRENT_STAT_HPP_INCLUDED
#define TORRENT_STAT_HPP_INCLUDED
#include <algorithm>
#include <vector>
2004-01-16 04:58:24 +01:00
#include <assert.h>
#include "libtorrent/size_type.hpp"
#include "libtorrent/invariant_check.hpp"
namespace libtorrent
{
class stat
{
friend class invariant_access;
2004-02-25 00:48:02 +01:00
enum { history = 5 };
public:
stat()
2003-12-22 08:14:35 +01:00
: m_downloaded_payload(0)
, m_uploaded_payload(0)
2003-12-07 06:53:04 +01:00
, m_downloaded_protocol(0)
, m_uploaded_protocol(0)
2003-12-22 08:14:35 +01:00
, m_total_download_payload(0)
, m_total_upload_payload(0)
2003-12-07 06:53:04 +01:00
, m_total_download_protocol(0)
, m_total_upload_protocol(0)
, m_peak_downloaded_per_second(0)
, m_peak_uploaded_per_second(0)
, m_mean_download_per_second(0)
, m_mean_upload_per_second(0)
{
std::fill(m_download_per_second_history, m_download_per_second_history+history, 0);
std::fill(m_upload_per_second_history, m_upload_per_second_history+history, 0);
}
2003-12-07 06:53:04 +01:00
void operator+=(const stat& s)
{
INVARIANT_CHECK;
2003-12-22 08:14:35 +01:00
m_downloaded_payload += s.m_downloaded_payload;
m_total_download_payload += s.m_downloaded_payload;
2003-12-07 06:53:04 +01:00
m_downloaded_protocol += s.m_downloaded_protocol;
m_total_download_protocol += s.m_downloaded_protocol;
2003-12-22 08:14:35 +01:00
m_uploaded_payload += s.m_uploaded_payload;
m_total_upload_payload += s.m_uploaded_payload;
2003-12-07 06:53:04 +01:00
m_uploaded_protocol += s.m_uploaded_protocol;
m_total_upload_protocol += s.m_uploaded_protocol;
}
void received_bytes(int bytes_payload, int bytes_protocol)
{
INVARIANT_CHECK;
2004-01-25 13:37:15 +01:00
assert(bytes_payload >= 0);
assert(bytes_protocol >= 0);
2003-12-22 08:14:35 +01:00
m_downloaded_payload += bytes_payload;
m_total_download_payload += bytes_payload;
2003-12-07 06:53:04 +01:00
m_downloaded_protocol += bytes_protocol;
m_total_download_protocol += bytes_protocol;
}
2004-01-16 04:58:24 +01:00
2003-12-07 06:53:04 +01:00
void sent_bytes(int bytes_payload, int bytes_protocol)
{
INVARIANT_CHECK;
2004-01-25 13:37:15 +01:00
assert(bytes_payload >= 0);
assert(bytes_protocol >= 0);
2003-12-22 08:14:35 +01:00
m_uploaded_payload += bytes_payload;
m_total_upload_payload += bytes_payload;
2003-12-07 06:53:04 +01:00
m_uploaded_protocol += bytes_protocol;
m_total_upload_protocol += bytes_protocol;
}
// should be called once every second
void second_tick();
2003-12-07 06:53:04 +01:00
// only counts the payload data!
2004-01-25 13:37:15 +01:00
float upload_rate() const { return m_mean_upload_per_second; }
float download_rate() const { return m_mean_download_per_second; }
float down_peak() const { return m_peak_downloaded_per_second; }
float up_peak() const { return m_peak_uploaded_per_second; }
2004-01-25 13:37:15 +01:00
size_type total_payload_upload() const { return m_total_upload_payload; }
size_type total_payload_download() const { return m_total_download_payload; }
2003-12-22 08:14:35 +01:00
2004-01-25 13:37:15 +01:00
size_type total_protocol_upload() const { return m_total_upload_protocol; }
size_type total_protocol_download() const { return m_total_download_protocol; }
2004-02-26 01:27:06 +01:00
// this is used to offset the statistics when a
// peer_connection is opened and have some previous
// transfers from earlier connections.
2004-02-25 00:55:42 +01:00
void add_stat(size_type downloaded, size_type uploaded)
{
m_total_download_payload += downloaded;
m_total_upload_payload += uploaded;
}
private:
2004-01-25 13:37:15 +01:00
#ifndef NDEBUG
void check_invariant() const
2004-01-25 13:37:15 +01:00
{
assert(m_mean_upload_per_second >= 0);
assert(m_mean_download_per_second >= 0);
assert(m_peak_uploaded_per_second >= 0);
assert(m_peak_downloaded_per_second >= 0);
assert(m_total_upload_payload >= 0);
assert(m_total_download_payload >= 0);
assert(m_total_upload_protocol >= 0);
assert(m_total_download_protocol >= 0);
}
#endif
2003-12-07 06:53:04 +01:00
// history of download/upload speeds a few seconds back
2004-01-25 13:37:15 +01:00
int m_download_per_second_history[history];
int m_upload_per_second_history[history];
// the accumulators we are adding the downloads/upploads
2003-12-07 06:53:04 +01:00
// to this second. This only counts the actual payload
// and ignores the bytes sent as protocol chatter.
2004-01-25 13:37:15 +01:00
int m_downloaded_payload;
int m_uploaded_payload;
2003-12-07 06:53:04 +01:00
// the accumulators we are adding the downloads/upploads
// to this second. This only counts the protocol
// chatter and ignores the actual payload
2004-01-25 13:37:15 +01:00
int m_downloaded_protocol;
int m_uploaded_protocol;
2003-12-07 06:53:04 +01:00
// total download/upload counters
2003-12-07 06:53:04 +01:00
// only counting payload data
2004-01-16 04:58:24 +01:00
size_type m_total_download_payload;
size_type m_total_upload_payload;
2003-12-07 06:53:04 +01:00
// total download/upload counters
// only counting protocol chatter
2004-01-16 04:58:24 +01:00
size_type m_total_download_protocol;
size_type m_total_upload_protocol;
2003-12-07 06:53:04 +01:00
// peak mean download/upload rates
float m_peak_downloaded_per_second;
float m_peak_uploaded_per_second;
// current mean download/upload rates
float m_mean_download_per_second;
float m_mean_upload_per_second;
};
}
#endif // TORRENT_STAT_HPP_INCLUDED