Debian Codegraph @

toolbin/tests/run_parallel

raw
1 #!/usr/bin/env python
2
3 # Copyright (C) 2001-2023 Artifex Software, Inc.
4 # All Rights Reserved.
5 #
6 # This software is provided AS-IS with no warranty, either express or
7 # implied.
8 #
9 # This software is distributed under license and may not be copied,
10 # modified or distributed except as expressly authorized under the terms
11 # of the license contained in the file LICENSE in this distribution.
12 #
13 # Refer to licensing information at http://www.artifex.com or contact
14 # Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
15 # CA 94129, USA, for further information.
16 #
17
18 # This is a script to parallelize the regression runs "by hand"
19 # for running on a batch-mode cluster under the PBS queue system.
20 # it generates a custom testing.cfg and comparefiles directory
21 # for each node, creates a set of job description files, and runs them.
22
23 import os
24 import string
25 import re
26 import sys
27 import getopt
28
29 ## globals -- edit these to make it work
30 run=True # whether to submit the job after creating it
31 try:
32 home=os.environ["HOME"]
33 except KeyError:
34 home=''
35 base=os.getcwd()
36 testdir=home+"/tests/ps/ps3fts" # directory of files to run
37 configfile="testing.cfg" # template config file
38 files=os.listdir(testdir) # list of files to run
39
40 ## defaults -- override from the command line
41 action='run'
42 revision=None # revision we're testing, if known
43
44 # parse the command line
45 opts, args = getopt.getopt(sys.argv[1:], "r:", ["rev="])
46 for o, a in opts:
47 if o in ("-r", "--rev"):
48 revision = a
49 print "parallel run for r" + revision
50 if len(args):
51 action = args[0]
52
53 ## helper functions
54
55 def choosecluster():
56 '''Decide how many nodes of which cluster to run on.
57 returns a (cluster_name, node_count) tuple.'''
58
59 # figure out how many nodes are free
60 upnodes = os.popen("upnodes")
61 r = re.compile('^\s+(?P<cluster>\w+).*\s+(?P<procs>\d+)\s+(?P<free>\d+)\s*$')
62 clusters=[]
63 nodes = 0
64 for line in upnodes.readlines():
65 m = r.match(line)
66 if m:
67 name = m.group("cluster")
68 procs = int(m.group("procs"))
69 free = int(m.group("free"))
70 # remember the cluster with the most free nodes
71 if free > nodes and name != 'total':
72 nodes = free
73 cluster = name
74 clusters.append((name,procs,free))
75 return (cluster, nodes)
76
77 def makepbs(filename):
78 '''Make a pbs job description file for a command.'''
79 outfile=open(filename+".pbs","w")
80 if cluster == 'red' and nodes > 1:
81 # upnodes reports dual-core nodes twice
82 outfile.write("#PBS -l nodes=%d:run:%s:ppn=2\n" % (nodes/2,cluster))
83 else:
84 outfile.write("#PBS -l nodes=%d:run:%s\n" % (nodes,cluster))
85 outfile.write("cd %s\n" % base)
86 outfile.write("mpiexec -comm none ./%s\n" % filename)
87 outfile.close()
88
89 def makepbscleanup(configfiles, comparefiledirs, jobid=None):
90 '''Make a pbs job to cleanup after another.
91 Pass in sequences of the files and directories to be removed
92 and the jobid, if any, that the cleanup should run after.'''
93 outfile=open("run_regression_cleanup.pbs","w")
94 outfile.write("#PBS -l nodes=1:run")
95 # run this on nina by default since it's trivial
96 #outfile.write(":nina")
97 if jobid:
98 outfile.write(" -W depend=afterany:%s\n" % jobid)
99 else:
100 outfile.write("\n")
101 outfile.write("cd %s\n" % base)
102 for node in range(nodes):
103 outfile.write("rm -rf " + comparefiledirs[node] + "\n")
104 outfile.write("rm " + configfiles[node] + "\n")
105 outfile.close()
106
107 def makepbsreport(jobid):
108 '''Create a report from the output of a previous job
109 Pass the jobid of the actual regression run so we
110 know which log to parse.'''
111 outfile = open("run_regression_report.pbs","w")
112 outfile.write("#PBS -l nodes=1:run")
113 # run this on nina by default since it's trivial
114 #outfile.write(":nina")
115 # run after the regression job is complete
116 outfile.write(" -W depend=afterany:%s\n" % jobid)
117 outfile.write("cd %s\n" % base)
118 if revision:
119 dest = " >> regression-r%s.log\n" % revision
120 else:
121 dest = " >> regression.%s.log\n" % jobid
122 # hack: we want the stderr output from the regression run, but
123 # the jobid pbs appends to the file isn't quite the same as
124 # what qsub (or qstat) give us.
125 log = "run_regression.pbs.e%s" % string.split(jobid, '.')[0]
126 outfile.write("echo Cluster-based regression report BETA -- may not be accurate" + dest)
127 outfile.write("echo Run split over %d nodes" % nodes + dest)
128 outfile.write("JOBS=`cat %s | egrep ^Ran | wc -l`" % log + dest)
129 outfile.write("echo Run completed $JOBS jobs" + dest)
130 outfile.write("STARTT=`stat -c %Y run_regression.pbs`" + dest)
131 outfile.write("ENDT=`stat -c %Y " + log + "`" + dest)
132 outfile.write("echo elapsed time $(($ENDT - $STARTT)) seconds" + dest)
133 outfile.write("DIFFS=`cat %s | egrep DIFFER$ | wc -l`" % log + dest)
134 outfile.write("echo Run shows $DIFFS differences" + dest)
135 outfile.write("echo" + dest)
136 outfile.write("cat %s | egrep ^Checking | sort" % log + dest)
137 outfile.write("cat %s | grep 'relevant files'" % log + dest)
138 outfile.close()
139
140 ## create a config files from the template for each node
141 (cluster, nodes) = choosecluster()
142 print "choosing %s with %d cpus free" % (cluster, nodes)
143
144 configfiles=[]
145 comparefiledirs=[]
146 print "configuring job..."
147 for node in range(nodes):
148 infile=open(configfile)
149 outfilename=configfile +"."+ str(node)
150 outfile=open(outfilename,"w")
151 # remember the filename for later cleanup
152 configfiles.append(outfilename)
153 for line in infile.readlines():
154 try:
155 key,value=string.split(line)
156 if key == "comparefiledir":
157 value=value[:-1]+"."+str(node)+"/"
158 # remember this for cleanup
159 comparefiledirs.append(value)
160 if key == "log_stderr": value=value[:-4]+"."+str(node)+".log"
161 if key == "log_stdout": value=value[:-4]+"."+str(node)+".log"
162 outfile.write(key+"\t"+value+"\n")
163 except ValueError:
164 outfile.write(line)
165 nodedir=home+"/comparefiles."+str(node)
166 # create the per-node directories
167 os.system("rm -rf " + comparefiledirs[node])
168 os.mkdir(comparefiledirs[node])
169
170 # split the test files into directories for each node
171 node=0
172 for file in files:
173 nodedir=comparefiledirs[node]
174 os.system("ln -s %s/%s %s/" % (testdir,file,nodedir))
175 node=node+1
176 if node >= len(comparefiledirs): node=0
177
178 # create our job description files
179 makepbs("run_regression")
180 makepbs("make_testdb")
181
182 ## submit the actual jobs
183 if run:
184 # qsub the pbs file
185
186 if action == 'update':
187 job= os.popen("qsub make_testdb.pbs")
188 else:
189 job = os.popen("qsub run_regression.pbs")
190 jobid = string.strip(job.readline())
191 print "run submitted as", jobid
192
193 # append a follow-up report generation job
194 if action == 'run':
195 makepbsreport(jobid)
196 job = os.popen("qsub run_regression_report.pbs")
197 report_jobid = string.strip(job.readline())
198 print "report job is", report_jobid
199
200 # append a follow-up job to do the cleanup
201 makepbscleanup(configfiles,comparefiledirs,jobid)
202 job = os.popen("qsub run_regression_cleanup.pbs")
203 cleanup_jobid = string.strip(job.readline())
204 print "cleanup job is", cleanup_jobid
205