zstd/lib/compress/zstdmt_compress.c

295 lines
10 KiB
C
Raw Normal View History

2016-12-26 22:19:36 -08:00
#include <stdlib.h> /* malloc */
#include <pool.h> /* threadpool */
#include "threading.h" /* mutex */
2016-12-26 22:19:36 -08:00
#include "zstd_internal.h" /* MIN, ERROR */
#include "zstdmt_compress.h"
#if 0
# include <stdio.h>
# include <unistd.h>
# include <sys/times.h>
static unsigned g_debugLevel = 2;
2016-12-26 22:19:36 -08:00
# define DEBUGLOG(l, ...) if (l<=g_debugLevel) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, " \n"); }
static unsigned long long GetCurrentClockTimeMicroseconds()
{
static clock_t _ticksPerSecond = 0;
if (_ticksPerSecond <= 0) _ticksPerSecond = sysconf(_SC_CLK_TCK);
struct tms junk; clock_t newTicks = (clock_t) times(&junk);
return ((((unsigned long long)newTicks)*(1000000))/_ticksPerSecond);
}
#define MUTEX_WAIT_TIME_DLEVEL 5
#define PTHREAD_MUTEX_LOCK(mutex) \
if (g_debugLevel>=MUTEX_WAIT_TIME_DLEVEL) { \
unsigned long long beforeTime = GetCurrentClockTimeMicroseconds(); \
pthread_mutex_lock(mutex); \
unsigned long long afterTime = GetCurrentClockTimeMicroseconds(); \
unsigned long long elapsedTime = (afterTime-beforeTime); \
if (elapsedTime > 1000) { /* or whatever threshold you like; I'm using 1 millisecond here */ \
DEBUGLOG(MUTEX_WAIT_TIME_DLEVEL, "Thread %li took %llu microseconds to acquire mutex %s \n", \
(long int) pthread_self(), elapsedTime, #mutex); \
} \
} else pthread_mutex_lock(mutex);
2016-12-26 22:19:36 -08:00
#else
2016-12-26 22:19:36 -08:00
# define DEBUGLOG(l, ...) /* disabled */
# define PTHREAD_MUTEX_LOCK(m) pthread_mutex_lock(m)
2016-12-26 22:19:36 -08:00
#endif
2016-12-26 22:19:36 -08:00
#define ZSTDMT_NBTHREADS_MAX 128
2016-12-31 05:45:33 -08:00
/* === Buffer Pool === */
2016-12-26 22:19:36 -08:00
typedef struct buffer_s {
void* start;
size_t size;
2016-12-26 22:19:36 -08:00
} buffer_t;
typedef struct ZSTDMT_bufferPool_s {
2016-12-31 05:45:33 -08:00
unsigned totalBuffers;;
2016-12-26 22:19:36 -08:00
unsigned nbBuffers;
2016-12-31 05:45:33 -08:00
buffer_t bTable[1]; /* variable size */
2016-12-26 22:19:36 -08:00
} ZSTDMT_bufferPool;
2016-12-31 05:45:33 -08:00
static ZSTDMT_bufferPool* ZSTDMT_createBufferPool(unsigned nbThreads)
{
unsigned const maxNbBuffers = 2*nbThreads + 2;
ZSTDMT_bufferPool* const bufPool = (ZSTDMT_bufferPool*)calloc(1, sizeof(ZSTDMT_bufferPool) + maxNbBuffers * sizeof(buffer_t));
if (bufPool==NULL) return NULL;
bufPool->totalBuffers = maxNbBuffers;
return bufPool;
}
static void ZSTDMT_freeBufferPool(ZSTDMT_bufferPool* bufPool)
{
unsigned u;
if (!bufPool) return; /* compatibility with free on NULL */
for (u=0; u<bufPool->totalBuffers; u++)
free(bufPool->bTable[u].start);
free(bufPool);
}
/* note : invocation only from main thread ! */
2016-12-26 22:19:36 -08:00
static buffer_t ZSTDMT_getBuffer(ZSTDMT_bufferPool* pool, size_t bSize)
{
if (pool->nbBuffers) { /* try to use an existing buffer */
2017-01-01 16:11:55 -08:00
buffer_t const buf = pool->bTable[--(pool->nbBuffers)];
size_t const availBufferSize = buf.size;
2016-12-26 22:19:36 -08:00
if ((availBufferSize >= bSize) & (availBufferSize <= 10*bSize)) /* large enough, but not too much */
return buf;
free(buf.start); /* size conditions not respected : create a new buffer */
}
/* create new buffer */
2017-01-01 16:11:55 -08:00
{ buffer_t buf;
buf.size = bSize;
buf.start = malloc(bSize);
return buf;
}
2016-12-26 22:19:36 -08:00
}
/* effectively store buffer for later re-use, up to pool capacity */
static void ZSTDMT_releaseBuffer(ZSTDMT_bufferPool* pool, buffer_t buf)
{
2016-12-31 05:45:33 -08:00
if (pool->nbBuffers < pool->totalBuffers) {
pool->bTable[pool->nbBuffers++] = buf; /* store for later re-use */
2016-12-26 22:19:36 -08:00
return;
}
2016-12-31 05:45:33 -08:00
/* Reached bufferPool capacity (should not happen) */
free(buf.start);
2016-12-26 22:19:36 -08:00
}
typedef struct {
ZSTD_CCtx* cctx;
const void* srcStart;
size_t srcSize;
buffer_t dstBuff;
int compressionLevel;
unsigned frameID;
size_t cSize;
unsigned jobCompleted;
pthread_mutex_t* jobCompleted_mutex;
pthread_cond_t* jobCompleted_cond;
} ZSTDMT_jobDescription;
2016-12-26 22:19:36 -08:00
/* ZSTDMT_compressFrame() : POOL_function type */
void ZSTDMT_compressFrame(void* jobDescription)
2016-12-26 22:19:36 -08:00
{
ZSTDMT_jobDescription* const job = (ZSTDMT_jobDescription*)jobDescription;
2017-01-01 16:11:55 -08:00
DEBUGLOG(5, "thread : compressing %u bytes from frame %u with ZSTD_compressCCtx : ", (unsigned)job->srcSize, job->jobCompleted);
job->cSize = ZSTD_compressCCtx(job->cctx, job->dstBuff.start, job->dstBuff.size, job->srcStart, job->srcSize, job->compressionLevel);
DEBUGLOG(5, "compressed to %u bytes ", (unsigned)job->cSize);
DEBUGLOG(5, "sending jobCompleted signal");
pthread_mutex_lock(job->jobCompleted_mutex);
2017-01-01 15:49:42 -08:00
job->jobCompleted = 1;
pthread_cond_signal(job->jobCompleted_cond);
pthread_mutex_unlock(job->jobCompleted_mutex);
DEBUGLOG(5, "ZSTDMT_compressFrame completed");
}
2016-12-31 05:45:33 -08:00
/* === CCtx Pool === */
typedef struct {
unsigned totalCCtx;
unsigned availCCtx;
ZSTD_CCtx* cctx[1]; /* variable size */
} ZSTDMT_CCtxPool;
2016-12-31 05:45:33 -08:00
/* note : CCtxPool invocation only from main thread */
static ZSTDMT_CCtxPool* ZSTDMT_createCCtxPool(unsigned nbThreads)
{
ZSTDMT_CCtxPool* const cctxPool = (ZSTDMT_CCtxPool*) calloc(1, sizeof(ZSTDMT_CCtxPool) + nbThreads*sizeof(ZSTD_CCtx*));
if (!cctxPool) return NULL;
{ unsigned u;
for (u=0; u<nbThreads; u++)
cctxPool->cctx[u] = ZSTD_createCCtx(); /* check for NULL result ! */
}
cctxPool->totalCCtx = cctxPool->availCCtx = nbThreads;
return cctxPool;
}
static ZSTD_CCtx* ZSTDMT_getCCtx(ZSTDMT_CCtxPool* pool)
{
if (pool->availCCtx) {
pool->availCCtx--;
return pool->cctx[pool->availCCtx];
2016-12-26 22:19:36 -08:00
}
/* should not be possible, since totalCCtx==nbThreads */
return ZSTD_createCCtx();
2016-12-26 22:19:36 -08:00
}
static void ZSTDMT_releaseCCtx(ZSTDMT_CCtxPool* pool, ZSTD_CCtx* cctx)
{
if (pool->availCCtx < pool->totalCCtx)
pool->cctx[pool->availCCtx++] = cctx;
else
/* should not be possible, since totalCCtx==nbThreads */
ZSTD_freeCCtx(cctx);
}
static void ZSTDMT_freeCCtxPool(ZSTDMT_CCtxPool* pool)
{
unsigned u;
for (u=0; u<pool->totalCCtx; u++)
ZSTD_freeCCtx(pool->cctx[u]);
free(pool);
}
struct ZSTDMT_CCtx_s {
POOL_ctx* factory;
2016-12-31 05:45:33 -08:00
ZSTDMT_bufferPool* buffPool;
ZSTDMT_CCtxPool* cctxPool;
unsigned nbThreads;
pthread_mutex_t jobCompleted_mutex;
pthread_cond_t jobCompleted_cond;
ZSTDMT_jobDescription jobs[1]; /* variable size */
};
2016-12-26 22:19:36 -08:00
ZSTDMT_CCtx *ZSTDMT_createCCtx(unsigned nbThreads)
{
2017-01-01 16:11:55 -08:00
ZSTDMT_CCtx* cctx;
2016-12-26 22:19:36 -08:00
if ((nbThreads < 1) | (nbThreads > ZSTDMT_NBTHREADS_MAX)) return NULL;
2017-01-01 16:11:55 -08:00
cctx = (ZSTDMT_CCtx*) calloc(1, sizeof(ZSTDMT_CCtx) + nbThreads*sizeof(ZSTDMT_jobDescription));
2016-12-26 22:19:36 -08:00
if (!cctx) return NULL;
cctx->nbThreads = nbThreads;
cctx->factory = POOL_create(nbThreads, 1);
2016-12-31 05:45:33 -08:00
cctx->buffPool = ZSTDMT_createBufferPool(nbThreads);
cctx->cctxPool = ZSTDMT_createCCtxPool(nbThreads);
pthread_mutex_init(&cctx->jobCompleted_mutex, NULL);
pthread_cond_init(&cctx->jobCompleted_cond, NULL);
2016-12-26 22:19:36 -08:00
return cctx;
}
size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx) /* incompleted ! */
2016-12-26 22:19:36 -08:00
{
POOL_free(mtctx->factory);
2016-12-31 05:45:33 -08:00
ZSTDMT_freeBufferPool(mtctx->buffPool);
ZSTDMT_freeCCtxPool(mtctx->cctxPool);
2016-12-31 05:45:33 -08:00
pthread_mutex_destroy(&mtctx->jobCompleted_mutex);
2017-01-01 15:49:42 -08:00
pthread_cond_destroy(&mtctx->jobCompleted_cond);
free(mtctx);
2016-12-26 22:19:36 -08:00
return 0;
}
size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx,
2016-12-26 22:19:36 -08:00
void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
int compressionLevel)
{
ZSTD_parameters const params = ZSTD_getParams(compressionLevel, srcSize, 0);
size_t const frameSizeTarget = (size_t)1 << (params.cParams.windowLog + 2);
unsigned const nbFramesMax = (unsigned)(srcSize / frameSizeTarget) + (srcSize < frameSizeTarget) /* min 1 */;
unsigned const nbFrames = MIN(nbFramesMax, mtctx->nbThreads);
2016-12-26 22:19:36 -08:00
size_t const avgFrameSize = (srcSize + (nbFrames-1)) / nbFrames;
size_t remainingSrcSize = srcSize;
const char* const srcStart = (const char*)src;
size_t frameStartPos = 0;
2016-12-26 22:19:36 -08:00
DEBUGLOG(2, "windowLog : %u => frameSizeTarget : %u ", params.cParams.windowLog, (U32)frameSizeTarget);
DEBUGLOG(2, "nbFrames : %u (size : %u bytes) ", nbFrames, (U32)avgFrameSize);
{ unsigned u;
for (u=0; u<nbFrames; u++) {
size_t const frameSize = MIN(remainingSrcSize, avgFrameSize);
size_t const dstBufferCapacity = u ? ZSTD_compressBound(frameSize) : dstCapacity;
2016-12-31 05:45:33 -08:00
buffer_t const dstBuffer = u ? ZSTDMT_getBuffer(mtctx->buffPool, dstBufferCapacity) : (buffer_t){ dst, dstCapacity };
ZSTD_CCtx* cctx = ZSTDMT_getCCtx(mtctx->cctxPool);
mtctx->jobs[u].srcStart = srcStart + frameStartPos;
mtctx->jobs[u].srcSize = frameSize;
mtctx->jobs[u].compressionLevel = compressionLevel;
mtctx->jobs[u].dstBuff = dstBuffer;
mtctx->jobs[u].cctx = cctx;
mtctx->jobs[u].frameID = u;
mtctx->jobs[u].jobCompleted = 0;
mtctx->jobs[u].jobCompleted_mutex = &mtctx->jobCompleted_mutex;
mtctx->jobs[u].jobCompleted_cond = &mtctx->jobCompleted_cond;
2016-12-26 22:19:36 -08:00
DEBUGLOG(3, "posting job %u (%u bytes)", u, (U32)frameSize);
POOL_add(mtctx->factory, ZSTDMT_compressFrame, &mtctx->jobs[u]);
2016-12-26 22:19:36 -08:00
frameStartPos += frameSize;
remainingSrcSize -= frameSize;
} }
/* note : since nbFrames <= nbThreads, all jobs should be running immediately in parallel */
{ unsigned frameID;
size_t dstPos = 0;
for (frameID=0; frameID<nbFrames; frameID++) {
DEBUGLOG(3, "ready to write frame %u ", frameID);
pthread_mutex_lock(&mtctx->jobCompleted_mutex);
while (mtctx->jobs[frameID].jobCompleted==0) {
DEBUGLOG(4, "waiting for jobCompleted signal for frame %u", frameID);
pthread_cond_wait(&mtctx->jobCompleted_cond, &mtctx->jobCompleted_mutex);
}
pthread_mutex_unlock(&mtctx->jobCompleted_mutex);
{ size_t const cSize = mtctx->jobs[frameID].cSize;
if (ZSTD_isError(cSize)) return cSize;
if (dstPos + cSize > dstCapacity) return ERROR(dstSize_tooSmall);
2016-12-31 05:45:33 -08:00
if (frameID) {
memcpy((char*)dst + dstPos, mtctx->jobs[frameID].dstBuff.start, cSize);
ZSTDMT_releaseBuffer(mtctx->buffPool, mtctx->jobs[frameID].dstBuff);
}
dstPos += cSize ;
}
ZSTDMT_releaseCCtx(mtctx->cctxPool, mtctx->jobs[frameID].cctx);
}
2016-12-31 05:45:33 -08:00
DEBUGLOG(3, "compressed size : %u ", (U32)dstPos);
return dstPos;
}
2016-12-26 22:19:36 -08:00
}