2017-09-30 14:19:50 +00:00
|
|
|
#ifndef __TRACYFILEWRITE_HPP__
|
|
|
|
#define __TRACYFILEWRITE_HPP__
|
|
|
|
|
2019-06-22 11:40:00 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning( disable: 4267 ) // conversion from don't care to whatever, possible loss of data
|
|
|
|
#endif
|
|
|
|
|
2017-09-30 16:37:32 +00:00
|
|
|
#include <algorithm>
|
2018-08-26 14:42:44 +00:00
|
|
|
#include <assert.h>
|
2024-06-02 10:27:17 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2017-09-30 14:19:50 +00:00
|
|
|
#include <stdio.h>
|
2017-10-01 00:23:30 +00:00
|
|
|
#include <string.h>
|
2024-06-02 10:27:17 +00:00
|
|
|
#include <thread>
|
2020-02-08 11:57:35 +00:00
|
|
|
#include <utility>
|
2024-06-02 10:27:17 +00:00
|
|
|
#include <vector>
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2018-04-21 12:53:40 +00:00
|
|
|
#include "TracyFileHeader.hpp"
|
2024-06-02 10:47:50 +00:00
|
|
|
#include "TracyFileMeta.hpp"
|
2022-07-17 11:41:40 +00:00
|
|
|
#include "../public/common/tracy_lz4.hpp"
|
|
|
|
#include "../public/common/tracy_lz4hc.hpp"
|
|
|
|
#include "../public/common/TracyForceInline.hpp"
|
2020-02-08 14:43:01 +00:00
|
|
|
#include "../zstd/zstd.h"
|
2017-09-30 17:25:24 +00:00
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2024-05-31 17:24:32 +00:00
|
|
|
enum class FileCompression
|
|
|
|
{
|
|
|
|
Fast,
|
|
|
|
Slow,
|
|
|
|
Extreme,
|
|
|
|
Zstd
|
|
|
|
};
|
|
|
|
|
2024-05-31 18:29:57 +00:00
|
|
|
class WriteStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WriteStream( FileCompression comp, int level )
|
|
|
|
: m_stream( nullptr )
|
|
|
|
, m_streamHC( nullptr )
|
|
|
|
, m_streamZstd( nullptr )
|
2024-06-02 10:27:17 +00:00
|
|
|
, m_buf( new char[FileBufSize] )
|
|
|
|
, m_second( new char[FileBufSize] )
|
|
|
|
, m_compressed( new char[FileBoundSize] )
|
2024-05-31 18:29:57 +00:00
|
|
|
{
|
|
|
|
switch( comp )
|
|
|
|
{
|
|
|
|
case FileCompression::Fast:
|
|
|
|
m_stream = LZ4_createStream();
|
|
|
|
break;
|
|
|
|
case FileCompression::Slow:
|
|
|
|
m_streamHC = LZ4_createStreamHC();
|
|
|
|
break;
|
|
|
|
case FileCompression::Extreme:
|
|
|
|
m_streamHC = LZ4_createStreamHC();
|
|
|
|
LZ4_resetStreamHC( m_streamHC, LZ4HC_CLEVEL_MAX );
|
|
|
|
break;
|
|
|
|
case FileCompression::Zstd:
|
|
|
|
m_streamZstd = ZSTD_createCStream();
|
|
|
|
ZSTD_CCtx_setParameter( m_streamZstd, ZSTD_c_compressionLevel, level );
|
|
|
|
ZSTD_CCtx_setParameter( m_streamZstd, ZSTD_c_contentSizeFlag, 0 );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~WriteStream()
|
|
|
|
{
|
2024-06-02 10:27:17 +00:00
|
|
|
delete[] m_buf;
|
|
|
|
delete[] m_second;
|
|
|
|
delete[] m_compressed;
|
|
|
|
|
2024-05-31 18:29:57 +00:00
|
|
|
if( m_stream ) LZ4_freeStream( m_stream );
|
|
|
|
if( m_streamHC ) LZ4_freeStreamHC( m_streamHC );
|
|
|
|
if( m_streamZstd ) ZSTD_freeCStream( m_streamZstd );
|
|
|
|
}
|
|
|
|
|
2024-06-02 10:27:17 +00:00
|
|
|
char* GetInputBuffer() { return m_buf; }
|
|
|
|
const char* GetCompressedData() const { return m_compressed; }
|
|
|
|
uint32_t GetSize() const { return m_size; }
|
2024-05-31 18:29:57 +00:00
|
|
|
|
2024-06-02 10:27:17 +00:00
|
|
|
void Compress( uint32_t sz )
|
2024-05-31 18:29:57 +00:00
|
|
|
{
|
|
|
|
if( m_stream )
|
|
|
|
{
|
2024-06-02 10:27:17 +00:00
|
|
|
m_size = LZ4_compress_fast_continue( m_stream, m_buf, m_compressed, sz, FileBoundSize, 1 );
|
2024-05-31 18:29:57 +00:00
|
|
|
}
|
|
|
|
else if( m_streamZstd )
|
|
|
|
{
|
|
|
|
ZSTD_outBuffer out = { m_compressed, FileBoundSize, 0 };
|
|
|
|
ZSTD_inBuffer in = { m_buf, sz, 0 };
|
|
|
|
const auto ret = ZSTD_compressStream2( m_streamZstd, &out, &in, ZSTD_e_flush );
|
|
|
|
assert( ret == 0 );
|
2024-06-02 10:27:17 +00:00
|
|
|
m_size = out.pos;
|
2024-05-31 18:29:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-02 10:27:17 +00:00
|
|
|
m_size = LZ4_compress_HC_continue( m_streamHC, m_buf, m_compressed, sz, FileBoundSize );
|
2024-05-31 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::swap( m_buf, m_second );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LZ4_stream_t* m_stream;
|
|
|
|
LZ4_streamHC_t* m_streamHC;
|
|
|
|
ZSTD_CStream* m_streamZstd;
|
|
|
|
|
|
|
|
char* m_buf;
|
|
|
|
char* m_second;
|
2024-06-02 10:27:17 +00:00
|
|
|
char* m_compressed;
|
|
|
|
uint32_t m_size;
|
2024-05-31 18:29:57 +00:00
|
|
|
};
|
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
class FileWrite
|
|
|
|
{
|
2024-06-02 10:27:17 +00:00
|
|
|
struct StreamHandle
|
|
|
|
{
|
|
|
|
StreamHandle( FileCompression comp, int level ) : stream( comp, level ) {}
|
|
|
|
|
|
|
|
WriteStream stream;
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
bool inputReady = false;
|
|
|
|
bool outputReady = false;
|
|
|
|
bool exit = false;
|
|
|
|
|
|
|
|
std::mutex signalLock;
|
|
|
|
std::condition_variable signal;
|
|
|
|
|
|
|
|
std::thread thread;
|
|
|
|
};
|
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
public:
|
2024-06-02 10:27:17 +00:00
|
|
|
static FileWrite* Open( const char* fn, FileCompression comp = FileCompression::Fast, int level = 1, int streams = -1 )
|
2017-09-30 14:19:50 +00:00
|
|
|
{
|
|
|
|
auto f = fopen( fn, "wb" );
|
2024-06-02 10:27:17 +00:00
|
|
|
if( !f ) return nullptr;
|
|
|
|
if( streams <= 0 ) streams = std::max<int>( 1, std::thread::hardware_concurrency() );
|
|
|
|
if( streams > 255 ) streams = 255;
|
|
|
|
return new FileWrite( f, comp, level, streams );
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~FileWrite()
|
|
|
|
{
|
2024-05-31 18:29:57 +00:00
|
|
|
Finish();
|
2017-09-30 14:19:50 +00:00
|
|
|
fclose( m_file );
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:57:35 +00:00
|
|
|
void Finish()
|
|
|
|
{
|
2024-05-31 18:29:57 +00:00
|
|
|
if( m_offset > 0 ) WriteBlock();
|
2024-06-02 10:27:17 +00:00
|
|
|
while( m_streamPending > 0 ) ProcessPending();
|
|
|
|
for( auto& v : m_streams )
|
|
|
|
{
|
|
|
|
std::lock_guard lock( v->signalLock );
|
|
|
|
v->exit = true;
|
|
|
|
v->signal.notify_one();
|
|
|
|
}
|
|
|
|
for( auto& v : m_streams ) v->thread.join();
|
|
|
|
m_streams.clear();
|
2020-02-08 11:57:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:02:04 +00:00
|
|
|
tracy_force_inline void Write( const void* ptr, size_t size )
|
2017-09-30 14:19:50 +00:00
|
|
|
{
|
2024-05-31 18:29:57 +00:00
|
|
|
if( m_offset + size <= FileBufSize )
|
2017-09-30 16:37:32 +00:00
|
|
|
{
|
2018-03-17 12:57:32 +00:00
|
|
|
WriteSmall( ptr, size );
|
2017-09-30 16:37:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-17 12:57:32 +00:00
|
|
|
WriteBig( ptr, size );
|
2017-09-30 16:37:32 +00:00
|
|
|
}
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 11:57:35 +00:00
|
|
|
std::pair<size_t, size_t> GetCompressionStatistics() const { return std::make_pair( m_srcBytes, m_dstBytes ); }
|
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
private:
|
2024-06-02 10:27:17 +00:00
|
|
|
FileWrite( FILE* f, FileCompression comp, int level, int streams )
|
|
|
|
: m_offset( 0 )
|
2017-09-30 17:25:24 +00:00
|
|
|
, m_file( f )
|
2020-02-08 11:57:35 +00:00
|
|
|
, m_srcBytes( 0 )
|
|
|
|
, m_dstBytes( 0 )
|
2018-04-21 12:53:40 +00:00
|
|
|
{
|
2024-06-02 10:27:17 +00:00
|
|
|
assert( streams > 0 );
|
|
|
|
assert( streams < 256 );
|
|
|
|
|
|
|
|
fwrite( TracyHeader, 1, sizeof( TracyHeader ), m_file );
|
|
|
|
uint8_t u8 = comp == FileCompression::Zstd ? 1 : 0;
|
|
|
|
fwrite( &u8, 1, 1, m_file );
|
|
|
|
u8 = streams;
|
|
|
|
fwrite( &u8, 1, 1, m_file );
|
|
|
|
|
|
|
|
m_streams.reserve( streams );
|
|
|
|
for( int i=0; i<streams; i++ )
|
2020-02-08 14:43:01 +00:00
|
|
|
{
|
2024-06-02 10:27:17 +00:00
|
|
|
auto uptr = std::make_unique<StreamHandle>( comp, level );
|
|
|
|
uptr->thread = std::thread( [ptr = uptr.get()]{ Worker( ptr ); } );
|
|
|
|
m_streams.emplace_back( std::move( uptr ) );
|
2020-02-08 14:43:01 +00:00
|
|
|
}
|
2024-06-02 10:27:17 +00:00
|
|
|
|
|
|
|
m_buf = m_streams[m_streamId]->stream.GetInputBuffer();
|
2018-04-21 12:53:40 +00:00
|
|
|
}
|
2017-09-30 16:37:32 +00:00
|
|
|
|
2018-03-17 12:57:32 +00:00
|
|
|
tracy_force_inline void WriteSmall( const void* ptr, size_t size )
|
|
|
|
{
|
2018-04-30 00:29:05 +00:00
|
|
|
memcpy( m_buf + m_offset, ptr, size );
|
2018-03-17 12:57:32 +00:00
|
|
|
m_offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBig( const void* ptr, size_t size )
|
|
|
|
{
|
|
|
|
auto src = (const char*)ptr;
|
|
|
|
while( size > 0 )
|
|
|
|
{
|
2024-05-31 18:29:57 +00:00
|
|
|
const auto sz = std::min( size, FileBufSize - m_offset );
|
2018-04-30 00:29:05 +00:00
|
|
|
memcpy( m_buf + m_offset, src, sz );
|
2018-03-17 12:57:32 +00:00
|
|
|
m_offset += sz;
|
|
|
|
src += sz;
|
|
|
|
size -= sz;
|
|
|
|
|
2024-05-31 18:29:57 +00:00
|
|
|
if( m_offset == FileBufSize )
|
2018-03-17 12:57:32 +00:00
|
|
|
{
|
2024-05-31 18:29:57 +00:00
|
|
|
WriteBlock();
|
2018-03-17 12:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:29:57 +00:00
|
|
|
void WriteBlock()
|
2017-09-30 17:25:24 +00:00
|
|
|
{
|
2020-02-08 11:57:35 +00:00
|
|
|
m_srcBytes += m_offset;
|
|
|
|
|
2024-06-02 10:27:17 +00:00
|
|
|
auto& hnd = *m_streams[m_streamId];
|
|
|
|
assert( hnd.stream.GetInputBuffer() == m_buf );
|
|
|
|
|
|
|
|
std::unique_lock lock( hnd.signalLock );
|
|
|
|
hnd.inputReady = true;
|
|
|
|
hnd.size = m_offset;
|
|
|
|
hnd.signal.notify_one();
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
m_streamPending++;
|
|
|
|
m_streamId = ( m_streamId + 1 ) % m_streams.size();
|
|
|
|
if( m_streamPending == m_streams.size() ) ProcessPending();
|
2024-05-31 18:29:57 +00:00
|
|
|
|
2017-09-30 17:25:24 +00:00
|
|
|
m_offset = 0;
|
2024-06-02 10:27:17 +00:00
|
|
|
m_buf = m_streams[m_streamId]->stream.GetInputBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessPending()
|
|
|
|
{
|
|
|
|
assert( m_streamPending > 0 );
|
|
|
|
int id = ( m_streamId + m_streams.size() - m_streamPending ) % m_streams.size();
|
|
|
|
m_streamPending--;
|
|
|
|
auto& hnd = *m_streams[id];
|
|
|
|
|
|
|
|
std::unique_lock lock( hnd.signalLock );
|
|
|
|
hnd.signal.wait( lock, [&hnd]{ return hnd.outputReady; } );
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
hnd.outputReady = false;
|
|
|
|
const uint32_t size = hnd.stream.GetSize();
|
|
|
|
m_dstBytes += size;
|
|
|
|
fwrite( &size, 1, sizeof( size ), m_file );
|
|
|
|
fwrite( hnd.stream.GetCompressedData(), 1, size, m_file );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Worker( StreamHandle* hnd )
|
|
|
|
{
|
|
|
|
std::unique_lock lock( hnd->signalLock );
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
hnd->signal.wait( lock, [&hnd]{ return hnd->inputReady || hnd->exit; } );
|
|
|
|
if( hnd->exit ) return;
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
hnd->stream.Compress( hnd->size );
|
|
|
|
hnd->inputReady = false;
|
|
|
|
|
|
|
|
lock.lock();
|
|
|
|
hnd->outputReady = true;
|
|
|
|
hnd->signal.notify_one();
|
|
|
|
}
|
2017-09-30 17:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 00:29:05 +00:00
|
|
|
char* m_buf;
|
2017-09-30 16:37:32 +00:00
|
|
|
size_t m_offset;
|
2024-06-02 10:27:17 +00:00
|
|
|
|
|
|
|
int m_streamId = 0;
|
|
|
|
int m_streamPending = 0;
|
|
|
|
std::vector<std::unique_ptr<StreamHandle>> m_streams;
|
|
|
|
FILE* m_file;
|
|
|
|
|
2020-02-08 11:57:35 +00:00
|
|
|
size_t m_srcBytes;
|
|
|
|
size_t m_dstBytes;
|
2017-09-30 14:19:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|