Home | History | Annotate | Download | only in simpleperf
      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 
     17 #include <libgen.h>
     18 #include <poll.h>
     19 #include <signal.h>
     20 #include <string>
     21 #include <vector>
     22 
     23 #include <base/logging.h>
     24 #include <base/strings.h>
     25 
     26 #include "command.h"
     27 #include "environment.h"
     28 #include "event_selection_set.h"
     29 #include "event_type.h"
     30 #include "read_elf.h"
     31 #include "record.h"
     32 #include "record_file.h"
     33 #include "utils.h"
     34 #include "workload.h"
     35 
     36 static std::string default_measured_event_type = "cpu-cycles";
     37 
     38 class RecordCommandImpl {
     39  public:
     40   RecordCommandImpl()
     41       : use_sample_freq_(true),
     42         sample_freq_(1000),
     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) {});
     49   }
     50 
     51   ~RecordCommandImpl() {
     52     signal(SIGCHLD, saved_sigchild_handler_);
     53   }
     54 
     55   bool Run(const std::vector<std::string>& args);
     56 
     57   static bool ReadMmapDataCallback(const char* data, size_t size);
     58 
     59  private:
     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();
     68 
     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.
     72 
     73   bool system_wide_collection_;
     74   const EventType* measured_event_type_;
     75   EventSelectionSet event_selection_set_;
     76 
     77   // mmap pages used by each perf event file, should be power of 2.
     78   const size_t perf_mmap_pages_;
     79 
     80   std::string record_filename_;
     81   std::unique_ptr<RecordFileWriter> record_file_writer_;
     82 
     83   sighandler_t saved_sigchild_handler_;
     84 };
     85 
     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)) {
     90     return false;
     91   }
     92   if (measured_event_type_ == nullptr) {
     93     if (!SetMeasuredEventType(default_measured_event_type)) {
     94       return false;
     95     }
     96   }
     97   SetEventSelection();
     98 
     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"});
    103   }
    104   std::unique_ptr<Workload> workload = Workload::CreateWorkload(workload_args);
    105   if (workload == nullptr) {
    106     return false;
    107   }
    108 
    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()) {
    113       return false;
    114     }
    115   } else {
    116     event_selection_set_.EnableOnExec();
    117     if (!event_selection_set_.OpenEventFilesForProcess(workload->GetPid())) {
    118       return false;
    119     }
    120   }
    121   if (!event_selection_set_.MmapEventFiles(perf_mmap_pages_)) {
    122     return false;
    123   }
    124   std::vector<pollfd> pollfds;
    125   event_selection_set_.PreparePollForEventFiles(&pollfds);
    126 
    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) {
    132     return false;
    133   }
    134   if (!DumpKernelAndModuleMmaps()) {
    135     return false;
    136   }
    137   if (system_wide_collection_ && !DumpThreadCommAndMmaps()) {
    138     return false;
    139   }
    140 
    141   // 5. Write records in mmap buffers of perf_event_files to output file while workload is running.
    142 
    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()) {
    147       return false;
    148     }
    149   }
    150   if (!workload->Start()) {
    151     return false;
    152   }
    153   auto callback =
    154       std::bind(&RecordCommandImpl::WriteData, this, std::placeholders::_1, std::placeholders::_2);
    155   while (true) {
    156     if (!event_selection_set_.ReadMmapEventData(callback)) {
    157       return false;
    158     }
    159     if (workload->IsFinished()) {
    160       break;
    161     }
    162     poll(&pollfds[0], pollfds.size(), -1);
    163   }
    164 
    165   // 6. Dump additional features, and close record file.
    166   if (!DumpAdditionalFeatures()) {
    167     return false;
    168   }
    169   if (!record_file_writer_->Close()) {
    170     return false;
    171   }
    172   return true;
    173 }
    174 
    175 bool RecordCommandImpl::ParseOptions(const std::vector<std::string>& args,
    176                                      std::vector<std::string>* non_option_args) {
    177   size_t i;
    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)) {
    183         return false;
    184       }
    185       char* endptr;
    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] << "'";
    189         return false;
    190       }
    191       use_sample_freq_ = false;
    192     } else if (args[i] == "-e") {
    193       if (!NextArgumentOrError(args, &i)) {
    194         return false;
    195       }
    196       if (!SetMeasuredEventType(args[i])) {
    197         return false;
    198       }
    199     } else if (args[i] == "-f" || args[i] == "-F") {
    200       if (!NextArgumentOrError(args, &i)) {
    201         return false;
    202       }
    203       char* endptr;
    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] << "'";
    207         return false;
    208       }
    209       use_sample_freq_ = true;
    210     } else if (args[i] == "-o") {
    211       if (!NextArgumentOrError(args, &i)) {
    212         return false;
    213       }
    214       record_filename_ = args[i];
    215     } else {
    216       LOG(ERROR) << "Unknown option for record command: '" << args[i] << "'\n";
    217       LOG(ERROR) << "Try `simpleperf help record`";
    218       return false;
    219     }
    220   }
    221 
    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]);
    226     }
    227   }
    228   return true;
    229 }
    230 
    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) {
    234     return false;
    235   }
    236   measured_event_type_ = event_type;
    237   return true;
    238 }
    239 
    240 void RecordCommandImpl::SetEventSelection() {
    241   event_selection_set_.AddEventType(*measured_event_type_);
    242   if (use_sample_freq_) {
    243     event_selection_set_.SetSampleFreq(sample_freq_);
    244   } else {
    245     event_selection_set_.SetSamplePeriod(sample_period_);
    246   }
    247   event_selection_set_.SampleIdAll();
    248 }
    249 
    250 bool RecordCommandImpl::WriteData(const char* data, size_t size) {
    251   return record_file_writer_->WriteData(data, size);
    252 }
    253 
    254 bool RecordCommandImpl::DumpKernelAndModuleMmaps() {
    255   KernelMmap kernel_mmap;
    256   std::vector<ModuleMmap> module_mmaps;
    257   if (!GetKernelAndModuleMmaps(&kernel_mmap, &module_mmaps)) {
    258     return false;
    259   }
    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())) {
    264     return false;
    265   }
    266   for (auto& module_mmap : module_mmaps) {
    267     std::string filename = module_mmap.filepath;
    268     if (filename.empty()) {
    269       filename = "[" + module_mmap.name + "]";
    270     }
    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())) {
    274       return false;
    275     }
    276   }
    277   return true;
    278 }
    279 
    280 bool RecordCommandImpl::DumpThreadCommAndMmaps() {
    281   std::vector<ThreadComm> thread_comms;
    282   if (!GetThreadComms(&thread_comms)) {
    283     return false;
    284   }
    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())) {
    289       return false;
    290     }
    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.
    295         continue;
    296       }
    297       for (auto& thread_mmap : thread_mmaps) {
    298         if (thread_mmap.executable == 0) {
    299           continue;  // No need to dump non-executable mmap info.
    300         }
    301         MmapRecord record =
    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())) {
    305           return false;
    306         }
    307       }
    308     }
    309   }
    310   return true;
    311 }
    312 
    313 bool RecordCommandImpl::DumpAdditionalFeatures() {
    314   if (!record_file_writer_->WriteFeatureHeader(1)) {
    315     return false;
    316   }
    317   return DumpBuildIdFeature();
    318 }
    319 
    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)) {
    324     return false;
    325   }
    326   std::vector<BuildIdRecord> build_id_records;
    327   BuildId build_id;
    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";
    333         continue;
    334       }
    335       build_id_records.push_back(
    336           CreateBuildIdRecord(true, UINT_MAX, build_id, DEFAULT_KERNEL_FILENAME_FOR_BUILD_ID));
    337     } else {
    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);
    341       }
    342       if (!GetModuleBuildId(module_name, &build_id)) {
    343         LOG(DEBUG) << "can't read build_id for module " << module_name;
    344         continue;
    345       }
    346       build_id_records.push_back(CreateBuildIdRecord(true, UINT_MAX, build_id, filename));
    347     }
    348   }
    349   // Add build_ids for user elf files.
    350   for (auto& filename : hit_user_files) {
    351     if (filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) {
    352       continue;
    353     }
    354     if (!GetBuildIdFromElfFile(filename, &build_id)) {
    355       LOG(DEBUG) << "can't read build_id from file " << filename;
    356       continue;
    357     }
    358     build_id_records.push_back(CreateBuildIdRecord(false, UINT_MAX, build_id, filename));
    359   }
    360   if (!record_file_writer_->WriteBuildIdFeature(build_id_records)) {
    361     return false;
    362   }
    363   return true;
    364 }
    365 
    366 class RecordCommand : public Command {
    367  public:
    368   RecordCommand()
    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") {
    380   }
    381 
    382   bool Run(const std::vector<std::string>& args) override {
    383     RecordCommandImpl impl;
    384     return impl.Run(args);
    385   }
    386 };
    387 
    388 RecordCommand record_command;
    389