Hosts.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <strings.h>
00005 #include <pvm3.h>
00006 #include "Hosts.h"
00007 
00008 // $Id: Hosts.c 608 2005-07-15 17:55:04Z coutinho $
00009 
00010 
00011 /// Constructor
00012 HostsStruct *hostsCreate(){
00013         int i;
00014         HostsStruct *hs = (HostsStruct*)malloc(sizeof(HostsStruct));
00015 
00016         for (i=0;i<MAXHOSTS;i++){
00017                 hs->hosts[i].status = NOTINIT; //not initialized
00018                 hs->hosts[i].numResources = 0;
00019         }
00020 
00021         hs->totalWeight = 0;
00022         
00023         return hs;
00024 }
00025 
00026 /// destroyer
00027 void hostsDestroy(HostsStruct *h){
00028         /*int i, j;
00029         
00030         //free host stuff
00031         for (i=0;i<MAXHOSTS;i++){
00032                 //free resources
00033                 for (j=0;j<h->hosts[i].numResources;j++){
00034                         free(h->hosts[i].resources[j]);
00035                 }
00036         }*/
00037         
00038         //free the struct
00039         free(h);
00040 }
00041 
00042 //add functions
00043 /// add a host to the host struct, empty
00044 int hostsAdd(HostsStruct *h){
00045         //max hosts reached
00046         if (h->numHosts >= MAXHOSTS){
00047                 printf("Hosts.c: error, cant add new host, maximum(%d) reached\n", MAXHOSTS);
00048                 return -1;
00049         }
00050         
00051         //get the host slot
00052         Host *host = &h->hosts[h->numHosts];
00053         
00054         //init resources
00055         host->numResources = 0;
00056         //host status now is available, but not used
00057         host->status = AVAIL;
00058         //set weight to 1, default
00059         host->weight = 1;
00060 
00061         //update total weight
00062         h->totalWeight++;
00063         
00064         //return the slot index
00065         return h->numHosts++;
00066 }
00067 
00068 /// adds a resource to the host, caller cannot free resource after this
00069 int hostsAddResource(HostsStruct *h, int hostIndex, char *resourceName){
00070         //invalid index
00071         if (hostIndex >= h->numHosts ){
00072                 printf("Hosts.c: cant add resource, invalid index(%d)\n", hostIndex);
00073                 return -1;
00074         }
00075         
00076         //get the host
00077         Host *host = &h->hosts[hostIndex];
00078 
00079         //check resources
00080         if (host->numResources > MAXRESOURCES){
00081                 printf("Hosts.c: warning, reached maximum number of resources(%d)\n", MAXRESOURCES);
00082                 return -1;
00083         }
00084         
00085         //add the resource
00086         strncpy(host->resources[host->numResources], resourceName, MAX_RNAME_LENGTH);
00087 
00088         //return the resource index
00089         return host->numResources++;
00090 
00091 }
00092 
00093 //set functions, argument can be released
00094 int hostsSetStatus(HostsStruct *h, int hostIndex, int status){
00095         //invalid index
00096         if (hostIndex >= h->numHosts ){
00097                 printf("Hosts.c: cant set status, invalid index(%d)\n", hostIndex);
00098                 return -1;
00099         }
00100 
00101         Host *host = &h->hosts[hostIndex];
00102         
00103         if (host->status != NOTAVAIL) { 
00104                 if (status == NOTAVAIL){
00105                         //we have to reduce totalWeight if we are disabling a host                      
00106                         h->totalWeight -= host->weight;
00107                 }
00108         }
00109 
00110         host->status = status;
00111         return 1;
00112 }
00113 
00114 int hostsSetWeight(HostsStruct *h, int hostIndex, int weight){
00115         //invalid index
00116         if (hostIndex >= h->numHosts ){
00117                 printf("Hosts.c: cant host weight, invalid index(%d)\n", hostIndex);
00118                 return -1;
00119         }
00120         
00121         Host *host = &h->hosts[hostIndex];
00122         int oldWeight = host->weight;
00123         if (weight < 1){
00124                 printf("Hosts.c: warning, invalid weight %d, using 1\n", weight);
00125                 host->weight = 1;
00126         }
00127         else {
00128                 host->weight = weight;
00129         }
00130 
00131         //update total weight
00132         h->totalWeight += host->weight - oldWeight;
00133         return 1;
00134 }
00135 
00136 int hostsSetMemory(HostsStruct *h, int hostIndex, int mem){
00137         //invalid index
00138         if (hostIndex >= h->numHosts ){
00139                 printf("Hosts.c: cant set host memory, invalid index(%d)\n", hostIndex);
00140                 return -1;
00141         }
00142         Host *host = &h->hosts[hostIndex];
00143 
00144         host->mem = mem;
00145         return 1;
00146 
00147 
00148 }
00149 
00150 int hostsSetName(HostsStruct *h, int hostIndex, char *name){
00151         //invalid index
00152         if (hostIndex >= h->numHosts ){
00153                 printf("Hosts.c: cant set host name, invalid index(%d)\n", hostIndex);
00154                 return -1;
00155         }
00156         Host *host = &h->hosts[hostIndex];
00157 
00158         //big name...
00159         if (strlen(name) > MAX_HNAME_LENGTH){
00160                 printf("Hosts.c: warning, hostname length greater than MAX_HNAME_LENGTH(%d), truncating\n", 
00161                         MAX_HNAME_LENGTH);
00162         }
00163 
00164         //cpy name
00165         strncpy(host->name, name, MAX_HNAME_LENGTH);
00166         host->name[MAX_HNAME_LENGTH] = '\0';
00167 
00168         char *arrayHosts[1];
00169         arrayHosts[0] = host->name;
00170         
00171         //adds the host to the virtual machine
00172         int hostStatus;
00173         int status_addhost = pvm_addhosts(arrayHosts, 1, &hostStatus);
00174 
00175         //error handling...
00176         if (status_addhost < 0) {
00177                 printf("Hosts.c: error, pvm_adhost\n");
00178                 pvm_perror("");
00179                 return -1;;
00180         }
00181         else  if ((status_addhost == 0 ) && (hostStatus != PvmDupHost)){
00182                 // created less hosts than we asked for, and no dup
00183                 printf("Hosts.c: error adding host %s to the pvm: ", host->name);
00184                 if (hostStatus == PvmBadParam) {
00185                         printf("bad hostname syntax\n");
00186                 }else if(hostStatus == PvmNoHost) {
00187                         printf("no such host\n");
00188                 }else if(hostStatus == PvmCantStart) {
00189                         printf("failed to start pvmd on host\n");
00190                 }else {
00191                         printf("pvm error %d\n", hostStatus);
00192                 }
00193                 return -1;;
00194         }
00195 
00196         return 1;
00197 }
00198 
00199 // get functions
00200 int hostsGetIndex(HostsStruct *h){
00201         int i=0;
00202         
00203         if (h->totalWeight <= 0) {
00204                 // There's no hosts in the config file or all configured hosts died
00205                 fprintf(stderr, "There's no more hosts in configuration file. Aborting ...\n"); 
00206                 exit(1);
00207         }
00208         
00209         //get a random number between >= 0 < totalWeight
00210         int number = (random() % h->totalWeight) + 1;
00211 
00212         //get the available host
00213         for (i=0;i<MAXHOSTS;i++){
00214                 Host *host = &h->hosts[i];
00215                 switch (host->status){
00216                         case NOTAVAIL:
00217                         case NOTINIT:
00218                                 continue;
00219                         case AVAIL:
00220                                 number -= host->weight;
00221                                 if (number <= 0){
00222                                         return i;
00223                                 }
00224                         break;
00225                 }
00226         }
00227         return -1;
00228 }
00229 
00230 int hostsGetIndexByName(HostsStruct *h, char *name){
00231         int i=0;
00232 
00233         for (i=0;i<h->numHosts;i++){
00234                 if (strncasecmp(h->hosts[i].name, name, MAX_HNAME_LENGTH) == 0){
00235                         return i;
00236                 }
00237         }
00238         //error, could not find the name
00239         return -1;
00240 }
00241 
00242 int hostsGetIndexByResource(HostsStruct *hs, char *resourceName){
00243         int i, hostsWhoHaveRec[MAXHOSTS], numHostsWhoHave=0;
00244         static int numHostsAlreadyChoseen = 0;
00245 
00246         for (i=0;i<hs->numHosts;i++){
00247                 if (hostsHasResource(hs, i, resourceName) == 1){
00248                         hostsWhoHaveRec[numHostsWhoHave++] = i;
00249                 }
00250         }
00251         
00252         if (numHostsWhoHave == 0)
00253                 return -1;
00254         
00255         //get a random number between >= 0 < totalWeight
00256         int number = (numHostsAlreadyChoseen++ % numHostsWhoHave);
00257 
00258         return hostsWhoHaveRec[number];
00259 }
00260 
00261 int hostsGetStatus(HostsStruct *h, int hostIndex){
00262         //invalid index
00263         if (hostIndex >= h->numHosts ){
00264                 printf("Hosts.c: cant get status, invalid index(%d)\n", hostIndex);
00265                 return -1;
00266         }
00267 
00268         Host *host = &h->hosts[hostIndex];
00269 
00270         return host->status;
00271 }
00272 
00273 int hostsGetMemory(HostsStruct *h, int hostIndex){
00274         //invalid index
00275         if (hostIndex >= h->numHosts ){
00276                 printf("Hosts.c: cant get memory, invalid index(%d)\n", hostIndex);
00277                 return -1;
00278         }
00279 
00280         Host *host = &h->hosts[hostIndex];
00281 
00282         return host->mem;
00283 }
00284 
00285 int hostsGetWeight(HostsStruct *h, int hostIndex){
00286         // invalid index
00287         if (hostIndex >= h->numHosts ){
00288                 printf("Hosts.c: cant get weight, invalid index(%d)\n", hostIndex);
00289                 return -1;
00290         }
00291 
00292         Host *host = &h->hosts[hostIndex];
00293 
00294         //we return the weight only if the host status is available
00295         if (hostsGetStatus(h, hostIndex) >= 0){
00296                 return host->weight;
00297         }
00298         else {
00299                 return 0;
00300         }
00301 }
00302 
00303 
00304 char *hostsGetName(HostsStruct *h, int hostIndex){
00305         //invalid index
00306         if (hostIndex >= h->numHosts ){
00307                 printf("Hosts.c: cant get name, invalid index(%d)\n", hostIndex);
00308                 return NULL;
00309         }
00310 
00311         Host *host = &h->hosts[hostIndex];
00312 
00313         return host->name;
00314 }
00315 
00316 //resource stuff......
00317 int hostsGetNumResources(HostsStruct *h, int hostIndex){
00318         //invalid index
00319         if (hostIndex >= h->numHosts ){
00320                 printf("Hosts.c: cant get number of resources, invalid index(%d)\n", hostIndex);
00321                 return -1;
00322         }
00323 
00324         Host *host = &h->hosts[hostIndex];
00325 
00326         return host->numResources;
00327 
00328 }
00329 
00330 int hostsHasResource(HostsStruct *h, int hostIndex, char *resourceName){
00331         //invalid index
00332         if (hostIndex >= h->numHosts ){
00333                 printf("Hosts.c: cant get resource, invalid index(%d)\n", hostIndex);
00334                 return 0;
00335         }
00336         Host *host = &h->hosts[hostIndex];
00337         int i;
00338         for (i=0;i<host->numResources;i++){
00339                 if (strncmp(resourceName, host->resources[i], MAX_RNAME_LENGTH) == 0){
00340                         return 1;
00341                 }
00342         }
00343         return 0;
00344 }
00345 //....................
00346 
00347 

Generated on Tue Jan 17 19:18:38 2006 for Void by  doxygen 1.4.6