summaryrefslogtreecommitdiff
path: root/indra/llcommon/lltimer.cpp
blob: 23cebf4336783a8b9d5f5362b3dbf06eea91bca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/** 
 * @file lltimer.cpp
 * @brief Cross-platform objects for doing timing 
 *
 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/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((U32)(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
LLUnit<LLUnits::Microseconds, U64> LLTimer::getTotalTime()
{
	// simply call into the implementation function.
	return totalTime();
}	

// static
LLUnit<LLUnits::Seconds, 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;
}


LLUnit<LLUnits::Seconds, F64> LLTimer::getElapsedTimeF64() const
{
	U64 last = mLastClockCount;
	return (F64)getElapsedTimeAndUpdate(last) * gClockFrequencyInv;
}

LLUnit<LLUnits::Seconds, F32> LLTimer::getElapsedTimeF32() const
{
	return (F32)getElapsedTimeF64();
}

LLUnit<LLUnits::Seconds, F64> LLTimer::getElapsedTimeAndResetF64()
{
	return (F64)getElapsedTimeAndUpdate(mLastClockCount) * gClockFrequencyInv;
}

LLUnit<LLUnits::Seconds, F32> LLTimer::getElapsedTimeAndResetF32()
{
	return (F32)getElapsedTimeAndResetF64();
}

///////////////////////////////////////////////////////////////////////////////

void  LLTimer::setTimerExpirySec(F32 expiration)
{
	mExpirationTicks = get_clock_count()
		+ (U64)((F32)(expiration * gClockFrequency));
}

LLUnit<LLUnits::Seconds, 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"\0"
	};

	HKEY hKey = NULL;
	LONG nResult = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,L"SYSTEM\\CurrentControlSet\\Enum\\PCI", 0,
								  KEY_EXECUTE | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey);
	
	WCHAR name[1024];
	DWORD name_len = 1024;
	FILETIME scrap;

	S32 key_num = 0;
	WCHAR pci_id[10];

	wcscpy(pci_id, L"0000:0000");	 /*Flawfinder: ignore*/

	while (nResult == ERROR_SUCCESS)
	{
		nResult = ::RegEnumKeyEx(hKey, key_num++, name, &name_len, NULL, NULL, NULL, &scrap);

		if (nResult == ERROR_SUCCESS)
		{
			memcpy(&pci_id[0],&name[4],4);		/* Flawfinder: ignore */
			memcpy(&pci_id[5],&name[13],4);		/* Flawfinder: ignore */

			for (S32 check = 0; bad_pci_list[check][0]; check++)
			{
				if (!wcscmp(pci_id, bad_pci_list[check]))
				{
//					llwarns << "unreliable PCI chipset found!! " << pci_id << endl;
					failed = TRUE;
					break;
				}
			}
//			llinfo << "PCI chipset found: " << pci_id << endl;
			name_len = 1024;
		}
	}
#endif
	return(failed);
}

///////////////////////////////////////////////////////////////////////////////
// 
// NON-MEMBER FUNCTIONS
//
///////////////////////////////////////////////////////////////////////////////

time_t time_corrected()
{
	return time(NULL) + gUTCOffset;
}


// Is the current computer (in its current time zone)
// observing daylight savings time?
BOOL is_daylight_savings()
{
	time_t now = time(NULL);

	// Internal buffer to local server time
	struct tm* internal_time = localtime(&now);

	// tm_isdst > 0  =>  daylight savings
	// tm_isdst = 0  =>  not daylight savings
	// tm_isdst < 0  =>  can't tell
	return (internal_time->tm_isdst > 0);
}


struct tm* utc_to_pacific_time(time_t utc_time, BOOL pacific_daylight_time)
{
	S32 pacific_offset_hours;
	if (pacific_daylight_time)
	{
		pacific_offset_hours = 7;
	}
	else
	{
		pacific_offset_hours = 8;
	}

	// We subtract off the PST/PDT offset _before_ getting
	// "UTC" time, because this will handle wrapping around
	// for 5 AM UTC -> 10 PM PDT of the previous day.
	utc_time -= pacific_offset_hours * MIN_PER_HOUR * SEC_PER_MIN;
 
	// Internal buffer to PST/PDT (see above)
	struct tm* internal_time = gmtime(&utc_time);

	/*
	// Don't do this, this won't correctly tell you if daylight savings is active in CA or not.
	if (pacific_daylight_time)
	{
		internal_time->tm_isdst = 1;
	}
	*/

	return internal_time;
}


void microsecondsToTimecodeString(U64 current_time, std::string& tcstring)
{
	U64 hours;
	U64 minutes;
	U64 seconds;
	U64 frames;
	U64 subframes;

	hours = current_time / (U64)3600000000ul;
	minutes = current_time / (U64)60000000;
	minutes %= 60;
	seconds = current_time / (U64)1000000;
	seconds %= 60;
	frames = current_time / (U64)41667;
	frames %= 24;
	subframes = current_time / (U64)42;
	subframes %= 100;

	tcstring = llformat("%3.3d:%2.2d:%2.2d:%2.2d.%2.2d",(int)hours,(int)minutes,(int)seconds,(int)frames,(int)subframes);
}


void secondsToTimecodeString(F32 current_time, std::string& tcstring)
{
	microsecondsToTimecodeString((U64)((F64)(SEC_TO_MICROSEC*current_time)), tcstring);
}