1 /*
2 * Copyright (C) 2020 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/initial_display_state/initial_display_state_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_polled_state_config.pbzero.h"
27 #include "protos/perfetto/trace/android/initial_display_state.pbzero.h"
28 #include "protos/perfetto/trace/trace_packet.pbzero.h"
29
30 namespace perfetto {
31
32 // static
33 const InitialDisplayStateDataSource::Descriptor
34 InitialDisplayStateDataSource::descriptor = {
35 /* name */ "android.polled_state",
36 /* flags */ Descriptor::kFlagsNone,
37 /*fill_descriptor_func*/ nullptr,
38 };
39
InitialDisplayStateDataSource(base::TaskRunner * task_runner,const DataSourceConfig & ds_config,TracingSessionID session_id,std::unique_ptr<TraceWriter> writer)40 InitialDisplayStateDataSource::InitialDisplayStateDataSource(
41 base::TaskRunner* task_runner,
42 const DataSourceConfig& ds_config,
43 TracingSessionID session_id,
44 std::unique_ptr<TraceWriter> writer)
45 : ProbesDataSource(session_id, &descriptor),
46 task_runner_(task_runner),
47 writer_(std::move(writer)),
48 weak_factory_(this) {
49 protos::pbzero::AndroidPolledStateConfig::Decoder cfg(
50 ds_config.android_polled_state_config_raw());
51 poll_period_ms_ = cfg.poll_ms();
52 if (poll_period_ms_ > 0 && poll_period_ms_ < 100) {
53 PERFETTO_ILOG("poll_ms %" PRIu32
54 " is less than minimum of 100ms. Increasing to 100ms.",
55 poll_period_ms_);
56 poll_period_ms_ = 100;
57 }
58 }
59
Start()60 void InitialDisplayStateDataSource::Start() {
61 Tick();
62 }
63
64 base::WeakPtr<InitialDisplayStateDataSource>
GetWeakPtr() const65 InitialDisplayStateDataSource::GetWeakPtr() const {
66 return weak_factory_.GetWeakPtr();
67 }
68
Tick()69 void InitialDisplayStateDataSource::Tick() {
70 if (poll_period_ms_) {
71 auto weak_this = GetWeakPtr();
72
73 uint32_t delay_ms =
74 poll_period_ms_ -
75 static_cast<uint32_t>(base::GetWallTimeMs().count() % poll_period_ms_);
76 task_runner_->PostDelayedTask(
77 [weak_this]() -> void {
78 if (weak_this) {
79 weak_this->Tick();
80 }
81 },
82 delay_ms);
83 }
84 WriteState();
85 }
86
WriteState()87 void InitialDisplayStateDataSource::WriteState() {
88 auto packet = writer_->NewTracePacket();
89 packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
90 const std::optional<std::string> screen_state_str =
91 ReadProperty("debug.tracing.screen_state");
92 const std::optional<std::string> screen_brightness_str =
93 ReadProperty("debug.tracing.screen_brightness");
94 const std::optional<int> screen_state =
95 screen_state_str ? base::StringToInt32(*screen_state_str) : std::nullopt;
96 const std::optional<double> screen_brightness =
97 screen_brightness_str ? base::StringToDouble(*screen_brightness_str)
98 : std::nullopt;
99 if (screen_state || screen_brightness) {
100 auto* state = packet->set_initial_display_state();
101 if (screen_state) {
102 state->set_display_state(*screen_state);
103 }
104 if (screen_brightness) {
105 state->set_brightness(*screen_brightness);
106 }
107 }
108 packet->Finalize();
109 // For most data sources we would not want to flush every time we have
110 // something to write. However this source tends to emit very slowly and it is
111 // very possible that it would only flush at the end of the trace - at which
112 // point it might not be able to write anything (e.g. DISCARD buffer might be
113 // full). Taking the hit of 4kB each time we write seems reasonable to make
114 // this behave more predictably.
115 writer_->Flush();
116 }
117
118 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
ReadProperty(const std::string name)119 const std::optional<std::string> InitialDisplayStateDataSource::ReadProperty(
120 const std::string name) {
121 std::string value = base::GetAndroidProp(name.c_str());
122 if (value.empty()) {
123 PERFETTO_ELOG("Unable to read %s", name.c_str());
124 return std::nullopt;
125 }
126 return std::make_optional(value);
127 }
128 #else
ReadProperty(const std::string name)129 const std::optional<std::string> InitialDisplayStateDataSource::ReadProperty(
130 const std::string name __attribute__((unused))) {
131 PERFETTO_ELOG("Initial display state only supported on Android.");
132 return std::nullopt;
133 }
134 #endif
135
Flush(FlushRequestID,std::function<void ()> callback)136 void InitialDisplayStateDataSource::Flush(FlushRequestID,
137 std::function<void()> callback) {
138 writer_->Flush(callback);
139 }
140
141 } // namespace perfetto
142