• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "profiler.h"
16 
17 #include <android-base/properties.h>
18 #include <sys/prctl.h>
19 
20 #include "perfetto.h"
21 
22 namespace {
23 
24 class GpuCounterDataSource : public perfetto::DataSource<GpuCounterDataSource> {
25  public:
OnSetup(const SetupArgs & args)26   void OnSetup(const SetupArgs& args) override {
27     PERFETTO_ILOG("GpuCounterDataSource OnSetup, name: %s", args.config->name().c_str());
28     const std::string& config_raw = args.config->gpu_counter_config_raw();
29     perfetto::protos::pbzero::GpuCounterConfig::Decoder config(config_raw);
30     for(auto it = config.counter_ids(); it; ++it) {
31       counter_ids.push_back(it->as_uint32());
32     }
33     first = true;
34   }
35 
OnStart(const StartArgs &)36   void OnStart(const StartArgs&) override {
37     PERFETTO_ILOG("GpuCounterDataSource OnStart called");
38   }
39 
OnStop(const StopArgs &)40   void OnStop(const StopArgs&) override {
41     PERFETTO_ILOG("GpuCounterDataSource OnStop called");
42   }
43 
44   bool first = true;
45   uint64_t count = 0;
46   std::vector<uint32_t> counter_ids;
47 };
48 
49 class GpuRenderStageDataSource: public perfetto::DataSource<GpuRenderStageDataSource> {
50  public:
OnSetup(const SetupArgs & args)51   void OnSetup(const SetupArgs& args) override {
52     PERFETTO_ILOG("GpuRenderStageDataSource OnSetup called, name: %s",
53       args.config->name().c_str());
54     first = true;
55   }
56 
OnStart(const StartArgs &)57   void OnStart(const StartArgs&) override {
58     PERFETTO_ILOG("GpuRenderStageDataSource OnStart called");
59   }
60 
OnStop(const StopArgs &)61   void OnStop(const StopArgs&) override {
62     PERFETTO_ILOG("GpuRenderStageDataSource OnStop called");
63   }
64 
65   bool first = true;
66   uint64_t count = 0;
67 };
68 
69 }
70 
try_register_goldfish_perfetto()71 void try_register_goldfish_perfetto() {
72   std::string enableString = android::base::GetProperty("debug.graphics.gpu.profiler.perfetto", "");
73   if (enableString != "1" && enableString != "true") {
74     return;
75   }
76   if (!prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
77     return;
78   }
79   perfetto::TracingInitArgs args;
80   args.backends = perfetto::kSystemBackend;
81   perfetto::Tracing::Initialize(args);
82   {
83     perfetto::DataSourceDescriptor dsd;
84     dsd.set_name("gpu.counters");
85     GpuCounterDataSource::Register(dsd);
86   }
87 
88   {
89     perfetto::DataSourceDescriptor dsd;
90     dsd.set_name("gpu.renderstages");
91     GpuRenderStageDataSource::Register(dsd);
92   }
93 }
94