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[#33(seq=101,sz=98KB,0) #31(seq=81,sz=159KB,0) #26(seq=0,sz=637KB,0)]

Reviewers: sdong, haobo, dhruba

Reviewed By: haobo

CC: leveldb

Differential Revision: https://reviews.facebook.net/D18549
This commit is contained in:
Igor Canadi 2014-05-14 12:13:50 -07:00
parent 3e4a9ec241
commit f4574449e9
4 changed files with 62 additions and 43 deletions

View File

@ -8,7 +8,13 @@
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "db/compaction.h" #include "db/compaction.h"
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <vector>
#include "db/column_family.h" #include "db/column_family.h"
#include "util/logging.h"
namespace rocksdb { namespace rocksdb {
@ -191,71 +197,51 @@ void Compaction::ResetNextCompactionIndex() {
input_version_->ResetNextCompactionIndex(level_); input_version_->ResetNextCompactionIndex(level_);
} }
/* namespace {
for sizes >=10TB, print "XXTB" int InputSummary(const std::vector<FileMetaData*>& files, char* output,
for sizes >=10GB, print "XXGB" int len) {
etc.
*/
static void FileSizeSummary(unsigned long long sz, char* output, int len) {
const unsigned long long ull10 = 10;
if (sz >= ull10<<40) {
snprintf(output, len, "%lluTB", sz>>40);
} else if (sz >= ull10<<30) {
snprintf(output, len, "%lluGB", sz>>30);
} else if (sz >= ull10<<20) {
snprintf(output, len, "%lluMB", sz>>20);
} else if (sz >= ull10<<10) {
snprintf(output, len, "%lluKB", sz>>10);
} else {
snprintf(output, len, "%lluB", sz);
}
}
static int InputSummary(std::vector<FileMetaData*>& files, char* output,
int len) {
*output = '\0'; *output = '\0';
int write = 0; int write = 0;
for (unsigned int i = 0; i < files.size(); i++) { for (unsigned int i = 0; i < files.size(); i++) {
int sz = len - write; int sz = len - write;
int ret; int ret;
char sztxt[16]; char sztxt[16];
FileSizeSummary((unsigned long long)files.at(i)->file_size, sztxt, 16); AppendHumanBytes(files.at(i)->file_size, sztxt, 16);
ret = snprintf(output + write, sz, "%lu(%s) ", ret = snprintf(output + write, sz, "%" PRIu64 "(%s) ", files.at(i)->number,
(unsigned long)files.at(i)->number,
sztxt); sztxt);
if (ret < 0 || ret >= sz) if (ret < 0 || ret >= sz) break;
break;
write += ret; write += ret;
} }
return write; // if files.size() is non-zero, overwrite the last space
return write - !!files.size();
} }
} // namespace
void Compaction::Summary(char* output, int len) { void Compaction::Summary(char* output, int len) {
int write = snprintf(output, len, int write =
"Base version %lu Base level %d, seek compaction:%d, inputs: [", snprintf(output, len, "Base version %" PRIu64
(unsigned long)input_version_->GetVersionNumber(), " Base level %d, seek compaction:%d, inputs: [",
level_, input_version_->GetVersionNumber(), level_, seek_compaction_);
seek_compaction_);
if (write < 0 || write >= len) { if (write < 0 || write >= len) {
return; return;
} }
write += InputSummary(inputs_[0], output+write, len-write); write += InputSummary(inputs_[0], output + write, len - write);
if (write < 0 || write >= len) { if (write < 0 || write >= len) {
return; return;
} }
write += snprintf(output+write, len-write, "],["); write += snprintf(output + write, len - write, "], [");
if (write < 0 || write >= len) { if (write < 0 || write >= len) {
return; return;
} }
write += InputSummary(inputs_[1], output+write, len-write); write += InputSummary(inputs_[1], output + write, len - write);
if (write < 0 || write >= len) { if (write < 0 || write >= len) {
return; return;
} }
snprintf(output+write, len-write, "]"); snprintf(output + write, len - write, "]");
} }
} // namespace rocksdb } // namespace rocksdb

View File

@ -7,9 +7,9 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#define __STDC_FORMAT_MACROS
#include "db/version_set.h" #include "db/version_set.h"
#define __STDC_FORMAT_MACROS
#include <inttypes.h> #include <inttypes.h>
#include <algorithm> #include <algorithm>
#include <map> #include <map>
@ -1151,6 +1151,10 @@ const char* Version::LevelSummary(LevelSummaryStorage* scratch) const {
if (ret < 0 || ret >= sz) break; if (ret < 0 || ret >= sz) break;
len += ret; len += ret;
} }
if (len > 0) {
// overwrite the last space
--len;
}
snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, "]"); snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, "]");
return scratch->buffer; return scratch->buffer;
} }
@ -1160,16 +1164,20 @@ const char* Version::LevelFileSummary(FileSummaryStorage* scratch,
int len = snprintf(scratch->buffer, sizeof(scratch->buffer), "files_size["); int len = snprintf(scratch->buffer, sizeof(scratch->buffer), "files_size[");
for (const auto& f : files_[level]) { for (const auto& f : files_[level]) {
int sz = sizeof(scratch->buffer) - len; int sz = sizeof(scratch->buffer) - len;
char sztxt[16];
AppendHumanBytes(f->file_size, sztxt, 16);
int ret = snprintf(scratch->buffer + len, sz, int ret = snprintf(scratch->buffer + len, sz,
"#%lu(seq=%lu,sz=%lu,%lu) ", "#%" PRIu64 "(seq=%" PRIu64 ",sz=%s,%d) ", f->number,
(unsigned long)f->number, f->smallest_seqno, sztxt,
(unsigned long)f->smallest_seqno, static_cast<int>(f->being_compacted));
(unsigned long)f->file_size,
(unsigned long)f->being_compacted);
if (ret < 0 || ret >= sz) if (ret < 0 || ret >= sz)
break; break;
len += ret; len += ret;
} }
// overwrite the last space (only if files_[level].size() is non-zero)
if (files_[level].size() && len > 0) {
--len;
}
snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, "]"); snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, "]");
return scratch->buffer; return scratch->buffer;
} }

View File

@ -9,6 +9,8 @@
#include "util/logging.h" #include "util/logging.h"
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
@ -18,6 +20,26 @@
namespace rocksdb { namespace rocksdb {
// for sizes >=10TB, print "XXTB"
// for sizes >=10GB, print "XXGB"
// etc.
// append file size summary to output and return the len
int AppendHumanBytes(uint64_t bytes, char* output, int len) {
const uint64_t ull10 = 10;
if (bytes >= ull10 << 40) {
return snprintf(output, len, "%" PRIu64 "TB", bytes >> 40);
} else if (bytes >= ull10 << 30) {
return snprintf(output, len, "%" PRIu64 "GB", bytes >> 30);
} else if (bytes >= ull10 << 20) {
return snprintf(output, len, "%" PRIu64 "MB", bytes >> 20);
} else if (bytes >= ull10 << 10) {
return snprintf(output, len, "%" PRIu64 "KB", bytes >> 10);
} else {
return snprintf(output, len, "%" PRIu64 "B", bytes);
}
}
void AppendNumberTo(std::string* str, uint64_t num) { void AppendNumberTo(std::string* str, uint64_t num) {
char buf[30]; char buf[30];
snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num); snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num);

View File

@ -21,6 +21,9 @@ namespace rocksdb {
class Slice; class Slice;
class WritableFile; class WritableFile;
// Append a human-readable size in bytes
int AppendHumanBytes(uint64_t bytes, char* output, int len);
// Append a human-readable printout of "num" to *str // Append a human-readable printout of "num" to *str
extern void AppendNumberTo(std::string* str, uint64_t num); extern void AppendNumberTo(std::string* str, uint64_t num);