{"ind_call", PERF_SAMPLE_BRANCH_IND_CALL},
};
+static std::unordered_map<std::string, int> clockid_map = {
+ {"realtime", CLOCK_REALTIME},
+ {"monotonic", CLOCK_MONOTONIC},
+ {"monotonic_raw", CLOCK_MONOTONIC_RAW},
+ {"boottime", CLOCK_BOOTTIME},
+};
+
// The max size of records dumped by kernel is 65535, and dump stack size
// should be a multiply of 8, so MAX_DUMP_STACK_SIZE is 65528.
constexpr uint32_t MAX_DUMP_STACK_SIZE = 65528;
" frame as the method to parse call graph in stack.\n"
" Default is dwarf,65528.\n"
"-g Same as '--call-graph dwarf'.\n"
+"--clockid clock_id Generate timestamps of samples using selected clock.\n"
+" Possible values are: realtime, monotonic,\n"
+" monotonic_raw, boottime, perf. Default is perf.\n"
"--cpu cpu_item1,cpu_item2,...\n"
" Collect samples only on the selected cpus. cpu_item can be cpu\n"
" number like 1, or cpu range like 0-3.\n"
duration_in_sec_(0),
can_dump_kernel_symbols_(true),
dump_symbols_(true),
+ clockid_("perf"),
event_selection_set_(false),
mmap_page_range_(std::make_pair(1, DESIRED_PAGES_IN_MAPPED_BUFFER)),
record_filename_("perf.data"),
double duration_in_sec_;
bool can_dump_kernel_symbols_;
bool dump_symbols_;
+ std::string clockid_;
std::vector<int> cpus_;
EventSelectionSet event_selection_set_;
<< args[i];
return false;
}
+ } else if (args[i] == "--clockid") {
+ if (!NextArgumentOrError(args, &i)) {
+ return false;
+ }
+ if (args[i] != "perf") {
+ if (!IsSettingClockIdSupported()) {
+ LOG(ERROR) << "Setting clockid is not supported by the kernel.";
+ return false;
+ }
+ if (clockid_map.find(args[i]) == clockid_map.end()) {
+ LOG(ERROR) << "Invalid clockid: " << args[i];
+ return false;
+ }
+ }
+ clockid_ = args[i];
} else if (args[i] == "--cpu") {
if (!NextArgumentOrError(args, &i)) {
return false;
}
}
event_selection_set_.SetInherit(child_inherit_);
+ if (clockid_ != "perf") {
+ event_selection_set_.SetClockId(clockid_map[clockid_]);
+ }
return true;
}
android::base::GetProperty("ro.product.model", "").c_str(),
android::base::GetProperty("ro.product.name", "").c_str());
#endif
+ info_map["clockid"] = clockid_;
return record_file_writer_->WriteMetaInfoFeature(info_map);
}
TEST(record_cmd, exit_with_parent_option) {
ASSERT_TRUE(RunRecordCmd({"--exit-with-parent"}));
}
+
+TEST(record_cmd, clockid_option) {
+ if (!IsSettingClockIdSupported()) {
+ ASSERT_FALSE(RunRecordCmd({"--clockid", "monotonic"}));
+ } else {
+ TemporaryFile tmpfile;
+ ASSERT_TRUE(RunRecordCmd({"--clockid", "monotonic"}, tmpfile.path));
+ std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
+ ASSERT_TRUE(reader);
+ std::unordered_map<std::string, std::string> info_map;
+ ASSERT_TRUE(reader->ReadMetaInfoFeature(&info_map));
+ ASSERT_EQ(info_map["clockid"], "monotonic");
+ }
+}
return false;
}
+bool IsSettingClockIdSupported() {
+ const EventType* type = FindEventTypeByName("cpu-cycles");
+ if (type == nullptr) {
+ return false;
+ }
+ // Check if the kernel supports setting clockid, which was added in kernel 4.0. Just check with
+ // one clockid is enough. Because all needed clockids were supported before kernel 4.0.
+ perf_event_attr attr = CreateDefaultPerfEventAttr(*type);
+ attr.use_clockid = 1;
+ attr.clockid = CLOCK_MONOTONIC;
+ return IsEventAttrSupported(attr);
+}
+
bool EventSelectionSet::BuildAndCheckEventSelection(
const std::string& event_name, EventSelection* selection) {
std::unique_ptr<EventTypeAndModifier> event_type = ParseEventType(event_name);
}
}
+void EventSelectionSet::SetClockId(int clock_id) {
+ for (auto& group : groups_) {
+ for (auto& selection : group) {
+ selection.event_attr.use_clockid = 1;
+ selection.event_attr.clockid = clock_id;
+ }
+ }
+}
+
bool EventSelectionSet::NeedKernelSymbol() const {
for (const auto& group : groups_) {
for (const auto& selection : group) {
void EnableFpCallChainSampling();
bool EnableDwarfCallChainSampling(uint32_t dump_stack_size);
void SetInherit(bool enable);
+ void SetClockId(int clock_id);
bool NeedKernelSymbol() const;
void AddMonitoredProcesses(const std::set<pid_t>& processes) {
bool IsBranchSamplingSupported();
bool IsDwarfCallChainSamplingSupported();
bool IsDumpingRegsForTracepointEventsSupported();
+bool IsSettingClockIdSupported();
#endif // SIMPLE_PERF_EVENT_SELECTION_SET_H_