Exemplo n.º 1
0
//时间回滚了 return 0
//否则,return other
uint64_t idworker::next_id()
{
    uint64_t timestamp = timeGen();
    if (lastTimestamp == timestamp)
    {
        
        sequence = (sequence + 1) & sequenceMask;
        if (sequence == 0)
        {
            //printf("########### %llu\n" , sequenceMask);
            timestamp = tilNextMillis(lastTimestamp);
        }
    }
    else
    {
        sequence = 0;
    }
    if (timestamp < lastTimestamp)
    {
        return 0;
    }

    lastTimestamp = timestamp;
    uint64_t nextId = ((timestamp - twepoch << timestampLeftShift))
            | (worker_id << workerIdShift) | (sequence);

    //printf( "timestamp:%llu ,timestampLeftShift:%llu,nextId:%llu,workerId:%llu,sequence:%llu\n"
      //   , timestamp,timestampLeftShift,nextId,worker_id,sequence);
    return nextId;
}
Exemplo n.º 2
0
//Time generation for events
long double timeGen(double rate, bool choice)
{
  double u;
  double alpha = 1;  // subject to change
//  srand48(time(NULL));
  u = drand48();
  if( choice == true )	//exponential
  {
    return((-1/rate)*log(1-u));
  } else {		//pareto
    double x_m = -log(.9)/rate;
    return( x_m * exp( timeGen(rate, true)));
  }
}
Exemplo n.º 3
0
Packet_List::Packet_List(int size, double rate, int c)
{
  if( c  == 1) //Choose distribution
  { choice = false; } else { choice = true; }
  Packet_Node *temp; // used for pointers
  Packet_Node *temp_new; //Used for new
  double arrival = timeGen(rate, choice);  //Arrival time
  double arrival2;
  first = new Packet_Node( arrival, timeGen(1, true) );//first arrival
  temp = first;
  for(int i = 1; i < size; i++ )	//Rest of the arrivals
  { 
    arrival2 = timeGen(rate, choice);
    arrival += arrival2;
    temp_new = new Packet_Node( arrival, timeGen(1, true));
//if(arrival2 < temp_new->getService())
//{cout << i << "        " << arrival2 << "         "  << temp_new->getService() << endl;}

    temp->setNext( temp_new );
    temp = temp_new;
    temp_new = NULL;
  }
}