Skip to content

Commit 9bde2c4

Browse files
authored
Pretty-print byte sizes (LLNL#86)
1 parent 6c3ae14 commit 9bde2c4

File tree

6 files changed

+155
-12
lines changed

6 files changed

+155
-12
lines changed
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Copyright (c) 2016, David Boehme.
2+
* All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
/*! \file unitfmt.h
29+
* \brief Scale values to human-readable size
30+
*/
31+
32+
#pragma once
33+
34+
#include <stdint.h>
35+
36+
#ifdef __cplusplus
37+
extern "C" {
38+
#endif
39+
40+
struct unitfmt_table{
41+
unsigned long long factor;
42+
const char* symbol;
43+
};
44+
45+
/*! \brief A format table for byte values (in base-2) */
46+
extern const struct unitfmt_table unitfmt_bytes[];
47+
48+
typedef struct {
49+
double val;
50+
const char* symbol;
51+
} unitfmt_result;
52+
53+
54+
/*! \brief Scale values to human-readable size
55+
*
56+
* \param val Value to scale
57+
* \param table Reference table with unit conversion factors and symbols
58+
*
59+
* \return Conversion result. Member val is the scaled value, member
60+
* symbol points to the conversion factor symbol.
61+
*/
62+
unitfmt_result
63+
unitfmt(unsigned long long val, const struct unitfmt_table table[]);
64+
65+
#ifdef __cplusplus
66+
} /* extern "C" */
67+
#endif

src/caliper/MemoryPool.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#include "caliper/common/RuntimeConfig.h"
3636

37+
#include "caliper/common/c-util/unitfmt.h"
38+
3739
#include "caliper/common/util/spinlock.hpp"
3840

3941
#include <algorithm>
@@ -103,9 +105,14 @@ struct MemoryPool::MemoryPoolImpl
103105
}
104106

105107
std::ostream& print_statistics(std::ostream& os) const {
108+
unitfmt_result bytes_reserved
109+
= unitfmt(m_total_reserved, unitfmt_bytes);
110+
unitfmt_result bytes_used
111+
= unitfmt(m_total_used, unitfmt_bytes);
112+
106113
os << "Metadata memory pool: "
107-
<< m_total_reserved << " bytes reserved, "
108-
<< m_total_used << " bytes used";
114+
<< bytes_reserved.val << " " << bytes_reserved.symbol << " reserved, "
115+
<< bytes_used.val << " " << bytes_used.symbol << " used";
109116

110117
return os;
111118
}

src/common/c-util/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(C_UTIL_SOURCES
2+
unitfmt.c
23
vlenc.c)
34

45
add_library(c-util OBJECT ${C_UTIL_SOURCES})

src/common/c-util/unitfmt.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright (c) 2016, David Boehme.
2+
* All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
/*! \file unitfmt.c
29+
* \brief Scale values to human-readable size
30+
*/
31+
32+
#include "caliper/common/c-util/unitfmt.h"
33+
34+
const struct unitfmt_table unitfmt_bytes[] = {
35+
{ 1, "Byte(s)" },
36+
{ 1024, "KiB" },
37+
{ 1024 * 1024, "MiB" },
38+
{ 1024 * 1024 * 1024, "GiB" },
39+
{ 0, "GiB" }
40+
};
41+
42+
unitfmt_result
43+
unitfmt(unsigned long long val, const struct unitfmt_table table[])
44+
{
45+
double dval = (double) val;
46+
unitfmt_result result = { dval, table[0].symbol };
47+
48+
for (const struct unitfmt_table* p = table; p->factor && dval >= p->factor; ++p) {
49+
result.val = dval / p->factor;
50+
result.symbol = p->symbol;
51+
}
52+
53+
return result;
54+
}

src/services/aggregate/Aggregate.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "caliper/common/RuntimeConfig.h"
4545
#include "caliper/common/Variant.h"
4646

47+
#include "caliper/common/c-util/unitfmt.h"
4748
#include "caliper/common/c-util/vlenc.h"
4849

4950
#include "caliper/common/util/spinlock.hpp"
@@ -796,14 +797,18 @@ class AggregateDB {
796797
}
797798

798799
static void finish_cb(Caliper* c) {
799-
Log(2).stream() << "Aggregate: max key len " << s_global_max_keylen << ", "
800-
<< s_global_num_kernel_entries << " entries, "
801-
<< s_global_num_trie_entries << " nodes, "
802-
<< s_global_num_trie_blocks + s_global_num_kernel_blocks << " blocks ("
803-
<< s_global_num_trie_blocks * sizeof(TrieNode) * 1024
804-
+ s_global_num_kernel_blocks * sizeof(AggregateKernel) * 1024
805-
<< " bytes reserved)"
806-
<< std::endl;
800+
if (Log::verbosity() >= 2) {
801+
unitfmt_result bytes_reserved =
802+
unitfmt(s_global_num_trie_blocks * sizeof(TrieNode) * 1024
803+
+ s_global_num_kernel_blocks * sizeof(AggregateKernel) * 1024, unitfmt_bytes);
804+
805+
Log(2).stream() << "Aggregate: max key len " << s_global_max_keylen << ", "
806+
<< s_global_num_kernel_entries << " entries, "
807+
<< s_global_num_trie_entries << " nodes, "
808+
<< s_global_num_trie_blocks + s_global_num_kernel_blocks << " blocks ("
809+
<< bytes_reserved.val << " " << bytes_reserved.symbol << " reserved)"
810+
<< std::endl;
811+
}
807812

808813
// report attribute keys we haven't found
809814
for (size_t i = 0; i < s_key_attribute_ids.size(); ++i)

src/services/trace/Trace.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "caliper/common/Log.h"
4444
#include "caliper/common/RuntimeConfig.h"
4545

46+
#include "caliper/common/c-util/unitfmt.h"
47+
4648
#include "caliper/common/util/spinlock.hpp"
4749

4850
#include <pthread.h>
@@ -262,9 +264,16 @@ namespace
262264
}
263265

264266
if (Log::verbosity() > 1) {
267+
unitfmt_result bytes_reserved
268+
= unitfmt(aggregate_info.reserved, unitfmt_bytes);
269+
unitfmt_result bytes_used
270+
= unitfmt(aggregate_info.used, unitfmt_bytes);
271+
265272
Log(2).stream() << "Trace: "
266-
<< aggregate_info.reserved << " bytes reserved, "
267-
<< aggregate_info.used << " bytes used in "
273+
<< bytes_reserved.val << " "
274+
<< bytes_reserved.symbol << " reserved, "
275+
<< bytes_used.val << " "
276+
<< bytes_used.symbol << " used, "
268277
<< aggregate_info.nchunks << " chunks." << std::endl;
269278
}
270279

0 commit comments

Comments
 (0)