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/core/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, const DataSourceInstanceID*, size_t) override; 47 void StopDataSource(DataSourceInstanceID) override; 48 49 // nops: OnDisconnect()50 void OnDisconnect() override {} OnTracingSetup()51 void OnTracingSetup() override {} ClearIncrementalState(const DataSourceInstanceID *,size_t)52 void ClearIncrementalState(const DataSourceInstanceID*, size_t) override {} 53 54 void ConnectInProcess(TracingService* svc); 55 56 // virtual for testing 57 virtual bool SetAndroidProperty(const std::string& name, 58 const std::string& value); 59 60 private: 61 struct MetatraceState { 62 // If multiple metatrace sources are enabled concurrently, only the first 63 // one becomes active. But we still want to be responsive to the others' 64 // flushes. 65 std::map<DataSourceInstanceID, MetatraceWriter> writers; 66 }; 67 68 struct LazyAndroidDaemonState { 69 // Track active instances to know when to stop. 70 std::set<DataSourceInstanceID> instance_ids; 71 // Delay between the last matching session stopping, and the lazy system 72 // property being unset (to shut down the daemon). 73 uint32_t stop_delay_ms; 74 uint64_t generation = 0; 75 }; 76 77 void MaybeInitiateLazyStop(DataSourceInstanceID ds_id, 78 LazyAndroidDaemonState* lazy_state, 79 const char* prop_name); 80 81 base::TaskRunner* const task_runner_; 82 std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; 83 84 MetatraceState metatrace_; 85 LazyAndroidDaemonState lazy_heapprofd_; 86 LazyAndroidDaemonState lazy_traced_perf_; 87 std::set<DataSourceInstanceID> java_hprof_oome_instances_; 88 89 base::WeakPtrFactory<BuiltinProducer> weak_factory_; // Keep last. 90 }; 91 92 } // namespace perfetto 93 94 #endif // SRC_TRACED_SERVICE_BUILTIN_PRODUCER_H_ 95