e97a4f71a009c52f18dea26a1da3350e3b474466
2 """ A simple tool to run jobs from a database in parallel."""
4 __author__
= "Stefan Huber"
5 __copyright__
= "Copyright 2013"
11 import sys
, getopt
, os
17 def printStatusInfo(conn
):
20 c
.execute("SELECT count(id) FROM jobs;")
21 nototal
, = c
.fetchone()
23 c
.execute("SELECT count(id) FROM jobs WHERE done=1;")
24 nodone
, = c
.fetchone()
26 c
.execute("SELECT sum(workloadestm) FROM jobs WHERE done=1;")
27 wldone
, = c
.fetchone()
31 c
.execute("SELECT sum(workloadestm) FROM jobs;")
32 wltotal
, = c
.fetchone()
36 print(nototal
, nodone
, wldone
, wltotal
)
37 perdone
= 100.0*float(nodone
)/float(nototal
)
38 perwl
= 100.0*float(wldone
)/float(wltotal
)
40 print("%d (%.1f%%) of %d jobs done. %.1f%% of the workload finished." % \
41 (nodone
, perdone
, nototal
, perwl
))
43 def createPropertiesTable(conn
, propdef
):
44 conn
.execute("BEGIN EXCLUSIVE")
47 c
.execute("SELECT count(name) FROM sqlite_master WHERE name='properties';")
48 if c
.fetchone() == (0,):
49 print("Creating properties table.")
50 sqlstmt
= "CREATE TABLE properties (\
51 jobid INTEGER PRIMARY KEY,\
53 FOREIGN KEY (jobid) REFERENCES jobs (id));" % (propdef
,)
59 proc
= subprocess
.Popen(cmd
, \
60 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
, shell
=True)
61 out
, err
= proc
.communicate()
62 exitcode
= proc
.wait()
67 return exitcode
, out
, err
69 def processJob(conn
, jobid
):
70 print("Process job %d" % (jobid
))
73 c
.execute("SELECT cmd FROM jobs WHERE id=?", (jobid
,))
76 ec
, out
, err
= runCmd(cmd
)
77 c
.execute("UPDATE jobs SET exitcode=?, done=1 WHERE id=?;", (ec
, jobid
))
80 for l
in out
.splitlines():
81 if l
.startswith("DB-PROPERTIES:"):
83 for l
in err
.splitlines():
84 if l
.startswith("DB-PROPERTIES:"):
90 for k
, v
in p
.iteritems():
94 collist
= ", ".join([str(k
) for k
in prop
.keys()])
95 collist
= "jobid, " + collist
97 vallist
= ", ".join(["?" for k
in prop
.keys()])
98 vallist
= "?, " + vallist
101 sqlstmt
= "INSERT INTO properties (%s) VALUES (%s);" % (collist
,vallist
)
102 c
.execute(sqlstmt
, [jobid
] + list(prop
.values()))
107 def insertJobs(conn
, cmds
):
108 conn
.execute("BEGIN EXCLUSIVE")
109 conn
.executemany("INSERT INTO jobs (cmd) VALUES (?);", cmds
)
112 def createSchema(conn
):
115 c
.execute("BEGIN EXCLUSIVE")
117 # Create table, if necessary
118 c
.execute("SELECT count(name) FROM sqlite_master WHERE name='jobs';")
119 if c
.fetchone() == (0,):
120 print("Creating jobs table.")
121 conn
.execute("CREATE TABLE jobs ( \
122 id INTEGER PRIMARY KEY AUTOINCREMENT, \
123 cmd STRING NOT NULL, \
124 started BOOL DEFAULT (0) NOT NULL, \
125 done BOOL DEFAULT (0) NOT NULL, \
127 workloadestm REAL DEFAULT (1) NOT NULL)")
131 def getNextJobId(conn
):
134 c
.execute("BEGIN EXCLUSIVE")
135 c
.execute("SELECT id FROM jobs WHERE NOT started=1 LIMIT 1;")
142 conn
.execute("UPDATE jobs SET started=1 WHERE id=?;", (jobid
,))
152 """Print usage text of this program"""
155 Take the jobs defined in jobs table of the given database and process one job
156 after the other. Multiple instances may be launched against the same database.
159 {0} [OPTIONS] [COMMANDS] -d database
163 -c cmdfn add jobs from the file with list of commands
165 -s print status information
166 -w work on the database
169 -d database the database to process
170 -p cols-def create properties table with SQL column spec
171 -v print output of the job's command
173 Commands may be combined in one call of {0}.
175 A list of jobs may be importet line-by-line from a file using the -c option.
176 Every job may output to stdout or stderr a string of the form
177 DB-PROPERTIES: {{ "key": "value", "key2": 1.23, "key3": True }}
178 It is assumed that a table 'properties' exists with the columns jobid, key,
179 key2, and key3. The corresponding values are inserted into this table. Using
180 the option -p such a properties table can be created by giving a list of
181 column definitions in SQL style.
183 The jobs table also contains a 'workloadestm' column that is used when
184 estimating the finished workload so far. The entries default to 1 and may be
188 # create cmds.sh with jobs
189 echo "ulimit -v 2000000 -t 1200; ./isprime 65535" > cmds.sh
190 echo "ulimit -v 2000000 -t 1200; ./isprime 65537" >> cmds.sh
191 # create an initial database, but do not work
192 {0} -d jobs.db -c cmds.sh \\
193 -p 'number INTEGER, time REAL, mem INTEGER'
199 """.format(sys
.argv
[0]))
202 if __name__
== "__main__":
212 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hd:c:p:wsv")
214 for opt
, arg
in opts
:
231 print("Unknown option '", opt
, "'.")
233 except getopt
.GetoptError
as e
:
234 print("Error parsing arguments:", e
)
236 sys
.exit(os
.EX_USAGE
)
239 print("No database given.")
240 sys
.exit(os
.EX_USAGE
)
242 conn
= sqlite3
.connect(dbfn
)
246 printStatusInfo(conn
)
249 createPropertiesTable(conn
, propdef
)
252 print("Adding jobs...")
253 cmds
= open(cmdfn
).readlines()
254 cmds
= [(c
.strip(),) for c
in cmds
]
255 insertJobs(conn
, cmds
)
259 jobid
= getNextJobId(conn
)
261 print("All jobs have been started.")
263 processJob(conn
, jobid
)