xref: /aosp_15_r20/external/perfetto/src/bigtrace/orchestrator/resizable_task_pool.h (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 #ifndef SRC_BIGTRACE_ORCHESTRATOR_RESIZABLE_TASK_POOL_H_
18 #define SRC_BIGTRACE_ORCHESTRATOR_RESIZABLE_TASK_POOL_H_
19 
20 #include <functional>
21 #include <mutex>
22 #include <thread>
23 
24 #include <grpcpp/client_context.h>
25 
26 namespace perfetto::bigtrace {
27 
28 // This struct maps a thread to a context in order to allow for the cancellation
29 // of the thread's current gRPC call through ClientContext's TryCancel
30 struct ThreadWithContext {
ThreadWithContextThreadWithContext31   explicit ThreadWithContext(std::function<void(ThreadWithContext*)> fn)
32       : thread(fn, this) {}
33 
34   // Cancels the gRPC call through ClientContext as well as signalling a stop to
35   // the thread
CancelThreadWithContext36   void Cancel() {
37     client_context->TryCancel();
38     std::lock_guard<std::mutex> lk(mutex);
39     is_thread_cancelled = true;
40   }
41 
42   // Returns whether the thread has been cancelled
IsCancelledThreadWithContext43   bool IsCancelled() {
44     std::lock_guard<std::mutex> lk(mutex);
45     return is_thread_cancelled;
46   }
47 
48   std::mutex mutex;
49   std::unique_ptr<grpc::ClientContext> client_context;
50   std::thread thread;
51   bool is_thread_cancelled = false;
52 };
53 
54 // This pool manages a set of running tasks for a given query, and provides the
55 // ability to resize in order to fairly distribute an equal number of workers
56 // for each user through preemption
57 class ResizableTaskPool {
58  public:
59   explicit ResizableTaskPool(std::function<void(ThreadWithContext*)> fn);
60   void Resize(uint32_t new_size);
61   void JoinAll();
62 
63  private:
64   std::function<void(ThreadWithContext*)> fn_;
65   std::vector<std::unique_ptr<ThreadWithContext>> contextual_threads_;
66 };
67 }  // namespace perfetto::bigtrace
68 
69 #endif  // SRC_BIGTRACE_ORCHESTRATOR_RESIZABLE_TASK_POOL_H_
70