virtual void RenderSceneCB() { long long TimeNowMillis = GetCurrentTimeMillis(); assert(TimeNowMillis >= m_currentTimeMillis); unsigned int DeltaTimeMillis = (unsigned int)(TimeNowMillis - m_currentTimeMillis); m_currentTimeMillis = TimeNowMillis; m_pGameCamera->OnRender(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_pLightingTechnique->Enable(); m_pTexture->Bind(COLOR_TEXTURE_UNIT); m_pNormalMap->Bind(NORMAL_TEXTURE_UNIT); Pipeline p; p.Scale(20.0f, 20.0f, 1.0f); p.Rotate(90.0f, 0.0, 0.0f); p.SetCamera(m_pGameCamera->GetPos(), m_pGameCamera->GetTarget(), m_pGameCamera->GetUp()); p.SetPerspectiveProj(m_persProjInfo); m_pLightingTechnique->SetWVP(p.GetWVPTrans()); m_pLightingTechnique->SetWorldMatrix(p.GetWorldTrans()); m_pGround->Render(); m_particleSystem.Render(DeltaTimeMillis, p.GetVPTrans(), m_pGameCamera->GetPos()); glutSwapBuffers(); }
virtual void log(const char * level, const char * msg, va_list args) { TimeCheckedGuardedArea guard(_mutex, "logger"); if (!f) return; #ifdef LINUX struct timeval tval; gettimeofday(&tval, NULL); int ms = tval.tv_usec; time_t t = tval.tv_sec; #if LOG_HEAP_USAGE struct mallinfo mi = mallinfo(); int memusage = mi.arena; #endif #else unsigned __int64 ts = GetCurrentTimeMillis(); //time_t t = (time_t)time(0); time_t t = ts / 1000; int ms = (ts % 1000) * 1000; #if LOG_HEAP_USAGE int memusage = 0; #endif #endif tm * bt = localtime(&t); #if LOG_HEAP_USAGE fprintf(f, "%04d/%02d/%02d %02d:%02d:%02d.%04d [%d] %s ", bt->tm_year + 1900, bt->tm_mon + 1, bt->tm_mday, bt->tm_hour, bt->tm_min, bt->tm_sec, ms / 100, memusage, level); #else fprintf(f, "%04d/%02d/%02d %02d:%02d:%02d.%04d %s ", bt->tm_year + 1900, bt->tm_mon + 1, bt->tm_mday, bt->tm_hour, bt->tm_min, bt->tm_sec, ms / 100, level); #endif vfprintf(f, msg, args); fprintf(f, "\n"); if (autoFlush) fflush(f); }
void ThreadPool::RemoveIdleThreads() { kpr::ScopedLock<kpr::Monitor> lock(*this); if (m_maxIdleTime == 0) { return; } unsigned long long time =GetCurrentTimeMillis(); int interval = (int)(time - m_lastRemoveIdleThreadsTime); m_lastRemoveIdleThreadsTime = time; std::list<ThreadPoolWorker_var>::iterator it = m_workers.begin(); int size = (int)m_workers.size(); while (size > m_minCount && it != m_workers.end()) { if ((*it)->IdleTime(interval) > m_maxIdleTime) { (*it) -> Stop(); size--; } it++; } }
void TimerThread::Run() { unsigned long long lastCheckTime = GetCurrentTimeMillis(); unsigned long long currentCheckTime = lastCheckTime; while (!m_closed) { currentCheckTime = GetCurrentTimeMillis(); unsigned int elapse = (unsigned int)(currentCheckTime - lastCheckTime); std::list<TimerInfo> timeList; CheckTimeOut(elapse,timeList); if (!timeList.empty()) { std::list<TimerInfo>::iterator it = timeList.begin(); for (; it != timeList.end(); it++) { it->pTimerHandler->OnTimeOut(it->id); } } unsigned long long checkEndTime = GetCurrentTimeMillis(); int sleepTime = m_checkInterval - (int)(checkEndTime -currentCheckTime); if (sleepTime < 0) { sleepTime = 0; } lastCheckTime = currentCheckTime; try { kpr::ScopedLock<kpr::Monitor> lock(*this); Wait(sleepTime); } catch (...) { } } }
OgldevApp::OgldevApp() #ifndef WIN32 : m_fontRenderer(sMarkup) #endif { m_frameCount = 0; m_frameTime = 0; m_fps = 0; m_frameTime = m_startTime = GetCurrentTimeMillis(); }
ProcessQueue::ProcessQueue() { m_queueOffsetMax = 0L; m_msgCount=0; m_dropped = false; m_locked = false; m_lastLockTimestamp = GetCurrentTimeMillis(); m_consuming = false; }
void OgldevApp::CalcFPS() { m_frameCount++; long long time = GetCurrentTimeMillis(); if (time - m_frameTime >= 1000) { m_frameTime = time; m_fps = m_frameCount; m_frameCount = 0; } }
ThreadPool:: ThreadPool (int count, int minCount, int maxCount, int step, int maxIdleTime, int checkldleThreadsInterval) { m_destroy = false; m_minCount = minCount; m_maxCount = maxCount; m_maxIdleTime = maxIdleTime; m_count = 0; m_step = step; m_index = 0; m_lastRemoveIdleThreadsTime = GetCurrentTimeMillis(); if (m_minCount <= 0) { m_minCount = MIN_THREAD_COUNT; } if (m_maxCount < 0) { m_maxCount = MAX_THREAD_COUNT; } if (m_maxIdleTime < 0) { m_maxIdleTime = MAX_IDLE_THREAD_TIME; } if (m_maxCount != 0 && m_maxCount < m_minCount) { m_minCount = MIN_THREAD_COUNT; } if ((m_maxCount != 0 && count > m_maxCount) || count < m_minCount) { count = m_minCount; } if (checkldleThreadsInterval < 0) { checkldleThreadsInterval = CHECK_IDLE_THREADS_INTERVAL; } AddThreads(count); m_manager = new ThreadPoolManage(this,checkldleThreadsInterval); m_manager->Start(); }
bool RebalanceImpl::lock(MessageQueue& mq) { FindBrokerResult findBrokerResult = m_pMQClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), MixAll::MASTER_ID, true); if (!findBrokerResult.brokerAddr.empty()) { LockBatchRequestBody* requestBody = new LockBatchRequestBody(); requestBody->setConsumerGroup(m_consumerGroup); requestBody->setClientId(m_pMQClientFactory->getClientId()); requestBody->getMqSet().insert(mq); try { std::set<MessageQueue> lockedMq = m_pMQClientFactory->getMQClientAPIImpl()->lockBatchMQ( findBrokerResult.brokerAddr, requestBody, 1000); std::set<MessageQueue>::iterator it = lockedMq.begin(); for (; it != lockedMq.end(); it++) { kpr::ScopedLock<kpr::Mutex> lock(m_processQueueTableLock); MessageQueue mmqq = *it; std::map<MessageQueue, ProcessQueue*>::iterator itt = m_processQueueTable.find(mmqq); if (itt != m_processQueueTable.end()) { itt->second->setLocked(true); itt->second->setLastLockTimestamp(GetCurrentTimeMillis()); } } it = lockedMq.find(mq); if (it != lockedMq.end()) { return true; } //TODO log.info("the message queue lock {}, {} {}",// // (lockOK ? "OK" : "Failed"), // // this.consumerGroup, // // mq); return false; } catch (...) { //TODO log.error("lockBatchMQ exception, " + mq, e); } } return false; }
bool Mutex::AwaitImpl(const Condition &cond, int64 end_time) { // end_time is milliseconds since the epoch. A value of zero indicates no // timeout. while (!cond.Eval() && (end_time == 0 || GetCurrentTimeMillis() < end_time)) { Unlock(); // Yield the rest of our CPU timeslice before reacquiring the lock. // Otherwise we'll spin pointlessly here, hurting performance. // (The Condition cannot possibly change when no other thread runs.) ThreadYield(); Lock(); } return cond.Eval(); }
Tutorial28() { m_pLightingTechnique = NULL; m_pGameCamera = NULL; m_pGround = NULL; m_pTexture = NULL; m_pNormalMap = NULL; m_dirLight.AmbientIntensity = 0.2f; m_dirLight.DiffuseIntensity = 0.8f; m_dirLight.Color = Vector3f(1.0f, 1.0f, 1.0f); m_dirLight.Direction = Vector3f(1.0f, 0.0f, 0.0f); m_persProjInfo.FOV = 60.0f; m_persProjInfo.Height = WINDOW_HEIGHT; m_persProjInfo.Width = WINDOW_WIDTH; m_persProjInfo.zNear = 1.0f; m_persProjInfo.zFar = 100.0f; m_currentTimeMillis = GetCurrentTimeMillis(); }
void ConsumeOrderlyRequest::Do() { // 保证在当前Consumer内,同一队列串行消费 kpr::Mutex* objLock = m_pService->getMessageQueueLock().fetchLockObject(*m_pMessageQueue); { kpr::ScopedLock<kpr::Mutex> lock(*objLock); // 保证在Consumer集群,同一队列串行消费 if (m_pProcessQueue->isLocked() || !m_pProcessQueue->isLockExpired()) { long long beginTime = GetCurrentTimeMillis(); for (bool continueConsume = true; continueConsume;) { if (m_pProcessQueue->isDroped()) { //TODO log.info("the message queue not be able to consume, because it's droped {}", // this.messageQueue); break; } if (!m_pProcessQueue->isLocked()) { //TODO log.warn("the message queue not locked, so consume later, {}", this.messageQueue); //ConsumeMessageOrderlyService.this.tryLockLaterAndReconsume(this.messageQueue, // this.processQueue, 10); break; } if (m_pProcessQueue->isLockExpired()) { //TODO log.warn("the message queue lock expired, so consume later, {}",this.messageQueue); //ConsumeMessageOrderlyService.this.tryLockLaterAndReconsume(this.messageQueue, // this.processQueue, 10); break; } // 在线程数小于队列数情况下,防止个别队列被饿死 long interval = long (GetCurrentTimeMillis() - beginTime); if (interval > ConsumeMessageOrderlyService::s_MaxTimeConsumeContinuously) { // 过10ms后再消费 m_pService->submitConsumeRequestLater(m_pProcessQueue, m_pMessageQueue, 10); break; } int consumeBatchSize = m_pService->getDefaultMQPushConsumer()->getConsumeMessageBatchMaxSize(); std::list<MessageExt*> msgs = m_pProcessQueue->takeMessags(consumeBatchSize); if (!msgs.empty()) { ConsumeOrderlyContext context(*m_pMessageQueue); ConsumeOrderlyStatus status = SUSPEND_CURRENT_QUEUE_A_MOMENT; // 执行Hook ConsumeMessageContext consumeMessageContext; if (m_pService->getDefaultMQPushConsumerImpl()->hasHook()) { consumeMessageContext.consumerGroup = m_pService->getConsumerGroup(); consumeMessageContext.mq = *m_pMessageQueue; consumeMessageContext.msgList = msgs; consumeMessageContext.success = false; m_pService->getDefaultMQPushConsumerImpl()->executeHookBefore(consumeMessageContext); } long long beginTimestamp = GetCurrentTimeMillis(); try { status = m_pService->getMessageListener()->consumeMessage(msgs, context); } catch (...) { // TODO log.warn("consumeMessage exception: {} Group: {} Msgs: {} MQ: {}",// // RemotingHelper.exceptionSimpleDesc(e),// // ConsumeMessageOrderlyService.this.consumerGroup,// // msgs,// // messageQueue); } long consumeRT = long (GetCurrentTimeMillis() - beginTimestamp); // 用户抛出异常或者返回null,都挂起队列 //if (null == status) { // status = ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; //} // 执行Hook if (m_pService->getDefaultMQPushConsumerImpl()->hasHook()) { consumeMessageContext.success = (SUCCESS == status || COMMIT == status); m_pService->getDefaultMQPushConsumerImpl()->executeHookAfter(consumeMessageContext); } // 记录统计信息 m_pService->getConsumerStat().consumeMsgRTTotal.fetchAndAdd(consumeRT); MixAll::compareAndIncreaseOnly(m_pService->getConsumerStat() .consumeMsgRTMax, consumeRT); continueConsume = m_pService->processConsumeResult(&msgs, status, context, *this); } else { continueConsume = false; } } } // 没有拿到当前队列的锁,稍后再消费 else { m_pService->tryLockLaterAndReconsume(m_pMessageQueue, m_pProcessQueue, 10); } } delete this; }
mtime_t BotTime::GetUptime() { return GetCurrentTimeMillis() - launchtime; }
bool Mutex::AwaitWithTimeout(const Condition &cond, int timeout_milliseconds) { assert(timeout_milliseconds > 0); return AwaitImpl(cond, GetCurrentTimeMillis() + timeout_milliseconds); }
bool ProcessQueue::isLockExpired() { bool result = (GetCurrentTimeMillis() - m_lastLockTimestamp) > s_RebalanceLockMaxLiveTime; return result; }
float OgldevApp::GetRunningTime() { float RunningTime = (float)((double)GetCurrentTimeMillis() - (double)m_startTime) / 1000.0f; return RunningTime; }
void RebalanceImpl::lockAll() { std::map<std::string, std::set<MessageQueue> > brokerMqs = buildProcessQueueTableByBrokerName(); std::map<std::string, std::set<MessageQueue> >::iterator it = brokerMqs.begin(); for (;it != brokerMqs.end();it++) { std::string brokerName = it->first; std::set<MessageQueue> mqs = it->second; if (mqs.empty()) { continue; } FindBrokerResult findBrokerResult = m_pMQClientFactory->findBrokerAddressInSubscribe(brokerName, MixAll::MASTER_ID, true); if (!findBrokerResult.brokerAddr.empty()) { LockBatchRequestBody* requestBody = new LockBatchRequestBody(); requestBody->setConsumerGroup(m_consumerGroup); requestBody->setClientId(m_pMQClientFactory->getClientId()); requestBody->setMqSet(mqs); try { std::set<MessageQueue> lockOKMQSet = m_pMQClientFactory->getMQClientAPIImpl()->lockBatchMQ( findBrokerResult.brokerAddr, requestBody, 1000); // 锁定成功的队列 std::set<MessageQueue>::iterator its = lockOKMQSet.begin(); for (;its != lockOKMQSet.end();its++) { MessageQueue mq = *its; std::map<MessageQueue, ProcessQueue*>::iterator itt = m_processQueueTable.find(mq); if (itt != m_processQueueTable.end()) { ProcessQueue* processQueue = itt->second; if (!processQueue->isLocked()) { //TODO log.info("the message queue locked OK, Group: {} {}", this.consumerGroup, mq); } processQueue->setLocked(true); processQueue->setLastLockTimestamp(GetCurrentTimeMillis()); } } // 锁定失败的队列 its = mqs.begin(); for (;its != mqs.end();its++) { MessageQueue mq = *its; std::set<MessageQueue>::iterator itf = lockOKMQSet.find(mq); if (itf == lockOKMQSet.end()) { std::map<MessageQueue, ProcessQueue*>::iterator itt = m_processQueueTable.find(mq); if (itt != m_processQueueTable.end()) { itt->second->setLocked(false); //TODO log.warn("the message queue locked Failed, Group: {} {}", this.consumerGroup, // mq); } } } } catch (...) { //TODO log.error("lockBatchMQ exception, " + mqs, e); } } } }
int main(int argc, char* argv[]) { if (argc<2) { Usage(argv[0]); return 0; } int count = 1000; int size = 32; for (int i=2; i< argc; i++) { if (strcmp(argv[i],"-n")==0) { if (i+1 < argc) { count = atoi(argv[i+1]); i++; } else { Usage(argv[0]); return 0; } } else if (strcmp(argv[i],"-v")==0) { if (i+1 < argc) { size = atoi(argv[i+1]); i++; } else { Usage(argv[0]); return 0; } } else { Usage(argv[0]); return 0; } } DefaultMQProducer producer("please_rename_unique_group_name"); producer.setNamesrvAddr(argv[1]); producer.start(); std::string tags[] = { "TagA", "TagB", "TagC", "TagD", "TagE" }; char key[16]; char* value = new char[size]; strcpy(value,"Hello RocketMQ"); for (int i=14;i<size-8;i++) { char ch; switch (i%3) { case 0: ch='a'; break; case 1: ch='b'; break; case 2: default: ch='c'; break; } *(value+i) = ch; } long long begin = GetCurrentTimeMillis(); int success=0; for (int i = 0; i < count; i++) { try { sprintf(key,"KEY%d",i); sprintf(value + size -8,"%d",i); Message msg("TopicTest",// topic tags[i % 5],// tag key,// key value,// body strlen(value)+1); SendResult sendResult = producer.send(msg); if (sendResult.getSendStatus() == SEND_OK) { success++; } } catch (MQClientException& e) { std::cout<<e<<std::endl; MySleep(1000); } } long long end = GetCurrentTimeMillis(); int time = end - begin; int tps = success*1.0/time*1000.;; printf("success=%d,time=%d,tps=%d\n",success,time,tps); producer.shutdown(); return 0; }