xref: /aosp_15_r20/external/perfetto/src/bigtrace/orchestrator/trace_address_pool.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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 "src/bigtrace/orchestrator/trace_address_pool.h"
18 #include "perfetto/base/logging.h"
19 
20 namespace perfetto::bigtrace {
21 
TraceAddressPool(const std::vector<std::string> & trace_addresses)22 TraceAddressPool::TraceAddressPool(
23     const std::vector<std::string>& trace_addresses)
24     : trace_addresses_(trace_addresses) {}
25 
26 // Pops a trace address from the pool, blocking if necessary
27 //
28 // Returns a nullopt if the pool is empty
Pop()29 std::optional<std::string> TraceAddressPool::Pop() {
30   std::lock_guard<std::mutex> trace_addresses_guard(trace_addresses_lock_);
31   if (trace_addresses_.size() == 0) {
32     return std::nullopt;
33   }
34   std::string trace_address = trace_addresses_.back();
35   trace_addresses_.pop_back();
36   running_queries_++;
37   return trace_address;
38 }
39 
40 // Marks a trace address as cancelled
41 //
42 // Returns cancelled trace addresses to the pool for future calls to |Pop|
MarkCancelled(std::string trace_address)43 void TraceAddressPool::MarkCancelled(std::string trace_address) {
44   std::lock_guard<std::mutex> guard(trace_addresses_lock_);
45   PERFETTO_CHECK(running_queries_-- > 0);
46   trace_addresses_.push_back(std::move(trace_address));
47 }
48 
49 // Returns the number of remaining trace addresses which require processing
RemainingCount()50 uint32_t TraceAddressPool::RemainingCount() {
51   std::lock_guard<std::mutex> guard(trace_addresses_lock_);
52   return static_cast<uint32_t>(trace_addresses_.size()) + running_queries_;
53 }
54 
55 }  // namespace perfetto::bigtrace
56