inline static void *Execute(void* Arg) {
    HStackThreadState *th_state;
    long i, rnum;
    volatile int j;
    long id = (long) Arg;

    fastRandomSetSeed(id + 1);
    th_state = getAlignedMemory(CACHE_LINE_SIZE, sizeof(HStackThreadState));
    HStackThreadStateInit(&object_struct, th_state, (int)id);
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();

    for (i = 0; i < RUNS; i++) {
        // perform a push operation
        HStackPush(&object_struct, th_state, id, id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ; 
        // perform a pop operation
        HStackPop(&object_struct, th_state, id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
inline static void *Execute(void* Arg) {
    MSQueueThreadState *th_state;
    long i;
    long id = (long) Arg;
    long rnum;
    volatile long j;

    th_state = getAlignedMemory(CACHE_LINE_SIZE, sizeof(MSQueueThreadState));
    MSQueueThreadStateInit(th_state, MIN_BAK, MAX_BAK);
    fastRandomSetSeed(id + 1);
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();

    for (i = 0; i < RUNS; i++) {
        MSQueueEnqueue(&queue, th_state, id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
        MSQueueDequeue(&queue, th_state);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
inline static void *Execute(void* Arg) {
    long i, rnum, mybank;
    volatile long j;
    long id = (long) Arg;
    volatile ToggleVector lactive_set;
    ToggleVector mystate;

    fastRandomSetSeed(id + 1);
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();
    TVEC_SET_ZERO(&mystate);
    TVEC_SET_BIT(&mystate, id);
    mybank = TVEC_GET_BANK_OF_BIT(id);
    for (i = 0; i < RUNS; i++) {
        mystate = TVEC_NEGATIVE(mystate);
        TVEC_ATOMIC_ADD_BANK(&active_set, &mystate, mybank);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
        lactive_set = active_set;
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
Edge chooseMove(UnscoredState state, Strategy strategy, int turnTimeMillis) {
    assert(turnTimeMillis > 0);

    Edge moveChoice = NO_EDGE;

    unsigned long long startTime = getTimeMillis();

    switch(strategy) {
    case RANDOM_MOVE:
        moveChoice =  getRandomMove(&state);
        break;
    case FIRST_BOX_COMPLETING_MOVE:
    {
        Edge move = getFirstBoxCompletingMove(&state);

        if (move != NO_EDGE)
            moveChoice = move;
        else
            moveChoice = getRandomMove(&state);

    }
    break;
    case MONTE_CARLO:
        moveChoice = getMCTSMove(&state, turnTimeMillis, false);
        break;
    case GMCTS:
        moveChoice = getGMCTSMove(&state, turnTimeMillis);
        break;
    case ALPHA_BETA:
        moveChoice = getABMove(&state, 7, false);
        break;
    case GRAPHS:
        moveChoice = getGraphsMove(&state);
        break;
    case DEEPBOX:
        moveChoice = getDeepBoxMove(&state, turnTimeMillis);
        break;
    }

    unsigned long long endTime = getTimeMillis();
    unsigned long long timeTaken = endTime - startTime;
    log_log("chooseMove: Chose %d. Time taken to choose: %llu.\n", moveChoice, timeTaken);

    return moveChoice; // Avoid compiler warnings.
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
void CommonTimeBase::calculateOffset( int _serverTimeMillis )
{
	int tmpPingTime = 0;
	
	// Don't do anything statistical until we have enough readings to do so,
	// currently waiting until we have half of the amount of values we want
	if( pingMillis.size() > (PING_AMOUNT_TO_KEEP/2) )
	{
		// grad the best X percent of values, get the average and use that
		float percentile = 0.2f;
		int percentileIndex = pingMillis.size() * percentile;
		int tmpIndex = 0;
		int tmpPingAccumulator = 0;
		
		// this is clunky and needs to be re-written
		multiset<int>::iterator tmpIt;
		for ( tmpIt = pingMillis.begin(); tmpIt != pingMillis.end(); tmpIt++)
		{
			if( tmpIndex < percentileIndex )
			{
				tmpPingAccumulator += *tmpIt;
			}
			tmpIndex++;
		}
		
		tmpPingTime = tmpPingAccumulator / percentileIndex;
		
		//cout << "We are assuming an average ping time of: " << tmpPingTime << endl;
	}
	else
	{
		// we just grab the best ping time
		multiset<int>::iterator tmpIt = pingMillis.begin();
		tmpPingTime = *tmpIt;
	}
	
	// Todo: Seems more logical here to check against the lowest percentile,
	// if the ping is below, we can use it to adjust the clock.
	
	int currServerTime = _serverTimeMillis + tmpPingTime;
	
	// if the clock seems to be off by a certain threshold
	int tmpDiffPrevServerTime = currServerTime - getTimeMillis();
	if( abs(tmpDiffPrevServerTime) > maxDiffAdjustmentThreshold )
	{
		offsetMillisTarget = currServerTime - getInternalTimeMillis();
	}
	else
	{
		cout << "We weren't off by that much, so no need to adjust, " << tmpDiffPrevServerTime << endl;
	}
	
	// if we only have one entry in the pingMillis multiset, we just set the offset straight away, easing or no easing
	if( pingMillis.size() <= 1) offsetMillis = offsetMillisTarget;
}
int main(void) {
    lhead = CLHLockInit();
    BarrierInit(&bar, N_THREADS);
    StartThreadsN(N_THREADS, Execute, _DONT_USE_UTHREADS_);
    JoinThreadsN(N_THREADS);
    d2 = getTimeMillis();

    printf("time: %d (ms)\tthroughput: %.2f (millions ops/sec)\t", (int) (d2 - d1), 2*RUNS*N_THREADS/(1000.0*(d2 - d1)));
    printStats(N_THREADS);
    return 0;
}
int main(int argc, char *argv[]) {
    TVEC_SET_ZERO((ToggleVector *)&active_set);
    BarrierInit(&bar, N_THREADS);
    StartThreadsN(N_THREADS, Execute, _DONT_USE_UTHREADS_);
    JoinThreadsN(N_THREADS);
    d2 = getTimeMillis();

    printf("time: %d (ms)\tthroughput: %.2f (millions ops/sec)\t", (int) (d2 - d1), 2*RUNS*N_THREADS/(1000.0*(d2 - d1)));
    printStats(N_THREADS);

    return 0;
}
int main(void) {
    SHARED_OBJECT_INIT();
    pthread_spin_init(&lock, PTHREAD_PROCESS_SHARED);
    BarrierInit(&bar, N_THREADS);
    StartThreadsN(N_THREADS, Execute, _USE_UTHREADS_);
    JoinThreadsN(N_THREADS);
    d2 = getTimeMillis();

    printf("time: %d (ms)\tthroughput: %.2f (millions ops/sec)\t", (int) (d2 - d1), RUNS*N_THREADS/(1000.0*(d2 - d1)));
    printStats(N_THREADS);

    return 0;
}
int main(void) {
    CCStackInit(&object_struct);
    BarrierInit(&bar, N_THREADS);
    StartThreadsN(N_THREADS, Execute, _DONT_USE_UTHREADS_);
    JoinThreadsN(N_THREADS);
    d2 = getTimeMillis();

    printf("time: %d (ms)\tthroughput: %.2f (millions ops/sec)\t", (int) (d2 - d1), 2*RUNS*N_THREADS/(1000.0*(d2 - d1)));
    printStats(N_THREADS);

#ifdef DEBUG
    fprintf(stderr, "object state:    counter: %d rounds: %d\n", object_struct.object_struct.counter, object_struct.object_struct.rounds);
#endif

    return 0;
}
int main(void) {
    SHARED_OBJECT_INIT();
    BarrierInit(&bar, N_THREADS);
    StartThreadsN(N_THREADS, Execute, _DONT_USE_UTHREADS_);
    JoinThreadsN(N_THREADS);
    d2 = getTimeMillis();

    printf("time: %d (ms)\tthroughput: %.2f (millions ops/sec)\t", (int) (d2 - d1), RUNS*N_THREADS/(1000.0*(d2 - d1)));
    printStats(N_THREADS);
#ifdef DEBUG
    fprintf(stderr, "object counter: %d\n", object_lock.counter);
    fprintf(stderr, "rounds: %d\n", object_lock.rounds);
    fprintf(stderr, "Average helping: %f\n", (float)object_lock.counter/object_lock.rounds);
#endif

    return 0;
}
inline static void *Execute(void* Arg) {
    long i, rnum;
    volatile int j;
    long id = (long) Arg;

    fastRandomSetSeed(id + 1);
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();

    for (i = 0; i < RUNS; i++) {
        pthread_spin_lock(&lock);
        object = object + 1;
        pthread_spin_unlock(&lock);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
inline static void *Execute(void* Arg) {
    SimThreadState *th_state;
    long i, rnum;
    long id = (long) Arg;
    volatile long j;

    th_state = getAlignedMemory(CACHE_LINE_SIZE, sizeof(SimThreadState));
	SimThreadStateInit(th_state, id);
    fastRandomSetSeed((unsigned long)id + 1);
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();

    for (i = 0; i < RUNS; i++) {
        SimApplyOp(sim_struct, th_state, fetchAndMultiply, (Object) (id + 1), id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
inline static void *Execute(void* Arg) {
    OyamaThreadState *th_state;
    long i, rnum;
    volatile int j;
    long id = (long) Arg;

    fastRandomSetSeed(id + 1);
    th_state = getAlignedMemory(CACHE_LINE_SIZE, sizeof(OyamaThreadState));
	OyamaThreadStateInit(th_state);
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();

    for (i = 0; i < RUNS; i++) {
        // perform a fetchAndMultiply operation
        OyamaApplyOp((OyamaStruct *)&object_lock, th_state, fetchAndMultiply, (ArgVal) id, id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
void ProvenanceRepository::run() {
  // threshold for purge
  uint64_t purgeThreshold = max_partition_bytes_ * 3 / 4;
  while (running_) {
    std::this_thread::sleep_for(std::chrono::milliseconds(purge_period_));
    std::this_thread::sleep_for(std::chrono::milliseconds(purge_period_));
    uint64_t curTime = getTimeMillis();
    uint64_t size = repoSize();
    if (size >= purgeThreshold) {
      std::vector<std::string> purgeList;
      leveldb::Iterator* it = db_->NewIterator(leveldb::ReadOptions());
      for (it->SeekToFirst(); it->Valid(); it->Next()) {
        ProvenanceEventRecord eventRead;
        std::string key = it->key().ToString();
        if (eventRead.DeSerialize(reinterpret_cast<uint8_t*>(const_cast<char*>(it->value().data())), it->value().size())) {
          if ((curTime - eventRead.getEventTime()) > max_partition_millis_)
            purgeList.push_back(key);
        } else {
          logger_->log_debug("NiFi Provenance retrieve event %s fail", key.c_str());
          purgeList.push_back(key);
        }
      }
      delete it;
      std::vector<std::string>::iterator itPurge;

      for (itPurge = purgeList.begin(); itPurge != purgeList.end(); itPurge++) {
        std::string eventId = *itPurge;
        logger_->log_info("ProvenanceRepository Repo Purge %s", eventId.c_str());
        Delete(eventId);
      }
    }
    if (size > max_partition_bytes_)
      repo_full_ = true;
    else
      repo_full_ = false;
  }
  return;
}
inline static void *Execute(void* Arg) {
    long i;
    long rnum;
    long id = (long) Arg;
    volatile int j;

    fastRandomSetSeed(id + 1);
    init_pool(&pool_node, sizeof(ListNode));
    BarrierWait(&bar);
    if (id == 0)
        d1 = getTimeMillis();

    for (i = 0; i < RUNS; i++) {
        push((Object)1, id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
        pop(id);
        rnum = fastRandomRange(1, MAX_WORK);
        for (j = 0; j < rnum; j++)
            ;
    }
    return NULL;
}
FlowFile::FlowFile()
    : size_(0),
      id_(0),
      stored(false),
      offset_(0),
      last_queue_date_(0),
      penaltyExpiration_ms_(0),
      event_time_(0),
      claim_(nullptr),
      marked_delete_(false),
      connection_(nullptr),
      original_connection_(),
      logger_(logging::LoggerFactory<FlowFile>::getLogger()) {
  entry_date_ = getTimeMillis();
  lineage_start_date_ = entry_date_;

  char uuidStr[37];

  // Generate the global UUID for the flow record
  uuid_generate(uuid_);

  uuid_unparse_lower(uuid_, uuidStr);
  uuid_str_ = uuidStr;
}
Exemple #17
0
int main(int argc, char *argv[]) {	
	leaderCommand = NULL;
	sendHeartBeatMillis = 5000;//send a heartbeat after this many millis
	heartBeatTimeoutMillis = sendHeartBeatMillis * 3;//assume leader is dead if no heartbeat after this long
	heartBeatMsgLength = strlen(heartBeatMsg);
	addrs = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in) * MAX_HOSTS);//using a list here would be more memory efficient
	if(addrs == NULL) {
		fprintf(stderr, "Could not allocate memory\n");
		exit(1);
	}
	parseArgs(argc, argv);
	myIpAndPortStr = (char *)malloc(ipAndPortStrLength);
	fromIpAndPortStr = (char *)malloc(ipAndPortStrLength);

	serverSock = socket(AF_INET, SOCK_DGRAM, 0);
	if (serverSock < 0) {
		fprintf(stderr, "Opening socket\n");
		exit(1);
	}
	struct sockaddr_in serverSockAddr;
	memset(&serverSockAddr, 0, sizeof(serverSockAddr));
	serverSockAddr.sin_family = AF_INET;
	serverSockAddr.sin_addr.s_addr = INADDR_ANY;
	serverSockAddr.sin_port = htons(serverPort);
	if (bind(serverSock, (struct sockaddr *)&serverSockAddr, sizeof(struct sockaddr)) < 0)  {
		perror("Binding socket");
		exit(1);
	}
	setIpAndPortStr(myIpAndPortStr, (struct sockaddr *)&serverSockAddr);

	struct pollfd fd;
	fd.fd = serverSock;
	fd.events = POLLIN;

	int isLeader = 0;
	leaderChanged(0);//initialize not leader

	while(1) {
		if(isLeader) {
			//send heartbeats unless we receive a heartbeat from someone else, then assume not leader
			long lastHeartbeatSentMillis = 0;
			long pollTimeout;
			int pollResult;
			while(1) {			
				//http://linux.die.net/man/2/poll	
				pollTimeout = lastHeartbeatSentMillis + sendHeartBeatMillis - getTimeMillis();				
				if(pollTimeout <= 0) {
					pollTimeout = sendHeartBeatMillis;
				}
				pollResult = poll(&fd, 1, (int)pollTimeout);
				if(pollResult < 0) {
					perror("IsLeader poll error");
				} else if(pollResult == 0) {
					sendHeartbeat();			
					lastHeartbeatSentMillis = getTimeMillis();
				} else {
					if(fd.revents & POLLIN) {
						if(recvHeartbeat()) {
							//someone else thinks they are the leader, compare from ip+port to me, whichever sorts lower is the leader
							//if i am the leader, immediately send a heartbeat msg, which will tell the other leader to not be the leader
							//since it will get to this same spot, and by comparing ip+port determine that it is not the leader
							setIpAndPortStr(fromIpAndPortStr, (struct sockaddr *)&from);
							if(strcmp(myIpAndPortStr, fromIpAndPortStr) < 0) {
								isLeader = 0;
								leaderChanged(isLeader);
								sendHeartbeat();
							}
						}						
						break;
					} else if(fd.revents & POLLERR) {
						//last message errored, what to do?
					}
				}
			}
		} else {
			long lastHeartbeatReceivedMillis = 0;
			int pollResult;
			long pollTimeout;
			while(1) {
				//listen for heartbeats, if don't hear heartbeat within x sec, assume self is leader
				pollTimeout = lastHeartbeatReceivedMillis + heartBeatTimeoutMillis - getTimeMillis();
				if(pollTimeout <= 0) {
					pollTimeout = heartBeatTimeoutMillis;
				}
				pollResult = poll(&fd, 1, (int)pollTimeout);
				if(pollResult < 0) {
					perror("IsNotLeader poll error");
				} else if(pollResult == 0) {
					isLeader = 1;
					leaderChanged(isLeader);
					sendHeartbeat();
					break;
					//no heartbeats received from leader, take over leadership
					//TODO random delay to prevent slave storm to take over leadership?
				} else {
					if(fd.revents & POLLIN) {
						if(recvHeartbeat()) {
							lastHeartbeatReceivedMillis = getTimeMillis();
						}
					}
				}		
			}		
		}
	}
	return 0;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------
//
float CommonTimeBase::getTimeSecs()
{
	return getTimeMillis() / 1000.0f;
}