Skip to content

Commit 9b2451b

Browse files
authored
Merge pull request #40 from liyancn/master
add ls_info api functions and examples
2 parents 2ecc74a + 2f8a5ab commit 9b2451b

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

lsinfo.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from pythonlsf import lsf
2+
import sys
3+
4+
def printLsInfo(argList):
5+
if lsf.lsb_init("test") > 0:
6+
return -1;
7+
8+
valueList=["Boolean","Numeric","String","Dynamic","External"]
9+
orderList=["Inc","Dec","N/A"]
10+
allInfo = lsf.ls_info_py()
11+
print("Current cluster has {} resources in total.".format(allInfo["nRes"]))
12+
print("Current cluster has {} types in total.".format(allInfo["nTypes"]))
13+
print("Current cluster has {} models in total.".format(allInfo["nModels"]))
14+
resTable = allInfo["resTable"]
15+
16+
matchList = []
17+
unMatchList = []
18+
showAll = 0
19+
mFlag = 0
20+
mmFlag = 0
21+
rFlag = 0
22+
tFlag = 0
23+
resFound = 0
24+
if "-m" in argList:
25+
mFlag = 1
26+
if "-M" in argList:
27+
mFlag = 1
28+
mmFlag = 1
29+
if "-r" in argList:
30+
rFlag = 1
31+
if "-t" in argList:
32+
tFlag = 1
33+
34+
if len(argList) > 0:
35+
for target in argList:
36+
if target[0] != "-":
37+
resFound = 0
38+
for i in range(len(resTable)):
39+
if resTable[i].name == target :
40+
matchList.append(i)
41+
resFound = 1
42+
break
43+
if resFound == 0:
44+
unMatchList.append(target)
45+
46+
if len(argList) == 0 and len(unMatchList) == 0:
47+
showAll = 1
48+
49+
if (showAll == 1 or rFlag > 0 or len(matchList) > 0 or len(unMatchList) > 0) :
50+
print("ESOURCE_NAME TYPE ORDER DESCRIPTION")
51+
if len(matchList) == 0 and len(unMatchList) == 0:
52+
for i in range(len(resTable)):
53+
print("{} {} {} {}".format(resTable[i].name, valueList[resTable[i].valueType], orderList[resTable[i].orderType], resTable[i].des))
54+
55+
else:
56+
for i in range(len(resTable)):
57+
if i in matchList :
58+
print("{} {} {} {}".format(resTable[i].name, valueList[resTable[i].valueType], orderList[resTable[i].orderType], resTable[i].des))
59+
for target in unMatchList :
60+
print("{}: resource name not found.".format(target))
61+
if (showAll == 1 or tFlag > 0):
62+
hostTypes = allInfo["hostTypes"]
63+
print("TYPE_NAME")
64+
for i in range(len(hostTypes)):
65+
print("{}".format(hostTypes[i]))
66+
if mFlag > 0 :
67+
hostModels = allInfo["hostModels"]
68+
hostArchs = allInfo["hostArchs"]
69+
modelRefs = allInfo["modelRefs"]
70+
cpuFactor = allInfo["cpuFactor"]
71+
print("MODEL_NAME CPU_FACTOR ARCHITECTURE")
72+
for i in range(allInfo["nModels"]):
73+
if (mmFlag > 0 or modelRefs[i] > 0):
74+
print("{} {} {}".format(hostModels[i],cpuFactor[i],hostArchs[i]))
75+
if (showAll == 0 and len(matchList) == 0 and mFlag == 0 and mmFlag == 0 and rFlag == 0 and tFlag == 0):
76+
print("No match resource found.")
77+
78+
79+
return 0
80+
81+
if __name__ == '__main__':
82+
print("LSF Clustername is : {}".format(lsf.ls_getclustername()))
83+
argList = []
84+
if len(sys.argv) > 1 :
85+
for i in range(1,len(sys.argv)):
86+
argList.append(sys.argv[i])
87+
printLsInfo(argList)
88+

pythonlsf/lsf.i

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,111 @@ PyObject * ls_load_py(char *resreq, int *numhosts, int options, char *fromhost)
463463
return result;
464464
}
465465

466+
PyObject * ls_info_py() {
467+
struct resItem * allRes = NULL;
468+
struct lsInfo * allInfo = NULL;
469+
char *type = NULL;
470+
char *model = NULL;
471+
char *arch = NULL;
472+
473+
int i = 0, j = 0;
474+
475+
allInfo = ls_info();
476+
if (allInfo == NULL) {
477+
ls_perror("ls_info_py");
478+
exit(-1);
479+
}
480+
481+
PyObject * result = PyDict_New();
482+
PyObject * nRes = Py_BuildValue("i",allInfo->nRes);
483+
PyDict_SetItemString(result, "nRes",nRes);
484+
PyObject * nTypes = Py_BuildValue("i", allInfo->nTypes);
485+
PyDict_SetItemString(result, "nTypes", nTypes);
486+
PyObject * nModels = Py_BuildValue("i", allInfo->nModels);
487+
PyDict_SetItemString(result, "nModels", nModels);
488+
PyObject * numIndx = Py_BuildValue("i", allInfo->numIndx);
489+
PyDict_SetItemString(result, "numIndx", numIndx);
490+
PyObject * numUsrIndx = Py_BuildValue("i", allInfo->numUsrIndx);
491+
PyDict_SetItemString(result, "numUsrIndx", numUsrIndx);
492+
493+
allRes = allInfo->resTable;
494+
for (i = 0; i < allInfo->nRes; i++) {
495+
int size_string = sizeof(allRes[i].name);
496+
int len_string = strlen(allRes[i].name);
497+
for (j = len_string; j < size_string; j++) {
498+
allRes[i].name[j] = 0;
499+
}
500+
size_string = sizeof(allRes[i].des);
501+
len_string = strlen(allRes[i].des);
502+
for (j = len_string; j < size_string; j++) {
503+
allRes[i].des[j] = 0;
504+
}
505+
}
506+
507+
PyObject * resRst = PyList_New(allInfo->nRes);
508+
for (i = 0; i < allInfo->nRes; i++) {
509+
PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(&allRes[i]),
510+
SWIGTYPE_p_resItem, 0 | 0);
511+
PyList_SetItem(resRst,i,o);
512+
}
513+
PyDict_SetItemString(result, "resTable", resRst);
514+
515+
PyObject * typeRst = PyList_New(allInfo->nTypes);
516+
for (i = 0; i < allInfo->nTypes; i++) {
517+
type = strdup(allInfo->hostTypes[i]);
518+
int size_string = sizeof(type);
519+
int len_string = strlen(type);
520+
for (j = len_string; j < size_string; j++) {
521+
type[j] = 0;
522+
}
523+
PyObject * pyType = Py_BuildValue("s",type);
524+
PyList_SetItem(typeRst,i,pyType);
525+
if (type != NULL) {
526+
free(type);
527+
}
528+
}
529+
PyDict_SetItemString(result, "hostTypes", typeRst);
530+
531+
PyObject * modelRst = PyList_New(allInfo->nModels);
532+
PyObject * archRst = PyList_New(allInfo->nModels);
533+
PyObject * refRst = PyList_New(allInfo->nModels);
534+
PyObject * factorRst = PyList_New(allInfo->nModels);
535+
536+
for (i = 0; i < allInfo->nModels; i++) {
537+
model = strdup(allInfo->hostModels[i]);
538+
int size_string = sizeof(model);
539+
int len_string = strlen(model);
540+
for (j = len_string; j < size_string; j++) {
541+
model[j] = 0;
542+
}
543+
PyObject *pyModel = Py_BuildValue("s",model);
544+
PyList_SetItem(modelRst,i,pyModel);
545+
546+
arch = strdup(allInfo->hostArchs[i]);
547+
size_string = sizeof(arch);
548+
len_string = strlen(arch);
549+
for (j = len_string; j < size_string; j++) {
550+
arch[j] = 0;
551+
}
552+
PyObject *pyArch = Py_BuildValue("s",arch);
553+
PyList_SetItem(archRst,i,pyArch);
554+
555+
PyObject *pyRef = Py_BuildValue("i",allInfo->modelRefs[i]);
556+
PyList_SetItem(refRst,i,pyRef);
557+
558+
PyObject *pyFactor = Py_BuildValue("f",allInfo->cpuFactor[i]);
559+
PyList_SetItem(factorRst,i,pyFactor);
560+
}
561+
562+
PyDict_SetItemString(result,"hostModels",modelRst);
563+
PyDict_SetItemString(result,"hostArchs",archRst);
564+
PyDict_SetItemString(result,"modelRefs",refRst);
565+
PyDict_SetItemString(result,"cpuFactor",factorRst);
566+
567+
return result;
568+
}
466569

570+
467571
PyObject * get_queue_info_by_name(char** name, int num) {
468572
struct queueInfoEnt* queueinfo;
469573
int numqueues = num;

0 commit comments

Comments
 (0)