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

CVS | Administration


File:  [GForge:cvsroot/nutss] / libnutss / examples / slinks_variable_load.c
Revision 1.27: download - view: text, annotated - select for diffs
Sat Jul 28 00:41:57 2007 UTC (10 years, 1 month ago) by ths
Branches: MAIN
CVS tags: HEAD
big changes to slinks test harnes to allow for more realistic experiments

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef WIN32
#include <poll.h>
#include <sys/socket.h>
#endif
#include <pthread.h>
#include "../include/swaplinks.h"

#ifdef WIN32
#define sleep(sec) Sleep((sec)*1000)
#define snprintf   _snprintf
#endif
#define clear(a) a[0]=0
#define streq(a,b) !strcmp((a),(b))
#define strlenn(s) (s) ? strlen(s) : 0
#define randint(N) ((int)(rand() / (((double)RAND_MAX + 1) / (N))))
#define assert(p) if(!(p)) { printf("Assertion '%s' FAILED!\n", #p); exit(1); }
#define REDUNDANCY 5
#define PROPAGATION 300
#define GRACE 3
#define ALLOTMENT 400
#define WALK_LENGTH 10
#define MAX_FLOWS 256
#define BUFFER_SIZE 128
#define URI_LENGTH 256

typedef struct flow {
    int duration, weight, exts;
    char source[URI_LENGTH];
    struct flow *next, *last;
} flow_t;

typedef struct int_pair_t { int i; int o; } int_pair;
typedef struct flow_spec_t { int weight; int expiration; } flow_spec;

int_pair process_neighbor_list(char*);

// Gobal information, used by multiple threads
int num, nodes = 0, peers, capacity;
swaplinks_p swp;
FILE *f = NULL, *s = NULL;

// Node's receiving thread that decides whether to accept an incoming
// request and records all the necessary information
void *recvthread(void *arg) {
    struct sockaddr_ns peer;
    socklen_t len = sizeof(peer);
    char msg[BUFFER_SIZE], tmp[BUFFER_SIZE];
    int load = 0, bytes, msg_count, i;
    time_t start,now;
    flow_t newflow;
    flow_spec flows[MAX_FLOWS];

    start = time(NULL);
    for(;;) {
        memset(&peer,0,sizeof(peer));
	sleep(1);
        if((bytes=swaplinks_recvfrom(swp, msg, sizeof(msg), 0, &peer, &len)) > 0) {
            msg[bytes] = 0;
            // Flood SHUTDOWN to neighbors, then power down
            if(streq(msg,"SHUTDOWN")) {
                swaplinks_update_walk_length(swp,1);
                for(i = 0; i < peers*2; i++) swaplinks_sendtoany(swp,"SHUTDOWN",9,0);
                printf("%d Recieved KILL signal...\n",num);
                break;
            }
            // parse the new flow
            memcpy(tmp,msg,sizeof(tmp));
            newflow.duration = atoi(strtok(tmp,";"));
            newflow.weight = atoi(strtok(NULL,";"));
            newflow.exts = atoi(strtok(NULL,";"));
            strncpy(newflow.source,peer.user+4,sizeof(newflow.source));
            if(s) fprintf(s,"    %d recvd request #%d from %s - w:%d d:%d\n",
                          num,++msg_count,newflow.source,newflow.weight,newflow.duration);

            // retire any expired flows before processing
            now = difftime(time(NULL),start);
            for(i=0;i < MAX_FLOWS;i++) {
                if(0 < flows[i].expiration && flows[i].expiration < now) {
                    load -= flows[i].weight;
                    flows[i].weight = 0;
                    flows[i].expiration = 0;
                    if(s) fprintf(s,"        %d finished accomodating flow at %ld: %d\n",num,now,load);
                }
            }
            if(load+newflow.weight < capacity) {
                load += newflow.weight;
                for(i=0;i < MAX_FLOWS;i++) {
                    if(flows[i].expiration == 0) {
                        flows[i].weight = newflow.weight;
                        flows[i].expiration = now + newflow.duration;
                        if(s) fprintf(s,"        %d accepted request at %ld: %d\n",num,now,load);
                        if(f) fprintf(f,"%ld %s\n",now,msg);
                        break;
                    }
                }
                assert(i != MAX_FLOWS);
            }
            // ADMISSION CONTROL
            // do not accept the flow if it will overload this node.
            // if the request been pushed too far, reject it, otherwise
            // pass it on to a neighbor
            else if(newflow.exts < GRACE) {
                swaplinks_update_walk_length(swp,1);
                snprintf(tmp,sizeof(tmp),"%d;%d;%d;",newflow.duration,newflow.weight,newflow.exts+1);
                swaplinks_sendtoany(swp,tmp,strlenn(tmp)+1,0);
                swaplinks_update_walk_length(swp,WALK_LENGTH);
                if(s) fprintf(s,"        %d rejected flow, load too high: %d <= %d\n",num,capacity,load+newflow.weight);
                if(f) fprintf(f,"%ld %s\n",now,"reject");
            }
            else {
                if(s) fprintf(s,"        %d retired unserviceable request\n",num);
                if(f) fprintf(f,"%ld %s\n",now,"retire");
            }
        }
    }
    // close output files to assure data is recorded
    if(f && f != stderr) { fclose(f); f = NULL; }
    if(s && s != stdout) { fclose(s); s = NULL; }

    pthread_exit(NULL);
}

int main(int argc, char **argv) {
    struct sockaddr_ns reg;
    char tmp[BUFFER_SIZE], nbrs[4096];
    int time = 0, duration, weight, percent, limit, i;
    FILE *g;
    int_pair p;
    pthread_t tid;

    // parse args
    if(argc > 1) num = atoi(argv[1]);
    if(num != 99 && argc > 6) {
        peers = atoi(argv[2]);
        limit = atoi(argv[3]);
        percent = atoi(argv[4]);
        duration = atoi(argv[5]);
        weight = atoi(argv[6]);
        capacity = peers*ALLOTMENT;
    }
    else if(num == 99 && argc > 4) {
        nodes = atoi(argv[2]);
        // peer coordinator with more nodes
        // to facilitate dispersion of kill signals
        peers = atoi(argv[3]);
        time = atoi(argv[4]);
    }
    else if(num != 99) {
        printf("Usage:\n  slinksvariableload node_number num_peers num_messages percent_load flow_duration flow_weight capacity\n");
        exit(1);
    }
    else {
       printf("Coordinator Usage:\n  slinksvariableload 99 num_nodes num_peers experiment_duration_minutes\n");
       exit(1);
    }

    // set all the important nutss fields
    snprintf(tmp, sizeof(tmp), "test%d", num);
    nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlenn(tmp));
    nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlenn(tmp));
    nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlenn(tmp));
    strncpy(tmp, "nutss.net", sizeof(tmp));
    nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlenn(tmp));
    strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
    nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlenn(tmp));

    // open output files
    if(num != 99) {
        sprintf(tmp, "output/data_node%d.txt", num); f = fopen(tmp,"w");
//        sprintf(tmp, "output/output_node%d.txt", num); s = fopen(tmp,"w");
//        sprintf(tmp, "output/nbrs_node%d.txt", num); g = fopen(tmp,"w");
        if (f == NULL) f = stderr;
        if (s == NULL) s = stdout;
    }
    if(f) fprintf(f,"%d\n",peers);

    // build registrar uri
    memset(&reg, 0, sizeof(reg));
    reg.family = AF_NUTSS;
    strncpy(reg.user, "ths1", sizeof(reg.user));
    strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
    strncpy(reg.service, "swaplinksd", sizeof(reg.service));
    // start up swaplinks and let it get going
    swaplinks_init(); swp = swaplinks_new("cloud9", &reg, peers, WALK_LENGTH); sleep(5);

    // coordinator thread
    if(num == 99) {
        // let nodes wake up to recieve the greetings
        for(;time > 0;time--) {
            printf(" ---> %d MINUTE%s REMAIN%s\n",time,time==1?"":"S",time==1?"S":"");
            sleep(60);
        }
        printf("Now sending KILL signals\n",limit);
        for(i = 0; i < nodes*REDUNDANCY; i++) swaplinks_sendtoany(swp, "SHUTDOWN", 9, 0);
        // give them time to scramble
        printf("Waiting for KILL signals to propagate\n");
        sleep(PROPAGATION);
        // clean up their mess
        printf("Forcibly removing remaining nodes and associated file\n");
        system("find output -size 0c | xargs rm -rf");
        // then kill stragglers more violently (including the coordinator and its sending thread as well)
        system("killall lt-slinksvariableload");
        return 0;
    }

    pthread_create(&tid, NULL, recvthread, swp);
    // give recieving threads a chance to wake up
    sleep(5);

    while (time++ < limit) {
        // send a request for resources with probability percent/100
        if(randint(100) < percent) {
            snprintf(tmp,sizeof(tmp),"%d;%d;0;",duration,weight);
            swaplinks_sendtoany(swp,tmp,strlenn(tmp)+1, 0);
        }
        // record number and type of neighbors
      //  swaplinks_get_neighbors(swp,nbrs,4096);
      //  p = process_neighbor_list(nbrs);
      //  if(g) fprintf(g,"%d %d\n",p.i,p.o);
      //  clear(nbrs);
       // it might make more sense to sleep for more time between requests
        sleep(3);
    }

  //  if(g) fclose(g);
    if(s) printf("%d DONE SENDING\n",num);
    pthread_join(tid, NULL);
    if(s) fprintf(s,"%d SHUTTING DOWN\n",num);

    return 0;
}

int_pair process_neighbor_list(char *s) {
    int_pair p = { 0, 0 };
    char *t,*v;

    if(!s) return p;
    while(*s) {
        if(*s++ == '_') {
            switch(*s++) {
                case 'i': p.i++; break;
                case 'o': p.o++; break;
                default: exit(s[-1]);
            }
        }
    }
    return p;
}

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