⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 churngen.cc

📁 这是关于覆盖网的各种负载均衡算法的程序。好好研究
💻 CC
字号:
/* $Id: churnGen.cc,v 1.4 2005/08/02 19:35:48 jonathan Exp $ * Jonathan Ledlie, Harvard University. * Copyright 2005.  All rights reserved. */#include <stdio.h>#include <stdlib.h>#ifdef LINUX#include <getopt.h>#endif#include <sys/types.h>#include <unistd.h>#include <algorithm>#include <vector>#include "math_util.h"#include "error.h"#include "distributions.h"FILE *logFP;int previousBirth = 0;bool debug = false;void birth (int id, int round) {  printf ("%d b %d\n", round, id);  previousBirth = round;}void death (int id, int round) {  printf ("%d d %d\n", round, id);  int session = round - previousBirth;  fprintf (logFP, "%d\n", session);}char *usage = \"Usage: churn, Distributions\n" \"            -l lifetime distribution\n" \"            -u chance of a node being up at beginning\n" \"            -s random seed\n" \"            -b time to begin recording\n" \"            -e time to end recording\n" \"            -n number of nodes\n" \"\n" \"Distributions:\n" \" Pareto       P/scale/shape\n" \" Normal       N/mean/stddev\n" \" Zipf         Z/alpha/num-elements\n" \" Poisson      F/mean\n" \" Uniform      U [0..1)\n" \" Constant     C/value\n""\n Run the output through sort -n\n";void printUsage () {  printf ("%s", usage);  exit (-1);}void printUsage (char* problem) {  printf ("%s\n", problem);  printUsage ();}int main (int argc, char **argv) {  char c = 0;  char *lifetimeDistStr = NULL;  Distribution *lifetimeDist;  DistributionFactory *distFactory = NULL;  int seed = getpid ();  int nodeCount = -1;  double pctUp = .5;  int beginRecord = -1, endRecord = -1;  char filename[20];  memset (filename, 0, 20);  sprintf (filename, "churnGen.log");  if ((logFP = fopen(filename, "w")) == NULL) {    printf("%s: file write open error.\n", filename);    exit (-1);  }  distFactory = new DistributionFactory ();  while ( (c = getopt(argc,argv,"n:l:u:s:b:e:")) >= 0) {    switch (c) {    case 'l'://lifetime distribution      lifetimeDistStr = optarg;      lifetimeDist = distFactory->newDistribution (lifetimeDistStr);      if (lifetimeDist == NULL)      	printUsage ("Problem with lifetime distribution");      break;    case 'u'://      pctUp = atof (optarg);      if (pctUp > 1. || pctUp <= 0.) {//pctUp should  be (0,1]      	printUsage ("Problem with up pct.");      }      break;    case 's'://radom seed      seed = atoi (optarg);      break;    case 'b'://begin record time      beginRecord = atoi (optarg);      break;    case 'e':// end record time      endRecord = atoi (optarg);      break;    case 'n'://node count      nodeCount = atoi (optarg);      break;    default:      printUsage ();      break;    }  }  if (beginRecord < 0 || endRecord <= 0) {    printUsage ("Error in begin or end record.");  }  if (nodeCount <= 0) {    printUsage ("Error in number of nodes.");  }  SRand (seed);//设置随机数种子  int halfway = (endRecord - beginRecord)/2;  printf ("%d r 0\n", halfway);  for (int i = 0; i < nodeCount; i++) {//对各个节点产生事件    previousBirth = 0;//    int round = 0;//初始时间    bool up = true;//    if (randPct() < pctUp) {//generate multiple events in a same round 0      ;    } else {      round = (int)(lifetimeDist->next());//产生下一轮    }	    bool beenBorn = false;//是否产生了该节点    //generate events for the same node    while (round < endRecord) {      if (up) {//死掉的节点		if (round >= beginRecord) {		  birth (i, round-beginRecord);//产生该节点		  beenBorn = true;		}		up = false;      } else {//活着的节点		if (round >= beginRecord) {			  if (!beenBorn) {//			    birth (i, 0);			  }			  death (i, round-beginRecord);		}		up = true;//标记死掉      }	//产生一个活着或者死掉的间隔      int session = 0;      while (session == 0) {	session = (int)(lifetimeDist->next());      }      round += session;    }  }  fclose (logFP);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -