<s> """<STR_LIT>""" <EOL> import itertools <EOL> import logging <EOL> import os <EOL> import posixpath <EOL> import re <EOL> import tarfile <EOL> from perfkitbenchmarker import configs <EOL> from perfkitbenchmarker import data <EOL> from perfkitbenchmarker import errors <EOL> from perfkitbenchmarker import flags <EOL> from perfkitbenchmarker import sample <EOL> from perfkitbenchmarker import stages <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_enum ( <EOL> '<STR_LIT>' , '<STR_LIT:int>' , [ '<STR_LIT:int>' , '<STR_LIT>' , '<STR_LIT:all>' ] , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( <EOL> '<STR_LIT>' , <NUM_LIT:3> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_boolean ( <EOL> '<STR_LIT>' , False , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_boolean ( <EOL> '<STR_LIT>' , False , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> BENCHMARK_NAME = '<STR_LIT>' <EOL> BENCHMARK_CONFIG = """<STR_LIT>""" <EOL> _BENCHMARK_SPECIFIC_VM_STATE_ATTR = '<STR_LIT>' <EOL> _MOUNT_DIR = '<STR_LIT>' <EOL> _SPECCPU2006_DIR = '<STR_LIT>' <EOL> _SPECCPU2006_ISO = '<STR_LIT>' <EOL> _SPECCPU2006_TAR = '<STR_LIT>' <EOL> _TAR_REQUIRED_MEMBERS = '<STR_LIT>' , '<STR_LIT>' <EOL> def GetConfig ( user_config ) : <EOL> return configs . LoadConfig ( BENCHMARK_CONFIG , user_config , BENCHMARK_NAME ) <EOL> def CheckPrerequisites ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _CheckTarFile ( FLAGS . runspec_config , <EOL> examine_members = stages . PROVISION in FLAGS . run_stage ) <EOL> except data . ResourceNotFound : <EOL> _CheckIsoAndCfgFile ( FLAGS . runspec_config ) <EOL> def _CheckTarFile ( runspec_config , examine_members ) : <EOL> """<STR_LIT>""" <EOL> tar_file_path = data . ResourcePath ( _SPECCPU2006_TAR ) <EOL> logging . info ( '<STR_LIT>' , tar_file_path , <EOL> _SPECCPU2006_ISO ) <EOL> if posixpath . basename ( runspec_config ) != runspec_config : <EOL> raise errors . Config . InvalidValue ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( runspec_config , os . linesep ) ) <EOL> if not examine_members : <EOL> return <EOL> with tarfile . open ( tar_file_path , '<STR_LIT:r>' ) as tf : <EOL> members = tf . getnames ( ) <EOL> cfg_member = '<STR_LIT>' . format ( runspec_config ) <EOL> required_members = itertools . chain ( _TAR_REQUIRED_MEMBERS , [ cfg_member ] ) <EOL> missing_members = set ( required_members ) . difference ( members ) <EOL> if missing_members : <EOL> raise errors . Benchmarks . PrepareException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> linesep = os . linesep , tar = tar_file_path , <EOL> members = os . linesep . join ( sorted ( missing_members ) ) ) ) <EOL> def _CheckIsoAndCfgFile ( runspec_config ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> data . ResourcePath ( _SPECCPU2006_ISO ) <EOL> except data . ResourceNotFound : <EOL> logging . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : _SPECCPU2006_ISO } ) <EOL> raise <EOL> try : <EOL> data . ResourcePath ( runspec_config ) <EOL> except data . ResourceNotFound : <EOL> logging . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , runspec_config ) <EOL> raise <EOL> class _SpecCpu2006SpecificState ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . cfg_file_path = None <EOL> self . iso_file_path = None <EOL> self . mount_dir = None <EOL> self . spec_dir = None <EOL> self . tar_file_path = None <EOL> def Prepare ( benchmark_spec ) : <EOL> """<STR_LIT>""" <EOL> vm = benchmark_spec . vms [ <NUM_LIT:0> ] <EOL> speccpu_vm_state = _SpecCpu2006SpecificState ( ) <EOL> setattr ( vm , _BENCHMARK_SPECIFIC_VM_STATE_ATTR , speccpu_vm_state ) <EOL> vm . Install ( '<STR_LIT>' ) <EOL> vm . Install ( '<STR_LIT>' ) <EOL> vm . Install ( '<STR_LIT>' ) <EOL> if FLAGS . runspec_enable_32bit : <EOL> vm . Install ( '<STR_LIT>' ) <EOL> vm . Install ( '<STR_LIT>' ) <EOL> scratch_dir = vm . GetScratchDir ( ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( scratch_dir ) ) <EOL> speccpu_vm_state . spec_dir = posixpath . join ( scratch_dir , _SPECCPU2006_DIR ) <EOL> try : <EOL> _PrepareWithTarFile ( vm , speccpu_vm_state ) <EOL> except data . ResourceNotFound : <EOL> _PrepareWithIsoFile ( vm , speccpu_vm_state ) <EOL> def _PrepareWithTarFile ( vm , speccpu_vm_state ) : <EOL> """<STR_LIT>""" <EOL> scratch_dir = vm . GetScratchDir ( ) <EOL> local_tar_file_path = data . ResourcePath ( _SPECCPU2006_TAR ) <EOL> speccpu_vm_state . tar_file_path = posixpath . join ( scratch_dir , _SPECCPU2006_TAR ) <EOL> vm . PushFile ( local_tar_file_path , scratch_dir ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( dir = scratch_dir , <EOL> tar = _SPECCPU2006_TAR ) ) <EOL> speccpu_vm_state . cfg_file_path = posixpath . join ( <EOL> speccpu_vm_state . spec_dir , '<STR_LIT>' , FLAGS . runspec_config ) <EOL> def _PrepareWithIsoFile ( vm , speccpu_vm_state ) : <EOL> """<STR_LIT>""" <EOL> scratch_dir = vm . GetScratchDir ( ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( speccpu_vm_state . spec_dir ) ) <EOL> local_iso_file_path = data . ResourcePath ( _SPECCPU2006_ISO ) <EOL> speccpu_vm_state . iso_file_path = posixpath . join ( scratch_dir , _SPECCPU2006_ISO ) <EOL> vm . PushFile ( local_iso_file_path , scratch_dir ) <EOL> speccpu_vm_state . mount_dir = posixpath . join ( scratch_dir , _MOUNT_DIR ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( speccpu_vm_state . mount_dir ) ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( <EOL> speccpu_vm_state . iso_file_path , speccpu_vm_state . mount_dir ) ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( speccpu_vm_state . mount_dir , <EOL> speccpu_vm_state . spec_dir ) ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( speccpu_vm_state . spec_dir ) ) <EOL> local_cfg_file_path = data . ResourcePath ( FLAGS . runspec_config ) <EOL> cfg_file_name = os . path . basename ( local_cfg_file_path ) <EOL> speccpu_vm_state . cfg_file_path = posixpath . join ( <EOL> speccpu_vm_state . spec_dir , '<STR_LIT>' , cfg_file_name ) <EOL> vm . PushFile ( local_cfg_file_path , speccpu_vm_state . cfg_file_path ) <EOL> install_script_path = posixpath . join ( speccpu_vm_state . spec_dir , '<STR_LIT>' ) <EOL> vm . RobustRemoteCommand ( '<STR_LIT>' . format ( install_script_path ) ) <EOL> def _ExtractScore ( stdout , vm , keep_partial_results ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> re_begin_section = re . compile ( '<STR_LIT>' ) <EOL> re_end_section = re . compile ( r'<STR_LIT>' ) <EOL> result_section = [ ] <EOL> in_result_section = False <EOL> for line in stdout . splitlines ( ) : <EOL> if in_result_section : <EOL> result_section . append ( line ) <EOL> match = re . search ( re_begin_section , line ) <EOL> if match : <EOL> assert not in_result_section <EOL> in_result_section = True <EOL> continue <EOL> match = re . search ( re_end_section , line ) <EOL> if match : <EOL> assert in_result_section <EOL> spec_name = str ( match . group ( <NUM_LIT:1> ) ) <EOL> try : <EOL> spec_score = float ( match . group ( <NUM_LIT:2> ) ) <EOL> except ValueError : <EOL> spec_score = None <EOL> in_result_section = False <EOL> result_section . pop ( ) <EOL> metadata = { '<STR_LIT>' : vm . num_cpus } <EOL> metadata . update ( vm . GetMachineTypeDict ( ) ) <EOL> missing_results = [ ] <EOL> for benchmark in result_section : <EOL> if '<STR_LIT>' in benchmark : <EOL> logging . warning ( '<STR_LIT>' , benchmark ) <EOL> missing_results . append ( str ( benchmark . split ( ) [ <NUM_LIT:0> ] ) ) <EOL> continue <EOL> name , _ , _ , score , _ = benchmark . split ( ) <EOL> results . append ( sample . Sample ( str ( name ) , float ( score ) , '<STR_LIT>' , metadata ) ) <EOL> if spec_score is None : <EOL> missing_results . append ( spec_name ) <EOL> if missing_results : <EOL> if keep_partial_results : <EOL> metadata [ '<STR_LIT>' ] = '<STR_LIT:true>' <EOL> metadata [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( missing_results ) <EOL> else : <EOL> raise errors . Benchmarks . RunError ( <EOL> '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( missing_results ) ) <EOL> if spec_score is not None : <EOL> results . append ( sample . Sample ( spec_name , spec_score , '<STR_LIT>' , metadata ) ) <EOL> return results <EOL> def _ParseOutput ( vm , spec_dir ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> log_files = [ ] <EOL> if FLAGS . benchmark_subset in ( '<STR_LIT:int>' , '<STR_LIT:all>' ) : <EOL> log_files . append ( '<STR_LIT>' ) <EOL> if FLAGS . benchmark_subset in ( '<STR_LIT>' , '<STR_LIT:all>' ) : <EOL> log_files . append ( '<STR_LIT>' ) <EOL> for log in log_files : <EOL> stdout , _ = vm . RemoteCommand ( '<STR_LIT>' % ( spec_dir , log ) , <EOL> should_log = True ) <EOL> results . extend ( _ExtractScore ( stdout , vm , <EOL> FLAGS . runspec_keep_partial_results ) ) <EOL> return results <EOL> def Run ( benchmark_spec ) : <EOL> """<STR_LIT>""" <EOL> vm = benchmark_spec . vms [ <NUM_LIT:0> ] <EOL> speccpu_vm_state = getattr ( vm , _BENCHMARK_SPECIFIC_VM_STATE_ATTR ) <EOL> num_cpus = vm . num_cpus <EOL> runspec_flags = [ <EOL> ( '<STR_LIT>' , posixpath . basename ( speccpu_vm_state . cfg_file_path ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:size>' , '<STR_LIT>' ) , ( '<STR_LIT>' , num_cpus ) , <EOL> ( '<STR_LIT>' , FLAGS . runspec_iterations ) ] <EOL> if FLAGS . runspec_define : <EOL> for runspec_define in FLAGS . runspec_define . split ( '<STR_LIT:U+002C>' ) : <EOL> runspec_flags . append ( ( '<STR_LIT>' , runspec_define ) ) <EOL> runspec_cmd = '<STR_LIT>' . format ( <EOL> flags = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' . format ( k , v ) for k , v in runspec_flags ) , <EOL> subset = FLAGS . benchmark_subset ) <EOL> cmd = '<STR_LIT>' . join ( ( <EOL> '<STR_LIT>' . format ( speccpu_vm_state . spec_dir ) , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , runspec_cmd ) ) <EOL> vm . RobustRemoteCommand ( cmd ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> return _ParseOutput ( vm , speccpu_vm_state . spec_dir ) <EOL> def Cleanup ( benchmark_spec ) : <EOL> """<STR_LIT>""" <EOL> vm = benchmark_spec . vms [ <NUM_LIT:0> ] <EOL> speccpu_vm_state = getattr ( vm , _BENCHMARK_SPECIFIC_VM_STATE_ATTR , None ) <EOL> if speccpu_vm_state : <EOL> if speccpu_vm_state . mount_dir : <EOL> try : <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( speccpu_vm_state . mount_dir ) ) <EOL> except errors . VirtualMachine . RemoteCommandError : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> targets = '<STR_LIT:U+0020>' . join ( p for p in speccpu_vm_state . __dict__ . values ( ) if p ) <EOL> vm . RemoteCommand ( '<STR_LIT>' . format ( targets ) ) </s>
