tracy/server/TracyFileRead.hpp

282 lines
7.9 KiB
C++
Raw Normal View History

2017-09-30 14:19:50 +00:00
#ifndef __TRACYFILEREAD_HPP__
#define __TRACYFILEREAD_HPP__
#include <atomic>
2017-09-30 16:43:39 +00:00
#include <algorithm>
#include <stdexcept>
2017-09-30 14:19:50 +00:00
#include <stdio.h>
2017-10-01 00:23:30 +00:00
#include <string.h>
2019-10-07 19:32:27 +00:00
#include <string>
#include <thread>
2017-09-30 14:19:50 +00:00
#include "TracyFileHeader.hpp"
2017-09-30 17:25:24 +00:00
#include "../common/tracy_lz4.hpp"
#include "../common/TracyForceInline.hpp"
2017-09-30 17:25:24 +00:00
2017-09-30 14:19:50 +00:00
namespace tracy
{
struct NotTracyDump : public std::exception {};
2017-09-30 14:19:50 +00:00
class FileRead
{
public:
static FileRead* Open( const char* fn )
{
auto f = fopen( fn, "rb" );
2019-10-07 19:32:27 +00:00
return f ? new FileRead( f, fn ) : nullptr;
2017-09-30 14:19:50 +00:00
}
~FileRead()
{
m_exit.store( true, std::memory_order_relaxed );
m_decThread.join();
2017-09-30 14:19:50 +00:00
fclose( m_file );
2017-09-30 17:25:24 +00:00
LZ4_freeStreamDecode( m_stream );
2017-09-30 14:19:50 +00:00
}
tracy_force_inline void Read( void* ptr, size_t size )
2017-09-30 14:19:50 +00:00
{
2017-09-30 23:59:07 +00:00
if( size <= BufSize - m_offset )
2017-09-30 16:43:39 +00:00
{
ReadSmall( ptr, size );
2017-09-30 23:59:07 +00:00
}
else
{
ReadBig( ptr, size );
2017-09-30 16:43:39 +00:00
}
2017-09-30 14:19:50 +00:00
}
tracy_force_inline void Skip( size_t size )
2018-04-20 12:27:20 +00:00
{
if( size <= BufSize - m_offset )
{
m_offset += size;
}
else
{
SkipBig( size );
2018-04-20 12:27:20 +00:00
}
}
template<class T>
tracy_force_inline void Read( T& v )
{
if( sizeof( T ) < BufSize - m_offset )
{
memcpy( &v, m_buf + m_offset, sizeof( T ) );
m_offset += sizeof( T );
}
else
{
T tmp;
ReadBig( &tmp, sizeof( T ) );
memcpy( &v, &tmp, sizeof( T ) );
}
}
2019-11-08 23:25:12 +00:00
template<class T, class U>
tracy_force_inline void Read2( T& v0, U& v1 )
{
2019-11-08 23:25:12 +00:00
if( sizeof( T ) + sizeof( U ) < BufSize - m_offset )
{
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
2019-11-08 23:25:12 +00:00
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
m_offset += sizeof( T ) + sizeof( U );
}
else
{
2019-11-08 23:25:12 +00:00
char tmp[sizeof( T ) + sizeof( U )];
ReadBig( tmp, sizeof( T ) + sizeof( U ) );
memcpy( &v0, tmp, sizeof( T ) );
2019-11-08 23:25:12 +00:00
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
}
}
2019-11-08 23:27:49 +00:00
template<class T, class U, class V>
tracy_force_inline void Read3( T& v0, U& v1, V& v2 )
{
if( sizeof( T ) + sizeof( U ) + sizeof( V ) < BufSize - m_offset )
{
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V );
}
else
{
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V )];
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) );
memcpy( &v0, tmp, sizeof( T ) );
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
}
}
template<class T, class U, class V, class W>
tracy_force_inline void Read4( T& v0, U& v1, V& v2, W& v3 )
{
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) < BufSize - m_offset )
{
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W );
}
else
{
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W )];
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) );
memcpy( &v0, tmp, sizeof( T ) );
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
}
}
bool IsEOF()
{
if( m_lastBlock != BufSize && m_offset == m_lastBlock ) return true;
if( m_offset == BufSize )
{
if( fseek( m_file, 1, SEEK_CUR ) != 0 ) return true;
fseek( m_file, -1, SEEK_CUR );
}
return false;
}
2019-10-07 19:32:27 +00:00
const std::string& GetFilename() const { return m_filename; }
2017-09-30 14:19:50 +00:00
private:
2019-10-07 19:32:27 +00:00
FileRead( FILE* f, const char* fn )
2017-09-30 17:25:24 +00:00
: m_stream( LZ4_createStreamDecode() )
, m_file( f )
, m_buf( m_bufData[1] )
, m_second( m_bufData[0] )
, m_offset( 0 )
, m_lastBlock( 0 )
, m_signalSwitch( false )
, m_signalAvailable( false )
, m_exit( false )
, m_filename( fn )
{
char hdr[4];
if( fread( hdr, 1, sizeof( hdr ), m_file ) != sizeof( hdr ) ) throw NotTracyDump();
if( memcmp( hdr, Lz4Header, sizeof( hdr ) ) != 0 )
{
fseek( m_file, 0, SEEK_SET );
uint32_t sz;
static_assert( sizeof( sz ) == sizeof( hdr ), "Size mismatch" );
memcpy( &sz, hdr, sizeof( sz ) );
if( sz > LZ4Size ) throw NotTracyDump();
}
ReadBlock();
std::swap( m_buf, m_second );
m_decThread = std::thread( [this] { Worker(); } );
}
void Worker()
{
for(;;)
{
ReadBlock();
for(;;)
{
if( m_exit.load( std::memory_order_relaxed ) == true ) return;
if( m_signalSwitch.load( std::memory_order_relaxed ) == true ) break;
std::this_thread::yield();
}
m_signalSwitch.store( false, std::memory_order_relaxed );
std::swap( m_buf, m_second );
m_offset = 0;
m_signalAvailable.store( true, std::memory_order_release );
if( m_lastBlock != BufSize ) return;
}
}
2017-09-30 16:43:39 +00:00
tracy_force_inline void ReadSmall( void* ptr, size_t size )
{
memcpy( ptr, m_buf + m_offset, size );
m_offset += size;
}
void ReadBig( void* ptr, size_t size )
{
auto dst = (char*)ptr;
while( size > 0 )
{
if( m_offset == BufSize )
{
m_signalSwitch.store( true, std::memory_order_relaxed );
while( m_signalAvailable.load( std::memory_order_acquire ) == false ) { std::this_thread::yield(); }
m_signalAvailable.store( false, std::memory_order_relaxed );
}
const auto sz = std::min( size, BufSize - m_offset );
memcpy( dst, m_buf + m_offset, sz );
m_offset += sz;
dst += sz;
size -= sz;
}
}
void SkipBig( size_t size )
{
while( size > 0 )
{
if( m_offset == BufSize )
{
m_signalSwitch.store( true, std::memory_order_relaxed );
while( m_signalAvailable.load( std::memory_order_acquire ) == false ) { std::this_thread::yield(); }
m_signalAvailable.store( false, std::memory_order_relaxed );
}
const auto sz = std::min( size, BufSize - m_offset );
m_offset += sz;
size -= sz;
}
}
void ReadBlock()
{
char m_lz4buf[LZ4Size];
uint32_t sz;
if( fread( &sz, 1, sizeof( sz ), m_file ) == sizeof( sz ) )
{
fread( m_lz4buf, 1, sz, m_file );
m_lastBlock = (size_t)LZ4_decompress_safe_continue( m_stream, m_lz4buf, m_second, sz, BufSize );
}
else
{
m_lastBlock = 0;
}
}
2017-09-30 16:43:39 +00:00
enum { BufSize = 64 * 1024 };
2017-09-30 17:25:24 +00:00
enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) };
2017-09-30 14:19:50 +00:00
2017-09-30 17:25:24 +00:00
LZ4_streamDecode_t* m_stream;
2017-09-30 14:19:50 +00:00
FILE* m_file;
char* m_buf;
char* m_second;
2017-09-30 16:43:39 +00:00
size_t m_offset;
size_t m_lastBlock;
std::atomic<bool> m_signalSwitch;
std::atomic<bool> m_signalAvailable;
std::atomic<bool> m_exit;
std::thread m_decThread;
std::string m_filename;
char m_bufData[2][BufSize];
2017-09-30 14:19:50 +00:00
};
}
#endif