2014-07-06 21:18:00 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2011, 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/config.hpp"
|
|
|
|
#include "libtorrent/io.hpp"
|
|
|
|
#include <cstring>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <cstdlib>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <map>
|
2016-05-25 06:31:52 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cinttypes> // for PRId64 et.al.
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace libtorrent::detail; // for write_* and read_*
|
|
|
|
|
|
|
|
void print_usage()
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "usage: parse_access_log log-file\n\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
"prints a gnuplot readable data file to stdout\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct file_op
|
|
|
|
{
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t timestamp;
|
|
|
|
std::uint64_t offset;
|
|
|
|
std::uint8_t event;
|
2014-07-06 21:18:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (argc != 2) print_usage();
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* log_file = std::fopen(argv[1], "r");
|
2016-07-09 22:26:26 +02:00
|
|
|
if (log_file == nullptr)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "failed to open logfile: %s\n%d: %s\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
, argv[1], errno, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* writes_file = std::fopen("writes.log", "w+");
|
|
|
|
FILE* reads_file = std::fopen("reads.log", "w+");
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* writes_elev_file = std::fopen("writes_elevator.log", "w+");
|
|
|
|
FILE* reads_elev_file = std::fopen("reads_elevator.log", "w+");
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
typedef std::map<std::uint32_t, file_op> op_map;
|
2014-07-06 21:18:00 +02:00
|
|
|
op_map outstanding_ops;
|
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t first_timestamp = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char entry[21];
|
|
|
|
char* ptr = entry;
|
2016-04-25 23:22:09 +02:00
|
|
|
int ret = int(fread(&entry, 1, sizeof(entry), log_file));
|
2014-07-06 21:18:00 +02:00
|
|
|
if (ret != sizeof(entry)) break;
|
|
|
|
|
|
|
|
file_op op;
|
|
|
|
op.timestamp = read_uint64(ptr);
|
|
|
|
op.offset = read_uint64(ptr);
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t event_id = read_uint32(ptr);
|
2014-07-06 21:18:00 +02:00
|
|
|
op.event = read_uint8(ptr);
|
|
|
|
|
|
|
|
if (first_timestamp == 0) first_timestamp = op.timestamp;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
bool write = (op.event & 1) != 0;
|
|
|
|
bool complete = (op.event & 2) != 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (complete)
|
|
|
|
{
|
2016-07-09 22:26:26 +02:00
|
|
|
FILE* out_file = nullptr;
|
2014-07-06 21:18:00 +02:00
|
|
|
op_map::iterator i = outstanding_ops.find(event_id);
|
|
|
|
if (i != outstanding_ops.end())
|
|
|
|
{
|
|
|
|
if (i->second.timestamp > op.timestamp)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "end-event stamped before "
|
2014-07-06 21:18:00 +02:00
|
|
|
"start-event: %" PRId64 " started at: %f\n"
|
|
|
|
, op.offset, double(i->second.timestamp) / 1000000.f);
|
|
|
|
i->second.timestamp = op.timestamp;
|
|
|
|
}
|
2016-04-30 06:24:45 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
out_file = write ? writes_file : reads_file;
|
|
|
|
double start_time = double(i->second.timestamp - first_timestamp) / 1000000.0;
|
|
|
|
double end_time = double(op.timestamp - first_timestamp) / 1000000.0;
|
|
|
|
double duration_time = double(op.timestamp - i->second.timestamp) / 1000000.0;
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(out_file, "%f\t%" PRId64 "\t%f\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
, start_time, op.offset, duration_time);
|
|
|
|
|
|
|
|
out_file = write ? writes_elev_file : reads_elev_file;
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(out_file, "%f\t%" PRId64 "\n", end_time, op.offset);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
outstanding_ops.erase(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "no start event for (%u): %" PRId64 " ended at: %f\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
, event_id, op.offset, double(op.timestamp) / 1000000.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
op_map::iterator i = outstanding_ops.find(event_id);
|
|
|
|
if (i != outstanding_ops.end())
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::fprintf(stderr, "duplicate start event for (%u): %" PRId64 " at: %f"
|
2014-07-06 21:18:00 +02:00
|
|
|
"(current start is at: %f)\n"
|
|
|
|
, event_id, op.offset, double(i->second.timestamp - first_timestamp) / 1000000.f
|
|
|
|
, double(op.timestamp - first_timestamp) / 1000000.f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outstanding_ops[event_id] = op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fclose(writes_file);
|
|
|
|
std::fclose(reads_file);
|
|
|
|
std::fclose(writes_elev_file);
|
|
|
|
std::fclose(reads_elev_file);
|
|
|
|
std::fclose(log_file);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
FILE* gnuplot = std::fopen("file_access.gnuplot", "w+");
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
char const* gnuplot_file =
|
|
|
|
"set term png size 1400,1024\n"
|
|
|
|
"set output \"file_access.png\"\n"
|
|
|
|
"set xlabel \"time (s)\"\n"
|
|
|
|
"set ylabel \"file offset\"\n"
|
|
|
|
"set style line 1 lc rgb \"#ff8888\"\n"
|
|
|
|
"set style line 2 lc rgb \"#88ff88\"\n"
|
|
|
|
"set style arrow 1 nohead ls 1\n"
|
|
|
|
"set style arrow 2 nohead ls 2\n"
|
|
|
|
"plot \"writes.log\" using 1:2:3:(0) title \"writes\" with vectors arrowstyle 1, "
|
|
|
|
"\"reads.log\" using 1:2:3:(0) title \"reads\" with vectors arrowstyle 2\n";
|
|
|
|
|
2016-05-25 06:31:52 +02:00
|
|
|
std::fwrite(gnuplot_file, strlen(gnuplot_file), 1, gnuplot);
|
|
|
|
std::fclose(gnuplot);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
system("gnuplot file_access.gnuplot");
|
|
|
|
|
|
|
|
assert(outstanding_ops.empty());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|