thin_metadata_size.c: fix --maxthins unit processing

This commit is contained in:
Heinz Mauelshagen 2013-07-12 13:29:12 +02:00
parent 99a1b96b2f
commit 931221647c

View File

@ -19,9 +19,10 @@
/*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/
static char *prg; static char *prg; /* program name */
enum numeric_options { blocksize, poolsize, maxthins, numeric, opt_end}; enum numeric_options { BLOCKSIZE, POOLSIZE, MAXTHINS, NUMERIC, OPT_END};
enum return_units { RETURN_BYTES, RETURN_SECTORS };
struct global { struct global {
#define UNIT_ARRAY_SZ 18 #define UNIT_ARRAY_SZ 18
struct { struct {
@ -31,7 +32,7 @@ struct global {
} unit; } unit;
struct options { struct options {
unsigned long long n[opt_end]; unsigned long long n[OPT_END];
char unit; char unit;
} options; } options;
}; };
@ -48,16 +49,19 @@ static struct global *init(void)
"zetabytes", "zebibytes", "yottabytes", "yobibytes" }; "zetabytes", "zebibytes", "yottabytes", "yobibytes" };
memset(&r, 0, sizeof(r)); memset(&r, 0, sizeof(r));
r.unit.chars = "bskKmMgGtTpPeEzZyY"; r.unit.chars = "bskKmMgGtTpPeEzZyY";
u = 0; u = 0;
r.unit.factors[u++] = 1, r.unit.factors[u++] = 512, r.unit.factors[u++] = 1024, r.unit.factors[u++] = 1000; r.unit.factors[u++] = 1;
r.unit.factors[u++] = 512;
r.unit.factors[u++] = 1024;
r.unit.factors[u++] = 1000;
for ( ; u < UNIT_ARRAY_SZ; u += 2) { for ( ; u < UNIT_ARRAY_SZ; u += 2) {
r.unit.factors[u] = r.unit.factors[2] * r.unit.factors[u - 2]; r.unit.factors[u] = r.unit.factors[2] * r.unit.factors[u - 2];
r.unit.factors[u+1] = r.unit.factors[3] * r.unit.factors[u - 1]; r.unit.factors[u+1] = r.unit.factors[3] * r.unit.factors[u - 1];
} }
u = UNIT_ARRAY_SZ; for (u = UNIT_ARRAY_SZ; u--; )
while (u--)
r.unit.strings[u] = unit_strings[u]; r.unit.strings[u] = unit_strings[u];
r.options.unit = 's'; r.options.unit = 's';
@ -80,25 +84,27 @@ static void abort_prg(const char *msg)
static void check_opts(struct options *options) static void check_opts(struct options *options)
{ {
if (!options->n[blocksize] || !options->n[poolsize] || !options->n[maxthins]) if (!options->n[BLOCKSIZE] || !options->n[POOLSIZE] || !options->n[MAXTHINS])
abort_prg("3 arguments required!"); abort_prg("3 arguments required!");
else if (options->n[blocksize] & (options->n[blocksize] - 1)) else if (options->n[BLOCKSIZE] & (options->n[BLOCKSIZE] - 1))
abort_prg("block size must be 2^^N"); abort_prg("block size must be 2^^N");
else if (options->n[poolsize] < options->n[blocksize]) else if (options->n[POOLSIZE] < options->n[BLOCKSIZE])
abort_prg("poolsize must be much larger than blocksize"); abort_prg("POOLSIZE must be much larger than BLOCKSIZE");
else if (!options->n[maxthins]) else if (!options->n[MAXTHINS])
abort_prg("maximum number of thin provisioned devices must be > 0"); abort_prg("maximum number of thin provisioned devices must be > 0");
} }
static unsigned long long to_bytes(struct global *g, char *sz, int div) static unsigned long long to_bytes(struct global *g, char *sz, enum return_units unit)
{ {
unsigned len = strlen(sz); unsigned len = strlen(sz);
unsigned long long r;
char uc = 's', *us = strchr(g->unit.chars, sz[len-1]); char uc = 's', *us = strchr(g->unit.chars, sz[len-1]);
if (us) if (us)
uc = sz[len-1], sz[len-1] = 0; uc = *us, sz[len-1] = 0;
return g->unit.factors[get_index(g, uc)] * atoll(sz) / (div ? bytes_per_sector : 1); r = atoll(sz) * g->unit.factors[get_index(g, uc)];
return (!us || unit == RETURN_SECTORS) ? r / bytes_per_sector : r;
} }
static void printf_aligned(struct global *g, char *a, char *b, char *c, int units) static void printf_aligned(struct global *g, char *a, char *b, char *c, int units)
@ -117,13 +123,23 @@ static void help(struct global *g)
printf ("Thin Provisioning Metadata Device Size Calculator.\nUsage: %s [opts]\n", prg); printf ("Thin Provisioning Metadata Device Size Calculator.\nUsage: %s [opts]\n", prg);
printf_aligned(g, "-b", "--block-size BLOCKSIZE", "Block size of thin provisioned devices.", 1); printf_aligned(g, "-b", "--block-size BLOCKSIZE", "Block size of thin provisioned devices.", 1);
printf_aligned(g, "-s", "--pool-size SIZE", "Size of pool device.", 1); printf_aligned(g, "-s", "--pool-size SIZE", "Size of pool device.", 1);
printf_aligned(g, "-m", "--max-thins #MAXTHINS", "Maximum sum of all thin devices and snapshots.", 0); printf_aligned(g, "-m", "--max-thins #MAXTHINS", "Maximum sum of all thin devices and snapshots.", 1);
printf_aligned(g, "-u", "--unit ", "Output unit specifier.", 1); printf_aligned(g, "-u", "--unit ", "Output unit specifier.", 1);
printf_aligned(g, "-n", "--numeric-only", "Output numeric value only.", 0); printf_aligned(g, "-n", "--numeric-only", "Output numeric value only.", 0);
printf_aligned(g, "-h", "--help", "This help.", 0); printf_aligned(g, "-h", "--help", "This help.", 0);
exit(0); exit(0);
} }
static void check_unit(struct global *g, char *arg)
{
if (*(arg + 1))
abort_prg("only one unit specifier allowed!");
else if (!strchr(g->unit.chars, *arg))
abort_prg("output unit specifier invalid!");
g->options.unit = *arg;
}
static struct global *parse_command_line(struct global *g, int argc, char **argv) static struct global *parse_command_line(struct global *g, int argc, char **argv)
{ {
int c; int c;
@ -139,20 +155,15 @@ static struct global *parse_command_line(struct global *g, int argc, char **argv
while ((c = getopt_long(argc, argv, "b:s:m:u:nh", long_options, NULL)) != -1) { while ((c = getopt_long(argc, argv, "b:s:m:u:nh", long_options, NULL)) != -1) {
if (c == 'b') if (c == 'b')
g->options.n[blocksize] = to_bytes(g, optarg, 1); g->options.n[BLOCKSIZE] = to_bytes(g, optarg, RETURN_SECTORS);
else if (c == 's') else if (c == 's')
g->options.n[poolsize] = to_bytes(g, optarg, 1); g->options.n[POOLSIZE] = to_bytes(g, optarg, RETURN_SECTORS);
else if (c == 'm') else if (c == 'm')
g->options.n[maxthins] = to_bytes(g, optarg, 0); g->options.n[MAXTHINS] = to_bytes(g, optarg, RETURN_BYTES);
else if (c == 'u') { else if (c == 'u')
if (*(optarg + 1)) check_unit(g, optarg);
abort_prg("only one unit specifier allowed!"); else if (c == 'n')
else if (!strchr(g->unit.chars, *optarg)) g->options.n[NUMERIC] = 1;
abort_prg("output unit specifier invalid!");
g->options.unit = *optarg;
} else if (c == 'n')
g->options.n[numeric] = 1;
else if (c == 'h') else if (c == 'h')
help(g); help(g);
else else
@ -186,7 +197,7 @@ static void printf_precision(double r, int full, char *unit_str)
if (r == rtrunc) if (r == rtrunc)
printf("%llu", (unsigned long long) r); printf("%llu", (unsigned long long) r);
else else
printf(r - truncl(r) < 1E-3 ? "%0.3e" : "%0.3f", r); printf(r - truncl(r) < 1E-2 ? "%0.2e" : "%0.2f", r);
if (full) if (full)
printf(" %s", unit_str); printf(" %s", unit_str);
@ -200,10 +211,10 @@ static void estimated_result(struct global *g)
double r; double r;
/* double-fold # of nodes, because they aren't fully populated in average */ /* double-fold # of nodes, because they aren't fully populated in average */
r = (1.0 + (2 * g->options.n[poolsize] / g->options.n[blocksize] / mappings_per_block() + g->options.n[maxthins])) * 8 * bytes_per_sector; /* in bytes! */ r = (1.0 + (2 * g->options.n[POOLSIZE] / g->options.n[BLOCKSIZE] / mappings_per_block() + g->options.n[MAXTHINS])) * 8 * bytes_per_sector; /* in bytes! */
r /= g->unit.factors[idx]; /* in requested unit */ r /= g->unit.factors[idx]; /* in requested unit */
printf_precision(r, !g->options.n[numeric], g->unit.strings[idx]); printf_precision(r, !g->options.n[NUMERIC], g->unit.strings[idx]);
} }
int main(int argc, char **argv) int main(int argc, char **argv)