void DBConsole::swap() { //## SHOW FPS ##### write("FPS: " + std::to_string((int)getFPS()), BUFFER_W - 10, 0); //## FPS ##### long delta = getCurrentTimeMillis() - lastRenderTime; if (FPS_LIMITER > 0) { while (delta < (1000.0 / (FPS_LIMITER + 3))) { delta = getCurrentTimeMillis() - lastRenderTime; doSystemSleep(0); } } fpsSum += delta; lastRenderTime = getCurrentTimeMillis(); fpsCount++; if (fpsSum > 1000) { fps = 1 / (fpsSum / (fpsCount * 1000.0)); fpsSum = fpsCount = 0; } //## SWAPPING ##### for (int x = 0; x < BUFFER_W; x++) { for (int y = 0; y < BUFFER_H; y++) { if (buffer[x][y] != display[x][y]) { writeToConsole(buffer[x][y], x, y); display[x][y] = buffer[x][y]; } } } }
void InputManager::run() { InputEvent* inputEvent; while (running && eventBuffer != NULL) { eventBuffer->waitEvent(); if (!running) { break; } inputEvent = eventBuffer->getNextEvent(); while (inputEvent != NULL) { if (inputEvent->isPressedType() && ((getCurrentTimeMillis() - timeStamp) > 150)) { timeStamp = getCurrentTimeMillis(); if (!dispatchEvent(inputEvent)) { delete inputEvent; inputEvent = eventBuffer->getNextEvent(); continue; } } dispatchProceduralEvent(inputEvent); delete inputEvent; inputEvent = eventBuffer->getNextEvent(); } } }
int extrapolateMax(unsigned long millis) { int i; int max; unsigned long actualMillis; unsigned int iterations[extrapolationIterations]; unsigned long long times[extrapolationIterations]; unsigned long itStartTime; double multiplier; double power; // takes ~7 seconds to calculate max, so if millis is less than 10s // we're not going anywhere if (millis < 10000) { return 0; } fprintf(stderr, "Calculating extrapolated max...\n"); for (i = 0; i < extrapolationIterations; i++) { iterations[i] = (i * 3) + extrapolationStart; itStartTime = getCurrentTimeMillis(); if (algorithm == FORWARD_ALGORITHM) { algorithmInit(iterations[i]); algorithmNext(1, 1, 3, 1); algorithmDestroy(); } else if (algorithm == REVERSE_ALGORITHM) { reverseInit(iterations[i], 0); reverseNext(1, 1, 3, 1); reverseDestroy(); } times[i] = getCurrentTimeMillis() - itStartTime; } fitExponential(iterations, times, extrapolationIterations, &multiplier, &power); //fprintf(stderr, "Got multiplier = %g, power = %g\n", multiplier, power); // the 0.95 factor here is a safety zone actualMillis = millis - (getCurrentTimeMillis() - startTimeMillis); max = projectedIterationsInMillisExp(actualMillis * 0.95, multiplier, power); //fprintf(stderr, "%lums: %u iterations.\n", actualMillis, max); // sanity check: //for (i = 1; i <= 10; i++) //{ // fprintf(stderr, "Calibration implies %u iterations in %u minutes\n", projectedIterationsInMillisExp(i * 60000 * 0.95, multiplier, power), i); //} return max; }
/** * Allocates a lot of small pieces of memory. */ void evalSmallPieces(){ void * startMemory, *endMemory; int startStatm, endStatm; int t, i; int N = 9000; void * addr[N]; long timeMillis = 0; for(t = 0; t < TIMES; t++){ if(t == 0){ startMemory = getEndHeap(); startStatm = getCurrMemUsage(); } long tmpTime = getCurrentTimeMillis(); for(i = 0; i < N; i++){ addr[i] = malloc(1024); } long timed = (getCurrentTimeMillis()-tmpTime); /*fprintf(stderr, "Time to malloc: %li\n", timed);*/ timeMillis += timed; if(t == 0){ endMemory = getEndHeap(); endStatm = getCurrMemUsage(); } /* for(i = 0; i < N; i++){ free(addr[i]); } */ } timeMillis = timeMillis/TIMES; if(useEndHeap){ int memUsed = getUsedMemoryHeap(startMemory, endMemory); printEvalResults(memUsed, timeMillis); }else{ int memUsed = getUsedMemoryStatm(startStatm, endStatm); printEvalResults(memUsed, timeMillis); } }
/** Allocate a lot of memory, free everything and allocate it again; best fit should give worse performance than first, because first will find a free block right away while best fit will search the whole list every time. */ void evalBadBestFit(){ void * startMemory, *endMemory; int startStatm, endStatm; int i; int N = 9000; void * addr[N]; long timeMillis = 0; startMemory = getEndHeap(); startStatm = getCurrMemUsage(); /* Allocate all N blocks of sizes 1 kB */ long tmpTime = getCurrentTimeMillis(); for(i = 0; i < N; i++){ addr[i] = malloc(1024); } long timed = (getCurrentTimeMillis()-tmpTime); timeMillis += timed; /* Free N blocks */ for(i = 0; i < N; i++){ free(addr[i]); } /* Allocate N blocks again */ tmpTime = getCurrentTimeMillis(); for(i = 0; i < N; i++){ addr[i] = malloc(1024); } timeMillis += (getCurrentTimeMillis()-tmpTime); endMemory = getEndHeap(); endStatm = getCurrMemUsage(); if(useEndHeap){ int memUsed = getUsedMemoryHeap(startMemory, endMemory); printEvalResults(memUsed, timeMillis); }else{ int memUsed = getUsedMemoryStatm(startStatm, endStatm); printEvalResults(memUsed, timeMillis); } }
/** Allocate memory, free some, then allocate some more to evaluate how the algorithms cope with a fragmented list with a lot of free memory. */ void evalFragmentedList(){ void * startMemory, *endMemory; int startStatm, endStatm; int i; int N = 9000; void * addr[N]; long timeMillis = 0; startMemory = getEndHeap(); startStatm = getCurrMemUsage(); /* Allocate all 9000 blocks of sizes 1-30 kB */ long tmpTime = getCurrentTimeMillis(); for(i = 0; i < N; i++){ addr[i] = malloc(sizes[i%30]*1024); } long timed = (getCurrentTimeMillis()-tmpTime); timeMillis += timed; /* Free 1000 blocks (every 9th) */ for(i = 0; i < 1000; i++){ free(addr[i*9]); } /* Allocate 1000 blocks again */ tmpTime = getCurrentTimeMillis(); for(i = 0; i < 1000; i++){ addr[i*9] = malloc(sizes[i%30]*1024); } timeMillis += (getCurrentTimeMillis()-tmpTime); endMemory = getEndHeap(); endStatm = getCurrMemUsage(); if(useEndHeap){ int memUsed = getUsedMemoryHeap(startMemory, endMemory); printEvalResults(memUsed, timeMillis); }else{ int memUsed = getUsedMemoryStatm(startStatm, endStatm); printEvalResults(memUsed, timeMillis); } }
void Player::play() { this->forcedNaturalEnd = false; this->status = PLAY; if (scopeInitTime > 0) { elapsedTime = scopeInitTime * 1000; } else { elapsedTime = 0; } elapsedPause = 0; initTime = getCurrentTimeMillis(); }
double Player::getMediaTime() { double mediaTime; mediaTime = 0; if (status == PAUSE) { mediaTime = elapsedTime; } else { mediaTime = elapsedTime + getCurrentTimeMillis() - initTime - elapsedPause; } return (mediaTime / 1000); }
void TimeStampGenerator::reset() { startTime = getCurrentTimeMillis(); }
VCard32 TimeStampGenerator::getTimeStamp() { return getCurrentTimeMillis() - startTime; }
void Player::resume() { initTime = getCurrentTimeMillis(); elapsedPause = elapsedPause + (initTime - pauseTime); this->status = PLAY; }
void Player::pause() { pauseTime = getCurrentTimeMillis(); elapsedTime = elapsedTime + (pauseTime - initTime); this->status = PAUSE; }
void CGEFrameRecorder::runProc() { //processingFilters 将可能改变 targetTextureID和bufferTextureID, lock 以保证其他线程使用 std::unique_lock<std::mutex> uniqueLock(m_resultMutex); if(m_globalFilter != nullptr) { m_frameHandler->processingWithFilter(m_globalFilter); } m_frameHandler->processingFilters(); if(isRecordingStarted() && !m_isRecordingPaused) { //第一帧必然记录 if(m_recordingTimestamp == 0.0) { m_recordingTimestamp = 0.0001; //设置为 0.0001 ms, 表示已经开始 m_lastRecordingTime = getCurrentTimeMillis(); CGE_LOG_INFO("first frame..."); } else { double currentTime = getCurrentTimeMillis(); m_recordingTimestamp += currentTime - m_lastRecordingTime; m_lastRecordingTime = currentTime; // CGE_LOG_INFO("time stamp %g...", m_recordingTimestamp); } int ptsInFact = m_recordingTimestamp * (m_recordFPS / 1000.0); if(ptsInFact < m_currentPTS) { CGE_LOG_INFO("帧速过快, 丢弃帧..."); return ; } else if(ptsInFact > m_currentPTS + 3) { CGE_LOG_INFO("帧速较慢, 填补帧..."); m_currentPTS = ptsInFact; } else { // CGE_LOG_INFO("帧速合适的很..."); if(m_currentPTS == ptsInFact) m_currentPTS = ptsInFact + 1; else m_currentPTS = ptsInFact; } if(m_recordThread != nullptr) { m_frameHandler->useImageFBO(); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_frameHandler->getBufferTextureID(), 0); glViewport(0, 0, m_dstSize.width, m_dstSize.height); m_cacheDrawer->drawTexture(m_frameHandler->getTargetTextureID()); glFinish(); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_frameHandler->getTargetTextureID(), 0); if(m_recordThread->isActive() && m_recordThread->totalWorks() != 0) return; m_recordThread->run(CGEThreadPool::Work(m_recordingWork, (void*)m_currentPTS)); } else { auto bufferCache = m_recordImageThread->getData4Write(); if(bufferCache.buffer != nullptr) { // auto tm = getCurrentTimeMillis(); m_frameHandler->useImageFBO(); // CGE_LOG_ERROR("draw texture 时间: %g", (getCurrentTimeMillis() - tm)); glReadPixels(0, 0, m_dstSize.width, m_dstSize.height, GL_RGBA, GL_UNSIGNED_BYTE, bufferCache.buffer); // CGE_LOG_ERROR("录制readpixel时间: %g", (getCurrentTimeMillis() - tm)); bufferCache.pts = m_currentPTS; m_recordImageThread->putData4Read(bufferCache); } } } }