2015-01-24 00:58:16 +00:00
/*
fullbench . c - Detailed bench program for zstd
2016-03-15 19:47:23 +00:00
Copyright ( C ) Yann Collet 2014 - 2016
2015-01-24 00:58:16 +00:00
GPL v2 License
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License along
with this program ; if not , write to the Free Software Foundation , Inc . ,
51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA .
You can contact the author at :
2016-03-15 19:47:23 +00:00
- zstd homepage : http : //www.zstd.net
2015-01-24 00:58:16 +00:00
*/
2016-03-15 19:47:23 +00:00
/*_************************************
2015-01-24 00:58:16 +00:00
* Compiler Options
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Disable some Visual warning messages */
# define _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */
/* Unix Large Files support (>4GB) */
# if (defined(__sun__) && (!defined(__LP64__))) /* Sun Solaris 32-bits requires specific definitions */
# define _LARGEFILE_SOURCE
# define _FILE_OFFSET_BITS 64
# elif ! defined(__LP64__) /* No point defining Large file for 64 bit */
# define _LARGEFILE64_SOURCE
# endif
2016-03-15 19:47:23 +00:00
/*_************************************
2015-01-24 00:58:16 +00:00
* Includes
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <stdlib.h> /* malloc */
# include <stdio.h> /* fprintf, fopen, ftello64 */
# include <sys/types.h> /* stat64 */
# include <sys/stat.h> /* stat64 */
# include <string.h> /* strcmp */
2016-03-17 15:18:36 +00:00
# include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
2015-01-24 00:58:16 +00:00
2015-10-18 21:18:32 +00:00
# include "mem.h"
2015-11-12 15:00:04 +00:00
# include "zstd_static.h"
2015-01-24 00:58:16 +00:00
# include "fse_static.h"
2016-03-15 19:47:23 +00:00
# include "zbuff.h"
2015-02-10 17:15:20 +00:00
# include "datagen.h"
2015-01-24 00:58:16 +00:00
2016-03-15 19:47:23 +00:00
/*_************************************
2015-01-24 00:58:16 +00:00
* Compiler Options
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* S_ISREG & gettimeofday() are not supported by MSVC */
# if !defined(S_ISREG)
# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
# endif
2016-03-15 19:47:23 +00:00
/*_************************************
2015-01-24 00:58:16 +00:00
* Constants
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-10-25 23:06:36 +00:00
# define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
2015-01-24 00:58:16 +00:00
# ifndef ZSTD_VERSION
# define ZSTD_VERSION ""
# endif
# define AUTHOR "Yann Collet"
# define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_VERSION, (int)(sizeof(void*)*8), AUTHOR, __DATE__
# define KB *(1<<10)
# define MB *(1<<20)
# define NBLOOPS 6
2016-03-17 15:18:36 +00:00
# define TIMELOOP_S 2
2015-01-24 00:58:16 +00:00
# define KNUTH 2654435761U
# define MAX_MEM (1984 MB)
2015-02-11 07:34:50 +00:00
# define COMPRESSIBILITY_DEFAULT 0.50
2016-03-16 14:26:51 +00:00
static const size_t g_sampleSize = 10000000 ;
2015-01-24 00:58:16 +00:00
2016-03-15 19:47:23 +00:00
/*_************************************
2015-01-24 00:58:16 +00:00
* Macros
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
2016-03-15 19:47:23 +00:00
/*_************************************
2015-01-24 00:58:16 +00:00
* Benchmark Parameters
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-03-17 18:37:33 +00:00
static U32 g_nbIterations = NBLOOPS ;
2015-02-11 07:34:50 +00:00
static double g_compressibility = COMPRESSIBILITY_DEFAULT ;
2015-01-24 00:58:16 +00:00
2016-03-17 18:37:33 +00:00
static void BMK_SetNbIterations ( U32 nbLoops )
2015-01-24 00:58:16 +00:00
{
2016-03-17 18:37:33 +00:00
g_nbIterations = nbLoops ;
DISPLAY ( " - %i iterations - \n " , g_nbIterations ) ;
2015-01-24 00:58:16 +00:00
}
2016-03-15 19:47:23 +00:00
/*_*******************************************************
2015-01-24 00:58:16 +00:00
* Private functions
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-03-17 15:18:36 +00:00
static clock_t BMK_clockSpan ( clock_t clockStart )
2015-01-24 00:58:16 +00:00
{
2016-03-17 18:37:33 +00:00
return clock ( ) - clockStart ; /* works even if overflow, span limited to <= ~30mn */
2015-01-24 00:58:16 +00:00
}
static size_t BMK_findMaxMem ( U64 requiredMem )
{
2016-03-25 19:29:35 +00:00
size_t const step = 64 MB ;
2016-03-15 19:47:23 +00:00
void * testmem = NULL ;
2015-01-24 00:58:16 +00:00
requiredMem = ( ( ( requiredMem > > 26 ) + 1 ) < < 26 ) ;
if ( requiredMem > MAX_MEM ) requiredMem = MAX_MEM ;
2016-03-17 18:37:33 +00:00
requiredMem + = step ;
do {
2016-03-15 19:47:23 +00:00
testmem = malloc ( ( size_t ) requiredMem ) ;
2015-01-24 00:58:16 +00:00
requiredMem - = step ;
2016-03-17 18:37:33 +00:00
} while ( ! testmem ) ;
2015-01-24 00:58:16 +00:00
free ( testmem ) ;
2016-03-17 18:37:33 +00:00
return ( size_t ) requiredMem ;
2015-01-24 00:58:16 +00:00
}
2016-03-15 20:44:39 +00:00
static U64 BMK_GetFileSize ( const char * infilename )
2015-01-24 00:58:16 +00:00
{
int r ;
# if defined(_MSC_VER)
struct _stat64 statbuf ;
r = _stat64 ( infilename , & statbuf ) ;
# else
struct stat statbuf ;
r = stat ( infilename , & statbuf ) ;
# endif
if ( r | | ! S_ISREG ( statbuf . st_mode ) ) return 0 ; /* No good... */
return ( U64 ) statbuf . st_size ;
}
2016-03-15 19:47:23 +00:00
/*_*******************************************************
2015-01-24 00:58:16 +00:00
* Benchmark wrappers
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
typedef enum { bt_compressed , bt_raw , bt_rle , bt_end } blockType_t ;
2016-03-15 19:47:23 +00:00
typedef struct {
2015-01-24 00:58:16 +00:00
blockType_t blockType ;
U32 unusedBits ;
U32 origSize ;
} blockProperties_t ;
size_t local_ZSTD_compress ( void * dst , size_t dstSize , void * buff2 , const void * src , size_t srcSize )
{
( void ) buff2 ;
2015-11-11 12:43:58 +00:00
return ZSTD_compress ( dst , dstSize , src , srcSize , 1 ) ;
2015-01-24 00:58:16 +00:00
}
2016-03-16 15:05:18 +00:00
static size_t g_cSize = 0 ;
2015-01-24 00:58:16 +00:00
size_t local_ZSTD_decompress ( void * dst , size_t dstSize , void * buff2 , const void * src , size_t srcSize )
{
( void ) src ; ( void ) srcSize ;
return ZSTD_decompress ( dst , dstSize , buff2 , g_cSize ) ;
}
2016-03-16 15:05:18 +00:00
static ZSTD_DCtx * g_zdc = NULL ;
2015-11-12 15:00:04 +00:00
extern size_t ZSTD_decodeLiteralsBlock ( ZSTD_DCtx * ctx , const void * src , size_t srcSize ) ;
2015-01-24 00:58:16 +00:00
size_t local_ZSTD_decodeLiteralsBlock ( void * dst , size_t dstSize , void * buff2 , const void * src , size_t srcSize )
{
2015-10-18 21:18:32 +00:00
( void ) src ; ( void ) srcSize ; ( void ) dst ; ( void ) dstSize ;
2016-03-16 15:05:18 +00:00
return ZSTD_decodeLiteralsBlock ( ( ZSTD_DCtx * ) g_zdc , buff2 , g_cSize ) ;
2015-01-24 00:58:16 +00:00
}
2016-03-15 19:47:23 +00:00
extern size_t ZSTD_getcBlockSize ( const void * src , size_t srcSize , blockProperties_t * bpPtr ) ;
2016-04-09 15:38:53 +00:00
extern size_t ZSTD_decodeSeqHeaders ( int * nbSeq , FSE_DTable * DTableLL , FSE_DTable * DTableML , FSE_DTable * DTableOffb , U32 tableRepeatFlag , const void * src , size_t srcSize ) ;
2015-01-24 00:58:16 +00:00
size_t local_ZSTD_decodeSeqHeaders ( void * dst , size_t dstSize , void * buff2 , const void * src , size_t srcSize )
{
2015-12-10 14:51:38 +00:00
U32 DTableML [ FSE_DTABLE_SIZE_U32 ( 10 ) ] , DTableLL [ FSE_DTABLE_SIZE_U32 ( 10 ) ] , DTableOffb [ FSE_DTABLE_SIZE_U32 ( 9 ) ] ; /* MLFSELog, LLFSELog and OffFSELog are not public values */
2015-08-19 23:02:43 +00:00
int nbSeq ;
2015-01-24 00:58:16 +00:00
( void ) src ; ( void ) srcSize ; ( void ) dst ; ( void ) dstSize ;
2016-04-09 15:38:53 +00:00
return ZSTD_decodeSeqHeaders ( & nbSeq , DTableLL , DTableML , DTableOffb , 0 , buff2 , g_cSize ) ;
2015-01-24 00:58:16 +00:00
}
2016-03-15 19:47:23 +00:00
static ZBUFF_CCtx * g_zbcc = NULL ;
2016-03-16 15:05:18 +00:00
size_t local_ZBUFF_compress ( void * dst , size_t dstCapacity , void * buff2 , const void * src , size_t srcSize )
2016-03-15 19:47:23 +00:00
{
size_t compressedSize ;
2016-03-16 15:05:18 +00:00
size_t srcRead = srcSize , dstWritten = dstCapacity ;
2016-03-15 19:47:23 +00:00
( void ) buff2 ;
ZBUFF_compressInit ( g_zbcc , 1 ) ;
ZBUFF_compressContinue ( g_zbcc , dst , & dstWritten , src , & srcRead ) ;
compressedSize = dstWritten ;
2016-03-16 15:05:18 +00:00
dstWritten = dstCapacity - compressedSize ;
2016-03-15 19:47:23 +00:00
ZBUFF_compressEnd ( g_zbcc , ( ( char * ) dst ) + compressedSize , & dstWritten ) ;
compressedSize + = dstWritten ;
return compressedSize ;
}
static ZBUFF_DCtx * g_zbdc = NULL ;
2016-03-16 15:05:18 +00:00
static size_t local_ZBUFF_decompress ( void * dst , size_t dstCapacity , void * buff2 , const void * src , size_t srcSize )
2016-03-15 19:47:23 +00:00
{
2016-03-16 15:05:18 +00:00
size_t srcRead = g_cSize , dstWritten = dstCapacity ;
2016-03-15 19:47:23 +00:00
( void ) src ; ( void ) srcSize ;
ZBUFF_decompressInit ( g_zbdc ) ;
ZBUFF_decompressContinue ( g_zbdc , dst , & dstWritten , buff2 , & srcRead ) ;
return dstWritten ;
}
2016-03-16 15:05:18 +00:00
static ZSTD_CCtx * g_zcc = NULL ;
size_t local_ZSTD_compressContinue ( void * dst , size_t dstCapacity , void * buff2 , const void * src , size_t srcSize )
{
size_t compressedSize ;
( void ) buff2 ;
ZSTD_compressBegin ( g_zcc , 1 ) ;
compressedSize = ZSTD_compressContinue ( g_zcc , dst , dstCapacity , src , srcSize ) ;
compressedSize + = ZSTD_compressEnd ( g_zcc , ( ( char * ) dst ) + compressedSize , dstCapacity - compressedSize ) ;
return compressedSize ;
}
size_t local_ZSTD_decompressContinue ( void * dst , size_t dstCapacity , void * buff2 , const void * src , size_t srcSize )
{
size_t regeneratedSize = 0 ;
const BYTE * ip = ( const BYTE * ) buff2 ;
const BYTE * const iend = ip + g_cSize ;
BYTE * op = ( BYTE * ) dst ;
size_t remainingCapacity = dstCapacity ;
( void ) src ; ( void ) srcSize ;
ZSTD_decompressBegin ( g_zdc ) ;
while ( ip < iend ) {
size_t const iSize = ZSTD_nextSrcSizeToDecompress ( g_zdc ) ;
size_t const decodedSize = ZSTD_decompressContinue ( g_zdc , op , remainingCapacity , ip , iSize ) ;
ip + = iSize ;
regeneratedSize + = decodedSize ;
op + = decodedSize ;
remainingCapacity - = decodedSize ;
}
return regeneratedSize ;
}
2015-01-24 00:58:16 +00:00
2016-03-15 19:47:23 +00:00
/*_*******************************************************
2015-01-24 00:58:16 +00:00
* Bench functions
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-03-16 14:26:51 +00:00
static size_t benchMem ( const void * src , size_t srcSize , U32 benchNb )
2015-01-24 00:58:16 +00:00
{
BYTE * dstBuff ;
size_t dstBuffSize ;
BYTE * buff2 ;
const char * benchName ;
size_t ( * benchFunction ) ( void * dst , size_t dstSize , void * verifBuff , const void * src , size_t srcSize ) ;
double bestTime = 100000000. ;
2015-02-10 17:15:20 +00:00
/* Selection */
2015-01-24 00:58:16 +00:00
switch ( benchNb )
{
case 1 :
benchFunction = local_ZSTD_compress ; benchName = " ZSTD_compress " ;
break ;
2016-03-15 20:13:52 +00:00
case 2 :
2015-01-24 00:58:16 +00:00
benchFunction = local_ZSTD_decompress ; benchName = " ZSTD_decompress " ;
break ;
2016-03-16 15:05:18 +00:00
case 11 :
benchFunction = local_ZSTD_compressContinue ; benchName = " ZSTD_compressContinue " ;
break ;
case 12 :
benchFunction = local_ZSTD_decompressContinue ; benchName = " ZSTD_decompressContinue " ;
break ;
2015-01-24 00:58:16 +00:00
case 31 :
benchFunction = local_ZSTD_decodeLiteralsBlock ; benchName = " ZSTD_decodeLiteralsBlock " ;
break ;
case 32 :
benchFunction = local_ZSTD_decodeSeqHeaders ; benchName = " ZSTD_decodeSeqHeaders " ;
break ;
2016-03-15 19:47:23 +00:00
case 41 :
benchFunction = local_ZBUFF_compress ; benchName = " ZBUFF_compressContinue " ;
break ;
case 42 :
benchFunction = local_ZBUFF_decompress ; benchName = " ZBUFF_decompressContinue " ;
break ;
2015-01-24 00:58:16 +00:00
default :
return 0 ;
}
/* Allocation */
2015-01-24 11:08:52 +00:00
dstBuffSize = ZSTD_compressBound ( srcSize ) ;
2015-06-18 15:43:16 +00:00
dstBuff = ( BYTE * ) malloc ( dstBuffSize ) ;
buff2 = ( BYTE * ) malloc ( dstBuffSize ) ;
2016-03-15 19:47:23 +00:00
if ( ( ! dstBuff ) | | ( ! buff2 ) ) {
2015-01-24 00:58:16 +00:00
DISPLAY ( " \n Error: not enough memory! \n " ) ;
free ( dstBuff ) ; free ( buff2 ) ;
return 12 ;
}
/* Preparation */
switch ( benchNb )
{
2016-03-15 20:13:52 +00:00
case 2 :
2015-11-11 12:43:58 +00:00
g_cSize = ZSTD_compress ( buff2 , dstBuffSize , src , srcSize , 1 ) ;
2015-01-24 00:58:16 +00:00
break ;
2016-03-16 15:05:18 +00:00
case 11 :
if ( g_zcc = = NULL ) g_zcc = ZSTD_createCCtx ( ) ;
break ;
case 12 :
if ( g_zdc = = NULL ) g_zdc = ZSTD_createDCtx ( ) ;
2015-11-11 12:43:58 +00:00
g_cSize = ZSTD_compress ( buff2 , dstBuffSize , src , srcSize , 1 ) ;
2015-01-24 00:58:16 +00:00
break ;
2015-02-10 17:15:20 +00:00
case 31 : /* ZSTD_decodeLiteralsBlock */
2016-03-17 15:42:16 +00:00
if ( g_zdc = = NULL ) g_zdc = ZSTD_createDCtx ( ) ;
2016-03-16 15:05:18 +00:00
{ blockProperties_t bp ;
2016-03-17 15:42:16 +00:00
ZSTD_frameParams zfp ;
size_t frameHeaderSize , skippedSize ;
2015-11-11 12:43:58 +00:00
g_cSize = ZSTD_compress ( dstBuff , dstBuffSize , src , srcSize , 1 ) ;
2016-03-17 15:42:16 +00:00
frameHeaderSize = ZSTD_getFrameParams ( & zfp , dstBuff , ZSTD_frameHeaderSize_min ) ;
if ( frameHeaderSize = = 0 ) frameHeaderSize = ZSTD_frameHeaderSize_min ;
ZSTD_getcBlockSize ( dstBuff + frameHeaderSize , dstBuffSize , & bp ) ; /* Get 1st block type */
2016-03-15 19:47:23 +00:00
if ( bp . blockType ! = bt_compressed ) {
2015-01-24 12:31:55 +00:00
DISPLAY ( " ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible) \n " ) ;
2015-11-13 14:08:43 +00:00
goto _cleanOut ;
2015-01-24 12:31:55 +00:00
}
2016-03-17 15:42:16 +00:00
skippedSize = frameHeaderSize + 3 /* ZSTD_blockHeaderSize */ ;
memcpy ( buff2 , dstBuff + skippedSize , g_cSize - skippedSize ) ;
2015-11-25 13:42:45 +00:00
srcSize = srcSize > 128 KB ? 128 KB : srcSize ; /* speed relative to block */
2015-01-24 00:58:16 +00:00
break ;
}
2015-02-10 17:15:20 +00:00
case 32 : /* ZSTD_decodeSeqHeaders */
2016-03-16 15:05:18 +00:00
if ( g_zdc = = NULL ) g_zdc = ZSTD_createDCtx ( ) ;
{ blockProperties_t bp ;
2016-03-17 15:42:16 +00:00
ZSTD_frameParams zfp ;
2015-01-24 00:58:16 +00:00
const BYTE * ip = dstBuff ;
const BYTE * iend ;
2016-03-17 15:42:16 +00:00
size_t frameHeaderSize , cBlockSize ;
2015-11-25 13:42:45 +00:00
ZSTD_compress ( dstBuff , dstBuffSize , src , srcSize , 1 ) ; /* it would be better to use direct block compression here */
2016-03-17 15:42:16 +00:00
g_cSize = ZSTD_compress ( dstBuff , dstBuffSize , src , srcSize , 1 ) ;
frameHeaderSize = ZSTD_getFrameParams ( & zfp , dstBuff , ZSTD_frameHeaderSize_min ) ;
if ( frameHeaderSize = = 0 ) frameHeaderSize = ZSTD_frameHeaderSize_min ;
ip + = frameHeaderSize ; /* Skip frame Header */
cBlockSize = ZSTD_getcBlockSize ( ip , dstBuffSize , & bp ) ; /* Get 1st block type */
2016-03-15 20:44:39 +00:00
if ( bp . blockType ! = bt_compressed ) {
2015-01-24 12:31:55 +00:00
DISPLAY ( " ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible) \n " ) ;
2015-11-13 14:08:43 +00:00
goto _cleanOut ;
2015-01-24 12:31:55 +00:00
}
2016-03-17 15:42:16 +00:00
iend = ip + 3 /* ZSTD_blockHeaderSize */ + cBlockSize ; /* End of first block */
ip + = 3 /* ZSTD_blockHeaderSize */ ; /* skip block header */
2016-03-16 15:05:18 +00:00
ip + = ZSTD_decodeLiteralsBlock ( g_zdc , ip , iend - ip ) ; /* skip literal segment */
2015-01-24 00:58:16 +00:00
g_cSize = iend - ip ;
2015-11-25 13:42:45 +00:00
memcpy ( buff2 , ip , g_cSize ) ; /* copy rest of block (it starts by SeqHeader) */
srcSize = srcSize > 128 KB ? 128 KB : srcSize ; /* speed relative to block */
2015-01-24 00:58:16 +00:00
break ;
}
2016-03-15 19:47:23 +00:00
case 41 :
2016-03-16 15:05:18 +00:00
if ( g_zbcc = = NULL ) g_zbcc = ZBUFF_createCCtx ( ) ;
2016-03-15 19:47:23 +00:00
break ;
case 42 :
2016-03-16 15:05:18 +00:00
if ( g_zbdc = = NULL ) g_zbdc = ZBUFF_createDCtx ( ) ;
2016-03-15 19:47:23 +00:00
g_cSize = ZSTD_compress ( buff2 , dstBuffSize , src , srcSize , 1 ) ;
break ;
2015-01-24 00:58:16 +00:00
/* test functions */
2015-11-25 13:42:45 +00:00
/* by convention, test functions can be added > 100 */
2015-01-24 00:58:16 +00:00
default : ;
}
2015-01-24 12:31:55 +00:00
{ size_t i ; for ( i = 0 ; i < dstBuffSize ; i + + ) dstBuff [ i ] = ( BYTE ) i ; } /* warming up memory */
2016-03-17 18:37:33 +00:00
{ U32 loopNb ;
for ( loopNb = 1 ; loopNb < = g_nbIterations ; loopNb + + ) {
2016-03-17 15:18:36 +00:00
clock_t const timeLoop = TIMELOOP_S * CLOCKS_PER_SEC ;
clock_t clockStart ;
U32 nbRounds ;
size_t benchResult = 0 ;
2015-01-24 00:58:16 +00:00
double averageTime ;
DISPLAY ( " %2i- %-30.30s : \r " , loopNb , benchName ) ;
2016-03-17 15:18:36 +00:00
clockStart = clock ( ) ;
while ( clock ( ) = = clockStart ) ;
clockStart = clock ( ) ;
for ( nbRounds = 0 ; BMK_clockSpan ( clockStart ) < timeLoop ; nbRounds + + ) {
benchResult = benchFunction ( dstBuff , dstBuffSize , buff2 , src , srcSize ) ;
if ( ZSTD_isError ( benchResult ) ) { DISPLAY ( " ERROR ! %s() => %s !! \n " , benchName , ZSTD_getErrorName ( benchResult ) ) ; exit ( 1 ) ; }
2015-01-24 00:58:16 +00:00
}
2016-03-17 15:18:36 +00:00
averageTime = ( ( ( double ) BMK_clockSpan ( clockStart ) ) / CLOCKS_PER_SEC ) / nbRounds ;
2015-01-24 00:58:16 +00:00
if ( averageTime < bestTime ) bestTime = averageTime ;
2016-03-17 15:18:36 +00:00
DISPLAY ( " %2i- %-30.30s : %7.1f MB/s (%9u) \r " , loopNb , benchName , ( double ) srcSize / ( 1 MB ) / bestTime , ( U32 ) benchResult ) ;
2016-03-17 18:37:33 +00:00
} }
2016-03-17 15:18:36 +00:00
DISPLAY ( " %2u \n " , benchNb ) ;
2015-01-24 00:58:16 +00:00
2015-11-13 14:08:43 +00:00
_cleanOut :
2015-01-24 00:58:16 +00:00
free ( dstBuff ) ;
free ( buff2 ) ;
return 0 ;
}
2016-03-16 14:26:51 +00:00
static int benchSample ( U32 benchNb )
2015-01-24 00:58:16 +00:00
{
2016-03-16 14:26:51 +00:00
size_t const benchedSize = g_sampleSize ;
2015-02-11 07:34:50 +00:00
const char * name = " Sample 10MiB " ;
2015-01-24 00:58:16 +00:00
/* Allocation */
2016-03-15 20:44:39 +00:00
void * origBuff = malloc ( benchedSize ) ;
if ( ! origBuff ) { DISPLAY ( " \n Error: not enough memory! \n " ) ; return 12 ; }
2015-01-24 00:58:16 +00:00
/* Fill buffer */
2015-02-11 07:34:50 +00:00
RDG_genBuffer ( origBuff , benchedSize , g_compressibility , 0.0 , 0 ) ;
2015-01-24 00:58:16 +00:00
/* bench */
DISPLAY ( " \r %79s \r " , " " ) ;
DISPLAY ( " %s : \n " , name ) ;
if ( benchNb )
benchMem ( origBuff , benchedSize , benchNb ) ;
else
for ( benchNb = 0 ; benchNb < 100 ; benchNb + + ) benchMem ( origBuff , benchedSize , benchNb ) ;
free ( origBuff ) ;
return 0 ;
}
2016-03-16 14:26:51 +00:00
static int benchFiles ( const char * * fileNamesTable , const int nbFiles , U32 benchNb )
2015-01-24 00:58:16 +00:00
{
/* Loop for each file */
2016-03-16 14:26:51 +00:00
int fileIdx ;
for ( fileIdx = 0 ; fileIdx < nbFiles ; fileIdx + + ) {
const char * inFileName = fileNamesTable [ fileIdx ] ;
2016-03-15 20:44:39 +00:00
FILE * inFile = fopen ( inFileName , " rb " ) ;
2015-01-24 00:58:16 +00:00
U64 inFileSize ;
size_t benchedSize ;
2016-03-15 20:44:39 +00:00
void * origBuff ;
2015-01-24 00:58:16 +00:00
/* Check file existence */
2016-03-15 20:44:39 +00:00
if ( inFile = = NULL ) { DISPLAY ( " Pb opening %s \n " , inFileName ) ; return 11 ; }
2015-01-24 00:58:16 +00:00
2015-12-10 14:51:38 +00:00
/* Memory allocation & restrictions */
2015-01-24 00:58:16 +00:00
inFileSize = BMK_GetFileSize ( inFileName ) ;
2016-03-15 20:44:39 +00:00
benchedSize = BMK_findMaxMem ( inFileSize * 3 ) / 3 ;
2015-01-24 00:58:16 +00:00
if ( ( U64 ) benchedSize > inFileSize ) benchedSize = ( size_t ) inFileSize ;
if ( benchedSize < inFileSize )
2016-03-16 14:26:51 +00:00
DISPLAY ( " Not enough memory for '%s' full size; testing %u MB only... \n " , inFileName , ( U32 ) ( benchedSize > > 20 ) ) ;
2015-01-24 00:58:16 +00:00
2015-01-24 11:08:52 +00:00
/* Alloc */
2016-03-15 20:44:39 +00:00
origBuff = malloc ( benchedSize ) ;
if ( ! origBuff ) { DISPLAY ( " \n Error: not enough memory! \n " ) ; fclose ( inFile ) ; return 12 ; }
2015-01-24 00:58:16 +00:00
2015-01-24 11:08:52 +00:00
/* Fill input buffer */
2015-01-24 00:58:16 +00:00
DISPLAY ( " Loading %s... \r " , inFileName ) ;
{
2016-03-16 14:26:51 +00:00
size_t readSize = fread ( origBuff , 1 , benchedSize , inFile ) ;
fclose ( inFile ) ;
if ( readSize ! = benchedSize ) {
DISPLAY ( " \n Error: problem reading file '%s' !! \n " , inFileName ) ;
free ( origBuff ) ;
return 13 ;
} }
2015-01-24 00:58:16 +00:00
2015-01-24 11:08:52 +00:00
/* bench */
2015-01-24 00:58:16 +00:00
DISPLAY ( " \r %79s \r " , " " ) ;
DISPLAY ( " %s : \n " , inFileName ) ;
if ( benchNb )
benchMem ( origBuff , benchedSize , benchNb ) ;
else
for ( benchNb = 0 ; benchNb < 100 ; benchNb + + ) benchMem ( origBuff , benchedSize , benchNb ) ;
2016-03-16 14:26:51 +00:00
free ( origBuff ) ;
2015-01-24 00:58:16 +00:00
}
return 0 ;
}
2016-03-15 20:44:39 +00:00
static int usage ( const char * exename )
2015-01-24 00:58:16 +00:00
{
DISPLAY ( " Usage : \n " ) ;
DISPLAY ( " %s [arg] file1 file2 ... fileX \n " , exename ) ;
DISPLAY ( " Arguments : \n " ) ;
DISPLAY ( " -H/-h : Help (this text + advanced options) \n " ) ;
return 0 ;
}
2016-03-16 14:26:51 +00:00
static int usage_advanced ( const char * exename )
2015-01-24 00:58:16 +00:00
{
2016-03-16 14:26:51 +00:00
usage ( exename ) ;
2015-01-24 00:58:16 +00:00
DISPLAY ( " \n Advanced options : \n " ) ;
DISPLAY ( " -b# : test only function # \n " ) ;
DISPLAY ( " -i# : iteration loops [1-9](default : %i) \n " , NBLOOPS ) ;
2015-02-11 07:34:50 +00:00
DISPLAY ( " -P# : sample compressibility (default : %.1f%%) \n " , COMPRESSIBILITY_DEFAULT * 100 ) ;
2015-01-24 00:58:16 +00:00
return 0 ;
}
2016-03-15 20:44:39 +00:00
static int badusage ( const char * exename )
2015-01-24 00:58:16 +00:00
{
DISPLAY ( " Wrong parameters \n " ) ;
usage ( exename ) ;
2016-03-16 14:26:51 +00:00
return 1 ;
2015-01-24 00:58:16 +00:00
}
2016-03-15 20:44:39 +00:00
int main ( int argc , const char * * argv )
2015-01-24 00:58:16 +00:00
{
2016-03-16 14:26:51 +00:00
int i , filenamesStart = 0 , result ;
const char * exename = argv [ 0 ] ;
const char * input_filename = NULL ;
2015-01-24 00:58:16 +00:00
U32 benchNb = 0 , main_pause = 0 ;
DISPLAY ( WELCOME_MESSAGE ) ;
2016-03-16 14:26:51 +00:00
if ( argc < 1 ) return badusage ( exename ) ;
2015-01-24 00:58:16 +00:00
2016-03-16 14:26:51 +00:00
for ( i = 1 ; i < argc ; i + + ) {
2016-03-15 20:44:39 +00:00
const char * argument = argv [ i ] ;
2016-03-16 14:26:51 +00:00
if ( ! argument ) continue ; /* Protection if argument empty */
2015-01-24 00:58:16 +00:00
2016-03-16 14:26:51 +00:00
/* Commands (note : aggregated commands are allowed) */
if ( argument [ 0 ] = = ' - ' ) {
2015-01-24 00:58:16 +00:00
2016-03-16 14:26:51 +00:00
while ( argument [ 1 ] ! = 0 ) {
argument + + ;
2015-01-24 00:58:16 +00:00
switch ( argument [ 0 ] )
{
2015-02-11 07:34:50 +00:00
/* Display help on usage */
2015-01-24 00:58:16 +00:00
case ' h ' :
2016-03-16 14:26:51 +00:00
case ' H ' : return usage_advanced ( exename ) ;
2015-01-24 00:58:16 +00:00
2015-02-11 07:34:50 +00:00
/* Pause at the end (hidden option) */
2015-01-24 00:58:16 +00:00
case ' p ' : main_pause = 1 ; break ;
2015-02-11 07:34:50 +00:00
/* Select specific algorithm to bench */
2015-01-24 00:58:16 +00:00
case ' b ' :
benchNb = 0 ;
2016-03-15 20:44:39 +00:00
while ( ( argument [ 1 ] > = ' 0 ' ) & & ( argument [ 1 ] < = ' 9 ' ) ) {
2015-01-24 00:58:16 +00:00
benchNb * = 10 ;
benchNb + = argument [ 1 ] - ' 0 ' ;
argument + + ;
}
break ;
2015-02-11 07:34:50 +00:00
/* Modify Nb Iterations */
2015-01-24 00:58:16 +00:00
case ' i ' :
2016-03-15 20:44:39 +00:00
if ( ( argument [ 1 ] > = ' 0 ' ) & & ( argument [ 1 ] < = ' 9 ' ) ) {
2015-01-24 00:58:16 +00:00
int iters = argument [ 1 ] - ' 0 ' ;
BMK_SetNbIterations ( iters ) ;
argument + + ;
}
break ;
2016-03-16 14:26:51 +00:00
/* Select compressibility of synthetic sample */
2015-02-11 07:34:50 +00:00
case ' P ' :
2016-03-15 20:44:39 +00:00
{ U32 proba32 = 0 ;
while ( ( argument [ 1 ] > = ' 0 ' ) & & ( argument [ 1 ] < = ' 9 ' ) ) {
2015-02-11 07:34:50 +00:00
proba32 * = 10 ;
proba32 + = argument [ 1 ] - ' 0 ' ;
argument + + ;
}
g_compressibility = ( double ) proba32 / 100. ;
}
break ;
/* Unknown command */
2016-03-16 14:26:51 +00:00
default : return badusage ( exename ) ;
2015-01-24 00:58:16 +00:00
}
}
continue ;
}
2015-02-11 07:34:50 +00:00
/* first provided filename is input */
2015-01-24 00:58:16 +00:00
if ( ! input_filename ) { input_filename = argument ; filenamesStart = i ; continue ; }
}
2016-03-16 14:26:51 +00:00
if ( filenamesStart = = 0 ) /* no input file */
2015-01-24 00:58:16 +00:00
result = benchSample ( benchNb ) ;
2016-03-16 14:26:51 +00:00
else
result = benchFiles ( argv + filenamesStart , argc - filenamesStart , benchNb ) ;
2015-01-24 00:58:16 +00:00
2015-07-05 07:20:56 +00:00
if ( main_pause ) { int unused ; printf ( " press enter... \n " ) ; unused = getchar ( ) ; ( void ) unused ; }
2015-01-24 00:58:16 +00:00
return result ;
}