PageRenderTime 31ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcommon/lltimer.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 168 lines | 92 code | 30 blank | 46 comment | 9 complexity | ef65eba1aeed8e53c3cc721a93e8db24 MD5 | raw file
  1/** 
  2 * @file lltimer.h
  3 * @brief Cross-platform objects for doing timing 
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_TIMER_H					
 28#define LL_TIMER_H
 29
 30#if LL_LINUX || LL_DARWIN || LL_SOLARIS
 31#include <sys/time.h>
 32#endif
 33#include <limits.h>
 34
 35#include "stdtypes.h"
 36
 37#include <string>
 38#include <list>
 39// units conversions
 40#ifndef USEC_PER_SEC
 41    const U32	USEC_PER_SEC	= 1000000;
 42#endif
 43const U32	SEC_PER_MIN		= 60;
 44const U32	MIN_PER_HOUR	= 60;
 45const U32	USEC_PER_MIN	= USEC_PER_SEC * SEC_PER_MIN;
 46const U32	USEC_PER_HOUR	= USEC_PER_MIN * MIN_PER_HOUR;
 47const U32	SEC_PER_HOUR	= SEC_PER_MIN * MIN_PER_HOUR;
 48const F64 	SEC_PER_USEC 	= 1.0 / (F64) USEC_PER_SEC;
 49
 50class LL_COMMON_API LLTimer 
 51{
 52public:
 53	static LLTimer *sTimer;				// global timer
 54	
 55protected:	
 56	U64 mLastClockCount;
 57	U64 mExpirationTicks;
 58	BOOL mStarted;
 59
 60public:
 61	LLTimer();
 62	~LLTimer();
 63
 64	static void initClass() { if (!sTimer) sTimer = new LLTimer; }
 65	static void cleanupClass() { delete sTimer; sTimer = NULL; }
 66
 67	// Return a high precision number of seconds since the start of
 68	// this application instance.
 69	static F64 getElapsedSeconds()
 70	{
 71		return sTimer->getElapsedTimeF64();
 72	}
 73
 74	// Return a high precision usec since epoch
 75	static U64 getTotalTime();
 76
 77	// Return a high precision seconds since epoch
 78	static F64 getTotalSeconds();
 79
 80
 81	// MANIPULATORS
 82	void start() { reset(); mStarted = TRUE; }
 83	void stop() { mStarted = FALSE; }
 84	void reset();								// Resets the timer
 85	void setLastClockCount(U64 current_count);		// Sets the timer so that the next elapsed call will be relative to this time
 86	void setTimerExpirySec(F32 expiration);
 87	BOOL checkExpirationAndReset(F32 expiration);
 88	BOOL hasExpired() const;
 89	F32 getElapsedTimeAndResetF32();	// Returns elapsed time in seconds with reset
 90	F64 getElapsedTimeAndResetF64();
 91
 92	F32 getRemainingTimeF32() const;
 93
 94	static BOOL knownBadTimer();
 95
 96	// ACCESSORS
 97	F32 getElapsedTimeF32() const;			// Returns elapsed time in seconds
 98	F64 getElapsedTimeF64() const;			// Returns elapsed time in seconds
 99
100	BOOL getStarted() const { return mStarted; }
101
102
103	static U64 getCurrentClockCount();		// Returns the raw clockticks
104};
105
106//
107// Various functions for initializing/accessing clock and timing stuff.  Don't use these without REALLY knowing how they work.
108//
109LL_COMMON_API U64 get_clock_count();
110LL_COMMON_API F64 calc_clock_frequency(U32 msecs);
111LL_COMMON_API void update_clock_frequencies();
112
113// Sleep for milliseconds
114LL_COMMON_API void ms_sleep(U32 ms);
115LL_COMMON_API U32 micro_sleep(U64 us, U32 max_yields = 0xFFFFFFFF);
116
117// Returns the correct UTC time in seconds, like time(NULL).
118// Useful on the viewer, which may have its local clock set wrong.
119LL_COMMON_API time_t time_corrected();
120
121static inline time_t time_min()
122{
123	if (sizeof(time_t) == 4)
124	{
125		return (time_t) INT_MIN;
126	} else {
127#ifdef LLONG_MIN
128		return (time_t) LLONG_MIN;
129#else
130		return (time_t) LONG_MIN;
131#endif
132	}
133}
134
135static inline time_t time_max()
136{
137	if (sizeof(time_t) == 4)
138	{
139		return (time_t) INT_MAX;
140	} else {
141#ifdef LLONG_MAX
142		return (time_t) LLONG_MAX;
143#else
144		return (time_t) LONG_MAX;
145#endif
146	}
147}
148
149// Correction factor used by time_corrected() above.
150extern LL_COMMON_API S32 gUTCOffset;
151
152// Is the current computer (in its current time zone)
153// observing daylight savings time?
154LL_COMMON_API BOOL is_daylight_savings();
155
156// Converts internal "struct tm" time buffer to Pacific Standard/Daylight Time
157// Usage:
158// S32 utc_time;
159// utc_time = time_corrected();
160// struct tm* internal_time = utc_to_pacific_time(utc_time, gDaylight);
161LL_COMMON_API struct tm* utc_to_pacific_time(time_t utc_time, BOOL pacific_daylight_time);
162
163LL_COMMON_API void microsecondsToTimecodeString(U64 current_time, std::string& tcstring);
164LL_COMMON_API void secondsToTimecodeString(F32 current_time, std::string& tcstring);
165
166U64 LL_COMMON_API totalTime();					// Returns current system time in microseconds
167
168#endif