Admin/mira.py
author blanchet
Fri, 25 Jul 2014 11:26:10 +0200
changeset 59010 09d2b853b20c
parent 58901 841f41710066
permissions -rw-r--r--
tuning
     1 """
     2     Test configuration descriptions for mira.
     3 """
     4 
     5 import os
     6 from os import path
     7 from glob import glob
     8 import subprocess
     9 from datetime import datetime
    10 import re
    11 
    12 import util
    13 
    14 from mira import schedule, misc
    15 from mira.environment import scheduler
    16 from mira import repositories
    17 
    18 # build and evaluation tools
    19 
    20 DEFAULT_TIMEOUT = 2 * 60 * 60
    21 SMLNJ_TIMEOUT = 72 * 60 * 60
    22 
    23 def prepare_isabelle_repository(loc_isabelle, loc_dependency_heaps, more_settings=''):
    24 
    25     # patch settings
    26     extra_settings = '''
    27 Z3_NON_COMMERCIAL="yes"
    28 
    29 init_components "/home/isabelle/contrib" "$ISABELLE_HOME/Admin/components/main"
    30 init_components "/home/isabelle/contrib" "$ISABELLE_HOME/Admin/components/optional"
    31 init_components "/home/isabelle/contrib" "$ISABELLE_HOME/Admin/components/nonfree"
    32 
    33 ''' + more_settings
    34 
    35     writer = open(path.join(loc_isabelle, 'etc', 'settings'), 'a')
    36     writer.write(extra_settings)
    37     writer.close()
    38 
    39 
    40 def isabelle_getenv(isabelle_home, var):
    41 
    42     _, out = env.run_process('%s/bin/isabelle' % isabelle_home, 'getenv', var)
    43     return out.split('=', 1)[1].strip()
    44 
    45 
    46 def extract_isabelle_run_timing(logdata):
    47 
    48     def to_secs(h, m, s):
    49         return (int(h) * 60 + int(m)) * 60 + int(s)
    50     pat = r'Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time'
    51     pat2 = r'Timing (\S+) \((\d+) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time, factor (\d+\.\d+)\)'
    52     t = dict((name, {'elapsed': to_secs(eh,em,es), 'cpu': to_secs(ch,cm,cs)})
    53              for name, eh, em, es, ch, cm, cs in re.findall(pat, logdata))
    54     for name, threads, elapsed, cpu, gc, factor in re.findall(pat2, logdata):
    55 
    56         if name not in t:
    57             t[name] = {}
    58 
    59         t[name]['threads'] = int(threads)
    60         t[name]['elapsed_inner'] = elapsed
    61         t[name]['cpu_inner'] = cpu
    62         t[name]['gc'] = gc
    63         t[name]['factor'] = factor
    64 
    65     return t
    66 
    67 
    68 def extract_isabelle_run_summary(logdata):
    69 
    70     re_error = re.compile(r'^(?:make: )?\*\*\* (.*)$', re.MULTILINE)
    71     summary = '\n'.join(re_error.findall(logdata))
    72     if summary == '':
    73         summary = 'ok'
    74 
    75     return summary
    76 
    77 
    78 def extract_image_size(isabelle_home):
    79     
    80     isabelle_output = isabelle_getenv(isabelle_home, 'ISABELLE_OUTPUT')
    81     if not path.exists(isabelle_output):
    82         return {}
    83 
    84     return dict((p, path.getsize(path.join(isabelle_output, p))) for p in os.listdir(isabelle_output) if p != "log")
    85 
    86 def extract_report_data(isabelle_home, logdata):
    87 
    88     return {
    89         'timing': extract_isabelle_run_timing(logdata),
    90         'image_size': extract_image_size(isabelle_home) }
    91 
    92 
    93 def isabelle_build(env, case, paths, dep_paths, playground, *cmdargs, **kwargs):
    94 
    95     more_settings = kwargs.get('more_settings', '')
    96     keep_results = kwargs.get('keep_results', True)
    97     timeout = kwargs.get('timeout', DEFAULT_TIMEOUT)
    98 
    99     isabelle_home = paths[0]
   100 
   101     home_user_dir = path.join(isabelle_home, 'home_user')
   102     os.makedirs(home_user_dir)
   103 
   104     # copy over build results from dependencies
   105     heap_dir = path.join(isabelle_home, 'heaps')
   106     classes_dir = path.join(heap_dir, 'classes')
   107     os.makedirs(classes_dir)
   108 
   109     for dep_path in dep_paths:
   110         subprocess.check_call(['cp', '-a'] + glob(dep_path + '/*') + [heap_dir])
   111 
   112     subprocess.check_call(['ln', '-s', classes_dir, path.join(isabelle_home, 'lib', 'classes')])
   113     jars = glob(path.join(classes_dir, 'ext', '*.jar'))
   114     if jars:
   115         subprocess.check_call(['touch'] + jars)
   116 
   117     # misc preparations
   118     if 'lxbroy10' in misc.hostnames():  # special settings for lxbroy10
   119         more_settings += '''
   120 ISABELLE_GHC="/usr/bin/ghc"
   121 '''
   122 
   123     prepare_isabelle_repository(isabelle_home, None, more_settings=more_settings)
   124     os.chdir(isabelle_home)
   125 
   126     args = (['-o', 'timeout=%s' % timeout] if timeout is not None else []) + list(cmdargs)
   127 
   128     # invoke build tool
   129     (return_code, log1) = env.run_process('%s/bin/isabelle' % isabelle_home, 'jedit', '-bf',
   130             USER_HOME=home_user_dir)
   131     (return_code, log2) = env.run_process('%s/bin/isabelle' % isabelle_home, 'build', '-s', '-v', *args,
   132             USER_HOME=home_user_dir)
   133     log = log1 + log2
   134 
   135     # collect report
   136     return (return_code == 0, extract_isabelle_run_summary(log),
   137       extract_report_data(isabelle_home, log), {'log': log}, heap_dir if keep_results else None)
   138 
   139 
   140 
   141 # Isabelle configurations
   142 
   143 @configuration(repos = [Isabelle], deps = [])
   144 def Pure(*args):
   145     """Pure Image"""
   146     return isabelle_build(*(args + ("-b", "Pure")))
   147 
   148 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   149 def HOL(*args):
   150     """HOL Image"""
   151     return isabelle_build(*(args + ("-b", "HOL")))
   152 
   153 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   154 def HOL_Library(*args):
   155     """HOL Library"""
   156     return isabelle_build(*(args + ("-b", "HOL-Library")))
   157 
   158 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   159 def HOLCF(*args):
   160     """HOLCF"""
   161     return isabelle_build(*(args + ("-b", "HOLCF")))
   162 
   163 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   164 def ZF(*args):
   165     """HOLCF"""
   166     return isabelle_build(*(args + ("-b", "ZF")))
   167 
   168 
   169 settings64='''
   170 # enforce 64 bit, overriding smart detection
   171 ML_PLATFORM="$ISABELLE_PLATFORM64"
   172 ML_HOME="$(dirname $ML_HOME)/$ML_PLATFORM"
   173 '''
   174 
   175 @configuration(repos = [Isabelle], deps = [])
   176 def Isabelle_makeall(*args):
   177     """Build all sessions"""
   178     return isabelle_build(*(args + ("-j", "6", "-o", "threads=4", "-a")), more_settings=settings64, keep_results=False)
   179 
   180 
   181 # Mutabelle configurations
   182 
   183 def invoke_mutabelle(theory, env, case, paths, dep_paths, playground):
   184 
   185     """Mutant testing for counterexample generators in Isabelle"""
   186 
   187     (loc_isabelle,) = paths
   188     (dep_isabelle,) = dep_paths
   189     more_settings = '''
   190 ISABELLE_GHC="/usr/bin/ghc"
   191 '''
   192     prepare_isabelle_repository(loc_isabelle, dep_isabelle, more_settings = more_settings)
   193     os.chdir(loc_isabelle)
   194     
   195     (return_code, log) = env.run_process('bin/isabelle',
   196       'mutabelle', '-O', playground, theory)
   197     
   198     try:
   199         mutabelle_log = util.readfile(path.join(playground, 'log'))
   200     except IOError:
   201         mutabelle_log = ''
   202 
   203     mutabelle_data = dict(
   204         (tool, {'counterexample': c, 'no_counterexample': n, 'timeout': t, 'error': e})
   205         for tool, c, n, t, e in re.findall(r'(\S+)\s+: C: (\d+) N: (\d+) T: (\d+) E: (\d+)', log))
   206 
   207     return (return_code == 0 and mutabelle_log != '', extract_isabelle_run_summary(log),
   208       {'mutabelle_results': {theory: mutabelle_data}},
   209       {'log': log, 'mutabelle_log': mutabelle_log}, None)
   210 
   211 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   212 def Mutabelle_Relation(*args):
   213     """Mutabelle regression suite on Relation theory"""
   214     return invoke_mutabelle('Relation', *args)
   215 
   216 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   217 def Mutabelle_List(*args):
   218     """Mutabelle regression suite on List theory"""
   219     return invoke_mutabelle('List', *args)
   220 
   221 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   222 def Mutabelle_Set(*args):
   223     """Mutabelle regression suite on Set theory"""
   224     return invoke_mutabelle('Set', *args)
   225 
   226 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   227 def Mutabelle_Map(*args):
   228     """Mutabelle regression suite on Map theory"""
   229     return invoke_mutabelle('Map', *args)
   230 
   231 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   232 def Mutabelle_Divides(*args):
   233     """Mutabelle regression suite on Divides theory"""
   234     return invoke_mutabelle('Divides', *args)
   235 
   236 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   237 def Mutabelle_MacLaurin(*args):
   238     """Mutabelle regression suite on MacLaurin theory"""
   239     return invoke_mutabelle('MacLaurin', *args)
   240 
   241 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   242 def Mutabelle_Fun(*args):
   243     """Mutabelle regression suite on Fun theory"""
   244     return invoke_mutabelle('Fun', *args)
   245 
   246 mutabelle_confs = 'Mutabelle_Relation Mutabelle_List Mutabelle_Set Mutabelle_Map Mutabelle_Divides Mutabelle_MacLaurin Mutabelle_Fun'.split(' ')
   247 
   248 @scheduler()
   249 def mutabelle_scheduler(env):
   250     """Scheduler for Mutabelle."""
   251     return schedule.age_scheduler(env, 'Isabelle', mutabelle_confs)
   252 
   253 
   254 # Judgement Day configurations
   255 
   256 judgement_day_provers = ('e', 'spass', 'vampire', 'z3', 'cvc3', 'yices')
   257 
   258 def judgement_day(base_path, theory, opts, env, case, paths, dep_paths, playground):
   259     """Judgement Day regression suite"""
   260 
   261     isa = paths[0]
   262     dep_path = dep_paths[0]
   263 
   264     os.chdir(path.join(playground, '..', base_path)) # Mirabelle requires specific cwd
   265     prepare_isabelle_repository(isa, dep_path)
   266 
   267     output = {}
   268     success_rates = {}
   269     some_success = False
   270 
   271     for atp in judgement_day_provers:
   272 
   273         log_dir = path.join(playground, 'mirabelle_log_' + atp)
   274         os.makedirs(log_dir)
   275 
   276         cmd = ('%s/bin/isabelle mirabelle -q -O %s sledgehammer[prover=%s,%s] %s.thy'
   277                % (isa, log_dir, atp, opts, theory))
   278 
   279         os.system(cmd)
   280         output[atp] = util.readfile(path.join(log_dir, theory + '.log'))
   281 
   282         percentages = list(re.findall(r'Success rate: (\d+)%', output[atp]))
   283         if len(percentages) == 2:
   284             success_rates[atp] = {
   285                 'sledgehammer': int(percentages[0]),
   286                 'metis': int(percentages[1])}
   287             if success_rates[atp]['sledgehammer'] > 0:
   288                 some_success = True
   289         else:
   290             success_rates[atp] = {}
   291 
   292 
   293     data = {'success_rates': success_rates}
   294     raw_attachments = dict((atp + "_output", output[atp]) for atp in judgement_day_provers)
   295     # FIXME: summary?
   296     return (some_success, '', data, raw_attachments, None)
   297 
   298 
   299 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   300 def JD_NS(*args):
   301     """Judgement Day regression suite NS"""
   302     return judgement_day('Isabelle/src/HOL/Auth', 'NS_Shared', 'prover_timeout=10', *args)
   303 
   304 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   305 def JD_FTA(*args):
   306     """Judgement Day regression suite FTA"""
   307     return judgement_day('Isabelle/src/HOL/Library', 'Fundamental_Theorem_Algebra', 'prover_timeout=10', *args)
   308 
   309 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   310 def JD_Hoare(*args):
   311     """Judgement Day regression suite Hoare"""
   312     return judgement_day('Isabelle/src/HOL/IMPP', 'Hoare', 'prover_timeout=10', *args)
   313 
   314 @configuration(repos = [Isabelle], deps = [(HOL, [0])])
   315 def JD_SN(*args):
   316     """Judgement Day regression suite SN"""
   317     return judgement_day('Isabelle/src/HOL/Proofs/Lambda', 'StrongNorm', 'prover_timeout=10', *args)
   318 
   319 
   320 JD_confs = 'JD_NS JD_FTA JD_Hoare JD_SN JD_Arrow JD_FFT JD_Jinja JD_QE JD_S2S'.split(' ')
   321 
   322 @scheduler()
   323 def judgement_day_scheduler(env):
   324     """Scheduler for Judgement Day."""
   325     return schedule.age_scheduler(env, 'Isabelle', JD_confs)
   326 
   327 
   328 # SML/NJ
   329 
   330 smlnj_settings = '''
   331 ML_SYSTEM=smlnj
   332 ML_HOME="/home/smlnj/110.76/bin"
   333 ML_OPTIONS="@SMLdebug=/dev/null @SMLalloc=1024"
   334 ML_PLATFORM=$(eval $("$ML_HOME/.arch-n-opsys" 2>/dev/null); echo "$HEAP_SUFFIX")
   335 '''
   336 
   337 @configuration(repos = [Isabelle], deps = [(Pure, [0])])
   338 def SML_HOL(*args):
   339     """HOL image built with SML/NJ"""
   340     return isabelle_build(*(args + ("-b", "HOL")), more_settings=smlnj_settings, timeout=SMLNJ_TIMEOUT)
   341 
   342 @configuration(repos = [Isabelle], deps = [])
   343 def SML_makeall(*args):
   344     """SML/NJ build of all possible sessions"""
   345     return isabelle_build(*(args + ("-j", "3", "-a")), more_settings=smlnj_settings, timeout=SMLNJ_TIMEOUT)
   346 
   347