2017-08-18 23:52:05 +00:00
/*
2017-01-20 22:00:41 +00:00
* Copyright ( c ) 2016 - present , Yann Collet , Facebook , Inc .
* All rights reserved .
*
2017-08-18 23:52:05 +00:00
* This source code is licensed under both the BSD - style license ( found in the
* LICENSE file in the root directory of this source tree ) and the GPLv2 ( found
* in the COPYING file in the root directory of this source tree ) .
2017-09-08 07:09:23 +00:00
* You may select , at your option , one of the above - listed licenses .
2017-01-20 22:00:41 +00:00
*/
2017-01-19 23:32:07 +00:00
/* ====== Tuning parameters ====== */
2018-02-02 03:29:30 +00:00
# define ZSTDMT_NBWORKERS_MAX 200
2018-01-17 20:39:58 +00:00
# define ZSTDMT_JOBSIZE_MAX (MEM_32bits() ? (512 MB) : (2 GB)) /* note : limited by `jobSize` type, which is `unsigned` */
2017-07-13 09:22:58 +00:00
# define ZSTDMT_OVERLAPLOG_DEFAULT 6
2017-01-22 06:06:49 +00:00
2017-01-20 20:23:30 +00:00
/* ====== Compiler specifics ====== */
# if defined(_MSC_VER)
2017-06-03 01:20:48 +00:00
# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
2017-01-19 23:32:07 +00:00
# endif
2017-01-20 20:23:30 +00:00
/* ====== Dependencies ====== */
2017-06-03 01:20:48 +00:00
# include <string.h> /* memcpy, memset */
2018-01-13 21:18:57 +00:00
# include <limits.h> /* INT_MAX */
2017-06-03 01:20:48 +00:00
# include "pool.h" /* threadpool */
# include "threading.h" /* mutex */
2017-11-08 00:15:23 +00:00
# include "zstd_compress_internal.h" /* MIN, ERROR, ZSTD_*, ZSTD_highbit32 */
2018-03-07 03:51:21 +00:00
# include "zstd_ldm.h"
2016-12-27 06:19:36 +00:00
# include "zstdmt_compress.h"
2018-03-07 03:51:21 +00:00
/* Guards code to support resizing the SeqPool.
* We will want to resize the SeqPool to save memory in the future .
* Until then , comment the code out since it is unused .
*/
# define ZSTD_RESIZE_SEQPOOL 0
2017-01-20 20:23:30 +00:00
/* ====== Debug ====== */
2018-06-13 18:59:26 +00:00
# if defined(DEBUGLEVEL) && (DEBUGLEVEL>=2)
2017-01-17 23:31:16 +00:00
2016-12-27 06:19:36 +00:00
# include <stdio.h>
2016-12-29 00:24:01 +00:00
# include <unistd.h>
# include <sys / times.h>
2017-01-19 18:18:17 +00:00
2017-05-30 23:12:06 +00:00
# define DEBUG_PRINTHEX(l,p,n) { \
unsigned debug_u ; \
for ( debug_u = 0 ; debug_u < ( n ) ; debug_u + + ) \
2018-06-13 18:59:26 +00:00
RAWLOG ( l , " %02X " , ( ( const unsigned char * ) ( p ) ) [ debug_u ] ) ; \
RAWLOG ( l , " \n " ) ; \
2017-01-19 18:18:17 +00:00
}
2016-12-29 00:24:01 +00:00
2017-03-30 23:47:19 +00:00
static unsigned long long GetCurrentClockTimeMicroseconds ( void )
2016-12-29 00:24:01 +00:00
{
static clock_t _ticksPerSecond = 0 ;
if ( _ticksPerSecond < = 0 ) _ticksPerSecond = sysconf ( _SC_CLK_TCK ) ;
2017-03-30 23:47:19 +00:00
{ struct tms junk ; clock_t newTicks = ( clock_t ) times ( & junk ) ;
return ( ( ( ( unsigned long long ) newTicks ) * ( 1000000 ) ) / _ticksPerSecond ) ; }
2016-12-29 00:24:01 +00:00
}
2017-07-01 13:59:24 +00:00
# define MUTEX_WAIT_TIME_DLEVEL 6
2017-09-27 23:39:40 +00:00
# define ZSTD_PTHREAD_MUTEX_LOCK(mutex) { \
2018-06-13 18:59:26 +00:00
if ( DEBUGLEVEL > = MUTEX_WAIT_TIME_DLEVEL ) { \
2017-05-30 23:12:06 +00:00
unsigned long long const beforeTime = GetCurrentClockTimeMicroseconds ( ) ; \
2017-09-27 23:39:40 +00:00
ZSTD_pthread_mutex_lock ( mutex ) ; \
2017-05-30 23:12:06 +00:00
{ unsigned long long const afterTime = GetCurrentClockTimeMicroseconds ( ) ; \
unsigned long long const elapsedTime = ( afterTime - beforeTime ) ; \
if ( elapsedTime > 1000 ) { /* or whatever threshold you like; I'm using 1 millisecond here */ \
DEBUGLOG ( MUTEX_WAIT_TIME_DLEVEL , " Thread took %llu microseconds to acquire mutex %s \n " , \
elapsedTime , # mutex ) ; \
} } \
2017-09-27 00:57:38 +00:00
} else { \
2017-09-27 23:39:40 +00:00
ZSTD_pthread_mutex_lock ( mutex ) ; \
2017-09-27 00:57:38 +00:00
} \
2017-05-30 23:12:06 +00:00
}
2016-12-29 00:24:01 +00:00
2016-12-27 06:19:36 +00:00
# else
2016-12-29 00:24:01 +00:00
2017-09-27 18:16:24 +00:00
# define ZSTD_PTHREAD_MUTEX_LOCK(m) ZSTD_pthread_mutex_lock(m)
2017-01-19 18:18:17 +00:00
# define DEBUG_PRINTHEX(l,p,n) {}
2016-12-29 00:24:01 +00:00
2016-12-27 06:19:36 +00:00
# endif
2016-12-29 00:24:01 +00:00
2017-01-12 00:25:46 +00:00
/* ===== Buffer Pool ===== */
2017-07-11 21:14:07 +00:00
/* a single Buffer Pool can be invoked from multiple threads in parallel */
2016-12-27 06:19:36 +00:00
typedef struct buffer_s {
void * start ;
2018-01-26 21:00:14 +00:00
size_t capacity ;
2016-12-27 06:19:36 +00:00
} buffer_t ;
2017-01-18 19:57:34 +00:00
static const buffer_t g_nullBuffer = { NULL , 0 } ;
2017-01-18 01:46:33 +00:00
2016-12-27 06:19:36 +00:00
typedef struct ZSTDMT_bufferPool_s {
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_t poolMutex ;
2017-07-11 22:17:25 +00:00
size_t bufferSize ;
2017-01-20 20:23:30 +00:00
unsigned totalBuffers ;
2016-12-27 06:19:36 +00:00
unsigned nbBuffers ;
2017-05-30 23:12:06 +00:00
ZSTD_customMem cMem ;
2016-12-31 13:45:33 +00:00
buffer_t bTable [ 1 ] ; /* variable size */
2016-12-27 06:19:36 +00:00
} ZSTDMT_bufferPool ;
2018-02-02 03:29:30 +00:00
static ZSTDMT_bufferPool * ZSTDMT_createBufferPool ( unsigned nbWorkers , ZSTD_customMem cMem )
2016-12-31 13:45:33 +00:00
{
2018-02-02 03:29:30 +00:00
unsigned const maxNbBuffers = 2 * nbWorkers + 3 ;
2017-05-30 23:12:06 +00:00
ZSTDMT_bufferPool * const bufPool = ( ZSTDMT_bufferPool * ) ZSTD_calloc (
sizeof ( ZSTDMT_bufferPool ) + ( maxNbBuffers - 1 ) * sizeof ( buffer_t ) , cMem ) ;
2016-12-31 13:45:33 +00:00
if ( bufPool = = NULL ) return NULL ;
2017-09-27 18:16:24 +00:00
if ( ZSTD_pthread_mutex_init ( & bufPool - > poolMutex , NULL ) ) {
2017-07-19 08:05:40 +00:00
ZSTD_free ( bufPool , cMem ) ;
return NULL ;
}
2017-07-11 22:17:25 +00:00
bufPool - > bufferSize = 64 KB ;
2016-12-31 13:45:33 +00:00
bufPool - > totalBuffers = maxNbBuffers ;
2017-01-22 06:06:49 +00:00
bufPool - > nbBuffers = 0 ;
2017-05-30 23:12:06 +00:00
bufPool - > cMem = cMem ;
2016-12-31 13:45:33 +00:00
return bufPool ;
}
static void ZSTDMT_freeBufferPool ( ZSTDMT_bufferPool * bufPool )
{
unsigned u ;
2017-10-03 00:28:57 +00:00
DEBUGLOG ( 3 , " ZSTDMT_freeBufferPool (address:%08X) " , ( U32 ) ( size_t ) bufPool ) ;
2016-12-31 13:45:33 +00:00
if ( ! bufPool ) return ; /* compatibility with free on NULL */
2017-09-29 06:01:31 +00:00
for ( u = 0 ; u < bufPool - > totalBuffers ; u + + ) {
2017-10-03 00:28:57 +00:00
DEBUGLOG ( 4 , " free buffer %2u (address:%08X) " , u , ( U32 ) ( size_t ) bufPool - > bTable [ u ] . start ) ;
2017-05-30 23:12:06 +00:00
ZSTD_free ( bufPool - > bTable [ u ] . start , bufPool - > cMem ) ;
2017-09-29 06:01:31 +00:00
}
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_destroy ( & bufPool - > poolMutex ) ;
2017-05-30 23:12:06 +00:00
ZSTD_free ( bufPool , bufPool - > cMem ) ;
2016-12-31 13:45:33 +00:00
}
2017-06-02 00:56:14 +00:00
/* only works at initialization, not during compression */
static size_t ZSTDMT_sizeof_bufferPool ( ZSTDMT_bufferPool * bufPool )
{
size_t const poolSize = sizeof ( * bufPool )
2018-01-13 21:18:57 +00:00
+ ( bufPool - > totalBuffers - 1 ) * sizeof ( buffer_t ) ;
2017-06-02 00:56:14 +00:00
unsigned u ;
size_t totalBufferSize = 0 ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_lock ( & bufPool - > poolMutex ) ;
2017-06-02 00:56:14 +00:00
for ( u = 0 ; u < bufPool - > totalBuffers ; u + + )
2018-01-26 21:00:14 +00:00
totalBufferSize + = bufPool - > bTable [ u ] . capacity ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & bufPool - > poolMutex ) ;
2017-06-02 00:56:14 +00:00
return poolSize + totalBufferSize ;
}
2018-01-26 19:06:34 +00:00
/* ZSTDMT_setBufferSize() :
* all future buffers provided by this buffer pool will have _at least_ this size
* note : it ' s better for all buffers to have same size ,
* as they become freely interchangeable , reducing malloc / free usages and memory fragmentation */
2017-11-30 00:42:20 +00:00
static void ZSTDMT_setBufferSize ( ZSTDMT_bufferPool * const bufPool , size_t const bSize )
2017-07-11 22:17:25 +00:00
{
2017-11-01 23:57:48 +00:00
ZSTD_pthread_mutex_lock ( & bufPool - > poolMutex ) ;
DEBUGLOG ( 4 , " ZSTDMT_setBufferSize: bSize = %u " , ( U32 ) bSize ) ;
2017-07-11 22:17:25 +00:00
bufPool - > bufferSize = bSize ;
2017-11-01 23:57:48 +00:00
ZSTD_pthread_mutex_unlock ( & bufPool - > poolMutex ) ;
2017-07-11 22:17:25 +00:00
}
2017-07-04 17:36:41 +00:00
/** ZSTDMT_getBuffer() :
2018-01-17 20:10:15 +00:00
* assumption : bufPool must be valid
* @ return : a buffer , with start pointer and size
* note : allocation may fail , in this case , start = = NULL and size = = 0 */
2017-07-11 22:17:25 +00:00
static buffer_t ZSTDMT_getBuffer ( ZSTDMT_bufferPool * bufPool )
2016-12-27 06:19:36 +00:00
{
2017-07-11 22:17:25 +00:00
size_t const bSize = bufPool - > bufferSize ;
2017-11-01 23:57:48 +00:00
DEBUGLOG ( 5 , " ZSTDMT_getBuffer: bSize = %u " , ( U32 ) bufPool - > bufferSize ) ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_lock ( & bufPool - > poolMutex ) ;
2017-07-11 21:14:07 +00:00
if ( bufPool - > nbBuffers ) { /* try to use an existing buffer */
buffer_t const buf = bufPool - > bTable [ - - ( bufPool - > nbBuffers ) ] ;
2018-01-26 21:00:14 +00:00
size_t const availBufferSize = buf . capacity ;
2017-09-29 23:27:47 +00:00
bufPool - > bTable [ bufPool - > nbBuffers ] = g_nullBuffer ;
2017-11-01 23:57:48 +00:00
if ( ( availBufferSize > = bSize ) & ( ( availBufferSize > > 3 ) < = bSize ) ) {
2017-05-30 23:12:06 +00:00
/* large enough, but not too much */
2017-11-01 23:57:48 +00:00
DEBUGLOG ( 5 , " ZSTDMT_getBuffer: provide buffer %u of size %u " ,
2018-01-26 21:00:14 +00:00
bufPool - > nbBuffers , ( U32 ) buf . capacity ) ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & bufPool - > poolMutex ) ;
2016-12-27 06:19:36 +00:00
return buf ;
2017-07-11 21:14:07 +00:00
}
2017-05-30 23:12:06 +00:00
/* size conditions not respected : scratch this buffer, create new one */
2017-11-01 23:57:48 +00:00
DEBUGLOG ( 5 , " ZSTDMT_getBuffer: existing buffer does not meet size conditions => freeing " ) ;
2017-07-11 21:14:07 +00:00
ZSTD_free ( buf . start , bufPool - > cMem ) ;
2016-12-27 06:19:36 +00:00
}
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & bufPool - > poolMutex ) ;
2016-12-27 06:19:36 +00:00
/* create new buffer */
2017-11-01 23:57:48 +00:00
DEBUGLOG ( 5 , " ZSTDMT_getBuffer: create a new buffer " ) ;
2017-01-20 20:23:30 +00:00
{ buffer_t buffer ;
2017-07-11 21:14:07 +00:00
void * const start = ZSTD_malloc ( bSize , bufPool - > cMem ) ;
2017-01-22 05:56:36 +00:00
buffer . start = start ; /* note : start can be NULL if malloc fails ! */
2018-01-26 21:00:14 +00:00
buffer . capacity = ( start = = NULL ) ? 0 : bSize ;
2018-01-17 20:10:15 +00:00
if ( start = = NULL ) {
DEBUGLOG ( 5 , " ZSTDMT_getBuffer: buffer allocation failure !! " ) ;
} else {
DEBUGLOG ( 5 , " ZSTDMT_getBuffer: created buffer of size %u " , ( U32 ) bSize ) ;
}
2017-01-22 05:56:36 +00:00
return buffer ;
}
2016-12-27 06:19:36 +00:00
}
2018-03-07 03:51:21 +00:00
# if ZSTD_RESIZE_SEQPOOL
/** ZSTDMT_resizeBuffer() :
* assumption : bufPool must be valid
* @ return : a buffer that is at least the buffer pool buffer size .
* If a reallocation happens , the data in the input buffer is copied .
*/
static buffer_t ZSTDMT_resizeBuffer ( ZSTDMT_bufferPool * bufPool , buffer_t buffer )
{
size_t const bSize = bufPool - > bufferSize ;
if ( buffer . capacity < bSize ) {
void * const start = ZSTD_malloc ( bSize , bufPool - > cMem ) ;
buffer_t newBuffer ;
newBuffer . start = start ;
newBuffer . capacity = start = = NULL ? 0 : bSize ;
if ( start ! = NULL ) {
assert ( newBuffer . capacity > = buffer . capacity ) ;
memcpy ( newBuffer . start , buffer . start , buffer . capacity ) ;
DEBUGLOG ( 5 , " ZSTDMT_resizeBuffer: created buffer of size %u " , ( U32 ) bSize ) ;
return newBuffer ;
}
DEBUGLOG ( 5 , " ZSTDMT_resizeBuffer: buffer allocation failure !! " ) ;
}
return buffer ;
}
# endif
2017-01-11 14:35:56 +00:00
/* store buffer for later re-use, up to pool capacity */
2017-07-11 21:14:07 +00:00
static void ZSTDMT_releaseBuffer ( ZSTDMT_bufferPool * bufPool , buffer_t buf )
2016-12-27 06:19:36 +00:00
{
2017-07-11 22:56:40 +00:00
if ( buf . start = = NULL ) return ; /* compatible with release on NULL */
2017-07-12 00:18:26 +00:00
DEBUGLOG ( 5 , " ZSTDMT_releaseBuffer " ) ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_lock ( & bufPool - > poolMutex ) ;
2017-07-11 21:14:07 +00:00
if ( bufPool - > nbBuffers < bufPool - > totalBuffers ) {
2017-07-12 00:18:26 +00:00
bufPool - > bTable [ bufPool - > nbBuffers + + ] = buf ; /* stored for later use */
2017-11-01 23:57:48 +00:00
DEBUGLOG ( 5 , " ZSTDMT_releaseBuffer: stored buffer of size %u in slot %u " ,
2018-01-26 21:00:14 +00:00
( U32 ) buf . capacity , ( U32 ) ( bufPool - > nbBuffers - 1 ) ) ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & bufPool - > poolMutex ) ;
2016-12-27 06:19:36 +00:00
return ;
}
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & bufPool - > poolMutex ) ;
2016-12-31 13:45:33 +00:00
/* Reached bufferPool capacity (should not happen) */
2017-11-01 23:57:48 +00:00
DEBUGLOG ( 5 , " ZSTDMT_releaseBuffer: pool capacity reached => freeing " ) ;
2017-07-11 21:14:07 +00:00
ZSTD_free ( buf . start , bufPool - > cMem ) ;
2016-12-27 06:19:36 +00:00
}
2018-03-07 03:51:21 +00:00
/* ===== Seq Pool Wrapper ====== */
static rawSeqStore_t kNullRawSeqStore = { NULL , 0 , 0 , 0 } ;
typedef ZSTDMT_bufferPool ZSTDMT_seqPool ;
static size_t ZSTDMT_sizeof_seqPool ( ZSTDMT_seqPool * seqPool )
{
return ZSTDMT_sizeof_bufferPool ( seqPool ) ;
}
static rawSeqStore_t bufferToSeq ( buffer_t buffer )
{
rawSeqStore_t seq = { NULL , 0 , 0 , 0 } ;
seq . seq = ( rawSeq * ) buffer . start ;
seq . capacity = buffer . capacity / sizeof ( rawSeq ) ;
return seq ;
}
static buffer_t seqToBuffer ( rawSeqStore_t seq )
{
buffer_t buffer ;
buffer . start = seq . seq ;
buffer . capacity = seq . capacity * sizeof ( rawSeq ) ;
return buffer ;
}
static rawSeqStore_t ZSTDMT_getSeq ( ZSTDMT_seqPool * seqPool )
{
if ( seqPool - > bufferSize = = 0 ) {
return kNullRawSeqStore ;
}
return bufferToSeq ( ZSTDMT_getBuffer ( seqPool ) ) ;
}
# if ZSTD_RESIZE_SEQPOOL
static rawSeqStore_t ZSTDMT_resizeSeq ( ZSTDMT_seqPool * seqPool , rawSeqStore_t seq )
{
return bufferToSeq ( ZSTDMT_resizeBuffer ( seqPool , seqToBuffer ( seq ) ) ) ;
}
# endif
static void ZSTDMT_releaseSeq ( ZSTDMT_seqPool * seqPool , rawSeqStore_t seq )
{
ZSTDMT_releaseBuffer ( seqPool , seqToBuffer ( seq ) ) ;
}
static void ZSTDMT_setNbSeq ( ZSTDMT_seqPool * const seqPool , size_t const nbSeq )
{
ZSTDMT_setBufferSize ( seqPool , nbSeq * sizeof ( rawSeq ) ) ;
}
static ZSTDMT_seqPool * ZSTDMT_createSeqPool ( unsigned nbWorkers , ZSTD_customMem cMem )
{
ZSTDMT_seqPool * seqPool = ZSTDMT_createBufferPool ( nbWorkers , cMem ) ;
ZSTDMT_setNbSeq ( seqPool , 0 ) ;
return seqPool ;
}
static void ZSTDMT_freeSeqPool ( ZSTDMT_seqPool * seqPool )
{
ZSTDMT_freeBufferPool ( seqPool ) ;
}
2017-01-12 00:25:46 +00:00
/* ===== CCtx Pool ===== */
2017-07-11 21:14:07 +00:00
/* a single CCtx Pool can be invoked from multiple threads in parallel */
2017-07-10 23:30:55 +00:00
2016-12-31 05:04:25 +00:00
typedef struct {
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_t poolMutex ;
2016-12-31 05:04:25 +00:00
unsigned totalCCtx ;
unsigned availCCtx ;
2017-05-30 23:12:06 +00:00
ZSTD_customMem cMem ;
2016-12-31 05:04:25 +00:00
ZSTD_CCtx * cctx [ 1 ] ; /* variable size */
} ZSTDMT_CCtxPool ;
2017-01-11 14:44:26 +00:00
/* note : all CCtx borrowed from the pool should be released back to the pool _before_ freeing the pool */
static void ZSTDMT_freeCCtxPool ( ZSTDMT_CCtxPool * pool )
{
unsigned u ;
2017-01-18 20:12:10 +00:00
for ( u = 0 ; u < pool - > totalCCtx ; u + + )
ZSTD_freeCCtx ( pool - > cctx [ u ] ) ; /* note : compatible with free on NULL */
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_destroy ( & pool - > poolMutex ) ;
2017-05-30 23:12:06 +00:00
ZSTD_free ( pool , pool - > cMem ) ;
2017-01-11 14:44:26 +00:00
}
2017-01-22 06:06:49 +00:00
/* ZSTDMT_createCCtxPool() :
2018-02-02 03:29:30 +00:00
* implies nbWorkers > = 1 , checked by caller ZSTDMT_createCCtx ( ) */
static ZSTDMT_CCtxPool * ZSTDMT_createCCtxPool ( unsigned nbWorkers ,
2017-06-02 00:56:14 +00:00
ZSTD_customMem cMem )
2016-12-31 05:04:25 +00:00
{
2017-05-30 23:12:06 +00:00
ZSTDMT_CCtxPool * const cctxPool = ( ZSTDMT_CCtxPool * ) ZSTD_calloc (
2018-02-02 03:29:30 +00:00
sizeof ( ZSTDMT_CCtxPool ) + ( nbWorkers - 1 ) * sizeof ( ZSTD_CCtx * ) , cMem ) ;
assert ( nbWorkers > 0 ) ;
2016-12-31 05:04:25 +00:00
if ( ! cctxPool ) return NULL ;
2017-09-27 18:16:24 +00:00
if ( ZSTD_pthread_mutex_init ( & cctxPool - > poolMutex , NULL ) ) {
2017-07-19 08:05:40 +00:00
ZSTD_free ( cctxPool , cMem ) ;
return NULL ;
}
2017-05-30 23:12:06 +00:00
cctxPool - > cMem = cMem ;
2018-02-02 03:29:30 +00:00
cctxPool - > totalCCtx = nbWorkers ;
2017-01-23 09:43:58 +00:00
cctxPool - > availCCtx = 1 ; /* at least one cctx for single-thread mode */
2017-05-30 23:12:06 +00:00
cctxPool - > cctx [ 0 ] = ZSTD_createCCtx_advanced ( cMem ) ;
2017-01-23 09:43:58 +00:00
if ( ! cctxPool - > cctx [ 0 ] ) { ZSTDMT_freeCCtxPool ( cctxPool ) ; return NULL ; }
2018-02-02 03:29:30 +00:00
DEBUGLOG ( 3 , " cctxPool created, with %u workers " , nbWorkers ) ;
2016-12-31 05:04:25 +00:00
return cctxPool ;
}
2017-06-02 00:56:14 +00:00
/* only works during initialization phase, not during compression */
static size_t ZSTDMT_sizeof_CCtxPool ( ZSTDMT_CCtxPool * cctxPool )
{
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_lock ( & cctxPool - > poolMutex ) ;
2018-02-02 03:29:30 +00:00
{ unsigned const nbWorkers = cctxPool - > totalCCtx ;
2017-07-10 23:30:55 +00:00
size_t const poolSize = sizeof ( * cctxPool )
2018-02-02 03:29:30 +00:00
+ ( nbWorkers - 1 ) * sizeof ( ZSTD_CCtx * ) ;
2017-07-10 23:30:55 +00:00
unsigned u ;
size_t totalCCtxSize = 0 ;
2018-02-02 03:29:30 +00:00
for ( u = 0 ; u < nbWorkers ; u + + ) {
2017-07-10 23:30:55 +00:00
totalCCtxSize + = ZSTD_sizeof_CCtx ( cctxPool - > cctx [ u ] ) ;
}
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & cctxPool - > poolMutex ) ;
2018-02-02 03:29:30 +00:00
assert ( nbWorkers > 0 ) ;
2017-07-10 23:30:55 +00:00
return poolSize + totalCCtxSize ;
}
2017-06-02 00:56:14 +00:00
}
2017-07-10 23:30:55 +00:00
static ZSTD_CCtx * ZSTDMT_getCCtx ( ZSTDMT_CCtxPool * cctxPool )
2016-12-31 05:04:25 +00:00
{
2017-07-10 23:30:55 +00:00
DEBUGLOG ( 5 , " ZSTDMT_getCCtx " ) ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_lock ( & cctxPool - > poolMutex ) ;
2017-07-10 23:30:55 +00:00
if ( cctxPool - > availCCtx ) {
cctxPool - > availCCtx - - ;
{ ZSTD_CCtx * const cctx = cctxPool - > cctx [ cctxPool - > availCCtx ] ;
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & cctxPool - > poolMutex ) ;
2017-07-10 23:30:55 +00:00
return cctx ;
} }
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & cctxPool - > poolMutex ) ;
2017-07-10 23:30:55 +00:00
DEBUGLOG ( 5 , " create one more CCtx " ) ;
return ZSTD_createCCtx_advanced ( cctxPool - > cMem ) ; /* note : can be NULL, when creation fails ! */
2016-12-27 06:19:36 +00:00
}
2016-12-31 05:04:25 +00:00
static void ZSTDMT_releaseCCtx ( ZSTDMT_CCtxPool * pool , ZSTD_CCtx * cctx )
{
2017-01-18 20:12:10 +00:00
if ( cctx = = NULL ) return ; /* compatibility with release on NULL */
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_lock ( & pool - > poolMutex ) ;
2016-12-31 05:04:25 +00:00
if ( pool - > availCCtx < pool - > totalCCtx )
pool - > cctx [ pool - > availCCtx + + ] = cctx ;
2017-07-10 23:30:55 +00:00
else {
2018-02-02 03:29:30 +00:00
/* pool overflow : should not happen, since totalCCtx==nbWorkers */
DEBUGLOG ( 4 , " CCtx pool overflow : free cctx " ) ;
2016-12-31 05:04:25 +00:00
ZSTD_freeCCtx ( cctx ) ;
2017-07-10 23:30:55 +00:00
}
2017-09-27 18:16:24 +00:00
ZSTD_pthread_mutex_unlock ( & pool - > poolMutex ) ;
2016-12-31 05:04:25 +00:00
}
2018-03-01 04:10:44 +00:00
/* ==== Serial State ==== */
2017-01-12 00:25:46 +00:00
2018-02-28 04:09:18 +00:00
typedef struct {
void const * start ;
size_t size ;
} range_t ;
2018-03-01 04:10:44 +00:00
typedef struct {
2018-03-07 03:51:21 +00:00
/* All variables in the struct are protected by mutex. */
2018-03-01 04:10:44 +00:00
ZSTD_pthread_mutex_t mutex ;
ZSTD_pthread_cond_t cond ;
ZSTD_CCtx_params params ;
2018-03-07 03:51:21 +00:00
ldmState_t ldmState ;
2018-03-01 04:10:44 +00:00
XXH64_state_t xxhState ;
unsigned nextJobID ;
2018-03-07 03:51:21 +00:00
/* Protects ldmWindow.
* Must be acquired after the main mutex when acquiring both .
*/
ZSTD_pthread_mutex_t ldmWindowMutex ;
ZSTD_pthread_cond_t ldmWindowCond ; /* Signaled when ldmWindow is udpated */
ZSTD_window_t ldmWindow ; /* A thread-safe copy of ldmState.window */
2018-03-01 04:10:44 +00:00
} serialState_t ;
2018-03-07 03:51:21 +00:00
static int ZSTDMT_serialState_reset ( serialState_t * serialState , ZSTDMT_seqPool * seqPool , ZSTD_CCtx_params params )
2018-03-01 04:10:44 +00:00
{
2018-03-07 03:51:21 +00:00
/* Adjust parameters */
if ( params . ldmParams . enableLdm ) {
DEBUGLOG ( 4 , " LDM window size = %u KB " , ( 1U < < params . cParams . windowLog ) > > 10 ) ;
params . ldmParams . windowLog = params . cParams . windowLog ;
ZSTD_ldm_adjustParameters ( & params . ldmParams , & params . cParams ) ;
assert ( params . ldmParams . hashLog > = params . ldmParams . bucketSizeLog ) ;
assert ( params . ldmParams . hashEveryLog < 32 ) ;
serialState - > ldmState . hashPower =
ZSTD_ldm_getHashPower ( params . ldmParams . minMatchLength ) ;
2018-03-20 01:23:54 +00:00
} else {
memset ( & params . ldmParams , 0 , sizeof ( params . ldmParams ) ) ;
2018-03-07 03:51:21 +00:00
}
2018-03-01 04:10:44 +00:00
serialState - > nextJobID = 0 ;
if ( params . fParams . checksumFlag )
XXH64_reset ( & serialState - > xxhState , 0 ) ;
2018-03-07 03:51:21 +00:00
if ( params . ldmParams . enableLdm ) {
ZSTD_customMem cMem = params . customMem ;
unsigned const hashLog = params . ldmParams . hashLog ;
size_t const hashSize = ( ( size_t ) 1 < < hashLog ) * sizeof ( ldmEntry_t ) ;
unsigned const bucketLog =
params . ldmParams . hashLog - params . ldmParams . bucketSizeLog ;
size_t const bucketSize = ( size_t ) 1 < < bucketLog ;
unsigned const prevBucketLog =
serialState - > params . ldmParams . hashLog -
serialState - > params . ldmParams . bucketSizeLog ;
/* Size the seq pool tables */
ZSTDMT_setNbSeq ( seqPool , ZSTD_ldm_getMaxNbSeq ( params . ldmParams , params . jobSize ) ) ;
/* Reset the window */
ZSTD_window_clear ( & serialState - > ldmState . window ) ;
serialState - > ldmWindow = serialState - > ldmState . window ;
/* Resize tables and output space if necessary. */
2018-03-20 00:54:04 +00:00
if ( serialState - > ldmState . hashTable = = NULL | | serialState - > params . ldmParams . hashLog < hashLog ) {
2018-03-07 03:51:21 +00:00
ZSTD_free ( serialState - > ldmState . hashTable , cMem ) ;
serialState - > ldmState . hashTable = ( ldmEntry_t * ) ZSTD_malloc ( hashSize , cMem ) ;
}
2018-03-20 00:54:04 +00:00
if ( serialState - > ldmState . bucketOffsets = = NULL | | prevBucketLog < bucketLog ) {
2018-03-07 03:51:21 +00:00
ZSTD_free ( serialState - > ldmState . bucketOffsets , cMem ) ;
serialState - > ldmState . bucketOffsets = ( BYTE * ) ZSTD_malloc ( bucketSize , cMem ) ;
}
if ( ! serialState - > ldmState . hashTable | | ! serialState - > ldmState . bucketOffsets )
return 1 ;
/* Zero the tables */
memset ( serialState - > ldmState . hashTable , 0 , hashSize ) ;
memset ( serialState - > ldmState . bucketOffsets , 0 , bucketSize ) ;
}
2018-03-01 04:10:44 +00:00
serialState - > params = params ;
2018-03-07 03:51:21 +00:00
return 0 ;
2018-03-01 04:10:44 +00:00
}
static int ZSTDMT_serialState_init ( serialState_t * serialState )
{
int initError = 0 ;
2018-03-07 03:51:21 +00:00
memset ( serialState , 0 , sizeof ( * serialState ) ) ;
2018-03-01 04:10:44 +00:00
initError | = ZSTD_pthread_mutex_init ( & serialState - > mutex , NULL ) ;
initError | = ZSTD_pthread_cond_init ( & serialState - > cond , NULL ) ;
2018-03-07 03:51:21 +00:00
initError | = ZSTD_pthread_mutex_init ( & serialState - > ldmWindowMutex , NULL ) ;
initError | = ZSTD_pthread_cond_init ( & serialState - > ldmWindowCond , NULL ) ;
2018-03-01 04:10:44 +00:00
return initError ;
}
static void ZSTDMT_serialState_free ( serialState_t * serialState )
{
2018-03-07 03:51:21 +00:00
ZSTD_customMem cMem = serialState - > params . customMem ;
2018-03-01 04:10:44 +00:00
ZSTD_pthread_mutex_destroy ( & serialState - > mutex ) ;
ZSTD_pthread_cond_destroy ( & serialState - > cond ) ;
2018-03-07 03:51:21 +00:00
ZSTD_pthread_mutex_destroy ( & serialState - > ldmWindowMutex ) ;
ZSTD_pthread_cond_destroy ( & serialState - > ldmWindowCond ) ;
ZSTD_free ( serialState - > ldmState . hashTable , cMem ) ;
ZSTD_free ( serialState - > ldmState . bucketOffsets , cMem ) ;
2018-03-01 04:10:44 +00:00
}
2018-03-07 03:51:21 +00:00
static void ZSTDMT_serialState_update ( serialState_t * serialState ,
ZSTD_CCtx * jobCCtx , rawSeqStore_t seqStore ,
range_t src , unsigned jobID )
2018-03-01 04:10:44 +00:00
{
/* Wait for our turn */
ZSTD_PTHREAD_MUTEX_LOCK ( & serialState - > mutex ) ;
while ( serialState - > nextJobID < jobID ) {
ZSTD_pthread_cond_wait ( & serialState - > cond , & serialState - > mutex ) ;
}
/* A future job may error and skip our job */
if ( serialState - > nextJobID = = jobID ) {
/* It is now our turn, do any processing necessary */
2018-03-07 03:51:21 +00:00
if ( serialState - > params . ldmParams . enableLdm ) {
size_t error ;
assert ( seqStore . seq ! = NULL & & seqStore . pos = = 0 & &
seqStore . size = = 0 & & seqStore . capacity > 0 ) ;
ZSTD_window_update ( & serialState - > ldmState . window , src . start , src . size ) ;
error = ZSTD_ldm_generateSequences (
& serialState - > ldmState , & seqStore ,
& serialState - > params . ldmParams , src . start , src . size ) ;
/* We provide a large enough buffer to never fail. */
assert ( ! ZSTD_isError ( error ) ) ; ( void ) error ;
/* Update ldmWindow to match the ldmState.window and signal the main
* thread if it is waiting for a buffer .
*/
ZSTD_PTHREAD_MUTEX_LOCK ( & serialState - > ldmWindowMutex ) ;
serialState - > ldmWindow = serialState - > ldmState . window ;
ZSTD_pthread_cond_signal ( & serialState - > ldmWindowCond ) ;
ZSTD_pthread_mutex_unlock ( & serialState - > ldmWindowMutex ) ;
}
2018-03-01 04:10:44 +00:00
if ( serialState - > params . fParams . checksumFlag & & src . size > 0 )
XXH64_update ( & serialState - > xxhState , src . start , src . size ) ;
}
/* Now it is the next jobs turn */
serialState - > nextJobID + + ;
ZSTD_pthread_cond_broadcast ( & serialState - > cond ) ;
ZSTD_pthread_mutex_unlock ( & serialState - > mutex ) ;
2018-03-07 03:51:21 +00:00
if ( seqStore . size > 0 ) {
size_t const err = ZSTD_referenceExternalSequences (
jobCCtx , seqStore . seq , seqStore . size ) ;
assert ( serialState - > params . ldmParams . enableLdm ) ;
assert ( ! ZSTD_isError ( err ) ) ;
( void ) err ;
}
2018-03-01 04:10:44 +00:00
}
static void ZSTDMT_serialState_ensureFinished ( serialState_t * serialState ,
unsigned jobID , size_t cSize )
{
ZSTD_PTHREAD_MUTEX_LOCK ( & serialState - > mutex ) ;
if ( serialState - > nextJobID < = jobID ) {
assert ( ZSTD_isError ( cSize ) ) ; ( void ) cSize ;
DEBUGLOG ( 5 , " Skipping past job %u because of error " , jobID ) ;
serialState - > nextJobID = jobID + 1 ;
ZSTD_pthread_cond_broadcast ( & serialState - > cond ) ;
2018-03-07 03:51:21 +00:00
ZSTD_PTHREAD_MUTEX_LOCK ( & serialState - > ldmWindowMutex ) ;
ZSTD_window_clear ( & serialState - > ldmWindow ) ;
ZSTD_pthread_cond_signal ( & serialState - > ldmWindowCond ) ;
ZSTD_pthread_mutex_unlock ( & serialState - > ldmWindowMutex ) ;
2018-03-01 04:10:44 +00:00
}
ZSTD_pthread_mutex_unlock ( & serialState - > mutex ) ;
}
/* ------------------------------------------ */
/* ===== Worker thread ===== */
/* ------------------------------------------ */
2018-02-28 04:09:18 +00:00
static const range_t kNullRange = { NULL , 0 } ;
2017-01-12 00:25:46 +00:00
typedef struct {
2018-01-26 18:44:09 +00:00
size_t consumed ; /* SHARED - set0 by mtctx, then modified by worker AND read by mtctx */
2018-01-26 20:15:43 +00:00
size_t cSize ; /* SHARED - set0 by mtctx, then modified by worker AND read by mtctx, then set0 by mtctx */
2018-01-27 01:48:33 +00:00
ZSTD_pthread_mutex_t job_mutex ; /* Thread-safe - used by mtctx and worker */
ZSTD_pthread_cond_t job_cond ; /* Thread-safe - used by mtctx and worker */
2018-01-26 20:15:43 +00:00
ZSTDMT_CCtxPool * cctxPool ; /* Thread-safe - used by mtctx and (all) workers */
ZSTDMT_bufferPool * bufPool ; /* Thread-safe - used by mtctx and (all) workers */
2018-03-07 03:51:21 +00:00
ZSTDMT_seqPool * seqPool ; /* Thread-safe - used by mtctx and (all) workers */
2018-03-01 04:10:44 +00:00
serialState_t * serial ; /* Thread-safe - used by mtctx and (all) workers */
2018-01-26 20:15:43 +00:00
buffer_t dstBuff ; /* set by worker (or mtctx), then read by worker & mtctx, then modified by mtctx => no barrier */
2018-02-28 04:09:18 +00:00
range_t prefix ; /* set by mtctx, then read by worker & mtctx => no barrier */
range_t src ; /* set by mtctx, then read by worker & mtctx => no barrier */
2018-03-01 04:10:44 +00:00
unsigned jobID ; /* set by mtctx, then read by worker => no barrier */
2018-01-30 22:43:36 +00:00
unsigned firstJob ; /* set by mtctx, then read by worker => no barrier */
unsigned lastJob ; /* set by mtctx, then read by worker => no barrier */
2018-01-26 18:20:38 +00:00
ZSTD_CCtx_params params ; /* set by mtctx, then read by worker => no barrier */
const ZSTD_CDict * cdict ; /* set by mtctx, then read by worker => no barrier */
unsigned long long fullFrameSize ; /* set by mtctx, then read by worker => no barrier */
size_t dstFlushed ; /* used only by mtctx */
unsigned frameChecksumNeeded ; /* used only by mtctx */
2017-01-12 00:25:46 +00:00
} ZSTDMT_jobDescription ;
2018-01-30 22:43:36 +00:00
/* ZSTDMT_compressionJob() is a POOL_function type */
void ZSTDMT_compressionJob ( void * jobDescription )
2017-01-12 00:25:46 +00:00
{
ZSTDMT_jobDescription * const job = ( ZSTDMT_jobDescription * ) jobDescription ;
2018-03-01 04:10:44 +00:00
ZSTD_CCtx_params jobParams = job - > params ; /* do not modify job->params ! copy it, modify the copy */
2017-11-01 23:57:48 +00:00
ZSTD_CCtx * const cctx = ZSTDMT_getCCtx ( job - > cctxPool ) ;
2018-03-07 03:51:21 +00:00
rawSeqStore_t rawSeqStore = ZSTDMT_getSeq ( job - > seqPool ) ;
2017-07-11 21:59:10 +00:00
buffer_t dstBuff = job - > dstBuff ;
2017-07-10 23:30:55 +00:00
2018-03-07 03:51:21 +00:00
/* Don't compute the checksum for chunks, since we compute it externally,
* but write it in the header .
*/
2018-03-01 04:10:44 +00:00
if ( job - > jobID ! = 0 ) jobParams . fParams . checksumFlag = 0 ;
2018-03-07 03:51:21 +00:00
/* Don't run LDM for the chunks, since we handle it externally */
jobParams . ldmParams . enableLdm = 0 ;
2018-03-01 04:10:44 +00:00
2018-01-13 21:18:57 +00:00
/* ressources */
2017-07-10 23:30:55 +00:00
if ( cctx = = NULL ) {
job - > cSize = ERROR ( memory_allocation ) ;
goto _endJob ;
}
2018-01-26 20:15:43 +00:00
if ( dstBuff . start = = NULL ) { /* streaming job : doesn't provide a dstBuffer */
2017-07-11 22:17:25 +00:00
dstBuff = ZSTDMT_getBuffer ( job - > bufPool ) ;
2017-07-11 21:59:10 +00:00
if ( dstBuff . start = = NULL ) {
job - > cSize = ERROR ( memory_allocation ) ;
goto _endJob ;
}
2018-01-26 01:35:49 +00:00
job - > dstBuff = dstBuff ; /* this value can be read in ZSTDMT_flush, when it copies the whole job */
2017-07-11 21:59:10 +00:00
}
2018-01-13 21:18:57 +00:00
/* init */
2017-10-14 01:32:06 +00:00
if ( job - > cdict ) {
2018-04-02 21:41:30 +00:00
size_t const initError = ZSTD_compressBegin_advanced_internal ( cctx , NULL , 0 , ZSTD_dct_auto , ZSTD_dtlm_fast , job - > cdict , jobParams , job - > fullFrameSize ) ;
2018-01-30 22:43:36 +00:00
assert ( job - > firstJob ) ; /* only allowed for first job */
2017-01-23 00:40:06 +00:00
if ( ZSTD_isError ( initError ) ) { job - > cSize = initError ; goto _endJob ; }
2017-02-24 07:09:10 +00:00
} else { /* srcStart points at reloaded section */
2018-02-28 04:09:18 +00:00
U64 const pledgedSrcSize = job - > firstJob ? job - > fullFrameSize : job - > src . size ;
2018-01-30 22:43:36 +00:00
{ size_t const forceWindowError = ZSTD_CCtxParam_setParameter ( & jobParams , ZSTD_p_forceMaxWindow , ! job - > firstJob ) ;
2018-01-13 21:18:57 +00:00
if ( ZSTD_isError ( forceWindowError ) ) {
job - > cSize = forceWindowError ;
goto _endJob ;
} }
fix a subtle issue in continue mode
The deep fuzzer tests caught a subtle bug that was probably there for a long time.
The impact of the bug is not a crash, or any other clear error signal,
rather, it reduces performance, by cutting data into smaller blocks.
Eventually, the following test would fail because it produces too many 1-byte blocks,
requiring more space than buffer can provide :
`./zstreamtest_asan --mt -s3514 -t1678312 -i1678314`
The root scenario is as follows :
- Create context, initialize it using explicit parameters or a `cdict` to pin them down, set `pledgedSrcSize=1`
- The compression parameters will not be adapted, but `windowSize` and `blockSize` will be automatically set to `1`.
`windowSize` and `blockSize` are dynamic values, set within `ZSTD_resetCCtx_internal()`.
The automatic adaptation makes it possible to generate smaller contexts for smaller input sizes.
- Complete compression
- New compression with same context, using same parameters, but `pledgedSrcSize=ZSTD_CONTENTSIZE_UNKNOWN`
trigger "continue mode"
- Continue mode doesn't modify blockSize, because it used to depend on `windowLog` only,
but in fact, it also depends on `pledgedSrcSize`.
- The "old" blocksize (1) is still there,
next compression will use this value to cut input into blocks,
resulting in more blocks and worse performance than necessary performance.
Given the scenario, and its possible variants, I'm surprised it did not show up before.
But I suspect it did show up, it's just that it never triggered an error, because "worse performance" is not a trigger.
The above test is a special corner case, where performance is so impacted that it reaches an error case.
The fix works, but I'm not completely pleased.
I think the current code relies too much on implied relations between variables.
This will likely break again in the future when some related part of the code change.
Unfortunately, no time to make larger changes if we want to keep the release target for zstd v1.3.3.
So a longer term fix will have to be considered after the release.
To do : create a reliable test case which triggers this scenario for CI tests.
2017-12-19 08:43:03 +00:00
{ size_t const initError = ZSTD_compressBegin_advanced_internal ( cctx ,
2018-03-20 22:13:14 +00:00
job - > prefix . start , job - > prefix . size , ZSTD_dct_rawContent , /* load dictionary in "content-only" mode (no header analysis) */
2018-04-02 21:41:30 +00:00
ZSTD_dtlm_fast ,
2018-01-16 23:34:41 +00:00
NULL , /*cdict*/
fix a subtle issue in continue mode
The deep fuzzer tests caught a subtle bug that was probably there for a long time.
The impact of the bug is not a crash, or any other clear error signal,
rather, it reduces performance, by cutting data into smaller blocks.
Eventually, the following test would fail because it produces too many 1-byte blocks,
requiring more space than buffer can provide :
`./zstreamtest_asan --mt -s3514 -t1678312 -i1678314`
The root scenario is as follows :
- Create context, initialize it using explicit parameters or a `cdict` to pin them down, set `pledgedSrcSize=1`
- The compression parameters will not be adapted, but `windowSize` and `blockSize` will be automatically set to `1`.
`windowSize` and `blockSize` are dynamic values, set within `ZSTD_resetCCtx_internal()`.
The automatic adaptation makes it possible to generate smaller contexts for smaller input sizes.
- Complete compression
- New compression with same context, using same parameters, but `pledgedSrcSize=ZSTD_CONTENTSIZE_UNKNOWN`
trigger "continue mode"
- Continue mode doesn't modify blockSize, because it used to depend on `windowLog` only,
but in fact, it also depends on `pledgedSrcSize`.
- The "old" blocksize (1) is still there,
next compression will use this value to cut input into blocks,
resulting in more blocks and worse performance than necessary performance.
Given the scenario, and its possible variants, I'm surprised it did not show up before.
But I suspect it did show up, it's just that it never triggered an error, because "worse performance" is not a trigger.
The above test is a special corner case, where performance is so impacted that it reaches an error case.
The fix works, but I'm not completely pleased.
I think the current code relies too much on implied relations between variables.
This will likely break again in the future when some related part of the code change.
Unfortunately, no time to make larger changes if we want to keep the release target for zstd v1.3.3.
So a longer term fix will have to be considered after the release.
To do : create a reliable test case which triggers this scenario for CI tests.
2017-12-19 08:43:03 +00:00
jobParams , pledgedSrcSize ) ;
2017-12-13 00:20:51 +00:00
if ( ZSTD_isError ( initError ) ) {
job - > cSize = initError ;
goto _endJob ;
2018-01-26 01:35:49 +00:00
} } }
2018-03-01 04:10:44 +00:00
2018-03-07 03:51:21 +00:00
/* Perform serial step as early as possible, but after CCtx initialization */
ZSTDMT_serialState_update ( job - > serial , cctx , rawSeqStore , job - > src , job - > jobID ) ;
2018-03-01 04:10:44 +00:00
2018-01-30 22:43:36 +00:00
if ( ! job - > firstJob ) { /* flush and overwrite frame header when it's not first job */
2018-02-28 04:09:18 +00:00
size_t const hSize = ZSTD_compressContinue ( cctx , dstBuff . start , dstBuff . capacity , job - > src . start , 0 ) ;
2017-12-13 00:20:51 +00:00
if ( ZSTD_isError ( hSize ) ) { job - > cSize = hSize ; /* save error code */ goto _endJob ; }
2018-01-30 22:43:36 +00:00
DEBUGLOG ( 5 , " ZSTDMT_compressionJob: flush and overwrite %u bytes of frame header (not first job) " , ( U32 ) hSize ) ;
2017-07-10 23:30:55 +00:00
ZSTD_invalidateRepCodes ( cctx ) ;
2017-01-19 18:18:17 +00:00
}
2017-01-12 00:25:46 +00:00
2018-01-13 21:18:57 +00:00
/* compress */
2018-01-30 22:43:36 +00:00
{ size_t const chunkSize = 4 * ZSTD_BLOCKSIZE_MAX ;
2018-02-28 04:09:18 +00:00
int const nbChunks = ( int ) ( ( job - > src . size + ( chunkSize - 1 ) ) / chunkSize ) ;
const BYTE * ip = ( const BYTE * ) job - > src . start ;
2018-01-13 21:18:57 +00:00
BYTE * const ostart = ( BYTE * ) dstBuff . start ;
BYTE * op = ostart ;
2018-01-26 21:00:14 +00:00
BYTE * oend = op + dstBuff . capacity ;
2018-01-30 22:43:36 +00:00
int chunkNb ;
2018-02-28 04:09:18 +00:00
if ( sizeof ( size_t ) > sizeof ( int ) ) assert ( job - > src . size < ( ( size_t ) INT_MAX ) * chunkSize ) ; /* check overflow */
DEBUGLOG ( 5 , " ZSTDMT_compressionJob: compress %u bytes in %i blocks " , ( U32 ) job - > src . size , nbChunks ) ;
2018-01-18 01:18:19 +00:00
assert ( job - > cSize = = 0 ) ;
2018-01-30 22:43:36 +00:00
for ( chunkNb = 1 ; chunkNb < nbChunks ; chunkNb + + ) {
size_t const cSize = ZSTD_compressContinue ( cctx , op , oend - op , ip , chunkSize ) ;
2018-01-13 21:18:57 +00:00
if ( ZSTD_isError ( cSize ) ) { job - > cSize = cSize ; goto _endJob ; }
2018-01-30 22:43:36 +00:00
ip + = chunkSize ;
2018-01-13 21:18:57 +00:00
op + = cSize ; assert ( op < oend ) ;
/* stats */
2018-01-30 22:43:36 +00:00
ZSTD_PTHREAD_MUTEX_LOCK ( & job - > job_mutex ) ;
2018-01-13 21:18:57 +00:00
job - > cSize + = cSize ;
2018-01-30 22:43:36 +00:00
job - > consumed = chunkSize * chunkNb ;
DEBUGLOG ( 5 , " ZSTDMT_compressionJob: compress new block : cSize==%u bytes (total: %u) " ,
2018-01-18 19:03:27 +00:00
( U32 ) cSize , ( U32 ) job - > cSize ) ;
2018-01-27 01:48:33 +00:00
ZSTD_pthread_cond_signal ( & job - > job_cond ) ; /* warns some more data is ready to be flushed */
ZSTD_pthread_mutex_unlock ( & job - > job_mutex ) ;
2018-01-13 21:18:57 +00:00
}
/* last block */
2018-01-30 22:43:36 +00:00
assert ( chunkSize > 0 ) ; assert ( ( chunkSize & ( chunkSize - 1 ) ) = = 0 ) ; /* chunkSize must be power of 2 for mask==(chunkSize-1) to work */
if ( ( nbChunks > 0 ) | job - > lastJob /*must output a "last block" flag*/ ) {
2018-02-28 04:09:18 +00:00
size_t const lastBlockSize1 = job - > src . size & ( chunkSize - 1 ) ;
size_t const lastBlockSize = ( ( lastBlockSize1 = = 0 ) & ( job - > src . size > = chunkSize ) ) ? chunkSize : lastBlockSize1 ;
2018-01-30 22:43:36 +00:00
size_t const cSize = ( job - > lastJob ) ?
2018-01-13 21:18:57 +00:00
ZSTD_compressEnd ( cctx , op , oend - op , ip , lastBlockSize ) :
ZSTD_compressContinue ( cctx , op , oend - op , ip , lastBlockSize ) ;
if ( ZSTD_isError ( cSize ) ) { job - > cSize = cSize ; goto _endJob ; }
/* stats */
2018-01-27 01:48:33 +00:00
ZSTD_PTHREAD_MUTEX_LOCK ( & job - > job_mutex ) ;
2018-01-13 21:18:57 +00:00
job - > cSize + = cSize ;
2018-01-27 01:48:33 +00:00
ZSTD_pthread_mutex_unlock ( & job - > job_mutex ) ;
2018-01-26 01:35:49 +00:00
} }
2017-01-12 00:25:46 +00:00
_endJob :
2018-03-01 04:10:44 +00:00
ZSTDMT_serialState_ensureFinished ( job - > serial , job - > jobID , job - > cSize ) ;
2018-02-28 04:09:18 +00:00
if ( job - > prefix . size > 0 )
DEBUGLOG ( 5 , " Finished with prefix: %zx " , ( size_t ) job - > prefix . start ) ;
DEBUGLOG ( 5 , " Finished with source: %zx " , ( size_t ) job - > src . start ) ;
2018-01-26 01:35:49 +00:00
/* release resources */
2018-03-07 03:51:21 +00:00
ZSTDMT_releaseSeq ( job - > seqPool , rawSeqStore ) ;
2017-07-10 23:30:55 +00:00
ZSTDMT_releaseCCtx ( job - > cctxPool , cctx ) ;
2018-01-13 21:18:57 +00:00
/* report */
2018-01-27 01:48:33 +00:00
ZSTD_PTHREAD_MUTEX_LOCK ( & job - > job_mutex ) ;
2018-02-28 04:09:18 +00:00
job - > consumed = job - > src . size ;
2018-01-27 01:48:33 +00:00
ZSTD_pthread_cond_signal ( & job - > job_cond ) ;
ZSTD_pthread_mutex_unlock ( & job - > job_mutex ) ;
2017-01-12 00:25:46 +00:00
}
2017-01-17 23:31:16 +00:00
/* ------------------------------------------ */
2017-01-12 00:25:46 +00:00
/* ===== Multi-threaded compression ===== */
2017-01-17 23:31:16 +00:00
/* ------------------------------------------ */
2017-01-12 00:25:46 +00:00
2017-07-11 22:56:40 +00:00
typedef struct {
2018-02-28 04:09:18 +00:00
range_t prefix ; /* read-only non-owned prefix buffer */
2017-07-11 22:56:40 +00:00
buffer_t buffer ;
size_t filled ;
} inBuff_t ;
2018-02-28 04:09:18 +00:00
typedef struct {
BYTE * buffer ; /* The round input buffer. All jobs get references
* to pieces of the buffer . ZSTDMT_tryGetInputRange ( )
* handles handing out job input buffers , and makes
* sure it doesn ' t overlap with any pieces still in use .
*/
size_t capacity ; /* The capacity of buffer. */
size_t pos ; /* The position of the current inBuff in the round
* buffer . Updated past the end if the inBuff once
* the inBuff is sent to the worker thread .
* pos < = capacity .
*/
} roundBuff_t ;
static const roundBuff_t kNullRoundBuff = { NULL , 0 , 0 } ;
2016-12-31 05:04:25 +00:00
struct ZSTDMT_CCtx_s {
POOL_ctx * factory ;
2017-05-27 07:21:33 +00:00
ZSTDMT_jobDescription * jobs ;
2017-07-11 21:59:10 +00:00
ZSTDMT_bufferPool * bufPool ;
2016-12-31 05:04:25 +00:00
ZSTDMT_CCtxPool * cctxPool ;
2018-03-07 03:51:21 +00:00
ZSTDMT_seqPool * seqPool ;
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
ZSTD_CCtx_params params ;
2017-01-17 23:31:16 +00:00
size_t targetSectionSize ;
2018-01-17 19:39:07 +00:00
size_t targetPrefixSize ;
2018-02-28 04:09:18 +00:00
roundBuff_t roundBuff ;
2017-01-17 23:31:16 +00:00
inBuff_t inBuff ;
2018-01-19 18:01:40 +00:00
int jobReady ; /* 1 => one job is already prepared, but pool has shortage of workers. Don't create another one. */
2018-03-01 04:10:44 +00:00
serialState_t serial ;
2018-01-16 23:28:43 +00:00
unsigned singleBlockingThread ;
2017-01-17 23:31:16 +00:00
unsigned jobIDMask ;
unsigned doneJobID ;
unsigned nextJobID ;
unsigned frameEnded ;
2017-01-18 23:18:17 +00:00
unsigned allJobsCompleted ;
2017-01-19 23:32:07 +00:00
unsigned long long frameContentSize ;
2018-01-18 00:39:02 +00:00
unsigned long long consumed ;
unsigned long long produced ;
2017-05-30 23:12:06 +00:00
ZSTD_customMem cMem ;
2017-06-03 08:15:02 +00:00
ZSTD_CDict * cdictLocal ;
const ZSTD_CDict * cdict ;
2016-12-31 05:04:25 +00:00
} ;
2018-01-27 02:09:25 +00:00
static void ZSTDMT_freeJobsTable ( ZSTDMT_jobDescription * jobTable , U32 nbJobs , ZSTD_customMem cMem )
{
U32 jobNb ;
if ( jobTable = = NULL ) return ;
for ( jobNb = 0 ; jobNb < nbJobs ; jobNb + + ) {
ZSTD_pthread_mutex_destroy ( & jobTable [ jobNb ] . job_mutex ) ;
ZSTD_pthread_cond_destroy ( & jobTable [ jobNb ] . job_cond ) ;
}
ZSTD_free ( jobTable , cMem ) ;
}
2018-01-27 01:08:58 +00:00
/* ZSTDMT_allocJobsTable()
2018-01-27 01:48:33 +00:00
* allocate and init a job table .
2018-01-27 02:09:25 +00:00
* update * nbJobsPtr to next power of 2 value , as size of table */
2018-01-27 01:48:33 +00:00
static ZSTDMT_jobDescription * ZSTDMT_createJobsTable ( U32 * nbJobsPtr , ZSTD_customMem cMem )
2017-06-30 22:44:57 +00:00
{
U32 const nbJobsLog2 = ZSTD_highbit32 ( * nbJobsPtr ) + 1 ;
U32 const nbJobs = 1 < < nbJobsLog2 ;
2018-01-27 02:09:25 +00:00
U32 jobNb ;
2018-01-27 02:18:42 +00:00
ZSTDMT_jobDescription * const jobTable = ( ZSTDMT_jobDescription * )
ZSTD_calloc ( nbJobs * sizeof ( ZSTDMT_jobDescription ) , cMem ) ;
2018-01-27 02:09:25 +00:00
int initError = 0 ;
2018-01-27 01:48:33 +00:00
if ( jobTable = = NULL ) return NULL ;
* nbJobsPtr = nbJobs ;
for ( jobNb = 0 ; jobNb < nbJobs ; jobNb + + ) {
2018-01-27 02:09:25 +00:00
initError | = ZSTD_pthread_mutex_init ( & jobTable [ jobNb ] . job_mutex , NULL ) ;
initError | = ZSTD_pthread_cond_init ( & jobTable [ jobNb ] . job_cond , NULL ) ;
2018-01-27 01:48:33 +00:00
}
2018-01-27 02:09:25 +00:00
if ( initError ! = 0 ) {
ZSTDMT_freeJobsTable ( jobTable , nbJobs , cMem ) ;
return NULL ;
2018-01-27 01:48:33 +00:00
}
2018-01-27 02:09:25 +00:00
return jobTable ;
2017-06-30 22:44:57 +00:00
}
2018-02-02 03:29:30 +00:00
/* ZSTDMT_CCtxParam_setNbWorkers():
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
* Internal use only */
2018-02-02 03:29:30 +00:00
size_t ZSTDMT_CCtxParam_setNbWorkers ( ZSTD_CCtx_params * params , unsigned nbWorkers )
2017-08-25 20:14:51 +00:00
{
2018-02-02 03:29:30 +00:00
if ( nbWorkers > ZSTDMT_NBWORKERS_MAX ) nbWorkers = ZSTDMT_NBWORKERS_MAX ;
params - > nbWorkers = nbWorkers ;
2017-08-25 20:14:51 +00:00
params - > overlapSizeLog = ZSTDMT_OVERLAPLOG_DEFAULT ;
params - > jobSize = 0 ;
2018-02-02 03:29:30 +00:00
return nbWorkers ;
2017-08-25 20:14:51 +00:00
}
2018-02-02 03:29:30 +00:00
ZSTDMT_CCtx * ZSTDMT_createCCtx_advanced ( unsigned nbWorkers , ZSTD_customMem cMem )
2016-12-27 06:19:36 +00:00
{
2017-05-30 23:12:06 +00:00
ZSTDMT_CCtx * mtctx ;
2018-02-02 03:29:30 +00:00
U32 nbJobs = nbWorkers + 2 ;
2018-03-01 04:10:44 +00:00
int initError ;
2018-02-02 03:29:30 +00:00
DEBUGLOG ( 3 , " ZSTDMT_createCCtx_advanced (nbWorkers = %u) " , nbWorkers ) ;
2017-05-30 23:12:06 +00:00
2018-02-02 03:29:30 +00:00
if ( nbWorkers < 1 ) return NULL ;
nbWorkers = MIN ( nbWorkers , ZSTDMT_NBWORKERS_MAX ) ;
2017-05-30 23:12:06 +00:00
if ( ( cMem . customAlloc ! = NULL ) ^ ( cMem . customFree ! = NULL ) )
/* invalid custom allocator */
return NULL ;
mtctx = ( ZSTDMT_CCtx * ) ZSTD_calloc ( sizeof ( ZSTDMT_CCtx ) , cMem ) ;
if ( ! mtctx ) return NULL ;
2018-02-02 03:29:30 +00:00
ZSTDMT_CCtxParam_setNbWorkers ( & mtctx - > params , nbWorkers ) ;
2017-05-30 23:12:06 +00:00
mtctx - > cMem = cMem ;
mtctx - > allJobsCompleted = 1 ;
2018-02-02 03:29:30 +00:00
mtctx - > factory = POOL_create_advanced ( nbWorkers , 0 , cMem ) ;
2018-01-27 01:48:33 +00:00
mtctx - > jobs = ZSTDMT_createJobsTable ( & nbJobs , cMem ) ;
2018-01-27 01:08:58 +00:00
assert ( nbJobs > 0 ) ; assert ( ( nbJobs & ( nbJobs - 1 ) ) = = 0 ) ; /* ensure nbJobs is a power of 2 */
2017-06-30 22:44:57 +00:00
mtctx - > jobIDMask = nbJobs - 1 ;
2018-02-02 03:29:30 +00:00
mtctx - > bufPool = ZSTDMT_createBufferPool ( nbWorkers , cMem ) ;
mtctx - > cctxPool = ZSTDMT_createCCtxPool ( nbWorkers , cMem ) ;
2018-03-07 03:51:21 +00:00
mtctx - > seqPool = ZSTDMT_createSeqPool ( nbWorkers , cMem ) ;
2018-03-01 04:10:44 +00:00
initError = ZSTDMT_serialState_init ( & mtctx - > serial ) ;
2018-02-28 04:09:18 +00:00
mtctx - > roundBuff = kNullRoundBuff ;
2018-03-07 03:51:21 +00:00
if ( ! mtctx - > factory | ! mtctx - > jobs | ! mtctx - > bufPool | ! mtctx - > cctxPool | ! mtctx - > seqPool | initError ) {
2017-05-30 23:12:06 +00:00
ZSTDMT_freeCCtx ( mtctx ) ;
2017-01-11 14:58:05 +00:00
return NULL ;
}
2018-02-02 03:29:30 +00:00
DEBUGLOG ( 3 , " mt_cctx created, for %u threads " , nbWorkers ) ;
2017-05-30 23:12:06 +00:00
return mtctx ;
}
2018-02-02 03:29:30 +00:00
ZSTDMT_CCtx * ZSTDMT_createCCtx ( unsigned nbWorkers )
2017-05-30 23:12:06 +00:00
{
2018-02-02 03:29:30 +00:00
return ZSTDMT_createCCtx_advanced ( nbWorkers , ZSTD_defaultCMem ) ;
2016-12-27 06:19:36 +00:00
}
2018-01-18 00:39:02 +00:00
2017-01-18 01:46:33 +00:00
/* ZSTDMT_releaseAllJobResources() :
2017-06-30 21:51:01 +00:00
* note : ensure all workers are killed first ! */
2017-01-18 01:46:33 +00:00
static void ZSTDMT_releaseAllJobResources ( ZSTDMT_CCtx * mtctx )
{
unsigned jobID ;
2017-07-04 17:36:41 +00:00
DEBUGLOG ( 3 , " ZSTDMT_releaseAllJobResources " ) ;
2017-01-18 01:46:33 +00:00
for ( jobID = 0 ; jobID < = mtctx - > jobIDMask ; jobID + + ) {
2017-10-03 00:28:57 +00:00
DEBUGLOG ( 4 , " job%02u: release dst address %08X " , jobID , ( U32 ) ( size_t ) mtctx - > jobs [ jobID ] . dstBuff . start ) ;
2017-07-11 21:59:10 +00:00
ZSTDMT_releaseBuffer ( mtctx - > bufPool , mtctx - > jobs [ jobID ] . dstBuff ) ;
2017-01-18 01:46:33 +00:00
mtctx - > jobs [ jobID ] . dstBuff = g_nullBuffer ;
2018-01-26 22:35:54 +00:00
mtctx - > jobs [ jobID ] . cSize = 0 ;
2017-01-18 01:46:33 +00:00
}
2017-01-19 18:18:17 +00:00
memset ( mtctx - > jobs , 0 , ( mtctx - > jobIDMask + 1 ) * sizeof ( ZSTDMT_jobDescription ) ) ;
2017-01-18 19:57:34 +00:00
mtctx - > inBuff . buffer = g_nullBuffer ;
2018-02-28 04:09:18 +00:00
mtctx - > inBuff . filled = 0 ;
2017-01-19 18:18:17 +00:00
mtctx - > allJobsCompleted = 1 ;
2017-01-18 01:46:33 +00:00
}
2018-01-26 01:35:49 +00:00
static void ZSTDMT_waitForAllJobsCompleted ( ZSTDMT_CCtx * mtctx )
2017-09-28 09:33:41 +00:00
{
DEBUGLOG ( 4 , " ZSTDMT_waitForAllJobsCompleted " ) ;
2018-01-26 01:35:49 +00:00
while ( mtctx - > doneJobID < mtctx - > nextJobID ) {
unsigned const jobID = mtctx - > doneJobID & mtctx - > jobIDMask ;
2018-01-27 01:48:33 +00:00
ZSTD_PTHREAD_MUTEX_LOCK ( & mtctx - > jobs [ jobID ] . job_mutex ) ;
2018-02-28 04:09:18 +00:00
while ( mtctx - > jobs [ jobID ] . consumed < mtctx - > jobs [ jobID ] . src . size ) {
2018-01-30 22:43:36 +00:00
DEBUGLOG ( 5 , " waiting for jobCompleted signal from job %u " , mtctx - > doneJobID ) ; /* we want to block when waiting for data to flush */
2018-01-27 01:48:33 +00:00
ZSTD_pthread_cond_wait ( & mtctx - > jobs [ jobID ] . job_cond , & mtctx - > jobs [ jobID ] . job_mutex ) ;
2017-09-28 09:33:41 +00:00
}
2018-01-27 01:48:33 +00:00
ZSTD_pthread_mutex_unlock ( & mtctx - > jobs [ jobID ] . job_mutex ) ;
2018-01-26 01:35:49 +00:00
mtctx - > doneJobID + + ;
2017-09-28 09:33:41 +00:00
}
}
2017-01-11 14:58:05 +00:00
size_t ZSTDMT_freeCCtx ( ZSTDMT_CCtx * mtctx )
2016-12-27 06:19:36 +00:00
{
2017-01-18 01:46:33 +00:00
if ( mtctx = = NULL ) return 0 ; /* compatible with free on NULL */
2017-09-29 03:44:22 +00:00
POOL_free ( mtctx - > factory ) ; /* stop and free worker threads */
ZSTDMT_releaseAllJobResources ( mtctx ) ; /* release job resources into pools first */
2018-01-27 01:48:33 +00:00
ZSTDMT_freeJobsTable ( mtctx - > jobs , mtctx - > jobIDMask + 1 , mtctx - > cMem ) ;
2017-09-29 03:44:22 +00:00
ZSTDMT_freeBufferPool ( mtctx - > bufPool ) ;
2016-12-31 05:04:25 +00:00
ZSTDMT_freeCCtxPool ( mtctx - > cctxPool ) ;
2018-03-07 03:51:21 +00:00
ZSTDMT_freeSeqPool ( mtctx - > seqPool ) ;
2018-03-01 04:10:44 +00:00
ZSTDMT_serialState_free ( & mtctx - > serial ) ;
2017-06-03 08:15:02 +00:00
ZSTD_freeCDict ( mtctx - > cdictLocal ) ;
2018-02-28 04:09:18 +00:00
if ( mtctx - > roundBuff . buffer )
ZSTD_free ( mtctx - > roundBuff . buffer , mtctx - > cMem ) ;
2017-05-30 23:12:06 +00:00
ZSTD_free ( mtctx , mtctx - > cMem ) ;
2016-12-27 06:19:36 +00:00
return 0 ;
}
2017-06-02 00:56:14 +00:00
size_t ZSTDMT_sizeof_CCtx ( ZSTDMT_CCtx * mtctx )
{
if ( mtctx = = NULL ) return 0 ; /* supports sizeof NULL */
return sizeof ( * mtctx )
2017-07-11 21:59:10 +00:00
+ POOL_sizeof ( mtctx - > factory )
+ ZSTDMT_sizeof_bufferPool ( mtctx - > bufPool )
+ ( mtctx - > jobIDMask + 1 ) * sizeof ( ZSTDMT_jobDescription )
+ ZSTDMT_sizeof_CCtxPool ( mtctx - > cctxPool )
2018-03-07 03:51:21 +00:00
+ ZSTDMT_sizeof_seqPool ( mtctx - > seqPool )
2018-02-28 04:09:18 +00:00
+ ZSTD_sizeof_CDict ( mtctx - > cdictLocal )
+ mtctx - > roundBuff . capacity ;
2017-06-02 00:56:14 +00:00
}
2017-08-25 20:14:51 +00:00
/* Internal only */
2017-12-13 00:20:51 +00:00
size_t ZSTDMT_CCtxParam_setMTCtxParameter ( ZSTD_CCtx_params * params ,
ZSTDMT_parameter parameter , unsigned value ) {
DEBUGLOG ( 4 , " ZSTDMT_CCtxParam_setMTCtxParameter " ) ;
2017-01-25 01:02:26 +00:00
switch ( parameter )
{
2017-12-13 00:20:51 +00:00
case ZSTDMT_p_jobSize :
DEBUGLOG ( 4 , " ZSTDMT_CCtxParam_setMTCtxParameter : set jobSize to %u " , value ) ;
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
if ( ( value > 0 ) /* value==0 => automatic job size */
& ( value < ZSTDMT_JOBSIZE_MIN ) )
value = ZSTDMT_JOBSIZE_MIN ;
2017-08-25 20:14:51 +00:00
params - > jobSize = value ;
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
return value ;
2017-01-30 19:00:00 +00:00
case ZSTDMT_p_overlapSectionLog :
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
if ( value > 9 ) value = 9 ;
2017-09-01 01:25:56 +00:00
DEBUGLOG ( 4 , " ZSTDMT_p_overlapSectionLog : %u " , value ) ;
2017-08-25 20:14:51 +00:00
params - > overlapSizeLog = ( value > = 9 ) ? 9 : value ;
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
return value ;
2017-01-25 01:02:26 +00:00
default :
2017-07-14 00:12:16 +00:00
return ERROR ( parameter_unsupported ) ;
2017-01-25 01:02:26 +00:00
}
}
2017-08-25 23:13:40 +00:00
size_t ZSTDMT_setMTCtxParameter ( ZSTDMT_CCtx * mtctx , ZSTDMT_parameter parameter , unsigned value )
2017-08-25 20:14:51 +00:00
{
2017-12-13 00:20:51 +00:00
DEBUGLOG ( 4 , " ZSTDMT_setMTCtxParameter " ) ;
2017-08-25 20:14:51 +00:00
switch ( parameter )
{
2017-12-13 00:20:51 +00:00
case ZSTDMT_p_jobSize :
2017-08-25 20:14:51 +00:00
return ZSTDMT_CCtxParam_setMTCtxParameter ( & mtctx - > params , parameter , value ) ;
case ZSTDMT_p_overlapSectionLog :
return ZSTDMT_CCtxParam_setMTCtxParameter ( & mtctx - > params , parameter , value ) ;
default :
return ERROR ( parameter_unsupported ) ;
}
}
2017-01-25 01:02:26 +00:00
2018-02-02 03:29:30 +00:00
/* Sets parameters relevant to the compression job,
* initializing others to default values . */
2018-01-27 01:08:58 +00:00
static ZSTD_CCtx_params ZSTDMT_initJobCCtxParams ( ZSTD_CCtx_params const params )
{
ZSTD_CCtx_params jobParams ;
memset ( & jobParams , 0 , sizeof ( jobParams ) ) ;
jobParams . cParams = params . cParams ;
jobParams . fParams = params . fParams ;
jobParams . compressionLevel = params . compressionLevel ;
return jobParams ;
}
2018-02-02 23:58:13 +00:00
/*! ZSTDMT_updateCParams_whileCompressing() :
2018-03-19 21:41:23 +00:00
* Updates only a selected set of compression parameters , to remain compatible with current frame .
2018-02-02 22:24:56 +00:00
* New parameters will be applied to next compression job . */
2018-03-19 21:41:23 +00:00
void ZSTDMT_updateCParams_whileCompressing ( ZSTDMT_CCtx * mtctx , const ZSTD_CCtx_params * cctxParams )
2018-02-02 00:13:31 +00:00
{
2018-02-03 00:31:20 +00:00
U32 const saved_wlog = mtctx - > params . cParams . windowLog ; /* Do not modify windowLog while compressing */
2018-03-19 21:41:23 +00:00
int const compressionLevel = cctxParams - > compressionLevel ;
2018-02-03 00:31:20 +00:00
DEBUGLOG ( 5 , " ZSTDMT_updateCParams_whileCompressing (level:%i) " ,
compressionLevel ) ;
2018-02-02 22:24:56 +00:00
mtctx - > params . compressionLevel = compressionLevel ;
2018-03-19 21:41:23 +00:00
{ ZSTD_compressionParameters cParams = ZSTD_getCParamsFromCCtxParams ( cctxParams , 0 , 0 ) ;
cParams . windowLog = saved_wlog ;
mtctx - > params . cParams = cParams ;
}
2018-02-02 00:13:31 +00:00
}
2018-02-02 03:29:30 +00:00
/* ZSTDMT_getNbWorkers():
2018-01-18 00:39:02 +00:00
* @ return nb threads currently active in mtctx .
* mtctx must be valid */
2018-02-02 03:29:30 +00:00
unsigned ZSTDMT_getNbWorkers ( const ZSTDMT_CCtx * mtctx )
2018-01-18 00:39:02 +00:00
{
assert ( mtctx ! = NULL ) ;
2018-02-02 03:29:30 +00:00
return mtctx - > params . nbWorkers ;
2018-01-18 00:39:02 +00:00
}
/* ZSTDMT_getFrameProgression():
* tells how much data has been consumed ( input ) and produced ( output ) for current frame .
* able to count progression inside worker threads .
2018-01-18 19:15:23 +00:00
* Note : mutex will be acquired during statistics collection . */
2018-01-18 00:39:02 +00:00
ZSTD_frameProgression ZSTDMT_getFrameProgression ( ZSTDMT_CCtx * mtctx )
{
2018-01-26 21:00:14 +00:00
ZSTD_frameProgression fps ;
2018-01-19 18:01:40 +00:00
DEBUGLOG ( 6 , " ZSTDMT_getFrameProgression " ) ;
2018-01-26 21:00:14 +00:00
fps . consumed = mtctx - > consumed ;
fps . produced = mtctx - > produced ;
2018-02-28 04:09:18 +00:00
fps . ingested = mtctx - > consumed + mtctx - > inBuff . filled ;
2018-01-18 00:39:02 +00:00
{ unsigned jobNb ;
2018-01-19 18:01:40 +00:00
unsigned lastJobNb = mtctx - > nextJobID + mtctx - > jobReady ; assert ( mtctx - > jobReady < = 1 ) ;
DEBUGLOG ( 6 , " ZSTDMT_getFrameProgression: jobs: from %u to <%u (jobReady:%u) " ,
mtctx - > doneJobID , lastJobNb , mtctx - > jobReady )
for ( jobNb = mtctx - > doneJobID ; jobNb < lastJobNb ; jobNb + + ) {
2018-01-18 00:39:02 +00:00
unsigned const wJobID = jobNb & mtctx - > jobIDMask ;
2018-01-27 01:48:33 +00:00
ZSTD_pthread_mutex_lock ( & mtctx - > jobs [ wJobID ] . job_mutex ) ;
{ size_t const cResult = mtctx - > jobs [ wJobID ] . cSize ;
size_t const produced = ZSTD_isError ( cResult ) ? 0 : cResult ;
fps . consumed + = mtctx - > jobs [ wJobID ] . consumed ;
2018-02-28 04:09:18 +00:00
fps . ingested + = mtctx - > jobs [ wJobID ] . src . size ;
2018-01-27 01:48:33 +00:00
fps . produced + = produced ;
}
ZSTD_pthread_mutex_unlock ( & mtctx - > jobs [ wJobID ] . job_mutex ) ;
2018-01-18 00:39:02 +00:00
}
}
2018-01-26 21:00:14 +00:00
return fps ;
2018-01-18 00:39:02 +00:00
}
2017-01-25 01:02:26 +00:00
/* ------------------------------------------ */
/* ===== Multi-threaded compression ===== */
/* ------------------------------------------ */
2016-12-31 05:04:25 +00:00
2018-03-19 22:56:36 +00:00
static size_t ZSTDMT_computeTargetJobLog ( ZSTD_CCtx_params const params )
{
2018-03-07 03:51:21 +00:00
if ( params . ldmParams . enableLdm )
return MAX ( 21 , params . cParams . chainLog + 4 ) ;
2018-03-21 23:20:30 +00:00
return MAX ( 20 , params . cParams . windowLog + 2 ) ;
2018-03-19 22:56:36 +00:00
}
static size_t ZSTDMT_computeOverlapLog ( ZSTD_CCtx_params const params )
{
unsigned const overlapRLog = ( params . overlapSizeLog > 9 ) ? 0 : 9 - params . overlapSizeLog ;
2018-03-07 03:51:21 +00:00
if ( params . ldmParams . enableLdm )
return ( MIN ( params . cParams . windowLog , ZSTDMT_computeTargetJobLog ( params ) - 2 ) - overlapRLog ) ;
2018-03-19 22:56:36 +00:00
return overlapRLog > = 9 ? 0 : ( params . cParams . windowLog - overlapRLog ) ;
}
static unsigned ZSTDMT_computeNbJobs ( ZSTD_CCtx_params params , size_t srcSize , unsigned nbWorkers ) {
2018-02-02 03:29:30 +00:00
assert ( nbWorkers > 0 ) ;
2018-03-19 22:56:36 +00:00
{ size_t const jobSizeTarget = ( size_t ) 1 < < ZSTDMT_computeTargetJobLog ( params ) ;
2018-01-30 22:43:36 +00:00
size_t const jobMaxSize = jobSizeTarget < < 2 ;
2018-02-02 03:29:30 +00:00
size_t const passSizeMax = jobMaxSize * nbWorkers ;
2018-01-16 23:28:43 +00:00
unsigned const multiplier = ( unsigned ) ( srcSize / passSizeMax ) + 1 ;
2018-02-02 03:29:30 +00:00
unsigned const nbJobsLarge = multiplier * nbWorkers ;
2018-01-30 22:43:36 +00:00
unsigned const nbJobsMax = ( unsigned ) ( srcSize / jobSizeTarget ) + 1 ;
2018-02-02 03:29:30 +00:00
unsigned const nbJobsSmall = MIN ( nbJobsMax , nbWorkers ) ;
2018-01-30 22:43:36 +00:00
return ( multiplier > 1 ) ? nbJobsLarge : nbJobsSmall ;
2018-01-16 23:28:43 +00:00
} }
2017-07-03 23:23:36 +00:00
2018-01-16 23:28:43 +00:00
/* ZSTDMT_compress_advanced_internal() :
* This is a blocking function : it will only give back control to caller after finishing its compression job .
*/
2017-08-22 21:24:47 +00:00
static size_t ZSTDMT_compress_advanced_internal (
2017-08-18 23:17:24 +00:00
ZSTDMT_CCtx * mtctx ,
void * dst , size_t dstCapacity ,
const void * src , size_t srcSize ,
const ZSTD_CDict * cdict ,
2018-03-07 03:51:21 +00:00
ZSTD_CCtx_params params )
2016-12-27 06:19:36 +00:00
{
2018-01-16 23:28:43 +00:00
ZSTD_CCtx_params const jobParams = ZSTDMT_initJobCCtxParams ( params ) ;
2018-03-19 22:56:36 +00:00
size_t const overlapSize = ( size_t ) 1 < < ZSTDMT_computeOverlapLog ( params ) ;
unsigned const nbJobs = ZSTDMT_computeNbJobs ( params , srcSize , params . nbWorkers ) ;
2018-01-30 22:43:36 +00:00
size_t const proposedJobSize = ( srcSize + ( nbJobs - 1 ) ) / nbJobs ;
size_t const avgJobSize = ( ( ( proposedJobSize - 1 ) & 0x1FFFF ) < 0x7FFF ) ? proposedJobSize + 0xFFFF : proposedJobSize ; /* avoid too small last block */
2016-12-27 06:19:36 +00:00
const char * const srcStart = ( const char * ) src ;
2017-06-30 22:44:57 +00:00
size_t remainingSrcSize = srcSize ;
2018-01-30 22:43:36 +00:00
unsigned const compressWithinDst = ( dstCapacity > = ZSTD_compressBound ( srcSize ) ) ? nbJobs : ( unsigned ) ( dstCapacity / ZSTD_compressBound ( avgJobSize ) ) ; /* presumes avgJobSize >= 256 KB, which should be the case */
2017-04-01 01:27:03 +00:00
size_t frameStartPos = 0 , dstBufferPos = 0 ;
2018-02-02 03:29:30 +00:00
assert ( jobParams . nbWorkers = = 0 ) ;
assert ( mtctx - > cctxPool - > totalCCtx = = params . nbWorkers ) ;
2016-12-31 05:04:25 +00:00
2018-03-07 03:51:21 +00:00
params . jobSize = ( U32 ) avgJobSize ;
2018-03-13 21:23:14 +00:00
DEBUGLOG ( 4 , " ZSTDMT_compress_advanced_internal: nbJobs=%2u (rawSize=%u bytes; fixedSize=%u) " ,
2018-01-30 22:43:36 +00:00
nbJobs , ( U32 ) proposedJobSize , ( U32 ) avgJobSize ) ;
2018-01-16 23:28:43 +00:00
2018-02-02 03:29:30 +00:00
if ( ( nbJobs = = 1 ) | ( params . nbWorkers < = 1 ) ) { /* fallback to single-thread mode : this is a blocking invocation anyway */
2017-01-23 09:43:58 +00:00
ZSTD_CCtx * const cctx = mtctx - > cctxPool - > cctx [ 0 ] ;
2018-03-13 21:23:14 +00:00
DEBUGLOG ( 4 , " ZSTDMT_compress_advanced_internal: fallback to single-thread mode " ) ;
2017-08-25 18:36:17 +00:00
if ( cdict ) return ZSTD_compress_usingCDict_advanced ( cctx , dst , dstCapacity , src , srcSize , cdict , jobParams . fParams ) ;
return ZSTD_compress_advanced_internal ( cctx , dst , dstCapacity , src , srcSize , NULL , 0 , jobParams ) ;
2017-01-23 08:56:54 +00:00
}
2018-01-16 23:28:43 +00:00
2018-01-30 22:43:36 +00:00
assert ( avgJobSize > = 256 KB ) ; /* condition for ZSTD_compressBound(A) + ZSTD_compressBound(B) <= ZSTD_compressBound(A+B), required to compress directly into Dst (no additional buffer) */
ZSTDMT_setBufferSize ( mtctx - > bufPool , ZSTD_compressBound ( avgJobSize ) ) ;
2018-03-07 03:51:21 +00:00
if ( ZSTDMT_serialState_reset ( & mtctx - > serial , mtctx - > seqPool , params ) )
return ERROR ( memory_allocation ) ;
2017-01-23 08:56:54 +00:00
2018-01-30 22:43:36 +00:00
if ( nbJobs > mtctx - > jobIDMask + 1 ) { /* enlarge job table */
U32 jobsTableSize = nbJobs ;
2018-01-27 01:48:33 +00:00
ZSTDMT_freeJobsTable ( mtctx - > jobs , mtctx - > jobIDMask + 1 , mtctx - > cMem ) ;
2017-06-30 22:44:57 +00:00
mtctx - > jobIDMask = 0 ;
2018-01-30 22:43:36 +00:00
mtctx - > jobs = ZSTDMT_createJobsTable ( & jobsTableSize , mtctx - > cMem ) ;
2017-06-30 22:44:57 +00:00
if ( mtctx - > jobs = = NULL ) return ERROR ( memory_allocation ) ;
2018-01-30 22:43:36 +00:00
assert ( ( jobsTableSize ! = 0 ) & & ( ( jobsTableSize & ( jobsTableSize - 1 ) ) = = 0 ) ) ; /* ensure jobsTableSize is a power of 2 */
mtctx - > jobIDMask = jobsTableSize - 1 ;
2017-06-30 22:44:57 +00:00
}
2016-12-27 06:19:36 +00:00
{ unsigned u ;
2018-01-30 22:43:36 +00:00
for ( u = 0 ; u < nbJobs ; u + + ) {
size_t const jobSize = MIN ( remainingSrcSize , avgJobSize ) ;
size_t const dstBufferCapacity = ZSTD_compressBound ( jobSize ) ;
2017-04-01 01:27:03 +00:00
buffer_t const dstAsBuffer = { ( char * ) dst + dstBufferPos , dstBufferCapacity } ;
2017-07-11 21:59:10 +00:00
buffer_t const dstBuffer = u < compressWithinDst ? dstAsBuffer : g_nullBuffer ;
2017-03-30 22:51:58 +00:00
size_t dictSize = u ? overlapSize : 0 ;
2017-01-12 01:01:28 +00:00
2018-02-28 04:09:18 +00:00
mtctx - > jobs [ u ] . prefix . start = srcStart + frameStartPos - dictSize ;
mtctx - > jobs [ u ] . prefix . size = dictSize ;
mtctx - > jobs [ u ] . src . start = srcStart + frameStartPos ;
mtctx - > jobs [ u ] . src . size = jobSize ; assert ( jobSize > 0 ) ; /* avoid job.src.size == 0 */
2018-01-18 01:18:19 +00:00
mtctx - > jobs [ u ] . consumed = 0 ;
mtctx - > jobs [ u ] . cSize = 0 ;
2017-10-14 01:32:06 +00:00
mtctx - > jobs [ u ] . cdict = ( u = = 0 ) ? cdict : NULL ;
2017-01-11 17:21:25 +00:00
mtctx - > jobs [ u ] . fullFrameSize = srcSize ;
2017-08-25 18:36:17 +00:00
mtctx - > jobs [ u ] . params = jobParams ;
2017-06-30 21:51:01 +00:00
/* do not calculate checksum within sections, but write it in header for first section */
2016-12-31 05:04:25 +00:00
mtctx - > jobs [ u ] . dstBuff = dstBuffer ;
2017-07-10 23:30:55 +00:00
mtctx - > jobs [ u ] . cctxPool = mtctx - > cctxPool ;
2017-07-11 21:59:10 +00:00
mtctx - > jobs [ u ] . bufPool = mtctx - > bufPool ;
2018-03-07 03:51:21 +00:00
mtctx - > jobs [ u ] . seqPool = mtctx - > seqPool ;
2018-03-01 04:10:44 +00:00
mtctx - > jobs [ u ] . serial = & mtctx - > serial ;
mtctx - > jobs [ u ] . jobID = u ;
2018-01-30 22:43:36 +00:00
mtctx - > jobs [ u ] . firstJob = ( u = = 0 ) ;
mtctx - > jobs [ u ] . lastJob = ( u = = nbJobs - 1 ) ;
2016-12-31 05:04:25 +00:00
2018-01-30 22:43:36 +00:00
DEBUGLOG ( 5 , " ZSTDMT_compress_advanced_internal: posting job %u (%u bytes) " , u , ( U32 ) jobSize ) ;
2018-02-28 04:09:18 +00:00
DEBUG_PRINTHEX ( 6 , mtctx - > jobs [ u ] . prefix . start , 12 ) ;
2018-01-30 22:43:36 +00:00
POOL_add ( mtctx - > factory , ZSTDMT_compressionJob , & mtctx - > jobs [ u ] ) ;
2016-12-31 05:04:25 +00:00
2018-01-30 22:43:36 +00:00
frameStartPos + = jobSize ;
2017-04-01 01:27:03 +00:00
dstBufferPos + = dstBufferCapacity ;
2018-01-30 22:43:36 +00:00
remainingSrcSize - = jobSize ;
2016-12-27 06:19:36 +00:00
} }
2016-12-31 05:04:25 +00:00
2017-07-03 22:52:19 +00:00
/* collect result */
2017-07-12 00:18:26 +00:00
{ size_t error = 0 , dstPos = 0 ;
2018-01-30 22:43:36 +00:00
unsigned jobID ;
for ( jobID = 0 ; jobID < nbJobs ; jobID + + ) {
DEBUGLOG ( 5 , " waiting for job %u " , jobID ) ;
ZSTD_PTHREAD_MUTEX_LOCK ( & mtctx - > jobs [ jobID ] . job_mutex ) ;
2018-02-28 04:09:18 +00:00
while ( mtctx - > jobs [ jobID ] . consumed < mtctx - > jobs [ jobID ] . src . size ) {
2018-01-30 22:43:36 +00:00
DEBUGLOG ( 5 , " waiting for jobCompleted signal from job %u " , jobID ) ;
ZSTD_pthread_cond_wait ( & mtctx - > jobs [ jobID ] . job_cond , & mtctx - > jobs [ jobID ] . job_mutex ) ;
2016-12-31 05:04:25 +00:00
}
2018-01-30 22:43:36 +00:00
ZSTD_pthread_mutex_unlock ( & mtctx - > jobs [ jobID ] . job_mutex ) ;
DEBUGLOG ( 5 , " ready to write job %u " , jobID ) ;
2017-01-01 16:31:33 +00:00
2018-01-30 22:43:36 +00:00
{ size_t const cSize = mtctx - > jobs [ jobID ] . cSize ;
2017-01-12 02:06:35 +00:00
if ( ZSTD_isError ( cSize ) ) error = cSize ;
if ( ( ! error ) & & ( dstPos + cSize > dstCapacity ) ) error = ERROR ( dstSize_tooSmall ) ;
2018-01-30 22:43:36 +00:00
if ( jobID ) { /* note : job 0 is written directly at dst, which is correct position */
2017-04-01 01:27:03 +00:00
if ( ! error )
2018-01-30 22:43:36 +00:00
memmove ( ( char * ) dst + dstPos , mtctx - > jobs [ jobID ] . dstBuff . start , cSize ) ; /* may overlap when job compressed within dst */
if ( jobID > = compressWithinDst ) { /* job compressed into its own buffer, which must be released */
DEBUGLOG ( 5 , " releasing buffer %u>=%u " , jobID , compressWithinDst ) ;
ZSTDMT_releaseBuffer ( mtctx - > bufPool , mtctx - > jobs [ jobID ] . dstBuff ) ;
2017-09-29 06:01:31 +00:00
} }
2018-01-30 22:43:36 +00:00
mtctx - > jobs [ jobID ] . dstBuff = g_nullBuffer ;
mtctx - > jobs [ jobID ] . cSize = 0 ;
2016-12-31 05:04:25 +00:00
dstPos + = cSize ;
}
2018-01-30 22:43:36 +00:00
} /* for (jobID=0; jobID<nbJobs; jobID++) */
2017-07-12 00:18:26 +00:00
2017-08-25 20:14:51 +00:00
DEBUGLOG ( 4 , " checksumFlag : %u " , params . fParams . checksumFlag ) ;
if ( params . fParams . checksumFlag ) {
2018-03-01 04:10:44 +00:00
U32 const checksum = ( U32 ) XXH64_digest ( & mtctx - > serial . xxhState ) ;
2017-07-12 00:18:26 +00:00
if ( dstPos + 4 > dstCapacity ) {
error = ERROR ( dstSize_tooSmall ) ;
} else {
DEBUGLOG ( 4 , " writing checksum : %08X \n " , checksum ) ;
MEM_writeLE32 ( ( char * ) dst + dstPos , checksum ) ;
dstPos + = 4 ;
} }
2017-06-20 01:25:35 +00:00
if ( ! error ) DEBUGLOG ( 4 , " compressed size : %u " , ( U32 ) dstPos ) ;
2017-01-12 02:06:35 +00:00
return error ? error : dstPos ;
2016-12-31 05:04:25 +00:00
}
2017-08-18 23:17:24 +00:00
}
size_t ZSTDMT_compress_advanced ( ZSTDMT_CCtx * mtctx ,
void * dst , size_t dstCapacity ,
const void * src , size_t srcSize ,
const ZSTD_CDict * cdict ,
2018-02-02 00:13:31 +00:00
ZSTD_parameters params ,
2017-08-18 23:17:24 +00:00
unsigned overlapLog )
{
ZSTD_CCtx_params cctxParams = mtctx - > params ;
cctxParams . cParams = params . cParams ;
cctxParams . fParams = params . fParams ;
2017-08-25 20:14:51 +00:00
cctxParams . overlapSizeLog = overlapLog ;
2017-08-22 21:24:47 +00:00
return ZSTDMT_compress_advanced_internal ( mtctx ,
dst , dstCapacity ,
src , srcSize ,
2017-08-25 20:14:51 +00:00
cdict , cctxParams ) ;
2017-06-30 21:51:01 +00:00
}
2016-12-27 06:19:36 +00:00
2017-06-30 21:51:01 +00:00
size_t ZSTDMT_compressCCtx ( ZSTDMT_CCtx * mtctx ,
void * dst , size_t dstCapacity ,
const void * src , size_t srcSize ,
int compressionLevel )
{
2017-07-13 09:22:58 +00:00
U32 const overlapLog = ( compressionLevel > = ZSTD_maxCLevel ( ) ) ? 9 : ZSTDMT_OVERLAPLOG_DEFAULT ;
2017-06-30 21:51:01 +00:00
ZSTD_parameters params = ZSTD_getParams ( compressionLevel , srcSize , 0 ) ;
params . fParams . contentSizeFlag = 1 ;
2017-07-13 09:22:58 +00:00
return ZSTDMT_compress_advanced ( mtctx , dst , dstCapacity , src , srcSize , NULL , params , overlapLog ) ;
2016-12-27 06:19:36 +00:00
}
2017-01-12 00:25:46 +00:00
/* ====================================== */
/* ======= Streaming API ======= */
/* ====================================== */
2017-08-22 21:24:47 +00:00
size_t ZSTDMT_initCStream_internal (
2018-01-26 01:35:49 +00:00
ZSTDMT_CCtx * mtctx ,
2018-03-20 22:13:14 +00:00
const void * dict , size_t dictSize , ZSTD_dictContentType_e dictContentType ,
2017-08-25 20:14:51 +00:00
const ZSTD_CDict * cdict , ZSTD_CCtx_params params ,
2017-08-18 23:17:24 +00:00
unsigned long long pledgedSrcSize )
2017-01-23 00:40:06 +00:00
{
2018-06-07 22:24:12 +00:00
DEBUGLOG ( 4 , " ZSTDMT_initCStream_internal (pledgedSrcSize=%u, nbWorkers=%u, cctxPool=%u) " ,
( U32 ) pledgedSrcSize , params . nbWorkers , mtctx - > cctxPool - > totalCCtx ) ;
2017-06-03 08:15:02 +00:00
/* params are supposed to be fully validated at this point */
2017-08-25 20:14:51 +00:00
assert ( ! ZSTD_isError ( ZSTD_checkCParams ( params . cParams ) ) ) ;
2017-06-03 08:15:02 +00:00
assert ( ! ( ( dict ) & & ( cdict ) ) ) ; /* either dict or cdict, not both */
2018-02-02 03:29:30 +00:00
assert ( mtctx - > cctxPool - > totalCCtx = = params . nbWorkers ) ;
2018-02-03 00:31:20 +00:00
/* init */
2018-01-17 20:39:58 +00:00
if ( params . jobSize = = 0 ) {
2018-03-07 03:51:21 +00:00
params . jobSize = 1U < < ZSTDMT_computeTargetJobLog ( params ) ;
2018-01-17 20:39:58 +00:00
}
if ( params . jobSize > ZSTDMT_JOBSIZE_MAX ) params . jobSize = ZSTDMT_JOBSIZE_MAX ;
2017-06-03 08:15:02 +00:00
2018-02-03 00:31:20 +00:00
mtctx - > singleBlockingThread = ( pledgedSrcSize < = ZSTDMT_JOBSIZE_MIN ) ; /* do not trigger multi-threading when srcSize is too small */
2018-01-26 01:35:49 +00:00
if ( mtctx - > singleBlockingThread ) {
2018-01-16 23:28:43 +00:00
ZSTD_CCtx_params const singleThreadParams = ZSTDMT_initJobCCtxParams ( params ) ;
2018-03-13 21:23:14 +00:00
DEBUGLOG ( 5 , " ZSTDMT_initCStream_internal: switch to single blocking thread mode " ) ;
2018-02-02 03:29:30 +00:00
assert ( singleThreadParams . nbWorkers = = 0 ) ;
2018-01-26 01:35:49 +00:00
return ZSTD_initCStream_internal ( mtctx - > cctxPool - > cctx [ 0 ] ,
2017-08-22 01:10:44 +00:00
dict , dictSize , cdict ,
2017-08-25 20:14:51 +00:00
singleThreadParams , pledgedSrcSize ) ;
2017-06-03 08:15:02 +00:00
}
2018-02-03 00:31:20 +00:00
2018-03-13 21:23:14 +00:00
DEBUGLOG ( 4 , " ZSTDMT_initCStream_internal: %u workers " , params . nbWorkers ) ;
2017-06-03 01:20:48 +00:00
2018-01-26 01:35:49 +00:00
if ( mtctx - > allJobsCompleted = = 0 ) { /* previous compression not correctly finished */
ZSTDMT_waitForAllJobsCompleted ( mtctx ) ;
ZSTDMT_releaseAllJobResources ( mtctx ) ;
mtctx - > allJobsCompleted = 1 ;
2017-01-18 23:18:17 +00:00
}
2017-06-03 08:15:02 +00:00
2018-01-26 01:35:49 +00:00
mtctx - > params = params ;
mtctx - > frameContentSize = pledgedSrcSize ;
2017-06-03 08:15:02 +00:00
if ( dict ) {
2018-01-26 01:35:49 +00:00
ZSTD_freeCDict ( mtctx - > cdictLocal ) ;
mtctx - > cdictLocal = ZSTD_createCDict_advanced ( dict , dictSize ,
2018-03-20 22:13:14 +00:00
ZSTD_dlm_byCopy , dictContentType , /* note : a loadPrefix becomes an internal CDict */
2018-01-26 01:35:49 +00:00
params . cParams , mtctx - > cMem ) ;
mtctx - > cdict = mtctx - > cdictLocal ;
if ( mtctx - > cdictLocal = = NULL ) return ERROR ( memory_allocation ) ;
2017-06-03 08:15:02 +00:00
} else {
2018-01-26 01:35:49 +00:00
ZSTD_freeCDict ( mtctx - > cdictLocal ) ;
mtctx - > cdictLocal = NULL ;
mtctx - > cdict = cdict ;
2017-06-03 08:15:02 +00:00
}
2018-03-19 22:56:36 +00:00
mtctx - > targetPrefixSize = ( size_t ) 1 < < ZSTDMT_computeOverlapLog ( params ) ;
2018-01-26 01:35:49 +00:00
DEBUGLOG ( 4 , " overlapLog=%u => %u KB " , params . overlapSizeLog , ( U32 ) ( mtctx - > targetPrefixSize > > 10 ) ) ;
mtctx - > targetSectionSize = params . jobSize ;
if ( mtctx - > targetSectionSize < ZSTDMT_JOBSIZE_MIN ) mtctx - > targetSectionSize = ZSTDMT_JOBSIZE_MIN ;
if ( mtctx - > targetSectionSize < mtctx - > targetPrefixSize ) mtctx - > targetSectionSize = mtctx - > targetPrefixSize ; /* job size must be >= overlap size */
DEBUGLOG ( 4 , " Job Size : %u KB (note : set to %u) " , ( U32 ) ( mtctx - > targetSectionSize > > 10 ) , params . jobSize ) ;
2018-02-28 04:09:18 +00:00
DEBUGLOG ( 4 , " inBuff Size : %u KB " , ( U32 ) ( mtctx - > targetSectionSize > > 10 ) ) ;
ZSTDMT_setBufferSize ( mtctx - > bufPool , ZSTD_compressBound ( mtctx - > targetSectionSize ) ) ;
{
2018-03-07 03:51:21 +00:00
/* If ldm is enabled we need windowSize space. */
size_t const windowSize = mtctx - > params . ldmParams . enableLdm ? ( 1U < < mtctx - > params . cParams . windowLog ) : 0 ;
2018-03-20 01:56:39 +00:00
/* Two buffers of slack, plus extra space for the overlap
* This is the minimum slack that LDM works with . One extra because
* flush might waste up to targetSectionSize - 1 bytes . Another extra
* for the overlap ( if > 0 ) , then one to fill which doesn ' t overlap
* with the LDM window .
*/
size_t const nbSlackBuffers = 2 + ( mtctx - > targetPrefixSize > 0 ) ;
2018-03-07 03:51:21 +00:00
size_t const slackSize = mtctx - > targetSectionSize * nbSlackBuffers ;
/* Compute the total size, and always have enough slack */
2018-02-28 04:09:18 +00:00
size_t const nbWorkers = MAX ( mtctx - > params . nbWorkers , 1 ) ;
2018-03-07 03:51:21 +00:00
size_t const sectionsSize = mtctx - > targetSectionSize * nbWorkers ;
size_t const capacity = MAX ( windowSize , sectionsSize ) + slackSize ;
2018-02-28 04:09:18 +00:00
if ( mtctx - > roundBuff . capacity < capacity ) {
if ( mtctx - > roundBuff . buffer )
ZSTD_free ( mtctx - > roundBuff . buffer , mtctx - > cMem ) ;
mtctx - > roundBuff . buffer = ( BYTE * ) ZSTD_malloc ( capacity , mtctx - > cMem ) ;
if ( mtctx - > roundBuff . buffer = = NULL ) {
mtctx - > roundBuff . capacity = 0 ;
return ERROR ( memory_allocation ) ;
}
mtctx - > roundBuff . capacity = capacity ;
}
}
DEBUGLOG ( 4 , " roundBuff capacity : %u KB " , ( U32 ) ( mtctx - > roundBuff . capacity > > 10 ) ) ;
mtctx - > roundBuff . pos = 0 ;
2018-01-26 01:35:49 +00:00
mtctx - > inBuff . buffer = g_nullBuffer ;
2018-02-28 04:09:18 +00:00
mtctx - > inBuff . filled = 0 ;
mtctx - > inBuff . prefix = kNullRange ;
2018-01-26 01:35:49 +00:00
mtctx - > doneJobID = 0 ;
mtctx - > nextJobID = 0 ;
mtctx - > frameEnded = 0 ;
mtctx - > allJobsCompleted = 0 ;
mtctx - > consumed = 0 ;
mtctx - > produced = 0 ;
2018-03-07 03:51:21 +00:00
if ( ZSTDMT_serialState_reset ( & mtctx - > serial , mtctx - > seqPool , params ) )
return ERROR ( memory_allocation ) ;
2017-01-12 00:25:46 +00:00
return 0 ;
2017-08-18 23:17:24 +00:00
}
2017-06-03 08:15:02 +00:00
size_t ZSTDMT_initCStream_advanced ( ZSTDMT_CCtx * mtctx ,
2017-07-11 21:59:10 +00:00
const void * dict , size_t dictSize ,
ZSTD_parameters params ,
unsigned long long pledgedSrcSize )
2017-01-23 00:40:06 +00:00
{
2018-01-17 19:39:07 +00:00
ZSTD_CCtx_params cctxParams = mtctx - > params ; /* retrieve sticky params */
DEBUGLOG ( 4 , " ZSTDMT_initCStream_advanced (pledgedSrcSize=%u) " , ( U32 ) pledgedSrcSize ) ;
2017-08-22 21:24:47 +00:00
cctxParams . cParams = params . cParams ;
cctxParams . fParams = params . fParams ;
2018-03-20 22:13:14 +00:00
return ZSTDMT_initCStream_internal ( mtctx , dict , dictSize , ZSTD_dct_auto , NULL ,
2017-08-22 21:24:47 +00:00
cctxParams , pledgedSrcSize ) ;
2017-06-03 08:15:02 +00:00
}
size_t ZSTDMT_initCStream_usingCDict ( ZSTDMT_CCtx * mtctx ,
const ZSTD_CDict * cdict ,
ZSTD_frameParameters fParams ,
unsigned long long pledgedSrcSize )
{
2017-08-25 20:14:51 +00:00
ZSTD_CCtx_params cctxParams = mtctx - > params ;
2018-01-17 19:39:07 +00:00
if ( cdict = = NULL ) return ERROR ( dictionary_wrong ) ; /* method incompatible with NULL cdict */
2017-08-26 00:58:28 +00:00
cctxParams . cParams = ZSTD_getCParamsFromCDict ( cdict ) ;
2017-08-25 20:14:51 +00:00
cctxParams . fParams = fParams ;
2018-03-20 22:13:14 +00:00
return ZSTDMT_initCStream_internal ( mtctx , NULL , 0 /*dictSize*/ , ZSTD_dct_auto , cdict ,
2017-08-25 20:14:51 +00:00
cctxParams , pledgedSrcSize ) ;
2017-01-23 00:40:06 +00:00
}
2017-06-03 08:15:02 +00:00
2017-01-20 00:59:56 +00:00
/* ZSTDMT_resetCStream() :
2017-10-17 21:07:43 +00:00
* pledgedSrcSize can be zero = = unknown ( for the time being )
* prefer using ZSTD_CONTENTSIZE_UNKNOWN ,
* as ` 0 ` might mean " empty " in the future */
2018-01-26 01:35:49 +00:00
size_t ZSTDMT_resetCStream ( ZSTDMT_CCtx * mtctx , unsigned long long pledgedSrcSize )
2017-01-20 00:59:56 +00:00
{
2017-10-17 21:07:43 +00:00
if ( ! pledgedSrcSize ) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN ;
2018-03-20 22:13:14 +00:00
return ZSTDMT_initCStream_internal ( mtctx , NULL , 0 , ZSTD_dct_auto , 0 , mtctx - > params ,
2017-08-22 21:24:47 +00:00
pledgedSrcSize ) ;
2017-01-20 00:59:56 +00:00
}
2018-01-26 01:35:49 +00:00
size_t ZSTDMT_initCStream ( ZSTDMT_CCtx * mtctx , int compressionLevel ) {
2018-01-16 23:28:43 +00:00
ZSTD_parameters const params = ZSTD_getParams ( compressionLevel , ZSTD_CONTENTSIZE_UNKNOWN , 0 ) ;
2018-01-26 01:35:49 +00:00
ZSTD_CCtx_params cctxParams = mtctx - > params ; /* retrieve sticky params */
2018-01-17 19:39:07 +00:00
DEBUGLOG ( 4 , " ZSTDMT_initCStream (cLevel=%i) " , compressionLevel ) ;
2017-08-22 21:24:47 +00:00
cctxParams . cParams = params . cParams ;
cctxParams . fParams = params . fParams ;
2018-03-20 22:13:14 +00:00
return ZSTDMT_initCStream_internal ( mtctx , NULL , 0 , ZSTD_dct_auto , NULL , cctxParams , ZSTD_CONTENTSIZE_UNKNOWN ) ;
2017-01-19 23:32:07 +00:00
}
2017-01-12 00:25:46 +00:00
2018-01-26 01:35:49 +00:00
/* ZSTDMT_writeLastEmptyBlock()
2018-01-26 19:06:34 +00:00
* Write a single empty block with an end - of - frame to finish a frame .
* Job must be created from streaming variant .
* This function is always successfull if expected conditions are fulfilled .
2018-01-26 01:35:49 +00:00
*/
2018-01-26 19:06:34 +00:00
static void ZSTDMT_writeLastEmptyBlock ( ZSTDMT_jobDescription * job )
2018-01-26 01:35:49 +00:00
{
2018-01-30 22:43:36 +00:00
assert ( job - > lastJob = = 1 ) ;
2018-02-28 04:09:18 +00:00
assert ( job - > src . size = = 0 ) ; /* last job is empty -> will be simplified into a last empty block */
2018-01-30 22:43:36 +00:00
assert ( job - > firstJob = = 0 ) ; /* cannot be first job, as it also needs to create frame header */
2018-01-26 19:06:34 +00:00
assert ( job - > dstBuff . start = = NULL ) ; /* invoked from streaming variant only (otherwise, dstBuff might be user's output) */
2018-02-28 04:09:18 +00:00
job - > dstBuff = ZSTDMT_getBuffer ( job - > bufPool ) ;
if ( job - > dstBuff . start = = NULL ) {
job - > cSize = ERROR ( memory_allocation ) ;
return ;
}
assert ( job - > dstBuff . capacity > = ZSTD_blockHeaderSize ) ; /* no buffer should ever be that small */
job - > src = kNullRange ;
2018-01-26 21:00:14 +00:00
job - > cSize = ZSTD_writeLastEmptyBlock ( job - > dstBuff . start , job - > dstBuff . capacity ) ;
2018-01-26 18:44:09 +00:00
assert ( ! ZSTD_isError ( job - > cSize ) ) ;
assert ( job - > consumed = = 0 ) ;
2018-01-26 01:35:49 +00:00
}
static size_t ZSTDMT_createCompressionJob ( ZSTDMT_CCtx * mtctx , size_t srcSize , ZSTD_EndDirective endOp )
2017-01-25 01:41:49 +00:00
{
2018-01-26 01:35:49 +00:00
unsigned const jobID = mtctx - > nextJobID & mtctx - > jobIDMask ;
2018-01-23 23:52:40 +00:00
int const endFrame = ( endOp = = ZSTD_e_end ) ;
2017-01-25 01:41:49 +00:00
2018-01-26 01:35:49 +00:00
if ( mtctx - > nextJobID > mtctx - > doneJobID + mtctx - > jobIDMask ) {
2018-01-20 01:35:08 +00:00
DEBUGLOG ( 5 , " ZSTDMT_createCompressionJob: will not create new job : table is full " ) ;
2018-01-26 01:35:49 +00:00
assert ( ( mtctx - > nextJobID & mtctx - > jobIDMask ) = = ( mtctx - > doneJobID & mtctx - > jobIDMask ) ) ;
2018-01-20 01:35:08 +00:00
return 0 ;
}
2018-01-19 21:19:59 +00:00
2018-01-26 01:35:49 +00:00
if ( ! mtctx - > jobReady ) {
2018-02-28 04:09:18 +00:00
BYTE const * src = ( BYTE const * ) mtctx - > inBuff . buffer . start ;
2018-01-19 18:01:40 +00:00
DEBUGLOG ( 5 , " ZSTDMT_createCompressionJob: preparing job %u to compress %u bytes with %u preload " ,
2018-02-28 04:09:18 +00:00
mtctx - > nextJobID , ( U32 ) srcSize , ( U32 ) mtctx - > inBuff . prefix . size ) ;
mtctx - > jobs [ jobID ] . src . start = src ;
mtctx - > jobs [ jobID ] . src . size = srcSize ;
assert ( mtctx - > inBuff . filled > = srcSize ) ;
mtctx - > jobs [ jobID ] . prefix = mtctx - > inBuff . prefix ;
2018-01-26 01:35:49 +00:00
mtctx - > jobs [ jobID ] . consumed = 0 ;
mtctx - > jobs [ jobID ] . cSize = 0 ;
mtctx - > jobs [ jobID ] . params = mtctx - > params ;
mtctx - > jobs [ jobID ] . cdict = mtctx - > nextJobID = = 0 ? mtctx - > cdict : NULL ;
mtctx - > jobs [ jobID ] . fullFrameSize = mtctx - > frameContentSize ;
mtctx - > jobs [ jobID ] . dstBuff = g_nullBuffer ;
mtctx - > jobs [ jobID ] . cctxPool = mtctx - > cctxPool ;
mtctx - > jobs [ jobID ] . bufPool = mtctx - > bufPool ;
2018-03-07 03:51:21 +00:00
mtctx - > jobs [ jobID ] . seqPool = mtctx - > seqPool ;
2018-03-01 04:10:44 +00:00
mtctx - > jobs [ jobID ] . serial = & mtctx - > serial ;
mtctx - > jobs [ jobID ] . jobID = mtctx - > nextJobID ;
2018-01-30 22:43:36 +00:00
mtctx - > jobs [ jobID ] . firstJob = ( mtctx - > nextJobID = = 0 ) ;
mtctx - > jobs [ jobID ] . lastJob = endFrame ;
2018-01-26 01:35:49 +00:00
mtctx - > jobs [ jobID ] . frameChecksumNeeded = endFrame & & ( mtctx - > nextJobID > 0 ) & & mtctx - > params . fParams . checksumFlag ;
mtctx - > jobs [ jobID ] . dstFlushed = 0 ;
2018-02-28 04:09:18 +00:00
/* Update the round buffer pos and clear the input buffer to be reset */
mtctx - > roundBuff . pos + = srcSize ;
mtctx - > inBuff . buffer = g_nullBuffer ;
mtctx - > inBuff . filled = 0 ;
/* Set the prefix */
2018-01-19 18:01:40 +00:00
if ( ! endFrame ) {
2018-02-28 04:09:18 +00:00
size_t const newPrefixSize = MIN ( srcSize , mtctx - > targetPrefixSize ) ;
mtctx - > inBuff . prefix . start = src + srcSize - newPrefixSize ;
mtctx - > inBuff . prefix . size = newPrefixSize ;
2018-01-19 18:01:40 +00:00
} else { /* endFrame==1 => no need for another input buffer */
2018-02-28 04:09:18 +00:00
mtctx - > inBuff . prefix = kNullRange ;
2018-01-26 01:35:49 +00:00
mtctx - > frameEnded = endFrame ;
if ( mtctx - > nextJobID = = 0 ) {
2018-01-30 22:43:36 +00:00
/* single job exception : checksum is already calculated directly within worker thread */
2018-01-26 01:35:49 +00:00
mtctx - > params . fParams . checksumFlag = 0 ;
} }
2017-01-25 01:41:49 +00:00
2018-01-26 01:35:49 +00:00
if ( ( srcSize = = 0 )
2018-01-30 22:43:36 +00:00
& & ( mtctx - > nextJobID > 0 ) /*single job must also write frame header*/ ) {
2018-01-26 20:15:43 +00:00
DEBUGLOG ( 5 , " ZSTDMT_createCompressionJob: creating a last empty block to end frame " ) ;
2018-01-26 01:35:49 +00:00
assert ( endOp = = ZSTD_e_end ) ; /* only possible case : need to end the frame with an empty last block */
2018-01-26 19:06:34 +00:00
ZSTDMT_writeLastEmptyBlock ( mtctx - > jobs + jobID ) ;
2018-01-26 01:35:49 +00:00
mtctx - > nextJobID + + ;
return 0 ;
}
}
2018-01-26 20:15:43 +00:00
DEBUGLOG ( 5 , " ZSTDMT_createCompressionJob: posting job %u : %u bytes (end:%u, jobNb == %u (mod:%u)) " ,
2018-01-26 01:35:49 +00:00
mtctx - > nextJobID ,
2018-02-28 04:09:18 +00:00
( U32 ) mtctx - > jobs [ jobID ] . src . size ,
2018-01-30 22:43:36 +00:00
mtctx - > jobs [ jobID ] . lastJob ,
2018-01-26 20:15:43 +00:00
mtctx - > nextJobID ,
jobID ) ;
2018-01-30 22:43:36 +00:00
if ( POOL_tryAdd ( mtctx - > factory , ZSTDMT_compressionJob , & mtctx - > jobs [ jobID ] ) ) {
2018-01-26 01:35:49 +00:00
mtctx - > nextJobID + + ;
mtctx - > jobReady = 0 ;
2018-01-19 18:01:40 +00:00
} else {
2018-01-26 01:35:49 +00:00
DEBUGLOG ( 5 , " ZSTDMT_createCompressionJob: no worker available for job %u " , mtctx - > nextJobID ) ;
mtctx - > jobReady = 1 ;
2018-01-19 18:01:40 +00:00
}
2017-01-25 01:41:49 +00:00
return 0 ;
}
2018-01-20 01:35:08 +00:00
/*! ZSTDMT_flushProduced() :
* ` output ` : ` pos ` will be updated with amount of data flushed .
2018-01-19 00:20:26 +00:00
* ` blockToFlush ` : if > 0 , the function will block and wait if there is no data available to flush .
* @ return : amount of data remaining within internal buffer , 0 if no more , 1 if unknown but > 0 , or an error code */
2018-01-26 01:35:49 +00:00
static size_t ZSTDMT_flushProduced ( ZSTDMT_CCtx * mtctx , ZSTD_outBuffer * output , unsigned blockToFlush , ZSTD_EndDirective end )
2017-01-23 19:43:51 +00:00
{
2018-01-26 01:35:49 +00:00
unsigned const wJobID = mtctx - > doneJobID & mtctx - > jobIDMask ;
2018-01-26 20:15:43 +00:00
DEBUGLOG ( 5 , " ZSTDMT_flushProduced (blocking:%u , job %u <= %u) " ,
blockToFlush , mtctx - > doneJobID , mtctx - > nextJobID ) ;
2018-01-20 01:35:08 +00:00
assert ( output - > size > = output - > pos ) ;
2018-01-27 01:48:33 +00:00
ZSTD_PTHREAD_MUTEX_LOCK ( & mtctx - > jobs [ wJobID ] . job_mutex ) ;
2018-01-26 20:15:43 +00:00
if ( blockToFlush
& & ( mtctx - > doneJobID < mtctx - > nextJobID ) ) {
2018-01-26 01:35:49 +00:00
assert ( mtctx - > jobs [ wJobID ] . dstFlushed < = mtctx - > jobs [ wJobID ] . cSize ) ;
2018-01-26 20:15:43 +00:00
while ( mtctx - > jobs [ wJobID ] . dstFlushed = = mtctx - > jobs [ wJobID ] . cSize ) { /* nothing to flush */
2018-02-28 04:09:18 +00:00
if ( mtctx - > jobs [ wJobID ] . consumed = = mtctx - > jobs [ wJobID ] . src . size ) {
2018-01-26 20:15:43 +00:00
DEBUGLOG ( 5 , " job %u is completely consumed (%u == %u) => don't wait for cond, there will be none " ,
2018-02-28 04:09:18 +00:00
mtctx - > doneJobID , ( U32 ) mtctx - > jobs [ wJobID ] . consumed , ( U32 ) mtctx - > jobs [ wJobID ] . src . size ) ;
2018-01-26 01:35:49 +00:00
break ;
}
2018-01-20 01:35:08 +00:00
DEBUGLOG ( 5 , " waiting for something to flush from job %u (currently flushed: %u bytes) " ,
2018-01-26 01:35:49 +00:00
mtctx - > doneJobID , ( U32 ) mtctx - > jobs [ wJobID ] . dstFlushed ) ;
2018-01-27 01:48:33 +00:00
ZSTD_pthread_cond_wait ( & mtctx - > jobs [ wJobID ] . job_cond , & mtctx - > jobs [ wJobID ] . job_mutex ) ; /* block when nothing to flush but some to come */
2018-01-20 01:35:08 +00:00
} }
2018-01-18 19:03:27 +00:00
2018-01-26 20:15:43 +00:00
/* try to flush something */
2018-01-27 01:08:58 +00:00
{ size_t cSize = mtctx - > jobs [ wJobID ] . cSize ; /* shared */
size_t const srcConsumed = mtctx - > jobs [ wJobID ] . consumed ; /* shared */
2018-02-28 04:09:18 +00:00
size_t const srcSize = mtctx - > jobs [ wJobID ] . src . size ; /* read-only, could be done after mutex lock, but no-declaration-after-statement */
2018-01-27 01:48:33 +00:00
ZSTD_pthread_mutex_unlock ( & mtctx - > jobs [ wJobID ] . job_mutex ) ;
2018-01-26 20:15:43 +00:00
if ( ZSTD_isError ( cSize ) ) {
2018-01-20 01:35:08 +00:00
DEBUGLOG ( 5 , " ZSTDMT_flushProduced: job %u : compression error detected : %s " ,
2018-01-26 20:15:43 +00:00
mtctx - > doneJobID , ZSTD_getErrorName ( cSize ) ) ;
2018-01-26 01:35:49 +00:00
ZSTDMT_waitForAllJobsCompleted ( mtctx ) ;
ZSTDMT_releaseAllJobResources ( mtctx ) ;
2018-01-26 20:15:43 +00:00
return cSize ;
2018-01-18 19:03:27 +00:00
}
2018-01-20 01:35:08 +00:00
/* add frame checksum if necessary (can only happen once) */
2018-01-27 01:08:58 +00:00
assert ( srcConsumed < = srcSize ) ;
if ( ( srcConsumed = = srcSize ) /* job completed -> worker no longer active */
2018-01-26 20:15:43 +00:00
& & mtctx - > jobs [ wJobID ] . frameChecksumNeeded ) {
2018-03-01 04:10:44 +00:00
U32 const checksum = ( U32 ) XXH64_digest ( & mtctx - > serial . xxhState ) ;
2018-01-23 23:19:11 +00:00
DEBUGLOG ( 4 , " ZSTDMT_flushProduced: writing checksum : %08X \n " , checksum ) ;
2018-01-26 20:15:43 +00:00
MEM_writeLE32 ( ( char * ) mtctx - > jobs [ wJobID ] . dstBuff . start + mtctx - > jobs [ wJobID ] . cSize , checksum ) ;
cSize + = 4 ;
mtctx - > jobs [ wJobID ] . cSize + = 4 ; /* can write this shared value, as worker is no longer active */
2018-01-26 01:35:49 +00:00
mtctx - > jobs [ wJobID ] . frameChecksumNeeded = 0 ;
2017-01-24 19:48:40 +00:00
}
2018-01-26 22:35:54 +00:00
if ( cSize > 0 ) { /* compression is ongoing or completed */
2018-01-27 01:08:58 +00:00
size_t const toFlush = MIN ( cSize - mtctx - > jobs [ wJobID ] . dstFlushed , output - > size - output - > pos ) ;
2018-01-26 22:35:54 +00:00
DEBUGLOG ( 5 , " ZSTDMT_flushProduced: Flushing %u bytes from job %u (completion:%u/%u, generated:%u) " ,
2018-01-27 01:08:58 +00:00
( U32 ) toFlush , mtctx - > doneJobID , ( U32 ) srcConsumed , ( U32 ) srcSize , ( U32 ) cSize ) ;
2018-01-26 22:35:54 +00:00
assert ( mtctx - > doneJobID < mtctx - > nextJobID ) ;
2018-01-26 20:15:43 +00:00
assert ( cSize > = mtctx - > jobs [ wJobID ] . dstFlushed ) ;
2018-01-26 22:35:54 +00:00
assert ( mtctx - > jobs [ wJobID ] . dstBuff . start ! = NULL ) ;
2018-01-27 01:08:58 +00:00
memcpy ( ( char * ) output - > dst + output - > pos ,
( const char * ) mtctx - > jobs [ wJobID ] . dstBuff . start + mtctx - > jobs [ wJobID ] . dstFlushed ,
toFlush ) ;
output - > pos + = toFlush ;
mtctx - > jobs [ wJobID ] . dstFlushed + = toFlush ; /* can write : this value is only used by mtctx */
2018-01-20 01:35:08 +00:00
2018-01-27 01:08:58 +00:00
if ( ( srcConsumed = = srcSize ) /* job completed */
2018-01-26 20:15:43 +00:00
& & ( mtctx - > jobs [ wJobID ] . dstFlushed = = cSize ) ) { /* output buffer fully flushed => free this job position */
2018-01-20 01:35:08 +00:00
DEBUGLOG ( 5 , " Job %u completed (%u bytes), moving to next one " ,
2018-01-26 20:15:43 +00:00
mtctx - > doneJobID , ( U32 ) mtctx - > jobs [ wJobID ] . dstFlushed ) ;
ZSTDMT_releaseBuffer ( mtctx - > bufPool , mtctx - > jobs [ wJobID ] . dstBuff ) ;
2018-01-26 01:35:49 +00:00
mtctx - > jobs [ wJobID ] . dstBuff = g_nullBuffer ;
2018-01-26 20:15:43 +00:00
mtctx - > jobs [ wJobID ] . cSize = 0 ; /* ensure this job slot is considered "not started" in future check */
2018-01-27 01:08:58 +00:00
mtctx - > consumed + = srcSize ;
2018-01-26 20:15:43 +00:00
mtctx - > produced + = cSize ;
2018-01-26 01:35:49 +00:00
mtctx - > doneJobID + + ;
2018-01-20 01:35:08 +00:00
} }
2018-01-19 00:20:26 +00:00
/* return value : how many bytes left in buffer ; fake it to 1 when unknown but >0 */
2018-01-26 20:15:43 +00:00
if ( cSize > mtctx - > jobs [ wJobID ] . dstFlushed ) return ( cSize - mtctx - > jobs [ wJobID ] . dstFlushed ) ;
2018-01-27 01:08:58 +00:00
if ( srcSize > srcConsumed ) return 1 ; /* current job not completely compressed */
2018-01-20 01:35:08 +00:00
}
2018-01-26 20:15:43 +00:00
if ( mtctx - > doneJobID < mtctx - > nextJobID ) return 1 ; /* some more jobs ongoing */
if ( mtctx - > jobReady ) return 1 ; /* one job is ready to push, just not yet in the list */
if ( mtctx - > inBuff . filled > 0 ) return 1 ; /* input is not empty, and still needs to be converted into a job */
2018-01-30 22:43:36 +00:00
mtctx - > allJobsCompleted = mtctx - > frameEnded ; /* all jobs are entirely flushed => if this one is last one, frame is completed */
2018-01-26 20:15:43 +00:00
if ( end = = ZSTD_e_end ) return ! mtctx - > frameEnded ; /* for ZSTD_e_end, question becomes : is frame completed ? instead of : are internal buffers fully flushed ? */
return 0 ; /* internal buffers fully flushed */
2018-01-20 01:35:08 +00:00
}
2017-01-23 19:43:51 +00:00
2018-02-28 04:09:18 +00:00
/**
* Returns the range of data used by the earliest job that is not yet complete .
* If the data of the first job is broken up into two segments , we cover both
* sections .
*/
static range_t ZSTDMT_getInputDataInUse ( ZSTDMT_CCtx * mtctx )
{
unsigned const firstJobID = mtctx - > doneJobID ;
unsigned const lastJobID = mtctx - > nextJobID ;
unsigned jobID ;
for ( jobID = firstJobID ; jobID < lastJobID ; + + jobID ) {
unsigned const wJobID = jobID & mtctx - > jobIDMask ;
size_t consumed ;
ZSTD_PTHREAD_MUTEX_LOCK ( & mtctx - > jobs [ wJobID ] . job_mutex ) ;
consumed = mtctx - > jobs [ wJobID ] . consumed ;
ZSTD_pthread_mutex_unlock ( & mtctx - > jobs [ wJobID ] . job_mutex ) ;
if ( consumed < mtctx - > jobs [ wJobID ] . src . size ) {
range_t range = mtctx - > jobs [ wJobID ] . prefix ;
if ( range . size = = 0 ) {
/* Empty prefix */
range = mtctx - > jobs [ wJobID ] . src ;
}
/* Job source in multiple segments not supported yet */
assert ( range . start < = mtctx - > jobs [ wJobID ] . src . start ) ;
return range ;
}
}
return kNullRange ;
}
/**
* Returns non - zero iff buffer and range overlap .
*/
static int ZSTDMT_isOverlapped ( buffer_t buffer , range_t range )
{
BYTE const * const bufferStart = ( BYTE const * ) buffer . start ;
BYTE const * const bufferEnd = bufferStart + buffer . capacity ;
BYTE const * const rangeStart = ( BYTE const * ) range . start ;
BYTE const * const rangeEnd = rangeStart + range . size ;
if ( rangeStart = = NULL | | bufferStart = = NULL )
return 0 ;
2018-03-20 00:54:04 +00:00
/* Empty ranges cannot overlap */
if ( bufferStart = = bufferEnd | | rangeStart = = rangeEnd )
return 0 ;
2018-02-28 04:09:18 +00:00
return bufferStart < rangeEnd & & rangeStart < bufferEnd ;
}
2018-03-07 03:51:21 +00:00
static int ZSTDMT_doesOverlapWindow ( buffer_t buffer , ZSTD_window_t window )
{
range_t extDict ;
range_t prefix ;
extDict . start = window . dictBase + window . lowLimit ;
extDict . size = window . dictLimit - window . lowLimit ;
prefix . start = window . base + window . dictLimit ;
prefix . size = window . nextSrc - ( window . base + window . dictLimit ) ;
DEBUGLOG ( 5 , " extDict [0x%zx, 0x%zx) " ,
( size_t ) extDict . start ,
( size_t ) extDict . start + extDict . size ) ;
DEBUGLOG ( 5 , " prefix [0x%zx, 0x%zx) " ,
( size_t ) prefix . start ,
( size_t ) prefix . start + prefix . size ) ;
return ZSTDMT_isOverlapped ( buffer , extDict )
| | ZSTDMT_isOverlapped ( buffer , prefix ) ;
}
static void ZSTDMT_waitForLdmComplete ( ZSTDMT_CCtx * mtctx , buffer_t buffer )
{
if ( mtctx - > params . ldmParams . enableLdm ) {
ZSTD_pthread_mutex_t * mutex = & mtctx - > serial . ldmWindowMutex ;
DEBUGLOG ( 5 , " source [0x%zx, 0x%zx) " ,
( size_t ) buffer . start ,
( size_t ) buffer . start + buffer . capacity ) ;
ZSTD_PTHREAD_MUTEX_LOCK ( mutex ) ;
while ( ZSTDMT_doesOverlapWindow ( buffer , mtctx - > serial . ldmWindow ) ) {
DEBUGLOG ( 6 , " Waiting for LDM to finish... " ) ;
ZSTD_pthread_cond_wait ( & mtctx - > serial . ldmWindowCond , mutex ) ;
}
DEBUGLOG ( 6 , " Done waiting for LDM to finish " ) ;
ZSTD_pthread_mutex_unlock ( mutex ) ;
}
}
2018-02-28 04:09:18 +00:00
/**
* Attempts to set the inBuff to the next section to fill .
* If any part of the new section is still in use we give up .
* Returns non - zero if the buffer is filled .
*/
static int ZSTDMT_tryGetInputRange ( ZSTDMT_CCtx * mtctx )
{
range_t const inUse = ZSTDMT_getInputDataInUse ( mtctx ) ;
size_t const spaceLeft = mtctx - > roundBuff . capacity - mtctx - > roundBuff . pos ;
size_t const target = mtctx - > targetSectionSize ;
buffer_t buffer ;
assert ( mtctx - > inBuff . buffer . start = = NULL ) ;
assert ( mtctx - > roundBuff . capacity > = target ) ;
if ( spaceLeft < target ) {
/* ZSTD_invalidateRepCodes() doesn't work for extDict variants.
* Simply copy the prefix to the beginning in that case .
*/
BYTE * const start = ( BYTE * ) mtctx - > roundBuff . buffer ;
size_t const prefixSize = mtctx - > inBuff . prefix . size ;
buffer . start = start ;
buffer . capacity = prefixSize ;
if ( ZSTDMT_isOverlapped ( buffer , inUse ) ) {
DEBUGLOG ( 6 , " Waiting for buffer... " ) ;
return 0 ;
}
2018-03-07 03:51:21 +00:00
ZSTDMT_waitForLdmComplete ( mtctx , buffer ) ;
2018-02-28 04:09:18 +00:00
memmove ( start , mtctx - > inBuff . prefix . start , prefixSize ) ;
mtctx - > inBuff . prefix . start = start ;
mtctx - > roundBuff . pos = prefixSize ;
}
buffer . start = mtctx - > roundBuff . buffer + mtctx - > roundBuff . pos ;
buffer . capacity = target ;
if ( ZSTDMT_isOverlapped ( buffer , inUse ) ) {
DEBUGLOG ( 6 , " Waiting for buffer... " ) ;
return 0 ;
}
assert ( ! ZSTDMT_isOverlapped ( buffer , mtctx - > inBuff . prefix ) ) ;
2018-03-07 03:51:21 +00:00
ZSTDMT_waitForLdmComplete ( mtctx , buffer ) ;
2018-02-28 04:09:18 +00:00
DEBUGLOG ( 5 , " Using prefix range [%zx, %zx) " ,
( size_t ) mtctx - > inBuff . prefix . start ,
( size_t ) mtctx - > inBuff . prefix . start + mtctx - > inBuff . prefix . size ) ;
DEBUGLOG ( 5 , " Using source range [%zx, %zx) " ,
( size_t ) buffer . start ,
( size_t ) buffer . start + buffer . capacity ) ;
mtctx - > inBuff . buffer = buffer ;
mtctx - > inBuff . filled = 0 ;
assert ( mtctx - > roundBuff . pos + buffer . capacity < = mtctx - > roundBuff . capacity ) ;
return 1 ;
}
2017-01-23 19:43:51 +00:00
2017-06-30 21:51:01 +00:00
/** ZSTDMT_compressStream_generic() :
2017-09-28 18:46:19 +00:00
* internal use only - exposed to be invoked from zstd_compress . c
2017-06-30 21:51:01 +00:00
* assumption : output and input are valid ( pos < = size )
* @ return : minimum amount of data remaining to flush , 0 if none */
size_t ZSTDMT_compressStream_generic ( ZSTDMT_CCtx * mtctx ,
ZSTD_outBuffer * output ,
ZSTD_inBuffer * input ,
ZSTD_EndDirective endOp )
{
2017-09-28 18:46:19 +00:00
unsigned forwardInputProgress = 0 ;
2018-03-13 21:23:14 +00:00
DEBUGLOG ( 5 , " ZSTDMT_compressStream_generic (endOp=%u, srcSize=%u) " ,
2018-01-26 22:35:54 +00:00
( U32 ) endOp , ( U32 ) ( input - > size - input - > pos ) ) ;
2017-06-30 21:51:01 +00:00
assert ( output - > pos < = output - > size ) ;
assert ( input - > pos < = input - > size ) ;
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
2018-01-16 23:28:43 +00:00
if ( mtctx - > singleBlockingThread ) { /* delegate to single-thread (synchronous) */
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
return ZSTD_compressStream_generic ( mtctx - > cctxPool - > cctx [ 0 ] , output , input , endOp ) ;
}
2017-06-30 21:51:01 +00:00
if ( ( mtctx - > frameEnded ) & & ( endOp = = ZSTD_e_continue ) ) {
2017-09-28 09:14:48 +00:00
/* current frame being ended. Only flush/end are allowed */
2017-06-30 21:51:01 +00:00
return ERROR ( stage_wrong ) ;
}
2017-06-30 22:44:57 +00:00
2017-09-28 18:46:19 +00:00
/* single-pass shortcut (note : synchronous-mode) */
2018-01-26 20:15:43 +00:00
if ( ( mtctx - > nextJobID = = 0 ) /* just started */
& & ( mtctx - > inBuff . filled = = 0 ) /* nothing buffered */
& & ( ! mtctx - > jobReady ) /* no job already created */
& & ( endOp = = ZSTD_e_end ) /* end order */
2018-01-25 22:52:34 +00:00
& & ( output - > size - output - > pos > = ZSTD_compressBound ( input - > size - input - > pos ) ) ) { /* enough space in dst */
2017-08-22 21:24:47 +00:00
size_t const cSize = ZSTDMT_compress_advanced_internal ( mtctx ,
2017-06-30 21:51:01 +00:00
( char * ) output - > dst + output - > pos , output - > size - output - > pos ,
( const char * ) input - > src + input - > pos , input - > size - input - > pos ,
2017-08-25 20:14:51 +00:00
mtctx - > cdict , mtctx - > params ) ;
2017-06-30 21:51:01 +00:00
if ( ZSTD_isError ( cSize ) ) return cSize ;
input - > pos = input - > size ;
output - > pos + = cSize ;
mtctx - > allJobsCompleted = 1 ;
mtctx - > frameEnded = 1 ;
return 0 ;
2017-07-04 17:36:41 +00:00
}
2017-06-30 21:51:01 +00:00
/* fill input buffer */
2018-01-19 18:01:40 +00:00
if ( ( ! mtctx - > jobReady )
& & ( input - > size > input - > pos ) ) { /* support NULL input */
2017-07-11 00:16:41 +00:00
if ( mtctx - > inBuff . buffer . start = = NULL ) {
2018-02-28 04:09:18 +00:00
assert ( mtctx - > inBuff . filled = = 0 ) ; /* Can't fill an empty buffer */
if ( ! ZSTDMT_tryGetInputRange ( mtctx ) ) {
/* It is only possible for this operation to fail if there are
* still compression jobs ongoing .
*/
assert ( mtctx - > doneJobID ! = mtctx - > nextJobID ) ;
2018-01-26 21:00:14 +00:00
}
}
2018-02-28 04:09:18 +00:00
if ( mtctx - > inBuff . buffer . start ! = NULL ) {
size_t const toLoad = MIN ( input - > size - input - > pos , mtctx - > targetSectionSize - mtctx - > inBuff . filled ) ;
assert ( mtctx - > inBuff . buffer . capacity > = mtctx - > targetSectionSize ) ;
2018-01-19 18:01:40 +00:00
DEBUGLOG ( 5 , " ZSTDMT_compressStream_generic: adding %u bytes on top of %u to buffer of size %u " ,
2018-02-28 04:09:18 +00:00
( U32 ) toLoad , ( U32 ) mtctx - > inBuff . filled , ( U32 ) mtctx - > targetSectionSize ) ;
2017-07-11 00:16:41 +00:00
memcpy ( ( char * ) mtctx - > inBuff . buffer . start + mtctx - > inBuff . filled , ( const char * ) input - > src + input - > pos , toLoad ) ;
input - > pos + = toLoad ;
mtctx - > inBuff . filled + = toLoad ;
2017-09-28 18:46:19 +00:00
forwardInputProgress = toLoad > 0 ;
2018-01-23 21:12:40 +00:00
}
if ( ( input - > pos < input - > size ) & & ( endOp = = ZSTD_e_end ) )
endOp = ZSTD_e_flush ; /* can't end now : not all input consumed */
}
2017-06-30 21:51:01 +00:00
2018-01-19 18:01:40 +00:00
if ( ( mtctx - > jobReady )
2018-02-28 04:09:18 +00:00
| | ( mtctx - > inBuff . filled > = mtctx - > targetSectionSize ) /* filled enough : let's compress */
2018-01-23 21:12:40 +00:00
| | ( ( endOp ! = ZSTD_e_continue ) & & ( mtctx - > inBuff . filled > 0 ) ) /* something to flush : let's go */
| | ( ( endOp = = ZSTD_e_end ) & & ( ! mtctx - > frameEnded ) ) ) { /* must finish the frame with a zero-size block */
2018-02-28 04:09:18 +00:00
size_t const jobSize = mtctx - > inBuff . filled ;
assert ( mtctx - > inBuff . filled < = mtctx - > targetSectionSize ) ;
2018-01-23 23:52:40 +00:00
CHECK_F ( ZSTDMT_createCompressionJob ( mtctx , jobSize , endOp ) ) ;
2017-06-30 21:51:01 +00:00
}
/* check for potential compressed data ready to be flushed */
2018-01-23 23:19:11 +00:00
{ size_t const remainingToFlush = ZSTDMT_flushProduced ( mtctx , output , ! forwardInputProgress , endOp ) ; /* block if there was no forward input progress */
2018-01-25 22:52:34 +00:00
if ( input - > pos < input - > size ) return MAX ( remainingToFlush , 1 ) ; /* input not consumed : do not end flush yet */
2018-01-19 21:19:59 +00:00
return remainingToFlush ;
2017-06-30 21:51:01 +00:00
}
}
2018-01-26 01:35:49 +00:00
size_t ZSTDMT_compressStream ( ZSTDMT_CCtx * mtctx , ZSTD_outBuffer * output , ZSTD_inBuffer * input )
2017-01-12 00:25:46 +00:00
{
2018-01-26 01:35:49 +00:00
CHECK_F ( ZSTDMT_compressStream_generic ( mtctx , output , input , ZSTD_e_continue ) ) ;
2017-01-24 19:48:40 +00:00
2017-01-12 00:25:46 +00:00
/* recommended next input size : fill current input buffer */
2018-02-28 04:09:18 +00:00
return mtctx - > targetSectionSize - mtctx - > inBuff . filled ; /* note : could be zero when input buffer is fully filled and no more availability to create new job */
2017-01-17 23:31:16 +00:00
}
2017-01-18 00:15:18 +00:00
2018-01-23 23:19:11 +00:00
static size_t ZSTDMT_flushStream_internal ( ZSTDMT_CCtx * mtctx , ZSTD_outBuffer * output , ZSTD_EndDirective endFrame )
2017-01-17 23:31:16 +00:00
{
2018-02-28 04:09:18 +00:00
size_t const srcSize = mtctx - > inBuff . filled ;
2017-12-13 00:20:51 +00:00
DEBUGLOG ( 5 , " ZSTDMT_flushStream_internal " ) ;
2017-01-17 23:31:16 +00:00
2018-01-19 21:19:59 +00:00
if ( mtctx - > jobReady /* one job ready for a worker to pick up */
| | ( srcSize > 0 ) /* still some data within input buffer */
2018-01-23 23:19:11 +00:00
| | ( ( endFrame = = ZSTD_e_end ) & & ! mtctx - > frameEnded ) ) { /* need a last 0-size block to end frame */
2018-01-20 01:35:08 +00:00
DEBUGLOG ( 5 , " ZSTDMT_flushStream_internal : create a new job (%u bytes, end:%u) " ,
2018-01-23 23:19:11 +00:00
( U32 ) srcSize , ( U32 ) endFrame ) ;
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
CHECK_F ( ZSTDMT_createCompressionJob ( mtctx , srcSize , endFrame ) ) ;
2017-01-17 23:31:16 +00:00
}
/* check if there is any data available to flush */
2018-01-23 23:19:11 +00:00
return ZSTDMT_flushProduced ( mtctx , output , 1 /* blockToFlush */ , endFrame ) ;
2017-01-17 23:31:16 +00:00
}
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
size_t ZSTDMT_flushStream ( ZSTDMT_CCtx * mtctx , ZSTD_outBuffer * output )
2017-01-17 23:31:16 +00:00
{
2017-06-20 01:25:35 +00:00
DEBUGLOG ( 5 , " ZSTDMT_flushStream " ) ;
2018-01-16 23:28:43 +00:00
if ( mtctx - > singleBlockingThread )
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
return ZSTD_flushStream ( mtctx - > cctxPool - > cctx [ 0 ] , output ) ;
2018-01-23 23:19:11 +00:00
return ZSTDMT_flushStream_internal ( mtctx , output , ZSTD_e_flush ) ;
2017-01-17 23:31:16 +00:00
}
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
size_t ZSTDMT_endStream ( ZSTDMT_CCtx * mtctx , ZSTD_outBuffer * output )
2017-01-17 23:31:16 +00:00
{
2017-06-30 21:51:01 +00:00
DEBUGLOG ( 4 , " ZSTDMT_endStream " ) ;
2018-01-16 23:28:43 +00:00
if ( mtctx - > singleBlockingThread )
Fixed Btree update
ZSTD_updateTree() expected to be followed by a Bt match finder, which would update zc->nextToUpdate.
With the new optimal match finder, it's not necessarily the case : a match might be found during repcode or hash3, and stops there because it reaches sufficient_len, without even entering the binary tree.
Previous policy was to nonetheless update zc->nextToUpdate, but the current position would not be inserted, creating "holes" in the btree, aka positions that will no longer be searched.
Now, when current position is not inserted, zc->nextToUpdate is not update, expecting ZSTD_updateTree() to fill the tree later on.
Solution selected is that ZSTD_updateTree() takes care of properly setting zc->nextToUpdate,
so that it no longer depends on a future function to do this job.
It took time to get there, as the issue started with a memory sanitizer error.
The pb would have been easier to spot with a proper `assert()`.
So this patch add a few of them.
Additionnally, I discovered that `make test` does not enable `assert()` during CLI tests.
This patch enables them.
Unfortunately, these `assert()` triggered other (unrelated) bugs during CLI tests, mostly within zstdmt.
So this patch also fixes them.
- Changed packed structure for gcc memory access : memory sanitizer would complain that a read "might" reach out-of-bound position on the ground that the `union` is larger than the type accessed.
Now, to avoid this issue, each type is independent.
- ZSTD_CCtxParams_setParameter() : @return provides the value of parameter, clamped/fixed appropriately.
- ZSTDMT : changed constant name to ZSTDMT_JOBSIZE_MIN
- ZSTDMT : multithreading is automatically disabled when srcSize <= ZSTDMT_JOBSIZE_MIN, since only one thread will be used in this case (saves memory and runtime).
- ZSTDMT : nbThreads is automatically clamped on setting the value.
2017-11-16 20:18:56 +00:00
return ZSTD_endStream ( mtctx - > cctxPool - > cctx [ 0 ] , output ) ;
2018-01-23 23:19:11 +00:00
return ZSTDMT_flushStream_internal ( mtctx , output , ZSTD_e_end ) ;
2017-01-12 00:25:46 +00:00
}