Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions CCPAssert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ bool CcpIsDebuggerPresent()
return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}

#elif defined(__linux__)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// A tracer (debugger) is attached if the TracerPid field of /proc/self/status
// is non-zero.
bool CcpIsDebuggerPresent()
{
FILE* file = fopen( "/proc/self/status", "r" );
if( !file )
{
return false;
}

bool traced = false;
char line[256];
while( fgets( line, sizeof( line ), file ) )
{
if( strncmp( line, "TracerPid:", 10 ) == 0 )
{
traced = atoi( line + 10 ) != 0;
break;
}
}
fclose( file );
return traced;
}

#elif _WIN32

bool CcpIsDebuggerPresent()
Expand Down
2 changes: 1 addition & 1 deletion CCPCallstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ void CCPCallstack::Enumerate( void ( *callback )( size_t codePointer, const char
{
std::string record = lines[i];
const char* plus = strchr( lines[i], '+' );
if( *plus )
if( plus )
{
std::string name;
GetLastWord( lines[i], plus - 1, name );
Expand Down
34 changes: 34 additions & 0 deletions CCPMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "include/CcpTelemetry.h"
#include "CcpMemoryTrackerMutex.h"

#include <atomic>

#ifdef __APPLE__
#include <malloc/malloc.h>
#include <mach/mach.h>
Expand All @@ -14,6 +16,8 @@
#include <psapi.h>
#else
#include <malloc.h>
#include <stdio.h>
#include <sys/resource.h>
#endif

// We need to initialize the memory system before any other static initializers are executed.
Expand Down Expand Up @@ -791,6 +795,36 @@ bool CcpGetProcessMemoryInfo( CcpProcessMemoryInfo& result )
result.pageFaultCount = size_t( vmEvents.faults );
return true;
}
#elif defined(__linux__)
// VmRSS/VmSize are reported in kB in /proc/self/status.
FILE* file = fopen( "/proc/self/status", "r" );
if( file )
{
size_t vmRss = 0;
size_t vmSize = 0;
char line[256];
while( fgets( line, sizeof( line ), file ) )
{
if( sscanf( line, "VmRSS: %zu", &vmRss ) == 1 )
{
continue;
}
if( sscanf( line, "VmSize: %zu", &vmSize ) == 1 )
{
continue;
}
}
fclose( file );
result.workingSetSize = vmRss * 1024;
result.pageFileUsage = vmSize * 1024;

rusage usage;
if( getrusage( RUSAGE_SELF, &usage ) == 0 )
{
result.pageFaultCount = size_t( usage.ru_minflt + usage.ru_majflt );
return true;
}
}
#endif
return false;
}
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ set(SRC_FILES
CcpCore.cpp
CCPAssert.cpp
CCPCallstack.cpp
CCPDefines.cpp
CcpDefines.cpp
CcpFileUtils.cpp
CCPHash.cpp
CCPMemory.cpp
CCPMemoryTracker.cpp
CCPMemoryTrackerMutex.h
CcpMemoryTrackerMutex.h
CcpMutex.cpp
CcpProcess.cpp
CcpSecureCrt.cpp
Expand Down
54 changes: 54 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
},
"hidden": true
},
{
"name": "linux",
"inherits": "common",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"hidden": true
},
{
"name": "x64-windows-v145",
"inherits": "windows",
Expand Down Expand Up @@ -73,6 +83,14 @@
},
"hidden": true
},
{
"name": "x64-linux",
"inherits": "linux",
"cacheVariables": {
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/vendor/github.com/carbonengine/vcpkg-registry/toolchains/x64-linux-carbon.cmake"
},
"hidden": true
},
{
"name": "x64-windows-v145-internal",
"inherits": "x64-windows-v145",
Expand Down Expand Up @@ -216,6 +234,42 @@
"VCPKG_TARGET_TRIPLET": "x64-osx-trinitydev",
"VCPKG_HOST_TRIPLET": "x64-osx-trinitydev"
}
},
{
"name": "x64-linux-internal",
"inherits": "x64-linux",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Internal",
"VCPKG_TARGET_TRIPLET": "x64-linux-internal",
"VCPKG_HOST_TRIPLET": "x64-linux-internal"
}
},
{
"name": "x64-linux-release",
"inherits": "x64-linux",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"VCPKG_TARGET_TRIPLET": "x64-linux-release",
"VCPKG_HOST_TRIPLET": "x64-linux-release"
}
},
{
"name": "x64-linux-debug",
"inherits": "x64-linux",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"VCPKG_TARGET_TRIPLET": "x64-linux-debug",
"VCPKG_HOST_TRIPLET": "x64-linux-debug"
}
},
{
"name": "x64-linux-trinitydev",
"inherits": "x64-linux",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "TrinityDev",
"VCPKG_TARGET_TRIPLET": "x64-linux-trinitydev",
"VCPKG_HOST_TRIPLET": "x64-linux-trinitydev"
}
}
]
}
15 changes: 13 additions & 2 deletions CcpFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ off_t CcpTell( int fd )
int ConvertShareMode( CcpShareMode shareMode )
{
int shflag = 0;
#ifndef __ANDROID__
// O_EXLOCK/O_SHLOCK are BSD extensions that do not exist on Linux or Android.
#if defined(O_EXLOCK) && defined(O_SHLOCK)
Comment thread
LelandJin marked this conversation as resolved.
switch( shareMode )
{
case CCP_SM_NOSHARING:
Expand Down Expand Up @@ -217,6 +218,7 @@ bool CcpRenameFile( const std::wstring& src, const std::wstring& dst )
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
Expand Down Expand Up @@ -734,10 +736,19 @@ std::wstring CcpExecutablePath()
tmp.resize( size );
_NSGetExecutablePath( &tmp[0], &size );
}

char actualpath [PATH_MAX];
char* path = realpath(&tmp[0], actualpath);
return std::wstring( CA2W( actualpath ) );
#elif defined(__linux__)
char buffer[PATH_MAX];
ssize_t len = readlink( "/proc/self/exe", buffer, sizeof( buffer ) - 1 );
if( len < 0 )
{
return std::wstring();
}
buffer[len] = 0;
return std::wstring( CA2W( buffer ) );
#else
static_assert( false, "CcpExecutablePath is not implemented" );
#endif
Expand Down
2 changes: 2 additions & 0 deletions CcpSemaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "tracy/TracyC.h"

#include <cstring>

// OS specific includes:
#ifdef _WIN32
// Nothing specific
Expand Down
1 change: 1 addition & 0 deletions CcpStatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "include/CcpStatistics.h"

#include <cmath>
#include <tracy/Tracy.hpp>

#include "CcpTelemetry.h"
Expand Down
6 changes: 2 additions & 4 deletions CcpTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,10 @@ void CcpTelemetryTick()
if ( !s_fiberEraseMap.empty() )
{
auto now = std::chrono::steady_clock::now();
auto elem = s_fiberEraseMap.front();
while ( !s_fiberEraseMap.empty() && elem.second >= now )
while ( !s_fiberEraseMap.empty() && s_fiberEraseMap.front().second >= now )
{
s_fiberNameStore.erase( elem.first );
s_fiberNameStore.erase( s_fiberEraseMap.front().first );
s_fiberEraseMap.pop();
elem = s_fiberEraseMap.front();
}
}

Expand Down
32 changes: 29 additions & 3 deletions CcpThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ bool CcpGetThreadTimes( int64_t& kernelTime, int64_t& userTime )
return GetThreadTimes( GetCurrentThread(), &dummy, &dummy, (LPFILETIME)&kernelTime, (LPFILETIME)&userTime ) != 0;
}

#elif __APPLE__
#elif defined(__APPLE__) || defined(__linux__)

#include <sys/time.h>
#include "include/CCPAssert.h"
#ifdef __APPLE__
#include <mach/thread_act.h>
#else
#include <sys/resource.h>
#endif

namespace
{
Expand All @@ -131,15 +135,19 @@ namespace

CcpThreadId_t CcpGetCurrentThreadId()
{
#ifdef __APPLE__
return pthread_mach_thread_np(pthread_self());
#else
return pthread_self();
#endif
}

CcpThreadHandle_t CcpCreateThread( CcpThreadProc_t threadProc, void* context, CcpThreadPriority_t priority )
{
pthread_attr_t attr;
if( pthread_attr_init( &attr ) != 0 )
{
return nullptr;
return CcpThreadHandle_t();
}

CreateThreadData* data = CCP_NEW( "CcpCreateThread/data" ) CreateThreadData;
Expand All @@ -161,7 +169,7 @@ CcpThreadHandle_t CcpCreateThread( CcpThreadProc_t threadProc, void* context, Cc
}
else
{
return nullptr;
return CcpThreadHandle_t();
}
}

Expand Down Expand Up @@ -243,7 +251,11 @@ int CcpJoinThreadWithTimeout( CcpThreadHandle_t threadHandle, uint32_t timeoutIn

CcpThreadId_t CcpGetThreadId( CcpThreadHandle_t handle )
{
#ifdef __APPLE__
return pthread_mach_thread_np(handle);
#else
return handle;
#endif
}

bool CcpSetThreadPriority( CcpThreadHandle_t thread, CcpThreadPriority_t priority )
Expand Down Expand Up @@ -301,6 +313,7 @@ void CcpSetThreadPriority( CcpThread& thread, CcpThreadPriority_t priority )
CcpSetThreadPriority( thread.native_handle(), priority );
}

#ifdef __APPLE__
bool CcpGetThreadTimes( int64_t& kernelTime, int64_t& userTime )
{
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
Expand All @@ -319,5 +332,18 @@ bool CcpGetThreadTimes( int64_t& kernelTime, int64_t& userTime )
kernelTime = int64_t( info.system_time.seconds ) * 10000000 + int64_t( info.system_time.microseconds ) * 10;
return true;
}
#else
bool CcpGetThreadTimes( int64_t& kernelTime, int64_t& userTime )
{
rusage usage;
if( getrusage( RUSAGE_THREAD, &usage ) != 0 )
{
return false;
}
userTime = int64_t( usage.ru_utime.tv_sec ) * 10000000 + int64_t( usage.ru_utime.tv_usec ) * 10;
kernelTime = int64_t( usage.ru_stime.tv_sec ) * 10000000 + int64_t( usage.ru_stime.tv_usec ) * 10;
return true;
}
#endif

#endif
2 changes: 2 additions & 0 deletions CcpTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "include/CcpTime.h"
#include "include/CCPAssert.h"
#include <cfloat>
#include <climits>
#include <cmath>

#ifdef _WIN32
Expand Down Expand Up @@ -113,6 +114,7 @@ uint64_t CcpGetTickCount()
#else

#include <time.h>
#include <sys/time.h>

uint64_t CcpGetTimestamp()
{
Expand Down
Loading