Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to show either VM or FILE, and moved all numbers to the left. #165

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 70 additions & 37 deletions src/bloaty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,7 @@ void Rollup::SortAndAggregateRows(RollupRow* row, const Rollup* base,
// data is in this format, we can print it to the screen (or verify the output
// in unit tests).

void PrintSpaces(size_t n, std::ostream* out) {
for (size_t i = 0; i < n; i++) {
*out << " ";
}
}
namespace {

std::string FixedWidthString(const std::string& input, size_t size) {
if (input.size() < size) {
Expand All @@ -619,6 +615,14 @@ std::string FixedWidthString(const std::string& input, size_t size) {
}
}

bool ShowFile(const OutputOptions& options) {
return options.show != ShowDomain::kShowVM;
}

bool ShowVM(const OutputOptions& options) {
return options.show != ShowDomain::kShowFile;
}

std::string LeftPad(const std::string& input, size_t size) {
std::string ret = input;
while (ret.size() < size) {
Expand Down Expand Up @@ -696,15 +700,24 @@ std::string PercentString(double percent, bool diff_mode) {
}
}

} // namespace

void RollupOutput::PrettyPrintRow(const RollupRow& row, size_t indent,
size_t longest_label,
const OutputOptions& options,
std::ostream* out) const {
*out << FixedWidthString("", indent) << " "
<< PercentString(row.vmpercent, diff_mode_) << " "
<< SiPrint(row.vmsize, diff_mode_) << " "
<< FixedWidthString(row.name, longest_label) << " "
<< SiPrint(row.filesize, diff_mode_) << " "
<< PercentString(row.filepercent, diff_mode_) << "\n";
*out << FixedWidthString("", indent) << " ";

if (ShowFile(options)) {
*out << PercentString(row.filepercent, diff_mode_) << " "
<< SiPrint(row.filesize, diff_mode_) << " ";
}

if (ShowVM(options)) {
*out << PercentString(row.vmpercent, diff_mode_) << " "
<< SiPrint(row.vmsize, diff_mode_) << " ";
}

*out << row.name << "\n";
}

bool RollupOutput::IsSame(const std::string& a, const std::string& b) {
Expand All @@ -720,10 +733,10 @@ bool RollupOutput::IsSame(const std::string& a, const std::string& b) {
}

void RollupOutput::PrettyPrintTree(const RollupRow& row, size_t indent,
size_t longest_label,
const OutputOptions& options,
std::ostream* out) const {
// Rows are printed before their sub-rows.
PrettyPrintRow(row, indent, longest_label, out);
PrettyPrintRow(row, indent, options, out);

if (!row.vmsize && !row.filesize) {
return;
Expand All @@ -736,45 +749,38 @@ void RollupOutput::PrettyPrintTree(const RollupRow& row, size_t indent,
}

for (const auto& child : row.sorted_children) {
PrettyPrintTree(child, indent + 4, longest_label, out);
PrettyPrintTree(child, indent + 4, options, out);
}
}

size_t RollupOutput::CalculateLongestLabel(const RollupRow& row,
int indent) const {
size_t ret = indent + row.name.size();
void RollupOutput::PrettyPrint(const OutputOptions& options,
std::ostream* out) const {
if (ShowFile(options)) {
*out << " FILE SIZE ";
}

for (const auto& child : row.sorted_children) {
ret = std::max(ret, CalculateLongestLabel(child, indent + 4));
if (ShowVM(options)) {
*out << " VM SIZE ";
}

return ret;
}
*out << "\n";

void RollupOutput::PrettyPrint(size_t max_label_len, std::ostream* out) const {
size_t longest_label = toplevel_row_.name.size();
for (const auto& child : toplevel_row_.sorted_children) {
longest_label = std::max(longest_label, CalculateLongestLabel(child, 0));
if (ShowFile(options)) {
*out << " -------------- ";
}

longest_label = std::min(longest_label, max_label_len);

*out << " VM SIZE ";
PrintSpaces(longest_label, out);
*out << " FILE SIZE";
*out << "\n";
if (ShowVM(options)) {
*out << " -------------- ";
}

*out << " -------------- ";
PrintSpaces(longest_label, out);
*out << " --------------";
*out << "\n";

for (const auto& child : toplevel_row_.sorted_children) {
PrettyPrintTree(child, 0, longest_label, out);
PrettyPrintTree(child, 0, options, out);
}

// The "TOTAL" row comes after all other rows.
PrettyPrintRow(toplevel_row_, 0, longest_label, out);
PrettyPrintRow(toplevel_row_, 0, options, out);
}

void RollupOutput::PrintRowToCSV(const RollupRow& row,
Expand Down Expand Up @@ -1801,6 +1807,7 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
string_view option;
int int_option;
uint64_t uint64_option;
bool has_domain = false;

while (!args.IsDone()) {
if (args.TryParseFlag("--")) {
Expand Down Expand Up @@ -1857,6 +1864,17 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
} else {
options->set_max_rows_per_level(int_option);
}
} else if (args.TryParseOption("--domain", &option)) {
has_domain = true;
if (option == "vm") {
output_options->show = ShowDomain::kShowVM;
} else if (option == "file") {
output_options->show = ShowDomain::kShowFile;
} else if (option == "both") {
output_options->show = ShowDomain::kShowBoth;
} else {
THROWF("unknown value for --domain: $0", option);
}
} else if (args.TryParseOption("-s", &option)) {
if (option == "vm") {
options->set_sort_by(Options::SORTBY_VMSIZE);
Expand Down Expand Up @@ -1908,6 +1926,21 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
options->add_data_source("sections");
}

if (has_domain && !options->has_sort_by()) {
// Default to sorting by what we are showing.
switch (output_options->show) {
case ShowDomain::kShowFile:
options->set_sort_by(Options::SORTBY_FILESIZE);
break;
case ShowDomain::kShowVM:
options->set_sort_by(Options::SORTBY_VMSIZE);
break;
case ShowDomain::kShowBoth:
options->set_sort_by(Options::SORTBY_BOTH);
break;
}
}

return true;
}

Expand Down
20 changes: 13 additions & 7 deletions src/bloaty.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,16 @@ enum class OutputFormat {
kTSV,
};

enum class ShowDomain {
kShowFile,
kShowVM,
kShowBoth,
};

struct OutputOptions {
OutputFormat output_format = OutputFormat::kPrettyPrint;
size_t max_label_len = 80;
ShowDomain show = ShowDomain::kShowBoth;
};

struct RollupOutput {
Expand All @@ -465,7 +472,7 @@ struct RollupOutput {
if (!source_names_.empty()) {
switch (options.output_format) {
case bloaty::OutputFormat::kPrettyPrint:
PrettyPrint(options.max_label_len, out);
PrettyPrint(options, out);
break;
case bloaty::OutputFormat::kCSV:
PrintToCSV(out, /*tabs=*/false);
Expand Down Expand Up @@ -505,13 +512,12 @@ struct RollupOutput {
bool diff_mode_ = false;

static bool IsSame(const std::string& a, const std::string& b);
void PrettyPrint(size_t max_label_len, std::ostream* out) const;
void PrettyPrint(const OutputOptions& options, std::ostream* out) const;
void PrintToCSV(std::ostream* out, bool tabs) const;
size_t CalculateLongestLabel(const RollupRow& row, int indent) const;
void PrettyPrintRow(const RollupRow& row, size_t indent, size_t longest_row,
std::ostream* out) const;
void PrettyPrintTree(const RollupRow& row, size_t indent, size_t longest_row,
std::ostream* out) const;
void PrettyPrintRow(const RollupRow& row, size_t indent,
const OutputOptions& options, std::ostream* out) const;
void PrettyPrintTree(const RollupRow& row, size_t indent,
const OutputOptions& options, std::ostream* out) const;
void PrintRowToCSV(const RollupRow& row,
std::vector<std::string> parent_labels,
std::ostream* out, bool tabs) const;
Expand Down