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