xref: /aosp_15_r20/system/extras/pinner/include/pin_utils.h (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker #pragma once
2*288bf522SAndroid Build Coastguard Worker 
3*288bf522SAndroid Build Coastguard Worker #include <list>
4*288bf522SAndroid Build Coastguard Worker #include "meminspect.h"
5*288bf522SAndroid Build Coastguard Worker 
6*288bf522SAndroid Build Coastguard Worker struct PinConfigFile {
7*288bf522SAndroid Build Coastguard Worker     std::string filename;
8*288bf522SAndroid Build Coastguard Worker 
9*288bf522SAndroid Build Coastguard Worker     // File relative offsets
10*288bf522SAndroid Build Coastguard Worker     std::vector<VmaRange> ranges;
11*288bf522SAndroid Build Coastguard Worker 
12*288bf522SAndroid Build Coastguard Worker     ZipEntryCoverage to_zipfilemem(const ZipEntryInfo& info);
13*288bf522SAndroid Build Coastguard Worker };
14*288bf522SAndroid Build Coastguard Worker 
15*288bf522SAndroid Build Coastguard Worker struct PinConfig {
16*288bf522SAndroid Build Coastguard Worker     std::list<PinConfigFile> files_;
17*288bf522SAndroid Build Coastguard Worker 
18*288bf522SAndroid Build Coastguard Worker     int parse(std::string filename, bool verbose = false);
19*288bf522SAndroid Build Coastguard Worker };
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker /**
22*288bf522SAndroid Build Coastguard Worker  * @brief Generate a pinlist file from a given list of vmas containing a list of 4-byte pairs
23*288bf522SAndroid Build Coastguard Worker  * representing (4-byte offset, 4-byte len) contiguous in memory and they are stored in big endian
24*288bf522SAndroid Build Coastguard Worker  * format.
25*288bf522SAndroid Build Coastguard Worker  *
26*288bf522SAndroid Build Coastguard Worker  * @param output_file Output file to write pinlist
27*288bf522SAndroid Build Coastguard Worker  * @param vmas_to_pin Set of vmas to write into pinlist file.
28*288bf522SAndroid Build Coastguard Worker  * @param write_quota Specifies a maximum amount o bytes to be written to the pinlist file
29*288bf522SAndroid Build Coastguard Worker  * or -1 means no limit.
30*288bf522SAndroid Build Coastguard Worker  * @return 0 on success, non-zero on failure
31*288bf522SAndroid Build Coastguard Worker  */
32*288bf522SAndroid Build Coastguard Worker int write_pinlist_file(const std::string& output_file, const std::vector<VmaRange>& vmas_to_pin,
33*288bf522SAndroid Build Coastguard Worker                        int64_t write_quota = -1);
34*288bf522SAndroid Build Coastguard Worker 
35*288bf522SAndroid Build Coastguard Worker /**
36*288bf522SAndroid Build Coastguard Worker  * @brief This method is the counter part of @see write_pinlist_file(). It will read an existing
37*288bf522SAndroid Build Coastguard Worker  * pinlist file.
38*288bf522SAndroid Build Coastguard Worker  *
39*288bf522SAndroid Build Coastguard Worker  * @param pinner_file Input pinlist file
40*288bf522SAndroid Build Coastguard Worker  * @param pinranges Vmas read from pinlist file. This is populated on call.
41*288bf522SAndroid Build Coastguard Worker  * @return 0 on success, non-zero on failure
42*288bf522SAndroid Build Coastguard Worker  */
43*288bf522SAndroid Build Coastguard Worker int read_pinlist_file(const std::string& pinner_file, /*out*/ std::vector<VmaRange>& pinranges);
44*288bf522SAndroid Build Coastguard Worker 
45*288bf522SAndroid Build Coastguard Worker enum ProbeType {
46*288bf522SAndroid Build Coastguard Worker     UNSET,     // No probe setup
47*288bf522SAndroid Build Coastguard Worker     GENERATE,  // Generate a probe
48*288bf522SAndroid Build Coastguard Worker     CUSTOM     // User generated probe
49*288bf522SAndroid Build Coastguard Worker };
50*288bf522SAndroid Build Coastguard Worker 
51*288bf522SAndroid Build Coastguard Worker class PinTool {
52*288bf522SAndroid Build Coastguard Worker   public:
53*288bf522SAndroid Build Coastguard Worker     enum DumpType { PROBE, FILE_COVERAGE, FILTERED };
54*288bf522SAndroid Build Coastguard Worker 
55*288bf522SAndroid Build Coastguard Worker   private:
56*288bf522SAndroid Build Coastguard Worker     std::string input_file_;
57*288bf522SAndroid Build Coastguard Worker     std::string custom_probe_file_;
58*288bf522SAndroid Build Coastguard Worker     PinConfig* pinconfig_;
59*288bf522SAndroid Build Coastguard Worker     std::vector<ZipEntryCoverage> filtered_files_;
60*288bf522SAndroid Build Coastguard Worker     bool verbose_;
61*288bf522SAndroid Build Coastguard Worker     ZipMemInspector* zip_inspector_ = nullptr;
62*288bf522SAndroid Build Coastguard Worker 
63*288bf522SAndroid Build Coastguard Worker   public:
PinTool(const std::string & input_file)64*288bf522SAndroid Build Coastguard Worker     PinTool(const std::string& input_file) : input_file_(input_file) {
65*288bf522SAndroid Build Coastguard Worker         zip_inspector_ = new ZipMemInspector(input_file_);
66*288bf522SAndroid Build Coastguard Worker     }
67*288bf522SAndroid Build Coastguard Worker 
~PinTool()68*288bf522SAndroid Build Coastguard Worker     ~PinTool() {
69*288bf522SAndroid Build Coastguard Worker         delete zip_inspector_;
70*288bf522SAndroid Build Coastguard Worker         delete pinconfig_;
71*288bf522SAndroid Build Coastguard Worker     }
72*288bf522SAndroid Build Coastguard Worker 
73*288bf522SAndroid Build Coastguard Worker     void set_verbose_output(bool verbose);
74*288bf522SAndroid Build Coastguard Worker 
75*288bf522SAndroid Build Coastguard Worker     // Read |probe_file| which should be a pinlist.meta style
76*288bf522SAndroid Build Coastguard Worker     // file and use it as current probe.
77*288bf522SAndroid Build Coastguard Worker     void read_probe_from_pinlist(std::string probe_file);
78*288bf522SAndroid Build Coastguard Worker 
79*288bf522SAndroid Build Coastguard Worker     // Compute a resident memory probe for |input_file_|
80*288bf522SAndroid Build Coastguard Worker     int probe_resident();
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker     // Compute coverage for each zip entry contained within
83*288bf522SAndroid Build Coastguard Worker     // |input_file_|.
84*288bf522SAndroid Build Coastguard Worker     // Note: It only works for zip files
85*288bf522SAndroid Build Coastguard Worker     void compute_zip_entry_coverages();
86*288bf522SAndroid Build Coastguard Worker 
87*288bf522SAndroid Build Coastguard Worker     /**
88*288bf522SAndroid Build Coastguard Worker      * Filter coverages based on a provided pinconfig style file
89*288bf522SAndroid Build Coastguard Worker      * See README.md for sample structure of pinconfig file.
90*288bf522SAndroid Build Coastguard Worker      *
91*288bf522SAndroid Build Coastguard Worker      * Note: It only works for zip files, for non zip files, this will be
92*288bf522SAndroid Build Coastguard Worker      * a no-op.
93*288bf522SAndroid Build Coastguard Worker      */
94*288bf522SAndroid Build Coastguard Worker     void filter_zip_entry_coverages(const std::string& pinconfig_file);
95*288bf522SAndroid Build Coastguard Worker 
96*288bf522SAndroid Build Coastguard Worker     void filter_zip_entry_coverages(PinConfig* pinconfig);
97*288bf522SAndroid Build Coastguard Worker 
98*288bf522SAndroid Build Coastguard Worker     /**
99*288bf522SAndroid Build Coastguard Worker      * Dumps output of existing coverages to console for |type|.
100*288bf522SAndroid Build Coastguard Worker      */
101*288bf522SAndroid Build Coastguard Worker     void dump_coverages(DumpType type);
102*288bf522SAndroid Build Coastguard Worker 
103*288bf522SAndroid Build Coastguard Worker     /**
104*288bf522SAndroid Build Coastguard Worker      * Writes coverages into a pinlist.meta style file.
105*288bf522SAndroid Build Coastguard Worker      *
106*288bf522SAndroid Build Coastguard Worker      * @param write_quota Maximum bytes allowed to be written to file.
107*288bf522SAndroid Build Coastguard Worker      */
108*288bf522SAndroid Build Coastguard Worker     void write_coverages_as_pinlist(std::string output_pinlist, int64_t write_quota = -1);
109*288bf522SAndroid Build Coastguard Worker 
110*288bf522SAndroid Build Coastguard Worker     std::vector<ZipEntryCoverage> get_filtered_zip_entries();
111*288bf522SAndroid Build Coastguard Worker 
112*288bf522SAndroid Build Coastguard Worker     /**
113*288bf522SAndroid Build Coastguard Worker      * Sets a user defined inspector, currently only used for testing.
114*288bf522SAndroid Build Coastguard Worker      */
115*288bf522SAndroid Build Coastguard Worker     void set_custom_zip_inspector(ZipMemInspector* inspector);
116*288bf522SAndroid Build Coastguard Worker };