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