[base] introduce a command type that gets registered with the app

This commit is contained in:
Joe Thornber
2016-01-08 12:51:52 +00:00
parent a709b9718b
commit c93e728ef4
28 changed files with 776 additions and 411 deletions

View File

@ -31,7 +31,6 @@ using namespace std;
//----------------------------------------------------------------
namespace {
class reporter_base {
public:
reporter_base(nested_output &o)
@ -326,24 +325,32 @@ namespace {
return r;
}
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--clear-needs-check-flag}" << endl
<< " {--super-block-only}" << endl
<< " {--skip-mappings}" << endl
<< " {--skip-hints}" << endl
<< " {--skip-discards}" << endl;
}
}
//----------------------------------------------------------------
int cache_check_main(int argc, char **argv)
cache_check_cmd::cache_check_cmd()
: command("cache_check")
{
}
void
cache_check_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-q|--quiet}" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--clear-needs-check-flag}" << endl
<< " {--super-block-only}" << endl
<< " {--skip-mappings}" << endl
<< " {--skip-hints}" << endl
<< " {--skip-discards}" << endl;
}
int
cache_check_cmd::run(int argc, char **argv)
{
int c;
flags fs;
@ -384,7 +391,7 @@ int cache_check_main(int argc, char **argv)
break;
case 'h':
usage(cout, basename(argv[0]));
usage(cout);
return 0;
case 'q':
@ -396,20 +403,18 @@ int cache_check_main(int argc, char **argv)
return 0;
default:
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
}
if (argc == optind) {
cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
return check_with_exception_handling(argv[optind], fs);
}
base::command caching::cache_check_cmd("cache_check", cache_check_main);
//----------------------------------------------------------------

View File

@ -54,20 +54,28 @@ namespace {
return 0;
}
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl;
}
}
//----------------------------------------------------------------
int cache_dump_main(int argc, char **argv)
cache_dump_cmd::cache_dump_cmd()
: command("cache_dump")
{
}
void
cache_dump_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-o <xml file>}" << endl
<< " {-V|--version}" << endl
<< " {--repair}" << endl;
}
int
cache_dump_cmd::run(int argc, char **argv)
{
int c;
flags fs;
@ -89,7 +97,7 @@ int cache_dump_main(int argc, char **argv)
break;
case 'h':
usage(cout, basename(argv[0]));
usage(cout);
return 0;
case 'o':
@ -101,20 +109,18 @@ int cache_dump_main(int argc, char **argv)
return 0;
default:
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
}
if (argc == optind) {
cerr << "No input file provided." << endl;
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
return dump(argv[optind], output, fs);
}
base::command caching::cache_dump_cmd("cache_dump", cache_dump_main);
//----------------------------------------------------------------

View File

@ -9,141 +9,145 @@
#include <stdint.h>
#include <stdexcept>
using namespace caching;
using namespace std;
//----------------------------------------------------------------
namespace {
struct flags {
flags()
: max_hint_width(4) {
cache_metadata_size_cmd::flags::flags()
: max_hint_width(4) {
// Dance around some spurious compiler warnings
device_size = 0;
block_size = 0;
nr_blocks = 0;
// Dance around some spurious compiler warnings
device_size = 0;
block_size = 0;
nr_blocks = 0;
device_size.reset();
block_size.reset();
nr_blocks.reset();
}
boost::optional<uint64_t> device_size;
boost::optional<uint32_t> block_size;
boost::optional<uint64_t> nr_blocks;
uint32_t max_hint_width;
};
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--block-size <sectors>}" << endl
<< " {--device-size <sectors>}" << endl
<< " {--nr-blocks <natural>}" << endl << endl
<< "These all relate to the size of the fast device (eg, SSD), rather" << endl
<< "than the whole cached device." << endl;
}
enum parse_result {
FINISH,
CONTINUE
};
parse_result parse_command_line(string const &prog_name, int argc, char **argv, flags &fs) {
int c;
char const short_opts[] = "hV";
option const long_opts[] = {
{ "block-size", required_argument, NULL, 0 },
{ "device-size", required_argument, NULL, 1 },
{ "nr-blocks", required_argument, NULL, 2 },
{ "max-hint-width", required_argument, NULL, 3 },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch (c) {
case 0:
fs.block_size = boost::lexical_cast<uint32_t>(optarg);
break;
case 1:
fs.device_size = boost::lexical_cast<uint64_t>(optarg);
break;
case 2:
fs.nr_blocks = boost::lexical_cast<uint64_t>(optarg);
break;
case 3:
fs.max_hint_width = boost::lexical_cast<uint32_t>(optarg);
break;
case 'h':
usage(cout, prog_name);
return FINISH;
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return FINISH;
break;
default:
usage(cerr, prog_name);
throw runtime_error("Invalid command line");
break;
}
}
return CONTINUE;
}
uint64_t get_nr_blocks(flags &fs) {
if (fs.device_size) {
if (!fs.block_size)
throw runtime_error("If you specify --device-size you must also give --block-size.");
uint64_t nr_blocks = *fs.device_size / *fs.block_size;
if (fs.nr_blocks) {
if (nr_blocks != *fs.nr_blocks)
throw runtime_error(
"Contradictory arguments given, --nr-blocks doesn't match the --device-size and --block-size.");
}
return nr_blocks;
}
if (fs.block_size && !fs.device_size)
throw runtime_error("If you specify --block-size you must also give --device-size.");
if (fs.nr_blocks)
return *fs.nr_blocks;
throw runtime_error("Please specify either --device-size and --block-size, or --nr-blocks.");
}
uint64_t meg(uint64_t n) {
return n * 2048;
}
uint64_t calc_size(uint64_t nr_blocks, uint32_t max_hint_width) {
uint64_t const SECTOR_SIZE = 512;
uint64_t const TRANSACTION_OVERHEAD = meg(4);
uint64_t const BYTES_PER_BLOCK = 16;
uint64_t const HINT_OVERHEAD_PER_BLOCK = 8;
uint64_t mapping_size = (nr_blocks * BYTES_PER_BLOCK) / SECTOR_SIZE;
uint64_t hint_size = (nr_blocks * (max_hint_width + HINT_OVERHEAD_PER_BLOCK)) / SECTOR_SIZE;
return TRANSACTION_OVERHEAD + mapping_size + hint_size;
}
device_size.reset();
block_size.reset();
nr_blocks.reset();
}
int cache_metadata_size_main(int argc, char **argv)
cache_metadata_size_cmd::parse_result
cache_metadata_size_cmd::parse_command_line(string const &prog_name, int argc, char **argv, flags &fs)
{
int c;
char const short_opts[] = "hV";
option const long_opts[] = {
{ "block-size", required_argument, NULL, 0 },
{ "device-size", required_argument, NULL, 1 },
{ "nr-blocks", required_argument, NULL, 2 },
{ "max-hint-width", required_argument, NULL, 3 },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
switch (c) {
case 0:
fs.block_size = boost::lexical_cast<uint32_t>(optarg);
break;
case 1:
fs.device_size = boost::lexical_cast<uint64_t>(optarg);
break;
case 2:
fs.nr_blocks = boost::lexical_cast<uint64_t>(optarg);
break;
case 3:
fs.max_hint_width = boost::lexical_cast<uint32_t>(optarg);
break;
case 'h':
usage(cout);
return FINISH;
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return FINISH;
break;
default:
usage(cerr);
throw runtime_error("Invalid command line");
break;
}
}
return CONTINUE;
}
uint64_t
cache_metadata_size_cmd::get_nr_blocks(flags &fs)
{
if (fs.device_size) {
if (!fs.block_size)
throw runtime_error("If you specify --device-size you must also give --block-size.");
uint64_t nr_blocks = *fs.device_size / *fs.block_size;
if (fs.nr_blocks) {
if (nr_blocks != *fs.nr_blocks)
throw runtime_error(
"Contradictory arguments given, --nr-blocks doesn't match the --device-size and --block-size.");
}
return nr_blocks;
}
if (fs.block_size && !fs.device_size)
throw runtime_error("If you specify --block-size you must also give --device-size.");
if (fs.nr_blocks)
return *fs.nr_blocks;
throw runtime_error("Please specify either --device-size and --block-size, or --nr-blocks.");
}
uint64_t
cache_metadata_size_cmd::meg(uint64_t n)
{
return n * 2048;
}
uint64_t
cache_metadata_size_cmd::calc_size(uint64_t nr_blocks, uint32_t max_hint_width)
{
uint64_t const SECTOR_SIZE = 512;
uint64_t const TRANSACTION_OVERHEAD = meg(4);
uint64_t const BYTES_PER_BLOCK = 16;
uint64_t const HINT_OVERHEAD_PER_BLOCK = 8;
uint64_t mapping_size = (nr_blocks * BYTES_PER_BLOCK) / SECTOR_SIZE;
uint64_t hint_size = (nr_blocks * (max_hint_width + HINT_OVERHEAD_PER_BLOCK)) / SECTOR_SIZE;
return TRANSACTION_OVERHEAD + mapping_size + hint_size;
}
//----------------------------------------------------------------
cache_metadata_size_cmd::cache_metadata_size_cmd()
: command("cache_metadata_size")
{
}
void
cache_metadata_size_cmd::usage(ostream &out) const
{
out << "Usage: " << get_name() << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl
<< " {--block-size <sectors>}" << endl
<< " {--device-size <sectors>}" << endl
<< " {--nr-blocks <natural>}" << endl << endl
<< "These all relate to the size of the fast device (eg, SSD), rather" << endl
<< "than the whole cached device." << endl;
}
int
cache_metadata_size_cmd::run(int argc, char **argv)
{
flags fs;
@ -167,6 +171,4 @@ int cache_metadata_size_main(int argc, char **argv)
return 0;
}
base::command caching::cache_metadata_size_cmd("cache_metadata_size", cache_metadata_size_main);
//----------------------------------------------------------------

View File

@ -40,20 +40,28 @@ namespace {
return 0;
}
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input metadata (binary format)>" << endl
<< " {-o|--output} <output metadata (binary format)>" << endl
<< " {-V|--version}" << endl;
}
}
//----------------------------------------------------------------
int cache_repair_main(int argc, char **argv)
cache_repair_cmd::cache_repair_cmd()
: command("cache_repair")
{
}
void
cache_repair_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input metadata (binary format)>" << endl
<< " {-o|--output} <output metadata (binary format)>" << endl
<< " {-V|--version}" << endl;
}
int
cache_repair_cmd::run(int argc, char **argv)
{
int c;
boost::optional<string> input_path, output_path;
@ -70,7 +78,7 @@ int cache_repair_main(int argc, char **argv)
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage(cout, basename(argv[0]));
usage(cout);
return 0;
case 'i':
@ -86,26 +94,24 @@ int cache_repair_main(int argc, char **argv)
return 0;
default:
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
}
if (!input_path) {
cerr << "no input file provided" << endl;
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
if (!output_path) {
cerr << "no output file provided" << endl;
usage(cerr, basename(argv[0]));
usage(cerr);
return 1;
}
return repair(*input_path, *output_path);
}
base::command caching::cache_repair_cmd("cache_repair", cache_repair_main);
//----------------------------------------------------------------

View File

@ -80,27 +80,35 @@ namespace {
return 0;
}
void usage(ostream &out, string const &cmd) {
out << "Usage: " << cmd << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
<< " {-V|--version}" << endl
<< endl
<< " {--debug-override-metadata-version} <integer>" << endl
<< " {--omit-clean-shutdown}" << endl;
}
}
int cache_restore_main(int argc, char **argv)
//----------------------------------------------------------------
cache_restore_cmd::cache_restore_cmd()
: command("cache_restore")
{
}
void
cache_restore_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options]" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
<< " {-V|--version}" << endl
<< endl
<< " {--debug-override-metadata-version} <integer>" << endl
<< " {--omit-clean-shutdown}" << endl;
}
int
cache_restore_cmd::run(int argc, char **argv)
{
int c;
flags fs;
char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:qV";
option const long_opts[] = {
{ "debug-override-metadata-version", required_argument, NULL, 0 },
@ -125,7 +133,7 @@ int cache_restore_main(int argc, char **argv)
break;
case 'h':
usage(cout, prog_name);
usage(cout);
return 0;
case 'i':
@ -145,31 +153,29 @@ int cache_restore_main(int argc, char **argv)
return 0;
default:
usage(cerr, prog_name);
usage(cerr);
return 1;
}
}
if (argc != optind) {
usage(cerr, prog_name);
usage(cerr);
return 1;
}
if (!fs.input) {
cerr << "No input file provided." << endl << endl;
usage(cerr, prog_name);
usage(cerr);
return 1;
}
if (!fs.output) {
cerr << "No output file provided." << endl << endl;
usage(cerr, prog_name);
usage(cerr);
return 1;
}
return restore(fs);
}
base::command caching::cache_restore_cmd("cache_restore", cache_restore_main);
//----------------------------------------------------------------

18
caching/commands.cc Normal file
View File

@ -0,0 +1,18 @@
#include "caching/commands.h"
using namespace base;
using namespace caching;
//----------------------------------------------------------------
void
caching::register_cache_commands(application &app)
{
app.add_cmd(command::ptr(new cache_check_cmd));
app.add_cmd(command::ptr(new cache_dump_cmd));
app.add_cmd(command::ptr(new cache_metadata_size_cmd));
app.add_cmd(command::ptr(new cache_restore_cmd));
app.add_cmd(command::ptr(new cache_repair_cmd));
}
//----------------------------------------------------------------

View File

@ -2,15 +2,68 @@
#define CACHING_COMMANDS_H
#include "base/application.h"
#include "boost/optional.hpp"
//----------------------------------------------------------------
namespace caching {
extern base::command cache_check_cmd;
extern base::command cache_dump_cmd;
extern base::command cache_metadata_size_cmd;
extern base::command cache_restore_cmd;
extern base::command cache_repair_cmd;
class cache_check_cmd : public base::command {
public:
cache_check_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class cache_dump_cmd : public base::command {
public:
cache_dump_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class cache_metadata_size_cmd : public base::command {
public:
cache_metadata_size_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
private:
struct flags {
flags();
boost::optional<uint64_t> device_size;
boost::optional<uint32_t> block_size;
boost::optional<uint64_t> nr_blocks;
uint32_t max_hint_width;
};
enum parse_result {
FINISH,
CONTINUE
};
parse_result parse_command_line(std::string const &prog_name, int argc, char **argv, flags &fs);
uint64_t get_nr_blocks(flags &fs);
uint64_t meg(uint64_t n);
uint64_t calc_size(uint64_t nr_blocks, uint32_t max_hint_width);
};
class cache_repair_cmd : public base::command {
public:
cache_repair_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
class cache_restore_cmd : public base::command {
public:
cache_restore_cmd();
virtual void usage(std::ostream &out) const;
virtual int run(int argc, char **argv);
};
void register_cache_commands(base::application &app);
}
//----------------------------------------------------------------