1 /* 2 * Copyright (C) 2019 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 #ifndef SRC_TRACED_SERVICE_BUILTIN_PRODUCER_H_ 18 #define SRC_TRACED_SERVICE_BUILTIN_PRODUCER_H_ 19 20 #include <map> 21 #include <set> 22 #include <string> 23 24 #include "perfetto/base/task_runner.h" 25 #include "perfetto/ext/base/weak_ptr.h" 26 #include "perfetto/ext/tracing/core/basic_types.h" 27 #include "perfetto/ext/tracing/core/producer.h" 28 #include "perfetto/ext/tracing/core/tracing_service.h" 29 #include "src/tracing/service/metatrace_writer.h" 30 31 namespace perfetto { 32 33 // Data sources built into the tracing service daemon (traced): 34 // * perfetto metatrace 35 // * lazy heapprofd daemon starter (android only) 36 // * lazy traced_perf daemon starter (android only) 37 // * java_hprof oom data source counter (android only) 38 class BuiltinProducer : public Producer { 39 public: 40 BuiltinProducer(base::TaskRunner* task_runner, uint32_t lazy_stop_delay_ms); 41 42 ~BuiltinProducer() override; 43 void OnConnect() override; 44 void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&) override; 45 void StartDataSource(DataSourceInstanceID, const DataSourceConfig&) override; 46 void Flush(FlushRequestID, 47 const DataSourceInstanceID*, 48 size_t, 49 FlushFlags) override; 50 void StopDataSource(DataSourceInstanceID) override; 51 52 // nops: OnDisconnect()53 void OnDisconnect() override {} OnTracingSetup()54 void OnTracingSetup() override {} ClearIncrementalState(const DataSourceInstanceID *,size_t)55 void ClearIncrementalState(const DataSourceInstanceID*, size_t) override {} 56 57 void ConnectInProcess(TracingService* svc); 58 59 // virtual for testing 60 virtual bool SetAndroidProperty(const std::string& name, 61 const std::string& value); 62 63 private: 64 struct MetatraceState { 65 // If multiple metatrace sources are enabled concurrently, only the first 66 // one becomes active. But we still want to be responsive to the others' 67 // flushes. 68 std::map<DataSourceInstanceID, MetatraceWriter> writers; 69 }; 70 71 struct LazyAndroidDaemonState { 72 // Track active instances to know when to stop. 73 std::set<DataSourceInstanceID> instance_ids; 74 // Delay between the last matching session stopping, and the lazy system 75 // property being unset (to shut down the daemon). 76 uint32_t stop_delay_ms; 77 uint64_t generation = 0; 78 }; 79 80 struct AndroidSdkSyspropGuardState { 81 // "Initialized" refers to whether the Perfetto SDK and the Track Event 82 // data source have been initialized in Skia in the given scenario. 83 // Note: once initialized, it cannot be de-initialized. 84 bool surfaceflinger_initialized = false; 85 bool hwui_globally_initialized = false; 86 std::set<std::string> hwui_packages_initialized = std::set<std::string>(); 87 // Incremented when the scope of what should be initialized increases. 88 uint64_t generation = 0; 89 }; 90 91 void MaybeInitiateLazyStop(DataSourceInstanceID ds_id, 92 LazyAndroidDaemonState* lazy_state, 93 const char* prop_name); 94 95 base::TaskRunner* const task_runner_; 96 std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; 97 98 MetatraceState metatrace_; 99 LazyAndroidDaemonState lazy_heapprofd_; 100 LazyAndroidDaemonState lazy_traced_perf_; 101 std::set<DataSourceInstanceID> java_hprof_oome_instances_; 102 AndroidSdkSyspropGuardState android_sdk_sysprop_guard_state_; 103 104 base::WeakPtrFactory<BuiltinProducer> weak_factory_; // Keep last. 105 }; 106 107 } // namespace perfetto 108 109 #endif // SRC_TRACED_SERVICE_BUILTIN_PRODUCER_H_ 110