record_file_writer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "record_file.h"
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <sys/mman.h>
  20. #include <unistd.h>
  21. #include <algorithm>
  22. #include <set>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <vector>
  26. #include <android-base/file.h>
  27. #include <android-base/logging.h>
  28. #include "dso.h"
  29. #include "event_attr.h"
  30. #include "perf_event.h"
  31. #include "record.h"
  32. #include "utils.h"
  33. using namespace PerfFileFormat;
  34. std::unique_ptr<RecordFileWriter> RecordFileWriter::CreateInstance(const std::string& filename) {
  35. // Remove old perf.data to avoid file ownership problems.
  36. std::string err;
  37. if (!android::base::RemoveFileIfExists(filename, &err)) {
  38. LOG(ERROR) << "failed to remove file " << filename << ": " << err;
  39. return nullptr;
  40. }
  41. FILE* fp = fopen(filename.c_str(), "web+");
  42. if (fp == nullptr) {
  43. PLOG(ERROR) << "failed to open record file '" << filename << "'";
  44. return nullptr;
  45. }
  46. return std::unique_ptr<RecordFileWriter>(new RecordFileWriter(filename, fp));
  47. }
  48. RecordFileWriter::RecordFileWriter(const std::string& filename, FILE* fp)
  49. : filename_(filename),
  50. record_fp_(fp),
  51. attr_section_offset_(0),
  52. attr_section_size_(0),
  53. data_section_offset_(0),
  54. data_section_size_(0),
  55. feature_section_offset_(0),
  56. feature_count_(0) {
  57. }
  58. RecordFileWriter::~RecordFileWriter() {
  59. if (record_fp_ != nullptr) {
  60. fclose(record_fp_);
  61. unlink(filename_.c_str());
  62. }
  63. }
  64. bool RecordFileWriter::WriteAttrSection(const std::vector<EventAttrWithId>& attr_ids) {
  65. if (attr_ids.empty()) {
  66. return false;
  67. }
  68. // Skip file header part.
  69. if (fseek(record_fp_, sizeof(FileHeader), SEEK_SET) == -1) {
  70. return false;
  71. }
  72. // Write id section.
  73. uint64_t id_section_offset;
  74. if (!GetFilePos(&id_section_offset)) {
  75. return false;
  76. }
  77. for (auto& attr_id : attr_ids) {
  78. if (!Write(attr_id.ids.data(), attr_id.ids.size() * sizeof(uint64_t))) {
  79. return false;
  80. }
  81. }
  82. // Write attr section.
  83. uint64_t attr_section_offset;
  84. if (!GetFilePos(&attr_section_offset)) {
  85. return false;
  86. }
  87. for (auto& attr_id : attr_ids) {
  88. FileAttr file_attr;
  89. file_attr.attr = *attr_id.attr;
  90. file_attr.ids.offset = id_section_offset;
  91. file_attr.ids.size = attr_id.ids.size() * sizeof(uint64_t);
  92. id_section_offset += file_attr.ids.size;
  93. if (!Write(&file_attr, sizeof(file_attr))) {
  94. return false;
  95. }
  96. }
  97. uint64_t data_section_offset;
  98. if (!GetFilePos(&data_section_offset)) {
  99. return false;
  100. }
  101. attr_section_offset_ = attr_section_offset;
  102. attr_section_size_ = data_section_offset - attr_section_offset;
  103. data_section_offset_ = data_section_offset;
  104. // Save event_attr for use when reading records.
  105. event_attr_ = *attr_ids[0].attr;
  106. return true;
  107. }
  108. bool RecordFileWriter::WriteRecord(const Record& record) {
  109. // linux-tools-perf only accepts records with size <= 65535 bytes. To make
  110. // perf.data generated by simpleperf be able to be parsed by linux-tools-perf,
  111. // Split simpleperf custom records which are > 65535 into a bunch of
  112. // RECORD_SPLIT records, followed by a RECORD_SPLIT_END record.
  113. constexpr uint32_t RECORD_SIZE_LIMIT = 65535;
  114. if (record.size() <= RECORD_SIZE_LIMIT) {
  115. WriteData(record.Binary(), record.size());
  116. return true;
  117. }
  118. CHECK_GT(record.type(), SIMPLE_PERF_RECORD_TYPE_START);
  119. const char* p = record.Binary();
  120. uint32_t left_bytes = static_cast<uint32_t>(record.size());
  121. RecordHeader header;
  122. header.type = SIMPLE_PERF_RECORD_SPLIT;
  123. char header_buf[Record::header_size()];
  124. char* header_p;
  125. while (left_bytes > 0) {
  126. uint32_t bytes_to_write = std::min(RECORD_SIZE_LIMIT - Record::header_size(), left_bytes);
  127. header.size = bytes_to_write + Record::header_size();
  128. header_p = header_buf;
  129. header.MoveToBinaryFormat(header_p);
  130. if (!WriteData(header_buf, Record::header_size())) {
  131. return false;
  132. }
  133. if (!WriteData(p, bytes_to_write)) {
  134. return false;
  135. }
  136. p += bytes_to_write;
  137. left_bytes -= bytes_to_write;
  138. }
  139. header.type = SIMPLE_PERF_RECORD_SPLIT_END;
  140. header.size = Record::header_size();
  141. header_p = header_buf;
  142. header.MoveToBinaryFormat(header_p);
  143. return WriteData(header_buf, Record::header_size());
  144. }
  145. bool RecordFileWriter::WriteData(const void* buf, size_t len) {
  146. if (!Write(buf, len)) {
  147. return false;
  148. }
  149. data_section_size_ += len;
  150. return true;
  151. }
  152. bool RecordFileWriter::Write(const void* buf, size_t len) {
  153. if (len != 0u && fwrite(buf, len, 1, record_fp_) != 1) {
  154. PLOG(ERROR) << "failed to write to record file '" << filename_ << "'";
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool RecordFileWriter::Read(void* buf, size_t len) {
  160. if (len != 0u && fread(buf, len, 1, record_fp_) != 1) {
  161. PLOG(ERROR) << "failed to read record file '" << filename_ << "'";
  162. return false;
  163. }
  164. return true;
  165. }
  166. bool RecordFileWriter::ReadDataSection(const std::function<void(const Record*)>& callback) {
  167. if (fseek(record_fp_, data_section_offset_, SEEK_SET) == -1) {
  168. PLOG(ERROR) << "fseek() failed";
  169. return false;
  170. }
  171. std::vector<char> record_buf(512);
  172. uint64_t read_pos = 0;
  173. while (read_pos < data_section_size_) {
  174. if (!Read(record_buf.data(), Record::header_size())) {
  175. return false;
  176. }
  177. RecordHeader header(record_buf.data());
  178. if (record_buf.size() < header.size) {
  179. record_buf.resize(header.size);
  180. }
  181. if (!Read(record_buf.data() + Record::header_size(), header.size - Record::header_size())) {
  182. return false;
  183. }
  184. read_pos += header.size;
  185. std::unique_ptr<Record> r = ReadRecordFromBuffer(event_attr_, header.type, record_buf.data());
  186. callback(r.get());
  187. }
  188. return true;
  189. }
  190. bool RecordFileWriter::GetFilePos(uint64_t* file_pos) {
  191. off_t offset = ftello(record_fp_);
  192. if (offset == -1) {
  193. PLOG(ERROR) << "ftello() failed";
  194. return false;
  195. }
  196. *file_pos = static_cast<uint64_t>(offset);
  197. return true;
  198. }
  199. bool RecordFileWriter::BeginWriteFeatures(size_t feature_count) {
  200. feature_section_offset_ = data_section_offset_ + data_section_size_;
  201. feature_count_ = feature_count;
  202. uint64_t feature_header_size = feature_count * sizeof(SectionDesc);
  203. // Reserve enough space in the record file for the feature header.
  204. std::vector<unsigned char> zero_data(feature_header_size);
  205. if (fseek(record_fp_, feature_section_offset_, SEEK_SET) == -1) {
  206. PLOG(ERROR) << "fseek() failed";
  207. return false;
  208. }
  209. return Write(zero_data.data(), zero_data.size());
  210. }
  211. bool RecordFileWriter::WriteBuildIdFeature(const std::vector<BuildIdRecord>& build_id_records) {
  212. if (!WriteFeatureBegin(FEAT_BUILD_ID)) {
  213. return false;
  214. }
  215. for (auto& record : build_id_records) {
  216. if (!Write(record.Binary(), record.size())) {
  217. return false;
  218. }
  219. }
  220. return WriteFeatureEnd(FEAT_BUILD_ID);
  221. }
  222. bool RecordFileWriter::WriteStringWithLength(const std::string& s) {
  223. uint32_t len = static_cast<uint32_t>(Align(s.size() + 1, 64));
  224. if (!Write(&len, sizeof(len))) {
  225. return false;
  226. }
  227. if (!Write(&s[0], s.size() + 1)) {
  228. return false;
  229. }
  230. size_t pad_size = Align(s.size() + 1, 64) - s.size() - 1;
  231. if (pad_size > 0u) {
  232. char align_buf[pad_size];
  233. memset(align_buf, '\0', pad_size);
  234. if (!Write(align_buf, pad_size)) {
  235. return false;
  236. }
  237. }
  238. return true;
  239. }
  240. bool RecordFileWriter::WriteFeatureString(int feature, const std::string& s) {
  241. if (!WriteFeatureBegin(feature)) {
  242. return false;
  243. }
  244. if (!WriteStringWithLength(s)) {
  245. return false;
  246. }
  247. return WriteFeatureEnd(feature);
  248. }
  249. bool RecordFileWriter::WriteCmdlineFeature(const std::vector<std::string>& cmdline) {
  250. if (!WriteFeatureBegin(FEAT_CMDLINE)) {
  251. return false;
  252. }
  253. uint32_t arg_count = cmdline.size();
  254. if (!Write(&arg_count, sizeof(arg_count))) {
  255. return false;
  256. }
  257. for (auto& arg : cmdline) {
  258. if (!WriteStringWithLength(arg)) {
  259. return false;
  260. }
  261. }
  262. return WriteFeatureEnd(FEAT_CMDLINE);
  263. }
  264. bool RecordFileWriter::WriteBranchStackFeature() {
  265. if (!WriteFeatureBegin(FEAT_BRANCH_STACK)) {
  266. return false;
  267. }
  268. return WriteFeatureEnd(FEAT_BRANCH_STACK);
  269. }
  270. bool RecordFileWriter::WriteFileFeatures(const std::vector<Dso*>& files) {
  271. for (Dso* dso : files) {
  272. // Always want to dump dex file offsets for DSO_DEX_FILE type.
  273. if (!dso->HasDumpId() && dso->type() != DSO_DEX_FILE) {
  274. continue;
  275. }
  276. uint32_t dso_type = dso->type();
  277. uint64_t min_vaddr;
  278. uint64_t file_offset_of_min_vaddr;
  279. dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
  280. // Dumping all symbols in hit files takes too much space, so only dump
  281. // needed symbols.
  282. const std::vector<Symbol>& symbols = dso->GetSymbols();
  283. std::vector<const Symbol*> dump_symbols;
  284. for (const auto& sym : symbols) {
  285. if (sym.HasDumpId()) {
  286. dump_symbols.push_back(&sym);
  287. }
  288. }
  289. std::sort(dump_symbols.begin(), dump_symbols.end(), Symbol::CompareByAddr);
  290. const std::vector<uint64_t>* dex_file_offsets = dso->DexFileOffsets();
  291. if (!WriteFileFeature(dso->Path(), dso_type, min_vaddr, file_offset_of_min_vaddr,
  292. dump_symbols, dex_file_offsets)) {
  293. return false;
  294. }
  295. }
  296. return true;
  297. }
  298. bool RecordFileWriter::WriteFileFeature(const std::string& file_path,
  299. uint32_t file_type,
  300. uint64_t min_vaddr,
  301. uint64_t file_offset_of_min_vaddr,
  302. const std::vector<const Symbol*>& symbols,
  303. const std::vector<uint64_t>* dex_file_offsets) {
  304. uint32_t size = file_path.size() + 1 + sizeof(uint32_t) * 2 +
  305. sizeof(uint64_t) + symbols.size() * (sizeof(uint64_t) + sizeof(uint32_t));
  306. for (const auto& symbol : symbols) {
  307. size += strlen(symbol->Name()) + 1;
  308. }
  309. if (dex_file_offsets != nullptr) {
  310. size += sizeof(uint32_t) + sizeof(uint64_t) * dex_file_offsets->size();
  311. }
  312. if (file_type == DSO_ELF_FILE) {
  313. size += sizeof(uint64_t);
  314. }
  315. std::vector<char> buf(sizeof(uint32_t) + size);
  316. char* p = buf.data();
  317. MoveToBinaryFormat(size, p);
  318. MoveToBinaryFormat(file_path.c_str(), file_path.size() + 1, p);
  319. MoveToBinaryFormat(file_type, p);
  320. MoveToBinaryFormat(min_vaddr, p);
  321. uint32_t symbol_count = static_cast<uint32_t>(symbols.size());
  322. MoveToBinaryFormat(symbol_count, p);
  323. for (const auto& symbol : symbols) {
  324. MoveToBinaryFormat(symbol->addr, p);
  325. uint32_t len = symbol->len;
  326. MoveToBinaryFormat(len, p);
  327. MoveToBinaryFormat(symbol->Name(), strlen(symbol->Name()) + 1, p);
  328. }
  329. if (dex_file_offsets != nullptr) {
  330. uint32_t offset_count = dex_file_offsets->size();
  331. MoveToBinaryFormat(offset_count, p);
  332. MoveToBinaryFormat(dex_file_offsets->data(), offset_count, p);
  333. }
  334. if (file_type == DSO_ELF_FILE) {
  335. MoveToBinaryFormat(file_offset_of_min_vaddr, p);
  336. }
  337. CHECK_EQ(buf.size(), static_cast<size_t>(p - buf.data()));
  338. return WriteFeature(FEAT_FILE, buf);
  339. }
  340. bool RecordFileWriter::WriteMetaInfoFeature(
  341. const std::unordered_map<std::string, std::string>& info_map) {
  342. uint32_t size = 0u;
  343. for (auto& pair : info_map) {
  344. size += pair.first.size() + 1;
  345. size += pair.second.size() + 1;
  346. }
  347. std::vector<char> buf(size);
  348. char* p = buf.data();
  349. for (auto& pair : info_map) {
  350. MoveToBinaryFormat(pair.first.c_str(), pair.first.size() + 1, p);
  351. MoveToBinaryFormat(pair.second.c_str(), pair.second.size() + 1, p);
  352. }
  353. return WriteFeature(FEAT_META_INFO, buf);
  354. }
  355. bool RecordFileWriter::WriteFeature(int feature, const std::vector<char>& data) {
  356. return WriteFeatureBegin(feature) && Write(data.data(), data.size()) && WriteFeatureEnd(feature);
  357. }
  358. bool RecordFileWriter::WriteFeatureBegin(int feature) {
  359. auto it = features_.find(feature);
  360. if (it == features_.end()) {
  361. CHECK_LT(features_.size(), feature_count_);
  362. auto& sec = features_[feature];
  363. if (!GetFilePos(&sec.offset)) {
  364. return false;
  365. }
  366. sec.size = 0;
  367. }
  368. return true;
  369. }
  370. bool RecordFileWriter::WriteFeatureEnd(int feature) {
  371. auto it = features_.find(feature);
  372. if (it == features_.end()) {
  373. return false;
  374. }
  375. uint64_t offset;
  376. if (!GetFilePos(&offset)) {
  377. return false;
  378. }
  379. it->second.size = offset - it->second.offset;
  380. return true;
  381. }
  382. bool RecordFileWriter::EndWriteFeatures() {
  383. // Used features (features_.size()) should be <= allocated feature space.
  384. CHECK_LE(features_.size(), feature_count_);
  385. if (fseek(record_fp_, feature_section_offset_, SEEK_SET) == -1) {
  386. PLOG(ERROR) << "fseek() failed";
  387. return false;
  388. }
  389. for (const auto& pair : features_) {
  390. if (!Write(&pair.second, sizeof(SectionDesc))) {
  391. return false;
  392. }
  393. }
  394. return true;
  395. }
  396. bool RecordFileWriter::WriteFileHeader() {
  397. FileHeader header;
  398. memset(&header, 0, sizeof(header));
  399. memcpy(header.magic, PERF_MAGIC, sizeof(header.magic));
  400. header.header_size = sizeof(header);
  401. header.attr_size = sizeof(FileAttr);
  402. header.attrs.offset = attr_section_offset_;
  403. header.attrs.size = attr_section_size_;
  404. header.data.offset = data_section_offset_;
  405. header.data.size = data_section_size_;
  406. for (const auto& pair : features_) {
  407. int i = pair.first / 8;
  408. int j = pair.first % 8;
  409. header.features[i] |= (1 << j);
  410. }
  411. if (fseek(record_fp_, 0, SEEK_SET) == -1) {
  412. return false;
  413. }
  414. if (!Write(&header, sizeof(header))) {
  415. return false;
  416. }
  417. return true;
  418. }
  419. bool RecordFileWriter::Close() {
  420. CHECK(record_fp_ != nullptr);
  421. bool result = true;
  422. // Write file header. We gather enough information to write file header only after
  423. // writing data section and feature section.
  424. if (!WriteFileHeader()) {
  425. result = false;
  426. }
  427. if (fclose(record_fp_) != 0) {
  428. PLOG(ERROR) << "failed to close record file '" << filename_ << "'";
  429. result = false;
  430. }
  431. record_fp_ = nullptr;
  432. return result;
  433. }