2013-07-11 22:04:00 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Red Hat, GmbH
|
|
|
|
*
|
|
|
|
* Calculates device-mapper thin privisioning
|
|
|
|
* metadata device size based on pool, block size and
|
|
|
|
* maximum expected thin provisioned devices and snapshots.
|
|
|
|
*
|
2013-07-17 16:55:03 +05:30
|
|
|
* This file is part of the thin-provisioning-tools source.
|
|
|
|
*
|
|
|
|
* thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
* and/or modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation, either version 3 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with thin-provisioning-tools. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2013-07-11 22:04:00 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-07-17 16:55:03 +05:30
|
|
|
#include "version.h"
|
2013-07-11 22:04:00 +05:30
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
#include <search.h>
|
|
|
|
|
2013-07-11 22:04:00 +05:30
|
|
|
/*----------------------------------------------------------------*/
|
|
|
|
|
2013-07-12 16:59:12 +05:30
|
|
|
enum numeric_options { BLOCKSIZE, POOLSIZE, MAXTHINS, NUMERIC, OPT_END};
|
|
|
|
enum return_units { RETURN_BYTES, RETURN_SECTORS };
|
2013-07-18 17:33:27 +05:30
|
|
|
enum numeric_type { NO_NUMBER, NUMBER, NUMBER_SHORT, NUMBER_LONG };
|
|
|
|
typedef unsigned bool;
|
|
|
|
enum bool_value { false = 0, true = 1};
|
2013-07-11 22:04:00 +05:30
|
|
|
struct global {
|
2013-07-17 19:30:39 +05:30
|
|
|
char *prg; /* program name */
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
/* Unit representations in characters, strings and numeric factors. */
|
2013-07-11 22:04:00 +05:30
|
|
|
struct {
|
|
|
|
char *chars;
|
2013-07-17 20:54:55 +05:30
|
|
|
char **strings;
|
|
|
|
unsigned long long *factors;
|
2013-07-11 22:04:00 +05:30
|
|
|
} unit;
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
/* Command line option properties. */
|
2013-07-11 22:04:00 +05:30
|
|
|
struct options {
|
2013-07-17 20:54:55 +05:30
|
|
|
unsigned unit_idx;
|
2013-07-17 16:55:03 +05:30
|
|
|
char *s[OPT_END];
|
2013-07-17 19:30:39 +05:30
|
|
|
unsigned long long n[OPT_END];
|
2013-07-11 22:04:00 +05:30
|
|
|
} options;
|
|
|
|
};
|
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
static void exit_prg(struct global *g, int ret)
|
|
|
|
{
|
2013-07-18 17:33:27 +05:30
|
|
|
if (g) {
|
|
|
|
unsigned u = OPT_END;
|
|
|
|
|
|
|
|
while (u--) {
|
|
|
|
if (g->options.s[u])
|
|
|
|
free (g->options.s[u]);
|
|
|
|
}
|
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
free(g);
|
2013-07-18 17:33:27 +05:30
|
|
|
}
|
2013-07-17 19:30:39 +05:30
|
|
|
|
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void abort_prg(struct global *g, const char *msg)
|
|
|
|
{
|
2013-07-18 17:33:27 +05:30
|
|
|
fprintf(stderr, "%s - %s\n", g ? g->prg : "fatal", msg);
|
2013-07-17 19:30:39 +05:30
|
|
|
exit_prg(g, 1);
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
static int unit_index(struct global *g, char *unit_string)
|
|
|
|
{
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
if (!unit_string)
|
|
|
|
return unit_index(g, "sectors");
|
|
|
|
|
|
|
|
len = strlen(unit_string);
|
|
|
|
if (len == 1) {
|
|
|
|
char *o = strchr(g->unit.chars, *unit_string);
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
return o - g->unit.chars;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
char **s;
|
|
|
|
|
|
|
|
for (s = g->unit.strings; *s; s++)
|
|
|
|
if (!strncmp(*s, unit_string, len))
|
|
|
|
return s - g->unit.strings;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
|
2013-07-17 19:30:39 +05:30
|
|
|
static struct global *init_prg(char *prg_path)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
2013-07-17 20:54:55 +05:30
|
|
|
unsigned u;
|
|
|
|
static char *unit_chars = "bskKmMgGtTpPeEzZyY";
|
2013-07-11 22:04:00 +05:30
|
|
|
static char *unit_strings[] = { "bytes", "sectors",
|
|
|
|
"kilobytes", "kibibytes", "megabytes", "mebibytes",
|
|
|
|
"gigabytes", "gibibytes", "terabytes", "tebibytes",
|
|
|
|
"petabytes", "pebibytes", "exabytes", "ebibytes",
|
2013-07-17 20:54:55 +05:30
|
|
|
"zetabytes", "zebibytes", "yottabytes", "yobibytes", NULL };
|
2013-07-18 17:33:27 +05:30
|
|
|
static unsigned long long unit_factors[ARRAY_SIZE(unit_strings) - 1] = { 1, 512, 1024, 1000 };
|
2013-07-17 19:30:39 +05:30
|
|
|
struct global *r = malloc(sizeof(*r));
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
abort_prg(r, "failed to allocate global context!");
|
|
|
|
|
|
|
|
memset(r, 0, sizeof(*r));
|
2013-07-17 20:54:55 +05:30
|
|
|
|
|
|
|
for (u = 4; unit_strings[u]; u += 2) {
|
2013-07-18 17:33:27 +05:30
|
|
|
unit_factors[u] = unit_factors[u-2] * unit_factors[2];
|
2013-07-17 20:54:55 +05:30
|
|
|
unit_factors[u+1] = unit_factors[u-1] * unit_factors[3];
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-17 20:54:55 +05:30
|
|
|
r->prg = basename(prg_path);
|
|
|
|
r->unit.chars = unit_chars;
|
|
|
|
r->unit.strings = unit_strings;
|
|
|
|
r->unit.factors = unit_factors;
|
2013-07-18 17:33:27 +05:30
|
|
|
r->options.unit_idx = unit_index(r, NULL);
|
2013-07-11 22:04:00 +05:30
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
return r;
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
static unsigned long long bytes_per_sector(struct global *g)
|
2013-07-17 16:55:03 +05:30
|
|
|
{
|
2013-07-18 17:33:27 +05:30
|
|
|
return g->unit.factors[unit_index(g, "sectors")];
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
static void check_opts(struct global *g)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
2013-07-17 19:30:39 +05:30
|
|
|
struct options *o = &g->options;
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
if (!o->n[BLOCKSIZE])
|
|
|
|
abort_prg(g, "block size required!");
|
|
|
|
else if (!o->n[POOLSIZE])
|
|
|
|
abort_prg(g, "pool size required!");
|
|
|
|
else if (!o->n[MAXTHINS])
|
|
|
|
abort_prg(g, "max thins required!");
|
2013-07-17 19:30:39 +05:30
|
|
|
else if (o->n[BLOCKSIZE] & (o->n[BLOCKSIZE] - 1))
|
|
|
|
abort_prg(g, "block size must be 2^^N");
|
|
|
|
else if (o->n[POOLSIZE] <= o->n[BLOCKSIZE])
|
2013-07-18 17:33:27 +05:30
|
|
|
abort_prg(g, "pool size must be larger than block size");
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
static unsigned long long to_bytes(struct global *g, char *sz, enum return_units unit, int *index)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
2013-07-17 19:30:39 +05:30
|
|
|
int idx;
|
2013-07-12 16:59:12 +05:30
|
|
|
unsigned long long r;
|
2013-07-18 17:33:27 +05:30
|
|
|
char *us = sz;
|
2013-07-17 19:30:39 +05:30
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
/* Get pointer to unit identifier. */
|
|
|
|
us += strspn(sz, "0123456789");
|
2013-07-17 19:30:39 +05:30
|
|
|
if (*us) {
|
2013-07-18 17:33:27 +05:30
|
|
|
idx = unit_index(g, us);
|
2013-07-17 19:30:39 +05:30
|
|
|
if (idx < 0)
|
|
|
|
abort_prg(g, "Invalid unit specifier!");
|
|
|
|
|
|
|
|
*us = 0;
|
|
|
|
*index = idx;
|
|
|
|
} else {
|
2013-07-18 17:33:27 +05:30
|
|
|
idx = unit_index(g, NULL);
|
2013-07-17 19:30:39 +05:30
|
|
|
us = NULL;
|
|
|
|
*index = -1;
|
|
|
|
}
|
2013-07-11 22:04:00 +05:30
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
r = atoll(sz) * g->unit.factors[idx];
|
|
|
|
return (!us || unit == RETURN_SECTORS) ? r / bytes_per_sector(g) : r;
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
static void printf_aligned(struct global *g, char *a, char *b, char *c, bool units, bool mandatory)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
strcpy(buf, b);
|
|
|
|
if (units)
|
2013-07-18 17:33:27 +05:30
|
|
|
strcat(buf, mandatory ? "{" :"["), strcat(buf, g->unit.chars), strcat(buf, mandatory ? "}" : "]");
|
2013-07-11 22:04:00 +05:30
|
|
|
|
2013-07-17 16:55:03 +05:30
|
|
|
printf("\t%-4s%-44s%s\n", a, buf, c);
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void help(struct global *g)
|
|
|
|
{
|
2013-07-17 19:30:39 +05:30
|
|
|
printf ("Thin Provisioning Metadata Device Size Calculator.\nUsage: %s [options]\n", g->prg);
|
2013-07-18 17:33:27 +05:30
|
|
|
printf_aligned(g, "-b", "--block-size BLOCKSIZE", "Block size of thin provisioned devices.", true, false);
|
|
|
|
printf_aligned(g, "-s", "--pool-size SIZE", "Size of pool device.", true, false);
|
|
|
|
printf_aligned(g, "-m", "--max-thins #MAXTHINS", "Maximum sum of all thin devices and snapshots.", true, false);
|
|
|
|
printf_aligned(g, "-u", "--unit ", "Output unit specifier.", true, true);
|
|
|
|
printf_aligned(g, "-n", "--numeric-only [short|long]", "Output numeric value only (optionally with short/long unit identifier).", false, false);
|
|
|
|
printf_aligned(g, "-h", "--help", "This help.", false, false);
|
|
|
|
printf_aligned(g, "-V", "--version", "Print thin provisioning tools version.", false, false);
|
2013-07-17 19:30:39 +05:30
|
|
|
exit_prg(g, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void version(struct global *g)
|
|
|
|
{
|
|
|
|
printf("%s\n", THIN_PROVISIONING_TOOLS_VERSION);
|
|
|
|
exit_prg(g, 1);
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-12 16:59:12 +05:30
|
|
|
static void check_unit(struct global *g, char *arg)
|
|
|
|
{
|
2013-07-18 17:33:27 +05:30
|
|
|
int idx = unit_index(g, arg);
|
2013-07-17 20:54:55 +05:30
|
|
|
|
|
|
|
if (idx < 0)
|
2013-07-17 19:30:39 +05:30
|
|
|
abort_prg(g, "output unit specifier invalid!");
|
2013-07-12 16:59:12 +05:30
|
|
|
|
2013-07-17 20:54:55 +05:30
|
|
|
g->options.unit_idx = idx;
|
2013-07-12 16:59:12 +05:30
|
|
|
}
|
|
|
|
|
2013-07-17 16:55:03 +05:30
|
|
|
static void check_numeric_option(struct global *g, char *arg)
|
|
|
|
{
|
2013-07-18 17:33:27 +05:30
|
|
|
if (g->options.n[NUMERIC] != NO_NUMBER)
|
2013-07-17 19:30:39 +05:30
|
|
|
abort_prg(g, "-n already given!");
|
2013-07-17 16:55:03 +05:30
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
g->options.n[NUMERIC] = NUMBER;
|
2013-07-17 16:55:03 +05:30
|
|
|
|
|
|
|
if (arg) {
|
2013-07-18 17:33:27 +05:30
|
|
|
bool unit_long = !strncmp("long", arg, strlen(arg));
|
|
|
|
|
|
|
|
if (!*arg || (strncmp("short", arg, strlen(arg)) && !unit_long))
|
2013-07-17 19:30:39 +05:30
|
|
|
abort_prg(g, "-n invalid option argument");
|
2013-07-17 16:55:03 +05:30
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
g->options.n[NUMERIC] = unit_long ? NUMBER_LONG : NUMBER_SHORT;
|
2013-07-17 16:55:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
static void check_size(struct global *g, enum numeric_options o, char *arg)
|
2013-07-17 16:55:03 +05:30
|
|
|
{
|
2013-07-17 19:30:39 +05:30
|
|
|
int idx;
|
2013-07-18 17:33:27 +05:30
|
|
|
bool valid_index = true;
|
2013-07-17 19:30:39 +05:30
|
|
|
|
2013-07-17 16:55:03 +05:30
|
|
|
if (g->options.n[o])
|
2013-07-17 19:30:39 +05:30
|
|
|
abort_prg(g, "option already given!");
|
2013-07-17 16:55:03 +05:30
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
g->options.n[o] = to_bytes(g, arg, o == MAXTHINS ? RETURN_BYTES : RETURN_SECTORS, &idx);
|
2013-07-18 17:33:27 +05:30
|
|
|
if (idx < 0) {
|
|
|
|
valid_index = false;
|
|
|
|
idx = g->options.unit_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
g->options.s[o] = malloc(strlen(arg) + strlen(g->unit.strings[idx]) + 1);
|
2013-07-17 16:55:03 +05:30
|
|
|
if (!g->options.s[o])
|
2013-07-17 19:30:39 +05:30
|
|
|
abort_prg(g, "failed to allocate string!");
|
|
|
|
|
|
|
|
strcpy(g->options.s[o], arg);
|
2013-07-18 17:33:27 +05:30
|
|
|
if (o != MAXTHINS || valid_index)
|
2013-07-17 19:30:39 +05:30
|
|
|
strcat(g->options.s[o], g->unit.strings[idx]);
|
2013-07-17 16:55:03 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_command_line(struct global *g, int argc, char **argv)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
|
|
|
int c;
|
|
|
|
static struct option long_options[] = {
|
2013-07-17 16:55:03 +05:30
|
|
|
{"block-size", required_argument, NULL, 'b' },
|
|
|
|
{"pool-size", required_argument, NULL, 's' },
|
|
|
|
{"max-thins", required_argument, NULL, 'm' },
|
|
|
|
{"unit", required_argument, NULL, 'u' },
|
|
|
|
{"numeric-only",optional_argument, NULL, 'n' },
|
|
|
|
{"help", no_argument, NULL, 'h' },
|
|
|
|
{"version", no_argument, NULL, 'V' },
|
2013-07-18 17:33:27 +05:30
|
|
|
{NULL, 0, NULL, 0 }
|
2013-07-11 22:04:00 +05:30
|
|
|
};
|
|
|
|
|
2013-07-17 16:55:03 +05:30
|
|
|
while ((c = getopt_long(argc, argv, "b:s:m:u:n::hV", long_options, NULL)) != -1) {
|
2013-07-17 19:30:39 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
check_size(g, BLOCKSIZE, optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
check_size(g, POOLSIZE, optarg);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
check_size(g, MAXTHINS, optarg);
|
|
|
|
break;
|
|
|
|
case 'u':
|
2013-07-12 16:59:12 +05:30
|
|
|
check_unit(g, optarg);
|
2013-07-17 19:30:39 +05:30
|
|
|
break;
|
|
|
|
case 'n':
|
2013-07-17 16:55:03 +05:30
|
|
|
check_numeric_option(g, optarg);
|
2013-07-17 19:30:39 +05:30
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
help(g); /* exits */
|
|
|
|
case 'V':
|
|
|
|
version(g); /* exits */
|
|
|
|
default:
|
2013-07-18 17:33:27 +05:30
|
|
|
exit_prg(g, 1);
|
2013-07-17 19:30:39 +05:30
|
|
|
}
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-17 19:30:39 +05:30
|
|
|
check_opts(g);
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static const unsigned mappings_per_block(void)
|
|
|
|
{
|
|
|
|
const struct {
|
|
|
|
const unsigned node;
|
|
|
|
const unsigned node_header;
|
|
|
|
const unsigned entry;
|
|
|
|
} btree_size = { 4096, 64, 16 };
|
|
|
|
|
|
|
|
return (btree_size.node - btree_size.node_header) / btree_size.entry;
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
static void print_precision(struct global *g, double r, unsigned idx)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
2013-07-18 17:33:27 +05:30
|
|
|
bool full = !g->options.n[NUMERIC];
|
2013-07-12 16:28:53 +05:30
|
|
|
double rtrunc = truncl(r);
|
|
|
|
|
|
|
|
if (full)
|
2013-07-18 17:33:27 +05:30
|
|
|
printf("%s - ", g->prg);
|
2013-07-12 16:28:53 +05:30
|
|
|
|
|
|
|
if (r == rtrunc)
|
|
|
|
printf("%llu", (unsigned long long) r);
|
|
|
|
else
|
2013-07-18 17:33:27 +05:30
|
|
|
printf(r - rtrunc < 1E-2 ? "%0.2e" : "%0.2f", r);
|
2013-07-12 16:28:53 +05:30
|
|
|
|
|
|
|
if (full)
|
2013-07-17 16:55:03 +05:30
|
|
|
printf(" %s", g->unit.strings[idx]);
|
2013-07-18 17:33:27 +05:30
|
|
|
else if (g->options.n[NUMERIC] > NUMBER) {
|
|
|
|
if (g->options.n[NUMERIC] == NUMBER_SHORT)
|
|
|
|
printf("%c", g->unit.chars[idx]);
|
|
|
|
else
|
|
|
|
printf("%s", g->unit.strings[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (full)
|
|
|
|
printf(" estimated metadata area size for \"--block-size=%s --pool-size=%s --max-thins=%s\"",
|
|
|
|
g->options.s[BLOCKSIZE], g->options.s[POOLSIZE], g->options.s[MAXTHINS]);
|
2013-07-12 16:28:53 +05:30
|
|
|
|
|
|
|
putchar('\n');
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
2013-07-17 16:55:03 +05:30
|
|
|
static void print_estimated_result(struct global *g)
|
2013-07-11 22:04:00 +05:30
|
|
|
{
|
|
|
|
double r;
|
|
|
|
|
|
|
|
/* double-fold # of nodes, because they aren't fully populated in average */
|
2013-07-18 17:33:27 +05:30
|
|
|
r = (1.0 + (2 * g->options.n[POOLSIZE] / g->options.n[BLOCKSIZE] / mappings_per_block() + g->options.n[MAXTHINS])); /* in 4k blocks */
|
|
|
|
r *= 8 * bytes_per_sector(g); /* in bytes! */
|
|
|
|
r /= g->unit.factors[g->options.unit_idx]; /* in requested unit */
|
2013-07-11 22:04:00 +05:30
|
|
|
|
2013-07-18 17:33:27 +05:30
|
|
|
print_precision(g, r, g->options.unit_idx);
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-07-17 19:30:39 +05:30
|
|
|
struct global *g = init_prg(*argv);
|
2013-07-17 16:55:03 +05:30
|
|
|
|
|
|
|
parse_command_line(g, argc, argv);
|
|
|
|
print_estimated_result(g);
|
2013-07-17 19:30:39 +05:30
|
|
|
exit_prg(g, 0);
|
|
|
|
return 0; /* Doesn't get here... */
|
2013-07-11 22:04:00 +05:30
|
|
|
}
|