bloaty_test.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "test.h"
  15. TEST_F(BloatyTest, EmptyObjectFile) {
  16. std::string file = "01-empty.o";
  17. uint64_t size;
  18. ASSERT_TRUE(GetFileSize(file, &size));
  19. // Empty .c file should result in a .o file with no vmsize.
  20. RunBloaty({"bloaty", file});
  21. EXPECT_EQ(top_row_->vmsize, 0);
  22. EXPECT_EQ(top_row_->filesize, size);
  23. EXPECT_GT(top_row_->sorted_children.size(), 1);
  24. // Same with segments (we fake segments on .o files).
  25. RunBloaty({"bloaty", "-d", "segments", file});
  26. EXPECT_EQ(top_row_->vmsize, 0);
  27. EXPECT_EQ(top_row_->filesize, size);
  28. EXPECT_GT(top_row_->sorted_children.size(), 1);
  29. // Same with symbols.
  30. RunBloaty({"bloaty", "-d", "symbols", file});
  31. EXPECT_EQ(top_row_->vmsize, 0);
  32. EXPECT_EQ(top_row_->filesize, size);
  33. EXPECT_GT(top_row_->sorted_children.size(), 1);
  34. // We can't run any of these targets against object files.
  35. std::string errmsg = "can't use data source";
  36. AssertBloatyFails({"bloaty", "-d", "compileunits", file}, errmsg);
  37. AssertBloatyFails({"bloaty", "-d", "inlines", file}, errmsg);
  38. }
  39. TEST_F(BloatyTest, SimpleObjectFile) {
  40. std::string file = "02-simple.o";
  41. uint64_t size;
  42. ASSERT_TRUE(GetFileSize(file, &size));
  43. // Test "-n 0" which should return an unlimited number of rows.
  44. RunBloaty({"bloaty", "-n", "0", file});
  45. EXPECT_GT(top_row_->vmsize, 64);
  46. EXPECT_LT(top_row_->vmsize, 300);
  47. EXPECT_EQ(top_row_->filesize, size);
  48. EXPECT_GT(top_row_->sorted_children.size(), 1);
  49. // Same with segments (we fake segments on .o files).
  50. RunBloaty({"bloaty", "-d", "segments", file});
  51. EXPECT_GT(top_row_->vmsize, 64);
  52. EXPECT_LT(top_row_->vmsize, 300);
  53. EXPECT_EQ(top_row_->filesize, size);
  54. EXPECT_GT(top_row_->sorted_children.size(), 1);
  55. // For inputfiles we should get everything attributed to the input file.
  56. RunBloaty({"bloaty", "-d", "inputfiles", file});
  57. AssertChildren(*top_row_, {
  58. std::make_tuple("02-simple.o", kUnknown, kUnknown)
  59. });
  60. // For symbols we should get entries for all our expected symbols.
  61. RunBloaty({"bloaty", "-d", "symbols", "-n", "40", "-s", "vm", file});
  62. AssertChildren(*top_row_, {
  63. std::make_tuple("func1", kUnknown, kSameAsVM),
  64. std::make_tuple("func2", kUnknown, kSameAsVM),
  65. std::make_tuple("bss_a", 8, 0),
  66. std::make_tuple("data_a", 8, 8),
  67. std::make_tuple("rodata_a", 8, 8),
  68. std::make_tuple("bss_b", 4, 0),
  69. std::make_tuple("data_b", 4, 4),
  70. std::make_tuple("rodata_b", 4, 4),
  71. });
  72. RunBloaty({"bloaty", "-d", "sections,symbols", "-n", "50", file});
  73. auto row = FindRow(".bss");
  74. ASSERT_TRUE(row != nullptr);
  75. AssertChildren(*row, {
  76. std::make_tuple("bss_a", 8, 0),
  77. std::make_tuple("bss_b", 4, 0),
  78. });
  79. row = FindRow(".data");
  80. ASSERT_TRUE(row != nullptr);
  81. AssertChildren(*row, {
  82. std::make_tuple("data_a", 8, 8),
  83. std::make_tuple("data_b", 4, 4),
  84. });
  85. row = FindRow(".rodata");
  86. ASSERT_TRUE(row != nullptr);
  87. AssertChildren(*row, {
  88. std::make_tuple("rodata_a", 8, 8),
  89. std::make_tuple("rodata_b", 4, 4),
  90. });
  91. }
  92. TEST_F(BloatyTest, SimpleArchiveFile) {
  93. std::string file = "03-simple.a";
  94. uint64_t size;
  95. ASSERT_TRUE(GetFileSize(file, &size));
  96. RunBloaty({"bloaty", file});
  97. EXPECT_GT(top_row_->vmsize, 8000);
  98. EXPECT_LT(top_row_->vmsize, 12000);
  99. //EXPECT_EQ(top_row_->filesize, size);
  100. EXPECT_GT(top_row_->sorted_children.size(), 3);
  101. RunBloaty({"bloaty", "-d", "segments", file});
  102. EXPECT_GT(top_row_->vmsize, 8000);
  103. EXPECT_LT(top_row_->vmsize, 12000);
  104. //EXPECT_EQ(top_row_->filesize, size);
  105. RunBloaty({"bloaty", "-d", "symbols", "-n", "40", "-s", "vm", file});
  106. AssertChildren(*top_row_, {
  107. std::make_tuple("bar_x", 4000, 4000),
  108. std::make_tuple("foo_x", 4000, 0),
  109. std::make_tuple("bar_func", kUnknown, kSameAsVM),
  110. std::make_tuple("foo_func", kUnknown, kSameAsVM),
  111. std::make_tuple("long_filename_x", 12, 12),
  112. std::make_tuple("bar_y", 4, 4),
  113. std::make_tuple("bar_z", 4, 0),
  114. std::make_tuple("foo_y", 4, 0),
  115. std::make_tuple("long_filename_y", 4, 4),
  116. });
  117. RunBloaty({"bloaty", "-d", "armembers,symbols", file});
  118. AssertChildren(*top_row_,
  119. {
  120. std::make_tuple("bar.o", kUnknown, kUnknown),
  121. std::make_tuple("foo.o", kUnknown, kUnknown),
  122. std::make_tuple("a_filename_longer_than_sixteen_chars.o",
  123. kUnknown, kUnknown),
  124. });
  125. auto row = FindRow("bar.o");
  126. ASSERT_TRUE(row != nullptr);
  127. AssertChildren(*row, {
  128. std::make_tuple("bar_x", 4000, 4000),
  129. std::make_tuple("bar_func", kUnknown, kSameAsVM),
  130. std::make_tuple("bar_y", 4, 4),
  131. std::make_tuple("bar_z", 4, 0),
  132. });
  133. row = FindRow("foo.o");
  134. ASSERT_TRUE(row != nullptr);
  135. AssertChildren(*row, {
  136. std::make_tuple("foo_x", 4000, 0),
  137. std::make_tuple("foo_func", kUnknown, kSameAsVM),
  138. std::make_tuple("foo_y", 4, 0),
  139. });
  140. row = FindRow("a_filename_longer_than_sixteen_chars.o");
  141. ASSERT_TRUE(row != nullptr);
  142. AssertChildren(*row, {
  143. std::make_tuple("long_filename_x", 12, 12),
  144. std::make_tuple("long_filename_y", 4, 4),
  145. });
  146. }
  147. TEST_F(BloatyTest, SimpleSharedObjectFile) {
  148. std::string file = "04-simple.so";
  149. uint64_t size;
  150. ASSERT_TRUE(GetFileSize(file, &size));
  151. RunBloaty({"bloaty", file});
  152. EXPECT_GT(top_row_->vmsize, 8000);
  153. EXPECT_LT(top_row_->vmsize, 12000);
  154. EXPECT_EQ(top_row_->filesize, size);
  155. EXPECT_GT(top_row_->sorted_children.size(), 3);
  156. RunBloaty({"bloaty", "-d", "segments", file});
  157. EXPECT_GT(top_row_->vmsize, 8000);
  158. EXPECT_LT(top_row_->vmsize, 12000);
  159. EXPECT_EQ(top_row_->filesize, size);
  160. RunBloaty({"bloaty", "-d", "symbols", "-n", "50", file});
  161. AssertChildren(*top_row_, {
  162. std::make_tuple("bar_x", 4000, 4000),
  163. std::make_tuple("foo_x", 4000, kUnknown),
  164. std::make_tuple("bar_func", kUnknown, kSameAsVM),
  165. std::make_tuple("foo_func", kUnknown, kSameAsVM),
  166. });
  167. }
  168. TEST_F(BloatyTest, SimpleBinary) {
  169. std::string file = "05-binary.bin";
  170. uint64_t size;
  171. ASSERT_TRUE(GetFileSize(file, &size));
  172. RunBloaty({"bloaty", file});
  173. EXPECT_GT(top_row_->vmsize, 8000);
  174. EXPECT_LT(top_row_->vmsize, 12000);
  175. EXPECT_EQ(top_row_->filesize, size);
  176. EXPECT_GT(top_row_->sorted_children.size(), 3);
  177. RunBloaty({"bloaty", "-d", "segments", file});
  178. EXPECT_GT(top_row_->vmsize, 8000);
  179. EXPECT_LT(top_row_->vmsize, 12000);
  180. EXPECT_EQ(top_row_->filesize, size);
  181. RunBloaty({"bloaty", "-d", "symbols", "-n", "50", "-s", "vm", file});
  182. AssertChildren(*top_row_, {
  183. std::make_tuple("bar_x", 4000, 4000),
  184. std::make_tuple("foo_x", 4000, 0),
  185. std::make_tuple("bar_func", kUnknown, kSameAsVM),
  186. std::make_tuple("foo_func", kUnknown, kSameAsVM),
  187. std::make_tuple("main", kUnknown, kSameAsVM),
  188. std::make_tuple("bar_y", 4, 4),
  189. std::make_tuple("bar_z", 4, 0),
  190. std::make_tuple("foo_y", 4, 0)
  191. });
  192. RunBloaty({"bloaty", "-d", "compileunits,symbols", file});
  193. auto row = FindRow("bar.o.c");
  194. ASSERT_TRUE(row != nullptr);
  195. // This only includes functions (not data) for now.
  196. AssertChildren(*row, {
  197. std::make_tuple("bar_x", 4000, kSameAsVM),
  198. std::make_tuple("bar_func", kUnknown, kSameAsVM),
  199. std::make_tuple("bar_y", kUnknown, kSameAsVM),
  200. std::make_tuple("bar_z", kUnknown, kSameAsVM),
  201. });
  202. row = FindRow("foo.o.c");
  203. ASSERT_TRUE(row != nullptr);
  204. // This only includes functions (not data) for now.
  205. AssertChildren(*row, {
  206. std::make_tuple("foo_x", 4000, 0),
  207. std::make_tuple("foo_func", kUnknown, kSameAsVM),
  208. std::make_tuple("foo_y", kUnknown, kSameAsVM),
  209. });
  210. RunBloaty({"bloaty", "-d", "sections,inlines", file});
  211. }
  212. TEST_F(BloatyTest, InputFiles) {
  213. std::string file1 = "05-binary.bin";
  214. std::string file2 = "07-binary-stripped.bin";
  215. uint64_t size1, size2;
  216. ASSERT_TRUE(GetFileSize(file1, &size1));
  217. ASSERT_TRUE(GetFileSize(file2, &size2));
  218. RunBloaty({"bloaty", file1, file2, "-d", "inputfiles"});
  219. AssertChildren(*top_row_,
  220. {std::make_tuple(file1, kUnknown, static_cast<int>(size1)),
  221. std::make_tuple(file2, kUnknown, static_cast<int>(size2))});
  222. // Should work with custom data sources.
  223. bloaty::Options options;
  224. google::protobuf::TextFormat::ParseFromString(R"(
  225. filename: "05-binary.bin"
  226. filename: "07-binary-stripped.bin"
  227. custom_data_source {
  228. name: "rewritten_inputfiles"
  229. base_data_source: "inputfiles"
  230. rewrite: {
  231. pattern: "binary"
  232. replacement: "binary"
  233. }
  234. }
  235. data_source: "rewritten_inputfiles"
  236. )", &options);
  237. RunBloatyWithOptions(options, bloaty::OutputOptions());
  238. AssertChildren(*top_row_, {std::make_tuple("binary", kUnknown,
  239. static_cast<int>(size1 + size2))});
  240. }
  241. TEST_F(BloatyTest, DiffMode) {
  242. RunBloaty({"bloaty", "06-diff.a", "--", "03-simple.a", "-d", "symbols"});
  243. AssertChildren(*top_row_, {
  244. std::make_tuple("foo_func", kUnknown, kSameAsVM),
  245. std::make_tuple("foo_y", 4, 0)
  246. });
  247. }
  248. TEST_F(BloatyTest, SeparateDebug) {
  249. RunBloaty({"bloaty", "--debug-file=05-binary.bin", "07-binary-stripped.bin",
  250. "-d", "symbols"});
  251. }