1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

libdm: allow formatting histogram strings with no whitespace

Allow dm_histogram_to_string() to format histogram strings with
no whitespace by passing a width value less than zero.
This commit is contained in:
Bryn M. Reeves 2015-09-03 21:48:15 +01:00
parent 0a73a5012a
commit f09e4f7b10
2 changed files with 46 additions and 12 deletions

View File

@ -2826,8 +2826,8 @@ uint64_t dm_histogram_get_sum(const struct dm_histogram *dmh);
/*
* Histogram formatting flags.
*/
#define DM_HISTOGRAM_VALUES 0x1
#define DM_HISTOGRAM_SUFFIX 0x2
#define DM_HISTOGRAM_SUFFIX 0x1
#define DM_HISTOGRAM_VALUES 0x2
#define DM_HISTOGRAM_PERCENT 0X4
#define DM_HISTOGRAM_BOUNDS_LOWER 0x10
#define DM_HISTOGRAM_BOUNDS_UPPER 0x20
@ -2840,6 +2840,12 @@ uint64_t dm_histogram_get_sum(const struct dm_histogram *dmh);
* The bin argument selects the bin to format. If this argument is less
* than zero all bins will be included in the resulting string.
*
* width specifies a minimum width for the field in characters; if it is
* zero the width will be determined automatically based on the options
* selected for formatting. A value less than zero disables field width
* control: bin boundaries and values will be output with a minimum
* amount of whitespace.
*
* flags is a collection of flag arguments that control the string format:
*
* DM_HISTOGRAM_VALUES - Include bin values in the string.

View File

@ -2116,7 +2116,7 @@ static int _make_bounds_string(char *buf, size_t size, uint64_t lower,
{
const char *l_suff = NULL;
const char *u_suff = NULL;
const char *sep = (flags & DM_HISTOGRAM_VALUES) ? ": " : "";
const char *sep = "";
char bound_buf[32];
int bounds = flags & DM_HISTOGRAM_BOUNDS_MASK;
@ -2131,6 +2131,9 @@ static int _make_bounds_string(char *buf, size_t size, uint64_t lower,
} else
l_suff = u_suff = "";
if (flags & DM_HISTOGRAM_VALUES)
sep = ":";
if (bounds > DM_HISTOGRAM_BOUNDS_LOWER) {
/* Handle infinite uppermost bound. */
if (upper == UINT64_MAX) {
@ -2162,9 +2165,11 @@ out:
return 0;
}
#define BOUND_WIDTH_NOSUFFIX 10 /* 999999999 nsecs */
#define BOUND_WIDTH 6 /* bounds string up to 9999xs */
#define COUNT_WIDTH 6 /* count string: up to 9999 */
#define PERCENT_WIDTH 8 /* percent string : 0.00-100.00% */
#define PERCENT_WIDTH 6 /* percent string : 0.00-100.00% */
#define DM_HISTOGRAM_VALUES_MASK 0x06
const char *dm_histogram_to_string(const struct dm_histogram *dmh, int bin,
int width, int flags)
@ -2186,12 +2191,15 @@ const char *dm_histogram_to_string(const struct dm_histogram *dmh, int bin,
} else
start = last = bin;
if (flags & DM_HISTOGRAM_PERCENT)
if (width < 0 || !values)
width = minwidth = 0; /* no padding */
else if (flags & DM_HISTOGRAM_PERCENT)
width = minwidth = (width) ? : PERCENT_WIDTH;
else if (flags & DM_HISTOGRAM_VALUES)
width = minwidth = (width) ? : COUNT_WIDTH;
else
width = 0; /* bounds only */
if (values && !width)
sep = ":";
/* Set bounds string to the empty string. */
bounds_buf[0] = '\0';
@ -2200,18 +2208,38 @@ const char *dm_histogram_to_string(const struct dm_histogram *dmh, int bin,
for (bin = start; bin <= last; bin++) {
if (bounds) {
int bounds_width;
/* Default bounds width depends on time suffixes. */
bounds_width = (!(flags & DM_HISTOGRAM_SUFFIX))
? BOUND_WIDTH_NOSUFFIX
: BOUND_WIDTH ;
bounds_width = (!width) ? width : bounds_width;
lower = dm_histogram_get_bin_lower(dmh, bin);
upper = dm_histogram_get_bin_upper(dmh, bin);
_make_bounds_string(bounds_buf, sizeof(bounds_buf),
len = sizeof(bounds_buf);
len = _make_bounds_string(bounds_buf, len,
lower, upper, flags,
(width) ? BOUND_WIDTH : 0);
sep = ", "; /* Comma separates "bounds: value" pairs */
width -= (int) (strlen(bounds_buf) - BOUND_WIDTH);
bounds_width);
/*
* Comma separates "bounds: value" pairs unless
* --noheadings is used.
*/
sep = (width || !values) ? "," : ":";
/* Adjust width by real bounds length if set. */
width -= (width) ? (len - (bounds_width + 1)) : 0;
/* -ve width indicates specified width was overrun. */
width = (width > 0) ? width : 0;
}
if (bin == last)
sep = "";
if (flags & DM_HISTOGRAM_PERCENT) {
dm_percent_t pr;
float value;