1*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
2*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
3*288bf522SAndroid Build Coastguard Worker #include <list>
4*288bf522SAndroid Build Coastguard Worker
5*288bf522SAndroid Build Coastguard Worker #include <pin_utils.h>
6*288bf522SAndroid Build Coastguard Worker
7*288bf522SAndroid Build Coastguard Worker using namespace std;
8*288bf522SAndroid Build Coastguard Worker
TEST(pintool_test,pinlist_matches_memranges)9*288bf522SAndroid Build Coastguard Worker TEST(pintool_test, pinlist_matches_memranges) {
10*288bf522SAndroid Build Coastguard Worker vector<VmaRange> vma_ranges;
11*288bf522SAndroid Build Coastguard Worker unsigned int page_size = sysconf(_SC_PAGESIZE);
12*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(0, 500));
13*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(5000, 5500));
14*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(21000, 13000));
15*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(50000, 35000));
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker string test_file = "/data/local/tmp/pintool_test";
18*288bf522SAndroid Build Coastguard Worker write_pinlist_file(test_file, vma_ranges);
19*288bf522SAndroid Build Coastguard Worker
20*288bf522SAndroid Build Coastguard Worker vector<VmaRange> read_ranges;
21*288bf522SAndroid Build Coastguard Worker read_pinlist_file(test_file, read_ranges);
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(vma_ranges.size(), read_ranges.size());
24*288bf522SAndroid Build Coastguard Worker for (size_t i = 0; i < vma_ranges.size(); ++i) {
25*288bf522SAndroid Build Coastguard Worker // We expect to write pinlists that are page-aligned, so
26*288bf522SAndroid Build Coastguard Worker // we compare against page aligned offsets.
27*288bf522SAndroid Build Coastguard Worker uint64_t unaligned_bytes = vma_ranges[i].offset % page_size;
28*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(vma_ranges[i].offset - unaligned_bytes, read_ranges[i].offset);
29*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(vma_ranges[i].length + unaligned_bytes, read_ranges[i].length);
30*288bf522SAndroid Build Coastguard Worker }
31*288bf522SAndroid Build Coastguard Worker
32*288bf522SAndroid Build Coastguard Worker remove(test_file.c_str());
33*288bf522SAndroid Build Coastguard Worker }
34*288bf522SAndroid Build Coastguard Worker
TEST(pintool_test,pinlist_quota_applied)35*288bf522SAndroid Build Coastguard Worker TEST(pintool_test, pinlist_quota_applied) {
36*288bf522SAndroid Build Coastguard Worker vector<VmaRange> vma_ranges;
37*288bf522SAndroid Build Coastguard Worker unsigned int page_size = sysconf(_SC_PAGESIZE);
38*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(0, 100));
39*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(page_size, 500));
40*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(page_size * 2, 300));
41*288bf522SAndroid Build Coastguard Worker vma_ranges.push_back(VmaRange(page_size * 3, 200));
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker const int ranges_to_write = 700;
44*288bf522SAndroid Build Coastguard Worker string test_file = "/data/local/tmp/pintool_test";
45*288bf522SAndroid Build Coastguard Worker write_pinlist_file(test_file, vma_ranges, ranges_to_write);
46*288bf522SAndroid Build Coastguard Worker
47*288bf522SAndroid Build Coastguard Worker vector<VmaRange> read_ranges;
48*288bf522SAndroid Build Coastguard Worker read_pinlist_file(test_file, read_ranges);
49*288bf522SAndroid Build Coastguard Worker
50*288bf522SAndroid Build Coastguard Worker int total_length = 0;
51*288bf522SAndroid Build Coastguard Worker for (size_t i = 0; i < read_ranges.size(); ++i) {
52*288bf522SAndroid Build Coastguard Worker total_length += read_ranges[i].length;
53*288bf522SAndroid Build Coastguard Worker }
54*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(total_length, ranges_to_write);
55*288bf522SAndroid Build Coastguard Worker
56*288bf522SAndroid Build Coastguard Worker remove(test_file.c_str());
57*288bf522SAndroid Build Coastguard Worker }
58*288bf522SAndroid Build Coastguard Worker
TEST(pintool_test,pinconfig_filter_coverage_matches)59*288bf522SAndroid Build Coastguard Worker TEST(pintool_test, pinconfig_filter_coverage_matches) {
60*288bf522SAndroid Build Coastguard Worker VmaRangeGroup* probe = new VmaRangeGroup();
61*288bf522SAndroid Build Coastguard Worker probe->ranges.push_back(VmaRange(0, 500));
62*288bf522SAndroid Build Coastguard Worker probe->ranges.push_back(VmaRange(1000, 5000));
63*288bf522SAndroid Build Coastguard Worker
64*288bf522SAndroid Build Coastguard Worker ZipMemInspector* inspector = new ZipMemInspector("");
65*288bf522SAndroid Build Coastguard Worker
66*288bf522SAndroid Build Coastguard Worker // Probed Resident Memory Offset ranges:
67*288bf522SAndroid Build Coastguard Worker // [0,500],[1000,6000]
68*288bf522SAndroid Build Coastguard Worker probe->compute_total_size();
69*288bf522SAndroid Build Coastguard Worker inspector->set_existing_probe(probe);
70*288bf522SAndroid Build Coastguard Worker
71*288bf522SAndroid Build Coastguard Worker // Emulate reading some files from the zip to compute their coverages
72*288bf522SAndroid Build Coastguard Worker // fake1 memory offset ranges [100,400]
73*288bf522SAndroid Build Coastguard Worker ZipEntryInfo info;
74*288bf522SAndroid Build Coastguard Worker info.name = "fake1";
75*288bf522SAndroid Build Coastguard Worker info.offset_in_zip = 100;
76*288bf522SAndroid Build Coastguard Worker info.file_size_bytes = 300;
77*288bf522SAndroid Build Coastguard Worker inspector->add_file_info(info);
78*288bf522SAndroid Build Coastguard Worker
79*288bf522SAndroid Build Coastguard Worker // fake2 memory offset ranges [600,3000]
80*288bf522SAndroid Build Coastguard Worker ZipEntryInfo info2;
81*288bf522SAndroid Build Coastguard Worker info2.name = "fake2";
82*288bf522SAndroid Build Coastguard Worker info2.offset_in_zip = 600;
83*288bf522SAndroid Build Coastguard Worker info2.file_size_bytes = 2400;
84*288bf522SAndroid Build Coastguard Worker inspector->add_file_info(info2);
85*288bf522SAndroid Build Coastguard Worker
86*288bf522SAndroid Build Coastguard Worker ZipEntryInfo info3;
87*288bf522SAndroid Build Coastguard Worker info2.name = "fake3";
88*288bf522SAndroid Build Coastguard Worker info2.offset_in_zip = 3100;
89*288bf522SAndroid Build Coastguard Worker info2.file_size_bytes = 200;
90*288bf522SAndroid Build Coastguard Worker inspector->add_file_info(info3);
91*288bf522SAndroid Build Coastguard Worker
92*288bf522SAndroid Build Coastguard Worker // Create a fake pinconfig file
93*288bf522SAndroid Build Coastguard Worker PinConfig* pinconfig = new PinConfig();
94*288bf522SAndroid Build Coastguard Worker
95*288bf522SAndroid Build Coastguard Worker // First file we want it entirely so don't provide ranges
96*288bf522SAndroid Build Coastguard Worker PinConfigFile pinconfig_file_1;
97*288bf522SAndroid Build Coastguard Worker pinconfig_file_1.filename = "fake1";
98*288bf522SAndroid Build Coastguard Worker pinconfig->files_.push_back(pinconfig_file_1);
99*288bf522SAndroid Build Coastguard Worker
100*288bf522SAndroid Build Coastguard Worker // Add a partially matched file
101*288bf522SAndroid Build Coastguard Worker PinConfigFile pinconfig_file_2;
102*288bf522SAndroid Build Coastguard Worker pinconfig_file_2.filename = "fake2";
103*288bf522SAndroid Build Coastguard Worker pinconfig_file_2.ranges.push_back(VmaRange(100, 500));
104*288bf522SAndroid Build Coastguard Worker pinconfig_file_2.ranges.push_back(VmaRange(800, 200));
105*288bf522SAndroid Build Coastguard Worker pinconfig->files_.push_back(pinconfig_file_2);
106*288bf522SAndroid Build Coastguard Worker
107*288bf522SAndroid Build Coastguard Worker // Add a file that does not exist
108*288bf522SAndroid Build Coastguard Worker PinConfigFile pinconfig_file_3;
109*288bf522SAndroid Build Coastguard Worker pinconfig_file_3.filename = "fake4";
110*288bf522SAndroid Build Coastguard Worker pinconfig_file_3.ranges.push_back(VmaRange(0, 1000));
111*288bf522SAndroid Build Coastguard Worker pinconfig->files_.push_back(pinconfig_file_3);
112*288bf522SAndroid Build Coastguard Worker
113*288bf522SAndroid Build Coastguard Worker PinTool pintool("");
114*288bf522SAndroid Build Coastguard Worker pintool.set_custom_zip_inspector(inspector);
115*288bf522SAndroid Build Coastguard Worker pintool.compute_zip_entry_coverages();
116*288bf522SAndroid Build Coastguard Worker pintool.filter_zip_entry_coverages(pinconfig);
117*288bf522SAndroid Build Coastguard Worker
118*288bf522SAndroid Build Coastguard Worker std::vector<ZipEntryCoverage> filtered = pintool.get_filtered_zip_entries();
119*288bf522SAndroid Build Coastguard Worker
120*288bf522SAndroid Build Coastguard Worker // We only matched 2 files, one should not have matched to any filter.
121*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered.size(), (unsigned long)2);
122*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[0].info.name, "fake1");
123*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[0].coverage.ranges[0].offset, (unsigned long)100);
124*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[0].coverage.ranges[0].length, (unsigned long)300);
125*288bf522SAndroid Build Coastguard Worker
126*288bf522SAndroid Build Coastguard Worker // Probe Resident has [0,500],[1000,6000].
127*288bf522SAndroid Build Coastguard Worker // fake2 file lives within [600,3000]
128*288bf522SAndroid Build Coastguard Worker // fake2 relative offsets from pinconfig [100,600],[800,1000]
129*288bf522SAndroid Build Coastguard Worker // fake2 absolute zip offsets are [700,1200],[1400,1600]
130*288bf522SAndroid Build Coastguard Worker // then matching absolute offsets against resident yields [1000,1200],[1400,1600]
131*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[1].info.name, "fake2");
132*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[1].info.offset_in_zip, (unsigned long)600);
133*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[1].coverage.ranges[0].offset, (unsigned long)1000);
134*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[1].coverage.ranges[0].length, (unsigned long)200);
135*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[1].coverage.ranges[1].offset, (unsigned long)1400);
136*288bf522SAndroid Build Coastguard Worker EXPECT_EQ(filtered[1].coverage.ranges[1].length, (unsigned long)200);
137*288bf522SAndroid Build Coastguard Worker }