2019-04-23 08:17:34 +10:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
2019-04-26 11:16:02 +10:00
|
|
|
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
2019-04-23 08:17:34 +10:00
|
|
|
*/
|
|
|
|
|
2020-12-06 10:34:33 +11:00
|
|
|
#include "emory/chunk/find.hpp"
|
|
|
|
#include "emory/chunk/region.hpp"
|
2019-04-26 11:16:02 +10:00
|
|
|
#include "emory/chunk/params.hpp"
|
2020-12-06 10:00:03 +11:00
|
|
|
#include "emory/chunk/ostream.hpp"
|
2019-04-26 11:16:02 +10:00
|
|
|
|
2019-04-23 08:17:34 +10:00
|
|
|
#include <cruft/util/io.hpp>
|
2019-04-26 11:16:02 +10:00
|
|
|
#include <cruft/util/view.hpp>
|
2019-04-23 08:55:16 +10:00
|
|
|
#include <cruft/util/parse/value.hpp>
|
2019-04-23 08:17:34 +10:00
|
|
|
|
2020-12-06 10:07:27 +11:00
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <fmt/compile.h>
|
|
|
|
|
2020-12-06 07:22:18 +11:00
|
|
|
#include <algorithm>
|
2019-04-23 08:17:34 +10:00
|
|
|
#include <iostream>
|
2020-12-06 07:22:18 +11:00
|
|
|
#include <compare>
|
2020-12-06 10:00:22 +11:00
|
|
|
#include <fstream>
|
2019-04-23 08:17:34 +10:00
|
|
|
|
2019-04-23 08:55:16 +10:00
|
|
|
|
2019-04-26 11:16:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-12-06 07:22:18 +11:00
|
|
|
static
|
|
|
|
std::strong_ordering
|
|
|
|
region_ordering (
|
|
|
|
emory::chunk::region const &a,
|
|
|
|
emory::chunk::region const &b
|
|
|
|
) {
|
|
|
|
if (auto const cmp = a.size () <=> b.size (); cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
for (int i = 0; i < std::ssize (a.digest); ++i)
|
|
|
|
if (auto const cmp = a.digest[i] <=> b.digest[i]; cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
return std::strong_ordering::equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool region_less (emory::chunk::region const &a, emory::chunk::region const &b)
|
|
|
|
{
|
|
|
|
return region_ordering (a, b) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool region_equal (emory::chunk::region const &a, emory::chunk::region const &b)
|
|
|
|
{
|
|
|
|
return region_ordering (a, b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//static bool overlap (emory::chunk::region const &a, emory::chunk::region const &b)
|
|
|
|
//{
|
|
|
|
// return a.offset.first < b.offset.second &&
|
|
|
|
// b.offset.first < a.offset.second;
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
2020-12-06 08:14:05 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
enum {
|
|
|
|
ARG_SELF,
|
2020-12-06 09:42:01 +11:00
|
|
|
|
|
|
|
ARGS_INPUT,
|
|
|
|
ARGS_OUTPUT,
|
|
|
|
|
2020-12-06 08:14:05 +11:00
|
|
|
ARG_BITS,
|
|
|
|
ARG_WINDOW,
|
2020-12-06 09:42:01 +11:00
|
|
|
ARG_MINIMUM,
|
2020-12-06 08:14:05 +11:00
|
|
|
|
|
|
|
NUM_ARGS,
|
2020-12-06 09:42:01 +11:00
|
|
|
NUM_ARGS_REQUIRED = 3,
|
2020-12-06 08:14:05 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-04-26 11:16:02 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2019-04-23 08:17:34 +10:00
|
|
|
int main (int argc, char const **argv)
|
|
|
|
{
|
2020-12-06 09:42:01 +11:00
|
|
|
if (argc < NUM_ARGS_REQUIRED) {
|
|
|
|
std::cerr << "usage: " << argv[ARG_SELF] << " <input> <output> [bits] [window] [minimum]\n"
|
2020-12-06 09:48:20 +11:00
|
|
|
<< "default bits = " << emory::chunk::DEFAULT_PARAMS.bits << '\n'
|
|
|
|
<< "default window = " << emory::chunk::DEFAULT_PARAMS.window << '\n'
|
|
|
|
<< "default minimum = " << emory::chunk::DEFAULT_PARAMS.minimum << '\n';
|
2019-04-23 08:55:16 +10:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-12-06 09:48:20 +11:00
|
|
|
emory::chunk::params p = emory::chunk::DEFAULT_PARAMS;
|
2020-12-06 10:00:03 +11:00
|
|
|
if (argc > ARG_BITS + 1)
|
|
|
|
p.bits = cruft::parse::from_string<std::size_t> (argv[ARG_WINDOW]);
|
|
|
|
if (argc > ARG_WINDOW + 1)
|
2020-12-06 09:48:20 +11:00
|
|
|
p.window = cruft::parse::from_string<std::size_t> (argv[ARG_BITS]);
|
2020-12-06 10:00:03 +11:00
|
|
|
if (argc > ARG_BITS + 1)
|
2020-12-06 09:48:20 +11:00
|
|
|
p.minimum = cruft::parse::from_string<std::size_t> (argv[ARG_MINIMUM]);
|
|
|
|
|
|
|
|
std::cerr << p << '\n';
|
2020-12-06 09:42:01 +11:00
|
|
|
|
2020-12-06 10:00:22 +11:00
|
|
|
std::ofstream output (argv[ARGS_OUTPUT], std::ios::out | std::ios::trunc);
|
|
|
|
output.exceptions (std::ios::badbit | std::ios::eofbit | std::ios::failbit);
|
2019-04-23 08:17:34 +10:00
|
|
|
|
2019-04-26 11:16:02 +10:00
|
|
|
cruft::mapped_file data (argv[ARGS_INPUT]);
|
2020-12-06 07:22:52 +11:00
|
|
|
std::cout << "size: " << data.size () << '\n';
|
|
|
|
|
|
|
|
std::cout << "processing\n";
|
2020-12-06 10:34:33 +11:00
|
|
|
std::vector<emory::chunk::region> src;
|
|
|
|
emory::chunk::find<emory::chunk::static_hash> (std::back_inserter (src), data, p);
|
2019-04-23 08:17:34 +10:00
|
|
|
|
2020-12-06 07:22:52 +11:00
|
|
|
std::cout << "validating\n";
|
2019-04-26 11:16:02 +10:00
|
|
|
std::cout << src.size () << " chunks\n";
|
2020-12-06 07:22:52 +11:00
|
|
|
std::sort (
|
2020-12-06 10:34:33 +11:00
|
|
|
src.begin (),
|
|
|
|
src.end (),
|
2020-12-06 07:22:52 +11:00
|
|
|
[] (auto const &a, auto const &b) { return a.offset.first < b.offset.first; }
|
|
|
|
);
|
2020-12-06 10:34:33 +11:00
|
|
|
for (off_t i = 0, cursor = 0; i < std::ssize (src); ++i) {
|
|
|
|
if (src[i].offset.first != cursor) {
|
2020-12-06 07:22:52 +11:00
|
|
|
std::cout << "non-overlapping chunks\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2020-12-06 10:34:33 +11:00
|
|
|
cursor = src[i].offset.second;
|
2020-12-06 07:22:52 +11:00
|
|
|
}
|
|
|
|
|
2020-12-06 10:34:33 +11:00
|
|
|
if (src.back ().offset.second != std::ssize (data)) {
|
2020-12-06 07:22:52 +11:00
|
|
|
std::cout << "invalid total size\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort (
|
2020-12-06 10:34:33 +11:00
|
|
|
src.begin (),
|
|
|
|
src.end (),
|
2020-12-06 07:22:52 +11:00
|
|
|
region_less
|
|
|
|
);
|
|
|
|
|
|
|
|
std::vector<off64_t> sizes;
|
|
|
|
std::transform (
|
2020-12-06 10:34:33 +11:00
|
|
|
src.begin (),
|
|
|
|
src.end (),
|
2020-12-06 07:22:52 +11:00
|
|
|
std::back_inserter (sizes),
|
|
|
|
[] (auto const &val) { return val.size (); }
|
|
|
|
);
|
|
|
|
auto const myaccum = std::accumulate (std::begin (sizes), std::end (sizes), 0);
|
|
|
|
std::cout << myaccum << '\n';
|
|
|
|
|
|
|
|
auto const total_bytes = std::accumulate (
|
2020-12-06 10:34:33 +11:00
|
|
|
src.begin (),
|
|
|
|
src.end (),
|
2020-12-06 07:22:52 +11:00
|
|
|
0,
|
|
|
|
[] (auto const accum, auto const rhs)
|
|
|
|
{
|
|
|
|
return accum + rhs.size ();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<emory::chunk::region> unique;
|
|
|
|
std::unique_copy (
|
2020-12-06 10:34:33 +11:00
|
|
|
src.begin (),
|
|
|
|
src.end (),
|
2020-12-06 07:22:52 +11:00
|
|
|
std::back_inserter (unique),
|
|
|
|
region_equal
|
|
|
|
);
|
|
|
|
auto const unique_bytes = std::accumulate (
|
|
|
|
unique.begin (),
|
|
|
|
unique.end (),
|
|
|
|
0, [] (auto const accum, auto const rhs) { return accum + rhs.size (); }
|
|
|
|
);
|
|
|
|
|
|
|
|
auto const duplicated_bytes = total_bytes - unique_bytes;
|
2020-12-06 10:07:27 +11:00
|
|
|
float const duplicated_fraction = float (duplicated_bytes) / total_bytes;
|
|
|
|
|
|
|
|
fmt::print (
|
|
|
|
"{} duplicated bytes of {} ({:.2f}%)\n",
|
|
|
|
duplicated_bytes,
|
|
|
|
total_bytes,
|
|
|
|
100.f * duplicated_fraction
|
|
|
|
);
|
2020-12-06 07:22:52 +11:00
|
|
|
|
2020-12-06 10:34:33 +11:00
|
|
|
std::cout << (src.size () - unique.size ()) << " duplicates\n";
|
2019-04-23 08:17:34 +10:00
|
|
|
}
|