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>
|
2017-09-30 14:19:50 +00:00
|
|
|
#include <stdio.h>
|
2017-10-01 00:23:30 +00:00
|
|
|
#include <string.h>
|
2020-02-08 11:57:35 +00:00
|
|
|
#include <utility>
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2018-04-21 12:53:40 +00:00
|
|
|
#include "TracyFileHeader.hpp"
|
2017-09-30 17:25:24 +00:00
|
|
|
#include "../common/tracy_lz4.hpp"
|
2018-08-26 14:25:43 +00:00
|
|
|
#include "../common/tracy_lz4hc.hpp"
|
2017-11-19 21:02:04 +00:00
|
|
|
#include "../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
|
|
|
|
{
|
|
|
|
|
|
|
|
class FileWrite
|
|
|
|
{
|
|
|
|
public:
|
2018-08-26 14:42:44 +00:00
|
|
|
enum class Compression
|
|
|
|
{
|
|
|
|
Fast,
|
|
|
|
Slow,
|
2020-02-08 14:43:01 +00:00
|
|
|
Extreme,
|
|
|
|
Zstd
|
2018-08-26 14:42:44 +00:00
|
|
|
};
|
|
|
|
|
2020-02-08 14:43:01 +00:00
|
|
|
static FileWrite* Open( const char* fn, Compression comp = Compression::Fast, int level = 1 )
|
2017-09-30 14:19:50 +00:00
|
|
|
{
|
|
|
|
auto f = fopen( fn, "wb" );
|
2020-02-08 14:43:01 +00:00
|
|
|
return f ? new FileWrite( f, comp, level ) : nullptr;
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~FileWrite()
|
|
|
|
{
|
2020-02-08 11:57:35 +00:00
|
|
|
if( m_offset > 0 ) WriteLz4Block();
|
2017-09-30 14:19:50 +00:00
|
|
|
fclose( m_file );
|
2018-08-26 14:25:43 +00:00
|
|
|
|
|
|
|
if( m_stream ) LZ4_freeStream( m_stream );
|
|
|
|
if( m_streamHC ) LZ4_freeStreamHC( m_streamHC );
|
2020-02-08 14:43:01 +00:00
|
|
|
if( m_streamZstd ) ZSTD_freeCStream( m_streamZstd );
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 11:57:35 +00:00
|
|
|
void Finish()
|
|
|
|
{
|
|
|
|
if( m_offset > 0 ) WriteLz4Block();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-09-30 16:37:32 +00:00
|
|
|
if( m_offset + size <= BufSize )
|
|
|
|
{
|
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:
|
2020-02-08 14:43:01 +00:00
|
|
|
FileWrite( FILE* f, Compression comp, int level )
|
2018-08-26 14:25:43 +00:00
|
|
|
: m_stream( nullptr )
|
|
|
|
, m_streamHC( nullptr )
|
2020-02-08 14:43:01 +00:00
|
|
|
, m_streamZstd( nullptr )
|
2017-09-30 17:25:24 +00:00
|
|
|
, m_file( f )
|
2018-04-30 00:29:05 +00:00
|
|
|
, m_buf( m_bufData[0] )
|
|
|
|
, m_second( m_bufData[1] )
|
2017-09-30 16:37:32 +00:00
|
|
|
, m_offset( 0 )
|
2020-02-08 11:57:35 +00:00
|
|
|
, m_srcBytes( 0 )
|
|
|
|
, m_dstBytes( 0 )
|
2018-04-21 12:53:40 +00:00
|
|
|
{
|
2018-08-26 14:42:44 +00:00
|
|
|
switch( comp )
|
2018-08-26 14:25:43 +00:00
|
|
|
{
|
2018-08-26 14:42:44 +00:00
|
|
|
case Compression::Fast:
|
2018-08-26 14:25:43 +00:00
|
|
|
m_stream = LZ4_createStream();
|
2018-08-26 14:42:44 +00:00
|
|
|
break;
|
|
|
|
case Compression::Slow:
|
|
|
|
m_streamHC = LZ4_createStreamHC();
|
|
|
|
break;
|
|
|
|
case Compression::Extreme:
|
|
|
|
m_streamHC = LZ4_createStreamHC();
|
2019-09-29 19:02:01 +00:00
|
|
|
LZ4_resetStreamHC( m_streamHC, LZ4HC_CLEVEL_MAX );
|
2018-08-26 14:42:44 +00:00
|
|
|
break;
|
2020-02-08 14:43:01 +00:00
|
|
|
case Compression::Zstd:
|
|
|
|
m_streamZstd = ZSTD_createCStream();
|
|
|
|
ZSTD_CCtx_setParameter( m_streamZstd, ZSTD_c_compressionLevel, level );
|
|
|
|
ZSTD_CCtx_setParameter( m_streamZstd, ZSTD_c_contentSizeFlag, 0 );
|
|
|
|
break;
|
2018-08-26 14:42:44 +00:00
|
|
|
default:
|
|
|
|
assert( false );
|
|
|
|
break;
|
2018-08-26 14:25:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 14:43:01 +00:00
|
|
|
if( comp == Compression::Zstd )
|
|
|
|
{
|
|
|
|
fwrite( ZstdHeader, 1, sizeof( ZstdHeader ), m_file );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fwrite( Lz4Header, 1, sizeof( Lz4Header ), m_file );
|
|
|
|
}
|
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 )
|
|
|
|
{
|
|
|
|
const auto sz = std::min( size, BufSize - 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;
|
|
|
|
|
|
|
|
if( m_offset == BufSize )
|
|
|
|
{
|
|
|
|
WriteLz4Block();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 17:25:24 +00:00
|
|
|
void WriteLz4Block()
|
|
|
|
{
|
|
|
|
char lz4[LZ4Size];
|
2018-08-26 14:25:43 +00:00
|
|
|
uint32_t sz;
|
|
|
|
if( m_stream )
|
|
|
|
{
|
|
|
|
sz = LZ4_compress_fast_continue( m_stream, m_buf, lz4, m_offset, LZ4Size, 1 );
|
|
|
|
}
|
2020-02-08 14:43:01 +00:00
|
|
|
else if( m_streamZstd )
|
|
|
|
{
|
|
|
|
ZSTD_outBuffer out = { lz4, LZ4Size, 0 };
|
|
|
|
ZSTD_inBuffer in = { m_buf, m_offset, 0 };
|
|
|
|
const auto ret = ZSTD_compressStream2( m_streamZstd, &out, &in, ZSTD_e_flush );
|
|
|
|
assert( ret == 0 );
|
|
|
|
sz = out.pos;
|
|
|
|
}
|
2018-08-26 14:25:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sz = LZ4_compress_HC_continue( m_streamHC, m_buf, lz4, m_offset, LZ4Size );
|
|
|
|
}
|
2020-02-08 11:57:35 +00:00
|
|
|
|
|
|
|
m_srcBytes += m_offset;
|
|
|
|
m_dstBytes += sz;
|
|
|
|
|
2017-09-30 17:25:24 +00:00
|
|
|
fwrite( &sz, 1, sizeof( sz ), m_file );
|
|
|
|
fwrite( lz4, 1, sz, m_file );
|
|
|
|
m_offset = 0;
|
2018-04-30 00:29:05 +00:00
|
|
|
std::swap( m_buf, m_second );
|
2017-09-30 17:25:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 16:37:32 +00:00
|
|
|
enum { BufSize = 64 * 1024 };
|
2020-02-08 14:43:01 +00:00
|
|
|
enum { LZ4Size = std::max( LZ4_COMPRESSBOUND( BufSize ), ZSTD_COMPRESSBOUND( BufSize ) ) };
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2017-09-30 17:25:24 +00:00
|
|
|
LZ4_stream_t* m_stream;
|
2018-08-26 14:25:43 +00:00
|
|
|
LZ4_streamHC_t* m_streamHC;
|
2020-02-08 14:43:01 +00:00
|
|
|
ZSTD_CStream* m_streamZstd;
|
2017-09-30 14:19:50 +00:00
|
|
|
FILE* m_file;
|
2018-04-30 00:29:05 +00:00
|
|
|
char m_bufData[2][BufSize];
|
|
|
|
char* m_buf;
|
|
|
|
char* m_second;
|
2017-09-30 16:37:32 +00:00
|
|
|
size_t m_offset;
|
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
|