diff options
author | Yi Kong <yikong@google.com> | 2020-10-28 21:48:08 +0800 |
---|---|---|
committer | Yi Kong <yikong@google.com> | 2020-11-02 23:38:23 +0800 |
commit | b93b338c9a6f37eafe17a34d0ec381fd51e6883d (patch) | |
tree | 8228036bdf92c964bb4e4db848e76044687f0fe4 /profcollectd | |
parent | 896defdd2e1edefa3fd06916fef872ff2c581ad0 (diff) | |
download | extras-b93b338c9a6f37eafe17a34d0ec381fd51e6883d.tar.gz |
profcollectd: Add enabled flag
Disable profcollectd by default, unless turned on through device config.
This is controlled in the binder service, instead of scheduler, to
reduce the amount of code being used when the feature is off.
Test: flash device
Bug: 79161490
Change-Id: I612c81db6a33cbecc4205854b229235efb665e2b
Diffstat (limited to 'profcollectd')
-rw-r--r-- | profcollectd/libprofcollectd/binder_service.cpp | 39 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/binder_service.h | 3 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/config_utils.cpp | 23 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/config_utils.h | 1 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/scheduler.cpp | 2 |
5 files changed, 53 insertions, 15 deletions
diff --git a/profcollectd/libprofcollectd/binder_service.cpp b/profcollectd/libprofcollectd/binder_service.cpp index 0e9534b3..1fbead9b 100644 --- a/profcollectd/libprofcollectd/binder_service.cpp +++ b/profcollectd/libprofcollectd/binder_service.cpp @@ -20,6 +20,7 @@ #include <android-base/logging.h> +#include "config_utils.h" #include "hwtrace_provider.h" #include "scheduler.h" @@ -31,7 +32,17 @@ using ::com::android::server::profcollect::IProfCollectd; namespace { -Status HandleIfError(const std::function<OptError()>& action) { +static constexpr const char* NOT_ENABLED_ERRMSG = + "profcollectd is not enabled through device config."; +static constexpr config_t CONFIG_ENABLED = {"enabled", "0"}; // Disabled by default. + +} // namespace + +Status ProfcollectdBinder::ForwardScheduler(const std::function<OptError()>& action) { + if (Scheduler == nullptr) { + return Status::fromExceptionCode(1, NOT_ENABLED_ERRMSG); + } + auto errmsg = action(); if (errmsg) { LOG(ERROR) << errmsg.value(); @@ -40,39 +51,43 @@ Status HandleIfError(const std::function<OptError()>& action) { return Status::ok(); } -} // namespace - ProfcollectdBinder::ProfcollectdBinder() { - ProfcollectdBinder::Scheduler = std::make_unique<ProfcollectdScheduler>(); - LOG(INFO) << "Binder service started"; + static bool enabled = getConfigFlagBool(CONFIG_ENABLED); + + if (enabled) { + ProfcollectdBinder::Scheduler = std::make_unique<ProfcollectdScheduler>(); + LOG(INFO) << "Binder service started"; + } else { + LOG(INFO) << NOT_ENABLED_ERRMSG; + } } Status ProfcollectdBinder::ReadConfig() { - return HandleIfError([=]() { return Scheduler->ReadConfig(); }); + return ForwardScheduler([=]() { return Scheduler->ReadConfig(); }); } Status ProfcollectdBinder::ScheduleCollection() { - return HandleIfError([=]() { return Scheduler->ScheduleCollection(); }); + return ForwardScheduler([=]() { return Scheduler->ScheduleCollection(); }); } Status ProfcollectdBinder::TerminateCollection() { - return HandleIfError([=]() { return Scheduler->TerminateCollection(); }); + return ForwardScheduler([=]() { return Scheduler->TerminateCollection(); }); } Status ProfcollectdBinder::TraceOnce(const std::string& tag) { - return HandleIfError([=]() { return Scheduler->TraceOnce(tag); }); + return ForwardScheduler([=]() { return Scheduler->TraceOnce(tag); }); } Status ProfcollectdBinder::ProcessProfile() { - return HandleIfError([=]() { return Scheduler->ProcessProfile(); }); + return ForwardScheduler([=]() { return Scheduler->ProcessProfile(); }); } Status ProfcollectdBinder::CreateProfileReport() { - return HandleIfError([=]() { return Scheduler->CreateProfileReport(); }); + return ForwardScheduler([=]() { return Scheduler->CreateProfileReport(); }); } Status ProfcollectdBinder::GetSupportedProvider(std::string* provider) { - return HandleIfError([=]() { return Scheduler->GetSupportedProvider(*provider); }); + return ForwardScheduler([=]() { return Scheduler->GetSupportedProvider(*provider); }); } } // namespace profcollectd diff --git a/profcollectd/libprofcollectd/binder_service.h b/profcollectd/libprofcollectd/binder_service.h index e269b56f..28c69958 100644 --- a/profcollectd/libprofcollectd/binder_service.h +++ b/profcollectd/libprofcollectd/binder_service.h @@ -42,6 +42,9 @@ class ProfcollectdBinder : public BinderService<ProfcollectdBinder>, protected: inline static std::unique_ptr<ProfcollectdScheduler> Scheduler; + + private: + binder::Status ForwardScheduler(const std::function<OptError()>& action); }; } // namespace profcollectd diff --git a/profcollectd/libprofcollectd/config_utils.cpp b/profcollectd/libprofcollectd/config_utils.cpp index 40a03427..ce92017d 100644 --- a/profcollectd/libprofcollectd/config_utils.cpp +++ b/profcollectd/libprofcollectd/config_utils.cpp @@ -16,6 +16,8 @@ #include "config_utils.h" +#include <android-base/parsedouble.h> +#include <android-base/parseint.h> #include <android-base/properties.h> #include <server_configurable_flags/get_flags.h> @@ -25,6 +27,8 @@ namespace android { namespace profcollectd { using ::android::base::GetProperty; +using ::android::base::ParseFloat; +using ::android::base::ParseInt; using ::server_configurable_flags::GetServerConfigurableFlag; std::string getBuildFingerprint() { @@ -37,12 +41,27 @@ std::string getConfigFlag(const config_t& config) { int getConfigFlagInt(const config_t& config) { std::string value = getConfigFlag(config); - return std::stoi(value); + int i; + if (!ParseInt(value, &i)) { + // Use default value if the server config value is malformed. + return ParseInt(config.defaultValue, &i); + } + return i; } float getConfigFlagFloat(const config_t& config) { std::string value = getConfigFlag(config); - return std::stof(value); + float f; + if (!ParseFloat(value, &f)) { + // Use default value if the server config value is malformed. + return ParseFloat(config.defaultValue, &f); + } + return f; +} + +bool getConfigFlagBool(const config_t& config) { + std::string value = getConfigFlag(config); + return value == "true"; } } // namespace profcollectd diff --git a/profcollectd/libprofcollectd/config_utils.h b/profcollectd/libprofcollectd/config_utils.h index 8ad5d0d1..b3a458a0 100644 --- a/profcollectd/libprofcollectd/config_utils.h +++ b/profcollectd/libprofcollectd/config_utils.h @@ -33,6 +33,7 @@ std::string getBuildFingerprint(); std::string getConfigFlag(const config_t& config); int getConfigFlagInt(const config_t& config); float getConfigFlagFloat(const config_t& config); +bool getConfigFlagBool(const config_t& config); } // namespace profcollectd } // namespace android diff --git a/profcollectd/libprofcollectd/scheduler.cpp b/profcollectd/libprofcollectd/scheduler.cpp index ce316eed..7e0ae608 100644 --- a/profcollectd/libprofcollectd/scheduler.cpp +++ b/profcollectd/libprofcollectd/scheduler.cpp @@ -97,7 +97,7 @@ ProfcollectdScheduler::ProfcollectdScheduler() { if ((hwtracer = REGISTER_SIMPLEPERF_ETM_PROVIDER())) { LOG(INFO) << "ETM provider registered."; } else { - LOG(ERROR) << "No hardware trace provider found for this architecture."; + LOG(ERROR) << "No hardware trace provider available."; } } |