tracy/server/TracyFileWrite.hpp

185 lines
4.6 KiB
C++
Raw Permalink Normal View History

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>
#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
#include "TracyFileHeader.hpp"
#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
{
class FileWrite
{
public:
enum class Compression
{
Fast,
Slow,
2020-02-08 14:43:01 +00:00
Extreme,
Zstd
};
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();
}
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 )
{
WriteSmall( ptr, size );
2017-09-30 16:37:32 +00:00
}
else
{
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 )
, 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 )
{
switch( comp )
2018-08-26 14:25:43 +00:00
{
case Compression::Fast:
2018-08-26 14:25:43 +00:00
m_stream = LZ4_createStream();
break;
case Compression::Slow:
m_streamHC = LZ4_createStreamHC();
break;
case Compression::Extreme:
m_streamHC = LZ4_createStreamHC();
LZ4_resetStreamHC( m_streamHC, LZ4HC_CLEVEL_MAX );
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;
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 );
}
}
2017-09-30 16:37:32 +00:00
tracy_force_inline void WriteSmall( const void* ptr, size_t size )
{
memcpy( m_buf + m_offset, ptr, size );
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 );
memcpy( m_buf + m_offset, src, sz );
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;
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;
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