/* $Id: churnGen.cc,v 1.4 2005/08/02 19:35:48 jonathan Exp $ * Jonathan Ledlie, Harvard University. * Copyright 2005. All rights reserved. */ #include #include #ifdef LINUX #include #endif #include #include #include #include #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': 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.) { printUsage ("Problem with up pct."); } break; case 's': seed = atoi (optarg); break; case 'b': beginRecord = atoi (optarg); break; case 'e': endRecord = atoi (optarg); break; case 'n': 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) { ; } else { round = (int)(lifetimeDist->next()); } bool beenBorn = false; 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); }