From 85bd40334e15feae804c81a579228ce43e6a371c Mon Sep 17 00:00:00 2001 From: liyancn <93751519+liyancn@users.noreply.github.com> Date: Fri, 5 Nov 2021 10:59:53 +0800 Subject: [PATCH 1/3] support for retrieve data correctly ls_info() api --- pythonlsf/lsf.i | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/pythonlsf/lsf.i b/pythonlsf/lsf.i index d9442aa..08ea4d4 100755 --- a/pythonlsf/lsf.i +++ b/pythonlsf/lsf.i @@ -463,7 +463,140 @@ PyObject * ls_load_py(char *resreq, int *numhosts, int options, char *fromhost) return result; } +PyObject * ls_load_py(char *resreq, int *numhosts, int options, char *fromhost) { + + struct hostLoad * hosts = NULL; + int i = 0, j = 0; + + hosts = ls_load(resreq, numhosts, options, fromhost); + + if (hosts == NULL) { + ls_perror("ls_load_py"); + exit(-1); + } + + /* Python3 can not handle dirty string.*/ + for (i = 0; i < *numhosts; i++) { + int size_string = sizeof(hosts[i].hostName); + int len_string = strlen(hosts[i].hostName); + for ( j = len_string; j < size_string; j++) hosts[i].hostName[j] = 0; + } + + PyObject * result = PyList_New(*numhosts); + for (i = 0; i < *numhosts; i++) { + PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(&hosts[i]), + SWIGTYPE_p_hostLoad, 0 | 0 ); + PyList_SetItem(result,i,o); + } + + return result; +} + +PyObject * ls_info_py() { + struct resItem * allRes = NULL; + struct lsInfo * allInfo = NULL; + char *type = NULL; + char *model = NULL; + char *arch = NULL; + + int i = 0, j = 0; + + allInfo = ls_info(); + if (allInfo == NULL) { + ls_perror("ls_info_py"); + exit(-1); + } + + PyObject * result = PyDict_New(); + PyObject * nRes = Py_BuildValue("i",allInfo->nRes); + PyDict_SetItemString(result, "nRes",nRes); + PyObject * nTypes = Py_BuildValue("i", allInfo->nTypes); + PyDict_SetItemString(result, "nTypes", nTypes); + PyObject * nModels = Py_BuildValue("i", allInfo->nModels); + PyDict_SetItemString(result, "nModels", nModels); + PyObject * numIndx = Py_BuildValue("i", allInfo->numIndx); + PyDict_SetItemString(result, "numIndx", numIndx); + PyObject * numUsrIndx = Py_BuildValue("i", allInfo->numUsrIndx); + PyDict_SetItemString(result, "numUsrIndx", numUsrIndx); + + allRes = allInfo->resTable; + for (i = 0; i < allInfo->nRes; i++) { + int size_string = sizeof(allRes[i].name); + int len_string = strlen(allRes[i].name); + for (j = len_string; j < size_string; j++) { + allRes[i].name[j] = 0; + } + size_string = sizeof(allRes[i].des); + len_string = strlen(allRes[i].des); + for (j = len_string; j < size_string; j++) { + allRes[i].des[j] = 0; + } + } + + PyObject * resRst = PyList_New(allInfo->nRes); + for (i = 0; i < allInfo->nRes; i++) { + PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(&allRes[i]), + SWIGTYPE_p_resItem, 0 | 0); + PyList_SetItem(resRst,i,o); + } + PyDict_SetItemString(result, "resTable", resRst); + + PyObject * typeRst = PyList_New(allInfo->nTypes); + for (i = 0; i < allInfo->nTypes; i++) { + type = strdup(allInfo->hostTypes[i]); + int size_string = sizeof(type); + int len_string = strlen(type); + for (j = len_string; j < size_string; j++) { + type[j] = 0; + } + PyObject * pyType = Py_BuildValue("s",type); + PyList_SetItem(typeRst,i,pyType); + if (type != NULL) { + free(type); + } + } + PyDict_SetItemString(result, "hostTypes", typeRst); + + PyObject * modelRst = PyList_New(allInfo->nModels); + PyObject * archRst = PyList_New(allInfo->nModels); + PyObject * refRst = PyList_New(allInfo->nModels); + PyObject * factorRst = PyList_New(allInfo->nModels); + + for (i = 0; i < allInfo->nModels; i++) { + model = strdup(allInfo->hostModels[i]); + int size_string = sizeof(model); + int len_string = strlen(model); + for (j = len_string; j < size_string; j++) { + model[j] = 0; + } + PyObject *pyModel = Py_BuildValue("s",model); + PyList_SetItem(modelRst,i,pyModel); + + arch = strdup(allInfo->hostArchs[i]); + size_string = sizeof(arch); + len_string = strlen(arch); + for (j = len_string; j < size_string; j++) { + arch[j] = 0; + } + PyObject *pyArch = Py_BuildValue("s",arch); + PyList_SetItem(archRst,i,pyArch); + + PyObject *pyRef = Py_BuildValue("i",allInfo->modelRefs[i]); + PyList_SetItem(refRst,i,pyRef); + + PyObject *pyFactor = Py_BuildValue("f",allInfo->cpuFactor[i]); + PyList_SetItem(factorRst,i,pyFactor); + } + + PyDict_SetItemString(result,"hostModels",modelRst); + PyDict_SetItemString(result,"hostArchs",archRst); + PyDict_SetItemString(result,"modelRefs",refRst); + PyDict_SetItemString(result,"cpuFactor",factorRst); + + return result; +} + PyObject * get_queue_info_by_name(char** name, int num) { struct queueInfoEnt* queueinfo; int numqueues = num; From c1148598bef939dc78ae920ef10435de8d68b573 Mon Sep 17 00:00:00 2001 From: liyancn <93751519+liyancn@users.noreply.github.com> Date: Fri, 5 Nov 2021 11:02:02 +0800 Subject: [PATCH 2/3] Update lsf.i --- pythonlsf/lsf.i | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/pythonlsf/lsf.i b/pythonlsf/lsf.i index 08ea4d4..e6f7d81 100755 --- a/pythonlsf/lsf.i +++ b/pythonlsf/lsf.i @@ -434,35 +434,6 @@ PyObject * get_host_load_from_batch ( } -PyObject * ls_load_py(char *resreq, int *numhosts, int options, char *fromhost) { - - struct hostLoad * hosts = NULL; - int i = 0, j = 0; - - hosts = ls_load(resreq, numhosts, options, fromhost); - - if (hosts == NULL) { - ls_perror("ls_load_py"); - exit(-1); - } - - /* Python3 can not handle dirty string.*/ - for (i = 0; i < *numhosts; i++) { - int size_string = sizeof(hosts[i].hostName); - int len_string = strlen(hosts[i].hostName); - for ( j = len_string; j < size_string; j++) hosts[i].hostName[j] = 0; - } - - PyObject * result = PyList_New(*numhosts); - for (i = 0; i < *numhosts; i++) { - PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(&hosts[i]), - SWIGTYPE_p_hostLoad, 0 | 0 ); - PyList_SetItem(result,i,o); - } - - return result; -} - PyObject * ls_load_py(char *resreq, int *numhosts, int options, char *fromhost) { struct hostLoad * hosts = NULL; From 2f8a5ab978d2c6f8ac1a721c845079515ee953d1 Mon Sep 17 00:00:00 2001 From: liyancn <93751519+liyancn@users.noreply.github.com> Date: Fri, 5 Nov 2021 11:07:22 +0800 Subject: [PATCH 3/3] example of get info from ls_info() --- lsinfo.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lsinfo.py diff --git a/lsinfo.py b/lsinfo.py new file mode 100644 index 0000000..58df362 --- /dev/null +++ b/lsinfo.py @@ -0,0 +1,88 @@ +from pythonlsf import lsf +import sys + +def printLsInfo(argList): + if lsf.lsb_init("test") > 0: + return -1; + + valueList=["Boolean","Numeric","String","Dynamic","External"] + orderList=["Inc","Dec","N/A"] + allInfo = lsf.ls_info_py() + print("Current cluster has {} resources in total.".format(allInfo["nRes"])) + print("Current cluster has {} types in total.".format(allInfo["nTypes"])) + print("Current cluster has {} models in total.".format(allInfo["nModels"])) + resTable = allInfo["resTable"] + + matchList = [] + unMatchList = [] + showAll = 0 + mFlag = 0 + mmFlag = 0 + rFlag = 0 + tFlag = 0 + resFound = 0 + if "-m" in argList: + mFlag = 1 + if "-M" in argList: + mFlag = 1 + mmFlag = 1 + if "-r" in argList: + rFlag = 1 + if "-t" in argList: + tFlag = 1 + + if len(argList) > 0: + for target in argList: + if target[0] != "-": + resFound = 0 + for i in range(len(resTable)): + if resTable[i].name == target : + matchList.append(i) + resFound = 1 + break + if resFound == 0: + unMatchList.append(target) + + if len(argList) == 0 and len(unMatchList) == 0: + showAll = 1 + + if (showAll == 1 or rFlag > 0 or len(matchList) > 0 or len(unMatchList) > 0) : + print("ESOURCE_NAME TYPE ORDER DESCRIPTION") + if len(matchList) == 0 and len(unMatchList) == 0: + for i in range(len(resTable)): + print("{} {} {} {}".format(resTable[i].name, valueList[resTable[i].valueType], orderList[resTable[i].orderType], resTable[i].des)) + + else: + for i in range(len(resTable)): + if i in matchList : + print("{} {} {} {}".format(resTable[i].name, valueList[resTable[i].valueType], orderList[resTable[i].orderType], resTable[i].des)) + for target in unMatchList : + print("{}: resource name not found.".format(target)) + if (showAll == 1 or tFlag > 0): + hostTypes = allInfo["hostTypes"] + print("TYPE_NAME") + for i in range(len(hostTypes)): + print("{}".format(hostTypes[i])) + if mFlag > 0 : + hostModels = allInfo["hostModels"] + hostArchs = allInfo["hostArchs"] + modelRefs = allInfo["modelRefs"] + cpuFactor = allInfo["cpuFactor"] + print("MODEL_NAME CPU_FACTOR ARCHITECTURE") + for i in range(allInfo["nModels"]): + if (mmFlag > 0 or modelRefs[i] > 0): + print("{} {} {}".format(hostModels[i],cpuFactor[i],hostArchs[i])) + if (showAll == 0 and len(matchList) == 0 and mFlag == 0 and mmFlag == 0 and rFlag == 0 and tFlag == 0): + print("No match resource found.") + + + return 0 + +if __name__ == '__main__': + print("LSF Clustername is : {}".format(lsf.ls_getclustername())) + argList = [] + if len(sys.argv) > 1 : + for i in range(1,len(sys.argv)): + argList.append(sys.argv[i]) + printLsInfo(argList) +