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
|
//========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "pch_tier0.h"
#include "tier0/platform.h"
#include "tier0/memalloc.h"
#include "tier0/dbg.h"
#include "tier0/threadtools.h"
#include <sys/time.h>
#include <unistd.h>
#ifdef OSX
#include <sys/sysctl.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif
static bool g_bBenchmarkMode = false;
static double g_FakeBenchmarkTime = 0;
static double g_FakeBenchmarkTimeInc = 1.0 / 66.0;
bool Plat_IsInBenchmarkMode()
{
return g_bBenchmarkMode;
}
void Plat_SetBenchmarkMode( bool bBenchmark )
{
g_bBenchmarkMode = bBenchmark;
}
#ifdef OSX
static uint64 start_time = 0;
static mach_timebase_info_data_t sTimebaseInfo;
static double conversion = 0.0;
void InitTime()
{
start_time = mach_absolute_time();
mach_timebase_info(&sTimebaseInfo);
conversion = 1e-9 * (double) sTimebaseInfo.numer / (double) sTimebaseInfo.denom;
}
uint64 Plat_GetClockStart()
{
if ( !start_time )
{
InitTime();
}
return start_time * conversion;
}
double Plat_FloatTime()
{
if ( g_bBenchmarkMode )
{
g_FakeBenchmarkTime += g_FakeBenchmarkTimeInc;
return g_FakeBenchmarkTime;
}
if ( !start_time )
{
InitTime();
}
uint64 now = mach_absolute_time();
return ( now - start_time ) * conversion;
}
#else
static int secbase = 0;
void InitTime( struct timeval &tp )
{
secbase = tp.tv_sec;
}
uint64 Plat_GetClockStart()
{
if ( !secbase )
{
struct timeval tp;
gettimeofday( &tp, NULL );
InitTime( tp );
}
return secbase;
}
double Plat_FloatTime()
{
if ( g_bBenchmarkMode )
{
g_FakeBenchmarkTime += g_FakeBenchmarkTimeInc;
return g_FakeBenchmarkTime;
}
struct timeval tp;
gettimeofday( &tp, NULL );
if ( !secbase )
{
InitTime( tp );
return ( tp.tv_usec / 1000000.0 );
}
return (( tp.tv_sec - secbase ) + tp.tv_usec / 1000000.0 );
}
#endif
uint32 Plat_MSTime()
{
if ( g_bBenchmarkMode )
{
g_FakeBenchmarkTime += g_FakeBenchmarkTimeInc;
return (unsigned long)(g_FakeBenchmarkTime * 1000.0);
}
struct timeval tp;
static int secbase = 0;
gettimeofday( &tp, NULL );
if ( !secbase )
{
secbase = tp.tv_sec;
return ( tp.tv_usec / 1000.0 );
}
return (unsigned long)(( tp.tv_sec - secbase )*1000.0 + tp.tv_usec / 1000.0 );
}
// Wraps the thread-safe versions of asctime. buf must be at least 26 bytes
char *Plat_asctime( const struct tm *tm, char *buf, size_t bufsize )
{
return asctime_r( tm, buf );
}
// Wraps the thread-safe versions of ctime. buf must be at least 26 bytes
char *Plat_ctime( const time_t *timep, char *buf, size_t bufsize )
{
return ctime_r( timep, buf );
}
// Wraps the thread-safe versions of gmtime
struct tm *Plat_gmtime( const time_t *timep, struct tm *result )
{
return gmtime_r( timep, result );
}
time_t Plat_timegm( struct tm *timeptr )
{
return timegm( timeptr );
}
// Wraps the thread-safe versions of localtime
struct tm *Plat_localtime( const time_t *timep, struct tm *result )
{
return localtime_r( timep, result );
}
bool vtune( bool resume )
{
}
// -------------------------------------------------------------------------------------------------- //
// Memory stuff.
// -------------------------------------------------------------------------------------------------- //
PLATFORM_INTERFACE void Plat_DefaultAllocErrorFn( unsigned long size )
{
}
typedef void (*Plat_AllocErrorFn)( unsigned long size );
Plat_AllocErrorFn g_AllocError = Plat_DefaultAllocErrorFn;
PLATFORM_INTERFACE void* Plat_Alloc( unsigned long size )
{
void *pRet = g_pMemAlloc->Alloc( size );
if ( pRet )
{
return pRet;
}
else
{
g_AllocError( size );
return 0;
}
}
PLATFORM_INTERFACE void* Plat_Realloc( void *ptr, unsigned long size )
{
void *pRet = g_pMemAlloc->Realloc( ptr, size );
if ( pRet )
{
return pRet;
}
else
{
g_AllocError( size );
return 0;
}
}
PLATFORM_INTERFACE void Plat_Free( void *ptr )
{
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
g_pMemAlloc->Free( ptr );
#else
free( ptr );
#endif
}
PLATFORM_INTERFACE void Plat_SetAllocErrorFn( Plat_AllocErrorFn fn )
{
g_AllocError = fn;
}
static char g_CmdLine[ 2048 ];
PLATFORM_INTERFACE void Plat_SetCommandLine( const char *cmdLine )
{
strncpy( g_CmdLine, cmdLine, sizeof(g_CmdLine) );
g_CmdLine[ sizeof(g_CmdLine) -1 ] = 0;
}
PLATFORM_INTERFACE void Plat_SetCommandLineArgs( char **argv, int argc )
{
g_CmdLine[0] = 0;
for ( int i = 0; i < argc; i++ )
{
strncat( g_CmdLine, argv[i], sizeof(g_CmdLine) - strlen(g_CmdLine) );
}
g_CmdLine[ sizeof(g_CmdLine) -1 ] = 0;
}
PLATFORM_INTERFACE const tchar *Plat_GetCommandLine()
{
return g_CmdLine;
}
PLATFORM_INTERFACE bool Is64BitOS()
{
#if defined OSX
return true;
#elif defined LINUX
FILE *pp = popen( "uname -m", "r" );
if ( pp != NULL )
{
char rgchArchString[256];
fgets( rgchArchString, sizeof( rgchArchString ), pp );
pclose( pp );
if ( !strncasecmp( rgchArchString, "x86_64", strlen( "x86_64" ) ) )
return true;
}
#else
Assert( !"implement Is64BitOS" );
#endif
return false;
}
bool Plat_IsInDebugSession()
{
#if defined(OSX)
int mib[4];
struct kinfo_proc info;
size_t size;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
size = sizeof(info);
info.kp_proc.p_flag = 0;
sysctl(mib,4,&info,&size,NULL,0);
bool result = ((info.kp_proc.p_flag & P_TRACED) == P_TRACED);
return result;
#elif defined(LINUX)
char s[256];
snprintf(s, 256, "/proc/%d/cmdline", getppid());
FILE * fp = fopen(s, "r");
if (fp != NULL)
{
fread(s, 256, 1, fp);
fclose(fp);
return (0 == strncmp(s, "gdb", 3));
}
return false;
#endif
}
void Plat_ExitProcess( int nCode )
{
_exit( nCode );
}
static int s_nWatchDogTimerTimeScale = 0;
static bool s_bInittedWD = false;
static void InitWatchDogTimer( void )
{
if( !strstr( g_CmdLine, "-nowatchdog" ) )
{
#ifdef _DEBUG
s_nWatchDogTimerTimeScale = 10; // debug is slow
#else
s_nWatchDogTimerTimeScale = 1;
#endif
}
}
// watchdog timer support
void BeginWatchdogTimer( int nSecs )
{
if (! s_bInittedWD )
{
s_bInittedWD = true;
InitWatchDogTimer();
}
nSecs *= s_nWatchDogTimerTimeScale;
nSecs = MIN( nSecs, 5 * 60 ); // no more than 5 minutes no matter what
if ( nSecs )
alarm( nSecs );
}
void EndWatchdogTimer( void )
{
alarm( 0 );
}
static CThreadMutex g_LocalTimeMutex;
void Plat_GetLocalTime( struct tm *pNow )
{
// We just provide a wrapper on this function so we can protect access to time() everywhere.
time_t ltime;
time( <ime );
Plat_ConvertToLocalTime( ltime, pNow );
}
void Plat_ConvertToLocalTime( uint64 nTime, struct tm *pNow )
{
// Since localtime() returns a global, we need to protect against multiple threads stomping it.
g_LocalTimeMutex.Lock();
time_t ltime = (time_t)nTime;
tm *pTime = localtime( <ime );
if ( pTime )
*pNow = *pTime;
else
memset( pNow, 0, sizeof( *pNow ) );
g_LocalTimeMutex.Unlock();
}
void Plat_GetTimeString( struct tm *pTime, char *pOut, int nMaxBytes )
{
g_LocalTimeMutex.Lock();
char *pStr = asctime( pTime );
strncpy( pOut, pStr, nMaxBytes );
pOut[nMaxBytes-1] = 0;
g_LocalTimeMutex.Unlock();
}
void Plat_gmtime( uint64 nTime, struct tm *pTime )
{
time_t tmtTime = nTime;
struct tm * tmp = gmtime( &tmtTime );
* pTime = * tmp;
}
#ifdef LINUX
size_t ApproximateProcessMemoryUsage( void )
{
int nRet = 0;
FILE *pFile = fopen( "/proc/self/statm", "r" );
if ( pFile )
{
int nSize, nTotalProgramSize, nResident, nResidentSetSize, nShare, nSharedPagesTotal, nDummy0;
if ( fscanf( pFile, "%d %d %d %d %d %d %d", &nSize, &nTotalProgramSize, &nResident, &nResidentSetSize, &nShare, &nSharedPagesTotal, &nDummy0 ) )
{
nRet = 4096 * nSize;
}
fclose( pFile );
}
return nRet;
}
#else
size_t ApproximateProcessMemoryUsage( void )
{
return 0;
}
#endif
|