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