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
00009
00010
00011
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;
00018 hs->hosts[i].numResources = 0;
00019 }
00020
00021 hs->totalWeight = 0;
00022
00023 return hs;
00024 }
00025
00026
00027 void hostsDestroy(HostsStruct *h){
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 free(h);
00040 }
00041
00042
00043
00044 int hostsAdd(HostsStruct *h){
00045
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
00052 Host *host = &h->hosts[h->numHosts];
00053
00054
00055 host->numResources = 0;
00056
00057 host->status = AVAIL;
00058
00059 host->weight = 1;
00060
00061
00062 h->totalWeight++;
00063
00064
00065 return h->numHosts++;
00066 }
00067
00068
00069 int hostsAddResource(HostsStruct *h, int hostIndex, char *resourceName){
00070
00071 if (hostIndex >= h->numHosts ){
00072 printf("Hosts.c: cant add resource, invalid index(%d)\n", hostIndex);
00073 return -1;
00074 }
00075
00076
00077 Host *host = &h->hosts[hostIndex];
00078
00079
00080 if (host->numResources > MAXRESOURCES){
00081 printf("Hosts.c: warning, reached maximum number of resources(%d)\n", MAXRESOURCES);
00082 return -1;
00083 }
00084
00085
00086 strncpy(host->resources[host->numResources], resourceName, MAX_RNAME_LENGTH);
00087
00088
00089 return host->numResources++;
00090
00091 }
00092
00093
00094 int hostsSetStatus(HostsStruct *h, int hostIndex, int status){
00095
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
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
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
00132 h->totalWeight += host->weight - oldWeight;
00133 return 1;
00134 }
00135
00136 int hostsSetMemory(HostsStruct *h, int hostIndex, int mem){
00137
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
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
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
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
00172 int hostStatus;
00173 int status_addhost = pvm_addhosts(arrayHosts, 1, &hostStatus);
00174
00175
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
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
00200 int hostsGetIndex(HostsStruct *h){
00201 int i=0;
00202
00203 if (h->totalWeight <= 0) {
00204
00205 fprintf(stderr, "There's no more hosts in configuration file. Aborting ...\n");
00206 exit(1);
00207 }
00208
00209
00210 int number = (random() % h->totalWeight) + 1;
00211
00212
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
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
00256 int number = (numHostsAlreadyChoseen++ % numHostsWhoHave);
00257
00258 return hostsWhoHaveRec[number];
00259 }
00260
00261 int hostsGetStatus(HostsStruct *h, int hostIndex){
00262
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
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
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
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
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
00317 int hostsGetNumResources(HostsStruct *h, int hostIndex){
00318
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
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