• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef INCLUDE_PERFETTO_PUBLIC_PRODUCER_H_
18 #define INCLUDE_PERFETTO_PUBLIC_PRODUCER_H_
19 
20 #include <stdint.h>
21 
22 #include "perfetto/public/abi/backend_type.h"
23 #include "perfetto/public/abi/producer_abi.h"
24 #include "perfetto/public/compiler.h"
25 
26 // Arguments for PerfettoProducerInit. This struct is not ABI-stable, fields can
27 // be added and rearranged.
28 struct PerfettoProducerInitArgs {
29   // Bitwise-or of backends that should be enabled.
30   PerfettoBackendTypes backends;
31 
32   // [Optional] Tune the size of the shared memory buffer between the current
33   // process and the service backend(s). This is a trade-off between memory
34   // footprint and the ability to sustain bursts of trace writes (see comments
35   // in shared_memory_abi.h).
36   // If set, the value must be a multiple of 4KB. The value can be ignored if
37   // larger than kMaxShmSize (32MB) or not a multiple of 4KB.
38   uint32_t shmem_size_hint_kb;
39 };
40 
41 // Initializes a PerfettoProducerInitArgs struct.
42 #define PERFETTO_PRODUCER_INIT_ARGS_INIT() \
43   { 0, 0 }
44 
45 // Initializes the global perfetto producer.
46 //
47 // It's ok to call this function multiple times, but if a backend was already
48 // initialized, most of `args` would be ignored.
PerfettoProducerInit(struct PerfettoProducerInitArgs args)49 static inline void PerfettoProducerInit(struct PerfettoProducerInitArgs args) {
50   struct PerfettoProducerBackendInitArgs* backend_args =
51       PerfettoProducerBackendInitArgsCreate();
52 
53   PerfettoProducerBackendInitArgsSetShmemSizeHintKb(backend_args,
54                                                     args.shmem_size_hint_kb);
55 
56   if (args.backends & PERFETTO_BACKEND_IN_PROCESS) {
57     PerfettoProducerInProcessInit(backend_args);
58   }
59   if (args.backends & PERFETTO_BACKEND_SYSTEM) {
60     PerfettoProducerSystemInit(backend_args);
61   }
62 
63   PerfettoProducerBackendInitArgsDestroy(backend_args);
64 }
65 
66 // Informs the tracing services to activate the single trigger `trigger_name` if
67 // any tracing session was waiting for it.
68 //
69 // Sends the trigger signal to all the initialized backends that are currently
70 // connected and that connect in the next `ttl_ms` milliseconds (but
71 // returns immediately anyway).
PerfettoProducerActivateTrigger(const char * trigger_name,uint32_t ttl_ms)72 static inline void PerfettoProducerActivateTrigger(const char* trigger_name,
73                                                    uint32_t ttl_ms) {
74   const char* trigger_names[2];
75   trigger_names[0] = trigger_name;
76   trigger_names[1] = PERFETTO_NULL;
77   PerfettoProducerActivateTriggers(trigger_names, ttl_ms);
78 }
79 
80 #endif  // INCLUDE_PERFETTO_PUBLIC_PRODUCER_H_
81