lltimer.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:13k
- /**
- * @file lltimer.cpp
- * @brief Cross-platform objects for doing timing
- *
- * $LicenseInfo:firstyear=2000&license=viewergpl$
- *
- * Copyright (c) 2000-2010, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- #include "linden_common.h"
- #include "lltimer.h"
- #include "u64.h"
- #if LL_WINDOWS
- # define WIN32_LEAN_AND_MEAN
- # include <winsock2.h>
- # include <windows.h>
- #elif LL_LINUX || LL_SOLARIS || LL_DARWIN
- # include <errno.h>
- # include <sys/time.h>
- #else
- # error "architecture not supported"
- #endif
- //
- // Locally used constants
- //
- const U32 SEC_PER_DAY = 86400;
- const F64 SEC_TO_MICROSEC = 1000000.f;
- const U64 SEC_TO_MICROSEC_U64 = 1000000;
- const F64 USEC_TO_SEC_F64 = 0.000001;
- //---------------------------------------------------------------------------
- // Globals and statics
- //---------------------------------------------------------------------------
- S32 gUTCOffset = 0; // viewer's offset from server UTC, in seconds
- LLTimer* LLTimer::sTimer = NULL;
- F64 gClockFrequency = 0.0;
- F64 gClockFrequencyInv = 0.0;
- F64 gClocksToMicroseconds = 0.0;
- U64 gTotalTimeClockCount = 0;
- U64 gLastTotalTimeClockCount = 0;
- //
- // Forward declarations
- //
- //---------------------------------------------------------------------------
- // Implementation
- //---------------------------------------------------------------------------
- #if LL_WINDOWS
- void ms_sleep(U32 ms)
- {
- Sleep(ms);
- }
- U32 micro_sleep(U64 us, U32 max_yields)
- {
- // max_yields is unused; just fiddle with it to avoid warnings.
- max_yields = 0;
- ms_sleep(us / 1000);
- return 0;
- }
- #elif LL_LINUX || LL_SOLARIS || LL_DARWIN
- static void _sleep_loop(struct timespec& thiswait)
- {
- struct timespec nextwait;
- bool sleep_more = false;
- do {
- int result = nanosleep(&thiswait, &nextwait);
- // check if sleep was interrupted by a signal; unslept
- // remainder was written back into 't' and we just nanosleep
- // again.
- sleep_more = (result == -1 && EINTR == errno);
- if (sleep_more)
- {
- if ( nextwait.tv_sec > thiswait.tv_sec ||
- (nextwait.tv_sec == thiswait.tv_sec &&
- nextwait.tv_nsec >= thiswait.tv_nsec) )
- {
- // if the remaining time isn't actually going
- // down then we're being shafted by low clock
- // resolution - manually massage the sleep time
- // downward.
- if (nextwait.tv_nsec > 1000000) {
- // lose 1ms
- nextwait.tv_nsec -= 1000000;
- } else {
- if (nextwait.tv_sec == 0) {
- // already so close to finished
- sleep_more = false;
- } else {
- // lose up to 1ms
- nextwait.tv_nsec = 0;
- }
- }
- }
- thiswait = nextwait;
- }
- } while (sleep_more);
- }
- U32 micro_sleep(U64 us, U32 max_yields)
- {
- U64 start = get_clock_count();
- // This is kernel dependent. Currently, our kernel generates software clock
- // interrupts at 250 Hz (every 4,000 microseconds).
- const U64 KERNEL_SLEEP_INTERVAL_US = 4000;
- S32 num_sleep_intervals = (us - (KERNEL_SLEEP_INTERVAL_US >> 1)) / KERNEL_SLEEP_INTERVAL_US;
- if (num_sleep_intervals > 0)
- {
- U64 sleep_time = (num_sleep_intervals * KERNEL_SLEEP_INTERVAL_US) - (KERNEL_SLEEP_INTERVAL_US >> 1);
- struct timespec thiswait;
- thiswait.tv_sec = sleep_time / 1000000;
- thiswait.tv_nsec = (sleep_time % 1000000) * 1000l;
- _sleep_loop(thiswait);
- }
- U64 current_clock = get_clock_count();
- U32 yields = 0;
- while ( (yields < max_yields)
- && (current_clock - start < us) )
- {
- sched_yield();
- ++yields;
- current_clock = get_clock_count();
- }
- return yields;
- }
- void ms_sleep(U32 ms)
- {
- long mslong = ms; // tv_nsec is a long
- struct timespec thiswait;
- thiswait.tv_sec = ms / 1000;
- thiswait.tv_nsec = (mslong % 1000) * 1000000l;
- _sleep_loop(thiswait);
- }
- #else
- # error "architecture not supported"
- #endif
- //
- // CPU clock/other clock frequency and count functions
- //
- #if LL_WINDOWS
- U64 get_clock_count()
- {
- static bool firstTime = true;
- static U64 offset;
- // ensures that callers to this function never have to deal with wrap
- // QueryPerformanceCounter implementation
- LARGE_INTEGER clock_count;
- QueryPerformanceCounter(&clock_count);
- if (firstTime) {
- offset = clock_count.QuadPart;
- firstTime = false;
- }
- return clock_count.QuadPart - offset;
- }
- F64 calc_clock_frequency(U32 uiMeasureMSecs)
- {
- __int64 freq;
- QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
- return (F64)freq;
- }
- #endif // LL_WINDOWS
- #if LL_LINUX || LL_DARWIN || LL_SOLARIS
- // Both Linux and Mac use gettimeofday for accurate time
- F64 calc_clock_frequency(unsigned int uiMeasureMSecs)
- {
- return 1000000.0; // microseconds, so 1 Mhz.
- }
- U64 get_clock_count()
- {
- // Linux clocks are in microseconds
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_sec*SEC_TO_MICROSEC_U64 + tv.tv_usec;
- }
- #endif
- void update_clock_frequencies()
- {
- gClockFrequency = calc_clock_frequency(50U);
- gClockFrequencyInv = 1.0/gClockFrequency;
- gClocksToMicroseconds = gClockFrequencyInv * SEC_TO_MICROSEC;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // returns a U64 number that represents the number of
- // microseconds since the unix epoch - Jan 1, 1970
- U64 totalTime()
- {
- U64 current_clock_count = get_clock_count();
- if (!gTotalTimeClockCount)
- {
- update_clock_frequencies();
- gTotalTimeClockCount = current_clock_count;
- #if LL_WINDOWS
- // Synch us up with local time (even though we PROBABLY don't need to, this is how it was implemented)
- // Unix platforms use gettimeofday so they are synced, although this probably isn't a good assumption to
- // make in the future.
- gTotalTimeClockCount = (U64)(time(NULL) * gClockFrequency);
- #endif
- // Update the last clock count
- gLastTotalTimeClockCount = current_clock_count;
- }
- else
- {
- if (current_clock_count >= gLastTotalTimeClockCount)
- {
- // No wrapping, we're all okay.
- gTotalTimeClockCount += current_clock_count - gLastTotalTimeClockCount;
- }
- else
- {
- // We've wrapped. Compensate correctly
- gTotalTimeClockCount += (0xFFFFFFFFFFFFFFFFULL - gLastTotalTimeClockCount) + current_clock_count;
- }
- // Update the last clock count
- gLastTotalTimeClockCount = current_clock_count;
- }
- // Return the total clock tick count in microseconds.
- return (U64)(gTotalTimeClockCount*gClocksToMicroseconds);
- }
- ///////////////////////////////////////////////////////////////////////////////
- LLTimer::LLTimer()
- {
- if (!gClockFrequency)
- {
- update_clock_frequencies();
- }
- mStarted = TRUE;
- reset();
- }
- LLTimer::~LLTimer()
- {
- }
- // static
- U64 LLTimer::getTotalTime()
- {
- // simply call into the implementation function.
- return totalTime();
- }
- // static
- F64 LLTimer::getTotalSeconds()
- {
- return U64_to_F64(getTotalTime()) * USEC_TO_SEC_F64;
- }
- void LLTimer::reset()
- {
- mLastClockCount = get_clock_count();
- mExpirationTicks = 0;
- }
- ///////////////////////////////////////////////////////////////////////////////
- U64 LLTimer::getCurrentClockCount()
- {
- return get_clock_count();
- }
- ///////////////////////////////////////////////////////////////////////////////
- void LLTimer::setLastClockCount(U64 current_count)
- {
- mLastClockCount = current_count;
- }
- ///////////////////////////////////////////////////////////////////////////////
- static
- U64 getElapsedTimeAndUpdate(U64& lastClockCount)
- {
- U64 current_clock_count = get_clock_count();
- U64 result;
- if (current_clock_count >= lastClockCount)
- {
- result = current_clock_count - lastClockCount;
- }
- else
- {
- // time has gone backward
- result = 0;
- }
- lastClockCount = current_clock_count;
- return result;
- }
- F64 LLTimer::getElapsedTimeF64() const
- {
- U64 last = mLastClockCount;
- return (F64)getElapsedTimeAndUpdate(last) * gClockFrequencyInv;
- }
- F32 LLTimer::getElapsedTimeF32() const
- {
- return (F32)getElapsedTimeF64();
- }
- F64 LLTimer::getElapsedTimeAndResetF64()
- {
- return (F64)getElapsedTimeAndUpdate(mLastClockCount) * gClockFrequencyInv;
- }
- F32 LLTimer::getElapsedTimeAndResetF32()
- {
- return (F32)getElapsedTimeAndResetF64();
- }
- ///////////////////////////////////////////////////////////////////////////////
- void LLTimer::setTimerExpirySec(F32 expiration)
- {
- mExpirationTicks = get_clock_count()
- + (U64)((F32)(expiration * gClockFrequency));
- }
- F32 LLTimer::getRemainingTimeF32() const
- {
- U64 cur_ticks = get_clock_count();
- if (cur_ticks > mExpirationTicks)
- {
- return 0.0f;
- }
- return F32((mExpirationTicks - cur_ticks) * gClockFrequencyInv);
- }
- BOOL LLTimer::checkExpirationAndReset(F32 expiration)
- {
- U64 cur_ticks = get_clock_count();
- if (cur_ticks < mExpirationTicks)
- {
- return FALSE;
- }
- mExpirationTicks = cur_ticks
- + (U64)((F32)(expiration * gClockFrequency));
- return TRUE;
- }
- BOOL LLTimer::hasExpired() const
- {
- return (get_clock_count() >= mExpirationTicks)
- ? TRUE : FALSE;
- }
- ///////////////////////////////////////////////////////////////////////////////
- BOOL LLTimer::knownBadTimer()
- {
- BOOL failed = FALSE;
- #if LL_WINDOWS
- WCHAR bad_pci_list[][10] = {L"1039:0530",
- L"1039:0620",
- L"10B9:0533",
- L"10B9:1533",
- L"1106:0596",
- L"1106:0686",
- L"1166:004F",
- L"1166:0050",
- L"8086:7110",
- L"