• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 SRC_JAVA_SDK_MAIN_CPP_UTILS_H_
18 #define SRC_JAVA_SDK_MAIN_CPP_UTILS_H_
19 
20 #include <string>
21 
22 #include <condition_variable>
23 #include <cstdint>
24 #include <memory>
25 #include <mutex>
26 #include <vector>
27 
28 #include "perfetto/public/abi/tracing_session_abi.h"
29 
30 // Copied from src/shared_lib/test/utils.h
31 
32 namespace perfetto {
33 namespace java_sdk {
34 namespace utils {
35 class WaitableEvent {
36  public:
37   WaitableEvent() = default;
Notify()38   void Notify() {
39     std::unique_lock<std::mutex> lock(m_);
40     notified_ = true;
41     cv_.notify_one();
42   }
WaitForNotification()43   bool WaitForNotification() {
44     std::unique_lock<std::mutex> lock(m_);
45     cv_.wait(lock, [this] { return notified_; });
46     return notified_;
47   }
IsNotified()48   bool IsNotified() {
49     std::unique_lock<std::mutex> lock(m_);
50     return notified_;
51   }
52 
53  private:
54   std::mutex m_;
55   std::condition_variable cv_;
56   bool notified_ = false;
57 };
58 
59 class TracingSession {
60  public:
61   class Builder {
62    public:
63     Builder() = default;
set_data_source_name(std::string data_source_name)64     Builder& set_data_source_name(std::string data_source_name) {
65       data_source_name_ = std::move(data_source_name);
66       return *this;
67     }
add_enabled_category(std::string category)68     Builder& add_enabled_category(std::string category) {
69       enabled_categories_.push_back(std::move(category));
70       return *this;
71     }
add_disabled_category(std::string category)72     Builder& add_disabled_category(std::string category) {
73       disabled_categories_.push_back(std::move(category));
74       return *this;
75     }
76     TracingSession Build();
77 
78    private:
79     std::string data_source_name_;
80     std::vector<std::string> enabled_categories_;
81     std::vector<std::string> disabled_categories_;
82   };
83 
84   static TracingSession Adopt(struct PerfettoTracingSessionImpl*);
85 
86   TracingSession(TracingSession&&) noexcept;
87 
88   ~TracingSession();
89 
session()90   struct PerfettoTracingSessionImpl* session() const { return session_; }
91 
92   bool FlushBlocking(uint32_t timeout_ms);
93   // Waits for the tracing session to be stopped.
94   void WaitForStopped();
95   // Asks the tracing session to stop. Doesn't wait for it to be stopped.
96   void StopAsync();
97   // Equivalent to StopAsync() + WaitForStopped().
98   void StopBlocking();
99   std::vector<uint8_t> ReadBlocking();
100 
101  private:
102   TracingSession() = default;
103   struct PerfettoTracingSessionImpl* session_;
104   std::unique_ptr<WaitableEvent> stopped_;
105 };
106 }  // namespace utils
107 }  // namespace java_sdk
108 }  // namespace perfetto
109 
110 #endif  // SRC_JAVA_SDK_MAIN_CPP_UTILS_H_
111