Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions CCPMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#include "include/CCPMemory.h"
#include "include/CCPMemoryTracker.h"
#include "include/CCPAssert.h"
#include "include/CcpSecureCrt.h"
#include "include/CcpTelemetry.h"
#include "CcpMemoryTrackerMutex.h"

#include <atomic>

#ifdef __APPLE__
#include <malloc/malloc.h>
#include <mach/mach.h>
Expand All @@ -14,6 +17,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 +796,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"
}
}
]
}
54 changes: 50 additions & 4 deletions CcpFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ off_t CcpTell( int fd )

#else

#include <sys/file.h>

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 All @@ -120,6 +123,39 @@ int ConvertShareMode( CcpShareMode shareMode )
return shflag;
}

namespace
{
#if defined(O_EXLOCK) && defined(O_SHLOCK)

// The share mode is applied atomically by open() via O_EXLOCK/O_SHLOCK.
inline int ApplyShareMode( int fd, CcpShareMode )
{
return fd;
}

#else

// BSD's O_EXLOCK/O_SHLOCK acquire a flock-style advisory lock atomically at
// open() time. Platforms without those flags apply the equivalent flock()
// right after opening instead.
int ApplyShareMode( int fd, CcpShareMode shareMode )
{
if( fd < 0 || shareMode == CCP_SM_RWSHARING )
{
return fd;
}

if( flock( fd, shareMode == CCP_SM_NOSHARING ? LOCK_EX : LOCK_SH ) != 0 )
{
close( fd );
return -1;
}
return fd;
}

#endif
}

int ConvertOpenMode( CcpOpenMode mode )
{
int oflag = 0;
Expand All @@ -145,7 +181,7 @@ int CcpOpenFile( const wchar_t* filename, CcpOpenMode mode, CcpShareMode shareMo
int shflag = ConvertShareMode( shareMode );
int fd = open( CW2A( filename ), oflag | shflag, S_IRUSR | S_IWUSR );

return fd;
return ApplyShareMode( fd, shareMode );
}

int CcpCreateFile( const wchar_t* filename )
Expand All @@ -160,7 +196,7 @@ int CcpCreateFile( const wchar_t* filename, CcpShareMode shareMode )
int shflag = ConvertShareMode( shareMode );
int fd = open( CW2A( filename ), O_CREAT | O_TRUNC | O_RDWR | shflag, S_IRUSR | S_IWUSR );

return fd;
return ApplyShareMode( fd, shareMode );
}

void CcpCloseFile( int fd )
Expand Down Expand Up @@ -217,6 +253,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 +771,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 CcpMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "include/CcpAtomic.h"
#include "include/CcpMutex.h"
#include "include/CcpThread.h"
#if CCP_TELEMETRY_ENABLED
#include "tracy/TracyC.h"
#endif


namespace
Expand Down
21 changes: 19 additions & 2 deletions CcpSemaphore.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// Copyright © 2013 CCP ehf.

#include "include/CcpSemaphore.h"
#include "include/CcpSecureCrt.h"

#if CCP_TELEMETRY_ENABLED
#include "tracy/TracyC.h"
#endif

#include <cstring>
#include <ctime>

// OS specific includes:
#ifdef _WIN32
Expand Down Expand Up @@ -58,6 +64,7 @@ CcpSemaphore::CcpSemaphore( const char* semaphoreName, uint32_t initialCount, ui
#endif
}

#if CCP_TELEMETRY_ENABLED
namespace
{
void AnnounceSemaphoreToTelemetry( TracyCLockCtx& ctx, const char* name )
Expand All @@ -73,6 +80,7 @@ namespace
}
}
}
#endif

// Preferred constructor, with default value overloads (see header file for details)
CcpSemaphore::CcpSemaphore( const char* semaphoreName )
Expand Down Expand Up @@ -168,9 +176,18 @@ bool CcpSemaphore::TimedWait( uint32_t timeoutInMs )
mts.tv_nsec = ( timeoutInMs % 1000 ) * 1000000;
const bool result = semaphore_timedwait( m_impl->semaphore, mts ) == KERN_SUCCESS;
#else
// sem_timedwait expects an absolute CLOCK_REALTIME timestamp, not a
// relative timeout; passing a relative value makes the wait return
// (almost) immediately.
timespec ts;
ts.tv_sec = timeoutInMs / 1000;
ts.tv_nsec = (timeoutInMs % 1000) * 1000000;
clock_gettime( CLOCK_REALTIME, &ts );
ts.tv_sec += timeoutInMs / 1000;
ts.tv_nsec += (timeoutInMs % 1000) * 1000000;
if( ts.tv_nsec >= 1000000000 )
{
ts.tv_sec += 1;
ts.tv_nsec -= 1000000000;
}
const bool result = sem_timedwait( &m_impl->semaphore, &ts ) == 0;
#endif

Expand Down
3 changes: 3 additions & 0 deletions CcpStatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include "include/CcpStatistics.h"

#include <cmath>
#if CCP_TELEMETRY_ENABLED
#include <tracy/Tracy.hpp>
#endif

#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
Loading