1 /*
2  * Copyright (C) 2021 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_TRACED_PROBES_KMEM_ACTIVITY_TRIGGER_H_
18 #define SRC_TRACED_PROBES_KMEM_ACTIVITY_TRIGGER_H_
19 
20 #include <memory>
21 #include <vector>
22 
23 #include "perfetto/ext/base/scoped_file.h"
24 #include "perfetto/ext/base/thread_checker.h"
25 #include "perfetto/ext/base/thread_task_runner.h"
26 #include "perfetto/ext/base/weak_ptr.h"
27 
28 namespace perfetto {
29 
30 class FtraceProcfs;
31 
32 class KmemActivityTrigger {
33  public:
34   KmemActivityTrigger();
35   ~KmemActivityTrigger();
36 
37  private:
38   // This object lives entirely on the KmemActivityTrigger |task_runner_|.
39   class WorkerData {
40    public:
41     WorkerData(base::TaskRunner*);
42     ~WorkerData();
43     void InitializeOnThread();
44     void ArmFtraceFDWatches();
45     void DisarmFtraceFDWatches();
46     void OnFtracePipeWakeup(size_t cpu);
47 
48    private:
49     // All the fields below are accessed only on the dedicated |task_runner_|.
50     base::TaskRunner* const task_runner_;
51     std::unique_ptr<FtraceProcfs> ftrace_procfs_;
52     std::vector<base::ScopedFile> trace_pipe_fds_;
53     size_t num_cpus_ = 0;
54     bool fd_watches_armed_ = false;
55 
56     // Keep last.
57     base::WeakPtrFactory<WorkerData> weak_ptr_factory_;
58     PERFETTO_THREAD_CHECKER(thread_checker_)
59   };
60 
61   base::ThreadTaskRunner task_runner_;
62   std::unique_ptr<WorkerData> worker_data_;
63 };
64 
65 }  // namespace perfetto
66 
67 #endif  // SRC_TRACED_PROBES_KMEM_ACTIVITY_TRIGGER_H_
68