• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "perfetto/base/task_runner.h"
20 #include "perfetto/base/time.h"
21 #include "perfetto/ext/base/optional.h"
22 #include "perfetto/ext/base/string_utils.h"
23 #include "perfetto/tracing/core/data_source_config.h"
24 
25 #include "protos/perfetto/config/android/android_polled_state_config.pbzero.h"
26 #include "protos/perfetto/trace/android/initial_display_state.pbzero.h"
27 #include "protos/perfetto/trace/trace_packet.pbzero.h"
28 
29 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
30 #include <sys/system_properties.h>
31 #endif
32 
33 namespace perfetto {
34 
35 // static
36 const InitialDisplayStateDataSource::Descriptor
37     InitialDisplayStateDataSource::descriptor = {
38         /* name */ "android.polled_state",
39         /* flags */ Descriptor::kFlagsNone,
40 };
41 
InitialDisplayStateDataSource(base::TaskRunner * task_runner,const DataSourceConfig & ds_config,TracingSessionID session_id,std::unique_ptr<TraceWriter> writer)42 InitialDisplayStateDataSource::InitialDisplayStateDataSource(
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::AndroidPolledStateConfig::Decoder cfg(
52       ds_config.android_polled_state_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 }
61 
Start()62 void InitialDisplayStateDataSource::Start() {
63   Tick();
64 }
65 
66 base::WeakPtr<InitialDisplayStateDataSource>
GetWeakPtr() const67 InitialDisplayStateDataSource::GetWeakPtr() const {
68   return weak_factory_.GetWeakPtr();
69 }
70 
Tick()71 void InitialDisplayStateDataSource::Tick() {
72   if (poll_period_ms_) {
73     auto weak_this = GetWeakPtr();
74 
75     uint32_t delay_ms =
76         poll_period_ms_ -
77         static_cast<uint32_t>(base::GetWallTimeMs().count() % poll_period_ms_);
78     task_runner_->PostDelayedTask(
79         [weak_this]() -> void {
80           if (weak_this) {
81             weak_this->Tick();
82           }
83         },
84         delay_ms);
85   }
86   WriteState();
87 }
88 
WriteState()89 void InitialDisplayStateDataSource::WriteState() {
90   auto packet = writer_->NewTracePacket();
91   packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
92   const base::Optional<std::string> screen_state_str =
93       ReadProperty("debug.tracing.screen_state");
94   const base::Optional<std::string> screen_brightness_str =
95       ReadProperty("debug.tracing.screen_brightness");
96   const base::Optional<int> screen_state =
97       screen_state_str ? base::StringToInt32(*screen_state_str) : base::nullopt;
98   const base::Optional<double> screen_brightness =
99       screen_brightness_str ? base::StringToDouble(*screen_brightness_str)
100                             : base::nullopt;
101   if (screen_state || screen_brightness) {
102     auto* state = packet->set_initial_display_state();
103     if (screen_state) {
104       state->set_display_state(*screen_state);
105     }
106     if (screen_brightness) {
107       state->set_brightness(*screen_brightness);
108     }
109   }
110   packet->Finalize();
111   writer_->Flush();
112 }
113 
114 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
ReadProperty(const std::string name)115 const base::Optional<std::string> InitialDisplayStateDataSource::ReadProperty(
116     const std::string name) {
117   char value[PROP_VALUE_MAX];
118   if (__system_property_get(name.c_str(), value)) {
119     return base::make_optional(value);
120   } else {
121     PERFETTO_ELOG("Unable to read %s", name.c_str());
122     return base::nullopt;
123   }
124 }
125 #else
ReadProperty(const std::string name)126 const base::Optional<std::string> InitialDisplayStateDataSource::ReadProperty(
127     const std::string name __attribute__((unused))) {
128   PERFETTO_ELOG("Initial display state only supported on Android.");
129   return base::nullopt;
130 }
131 #endif
132 
Flush(FlushRequestID,std::function<void ()> callback)133 void InitialDisplayStateDataSource::Flush(FlushRequestID,
134                                           std::function<void()> callback) {
135   writer_->Flush(callback);
136 }
137 
138 }  // namespace perfetto
139