tracy/server/TracyFileWrite.hpp

213 lines
5.1 KiB
C++
Raw 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
{
constexpr size_t FileBufSize = 64 * 1024;
constexpr size_t FileBoundSize = std::max( LZ4_COMPRESSBOUND( FileBufSize ), ZSTD_COMPRESSBOUND( FileBufSize ) );
enum class FileCompression
{
Fast,
Slow,
Extreme,
Zstd
};
class WriteStream
{
public:
WriteStream( FileCompression comp, int level )
: m_stream( nullptr )
, m_streamHC( nullptr )
, m_streamZstd( nullptr )
, m_buf( m_bufData[0] )
, m_second( m_bufData[1] )
{
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()
{
if( m_stream ) LZ4_freeStream( m_stream );
if( m_streamHC ) LZ4_freeStreamHC( m_streamHC );
if( m_streamZstd ) ZSTD_freeCStream( m_streamZstd );
}
char* GetBuffer() { return m_buf; }
const char* Compress( uint32_t& sz )
{
if( m_stream )
{
sz = LZ4_compress_fast_continue( m_stream, m_buf, m_compressed, sz, FileBoundSize, 1 );
}
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 );
sz = out.pos;
}
else
{
sz = LZ4_compress_HC_continue( m_streamHC, m_buf, m_compressed, sz, FileBoundSize );
}
std::swap( m_buf, m_second );
return m_compressed;
}
private:
LZ4_stream_t* m_stream;
LZ4_streamHC_t* m_streamHC;
ZSTD_CStream* m_streamZstd;
char m_bufData[2][FileBufSize];
char* m_buf;
char* m_second;
char m_compressed[FileBoundSize];
};
2017-09-30 14:19:50 +00:00
class FileWrite
{
public:
static FileWrite* Open( const char* fn, FileCompression comp = FileCompression::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()
{
Finish();
2017-09-30 14:19:50 +00:00
fclose( m_file );
}
2020-02-08 11:57:35 +00:00
void Finish()
{
if( m_offset > 0 ) WriteBlock();
2020-02-08 11:57:35 +00:00
}
tracy_force_inline void Write( const void* ptr, size_t size )
2017-09-30 14:19:50 +00:00
{
if( m_offset + size <= FileBufSize )
2017-09-30 16:37:32 +00:00
{
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:
FileWrite( FILE* f, FileCompression comp, int level )
: m_stream( comp, level )
2017-09-30 17:25:24 +00:00
, m_file( f )
, m_buf( m_stream.GetBuffer() )
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 )
{
if( comp == FileCompression::Zstd )
2020-02-08 14:43:01 +00:00
{
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, FileBufSize - m_offset );
memcpy( m_buf + m_offset, src, sz );
m_offset += sz;
src += sz;
size -= sz;
if( m_offset == FileBufSize )
{
WriteBlock();
}
}
}
void WriteBlock()
2017-09-30 17:25:24 +00:00
{
uint32_t sz = m_offset;
2020-02-08 11:57:35 +00:00
m_srcBytes += m_offset;
auto block = m_stream.Compress( sz );
2020-02-08 11:57:35 +00:00
m_dstBytes += sz;
2017-09-30 17:25:24 +00:00
fwrite( &sz, 1, sizeof( sz ), m_file );
fwrite( block, 1, sz, m_file );
2017-09-30 17:25:24 +00:00
m_offset = 0;
m_buf = m_stream.GetBuffer();
2017-09-30 17:25:24 +00:00
}
WriteStream m_stream;
2017-09-30 14:19:50 +00:00
FILE* m_file;
char* m_buf;
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