Skip to content

Commit 617af7a

Browse files
author
Yan Li
committed
merge from upstream
2 parents 8d27628 + 7925102 commit 617af7a

File tree

6 files changed

+221
-1
lines changed

6 files changed

+221
-1
lines changed

examples/disp_host.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pythonlsf import lsf
2+
import inspect
3+
4+
if __name__ == '__main__':
5+
if lsf.lsb_init("test") > 0:
6+
exit - 1;
7+
8+
for hostInfoEnt in lsf.get_host_info_all():
9+
attributes = [d for d in dir(hostInfoEnt)
10+
if not d.startswith('__')]
11+
item = {}
12+
for a in attributes:
13+
v = getattr(hostInfoEnt, a)
14+
if not inspect.ismethod(v) and isinstance(v, (int, str, float)):
15+
item[a] = v
16+
print(item)

examples/disp_rsvInfo.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pythonlsf import lsf
2+
3+
def printRsvInfo():
4+
if lsf.lsb_init("test") > 0:
5+
return -1;
6+
7+
intp_nent = lsf.new_intp()
8+
ents = lsf.lsb_reservationinfo(None,intp_nent,0)
9+
nent = lsf.intp_value(intp_nent)
10+
11+
print("{} rsvInfo in the cluster.".format(nent))
12+
13+
for i in range(nent) :
14+
ent = lsf.rsvInfoEntArray_getitem(ents, i)
15+
print('No.{} rsvId : {}'.format(i, ent.rsvId))
16+
print('No.{} name : {}'.format(i, ent.name))
17+
print('No.{} state : {}'.format(i, ent.state))
18+
print('No.{} options : {}'.format(i, ent.options))
19+
print('No.{} timeWindow : {}'.format(i, ent.timeWindow))
20+
print('No.{} numRsvJobs : {}'.format(i, ent.numRsvJobs))
21+
print('No.{} numRsvHosts : {}'.format(i, ent.numRsvHosts))
22+
23+
for j in range(ent.numRsvHosts):
24+
hostInfo_ent = lsf.hostRsvInfoEntArray_getitem(ent.rsvHosts, j)
25+
print(' [{}] host : {}'.format(j, hostInfo_ent.host))
26+
print(' [{}] numCPUs : {}'.format(j, hostInfo_ent.numCPUs))
27+
print(' [{}] numSlots : {}'.format(j, hostInfo_ent.numSlots))
28+
print(' [{}] numRsvProcs : {}'.format(j, hostInfo_ent.numRsvProcs))
29+
print(' [{}] numusedRsvProcs : {}'.format(j, hostInfo_ent.numusedRsvProcs))
30+
print(' [{}] numUsedProcs : {}'.format(j, hostInfo_ent.numUsedProcs))
31+
32+
return 0
33+
34+
if __name__ == '__main__':
35+
print("LSF Clustername is : {}".format(lsf.ls_getclustername()))
36+
printRsvInfo()

examples/job_cputime.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from pythonlsf import lsf
2+
3+
def print_job_cputime(jobid):
4+
5+
if lsf.lsb_init("job_info") > 0:
6+
print("failed to initialise the api")
7+
return
8+
9+
num_jobs_found = lsf.lsb_openjobinfo(jobid, "", "all", "", "", 0x2000)
10+
print('num_jobs_found: {}'.format(num_jobs_found))
11+
12+
if num_jobs_found == -1:
13+
print("no job was found")
14+
return
15+
16+
int_ptr = lsf.new_intp()
17+
lsf.intp_assign(int_ptr, num_jobs_found)
18+
19+
job_info = lsf.lsb_readjobinfo(int_ptr)
20+
21+
lsf.lsb_closejobinfo()
22+
23+
print('jobId: {}'.format(job_info.jobId))
24+
print('jobStatus: {}'.format(job_info.status))
25+
26+
# Get CPU time from run Rusage
27+
time_sum = float(job_info.runRusage.utime) + float(job_info.runRusage.stime)
28+
if time_sum > 0:
29+
print('The CPU time used is: {} seconds'.format(time_sum))
30+
31+
# Get CPU time from host Rusage of each execution host
32+
if job_info.numhRusages > 0:
33+
hRusages = job_info.hostRusage
34+
for i in range(0,job_info.numhRusages):
35+
hRusage = lsf.hRusageArray_getitem(hRusages, i)
36+
cpu_time = float(hRusage.utime) + float(hRusage.stime)
37+
print('HOST: {}, CPU_TIME: {} seconds'.format(hRusage.name, cpu_time))
38+
39+
return
40+
41+
if __name__ == "__main__":
42+
id = input("Enter a running job id:\n")
43+
print_job_cputime(int(id))

examples/job_info.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pythonlsf import lsf
2+
3+
4+
def get_single_job_info(jobid: int) -> lsf.jobInfoEnt:
5+
"""
6+
Get the jobInfoEnt struct for a single lsf job using its jobId
7+
8+
This function does not work for array jobs, only single jobs
9+
"""
10+
if lsf.lsb_init("job_info") > 0:
11+
print("could not initialise the api")
12+
return
13+
14+
# openjobinfo opens a connection to mbatchd and returns the amount of
15+
# jobs in the connection.
16+
num_jobs_found = lsf.lsb_openjobinfo(jobid, "", "all", "", "", 0x2000)
17+
18+
# make and assign an int pointer to the record of the jobs found
19+
int_ptr = lsf.new_intp()
20+
lsf.intp_assign(int_ptr, num_jobs_found)
21+
22+
# read the info at int_ptr and assign to a python object so we can read it.
23+
job_info = lsf.lsb_readjobinfo(int_ptr)
24+
25+
# close the connection to avoid a memory leak
26+
lsf.lsb_closejobinfo()
27+
28+
return job_info
29+
30+
31+
if __name__ == "__main__":
32+
id = input("enter a job id:\n")
33+
job_info = get_single_job_info(int(id))
34+
35+
print(f"job id: {job_info.jobId}\njob name: {job_info.jName}\n"
36+
f"status: {job_info.status:#x}")

pythonlsf/lsf.i

100755100644
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ int fclose(FILE *f);
5050
#endif
5151
%array_functions(LS_LONG_INT, LS_LONG_INTArray)
5252
%array_functions(guaranteedResourcePoolEnt, guaranteedResourcePoolEntArray)
53+
%array_functions(struct rsvInfoEnt, rsvInfoEntArray)
54+
%array_functions(struct hostRsvInfoEnt, hostRsvInfoEntArray)
55+
%array_functions(struct hRusage, hRusageArray)
5356

5457
//helper function for transforming char** to python list
5558
%inline %{
@@ -710,7 +713,7 @@ PyObject * get_pids_from_stream(struct jRusage * jrusage) {
710713
PyList_SetItem(result, i, o);
711714
}
712715
return result;
713-
}
716+
}
714717

715718
long * buildQueryColIndexs() {
716719
long * colIndexs = NULL;
@@ -726,4 +729,23 @@ long * buildQueryColIndexs() {
726729
return colIndexs;
727730
}
728731

732+
PyObject * get_host_info_all() {
733+
struct hostInfoEnt *hostinfo;
734+
char **hosts = NULL;
735+
int numhosts = 0;
736+
737+
// Return queries as C hostInfoEnt*
738+
hostinfo = lsb_hostinfo(hosts, &numhosts);
739+
740+
PyObject *result = PyList_New(numhosts); // Create PyObject * to get C returns
741+
int i;
742+
for (i = 0; i < numhosts; i++) { // Save queries in a loop to result
743+
PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(&hostinfo[i]),
744+
SWIGTYPE_p_hostInfoEnt, 0 | 0 );
745+
PyList_SetItem(result,i,o);
746+
}
747+
748+
return result;
749+
}
750+
729751
%}

read_JOB_FINISH_submitExt.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python3
2+
3+
from pythonlsf import lsf
4+
import sys
5+
6+
# Define function to get key and pair values from the eventrec.eventLog.jobFinishLog.submitExt
7+
def get_pair_from_submit_ext(submit_ext, id):
8+
if submit_ext is None:
9+
return None
10+
11+
12+
values_as_list = lsf.string_array_to_pylist(submit_ext.values, submit_ext.num)
13+
for i in range(submit_ext.num):
14+
key = lsf.intArray_getitem(submit_ext.keys, i)
15+
if key == id:
16+
return values_as_list[i]
17+
18+
return None
19+
20+
21+
def display(eventrec):
22+
"""
23+
display event record, this example to get the user group (-G) value from the JOB_FINISH record
24+
"""
25+
if eventrec.type == lsf.EVENT_JOB_FINISH:
26+
jobid = eventrec.eventLog.jobFinishLog.jobId
27+
fromHost = eventrec.eventLog.jobFinishLog.fromHost
28+
submit_ext=eventrec.eventLog.jobFinishLog.submitExt
29+
userGroup = get_pair_from_submit_ext(submit_ext,lsf.JDATA_EXT_USRGROUP)
30+
# jobgroup = eventrec.eventLog.jobFinishLog.jgroup
31+
# jobgroup = eventrec.eventLog.jobFinishLog.submitExt.values[1075]
32+
print("EVENT_JOB_FINISH jobid<%d>, fromHost<%s>, to jobgroup <%s>" %(jobid, fromHost, userGroup))
33+
else:
34+
print("event type is %d" %(eventrec.type))
35+
36+
def read_eventrec(path):
37+
"""
38+
read lsb.events
39+
"""
40+
lineNum = lsf.new_intp()
41+
lsf.intp_assign(lineNum, 0)
42+
fp = lsf.fopen(path, "r")
43+
if fp is None:
44+
print("The file %s does not exist." % path)
45+
sys.exit(1)
46+
47+
flag = 1
48+
49+
if lsf.lsb_init("test") > 0:
50+
exit(1)
51+
52+
while flag > 0:
53+
log = lsf.lsb_geteventrec(fp, lineNum)
54+
if log:
55+
display(log)
56+
else:
57+
flag = 0
58+
59+
60+
if __name__ == '__main__':
61+
if len(sys.argv) == 1:
62+
print("Usage: %s full_path_lsb.events_file" % (sys.argv[0]))
63+
sys.exit(0)
64+
65+
print("LSF Clustername is :", lsf.ls_getclustername())
66+
#read_eventrec("/opt/lsf8.0.1/work/cluster1/logdir/lsb.events")
67+
read_eventrec(sys.argv[1])

0 commit comments

Comments
 (0)