1*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2020 The Android Open Source Project 2*288bf522SAndroid Build Coastguard Worker# 3*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*288bf522SAndroid Build Coastguard Worker# 7*288bf522SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*288bf522SAndroid Build Coastguard Worker# 9*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*288bf522SAndroid Build Coastguard Worker# limitations under the License. 14*288bf522SAndroid Build Coastguard Workerimport io 15*288bf522SAndroid Build Coastguard Workerimport textwrap 16*288bf522SAndroid Build Coastguard Workerimport unittest 17*288bf522SAndroid Build Coastguard Worker 18*288bf522SAndroid Build Coastguard Workerfrom perf2cfg import analyze 19*288bf522SAndroid Build Coastguard Workerfrom perf2cfg import edit 20*288bf522SAndroid Build Coastguard Worker 21*288bf522SAndroid Build Coastguard Worker 22*288bf522SAndroid Build Coastguard Workerdef empty_analyzer(): 23*288bf522SAndroid Build Coastguard Worker return analyze.RecordAnalyzer() 24*288bf522SAndroid Build Coastguard Worker 25*288bf522SAndroid Build Coastguard Worker 26*288bf522SAndroid Build Coastguard Workerdef populated_analyzer(): 27*288bf522SAndroid Build Coastguard Worker analyzer = analyze.RecordAnalyzer() 28*288bf522SAndroid Build Coastguard Worker analyzer.target_arch = 'aarch64' 29*288bf522SAndroid Build Coastguard Worker samples = [ 30*288bf522SAndroid Build Coastguard Worker ('void hcf()', 4, 'cpu-cycles', 90), 31*288bf522SAndroid Build Coastguard Worker ('void hcf()', 8, 'cpu-cycles', 10), 32*288bf522SAndroid Build Coastguard Worker ('void hcf()', 8, 'cache-misses', 100), 33*288bf522SAndroid Build Coastguard Worker ] 34*288bf522SAndroid Build Coastguard Worker 35*288bf522SAndroid Build Coastguard Worker for sample in samples: 36*288bf522SAndroid Build Coastguard Worker analyzer.record_sample(*sample) 37*288bf522SAndroid Build Coastguard Worker 38*288bf522SAndroid Build Coastguard Worker return analyzer 39*288bf522SAndroid Build Coastguard Worker 40*288bf522SAndroid Build Coastguard Worker 41*288bf522SAndroid Build Coastguard Workerdef edit_string(analyzer, input_string): 42*288bf522SAndroid Build Coastguard Worker input_stream = io.StringIO(input_string) 43*288bf522SAndroid Build Coastguard Worker output_stream = io.StringIO() 44*288bf522SAndroid Build Coastguard Worker 45*288bf522SAndroid Build Coastguard Worker editor = edit.CfgEditor(analyzer, input_stream, output_stream) 46*288bf522SAndroid Build Coastguard Worker editor.edit() 47*288bf522SAndroid Build Coastguard Worker 48*288bf522SAndroid Build Coastguard Worker return output_stream 49*288bf522SAndroid Build Coastguard Worker 50*288bf522SAndroid Build Coastguard Worker 51*288bf522SAndroid Build Coastguard Workerclass TestEdit(unittest.TestCase): 52*288bf522SAndroid Build Coastguard Worker 53*288bf522SAndroid Build Coastguard Worker def test_empty_file(self): 54*288bf522SAndroid Build Coastguard Worker output_stream = edit_string(empty_analyzer(), '') 55*288bf522SAndroid Build Coastguard Worker self.assertEqual(output_stream.getvalue(), '') 56*288bf522SAndroid Build Coastguard Worker 57*288bf522SAndroid Build Coastguard Worker def test_wrong_filetype(self): 58*288bf522SAndroid Build Coastguard Worker with self.assertLogs() as ctx: 59*288bf522SAndroid Build Coastguard Worker edit_string( 60*288bf522SAndroid Build Coastguard Worker empty_analyzer(), """<!DOCTYPE html> 61*288bf522SAndroid Build Coastguard Worker <html> 62*288bf522SAndroid Build Coastguard Worker <head> 63*288bf522SAndroid Build Coastguard Worker <title>I'm not a CFG file</title> 64*288bf522SAndroid Build Coastguard Worker </head> 65*288bf522SAndroid Build Coastguard Worker </html>""") 66*288bf522SAndroid Build Coastguard Worker 67*288bf522SAndroid Build Coastguard Worker self.assertEqual( 68*288bf522SAndroid Build Coastguard Worker ctx.output, 69*288bf522SAndroid Build Coastguard Worker ['ERROR:root:Line 1: Expected a `begin_compilation` directive']) 70*288bf522SAndroid Build Coastguard Worker 71*288bf522SAndroid Build Coastguard Worker def test_no_architecture(self): 72*288bf522SAndroid Build Coastguard Worker with self.assertLogs() as ctx: 73*288bf522SAndroid Build Coastguard Worker edit_string( 74*288bf522SAndroid Build Coastguard Worker populated_analyzer(), """begin_compilation 75*288bf522SAndroid Build Coastguard Worker name "void noMetadata()" 76*288bf522SAndroid Build Coastguard Worker end_compilation""") 77*288bf522SAndroid Build Coastguard Worker 78*288bf522SAndroid Build Coastguard Worker self.assertEqual(ctx.output, [ 79*288bf522SAndroid Build Coastguard Worker 'WARNING:root:Could not deduce the CFG file ISA, assuming it is ' 80*288bf522SAndroid Build Coastguard Worker 'compatible with the target architecture aarch64' 81*288bf522SAndroid Build Coastguard Worker ]) 82*288bf522SAndroid Build Coastguard Worker 83*288bf522SAndroid Build Coastguard Worker def test_wrong_architecture(self): 84*288bf522SAndroid Build Coastguard Worker with self.assertLogs() as ctx: 85*288bf522SAndroid Build Coastguard Worker edit_string( 86*288bf522SAndroid Build Coastguard Worker populated_analyzer(), """begin_compilation 87*288bf522SAndroid Build Coastguard Worker name "isa:x86_64" 88*288bf522SAndroid Build Coastguard Worker end_compilation""") 89*288bf522SAndroid Build Coastguard Worker 90*288bf522SAndroid Build Coastguard Worker self.assertEqual(ctx.output, [ 91*288bf522SAndroid Build Coastguard Worker 'ERROR:root:The CFG file ISA x86_64 is incompatible with the ' 92*288bf522SAndroid Build Coastguard Worker 'target architecture aarch64' 93*288bf522SAndroid Build Coastguard Worker ]) 94*288bf522SAndroid Build Coastguard Worker 95*288bf522SAndroid Build Coastguard Worker def test_annotate_method(self): 96*288bf522SAndroid Build Coastguard Worker with self.assertLogs() as ctx: 97*288bf522SAndroid Build Coastguard Worker output_stream = edit_string( 98*288bf522SAndroid Build Coastguard Worker populated_analyzer(), 99*288bf522SAndroid Build Coastguard Worker textwrap.dedent("""\ 100*288bf522SAndroid Build Coastguard Worker begin_compilation 101*288bf522SAndroid Build Coastguard Worker name "isa:arm64 isa_features:a53,crc,-lse,-fp16,-dotprod,-sve" 102*288bf522SAndroid Build Coastguard Worker end_compilation 103*288bf522SAndroid Build Coastguard Worker begin_compilation 104*288bf522SAndroid Build Coastguard Worker name "void hcf()" 105*288bf522SAndroid Build Coastguard Worker end_compilation 106*288bf522SAndroid Build Coastguard Worker begin_cfg 107*288bf522SAndroid Build Coastguard Worker name "disassembly (after)" 108*288bf522SAndroid Build Coastguard Worker begin_block 109*288bf522SAndroid Build Coastguard Worker flags 110*288bf522SAndroid Build Coastguard Worker begin_HIR 111*288bf522SAndroid Build Coastguard Worker 0 0 NOPSlide dex_pc:0 loop:none 112*288bf522SAndroid Build Coastguard Worker 0x00000000: d503201f nop 113*288bf522SAndroid Build Coastguard Worker 0x00000004: d503201f nop 114*288bf522SAndroid Build Coastguard Worker 0x00000008: d503201f nop 115*288bf522SAndroid Build Coastguard Worker <|@ 116*288bf522SAndroid Build Coastguard Worker end_HIR 117*288bf522SAndroid Build Coastguard Worker end_block 118*288bf522SAndroid Build Coastguard Worker end_cfg""")) 119*288bf522SAndroid Build Coastguard Worker 120*288bf522SAndroid Build Coastguard Worker self.assertEqual(ctx.output, ['INFO:root:Annotated void hcf()']) 121*288bf522SAndroid Build Coastguard Worker self.assertEqual( 122*288bf522SAndroid Build Coastguard Worker output_stream.getvalue(), 123*288bf522SAndroid Build Coastguard Worker textwrap.dedent("""\ 124*288bf522SAndroid Build Coastguard Worker begin_compilation 125*288bf522SAndroid Build Coastguard Worker name "isa:arm64 isa_features:a53,crc,-lse,-fp16,-dotprod,-sve" 126*288bf522SAndroid Build Coastguard Worker end_compilation 127*288bf522SAndroid Build Coastguard Worker begin_compilation 128*288bf522SAndroid Build Coastguard Worker name "[cpu-cycles: 100.00%, cache-misses: 100.00%] void hcf()" 129*288bf522SAndroid Build Coastguard Worker end_compilation 130*288bf522SAndroid Build Coastguard Worker begin_cfg 131*288bf522SAndroid Build Coastguard Worker name "disassembly (after)" 132*288bf522SAndroid Build Coastguard Worker begin_block 133*288bf522SAndroid Build Coastguard Worker flags "cpu-cycles: 100.00%" "cache-misses: 100.00%" "HI" 134*288bf522SAndroid Build Coastguard Worker begin_HIR 135*288bf522SAndroid Build Coastguard Worker 0 0 NOPSlide dex_pc:0 loop:none 136*288bf522SAndroid Build Coastguard Worker _ 0x00000000: d503201f nop 137*288bf522SAndroid Build Coastguard Worker cpu-cycles: 90 (90.00%) 0x00000004: d503201f nop 138*288bf522SAndroid Build Coastguard Worker cache-misses: 0 (0.00%) 139*288bf522SAndroid Build Coastguard Worker cpu-cycles: 10 (10.00%) 0x00000008: d503201f nop 140*288bf522SAndroid Build Coastguard Worker cache-misses: 100 (100.00%) 141*288bf522SAndroid Build Coastguard Worker <|@ 142*288bf522SAndroid Build Coastguard Worker end_HIR 143*288bf522SAndroid Build Coastguard Worker end_block 144*288bf522SAndroid Build Coastguard Worker end_cfg""")) 145