1 /*
2 * Copyright (C) 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 "perfetto/public/abi/track_event_abi.h"
18 #include "perfetto/public/producer.h"
19 #include "perfetto/public/te_category_macros.h"
20 #include "perfetto/public/te_macros.h"
21 #include "perfetto/public/track_event.h"
22
23 #include <stdio.h>
24 #include <unistd.h>
25
26 #define EXAMPLE_CATEGORIES(C) \
27 C(rendering, "rendering", "Rendering events", "tag1", "tag2") \
28 C(physics, "physics", "Physics events", "tag1") \
29 C(cat, "cat", "Sample category") \
30 C(c3, "c3", "c3", "tag1", "tag2", "tag3") \
31 C(c4, "c4", "c4", "tag1", "tag2", "tag3", "tag4")
32
33 PERFETTO_TE_CATEGORIES_DEFINE(EXAMPLE_CATEGORIES)
34
35 static struct PerfettoTeRegisteredTrack mytrack;
36 static struct PerfettoTeRegisteredTrack mycounter;
37
EnabledCb(struct PerfettoTeCategoryImpl * c,PerfettoDsInstanceIndex inst_id,bool enabled,bool global_state_changed,void * user_arg)38 static void EnabledCb(struct PerfettoTeCategoryImpl* c,
39 PerfettoDsInstanceIndex inst_id,
40 bool enabled,
41 bool global_state_changed,
42 void* user_arg) {
43 printf("Callback: %p id: %u on: %d, global_state_changed: %d, user_arg:%p\n",
44 PERFETTO_STATIC_CAST(void*, c), inst_id,
45 PERFETTO_STATIC_CAST(int, enabled),
46 PERFETTO_STATIC_CAST(int, global_state_changed), user_arg);
47 if (enabled) {
48 PERFETTO_TE(physics, PERFETTO_TE_INSTANT("callback"), PERFETTO_TE_FLUSH());
49 }
50 }
51
main(void)52 int main(void) {
53 uint64_t flow_counter = 0;
54 struct PerfettoProducerInitArgs args = PERFETTO_PRODUCER_INIT_ARGS_INIT();
55 args.backends = PERFETTO_BACKEND_SYSTEM;
56 PerfettoProducerInit(args);
57 PerfettoTeInit();
58 PERFETTO_TE_REGISTER_CATEGORIES(EXAMPLE_CATEGORIES);
59 PerfettoTeNamedTrackRegister(&mytrack, "mytrack", 0,
60 PerfettoTeProcessTrackUuid());
61 PerfettoTeCounterTrackRegister(&mycounter, "mycounter",
62 PerfettoTeProcessTrackUuid());
63 PerfettoTeCategorySetCallback(&physics, EnabledCb, PERFETTO_NULL);
64 for (;;) {
65 PERFETTO_TE(rendering, PERFETTO_TE_INSTANT("name1"));
66 PERFETTO_TE(physics, PERFETTO_TE_INSTANT("name2"),
67 PERFETTO_TE_ARG_BOOL("dbg_arg", false),
68 PERFETTO_TE_ARG_STRING("dbg_arg2", "mystring"));
69 PERFETTO_TE(cat, PERFETTO_TE_SLICE_BEGIN("name"));
70 PERFETTO_TE(cat, PERFETTO_TE_SLICE_END());
71 flow_counter++;
72 PERFETTO_TE(physics, PERFETTO_TE_SLICE_BEGIN("name4"),
73 PERFETTO_TE_REGISTERED_TRACK(&mytrack),
74 PERFETTO_TE_FLOW(PerfettoTeProcessScopedFlow(flow_counter)));
75 PERFETTO_TE(physics, PERFETTO_TE_SLICE_END(),
76 PERFETTO_TE_REGISTERED_TRACK(&mytrack));
77 PERFETTO_TE(cat, PERFETTO_TE_INSTANT("name5"),
78 PERFETTO_TE_TIMESTAMP(PerfettoTeGetTimestamp()));
79 PERFETTO_TE(PERFETTO_TE_DYNAMIC_CATEGORY, PERFETTO_TE_INSTANT("name6"),
80 PERFETTO_TE_DYNAMIC_CATEGORY_STRING("physics"),
81 PERFETTO_TE_TERMINATING_FLOW(
82 PerfettoTeProcessScopedFlow(flow_counter)));
83 PERFETTO_TE(physics, PERFETTO_TE_COUNTER(),
84 PERFETTO_TE_REGISTERED_TRACK(&mycounter),
85 PERFETTO_TE_INT_COUNTER(79));
86 PERFETTO_TE(physics, PERFETTO_TE_INSTANT("name8"),
87 PERFETTO_TE_NAMED_TRACK("dynamictrack", 2,
88 PerfettoTeProcessTrackUuid()),
89 PERFETTO_TE_TIMESTAMP(PerfettoTeGetTimestamp()));
90 PERFETTO_TE(PERFETTO_TE_DYNAMIC_CATEGORY, PERFETTO_TE_COUNTER(),
91 PERFETTO_TE_DOUBLE_COUNTER(3.14),
92 PERFETTO_TE_REGISTERED_TRACK(&mycounter),
93 PERFETTO_TE_DYNAMIC_CATEGORY_STRING("physics"));
94 sleep(1);
95 }
96 }
97