Remove malloc from FormatFileNumber
Summary: Replace unnecessary malloc with stack allocation Test Plan: make all check Reviewers: sdong Reviewed By: sdong Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D21771
This commit is contained in:
parent
48081777f3
commit
0c9dc9f8e0
@ -720,10 +720,11 @@ Compaction* UniversalCompactionPicker::PickCompactionUniversalReadAmp(
|
|||||||
// first candidate to be compacted.
|
// first candidate to be compacted.
|
||||||
uint64_t candidate_size = f != nullptr? f->compensated_file_size : 0;
|
uint64_t candidate_size = f != nullptr? f->compensated_file_size : 0;
|
||||||
if (f != nullptr) {
|
if (f != nullptr) {
|
||||||
LogToBuffer(
|
char file_num_buf[kFormatFileNumberBufSize];
|
||||||
log_buffer, "[%s] Universal: Possible candidate file %s[%d].",
|
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId(), file_num_buf,
|
||||||
version->cfd_->GetName().c_str(),
|
sizeof(file_num_buf));
|
||||||
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId()).c_str(), loop);
|
LogToBuffer(log_buffer, "[%s] Universal: Possible candidate file %s[%d].",
|
||||||
|
version->cfd_->GetName().c_str(), file_num_buf, loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the suceeding files need compaction.
|
// Check if the suceeding files need compaction.
|
||||||
@ -815,12 +816,14 @@ Compaction* UniversalCompactionPicker::PickCompactionUniversalReadAmp(
|
|||||||
for (unsigned int i = start_index; i < first_index_after; i++) {
|
for (unsigned int i = start_index; i < first_index_after; i++) {
|
||||||
FileMetaData* f = c->input_version_->files_[level][i];
|
FileMetaData* f = c->input_version_->files_[level][i];
|
||||||
c->inputs_[0].files.push_back(f);
|
c->inputs_[0].files.push_back(f);
|
||||||
|
char file_num_buf[kFormatFileNumberBufSize];
|
||||||
|
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId(), file_num_buf,
|
||||||
|
sizeof(file_num_buf));
|
||||||
LogToBuffer(log_buffer,
|
LogToBuffer(log_buffer,
|
||||||
"[%s] Universal: Picking file %s[%d] "
|
"[%s] Universal: Picking file %s[%d] "
|
||||||
"with size %" PRIu64 " (compensated size %" PRIu64 ")\n",
|
"with size %" PRIu64 " (compensated size %" PRIu64 ")\n",
|
||||||
version->cfd_->GetName().c_str(),
|
version->cfd_->GetName().c_str(), file_num_buf, i,
|
||||||
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId()).c_str(),
|
f->fd.GetFileSize(), f->compensated_file_size);
|
||||||
i, f->fd.GetFileSize(), f->compensated_file_size);
|
|
||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
@ -854,29 +857,35 @@ Compaction* UniversalCompactionPicker::PickCompactionUniversalSizeAmp(
|
|||||||
start_index = loop; // Consider this as the first candidate.
|
start_index = loop; // Consider this as the first candidate.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
char file_num_buf[kFormatFileNumberBufSize];
|
||||||
|
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId(), file_num_buf,
|
||||||
|
sizeof(file_num_buf));
|
||||||
LogToBuffer(log_buffer, "[%s] Universal: skipping file %s[%d] compacted %s",
|
LogToBuffer(log_buffer, "[%s] Universal: skipping file %s[%d] compacted %s",
|
||||||
version->cfd_->GetName().c_str(),
|
version->cfd_->GetName().c_str(), file_num_buf, loop,
|
||||||
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId()).c_str(),
|
" cannot be a candidate to reduce size amp.\n");
|
||||||
loop, " cannot be a candidate to reduce size amp.\n");
|
|
||||||
f = nullptr;
|
f = nullptr;
|
||||||
}
|
}
|
||||||
if (f == nullptr) {
|
if (f == nullptr) {
|
||||||
return nullptr; // no candidate files
|
return nullptr; // no candidate files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char file_num_buf[kFormatFileNumberBufSize];
|
||||||
|
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId(), file_num_buf,
|
||||||
|
sizeof(file_num_buf));
|
||||||
LogToBuffer(log_buffer, "[%s] Universal: First candidate file %s[%d] %s",
|
LogToBuffer(log_buffer, "[%s] Universal: First candidate file %s[%d] %s",
|
||||||
version->cfd_->GetName().c_str(),
|
version->cfd_->GetName().c_str(), file_num_buf, start_index,
|
||||||
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId()).c_str(),
|
" to reduce size amp.\n");
|
||||||
start_index, " to reduce size amp.\n");
|
|
||||||
|
|
||||||
// keep adding up all the remaining files
|
// keep adding up all the remaining files
|
||||||
for (unsigned int loop = start_index; loop < files.size() - 1; loop++) {
|
for (unsigned int loop = start_index; loop < files.size() - 1; loop++) {
|
||||||
f = files[loop];
|
f = files[loop];
|
||||||
if (f->being_compacted) {
|
if (f->being_compacted) {
|
||||||
|
char file_num_buf[kFormatFileNumberBufSize];
|
||||||
|
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId(), file_num_buf,
|
||||||
|
sizeof(file_num_buf));
|
||||||
LogToBuffer(
|
LogToBuffer(
|
||||||
log_buffer, "[%s] Universal: Possible candidate file %s[%d] %s.",
|
log_buffer, "[%s] Universal: Possible candidate file %s[%d] %s.",
|
||||||
version->cfd_->GetName().c_str(),
|
version->cfd_->GetName().c_str(), file_num_buf, loop,
|
||||||
FormatFileNumber(f->fd.GetNumber(), f->fd.GetPathId()).c_str(), loop,
|
|
||||||
" is already being compacted. No size amp reduction possible.\n");
|
" is already being compacted. No size amp reduction possible.\n");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,9 @@
|
|||||||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||||||
// 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/filename.h"
|
#include "db/filename.h"
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -83,11 +84,17 @@ std::string TableFileName(const std::vector<DbPath>& db_paths, uint64_t number,
|
|||||||
return MakeTableFileName(path, number);
|
return MakeTableFileName(path, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FormatFileNumber(uint64_t number, uint32_t path_id) {
|
const size_t kFormatFileNumberBufSize = 38;
|
||||||
|
|
||||||
|
void FormatFileNumber(uint64_t number, uint32_t path_id, char* out_buf,
|
||||||
|
size_t out_buf_size) {
|
||||||
if (path_id == 0) {
|
if (path_id == 0) {
|
||||||
return std::to_string(number);
|
snprintf(out_buf, out_buf_size, "%" PRIu64, number);
|
||||||
} else {
|
} else {
|
||||||
return std::to_string(number) + "(path " + std::to_string(path_id) + ")";
|
snprintf(out_buf, out_buf_size, "%" PRIu64
|
||||||
|
"(path "
|
||||||
|
"%" PRIu32 ")",
|
||||||
|
number, path_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,11 @@ extern std::string MakeTableFileName(const std::string& name, uint64_t number);
|
|||||||
extern std::string TableFileName(const std::vector<DbPath>& db_paths,
|
extern std::string TableFileName(const std::vector<DbPath>& db_paths,
|
||||||
uint64_t number, uint32_t path_id);
|
uint64_t number, uint32_t path_id);
|
||||||
|
|
||||||
extern std::string FormatFileNumber(uint64_t number, uint32_t path_id);
|
// Sufficient buffer size for FormatFileNumber.
|
||||||
|
extern const size_t kFormatFileNumberBufSize;
|
||||||
|
|
||||||
|
extern void FormatFileNumber(uint64_t number, uint32_t path_id, char* out_buf,
|
||||||
|
size_t out_buf_size);
|
||||||
|
|
||||||
// Return the name of the descriptor file for the db named by
|
// Return the name of the descriptor file for the db named by
|
||||||
// "dbname" and the specified incarnation number. The result will be
|
// "dbname" and the specified incarnation number. The result will be
|
||||||
|
@ -265,9 +265,10 @@ class Repairer {
|
|||||||
if (!status.ok()) {
|
if (!status.ok()) {
|
||||||
std::string fname = TableFileName(
|
std::string fname = TableFileName(
|
||||||
options_.db_paths, t.meta.fd.GetNumber(), t.meta.fd.GetPathId());
|
options_.db_paths, t.meta.fd.GetNumber(), t.meta.fd.GetPathId());
|
||||||
Log(options_.info_log, "Table #%s: ignoring %s",
|
char file_num_buf[kFormatFileNumberBufSize];
|
||||||
FormatFileNumber(t.meta.fd.GetNumber(), t.meta.fd.GetPathId())
|
FormatFileNumber(t.meta.fd.GetNumber(), t.meta.fd.GetPathId(),
|
||||||
.c_str(),
|
file_num_buf, sizeof(file_num_buf));
|
||||||
|
Log(options_.info_log, "Table #%s: ignoring %s", file_num_buf,
|
||||||
status.ToString().c_str());
|
status.ToString().c_str());
|
||||||
ArchiveFile(fname);
|
ArchiveFile(fname);
|
||||||
} else {
|
} else {
|
||||||
|
@ -1393,7 +1393,7 @@ const char* Version::LevelFileSummary(FileSummaryStorage* scratch,
|
|||||||
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];
|
char sztxt[16];
|
||||||
AppendHumanBytes(f->fd.GetFileSize(), sztxt, 16);
|
AppendHumanBytes(f->fd.GetFileSize(), sztxt, sizeof(sztxt));
|
||||||
int ret = snprintf(scratch->buffer + len, sz,
|
int ret = snprintf(scratch->buffer + len, sz,
|
||||||
"#%" PRIu64 "(seq=%" PRIu64 ",sz=%s,%d) ",
|
"#%" PRIu64 "(seq=%" PRIu64 ",sz=%s,%d) ",
|
||||||
f->fd.GetNumber(), f->smallest_seqno, sztxt,
|
f->fd.GetNumber(), f->smallest_seqno, sztxt,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user