1 /*
2  * Copyright (C) 2022 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 "src/traced/probes/android_system_property/android_system_property_data_source.h"
18 #include <optional>
19 
20 #include "perfetto/base/task_runner.h"
21 #include "perfetto/base/time.h"
22 #include "perfetto/ext/base/android_utils.h"
23 #include "perfetto/ext/base/string_utils.h"
24 #include "perfetto/tracing/core/data_source_config.h"
25 
26 #include "protos/perfetto/config/android/android_system_property_config.pbzero.h"
27 #include "protos/perfetto/trace/android/android_system_property.pbzero.h"
28 #include "protos/perfetto/trace/trace_packet.pbzero.h"
29 
30 namespace perfetto {
31 
32 // static
33 const AndroidSystemPropertyDataSource::Descriptor
34     AndroidSystemPropertyDataSource::descriptor = {
35         /* name */ "android.system_property",
36         /* flags */ Descriptor::kFlagsNone,
37         /*fill_descriptor_func*/ nullptr,
38 };
39 
40 constexpr const char* REQUIRED_NAME_PREFIX = "debug.tracing.";
41 
AndroidSystemPropertyDataSource(base::TaskRunner * task_runner,const DataSourceConfig & ds_config,TracingSessionID session_id,std::unique_ptr<TraceWriter> writer)42 AndroidSystemPropertyDataSource::AndroidSystemPropertyDataSource(
43     base::TaskRunner* task_runner,
44     const DataSourceConfig& ds_config,
45     TracingSessionID session_id,
46     std::unique_ptr<TraceWriter> writer)
47     : ProbesDataSource(session_id, &descriptor),
48       task_runner_(task_runner),
49       writer_(std::move(writer)),
50       weak_factory_(this) {
51   protos::pbzero::AndroidSystemPropertyConfig::Decoder cfg(
52       ds_config.android_system_property_config_raw());
53   poll_period_ms_ = cfg.poll_ms();
54   if (poll_period_ms_ > 0 && poll_period_ms_ < 100) {
55     PERFETTO_ILOG("poll_ms %" PRIu32
56                   " is less than minimum of 100ms. Increasing to 100ms.",
57                   poll_period_ms_);
58     poll_period_ms_ = 100;
59   }
60   for (auto name_chars = cfg.property_name(); name_chars; ++name_chars) {
61     auto name = (*name_chars).ToStdString();
62     if (base::StartsWith(name, REQUIRED_NAME_PREFIX)) {
63       property_names_.push_back(name);
64     } else {
65       PERFETTO_ELOG("Property %s lacks required prefix %s", name.c_str(),
66                     REQUIRED_NAME_PREFIX);
67     }
68   }
69 }
70 
Start()71 void AndroidSystemPropertyDataSource::Start() {
72   Tick();
73 }
74 
75 base::WeakPtr<AndroidSystemPropertyDataSource>
GetWeakPtr() const76 AndroidSystemPropertyDataSource::GetWeakPtr() const {
77   return weak_factory_.GetWeakPtr();
78 }
79 
Tick()80 void AndroidSystemPropertyDataSource::Tick() {
81   if (poll_period_ms_) {
82     auto weak_this = GetWeakPtr();
83 
84     uint32_t delay_ms =
85         poll_period_ms_ -
86         static_cast<uint32_t>(base::GetWallTimeMs().count() % poll_period_ms_);
87     task_runner_->PostDelayedTask(
88         [weak_this]() -> void {
89           if (weak_this) {
90             weak_this->Tick();
91           }
92         },
93         delay_ms);
94   }
95   WriteState();
96 }
97 
WriteState()98 void AndroidSystemPropertyDataSource::WriteState() {
99   auto packet = writer_->NewTracePacket();
100   packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
101   auto* properties = packet->set_android_system_property();
102   for (const auto& name : property_names_) {
103     const std::optional<std::string> value = ReadProperty(name);
104     if (value) {
105       auto* property = properties->add_values();
106       property->set_name(name);
107       property->set_value(*value);
108     }
109   }
110   packet->Finalize();
111   // For most data sources we would not want to flush every time we have
112   // something to write. However this source tends to emit very slowly and it is
113   // very possible that it would only flush at the end of the trace - at which
114   // point it might not be able to write anything (e.g. DISCARD buffer might be
115   // full). Taking the hit of 4kB each time we write seems reasonable to make
116   // this behave more predictably.
117   writer_->Flush();
118 }
119 
120 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
ReadProperty(const std::string & name)121 const std::optional<std::string> AndroidSystemPropertyDataSource::ReadProperty(
122     const std::string& name) {
123   std::string value = base::GetAndroidProp(name.c_str());
124   if (value.empty()) {
125     PERFETTO_LOG("Unable to read %s", name.c_str());
126     return std::nullopt;
127   }
128   return std::make_optional(value);
129 }
130 #else
ReadProperty(const std::string &)131 const std::optional<std::string> AndroidSystemPropertyDataSource::ReadProperty(
132     const std::string&) {
133   PERFETTO_ELOG("Android System Properties only supported on Android.");
134   return std::nullopt;
135 }
136 #endif
137 
Flush(FlushRequestID,std::function<void ()> callback)138 void AndroidSystemPropertyDataSource::Flush(FlushRequestID,
139                                             std::function<void()> callback) {
140   writer_->Flush(callback);
141 }
142 
143 }  // namespace perfetto
144