2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <base/logging.h>
24 #include <base/strings.h>
27 #include "environment.h"
28 #include "event_selection_set.h"
29 #include "event_type.h"
32 #include "record_file.h"
36 static std::string default_measured_event_type = "cpu-cycles";
38 class RecordCommandImpl {
41 : use_sample_freq_(true),
43 system_wide_collection_(false),
44 measured_event_type_(nullptr),
45 perf_mmap_pages_(256),
46 record_filename_("perf.data") {
47 // We need signal SIGCHLD to break poll().
48 saved_sigchild_handler_ = signal(SIGCHLD, [](int) {});
51 ~RecordCommandImpl() {
52 signal(SIGCHLD, saved_sigchild_handler_);
55 bool Run(const std::vector<std::string>& args);
57 static bool ReadMmapDataCallback(const char* data, size_t size);
60 bool ParseOptions(const std::vector<std::string>& args, std::vector<std::string>* non_option_args);
61 bool SetMeasuredEventType(const std::string& event_type_name);
62 void SetEventSelection();
63 bool WriteData(const char* data, size_t size);
64 bool DumpKernelAndModuleMmaps();
65 bool DumpThreadCommAndMmaps();
66 bool DumpAdditionalFeatures();
67 bool DumpBuildIdFeature();
69 bool use_sample_freq_; // Use sample_freq_ when true, otherwise using sample_period_.
70 uint64_t sample_freq_; // Sample 'sample_freq_' times per second.
71 uint64_t sample_period_; // Sample once when 'sample_period_' events occur.
73 bool system_wide_collection_;
74 const EventType* measured_event_type_;
75 EventSelectionSet event_selection_set_;
77 // mmap pages used by each perf event file, should be power of 2.
78 const size_t perf_mmap_pages_;
80 std::string record_filename_;
81 std::unique_ptr<RecordFileWriter> record_file_writer_;
83 sighandler_t saved_sigchild_handler_;
86 bool RecordCommandImpl::Run(const std::vector<std::string>& args) {
87 // 1. Parse options, and use default measured event type if not given.
88 std::vector<std::string> workload_args;
89 if (!ParseOptions(args, &workload_args)) {
92 if (measured_event_type_ == nullptr) {
93 if (!SetMeasuredEventType(default_measured_event_type)) {
99 // 2. Create workload.
100 if (workload_args.empty()) {
101 // TODO: change default workload to sleep 99999, and run record until Ctrl-C.
102 workload_args = std::vector<std::string>({"sleep", "1"});
104 std::unique_ptr<Workload> workload = Workload::CreateWorkload(workload_args);
105 if (workload == nullptr) {
109 // 3. Open perf_event_files, create memory mapped buffers for perf_event_files, add prepare poll
110 // for perf_event_files.
111 if (system_wide_collection_) {
112 if (!event_selection_set_.OpenEventFilesForAllCpus()) {
116 event_selection_set_.EnableOnExec();
117 if (!event_selection_set_.OpenEventFilesForProcess(workload->GetPid())) {
121 if (!event_selection_set_.MmapEventFiles(perf_mmap_pages_)) {
124 std::vector<pollfd> pollfds;
125 event_selection_set_.PreparePollForEventFiles(&pollfds);
127 // 4. Open record file writer, and dump kernel/modules/threads mmap information.
128 record_file_writer_ = RecordFileWriter::CreateInstance(
129 record_filename_, event_selection_set_.FindEventAttrByType(*measured_event_type_),
130 event_selection_set_.FindEventFdsByType(*measured_event_type_));
131 if (record_file_writer_ == nullptr) {
134 if (!DumpKernelAndModuleMmaps()) {
137 if (system_wide_collection_ && !DumpThreadCommAndMmaps()) {
141 // 5. Write records in mmap buffers of perf_event_files to output file while workload is running.
143 // If monitoring only one process, we use the enable_on_exec flag, and don't need to start
144 // recording manually.
145 if (system_wide_collection_) {
146 if (!event_selection_set_.EnableEvents()) {
150 if (!workload->Start()) {
154 std::bind(&RecordCommandImpl::WriteData, this, std::placeholders::_1, std::placeholders::_2);
156 if (!event_selection_set_.ReadMmapEventData(callback)) {
159 if (workload->IsFinished()) {
162 poll(&pollfds[0], pollfds.size(), -1);
165 // 6. Dump additional features, and close record file.
166 if (!DumpAdditionalFeatures()) {
169 if (!record_file_writer_->Close()) {
175 bool RecordCommandImpl::ParseOptions(const std::vector<std::string>& args,
176 std::vector<std::string>* non_option_args) {
178 for (i = 1; i < args.size() && args[i].size() > 0 && args[i][0] == '-'; ++i) {
179 if (args[i] == "-a") {
180 system_wide_collection_ = true;
181 } else if (args[i] == "-c") {
182 if (!NextArgumentOrError(args, &i)) {
186 sample_period_ = strtoull(args[i].c_str(), &endptr, 0);
187 if (*endptr != '\0' || sample_period_ == 0) {
188 LOG(ERROR) << "Invalid sample period: '" << args[i] << "'";
191 use_sample_freq_ = false;
192 } else if (args[i] == "-e") {
193 if (!NextArgumentOrError(args, &i)) {
196 if (!SetMeasuredEventType(args[i])) {
199 } else if (args[i] == "-f" || args[i] == "-F") {
200 if (!NextArgumentOrError(args, &i)) {
204 sample_freq_ = strtoull(args[i].c_str(), &endptr, 0);
205 if (*endptr != '\0' || sample_freq_ == 0) {
206 LOG(ERROR) << "Invalid sample frequency: '" << args[i] << "'";
209 use_sample_freq_ = true;
210 } else if (args[i] == "-o") {
211 if (!NextArgumentOrError(args, &i)) {
214 record_filename_ = args[i];
216 LOG(ERROR) << "Unknown option for record command: '" << args[i] << "'\n";
217 LOG(ERROR) << "Try `simpleperf help record`";
222 if (non_option_args != nullptr) {
223 non_option_args->clear();
224 for (; i < args.size(); ++i) {
225 non_option_args->push_back(args[i]);
231 bool RecordCommandImpl::SetMeasuredEventType(const std::string& event_type_name) {
232 const EventType* event_type = EventTypeFactory::FindEventTypeByName(event_type_name);
233 if (event_type == nullptr) {
236 measured_event_type_ = event_type;
240 void RecordCommandImpl::SetEventSelection() {
241 event_selection_set_.AddEventType(*measured_event_type_);
242 if (use_sample_freq_) {
243 event_selection_set_.SetSampleFreq(sample_freq_);
245 event_selection_set_.SetSamplePeriod(sample_period_);
247 event_selection_set_.SampleIdAll();
250 bool RecordCommandImpl::WriteData(const char* data, size_t size) {
251 return record_file_writer_->WriteData(data, size);
254 bool RecordCommandImpl::DumpKernelAndModuleMmaps() {
255 KernelMmap kernel_mmap;
256 std::vector<ModuleMmap> module_mmaps;
257 if (!GetKernelAndModuleMmaps(&kernel_mmap, &module_mmaps)) {
260 const perf_event_attr& attr = event_selection_set_.FindEventAttrByType(*measured_event_type_);
261 MmapRecord mmap_record = CreateMmapRecord(attr, true, UINT_MAX, 0, kernel_mmap.start_addr,
262 kernel_mmap.len, kernel_mmap.pgoff, kernel_mmap.name);
263 if (!record_file_writer_->WriteData(mmap_record.BinaryFormat())) {
266 for (auto& module_mmap : module_mmaps) {
267 std::string filename = module_mmap.filepath;
268 if (filename.empty()) {
269 filename = "[" + module_mmap.name + "]";
271 MmapRecord mmap_record = CreateMmapRecord(attr, true, UINT_MAX, 0, module_mmap.start_addr,
272 module_mmap.len, 0, filename);
273 if (!record_file_writer_->WriteData(mmap_record.BinaryFormat())) {
280 bool RecordCommandImpl::DumpThreadCommAndMmaps() {
281 std::vector<ThreadComm> thread_comms;
282 if (!GetThreadComms(&thread_comms)) {
285 const perf_event_attr& attr = event_selection_set_.FindEventAttrByType(*measured_event_type_);
286 for (auto& thread : thread_comms) {
287 CommRecord record = CreateCommRecord(attr, thread.tgid, thread.tid, thread.comm);
288 if (!record_file_writer_->WriteData(record.BinaryFormat())) {
291 if (thread.is_process) {
292 std::vector<ThreadMmap> thread_mmaps;
293 if (!GetThreadMmapsInProcess(thread.tgid, &thread_mmaps)) {
294 // The thread may exit before we get its info.
297 for (auto& thread_mmap : thread_mmaps) {
298 if (thread_mmap.executable == 0) {
299 continue; // No need to dump non-executable mmap info.
302 CreateMmapRecord(attr, false, thread.tgid, thread.tid, thread_mmap.start_addr,
303 thread_mmap.len, thread_mmap.pgoff, thread_mmap.name);
304 if (!record_file_writer_->WriteData(record.BinaryFormat())) {
313 bool RecordCommandImpl::DumpAdditionalFeatures() {
314 if (!record_file_writer_->WriteFeatureHeader(1)) {
317 return DumpBuildIdFeature();
320 bool RecordCommandImpl::DumpBuildIdFeature() {
321 std::vector<std::string> hit_kernel_modules;
322 std::vector<std::string> hit_user_files;
323 if (!record_file_writer_->GetHitModules(&hit_kernel_modules, &hit_user_files)) {
326 std::vector<BuildIdRecord> build_id_records;
328 // Add build_ids for kernel/modules.
329 for (auto& filename : hit_kernel_modules) {
330 if (filename == DEFAULT_KERNEL_MMAP_NAME) {
331 if (!GetKernelBuildId(&build_id)) {
332 LOG(DEBUG) << "can't read build_id for kernel";
335 build_id_records.push_back(
336 CreateBuildIdRecord(true, UINT_MAX, build_id, DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID));
338 std::string module_name = basename(&filename[0]);
339 if (android::base::EndsWith(module_name, ".ko")) {
340 module_name = module_name.substr(0, module_name.size() - 3);
342 if (!GetModuleBuildId(module_name, &build_id)) {
343 LOG(DEBUG) << "can't read build_id for module " << module_name;
346 build_id_records.push_back(CreateBuildIdRecord(true, UINT_MAX, build_id, filename));
349 // Add build_ids for user elf files.
350 for (auto& filename : hit_user_files) {
351 if (filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) {
354 if (!GetBuildIdFromElfFile(filename, &build_id)) {
355 LOG(DEBUG) << "can't read build_id from file " << filename;
358 build_id_records.push_back(CreateBuildIdRecord(false, UINT_MAX, build_id, filename));
360 if (!record_file_writer_->WriteBuildIdFeature(build_id_records)) {
366 class RecordCommand : public Command {
369 : Command("record", "record sampling info in perf.data",
370 "Usage: simpleperf record [options] [command [command-args]]\n"
371 " Gather sampling information when running [command]. If [command]\n"
372 " is not specified, sleep 1 is used instead.\n"
373 " -a System-wide collection.\n"
374 " -c count Set event sample period.\n"
375 " -e event Select the event to sample (Use `simpleperf list`)\n"
376 " to find all possible event names.\n"
377 " -f freq Set event sample frequency.\n"
378 " -F freq Same as '-f freq'.\n"
379 " -o record_file_name Set record file name, default is perf.data.\n") {
382 bool Run(const std::vector<std::string>& args) override {
383 RecordCommandImpl impl;
384 return impl.Run(args);
388 RecordCommand record_command;