# # Collective Knowledge (description of a given machine for crowd-benchmarking and crowd-tuning) # # See CK LICENSE.txt for licensing details # See CK COPYRIGHT.txt for copyright details # # Developer: Grigori Fursin, Grigori.Fursin@cTuning.org, http://fursin.net # cfg={} # Will be updated by CK (meta description of this module) work={} # Will be updated by CK (temporal data) ck=None # Will be updated by CK (initialized CK kernel) # Local settings line='***************************************************************************************' ############################################################################## # Initialize module def init(i): """ Input: {} Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ return {'return':0} ############################################################################## # add new machine description def add(i): """ Input: { (data_uoa) or (alias) - force name of CK entry to store description of this machine (if empty, suggest automatically) (host_os) - host OS (detect, if omitted) (target_os) - OS module to check (if omitted, analyze host) (device_id) - device id if remote (such as adb) (use_host) - if 'yes', configure host as target (type) or (access_type) - access type to the machine: "android" "mingw" "wa_android" "wa_linux" "ck_node" "ssh" "rpc" "avro" "cluster" "quantum" Extra options per access type: * avro: avro_config - full path to JSON configuration file * cluster: cluster_config - full path to JSON configuration file or config_file * quantum: access_key - full path to access key file if needed - will be available via env CK_QCK_ACCESS_KEY_FILE user_login - user login if needed - will be available via env CK_QCK_ACCESS_LOGIN user_password - user password if needed - will be available via env CK_QCK_ACCESS_PASSWORD (share) - if 'yes', share public info about platform with the community via cknowledge.org/repo } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ import copy import os # Setting output o=i.get('out','') oo='' if o=='con': oo='con' # Params hos=i.get('host_os','') tos=i.get('target_os', '') tdid=i.get('device_id', '') at=i.get('access_type','') if at=='': at=i.get('type','') if i.get('use_host','')=='yes': at='host' duoa=i.get('data_uoa','') if duoa=='': duoa=i.get('alias','') exc='no' if i.get('share',''): exc='yes' er=i.get('exchange_repo','') esr=i.get('exchange_subrepo','') dp='' # detect platform # Preliminary host detect os_tags=[] r=ck.get_os_ck({}) if r['return']>0: return r if r['platform']=='win': os_tags=["windows","mingw"] else: os_tags=["linux"] # If not host target tat=cfg['target_access_types'] if at=='' and o=='con': ck.out(line) r=ck.select({'title':'Select access type for your target machine: ', 'dict':tat}) if r['return']>0: return r at=r['string'] # If access_type is empty, quit if at=='': return {'return':1, 'error':'access type is not specified'} # Continue processing tags=[] extra_check={} extra_deps={} prefix='' rtags='' if at!='': tags=tat[at]['tags'] dp=tat[at]['detect_platform'] extra_check=tat[at].get('extra_check',{}) extra_deps=tat[at].get('extra_deps',{}) prefix=tat[at].get('alias_prefix','') rtags=tat[at].get('record_tags','') dos='' # Check if use target OS as host OS (useful for Quantum) if tat[at].get('use_target_os_as_host','')=='yes': ii={'action':'detect', 'module_uoa':cfg['module_deps']['platform.os'], 'host_os':hos} r=ck.access(ii) if r['return']>0: return r tos=r['os_uoa'] # Extra checks if tos=='': if 'android-with-arch' in tags: # Check preliminary Android parameters including arch + API to finalize detecton of the CK OS description if o=='con': ck.out(line) ck.out('Attempting to detect Android API and arch ...') ck.out('') ii={'action':'detect', 'host_os':hos, 'target_os':'android-32', 'module_uoa':cfg['module_deps']['platform.cpu'], 'out':oo} r=ck.access(ii) if r['return']>0: return r tdid=r['device_id'] params=r['features']['os_misc'].get('adb_params',{}) sdk=str(params.get('ro.build.version.sdk','')) abi=str(params.get('ro.product.cpu.abi','')) if sdk!='': if o=='con': ck.out('') ck.out('Android API: '+sdk) for q in range(0, len(tags)): tags[q]+=',android-'+sdk if abi!='': if o=='con': ck.out('') ck.out('Android ABI: '+abi) if abi.startswith('arm64'): dos='*-arm64' elif abi.startswith('arm'): dos='*-arm' elif abi.startswith('x86_64'): dos='*-x86_64' elif abi.startswith('x86'): dos='*-x86' elif abi.startswith('mips64'): dos='*-mips64' elif abi.startswith('mips'): dos='*-mips' # Check tags and use host ones if empty if len(tags)==0 and len(os_tags)>0: tags=os_tags # Search target OS if tos=='': lst=[] for t in tags: ii={'action':'search', 'module_uoa':cfg['module_deps']['os'], 'data_uoa':dos, 'tags':t} r=ck.access(ii) if r['return']>0: return r for q in r['lst']: lst.append(q) if len(lst)==0: return {'return':1, 'error':'no OS found for tags "'+str(tags)+'" and OS wildcard "'+dos+'"'} elif len(lst)==1: tos=lst[0]['data_uoa'] else: ck.out(line) ck.out('Select most close OS and architecture on a target machine:') ck.out('') r=ck.select_uoa({'choices':lst}) if r['return']>0: return r tos=r['choice'] # Target OS should be finalized if tos=='': return {'return':1, 'error':'no target OS selected'} # Extra deps if len(extra_deps)>0: ck.out(line) r=ck.access({'action':'resolve', 'module_uoa':cfg['module_deps']['env'], 'deps':extra_deps, 'host_os':hos, 'target_os':tos, 'device_id':tdid, 'out':oo}) if r['return']>0: return r # Get user friend alias of OS if tos!='': r=ck.access({'action':'load', 'module_uoa':cfg['module_deps']['os'], 'data_uoa':tos}) if r['return']>0: return r tos=r['data_uoa'] tosd=r['dict'] if o=='con': if tos!='': ck.out(line) ck.out('Selected target OS UOA: '+tos) if tdid!='': ck.out('Selected target device ID: '+tdid) # Extra checks if needed dd={} files={} if len(extra_check)>0: ii=copy.deepcopy(extra_check) ii['out']=oo ii['device_id']=tdid r=ck.access(ii) if r['return']>0: return r files=r.get('files',{}) dd['device_cfg']={'wa_config':r.get('cfg',{})} ########################################################## Additional questions for SSH or CK or RPC if at=='ssh' or at=='ck_node' or at=='rpc': if 'device_cfg' not in dd: dd['device_cfg']={} dx={} henv={} keyfile='' ##################### if at=='rpc': ##################### ck.out('') r=ck.inp({'text':'Enter hostname (Enter for localhost): '}) host=r['string'].strip() if host=='': host='localhost' dx['host']=host ##################### ck.out('') r=ck.inp({'text':'Enter host port if needed: '}) port=r['string'].strip() dx['port']=port elif at=='ssh': ##################### ck.out('') r=ck.inp({'text':'Enter hostname (Enter for localhost): '}) host=r['string'].strip() if host=='': host='localhost' dx['host']=host ##################### ck.out('') r=ck.inp({'text':'Enter host port if needed: '}) port=r['string'].strip() dx['port']=port ##################### ck.out('') r=ck.inp({'text':'Enter username (Enter for root): '}) username=r['string'].strip() if username=='': username='root' dx['username']=username ##################### ck.out('') r=ck.inp({'text':'Enter full path to public keyfile: '}) keyfile=r['string'].strip() dx['keyfile']=keyfile else: ##################### ck.out('') r=ck.inp({'text':'Enter hostname (Enter for localhost): '}) host=r['string'].strip() if host=='': host='http://localhost' elif not host.startswith('http://'): host='http://'+host dx['host']=host ##################### ck.out('') r=ck.inp({'text':'Enter host port if needed (Enter for 3333): '}) port=r['string'].strip() if port=='': port='3333' dx['port']=port ##################### ck.out('') r=ck.inp({'text':'Enter full local path to file with secret key: '}) keyfile=r['string'].strip() dx['keyfile']=keyfile henv['CK_MACHINE_HOST']=dx.get('host','') henv['CK_MACHINE_PORT']=dx.get('port','') ##################### path_to_files="tmp-ck" if at=='ck_node': ck.out('') r=ck.inp({'text':'Enter full path to files on a target: '}) path_to_files=r['string'].strip() dx['path_to_files']=path_to_files dd['device_cfg']['remote_params']=dx # Prepare OS update port1='' if port!='': port1=':'+port keyfile1='' if at=='rpc': uod={ 'preset_host_env':henv, 'skip_platform_detection':'yes' } else: uod={ "remote": "yes", "remote_ssh":"yes", "remote_deinit": "", "remote_dir_sep": tosd.get('dir_sep',''), "remote_env_quotes_if_space": tosd.get('env_quotes_if_space',''), "remote_shell_end": "\"" } if at=='ssh': port2='' port3='' if port!='': port2='-p '+port port3='-P '+port if keyfile!='': keyfile1='-i '+keyfile uod.update({ "remote_dir": path_to_files, "remote_id": username+"@"+host+port1, "remote_init": "ssh "+port2+" -l "+username+" "+host+" "+keyfile1+" \"echo remote_init...\"", "remote_pull": "scp "+port3+" "+keyfile1+" "+username+"@"+host+":$#file1#$ \"$#file2#$\"", "remote_push": "scp "+port3+" "+keyfile1+" \"$#file1#$\" "+username+"@"+host+":$#file2#$", "remote_shell": "ssh "+port2+" -l "+username+" "+host+" "+keyfile1+" \"" }) else: if keyfile!='': keyfile1='--keyfile='+keyfile uod.update({ "remote_dir": "", "remote_dir_full": path_to_files, "remote_id": host+port1, "remote_init": "ck shell crowdnode --url="+host+port1+" "+keyfile1+" --cmd=\"echo remote_init...\"", "remote_pull": "ck pull crowdnode --url="+host+port1+" "+keyfile1+" --filename=$#file1#$ --filename2=\"$#file2#$\"", "remote_push": "ck push crowdnode --url="+host+port1+" "+keyfile1+" --filename=$#file1#$ --filename2=\"$#file2#$\"", "remote_shell": "ck shell crowdnode --url="+host+port1+" "+keyfile1+" --cmd=\"" }) tosd.update(uod) dd['device_cfg']['update_target_os_dict']=uod ########################################################## Additional questions for Apache AVRO distributed platforms if at=='avro': if 'device_cfg' not in dd: dd['device_cfg']={} dx={} henv={} ##################### avro_config=i.get('avro_config','') if avro_config=='': ck.out('') r=ck.inp({'text':'Enter path to AVRO JSON file with configuration of all nodes: '}) avro_config=r['string'].strip() # Check if exists if not os.path.isfile(avro_config): return {'return':1, 'error':'AVRO configuration file not found ('+avro_config+')'} # Add to files to save to machine dir r=ck.load_json_file({'json_file':avro_config}) if r['return']>0: return r dx['avro_config']=r['dict'] # henv['CK_MACHINE_HOST']=dx.get('host','') r=ck.load_text_file({'text_file':avro_config}) if r['return']>0: return r files['ip']=r['string'] dd['device_cfg']['remote_params']=dx uod={ 'preset_host_env':henv, 'skip_platform_detection':'yes' } tosd.update(uod) dd['device_cfg']['update_target_os_dict']=uod ########################################################## Additional questions for any CLUSTER platform if at=='cluster': if 'device_cfg' not in dd: dd['device_cfg']={} dx={} henv={} ##################### cluster_config=i.get('cluster_config','') if cluster_config=='': cluster_config=i.get('config_file','') if cluster_config=='': ck.out('') r=ck.inp({'text':'Enter path to CLUSTER JSON file with configuration of all nodes: '}) cluster_config=r['string'].strip() # Check if exists if not os.path.isfile(cluster_config): return {'return':1, 'error':'CLUSTER configuration file not found ('+cluster_config+')'} # Add to files to save to machine dir r=ck.load_json_file({'json_file':cluster_config}) if r['return']>0: return r dx['cluster_config']=r['dict'] # henv['CK_MACHINE_HOST']=dx.get('host','') r=ck.load_text_file({'text_file':cluster_config}) if r['return']>0: return r files['ip']=r['string'] dd['device_cfg']['remote_params']=dx uod={ 'preset_host_env':henv, 'skip_platform_detection':'yes' } tosd.update(uod) dd['device_cfg']['update_target_os_dict']=uod ########################################################## Additional questions for Quantum platforms if at=='quantum': if 'device_cfg' not in dd: dd['device_cfg']={} dx={} henv={} ##################### access_key=i.get('access_key','') if access_key=='': ck.out('') r=ck.inp({'text':'Input full path to an access key file for your remote Quantum machine or press Enter if it\'s not used: '}) access_key=r['string'].strip() # Check if exists if access_key!='': if not os.path.isfile(access_key): return {'return':1, 'error':'Access key file was not found ('+access_key+')'} dx['qck_access_key_file']=access_key henv['CK_QCK_ACCESS_KEY_FILE']=access_key ##################### user_login=i.get('user_login','') if user_login=='': ck.out('') r=ck.inp({'text':'Input user login name or press Enter if it\'s not used: '}) user_login=r['string'].strip() if user_login!='': dx['qck_access_login']=user_login henv['CK_QCK_ACCESS_LOGIN']=user_login ##################### user_password=i.get('user_password','') if user_password=='': ck.out('') r=ck.inp({'text':'Input user password or press Enter if it\'s not used (note that it will be stored in the CK "machine" entry in a plain text): '}) user_password=r['string'].strip() if user_password!='': dx['qck_access_password']=user_password henv['CK_QCK_ACCESS_PASSWORD']=user_password # Set vars used in program module dd['device_cfg']['remote_params']=dx uod={ 'preset_host_env':henv, 'skip_platform_detection':'yes' } tosd.update(uod) dd['device_cfg']['update_target_os_dict']=uod # Detect various parameters of the platform (to suggest platform name as well) pn='' rp={} if dp=='yes': if o=='con': ck.out(line) ck.out('Attempting to detect various parameters of your target machine ...') ck.out('') ii={'action':'detect', 'host_os':hos, 'target_os':tos, 'target_os_dict':tosd, 'device_id':tdid, 'module_uoa':cfg['module_deps']['platform'], 'exchange':exc, 'exchange_repo':er, 'exchange_subrepo':esr, 'out':oo} rp=ck.access(ii) if rp['return']>0: return rp tdid=rp.get('device_id','') pn=rp.get('features',{}).get('platform',{}).get('name','') # Check if has CPU features for device config features=rp.get('features',{}) xwa=features.get('cpu',{}).get('workload_automation',{}).get('device_config',{}) if len(xwa)>0: if 'device_cfg' not in dd: dd['device_cfg']={} if 'wa_config' not in dd['device_cfg']: dd['device_cfg']['wa_config']={} if 'device_config' not in dd['device_cfg']['wa_config']: dd['device_cfg']['wa_config']['device_config']={} dd['device_cfg']['wa_config']['device_config'].update(xwa) # Finalize machine meta dd.update({'host_os':hos, 'target_os':tos, 'device_id':tdid, 'host_os_uoa':rp.get('host_os_uoa',''), 'host_os_uid':rp.get('host_os_uid',''), 'host_os_dict':rp.get('host_os_dict',{}), 'target_os_uoa':rp.get('os_uoa',''), 'target_os_uid':rp.get('os_uid',''), 'target_os_dict':tosd, 'access_type':at, 'features':features}) # Suggest platform name if duoa=='': if o=='con': ck.out(line) if pn!='' and o=='con': ck.out('Detected target machine name: '+pn) ck.out('') if at=='host': duoa='host' elif pn!='': duoa=pn.lower().replace(' ','-').replace('_','-').replace('(','-').replace(')','-').replace('"','-') if duoa!='' and prefix!='': duoa=prefix+duoa if o=='con': s='Enter an alias for your machine to be recorded in your CK local repo' if duoa!='': s+=' or press Enter for "'+duoa+'"' s+=' : ' ck.out('') r=ck.inp({'text':s}) x=r['string'].strip() if x!='': duoa=x # Check that alias is there if duoa=='': return {'return':1, 'error':'machine alias is not defined'} # Check if entry already exists ii={'action':'load', 'module_uoa':work['self_module_uid'], 'data_uoa':duoa} r=ck.access(ii) if r['return']>0 and r['return']!=16: return r if r['return']==0: renew=False s='CK entry "machine:'+duoa+'" already exists' if o=='con': ck.out('') r=ck.inp({'text':s+'. Renew (Y/n)? '}) x=r['string'].strip().lower() if x=='' or x=='y' or x=='yes': renew=True else: return {'return':0} if not renew: return {'return':1, 'error':s} ii={'action':'update', 'module_uoa':work['self_module_uid'], 'data_uoa':duoa, 'dict':dd, 'tags':rtags, 'substitute':'yes', 'sort_keys':'yes'} r=ck.access(ii) if r['return']>0: return r duoa=r['data_uoa'] duid=r['data_uid'] p=r['path'] if len(files)>0: for f in files: xcfg=files[f] pp=os.path.join(p,f) r=ck.save_text_file({'text_file':pp, 'string':xcfg}) if r['return']>0: return r # Success if o=='con': ck.out(line) ck.out('Your target machine was successfully registered in CK with alias: '+duoa+' ('+duid+')') return {'return':0} ############################################################################## # show registered target machines and their status def show(i): """ Input: { } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ o=i.get('out','') if o=='con': ck.out(line) ck.out('Checking machines ...') ck.out('') h='
CK alias | \n' h+='Real machine name | \n' h+='Native ID | \n' h+='Target OS name | \n' h+='OS UOA | \n' h+='CPUs | \n' h+='GPUs | \n' # h+='GPGPUs | \n' h+='Status | \n' h+='
'+duoa+' | \n' h+=''+rn+' | \n' h+=''+tdid+' | \n' h+=''+on+' | \n' h+=''+tos+' | \n' h+=''+cpus+' | \n' h+=''+gpu+' | \n' # h+=''+gpgpus+' | \n' c+=sx h+=''+sx+' | \n' h+='