this patch adds new node list functions in libxcpu, namely:
Xpnodeset *xp_nodeset_list_by_state(char *server, char *state);
Xpnodeset *xp_nodeset_list_by_arch(char *server, char *arch);
Xpnodeset *xp_nodeset_list_by_min_jobs(char *server, int minjobs);
Xpnodeset *xp_nodeset_list_by_max_jobs(char *server, int maxjobs);
a node list can be obtained from statfs by passing one of the conditions
or a combination of them to:
xp_nodeset_listnet_filter(char *server, char *state, char *arch, int
minjobs,int maxjobs)
only those nodes matching this criteria would be returned.
this was needed so that we loop over the nodes just once, instead of
twice to filter the nodes.
Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>
Index: include/libxcpu.h
===================================================================
--- include/libxcpu.h (revision 744)
+++ include/libxcpu.h (working copy)
@@ -151,6 +151,10 @@
Xpnodeset *xp_nodeset_from_string(char *);
char *xp_nodeset_to_string(Xpnodeset *);
Xpnodeset *xp_nodeset_list(char *);
+Xpnodeset *xp_nodeset_list_by_state(char *server, char *state);
+Xpnodeset *xp_nodeset_list_by_arch(char *server, char *arch);
+Xpnodeset *xp_nodeset_list_by_min_jobs(char *server, int minjobs);
+Xpnodeset *xp_nodeset_list_by_max_jobs(char *server, int maxjobs);
int xp_nodeset_add(Xpnodeset *, Xpnode *);
int xp_nodeset_append(Xpnodeset *, Xpnodeset *);
int xp_nodeset_filter_by_node(Xpnodeset *, Xpnodeset *, char *nodelist);
Index: libxcpu/node.c
===================================================================
--- libxcpu/node.c (revision 744)
+++ libxcpu/node.c (working copy)
@@ -201,6 +201,52 @@
}
*/
+
+static char *
+xp_node_create_from_buf(Xpnode **node, char *buf)
+{
+ char *addr, *arch, *status, *eol, *numjobs;
+
+ addr = strchr(buf, '\t');
+ if(addr == NULL) {
+ sp_werror("Bad format in statfs: address", EIO);
+ return NULL;
+ }
+ *addr++ = '\0';
+
+ arch = strchr(addr, '\t');
+ if(arch == NULL) {
+ sp_werror("Bad format in statfs: architecture", EIO);
+ return NULL;
+ }
+ *arch++ = '\0';
+
+ status = strchr(arch, '\t');
+ if(status == NULL) {
+ sp_werror("Bad format in statfs: status", EIO);
+ return NULL;
+ }
+ *status++ = '\0';
+ numjobs = strchr(status, '\t');
+ if(numjobs == NULL) {
+ sp_werror("Bad format in statfs: numjobs", EIO);
+ return NULL;
+ }
+ *numjobs++ = '\0';
+ eol = strchr(numjobs, '\n');
+ if(eol) {
+ *eol++ = '\0';
+ }
+
+ *node = xp_node_create(buf, addr, arch, status, strtoul(numjobs, 0, 0));
+ if(*node == NULL) {
+ sp_werror("Error creating node", ENOMEM);
+ return NULL;
+ }
+ buf = eol;
+ return buf;
+}
+
static int
xp_nodeset_create_node(Xpnodeset *nds, char *name)
{
@@ -545,17 +591,19 @@
}
Xpnodeset *
-xp_nodeset_listfromnet(char *server)
+xp_nodeset_listnet_filter(char *server, char *state, char *arch, int minjobs,
+ int maxjobs)
{
- Spcfsys *fs;
- Spcfid *fid;
+ Spcfsys *fs = NULL;
+ Spcfid *fid = NULL;
Xpnodeset *ns = NULL;
Xpnode *node = NULL;
- char *buf = NULL, *name, *addr, *arch, *status, *eol, *numjobs;
+ char *buf = NULL, *name;
Spuser *user;
int n, bufsize, pos;
+ int add = 1;
- if(server == NULL)
+ if (!server)
return NULL;
user = sp_unix_users->uid2user(sp_unix_users, geteuid());
@@ -596,45 +644,24 @@
buf[pos] = '\0';
name = buf;
+
while(strcmp(name, "")) {
- addr = strchr(name, '\t');
- if(addr == NULL) {
- sp_werror("Bad format in statfs: address", EIO);
+ name = xp_node_create_from_buf(&node, name);
+ if (!name)
goto error;
- }
- *addr++ = '\0';
- arch = strchr(addr, '\t');
- if(arch == NULL) {
- sp_werror("Bad format in statfs: architecture", EIO);
- goto error;
- }
- *arch++ = '\0';
+ add = 1;
+ if (state)
+ add &= (!strcmp(node->status, state));
+ if (arch)
+ add &= (!strcmp(node->arch, arch));
+ if (minjobs >= 0)
+ add &= (node->numjobs >= minjobs);
+ if (maxjobs >= 0)
+ add &= (node->numjobs <= maxjobs);
- status = strchr(arch, '\t');
- if(status == NULL) {
- sp_werror("Bad format in statfs: status", EIO);
- goto error;
- }
- *status++ = '\0';
- numjobs = strchr(status, '\t');
- if(numjobs == NULL) {
- sp_werror("Bad format in statfs: numjobs", EIO);
- goto error;
- }
- *numjobs++ = '\0';
- eol = strchr(numjobs, '\n');
- if(eol) {
- *eol++ = '\0';
- }
-
- node = xp_node_create(name, addr, arch, status, strtoul(numjobs, 0, 0));
- if(node == NULL) {
- sp_werror("Error creating node", ENOMEM);
- goto error;
- }
- xp_nodeset_add(ns, node);
- name = eol;
+ if (add)
+ xp_nodeset_add(ns, node);
}
free(buf);
@@ -655,19 +682,19 @@
return NULL;
}
-
-
Xpnodeset *
-xp_nodeset_list(char *server)
+xp_nodeset_list_filter(char *server, char *state, char *arch, int minjobs,
+ int maxjobs)
{
+ int add;
Xpnodeset *ns = NULL;
Xpnode *n = NULL;
FILE *f;
- char buf[512], *name, *addr, *arch, *status, *numjobs, *eol;
+ char buf[512], *name;
- /* if server is given use that */
- if(server)
- return xp_nodeset_listfromnet(server);
+ if (server)
+ return xp_nodeset_listnet_filter(server, state, arch, minjobs,
+ maxjobs);
f = fopen("/mnt/statfs/state", "r");
if(f == NULL) {
@@ -684,46 +711,26 @@
while(fgets(buf, 511, f) != NULL) {
if(buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';
-
- name = buf;
- addr = strchr(name, '\t');
- if(addr == NULL) {
- sp_werror("Bad format in statfs: address", EIO);
- goto nodeset_list_err;
- }
- *addr++ = '\0';
- arch = strchr(addr, '\t');
- if(arch == NULL) {
- sp_werror("Bad format in statfs: architecture", EIO);
+ name = buf;
+ name = xp_node_create_from_buf(&n, name);
+ if (!name)
goto nodeset_list_err;
- }
- *arch++ = '\0';
+
+ add = 1;
+ if (state)
+ add &= (!strcmp(n->status, state));
+ if (arch)
+ add &= (!strcmp(n->arch, arch));
+ if (minjobs >= 0)
+ add &= (n->numjobs >= minjobs);
+ if (maxjobs >= 0)
+ add &= (n->numjobs <= maxjobs);
- status = strchr(arch, '\t');
- if(status == NULL) {
- sp_werror("Bad format in statfs: status", EIO);
- goto nodeset_list_err;
- }
- *status++ = '\0';
- numjobs = strchr(status, '\t');
- if(numjobs == NULL) {
- sp_werror("Bad format in statfs: numjobs", EIO);
- goto nodeset_list_err;
- }
- *numjobs++ = '\0';
- eol = strchr(numjobs, '\n');
- if(eol) {
- *eol++ = '\0';
- }
-
- n = xp_node_create(name, addr, arch, status, strtoul(numjobs, 0, 0));
- if(n == NULL) {
- sp_werror(Enomem, ENOMEM);
- goto nodeset_list_err;
- }
- xp_nodeset_add(ns, n);
+ if (add)
+ xp_nodeset_add(ns, n);
}
+
fclose(f);
return ns;
@@ -736,6 +743,58 @@
return NULL;
}
+Xpnodeset *
+xp_nodeset_listfromnet(char *server)
+{
+ if (!server)
+ return NULL;
+
+ return xp_nodeset_listnet_filter(server, NULL, NULL, -1, -1);
+}
+
+Xpnodeset *
+xp_nodeset_list(char *server)
+{
+ /* if server is given use that */
+ if(server)
+ return xp_nodeset_listfromnet(server);
+
+ return xp_nodeset_list_filter(server, NULL, NULL, -1, -1);
+}
+
+Xpnodeset *
+xp_nodeset_list_by_state(char *server, char *state)
+{
+ if (!state)
+ return xp_nodeset_list(server);
+
+ return xp_nodeset_list_filter(server, state, NULL, -1, -1);
+}
+
+Xpnodeset *
+xp_nodeset_list_by_arch(char *server, char *arch)
+{
+ if (!arch)
+ return xp_nodeset_list(server);
+
+ return xp_nodeset_list_filter(server, NULL, arch, -1, -1);
+}
+
+Xpnodeset *
+xp_nodeset_list_by_min_jobs(char *server, int minjobs)
+{
+ if (minjobs == 0)
+ return xp_nodeset_list(server);
+
+ return xp_nodeset_list_filter(server, NULL, NULL, minjobs, -1);
+}
+
+Xpnodeset *
+xp_nodeset_list_by_max_jobs(char *server, int maxjobs)
+{
+ return xp_nodeset_list_filter(server, NULL, NULL, -1, maxjobs);
+}
+
/*
static int
xp_node_cmp_arch(const void *a1, const void *a2)