From a86c7e3afc42226ec913ef5b50a5e04ec4d0ad04 Mon Sep 17 00:00:00 2001 From: Yi Kong Date: Wed, 3 Jun 2020 23:24:36 +0800 Subject: simpleperf: expose ETM collection methods for profcollectd Test: build Bug: 79161490 Change-Id: I947cb0c08a9be2dacbe00f4985c35552855c6fdc --- simpleperf/Android.bp | 13 +++++++ simpleperf/include/simpleperf_profcollect.h | 33 ++++++++++++++++ simpleperf/profcollect.cpp | 58 +++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 simpleperf/include/simpleperf_profcollect.h create mode 100644 simpleperf/profcollect.cpp diff --git a/simpleperf/Android.bp b/simpleperf/Android.bp index c93137da..3285a804 100644 --- a/simpleperf/Android.bp +++ b/simpleperf/Android.bp @@ -333,6 +333,19 @@ cc_binary { }, } +cc_library { + name: "libsimpleperf_profcollect", + defaults: [ + "simpleperf_shared_libs", + ], + host_supported: false, + srcs: ["profcollect.cpp"], + export_include_dirs: ["include"], + static_libs: ["libsimpleperf",], + shared_libs: ["libLLVM_android",], + visibility: ["//system/extras/profcollectd:__subpackages__"], +} + // simpleperf released in ndk cc_binary { name: "simpleperf_ndk", diff --git a/simpleperf/include/simpleperf_profcollect.h b/simpleperf/include/simpleperf_profcollect.h new file mode 100644 index 00000000..193aa641 --- /dev/null +++ b/simpleperf/include/simpleperf_profcollect.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace simpleperf { +namespace etm { + +bool HasSupport(); +bool Record(const std::filesystem::path& output, + const std::chrono::seconds& duration); +bool Inject(const std::filesystem::path& traceInput, + const std::filesystem::path& output, + const std::string& binaryFilter); + +} // namespace etm +} // namespace simpleperf diff --git a/simpleperf/profcollect.cpp b/simpleperf/profcollect.cpp new file mode 100644 index 00000000..8d286381 --- /dev/null +++ b/simpleperf/profcollect.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "command.h" +#include "event_attr.h" +#include "event_fd.h" +#include "event_type.h" + +namespace simpleperf { +namespace etm { + +bool HasSupport() { + const EventType* type = FindEventTypeByName("cs-etm"); + return IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), type->name); +} + +bool Record(const std::filesystem::path& output, + const std::chrono::seconds& duration) { + auto recordCmd = CreateCommandInstance("record"); + std::vector args; + args.push_back("-a"); + args.insert(args.end(), {"-e", "cs-etm:u"}); + args.insert(args.end(), {"--duration", std::to_string(duration.count())}); + args.insert(args.end(), {"-o", output}); + return recordCmd->Run(args); +} + +bool Inject(const std::filesystem::path& traceInput, + const std::filesystem::path& output, + const std::string& binaryFilter) { + auto injectCmd = CreateCommandInstance("inject"); + std::vector args; + args.insert(args.end(), {"-i", traceInput}); + args.insert(args.end(), {"-o", output}); + args.insert(args.end(), {"--output", "branch-list"}); + if (!binaryFilter.empty()) { + args.insert(args.end(), {"--binary", binaryFilter}); + } + return injectCmd->Run(args); +} + +} // namespace etm +} // namespace simpleperf -- cgit v1.2.3