1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_MAINTENANCE_COMPILER_CONTROLLER_H_
16 #define IORAP_SRC_MAINTENANCE_COMPILER_CONTROLLER_H_
17 
18 #include "db/file_models.h"
19 #include "inode2filename/inode_resolver.h"
20 
21 #include <string>
22 #include <vector>
23 
24 namespace android {
25 class Printer;
26 }  // namespace android
27 
28 namespace iorap::maintenance {
29 
30 // Enabling mock for testing purpose.
31 class IExec {
32  public:
33   virtual int Execve(const std::string& pathname,
34                      std::vector<std::string>& argv_vec,
35                      char *const envp[]) = 0;
36   virtual int Fork() = 0;
37   virtual ~IExec() = default;
38 };
39 
40 class Exec : public IExec {
41  public:
42    virtual int Execve(const std::string& pathname,
43                       std::vector<std::string>& argv_vec,
44                       char *const envp[]);
45    virtual int Fork();
46 };
47 
48 // Represents the parameters used for compilation controller.
49 struct ControllerParameters {
50   bool output_text;
51   // The path of inode2filepath file.
52   std::optional<std::string> inode_textcache;
53   bool verbose;
54   bool recompile;
55   uint64_t min_traces;
56   std::shared_ptr<IExec> exec;
57   bool exclude_dex_files;
58 
ControllerParametersControllerParameters59   ControllerParameters(bool output_text,
60                        std::optional<std::string> inode_textcache,
61                        bool verbose,
62                        bool recompile,
63                        uint64_t min_traces,
64                        std::shared_ptr<IExec> exec,
65                        bool exclude_dex_files) :
66     output_text(output_text),
67     inode_textcache(inode_textcache),
68     verbose(verbose),
69     recompile(recompile),
70     min_traces(min_traces),
71     exec(exec),
72     exclude_dex_files(exclude_dex_files) {
73   }
74 };
75 
76 // Control the compilation of perfetto traces in the sqlite db.
77 //
78 // The strategy now is to compile all the existing perfetto traces for an activity
79 // and skip ones if the number of perfetto traces is less than the min_traces.
80 //
81 // By default, the program doesn't replace the existing compiled trace, it just
82 // return true. To force replace the existing compiled trace, set `force` to true.
83 //
84 // The timestamp limit of the each perfetto trace is determined by `report_fully_drawn_ns`
85 // timestamp. If it doesn't exists, use `total_time_ns`. If neither of them exists,
86 // use the max.
87 
88 // Compile all activities of all packages in the database.
89 bool Compile(const std::string& db_path, const ControllerParameters& params);
90 
91 // Compile all activities in the package.
92 // If the version is not given, an arbitrary package that has the same name is used.
93 bool Compile(const std::string& db_path,
94              const std::string& package_name,
95              int version,
96              const ControllerParameters& params);
97 
98 // Compile trace for the activity.
99 // If the version is not given, an arbitrary package has the same name is used.
100 bool Compile(const std::string& db_path,
101              const std::string& package_name,
102              const std::string& activity_name,
103              int version,
104              const ControllerParameters& params);
105 // Visible for testing.
106 bool CompileAppsOnDevice(const db::DbHandle& db, const ControllerParameters& params);
107 
108 bool CompileSingleAppOnDevice(const db::DbHandle& db,
109                               const ControllerParameters& params,
110                               const std::string& package_name);
111 
112 void Dump(const db::DbHandle& db, ::android::Printer& printer);
113 
114 } // iorap::maintenance
115 
116 #endif  // IORAP_SRC_MAINTENANCE_COMPILER_CONTROLLER_H_
117