1 /*
2  * Copyright 2023 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 <android_runtime/AndroidRuntime.h>
18 #include <android_runtime/Log.h>
19 #include <nativehelper/JNIHelp.h>
20 #include <perfetto/public/data_source.h>
21 #include <perfetto/public/producer.h>
22 #include <perfetto/public/protos/trace/test_event.pzc.h>
23 #include <perfetto/public/protos/trace/trace_packet.pzc.h>
24 #include <utils/Log.h>
25 #include <utils/RefBase.h>
26 
27 #include <map>
28 #include <sstream>
29 #include <thread>
30 
31 #include "android_tracing_PerfettoDataSourceInstance.h"
32 #include "core_jni_helpers.h"
33 
34 namespace android {
35 
36 class PerfettoDataSource : public virtual RefBase {
37 public:
38     const std::string dataSourceName;
39     struct PerfettoDs dataSource = PERFETTO_DS_INIT();
40 
41     PerfettoDataSource(JNIEnv* env, jobject java_data_source, std::string data_source_name);
42     ~PerfettoDataSource();
43 
44     jobject newInstance(JNIEnv* env, void* ds_config, size_t ds_config_size,
45                         PerfettoDsInstanceIndex inst_id);
46 
47     bool TraceIterateBegin();
48     bool TraceIterateNext();
49     void TraceIterateBreak();
50     PerfettoDsInstanceIndex GetInstanceIndex();
51     void WritePackets(JNIEnv* env, jobjectArray packets);
52 
53     jobject GetCustomTls();
54     void SetCustomTls(jobject);
55     jobject GetIncrementalState();
56     void SetIncrementalState(jobject);
57 
58     void flushAll();
59 
60 private:
61     jobject mJavaDataSource;
62     std::map<PerfettoDsInstanceIndex, PerfettoDataSourceInstance*> mInstances;
63 };
64 
65 } // namespace android