1#!/usr/bin/env ruby 2 3# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16require 'ostruct' 17require 'yaml' 18 19# Object of `CompiledMethod` is created for each method, defined in the `validation.yaml`, which looks as follows: 20# AllocateObjectTlab: 21# spills_count_max: 0 22# code_size_max: 125 23# 24class CompiledMethod < OpenStruct 25 def initialize 26 super 27 end 28 29 def spills_count_max(value) 30 real_spills_count = self.spills_count.to_i 31 if real_spills_count > value 32 raise "[Validation] `spills_count_max` failed for method `#{self.name}`: expected(#{value}) > real(#{real_spills_count})" 33 end 34 end 35 36 def code_size_max(value) 37 code_size_number = self.code_size.to_i 38 if code_size_number > value 39 raise "[Validation] `code_size_max` failed for method `#{self.name}`: expected(#{value}) > real(#{code_size_number})" 40 end 41 end 42end 43 44def parse_methods(dump_file) 45 methods = [] 46 current_method = nil 47 good_data = false 48 File.open(dump_file).read.each_line do |line| 49 if !line.start_with? " " 50 if line.start_with? "METHOD_INFO:" 51 methods << CompiledMethod.new 52 good_data = true 53 next 54 elsif line.start_with? "CODE_STATS:" 55 good_data = true 56 else 57 good_data = false 58 end 59 next 60 end 61 next if !good_data || line.start_with?("==") 62 63 data = line.split(':', 2).map(&:strip) 64 if data&.size == 2 65 methods[-1][data[0]] = data[1] 66 end 67 end 68 Hash[methods.map { |m| [m.name.split('::')[1].to_sym, m] } ] 69end 70 71def main 72 validation_file = ARGV[0] 73 dump_file = ARGV[1] 74 75 methods = parse_methods(dump_file) 76 77 YAML.load_file(validation_file).each do |method_name, data| 78 method = methods[method_name.to_sym] 79 raise "Method not found: #{method_name}" unless method 80 data.each { |constraint, value| method.send(constraint.to_sym, value) } 81 end 82end 83 84main