Skip to content

Commit f457444

Browse files
committed
Clean up compaction logging
Summary: Cleaned up compaction logging a little bit. Now file sizes are easier to read. Also, removed the trailing space. Test Plan: verified that i'm happy with logging output: files_size[XRPLF#33(seq=101,sz=98KB,0) XRPLF#31(seq=81,sz=159KB,0) XRPLF#26(seq=0,sz=637KB,0)] Reviewers: sdong, haobo, dhruba Reviewed By: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D18549
1 parent 3e4a9ec commit f457444

File tree

4 files changed

+62
-43
lines changed

4 files changed

+62
-43
lines changed

db/compaction.cc

+23-37
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
// found in the LICENSE file. See the AUTHORS file for names of contributors.
99

1010
#include "db/compaction.h"
11+
12+
#define __STDC_FORMAT_MACROS
13+
#include <inttypes.h>
14+
#include <vector>
15+
1116
#include "db/column_family.h"
17+
#include "util/logging.h"
1218

1319
namespace rocksdb {
1420

@@ -191,71 +197,51 @@ void Compaction::ResetNextCompactionIndex() {
191197
input_version_->ResetNextCompactionIndex(level_);
192198
}
193199

194-
/*
195-
for sizes >=10TB, print "XXTB"
196-
for sizes >=10GB, print "XXGB"
197-
etc.
198-
*/
199-
static void FileSizeSummary(unsigned long long sz, char* output, int len) {
200-
const unsigned long long ull10 = 10;
201-
if (sz >= ull10<<40) {
202-
snprintf(output, len, "%lluTB", sz>>40);
203-
} else if (sz >= ull10<<30) {
204-
snprintf(output, len, "%lluGB", sz>>30);
205-
} else if (sz >= ull10<<20) {
206-
snprintf(output, len, "%lluMB", sz>>20);
207-
} else if (sz >= ull10<<10) {
208-
snprintf(output, len, "%lluKB", sz>>10);
209-
} else {
210-
snprintf(output, len, "%lluB", sz);
211-
}
212-
}
213-
214-
static int InputSummary(std::vector<FileMetaData*>& files, char* output,
215-
int len) {
200+
namespace {
201+
int InputSummary(const std::vector<FileMetaData*>& files, char* output,
202+
int len) {
216203
*output = '\0';
217204
int write = 0;
218205
for (unsigned int i = 0; i < files.size(); i++) {
219206
int sz = len - write;
220207
int ret;
221208
char sztxt[16];
222-
FileSizeSummary((unsigned long long)files.at(i)->file_size, sztxt, 16);
223-
ret = snprintf(output + write, sz, "%lu(%s) ",
224-
(unsigned long)files.at(i)->number,
209+
AppendHumanBytes(files.at(i)->file_size, sztxt, 16);
210+
ret = snprintf(output + write, sz, "%" PRIu64 "(%s) ", files.at(i)->number,
225211
sztxt);
226-
if (ret < 0 || ret >= sz)
227-
break;
212+
if (ret < 0 || ret >= sz) break;
228213
write += ret;
229214
}
230-
return write;
215+
// if files.size() is non-zero, overwrite the last space
216+
return write - !!files.size();
231217
}
218+
} // namespace
232219

233220
void Compaction::Summary(char* output, int len) {
234-
int write = snprintf(output, len,
235-
"Base version %lu Base level %d, seek compaction:%d, inputs: [",
236-
(unsigned long)input_version_->GetVersionNumber(),
237-
level_,
238-
seek_compaction_);
221+
int write =
222+
snprintf(output, len, "Base version %" PRIu64
223+
" Base level %d, seek compaction:%d, inputs: [",
224+
input_version_->GetVersionNumber(), level_, seek_compaction_);
239225
if (write < 0 || write >= len) {
240226
return;
241227
}
242228

243-
write += InputSummary(inputs_[0], output+write, len-write);
229+
write += InputSummary(inputs_[0], output + write, len - write);
244230
if (write < 0 || write >= len) {
245231
return;
246232
}
247233

248-
write += snprintf(output+write, len-write, "],[");
234+
write += snprintf(output + write, len - write, "], [");
249235
if (write < 0 || write >= len) {
250236
return;
251237
}
252238

253-
write += InputSummary(inputs_[1], output+write, len-write);
239+
write += InputSummary(inputs_[1], output + write, len - write);
254240
if (write < 0 || write >= len) {
255241
return;
256242
}
257243

258-
snprintf(output+write, len-write, "]");
244+
snprintf(output + write, len - write, "]");
259245
}
260246

261247
} // namespace rocksdb

db/version_set.cc

+14-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
// Use of this source code is governed by a BSD-style license that can be
88
// found in the LICENSE file. See the AUTHORS file for names of contributors.
99

10-
#define __STDC_FORMAT_MACROS
1110
#include "db/version_set.h"
1211

12+
#define __STDC_FORMAT_MACROS
1313
#include <inttypes.h>
1414
#include <algorithm>
1515
#include <map>
@@ -1151,6 +1151,10 @@ const char* Version::LevelSummary(LevelSummaryStorage* scratch) const {
11511151
if (ret < 0 || ret >= sz) break;
11521152
len += ret;
11531153
}
1154+
if (len > 0) {
1155+
// overwrite the last space
1156+
--len;
1157+
}
11541158
snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, "]");
11551159
return scratch->buffer;
11561160
}
@@ -1160,16 +1164,20 @@ const char* Version::LevelFileSummary(FileSummaryStorage* scratch,
11601164
int len = snprintf(scratch->buffer, sizeof(scratch->buffer), "files_size[");
11611165
for (const auto& f : files_[level]) {
11621166
int sz = sizeof(scratch->buffer) - len;
1167+
char sztxt[16];
1168+
AppendHumanBytes(f->file_size, sztxt, 16);
11631169
int ret = snprintf(scratch->buffer + len, sz,
1164-
"#%lu(seq=%lu,sz=%lu,%lu) ",
1165-
(unsigned long)f->number,
1166-
(unsigned long)f->smallest_seqno,
1167-
(unsigned long)f->file_size,
1168-
(unsigned long)f->being_compacted);
1170+
"#%" PRIu64 "(seq=%" PRIu64 ",sz=%s,%d) ", f->number,
1171+
f->smallest_seqno, sztxt,
1172+
static_cast<int>(f->being_compacted));
11691173
if (ret < 0 || ret >= sz)
11701174
break;
11711175
len += ret;
11721176
}
1177+
// overwrite the last space (only if files_[level].size() is non-zero)
1178+
if (files_[level].size() && len > 0) {
1179+
--len;
1180+
}
11731181
snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, "]");
11741182
return scratch->buffer;
11751183
}

util/logging.cc

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "util/logging.h"
1111

12+
#define __STDC_FORMAT_MACROS
13+
#include <inttypes.h>
1214
#include <errno.h>
1315
#include <stdarg.h>
1416
#include <stdio.h>
@@ -18,6 +20,26 @@
1820

1921
namespace rocksdb {
2022

23+
24+
// for sizes >=10TB, print "XXTB"
25+
// for sizes >=10GB, print "XXGB"
26+
// etc.
27+
// append file size summary to output and return the len
28+
int AppendHumanBytes(uint64_t bytes, char* output, int len) {
29+
const uint64_t ull10 = 10;
30+
if (bytes >= ull10 << 40) {
31+
return snprintf(output, len, "%" PRIu64 "TB", bytes >> 40);
32+
} else if (bytes >= ull10 << 30) {
33+
return snprintf(output, len, "%" PRIu64 "GB", bytes >> 30);
34+
} else if (bytes >= ull10 << 20) {
35+
return snprintf(output, len, "%" PRIu64 "MB", bytes >> 20);
36+
} else if (bytes >= ull10 << 10) {
37+
return snprintf(output, len, "%" PRIu64 "KB", bytes >> 10);
38+
} else {
39+
return snprintf(output, len, "%" PRIu64 "B", bytes);
40+
}
41+
}
42+
2143
void AppendNumberTo(std::string* str, uint64_t num) {
2244
char buf[30];
2345
snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num);

util/logging.h

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace rocksdb {
2121
class Slice;
2222
class WritableFile;
2323

24+
// Append a human-readable size in bytes
25+
int AppendHumanBytes(uint64_t bytes, char* output, int len);
26+
2427
// Append a human-readable printout of "num" to *str
2528
extern void AppendNumberTo(std::string* str, uint64_t num);
2629

0 commit comments

Comments
 (0)