Skip to content

Commit 3865005

Browse files
committed
Add an option to exclude based on source names
#203
1 parent 9293a0f commit 3865005

9 files changed

+623
-14
lines changed

src/bloaty.cc

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ class Rollup {
280280
CreateRows(row, base, options, true);
281281
}
282282

283-
void SetFilterRegex(const ReImpl* regex) { filter_regex_ = regex; }
283+
void SetFilterRegex(const ReImpl* regex_include, const ReImpl* regex_exclude) {
284+
filter_regex_include_ = regex_include;
285+
filter_regex_exclude_ = regex_exclude;
286+
}
284287

285288
// Add the values in "other" from this.
286289
void Add(const Rollup& other) {
@@ -316,7 +319,8 @@ class Rollup {
316319
int64_t filtered_vm_total_ = 0;
317320
int64_t filtered_file_total_ = 0;
318321

319-
const ReImpl* filter_regex_ = nullptr;
322+
const ReImpl* filter_regex_include_ = nullptr;
323+
const ReImpl* filter_regex_exclude_ = nullptr;
320324

321325
// Putting Rollup by value seems to work on some compilers/libs but not
322326
// others.
@@ -335,20 +339,33 @@ class Rollup {
335339
// If there are more entries names[i+1, i+2, etc] add them to sub-rollups.
336340
void AddInternal(const std::vector<std::string>& names, size_t i,
337341
uint64_t size, bool is_vmsize) {
338-
if (filter_regex_ != nullptr) {
342+
if (filter_regex_include_ != nullptr || filter_regex_exclude_ != nullptr) {
339343
// filter_regex_ is only set in the root rollup, which checks the full
340344
// label hierarchy for a match to determine whether a region should be
341345
// considered.
342-
bool any_matched = false;
346+
bool exclude = false;
347+
if (filter_regex_include_ != nullptr) {
348+
bool any_matched = false;
349+
350+
for (const auto& name : names) {
351+
if (ReImpl::PartialMatch(name, *filter_regex_include_)) {
352+
any_matched = true;
353+
break;
354+
}
355+
}
356+
exclude = !any_matched;
357+
}
343358

344-
for (const auto& name : names) {
345-
if (ReImpl::PartialMatch(name, *filter_regex_)) {
346-
any_matched = true;
347-
break;
359+
if (!exclude && filter_regex_exclude_ != nullptr) {
360+
for (const auto& name : names) {
361+
if (ReImpl::PartialMatch(name, *filter_regex_exclude_)) {
362+
exclude = true;
363+
break;
364+
}
348365
}
349366
}
350367

351-
if (!any_matched) {
368+
if (exclude) {
352369
// Ignore this region in the rollup and don't visit sub-rollups.
353370
if (is_vmsize) {
354371
CheckedAdd(&filtered_vm_total_, size);
@@ -1810,13 +1827,17 @@ void Bloaty::ScanAndRollupFiles(const std::vector<std::string>& filenames,
18101827
std::vector<std::thread> threads(num_threads);
18111828
ThreadSafeIterIndex index(filenames.size());
18121829

1813-
std::unique_ptr<ReImpl> regex = nullptr;
1830+
std::unique_ptr<ReImpl> regex_include = nullptr;
1831+
std::unique_ptr<ReImpl> regex_exclude = nullptr;
18141832
if (options_.has_source_filter()) {
1815-
regex = absl::make_unique<ReImpl>(options_.source_filter());
1833+
regex_include = absl::make_unique<ReImpl>(options_.source_filter());
1834+
}
1835+
if (options_.has_exclude_source_filter()) {
1836+
regex_exclude = absl::make_unique<ReImpl>(options_.exclude_source_filter());
18161837
}
18171838

18181839
for (int i = 0; i < num_threads; i++) {
1819-
thread_data[i].rollup.SetFilterRegex(regex.get());
1840+
thread_data[i].rollup.SetFilterRegex(regex_include.get(), regex_exclude.get());
18201841

18211842
threads[i] = std::thread(
18221843
[this, &index, &filenames](PerThreadData* data) {
@@ -1960,6 +1981,10 @@ USAGE: bloaty [OPTION]... FILE... [-- BASE_FILE...]
19601981
--list-sources Show a list of available sources and exit.
19611982
--source-filter=PATTERN
19621983
Only show keys with names matching this pattern.
1984+
--exclude-source-filter=PATTERN
1985+
Exclude keys with names matching this pattern.
1986+
When both --source-filter and --exclude-source-filter
1987+
match the same data, the data will be excluded.
19631988
19641989
Options for debugging Bloaty:
19651990
@@ -2159,6 +2184,8 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
21592184
}
21602185
} else if (args.TryParseOption("--source-filter", &option)) {
21612186
options->set_source_filter(std::string(option));
2187+
} else if (args.TryParseOption("--exclude-source-filter", &option)) {
2188+
options->set_exclude_source_filter(std::string(option));
21622189
} else if (args.TryParseFlag("-v")) {
21632190
options->set_verbose_level(1);
21642191
} else if (args.TryParseFlag("-vv")) {
@@ -2264,7 +2291,14 @@ void BloatyDoMain(const Options& options, const InputFileFactory& file_factory,
22642291
if (options.has_source_filter()) {
22652292
ReImpl re(options.source_filter());
22662293
if (!re.ok()) {
2267-
THROW("invalid regex for source_filter");
2294+
THROW("invalid regex for --source-filter");
2295+
}
2296+
}
2297+
2298+
if (options.has_exclude_source_filter()) {
2299+
ReImpl re(options.exclude_source_filter());
2300+
if (!re.ok()) {
2301+
THROW("invalid regex for --exclude-source-filter");
22682302
}
22692303
}
22702304

src/bloaty.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ message Options {
7575

7676
// Dump raw memory map instead of printing normal output.
7777
optional bool dump_raw_map = 14;
78+
79+
// Regex with which to exclude names in the data sources.
80+
optional string exclude_source_filter = 15;
7881
}
7982

8083
// A custom data source allows users to create their own label space by
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# RUN: %yaml2obj %s -o %t.obj
2+
# RUN: %bloaty --exclude-source-filter text %t.obj | %FileCheck %s --dump-input fail
3+
4+
--- !COFF
5+
OptionalHeader:
6+
AddressOfEntryPoint: 4160
7+
ImageBase: 268435456
8+
SectionAlignment: 4096
9+
FileAlignment: 512
10+
MajorOperatingSystemVersion: 4
11+
MinorOperatingSystemVersion: 0
12+
MajorImageVersion: 0
13+
MinorImageVersion: 0
14+
MajorSubsystemVersion: 4
15+
MinorSubsystemVersion: 0
16+
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
17+
DLLCharacteristics: [ ]
18+
SizeOfStackReserve: 1048576
19+
SizeOfStackCommit: 4096
20+
SizeOfHeapReserve: 1048576
21+
SizeOfHeapCommit: 4096
22+
ExportTable:
23+
RelativeVirtualAddress: 8304
24+
Size: 366
25+
ImportTable:
26+
RelativeVirtualAddress: 8224
27+
Size: 40
28+
ResourceTable:
29+
RelativeVirtualAddress: 0
30+
Size: 0
31+
ExceptionTable:
32+
RelativeVirtualAddress: 0
33+
Size: 0
34+
CertificateTable:
35+
RelativeVirtualAddress: 0
36+
Size: 0
37+
BaseRelocationTable:
38+
RelativeVirtualAddress: 12288
39+
Size: 16
40+
Debug:
41+
RelativeVirtualAddress: 0
42+
Size: 0
43+
Architecture:
44+
RelativeVirtualAddress: 0
45+
Size: 0
46+
GlobalPtr:
47+
RelativeVirtualAddress: 0
48+
Size: 0
49+
TlsTable:
50+
RelativeVirtualAddress: 0
51+
Size: 0
52+
LoadConfigTable:
53+
RelativeVirtualAddress: 0
54+
Size: 0
55+
BoundImport:
56+
RelativeVirtualAddress: 0
57+
Size: 0
58+
IAT:
59+
RelativeVirtualAddress: 8264
60+
Size: 8
61+
DelayImportDescriptor:
62+
RelativeVirtualAddress: 0
63+
Size: 0
64+
ClrRuntimeHeader:
65+
RelativeVirtualAddress: 0
66+
Size: 0
67+
header:
68+
Machine: IMAGE_FILE_MACHINE_I386
69+
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_LOCAL_SYMS_STRIPPED, IMAGE_FILE_32BIT_MACHINE, IMAGE_FILE_DEBUG_STRIPPED, IMAGE_FILE_DLL ]
70+
sections:
71+
- Name: .text
72+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
73+
VirtualAddress: 4096
74+
VirtualSize: 160
75+
SectionData: 5589E581EC0000000090B80020001050E88300000083C404C9C35589E581EC0000000090B80920001050E86900000083C404B800000000E900000000C9C210005589E581EC04000000908B4510508B450C508B450850E8250000008945FC8B45FCE900000000C9C20C00000000000000000000000000000000000000000000005589E581EC0000000090B801000000E900000000C9C20C00FF25482000100000
76+
- Name: .data
77+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
78+
VirtualAddress: 8192
79+
VirtualSize: 480
80+
SectionData: 637572696F757321004D61726B204A616E73656E00000000000000000000000050200000000000000000000058200000482000000000000000000000000000000000000000000000632000000000000063200000000000006D73766372742E646C6C00000070757473000000000000000000000000000000000000001A210000010000000D0000000D00000098200000CC20000000210000801000001A1000004010000000300000003000000030000000300000003000000030000020200000E0210000981000000010000027210000332100003F2100004D2100005E210000712100008221000095210000A9210000BF210000C6210000CB210000D221000000000100020003000400050006000700080009000A000B000C00706533325F7374642E646C6C005F446C6C4D61696E403132005F446C6C4D61696E403136005F5F646C6C7374617274403132005F5F66696E695F61727261795F656E64005F5F66696E695F61727261795F7374617274005F5F696E69745F61727261795F656E64005F5F696E69745F61727261795F7374617274005F5F707265696E69745F61727261795F656E64005F5F707265696E69745F61727261795F7374617274005F6564617461005F656E64005F65746578740068656C6C6F5F7468657265000000
81+
- Name: .reloc
82+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
83+
VirtualAddress: 12288
84+
VirtualSize: 16
85+
SectionData: 00100000100000000B3025309A300000
86+
symbols: []
87+
...
88+
89+
# CHECK: FILE SIZE VM SIZE
90+
# CHECK: -------------- --------------
91+
# CHECK: 33.3% 512 48.4% 480 .data
92+
# CHECK: 33.3% 512 1.6% 16 .reloc
93+
# CHECK: 24.5% 376 37.9% 376 [PE Headers]
94+
# CHECK: 7.8% 120 12.1% 120 [PE Section Headers]
95+
# CHECK: 1.0% 16 0.0% 0 [Unmapped]
96+
# CHECK: 100.0% 1.50Ki 100.0% 992 TOTAL
97+
# CHECK:Filtering enabled (source_filter); omitted file = 512, vm = 160 of entries
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# RUN: %yaml2obj %s -o %t.obj
2+
# RUN: %bloaty --exclude-source-filter text %t.obj | %FileCheck %s --dump-input fail
3+
4+
--- !COFF
5+
OptionalHeader:
6+
AddressOfEntryPoint: 4192
7+
ImageBase: 268435456
8+
SectionAlignment: 4096
9+
FileAlignment: 512
10+
MajorOperatingSystemVersion: 4
11+
MinorOperatingSystemVersion: 0
12+
MajorImageVersion: 0
13+
MinorImageVersion: 0
14+
MajorSubsystemVersion: 4
15+
MinorSubsystemVersion: 0
16+
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
17+
DLLCharacteristics: [ ]
18+
SizeOfStackReserve: 1048576
19+
SizeOfStackCommit: 4096
20+
SizeOfHeapReserve: 1048576
21+
SizeOfHeapCommit: 4096
22+
ExportTable:
23+
RelativeVirtualAddress: 8320
24+
Size: 336
25+
ImportTable:
26+
RelativeVirtualAddress: 8224
27+
Size: 40
28+
ResourceTable:
29+
RelativeVirtualAddress: 0
30+
Size: 0
31+
ExceptionTable:
32+
RelativeVirtualAddress: 12288
33+
Size: 36
34+
CertificateTable:
35+
RelativeVirtualAddress: 0
36+
Size: 0
37+
BaseRelocationTable:
38+
RelativeVirtualAddress: 0
39+
Size: 0
40+
Debug:
41+
RelativeVirtualAddress: 0
42+
Size: 0
43+
Architecture:
44+
RelativeVirtualAddress: 0
45+
Size: 0
46+
GlobalPtr:
47+
RelativeVirtualAddress: 0
48+
Size: 0
49+
TlsTable:
50+
RelativeVirtualAddress: 0
51+
Size: 0
52+
LoadConfigTable:
53+
RelativeVirtualAddress: 0
54+
Size: 0
55+
BoundImport:
56+
RelativeVirtualAddress: 0
57+
Size: 0
58+
IAT:
59+
RelativeVirtualAddress: 8264
60+
Size: 16
61+
DelayImportDescriptor:
62+
RelativeVirtualAddress: 0
63+
Size: 0
64+
ClrRuntimeHeader:
65+
RelativeVirtualAddress: 0
66+
Size: 0
67+
header:
68+
Machine: IMAGE_FILE_MACHINE_AMD64
69+
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LINE_NUMS_STRIPPED, IMAGE_FILE_LOCAL_SYMS_STRIPPED, IMAGE_FILE_LARGE_ADDRESS_AWARE, IMAGE_FILE_DEBUG_STRIPPED, IMAGE_FILE_DLL ]
70+
sections:
71+
- Name: .text
72+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
73+
VirtualAddress: 4096
74+
VirtualSize: 168
75+
SectionData: 554889E54881EC20000000488D0DEE0F0000E889000000C9C30000000104020504030150554889E54881EC2000000048894D10488955184C8945204C894D28488D0DC30F0000E855000000B800000000E900000000C9C3000000000000000000554889E54881EC3000000048894D10488955184C894520488B45204989C08B5518488B4D10E89AFFFFFF8945FC8B45FCE900000000C9C3000104020504030150FF25A20F00000000
76+
- Name: .data
77+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
78+
VirtualAddress: 8192
79+
VirtualSize: 480
80+
SectionData: 637572696F757321004D61726B204A616E73656E0000000000000000000000005820000000000000000000006820000048200000000000000000000000000000000000000000000073200000000000000000000000000000732000000000000000000000000000006D73766372742E646C6C000000707574730000000000000000000000000000000000000020210000010000000C0000000C000000A8200000D820000008210000241000000040000000400000004000000040000000400000004000006010000020200000E0210000A0100000001000002D2100003521000046210000592100006A2100007D21000091210000A7210000B1210000B8210000BD210000C421000000000100020003000400050006000700080009000A000B00706536345F7374642E646C6C00446C6C4D61696E005F5F66696E695F61727261795F656E64005F5F66696E695F61727261795F7374617274005F5F696E69745F61727261795F656E64005F5F696E69745F61727261795F7374617274005F5F707265696E69745F61727261795F656E64005F5F707265696E69745F61727261795F7374617274005F646C6C7374617274005F6564617461005F656E64005F65746578740068656C6C6F5F74686572650000000000000000000000000000000000
81+
- Name: .pdata
82+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
83+
VirtualAddress: 12288
84+
VirtualSize: 36
85+
SectionData: 0B100000191000001C1000002F100000571000001C1000006B1000009710000098100000
86+
symbols: []
87+
...
88+
89+
# CHECK: FILE SIZE VM SIZE
90+
# CHECK: -------------- --------------
91+
# CHECK: 33.3% 512 46.7% 480 .data
92+
# CHECK: 33.3% 512 3.5% 36 .pdata
93+
# CHECK: 25.5% 392 38.1% 392 [PE Headers]
94+
# CHECK: 7.8% 120 11.7% 120 [PE Section Headers]
95+
# CHECK: 100.0% 1.50Ki 100.0% 1.00Ki TOTAL
96+
# CHECK:Filtering enabled (source_filter); omitted file = 512, vm = 168 of entries
97+

0 commit comments

Comments
 (0)