xref: /aosp_15_r20/external/perfetto/examples/shared_lib/example_shlib_data_source.c (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2022 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 <unistd.h>
18 
19 #include "perfetto/public/data_source.h"
20 #include "perfetto/public/producer.h"
21 #include "perfetto/public/protos/trace/test_event.pzc.h"
22 #include "perfetto/public/protos/trace/trace_packet.pzc.h"
23 
24 static struct PerfettoDs custom = PERFETTO_DS_INIT();
25 
main(void)26 int main(void) {
27   struct PerfettoProducerInitArgs args = PERFETTO_PRODUCER_INIT_ARGS_INIT();
28   args.backends = PERFETTO_BACKEND_SYSTEM;
29   PerfettoProducerInit(args);
30 
31   PerfettoDsRegister(&custom, "com.example.custom_data_source",
32                      PerfettoDsParamsDefault());
33 
34   for (;;) {
35     PERFETTO_DS_TRACE(custom, ctx) {
36       struct PerfettoDsRootTracePacket root;
37       PerfettoDsTracerPacketBegin(&ctx, &root);
38 
39       perfetto_protos_TracePacket_set_timestamp(&root.msg, 42);
40       {
41         struct perfetto_protos_TestEvent for_testing;
42         perfetto_protos_TracePacket_begin_for_testing(&root.msg, &for_testing);
43 
44         perfetto_protos_TestEvent_set_cstr_str(&for_testing,
45                                                "This is a long string");
46         {
47           struct perfetto_protos_TestEvent_TestPayload payload;
48           perfetto_protos_TestEvent_begin_payload(&for_testing, &payload);
49 
50           for (int i = 0; i < 1000; i++) {
51             perfetto_protos_TestEvent_TestPayload_set_cstr_str(&payload,
52                                                                "nested");
53           }
54           perfetto_protos_TestEvent_end_payload(&for_testing, &payload);
55         }
56         perfetto_protos_TracePacket_end_for_testing(&root.msg, &for_testing);
57       }
58       PerfettoDsTracerPacketEnd(&ctx, &root);
59     }
60     sleep(1);
61   }
62 }
63