Files
libtorrent/examples/parse_request_log.cpp
2012-01-02 19:32:15 +00:00

338 lines
9.2 KiB
C++

/*
Copyright (c) 2012, 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.
*/
#include "libtorrent/file.hpp"
#include "libtorrent/io.hpp"
#include <cstring>
#include <boost/bind.hpp>
#include <stdlib.h>
#include <map>
#include <vector>
using namespace libtorrent;
using namespace libtorrent::detail; // for write_* and read_*
void print_usage()
{
fprintf(stderr, "usage: parse_request_log log-file\n");
exit(1);
}
// the event format in the log is:
// uint64_t timestamp (microseconds)
// uint64_t info-hash prefix
// uint32_t peer identifier
// uint32_t piece
// uint32_t start offset
// uint32_t length
struct request
{
boost::uint64_t timestamp;
boost::uint64_t infohash;
boost::uint32_t peer;
boost::uint32_t piece;
boost::uint32_t start;
boost::uint32_t length;
};
struct object_entry
{
int hits;
int cache_hits;
bool operator<(object_entry const& rhs) const
{
return hits < rhs.hits;
}
};
struct average_time
{
average_time() : first_request(0), num_requests(0), last_peer(0) {}
void sample(boost::uint32_t peer, boost::uint64_t timestamp)
{
// if (peer == last_peer) return;
// last_peer = peer;
++num_requests;
if (first_request == 0)
{
first_request = timestamp;
last_request = timestamp;
}
else
{
assert(timestamp >= last_request);
last_request = timestamp;
}
}
float request_rate() const
{
if (num_requests <= 8) return 0.f;
return float(num_requests) / float(last_request - first_request) * 1000000.f;
}
boost::uint64_t first_request;
boost::uint64_t last_request;
int num_requests;
boost::uint32_t last_peer;
};
struct cache
{
virtual bool incoming_request(request const& r) = 0;
virtual ~cache() {}
};
struct noop_cache : cache
{
virtual bool incoming_request(request const& r) { return false; }
};
struct lru_cache : cache
{
lru_cache(int size) : m_size(size) {}
virtual bool incoming_request(request const& r)
{
boost::uint64_t piece = (r.infohash & 0xffffffff00000000LL) | r.piece;
c