1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2018 - The Android Open Source Project 3*c2e18aaaSAndroid Build Coastguard Worker# 4*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 5*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 6*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 7*c2e18aaaSAndroid Build Coastguard Worker# 8*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 9*c2e18aaaSAndroid Build Coastguard Worker# 10*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 11*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 12*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 14*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 15*c2e18aaaSAndroid Build Coastguard Worker 16*c2e18aaaSAndroid Build Coastguard Worker"""AIDEgen metrics functions.""" 17*c2e18aaaSAndroid Build Coastguard Worker 18*c2e18aaaSAndroid Build Coastguard Workerimport logging 19*c2e18aaaSAndroid Build Coastguard Workerimport os 20*c2e18aaaSAndroid Build Coastguard Workerimport platform 21*c2e18aaaSAndroid Build Coastguard Workerimport sys 22*c2e18aaaSAndroid Build Coastguard Worker 23*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen import constant 24*c2e18aaaSAndroid Build Coastguard Workerfrom aidegen.lib import common_util 25*c2e18aaaSAndroid Build Coastguard Worker 26*c2e18aaaSAndroid Build Coastguard Worker# When combining 3 paths in a single try block, it's hard for the coverage 27*c2e18aaaSAndroid Build Coastguard Worker# counting algorithm to judge each real path clearly. So, separating them 28*c2e18aaaSAndroid Build Coastguard Worker# into its own try block will increase the coverage. 29*c2e18aaaSAndroid Build Coastguard Worker 30*c2e18aaaSAndroid Build Coastguard Worker# Original code as follows, 31*c2e18aaaSAndroid Build Coastguard Worker# try: 32*c2e18aaaSAndroid Build Coastguard Worker# from atest.metrics import metrics 33*c2e18aaaSAndroid Build Coastguard Worker# from atest.metrics import metrics_base 34*c2e18aaaSAndroid Build Coastguard Worker# from atest.metrics import metrics_utils 35*c2e18aaaSAndroid Build Coastguard Worker# except ImportError: 36*c2e18aaaSAndroid Build Coastguard Worker# logging.debug('Import metrics fail, can\'t send metrics.') 37*c2e18aaaSAndroid Build Coastguard Worker# metrics = None 38*c2e18aaaSAndroid Build Coastguard Worker# metrics_base = None 39*c2e18aaaSAndroid Build Coastguard Worker# metrics_utils = None 40*c2e18aaaSAndroid Build Coastguard Workertry: 41*c2e18aaaSAndroid Build Coastguard Worker from atest.metrics import metrics 42*c2e18aaaSAndroid Build Coastguard Workerexcept ImportError: 43*c2e18aaaSAndroid Build Coastguard Worker logging.debug('Import metrics fail, can\'t send metrics.') 44*c2e18aaaSAndroid Build Coastguard Worker metrics = None 45*c2e18aaaSAndroid Build Coastguard Worker 46*c2e18aaaSAndroid Build Coastguard Workertry: 47*c2e18aaaSAndroid Build Coastguard Worker from atest.metrics import metrics_base 48*c2e18aaaSAndroid Build Coastguard Workerexcept ImportError: 49*c2e18aaaSAndroid Build Coastguard Worker logging.debug('Import metrics fail, can\'t send metrics.') 50*c2e18aaaSAndroid Build Coastguard Worker metrics_base = None 51*c2e18aaaSAndroid Build Coastguard Worker 52*c2e18aaaSAndroid Build Coastguard Workertry: 53*c2e18aaaSAndroid Build Coastguard Worker from atest.metrics import metrics_utils 54*c2e18aaaSAndroid Build Coastguard Workerexcept ImportError: 55*c2e18aaaSAndroid Build Coastguard Worker logging.debug('Import metrics fail, can\'t send metrics.') 56*c2e18aaaSAndroid Build Coastguard Worker metrics_utils = None 57*c2e18aaaSAndroid Build Coastguard Worker 58*c2e18aaaSAndroid Build Coastguard Worker 59*c2e18aaaSAndroid Build Coastguard Workerdef starts_asuite_metrics(references): 60*c2e18aaaSAndroid Build Coastguard Worker """Starts to record metrics data. 61*c2e18aaaSAndroid Build Coastguard Worker 62*c2e18aaaSAndroid Build Coastguard Worker Send a metrics data to log server at the same time. 63*c2e18aaaSAndroid Build Coastguard Worker 64*c2e18aaaSAndroid Build Coastguard Worker Args: 65*c2e18aaaSAndroid Build Coastguard Worker references: a list of reference data, when importing whole Android 66*c2e18aaaSAndroid Build Coastguard Worker it contains 'is_android_tree'. 67*c2e18aaaSAndroid Build Coastguard Worker """ 68*c2e18aaaSAndroid Build Coastguard Worker if not metrics: 69*c2e18aaaSAndroid Build Coastguard Worker return 70*c2e18aaaSAndroid Build Coastguard Worker metrics_utils.print_data_collection_notice() 71*c2e18aaaSAndroid Build Coastguard Worker metrics_base.MetricsBase.tool_name = constant.AIDEGEN_TOOL_NAME 72*c2e18aaaSAndroid Build Coastguard Worker metrics_utils.get_start_time() 73*c2e18aaaSAndroid Build Coastguard Worker command = ' '.join(sys.argv) 74*c2e18aaaSAndroid Build Coastguard Worker metrics.AtestStartEvent( 75*c2e18aaaSAndroid Build Coastguard Worker command_line=command, 76*c2e18aaaSAndroid Build Coastguard Worker test_references=references, 77*c2e18aaaSAndroid Build Coastguard Worker cwd=os.getcwd(), 78*c2e18aaaSAndroid Build Coastguard Worker os=platform.platform()) 79*c2e18aaaSAndroid Build Coastguard Worker 80*c2e18aaaSAndroid Build Coastguard Worker 81*c2e18aaaSAndroid Build Coastguard Workerdef ends_asuite_metrics(exit_code, stacktrace='', logs=''): 82*c2e18aaaSAndroid Build Coastguard Worker """Send the end event to log server. 83*c2e18aaaSAndroid Build Coastguard Worker 84*c2e18aaaSAndroid Build Coastguard Worker Args: 85*c2e18aaaSAndroid Build Coastguard Worker exit_code: An integer of exit code. 86*c2e18aaaSAndroid Build Coastguard Worker stacktrace: A string of stacktrace. 87*c2e18aaaSAndroid Build Coastguard Worker logs: A string of logs. 88*c2e18aaaSAndroid Build Coastguard Worker 89*c2e18aaaSAndroid Build Coastguard Worker Returns: 90*c2e18aaaSAndroid Build Coastguard Worker Boolean: False if metrics_utils does not exist. 91*c2e18aaaSAndroid Build Coastguard Worker True when successfully send metrics. 92*c2e18aaaSAndroid Build Coastguard Worker """ 93*c2e18aaaSAndroid Build Coastguard Worker if not metrics_utils: 94*c2e18aaaSAndroid Build Coastguard Worker return False 95*c2e18aaaSAndroid Build Coastguard Worker metrics_utils.send_exit_event( 96*c2e18aaaSAndroid Build Coastguard Worker exit_code, 97*c2e18aaaSAndroid Build Coastguard Worker stacktrace=stacktrace, 98*c2e18aaaSAndroid Build Coastguard Worker logs=logs) 99*c2e18aaaSAndroid Build Coastguard Worker return True 100*c2e18aaaSAndroid Build Coastguard Worker 101*c2e18aaaSAndroid Build Coastguard Worker 102*c2e18aaaSAndroid Build Coastguard Workerdef send_exception_metrics(exit_code, stack_trace, log, err_msg): 103*c2e18aaaSAndroid Build Coastguard Worker """Sends exception metrics. 104*c2e18aaaSAndroid Build Coastguard Worker 105*c2e18aaaSAndroid Build Coastguard Worker For recording the exception metrics, this function is going to be called. 106*c2e18aaaSAndroid Build Coastguard Worker It is different to ends_asuite_metrics function, which will be called every 107*c2e18aaaSAndroid Build Coastguard Worker time AIDEGen process is finished. 108*c2e18aaaSAndroid Build Coastguard Worker The steps you need to do to call this function: 109*c2e18aaaSAndroid Build Coastguard Worker 1. Create an exit code in constants.py. 110*c2e18aaaSAndroid Build Coastguard Worker 2. Generate the stack trace info and the log for debugging. 111*c2e18aaaSAndroid Build Coastguard Worker 3. Show the warning message to users. 112*c2e18aaaSAndroid Build Coastguard Worker 113*c2e18aaaSAndroid Build Coastguard Worker Args: 114*c2e18aaaSAndroid Build Coastguard Worker exit_code: An integer of exit code. 115*c2e18aaaSAndroid Build Coastguard Worker stack_trace: A string of stacktrace. 116*c2e18aaaSAndroid Build Coastguard Worker log: A string of logs. 117*c2e18aaaSAndroid Build Coastguard Worker err_msg: A string to show warning. 118*c2e18aaaSAndroid Build Coastguard Worker """ 119*c2e18aaaSAndroid Build Coastguard Worker print('\n{} {}\n'.format(common_util.COLORED_INFO('Warning:'), err_msg)) 120*c2e18aaaSAndroid Build Coastguard Worker stack_trace = common_util.remove_user_home_path(stack_trace) 121*c2e18aaaSAndroid Build Coastguard Worker log = common_util.remove_user_home_path(log) 122*c2e18aaaSAndroid Build Coastguard Worker ends_asuite_metrics(exit_code, stack_trace, log) 123*c2e18aaaSAndroid Build Coastguard Worker 124*c2e18aaaSAndroid Build Coastguard Worker 125*c2e18aaaSAndroid Build Coastguard Workerdef performance_metrics(process_type, duration): 126*c2e18aaaSAndroid Build Coastguard Worker """ Records each process runtime and send it to clearcut. 127*c2e18aaaSAndroid Build Coastguard Worker 128*c2e18aaaSAndroid Build Coastguard Worker Args: 129*c2e18aaaSAndroid Build Coastguard Worker process_type: An integer of process type. 130*c2e18aaaSAndroid Build Coastguard Worker duration: Runtime for a specific process. 131*c2e18aaaSAndroid Build Coastguard Worker 132*c2e18aaaSAndroid Build Coastguard Worker Returns: 133*c2e18aaaSAndroid Build Coastguard Worker Boolean: False if metrics does not exist. 134*c2e18aaaSAndroid Build Coastguard Worker True when successfully send metrics. 135*c2e18aaaSAndroid Build Coastguard Worker """ 136*c2e18aaaSAndroid Build Coastguard Worker if not metrics: 137*c2e18aaaSAndroid Build Coastguard Worker return False 138*c2e18aaaSAndroid Build Coastguard Worker 139*c2e18aaaSAndroid Build Coastguard Worker metrics.LocalDetectEvent( 140*c2e18aaaSAndroid Build Coastguard Worker detect_type=process_type, 141*c2e18aaaSAndroid Build Coastguard Worker result=int(duration) 142*c2e18aaaSAndroid Build Coastguard Worker ) 143*c2e18aaaSAndroid Build Coastguard Worker return True 144