Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration

Annotation of libnutss/examples/slinks_variable_load.c, revision 1.28

1.24      ths         1: #include <stdio.h>
                      2: #include <stdlib.h>
                      3: #include <string.h>
1.27      ths         4: #include <time.h>
1.28    ! ths         5: #include <math.h>
        !             6: #include <limits.h>
1.24      ths         7: #ifndef WIN32
                      8: #include <poll.h>
                      9: #include <sys/socket.h>
                     10: #endif
                     11: #include <pthread.h>
                     12: #include "../include/swaplinks.h"
                     13:
                     14: #ifdef WIN32
                     15: #define sleep(sec) Sleep((sec)*1000)
                     16: #define snprintf   _snprintf
                     17: #endif
                     18: #define clear(a) a[0]=0
                     19: #define streq(a,b) !strcmp((a),(b))
                     20: #define strlenn(s) (s) ? strlen(s) : 0
                     21: #define randint(N) ((int)(rand() / (((double)RAND_MAX + 1) / (N))))
1.28    ! ths        22: #define round(f) (f)-(int)(f) > 0.5 ? (int)(f)+1 : (int)(f)
        !            23: #define min(a,b) ((a)<(b)?(a):(b))
        !            24: #define max(a,b) ((a)>(b)?(a):(b))
1.24      ths        25: #define assert(p) if(!(p)) { printf("Assertion '%s' FAILED!\n", #p); exit(1); }
1.28    ! ths        26: #define REDUNDANCY    5       // how many message repetitions are necessary to reach every node
        !            27: #define PROPAGATION   600     // how many seconds the coordinator should wait for every node to recieve KILL signal
        !            28: #define EXPIRATION    600     // how many seconds a node should keep a peering report fron another node
        !            29: #define GRACE         7       // how many times a request can be passed on by a full-capacity node
        !            30: #define ALLOTMENT     400     // how much load a node will accomadate for each of his neighbors (obsolete w/ auto peering)
        !            31: #define QUORUM        12      // how many reports a node needs before adjusting his peering level
        !            32: #define THRESHOLD     0.15    // how much different a newly calculated degree must be from the old one to switch to it
        !            33: #define MAX_REPORTS   30
        !            34: #define WALK_LENGTH   10
        !            35: #define MIN_PEERS     3
        !            36: #define DEFAULT_PEERS 8       // obsolete w/ auto peering
        !            37: #define MAX_PEERS     40
        !            38: #define MAX_FLOWS     256
        !            39: #define BUFFER_SIZE   128
        !            40: #define URI_LENGTH    256
1.24      ths        41:
                     42: typedef struct flow {
1.28    ! ths        43:     int id, duration, weight, exts, capacity;
1.27      ths        44:     char source[URI_LENGTH];
1.24      ths        45:     struct flow *next, *last;
                     46: } flow_t;
                     47:
1.28    ! ths        48: typedef struct int_pair_t { int i, o; }                         int_pair;
        !            49: typedef struct flw_spec_t { int weight, expiration; }           flw_spec;
        !            50: typedef struct nbr_rprt_t { int expiration, capacity; }  nbr_rprt;
1.24      ths        51:
                     52: int_pair process_neighbor_list(char*);
                     53:
1.27      ths        54: // Gobal information, used by multiple threads
1.28    ! ths        55: int num, nodes = 0, peers, capacity, alive = 1;
1.24      ths        56: swaplinks_p swp;
1.28    ! ths        57: FILE *f = NULL, *s = NULL, *q = NULL;
1.24      ths        58:
1.27      ths        59: // Node's receiving thread that decides whether to accept an incoming
1.28    ! ths        60: // request and records all the necessary information; also carries
        !            61: // out the gossiping-equilibrium protocol to assign peering level
1.27      ths        62: void *recvthread(void *arg) {
1.26      ths        63:     struct sockaddr_ns peer;
                     64:     socklen_t len = sizeof(peer);
1.27      ths        65:     char msg[BUFFER_SIZE], tmp[BUFFER_SIZE];
                     66:     int load = 0, bytes, msg_count, i;
                     67:     time_t start,now;
                     68:     flow_t newflow;
1.28    ! ths        69:     flw_spec flows[MAX_FLOWS];
        !            70:     nbr_rprt reports[MAX_FLOWS];
        !            71:     nbr_rprt tempbuf[MAX_FLOWS];
        !            72:     nbr_rprt sorted[MAX_FLOWS];
        !            73:
        !            74:     memset(flows,0,sizeof(flw_spec)*MAX_FLOWS);
        !            75:     memset(reports,0,sizeof(nbr_rprt)*MAX_FLOWS);
1.26      ths        76:
1.27      ths        77:     start = time(NULL);
                     78:     for(;;) {
1.24      ths        79:         memset(&peer,0,sizeof(peer));
1.28    ! ths        80:         sleep(1);
        !            81:       // simulate churn by having a small portion of nodes duck out early, and replace them periodically
        !            82: //        if(randint(1000) < 2) {
        !            83: //            printf("%d Withdrawing from cloud early\n",num);
        !            84: //            break;
        !            85: //        }
        !            86:       if((bytes=swaplinks_recvfrom(swp, msg, sizeof(msg), 0, &peer, &len)) > 0) {
1.27      ths        87:             msg[bytes] = 0;
1.28    ! ths        88:             memcpy(tmp,msg,bytes);
1.27      ths        89:             // Flood SHUTDOWN to neighbors, then power down
                     90:             if(streq(msg,"SHUTDOWN")) {
1.25      ths        91:                 swaplinks_update_walk_length(swp,1);
1.27      ths        92:                 for(i = 0; i < peers*2; i++) swaplinks_sendtoany(swp,"SHUTDOWN",9,0);
                     93:                 printf("%d Recieved KILL signal...\n",num);
1.25      ths        94:                 break;
                     95:             }
1.27      ths        96:             // parse the new flow
                     97:             memcpy(tmp,msg,sizeof(tmp));
                     98:             newflow.duration = atoi(strtok(tmp,";"));
                     99:             newflow.weight = atoi(strtok(NULL,";"));
                    100:             newflow.exts = atoi(strtok(NULL,";"));
1.28    ! ths       101:             newflow.id = atoi(strtok(NULL,";"));
        !           102:             newflow.capacity = atoi(strtok(NULL,";"));
1.27      ths       103:             strncpy(newflow.source,peer.user+4,sizeof(newflow.source));
                    104:
1.28    ! ths       105:             /*******/
        !           106:             // Adjust peering level based on updated picture of the state of the network
        !           107:             {
        !           108:                 int alive = 0, more = 0, max_capacity = 0, min_capacity = INT_MAX, numReports = 0, src, curmax, k;
        !           109:                 float rank, newDegree, tempfloat;
        !           110:                 // parse report
        !           111:                 now = difftime(time(NULL),start);
        !           112:                 src = atoi(newflow.source);
        !           113:                 assert(src < MAX_FLOWS);
        !           114:                 // retire any old data and count current reports
        !           115:                 for(i=0;i<MAX_FLOWS;i++) {
        !           116:                     if(0 < reports[i].expiration && reports[i].expiration > now)
        !           117:                         numReports++;
        !           118:                     if(0 < reports[i].expiration && reports[i].expiration < now)
        !           119:                         memset(reports+i,0,sizeof(nbr_rprt));
        !           120:                 }
        !           121:                 // dont add this report if we have enough already
        !           122:                 if(numReports < MAX_REPORTS) {
        !           123:                     reports[src].capacity = newflow.capacity;
        !           124:                     reports[src].expiration = now+EXPIRATION;
        !           125:                 }
        !           126:                 // gather important data based on known info
        !           127:                 for(i=0;i<MAX_FLOWS;i++) {
        !           128:                     if(0 < reports[i].capacity) alive++;
        !           129:                     if(max_capacity < reports[i].capacity) max_capacity = reports[i].capacity;
        !           130:                     if(min_capacity > reports[i].capacity && 0 < reports[i].capacity) min_capacity = reports[i].capacity;
        !           131:                     if(capacity < reports[i].capacity) more++;
        !           132:                 }
        !           133:                 // include this node's capacity in mix
        !           134:                 if(max_capacity < capacity) max_capacity = capacity;
        !           135:                 if(min_capacity > capacity) min_capacity = capacity;
        !           136:                 assert(alive > 0);
        !           137:                 rank = ((float)(alive-more))/alive;
        !           138:
        !           139:                 // sort information for viewing purposes
        !           140:     //                memcpy(tempbuf,reports,MAX_FLOWS*sizeof(nbr_rprt));
        !           141:     //                for(i=0;i<alive;i++) {
        !           142:     //                    curmax = 0;
        !           143:     //                    for(k=0;k<MAX_FLOWS;k++) {
        !           144:     //                        if(tempbuf[curmax].capacity < tempbuf[k].capacity)
        !           145:     //                            curmax = k;
        !           146:     //                    }
        !           147:     //                    sorted[i] = tempbuf[curmax];
        !           148:     //                    tempbuf[curmax].capacity = 0;
        !           149:     //                }
        !           150:
        !           151:                 // compute new degree... still need to think about compressing scale if it gets too big
        !           152:                 newDegree = rank*min(((float)max_capacity/min_capacity)*MIN_PEERS,MAX_PEERS);
        !           153:                 // only alter degree if we have a quorum and the change would be significant
        !           154:                 if(alive > QUORUM && abs(max(newDegree,MIN_PEERS)-peers) > peers*THRESHOLD)
        !           155:                     peers = round(max(newDegree,MIN_PEERS));
        !           156:                 if(q) fprintf(q,"%ld %d\n",now,peers);
        !           157:
        !           158:                 //                printf("- *%d* -\n",capacity);
        !           159:                 //                for(i=0;i<alive;i++)
        !           160:                 //                    printf(" %d:%4.2f",sorted[i].capacity,(double)(sorted[i].expiration)-difftime(time(NULL),start));
        !           161:                 //                printf("\n  %d peering at %d, %4.2f out of %d, adjmax %3.1f\n",num,peers,rank*100.0,alive+1,adjusted_max_peers);
        !           162:                 //                printf("---------\n");
        !           163:             }
        !           164:             /********/
        !           165:
        !           166:             if(s) fprintf(s,"    %d recvd request %d from %s - w:%d d:%d e:%d\n",
        !           167:                           num,newflow.id,newflow.source,newflow.weight,newflow.duration,newflow.exts);
1.27      ths       168:             // retire any expired flows before processing
                    169:             now = difftime(time(NULL),start);
1.28    ! ths       170:             for(i=0;i<MAX_FLOWS;i++) {
1.27      ths       171:                 if(0 < flows[i].expiration && flows[i].expiration < now) {
                    172:                     load -= flows[i].weight;
                    173:                     flows[i].weight = 0;
                    174:                     flows[i].expiration = 0;
                    175:                     if(s) fprintf(s,"        %d finished accomodating flow at %ld: %d\n",num,now,load);
                    176:                 }
                    177:             }
                    178:             if(load+newflow.weight < capacity) {
                    179:                 load += newflow.weight;
1.28    ! ths       180:                 // find an empty spot to record this flow
        !           181:                 for(i=0;i<MAX_FLOWS;i++) {
1.27      ths       182:                     if(flows[i].expiration == 0) {
                    183:                         flows[i].weight = newflow.weight;
                    184:                         flows[i].expiration = now + newflow.duration;
                    185:                         if(s) fprintf(s,"        %d accepted request at %ld: %d\n",num,now,load);
                    186:                         if(f) fprintf(f,"%ld %s\n",now,msg);
                    187:                         break;
1.24      ths       188:                     }
                    189:                 }
1.28    ! ths       190:                 // if we run out of space, i dont want to add more space, just die.
1.27      ths       191:                 assert(i != MAX_FLOWS);
1.24      ths       192:             }
                    193:             // ADMISSION CONTROL
1.27      ths       194:             // do not accept the flow if it will overload this node.
                    195:             // if the request been pushed too far, reject it, otherwise
                    196:             // pass it on to a neighbor
                    197:             else if(newflow.exts < GRACE) {
1.24      ths       198:                 swaplinks_update_walk_length(swp,1);
1.28    ! ths       199:                 snprintf(tmp,sizeof(tmp),"%d;%d;%d;%d;%d;",newflow.duration,newflow.weight,newflow.exts+1,newflow.id,newflow.capacity);
1.27      ths       200:                 swaplinks_sendtoany(swp,tmp,strlenn(tmp)+1,0);
                    201:                 swaplinks_update_walk_length(swp,WALK_LENGTH);
                    202:                 if(s) fprintf(s,"        %d rejected flow, load too high: %d <= %d\n",num,capacity,load+newflow.weight);
                    203:                 if(f) fprintf(f,"%ld %s\n",now,"reject");
                    204:             }
                    205:             else {
1.28    ! ths       206:                 if(s) fprintf(s,"        %d retired unserviceable request %d\n",num,newflow.id);
1.27      ths       207:                 if(f) fprintf(f,"%ld %s\n",now,"retire");
1.24      ths       208:             }
                    209:         }
                    210:     }
1.27      ths       211:     // close output files to assure data is recorded
1.26      ths       212:     if(f && f != stderr) { fclose(f); f = NULL; }
                    213:     if(s && s != stdout) { fclose(s); s = NULL; }
1.25      ths       214:
1.28    ! ths       215:     alive = 0;
        !           216:
1.24      ths       217:     pthread_exit(NULL);
                    218: }
                    219:
                    220: int main(int argc, char **argv) {
                    221:     struct sockaddr_ns reg;
1.27      ths       222:     char tmp[BUFFER_SIZE], nbrs[4096];
                    223:     int time = 0, duration, weight, percent, limit, i;
                    224:     FILE *g;
1.24      ths       225:     int_pair p;
1.27      ths       226:     pthread_t tid;
1.24      ths       227:
1.27      ths       228:     // parse args
1.24      ths       229:     if(argc > 1) num = atoi(argv[1]);
1.27      ths       230:     if(num != 99 && argc > 6) {
1.28    ! ths       231:         limit = atoi(argv[2]);
        !           232:         percent = atoi(argv[3]);
        !           233:         duration = atoi(argv[4]);
        !           234:         weight = atoi(argv[5]);
        !           235:         capacity = atoi(argv[6]);
        !           236:         peers = DEFAULT_PEERS;
1.24      ths       237:     }
1.25      ths       238:     else if(num == 99 && argc > 4) {
1.27      ths       239:         nodes = atoi(argv[2]);
                    240:         // peer coordinator with more nodes
                    241:         // to facilitate dispersion of kill signals
                    242:         peers = atoi(argv[3]);
                    243:         time = atoi(argv[4]);
1.25      ths       244:     }
1.24      ths       245:     else if(num != 99) {
1.28    ! ths       246:         printf("Usage:\n  slinksvariableload node_number num_messages percent_load flow_duration flow_weight capacity\n");
1.24      ths       247:         exit(1);
                    248:     }
1.25      ths       249:     else {
1.27      ths       250:        printf("Coordinator Usage:\n  slinksvariableload 99 num_nodes num_peers experiment_duration_minutes\n");
1.25      ths       251:        exit(1);
                    252:     }
1.24      ths       253:
1.27      ths       254:     // set all the important nutss fields
1.24      ths       255:     snprintf(tmp, sizeof(tmp), "test%d", num);
                    256:     nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlenn(tmp));
                    257:     nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlenn(tmp));
                    258:     nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlenn(tmp));
                    259:     strncpy(tmp, "nutss.net", sizeof(tmp));
                    260:     nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlenn(tmp));
                    261:     strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
                    262:     nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlenn(tmp));
                    263:
1.27      ths       264:     // open output files
1.24      ths       265:     if(num != 99) {
                    266:         sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
1.28    ! ths       267:         sprintf(tmp, "output/peer_node%d.txt", num); q = fopen(tmp,"w");
1.27      ths       268: //        sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
                    269: //        sprintf(tmp, "output/nbrs_node%d.txt", num); g = fopen(tmp,"w");
1.24      ths       270:         if (f == NULL) f = stderr;
                    271:         if (s == NULL) s = stdout;
                    272:     }
1.28    ! ths       273:     if(f) fprintf(f,"%d\n",capacity);
1.24      ths       274:
1.27      ths       275:     // build registrar uri
1.24      ths       276:     memset(&reg, 0, sizeof(reg));
                    277:     reg.family = AF_NUTSS;
                    278:     strncpy(reg.user, "ths1", sizeof(reg.user));
                    279:     strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
                    280:     strncpy(reg.service, "swaplinksd", sizeof(reg.service));
1.27      ths       281:     // start up swaplinks and let it get going
                    282:     swaplinks_init(); swp = swaplinks_new("cloud9", &reg, peers, WALK_LENGTH); sleep(5);
1.24      ths       283:
                    284:     // coordinator thread
                    285:     if(num == 99) {
1.25      ths       286:         // let nodes wake up to recieve the greetings
1.27      ths       287:         for(;time > 0;time--) {
                    288:             printf(" ---> %d MINUTE%s REMAIN%s\n",time,time==1?"":"S",time==1?"S":"");
1.28    ! ths       289:             clear(nbrs);
        !           290:             swaplinks_get_neighbors(swp,nbrs,4096);
        !           291:             p = process_neighbor_list(nbrs);
        !           292:             printf("%d %d\n",p.i,p.o);
1.27      ths       293:             sleep(60);
1.24      ths       294:         }
1.27      ths       295:         printf("Now sending KILL signals\n",limit);
                    296:         for(i = 0; i < nodes*REDUNDANCY; i++) swaplinks_sendtoany(swp, "SHUTDOWN", 9, 0);
1.25      ths       297:         // give them time to scramble
1.27      ths       298:         printf("Waiting for KILL signals to propagate\n");
                    299:         sleep(PROPAGATION);
1.26      ths       300:         // clean up their mess
1.27      ths       301:         printf("Forcibly removing remaining nodes and associated file\n");
1.26      ths       302:         system("find output -size 0c | xargs rm -rf");
1.27      ths       303:         // then kill stragglers more violently (including the coordinator and its sending thread as well)
1.25      ths       304:         system("killall lt-slinksvariableload");
1.28    ! ths       305:         printf("Coordinator shutting down.\n");
1.24      ths       306:         return 0;
                    307:     }
                    308:
                    309:     pthread_create(&tid, NULL, recvthread, swp);
1.27      ths       310:     // give recieving threads a chance to wake up
                    311:     sleep(5);
1.24      ths       312:
1.28    ! ths       313:     while (time++ < limit && alive) {
1.27      ths       314:         // send a request for resources with probability percent/100
                    315:         if(randint(100) < percent) {
1.28    ! ths       316:             snprintf(tmp,sizeof(tmp),"%d;%d;0;%d%d;%d;",duration,weight,num,time,capacity);
1.27      ths       317:             swaplinks_sendtoany(swp,tmp,strlenn(tmp)+1, 0);
                    318:         }
                    319:         // record number and type of neighbors
                    320:       //  swaplinks_get_neighbors(swp,nbrs,4096);
                    321:       //  p = process_neighbor_list(nbrs);
                    322:       //  if(g) fprintf(g,"%d %d\n",p.i,p.o);
                    323:       //  clear(nbrs);
                    324:        // it might make more sense to sleep for more time between requests
                    325:         sleep(3);
1.24      ths       326:     }
                    327:
1.27      ths       328:   //  if(g) fclose(g);
1.26      ths       329:     if(s) printf("%d DONE SENDING\n",num);
1.24      ths       330:     pthread_join(tid, NULL);
                    331:     if(s) fprintf(s,"%d SHUTTING DOWN\n",num);
                    332:
                    333:     return 0;
                    334: }
                    335:
1.28    ! ths       336: // read the neighbor list and extract the number of in and out nbrs
        !           337: // would also like to alter this to give the number of unique nbrs
        !           338: // instead of the raw number
1.24      ths       339: int_pair process_neighbor_list(char *s) {
                    340:     int_pair p = { 0, 0 };
                    341:     char *t,*v;
                    342:
                    343:     if(!s) return p;
                    344:     while(*s) {
                    345:         if(*s++ == '_') {
                    346:             switch(*s++) {
                    347:                 case 'i': p.i++; break;
                    348:                 case 'o': p.o++; break;
                    349:                 default: exit(s[-1]);
                    350:             }
                    351:         }
                    352:     }
                    353:     return p;
                    354: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment