Skip to content

Commit 1db0363

Browse files
committed
igzip: Add compress-decompress with dictionary to perf test
Change-Id: Ic396819537f5437e6aab3ebf5d023ed5cdbe852a Signed-off-by: Greg Tucker <[email protected]>
1 parent 112dd72 commit 1db0363

File tree

1 file changed

+132
-22
lines changed

1 file changed

+132
-22
lines changed

igzip/igzip_perf.c

Lines changed: 132 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
#define BUF_SIZE 1024
4242

43-
#define OPTARGS "hl:f:z:i:d:stub:y:w:o:"
43+
#define OPTARGS "hl:f:z:i:d:stub:y:w:o:D:"
4444

4545
#define COMPRESSION_QUEUE_LIMIT 32
4646
#define UNSET -1
@@ -109,6 +109,7 @@ int level_size_buf[10] = {
109109
enum {
110110
ISAL_STATELESS,
111111
ISAL_STATEFUL,
112+
ISAL_WITH_DICTIONARY,
112113
ZLIB
113114
};
114115

@@ -161,6 +162,7 @@ int usage(void)
161162
" -s performance test isa-l stateful inflate\n"
162163
" -t performance test isa-l stateless inflate\n"
163164
" -u performance test zlib inflate\n"
165+
" -D <file> use dictionary file\n"
164166
" -o <file> output file to store compressed data (last one if multiple)\n"
165167
" -b <size> input buffer size, applies to stateful options (-f,-z,-s)\n"
166168
" -y <type> flush type: 0 (default: no flush), 1 (sync flush), 2 (full flush)\n"
@@ -187,6 +189,8 @@ void print_deflate_perf_line(struct perf_info *info)
187189
printf(" isal_stateless_deflate-> ");
188190
else if (info->strategy.mode == ISAL_STATEFUL)
189191
printf(" isal_stateful_deflate-> ");
192+
else if (info->strategy.mode == ISAL_WITH_DICTIONARY)
193+
printf(" isal_dictionary_deflate-> ");
190194
else if (info->strategy.mode == ZLIB)
191195
printf(" zlib_deflate-> ");
192196

@@ -199,6 +203,8 @@ void print_inflate_perf_line(struct perf_info *info)
199203
printf(" isal_stateless_inflate-> ");
200204
else if (info->inflate_mode == ISAL_STATEFUL)
201205
printf(" isal_stateful_inflate-> ");
206+
else if (info->inflate_mode == ISAL_WITH_DICTIONARY)
207+
printf(" isal_dictionary_inflate-> ");
202208
else if (info->inflate_mode == ZLIB)
203209
printf(" zlib_inflate-> ");
204210

@@ -235,6 +241,40 @@ int isal_deflate_round(struct isal_zstream *stream, uint8_t * outbuf, uint32_t o
235241
return 0;
236242
}
237243

244+
int isal_deflate_dict_round(struct isal_zstream *stream, uint8_t * outbuf,
245+
uint32_t outbuf_size, uint8_t * inbuf,
246+
uint32_t inbuf_size, uint32_t level,
247+
uint8_t * level_buf, uint32_t level_buf_size, int flush_type,
248+
int hist_bits, struct isal_dict *dict_str)
249+
{
250+
int check;
251+
252+
/* Setup stream for compression with dictionary */
253+
isal_deflate_init(stream);
254+
stream->level = level;
255+
stream->level_buf = level_buf;
256+
stream->level_buf_size = level_buf_size;
257+
258+
if (COMP_OK != isal_deflate_reset_dict(stream, dict_str))
259+
return 1;
260+
261+
stream->end_of_stream = 1;
262+
stream->flush = flush_type;
263+
stream->next_in = inbuf;
264+
stream->avail_in = inbuf_size;
265+
stream->next_out = outbuf;
266+
stream->avail_out = outbuf_size;
267+
stream->hist_bits = hist_bits;
268+
269+
check = isal_deflate(stream);
270+
271+
/* Verify Compression Success */
272+
if (COMP_OK != check || stream->avail_in > 0)
273+
return 1;
274+
275+
return 0;
276+
}
277+
238278
int isal_inflate_round(struct inflate_state *state, uint8_t * inbuf, uint32_t inbuf_size,
239279
uint8_t * outbuf, uint32_t outbuf_size, int hist_bits)
240280
{
@@ -307,7 +347,8 @@ int isal_deflate_stateful_round(struct isal_zstream *stream, uint8_t * outbuf,
307347

308348
int isal_inflate_stateful_round(struct inflate_state *state, uint8_t * inbuf,
309349
uint32_t inbuf_size, uint32_t in_block_size, uint8_t * outbuf,
310-
uint32_t outbuf_size, int hist_bits)
350+
uint32_t outbuf_size, int hist_bits,
351+
uint8_t * dict_buf, int dict_file_size)
311352
{
312353
int check = ISAL_DECOMP_OK;
313354
uint64_t inbuf_remaining;
@@ -317,6 +358,9 @@ int isal_inflate_stateful_round(struct inflate_state *state, uint8_t * inbuf,
317358
state->next_out = outbuf;
318359
state->avail_out = outbuf_size;
319360
state->hist_bits = hist_bits;
361+
if (dict_file_size != 0)
362+
isal_inflate_set_dict(state, dict_buf, dict_file_size);
363+
320364
inbuf_remaining = inbuf_size;
321365

322366
while (ISAL_DECOMP_OK == check && inbuf_remaining >= in_block_size) {
@@ -414,6 +458,34 @@ int isal_deflate_perf(uint8_t * outbuf, uint64_t * outbuf_size, uint8_t * inbuf,
414458
return check;
415459
}
416460

461+
int isal_deflate_dict_perf(uint8_t * outbuf, uint64_t * outbuf_size, uint8_t * inbuf,
462+
uint64_t inbuf_size, int level, int flush_type, int hist_bits,
463+
int time, struct perf *start, uint8_t * dict_buf,
464+
int dict_file_size)
465+
{
466+
struct isal_zstream stream;
467+
struct isal_dict dict_str;
468+
uint8_t *level_buf = NULL;
469+
int check;
470+
471+
if (level_size_buf[level] > 0) {
472+
level_buf = malloc(level_size_buf[level]);
473+
if (level_buf == NULL)
474+
return 1;
475+
}
476+
477+
stream.level = level;
478+
isal_deflate_process_dict(&stream, &dict_str, dict_buf, dict_file_size);
479+
480+
BENCHMARK(start, time, check =
481+
isal_deflate_dict_round(&stream, outbuf, *outbuf_size, inbuf,
482+
inbuf_size, level, level_buf,
483+
level_size_buf[level], flush_type, hist_bits,
484+
&dict_str));
485+
*outbuf_size = stream.total_out;
486+
return check;
487+
}
488+
417489
int isal_deflate_stateful_perf(uint8_t * outbuf, uint64_t * outbuf_size, uint8_t * inbuf,
418490
uint64_t inbuf_size, int level, int flush_type,
419491
uint64_t in_block_size, int hist_bits, int time,
@@ -500,21 +572,23 @@ int isal_inflate_perf(uint8_t * inbuf, uint64_t inbuf_size, uint8_t * outbuf,
500572
int isal_inflate_stateful_perf(uint8_t * inbuf, uint64_t inbuf_size, uint8_t * outbuf,
501573
uint64_t outbuf_size, uint8_t * filebuf, uint64_t file_size,
502574
uint64_t in_block_size, int hist_bits, int time,
503-
struct perf *start)
575+
struct perf *start, uint8_t * dict_buf, int dict_file_size)
504576
{
505577
struct inflate_state state;
506578
int check;
507579

508580
if (in_block_size == 0)
509581
in_block_size = inbuf_size;
510582

511-
check = isal_inflate_round(&state, inbuf, inbuf_size, outbuf, outbuf_size, hist_bits);
583+
check = isal_inflate_stateful_round(&state, inbuf, inbuf_size, in_block_size, outbuf,
584+
outbuf_size, hist_bits, dict_buf, dict_file_size);
512585
if (check || state.total_out != file_size || memcmp(outbuf, filebuf, file_size))
513586
return 1;
514587

515588
BENCHMARK(start, time,
516589
isal_inflate_stateful_round(&state, inbuf, inbuf_size, in_block_size, outbuf,
517-
outbuf_size, hist_bits));
590+
outbuf_size, hist_bits, dict_buf,
591+
dict_file_size));
518592

519593
return 0;
520594

@@ -555,9 +629,12 @@ int zlib_inflate_perf(uint8_t * inbuf, uint64_t inbuf_size, uint8_t * outbuf,
555629
int main(int argc, char *argv[])
556630
{
557631
FILE *in = NULL;
632+
FILE *dict_fn = NULL;
558633
unsigned char *compressbuf, *decompbuf, *filebuf;
559634
char *outfile = NULL;
560635
int i, c, ret = 0;
636+
int dict_file_size = 0;
637+
uint8_t *dict_buf = NULL;
561638
uint64_t decompbuf_size, compressbuf_size;
562639
uint64_t block_count;
563640

@@ -628,6 +705,24 @@ int main(int argc, char *argv[])
628705
if (info.deflate_time < 0)
629706
usage();
630707
break;
708+
case 'D':
709+
dict_fn = fopen(optarg, "rb");
710+
if (!dict_fn) {
711+
printf("Can't open dictionary for reading\n");
712+
exit(0);
713+
}
714+
dict_file_size = get_filesize(dict_fn);
715+
dict_buf = malloc(dict_file_size);
716+
if (dict_buf == NULL || dict_file_size == 0) {
717+
printf("Can't allocate mem for dictionary buffer\n");
718+
exit(0);
719+
}
720+
if (dict_file_size != fread(dict_buf, 1, dict_file_size, dict_fn)) {
721+
printf("Couldn't read all of dictionary file\n");
722+
exit(0);
723+
}
724+
fclose(dict_fn);
725+
break;
631726
case 's':
632727
inflate_strat.stateful = 1;
633728
break;
@@ -745,26 +840,34 @@ int main(int argc, char *argv[])
745840

746841
info.deflate_size = compressbuf_size;
747842

748-
if (info.strategy.mode == ISAL_STATELESS)
843+
if (dict_file_size != 0) {
844+
info.strategy.mode = ISAL_WITH_DICTIONARY;
845+
ret = isal_deflate_dict_perf(compressbuf, &info.deflate_size,
846+
filebuf, info.file_size,
847+
compression_queue[i].level,
848+
info.flush_type, info.hist_bits,
849+
info.deflate_time, &info.start,
850+
dict_buf, dict_file_size);
851+
} else if (info.strategy.mode == ISAL_STATELESS)
749852
ret = isal_deflate_perf(compressbuf, &info.deflate_size, filebuf,
750-
info.file_size, compression_queue[i].level,
853+
info.file_size,
854+
compression_queue[i].level,
751855
info.flush_type, info.hist_bits,
752856
info.deflate_time, &info.start);
857+
753858
else if (info.strategy.mode == ISAL_STATEFUL)
754-
ret =
755-
isal_deflate_stateful_perf(compressbuf, &info.deflate_size,
756-
filebuf, info.file_size,
757-
compression_queue[i].level,
758-
info.flush_type, info.inblock_size,
759-
info.hist_bits, info.deflate_time,
760-
&info.start);
859+
ret = isal_deflate_stateful_perf(compressbuf, &info.deflate_size,
860+
filebuf, info.file_size,
861+
compression_queue[i].level,
862+
info.flush_type, info.inblock_size,
863+
info.hist_bits, info.deflate_time,
864+
&info.start);
761865
else if (info.strategy.mode == ZLIB)
762866
ret = zlib_deflate_perf(compressbuf, &info.deflate_size, filebuf,
763867
info.file_size, compression_queue[i].level,
764868
info.flush_type, info.inblock_size,
765869
info.hist_bits, info.deflate_time,
766870
&info.start);
767-
768871
if (ret) {
769872
printf(" Error in compression\n");
770873
continue;
@@ -786,6 +889,9 @@ int main(int argc, char *argv[])
786889
continue;
787890

788891
if (inflate_strat.stateless) {
892+
if (dict_file_size != 0)
893+
continue;
894+
789895
info.inflate_mode = ISAL_STATELESS;
790896
ret = isal_inflate_perf(compressbuf, info.deflate_size, decompbuf,
791897
decompbuf_size, filebuf, info.file_size,
@@ -798,13 +904,15 @@ int main(int argc, char *argv[])
798904
}
799905

800906
if (inflate_strat.stateful) {
801-
info.inflate_mode = ISAL_STATEFUL;
802-
ret =
803-
isal_inflate_stateful_perf(compressbuf, info.deflate_size,
804-
decompbuf, decompbuf_size, filebuf,
805-
info.file_size, info.inblock_size,
806-
info.hist_bits, info.inflate_time,
807-
&info.start);
907+
info.inflate_mode =
908+
(dict_file_size == 0) ? ISAL_STATEFUL : ISAL_WITH_DICTIONARY;
909+
910+
ret = isal_inflate_stateful_perf(compressbuf, info.deflate_size,
911+
decompbuf, decompbuf_size, filebuf,
912+
info.file_size, info.inblock_size,
913+
info.hist_bits, info.inflate_time,
914+
&info.start, dict_buf,
915+
dict_file_size);
808916

809917
if (ret)
810918
printf(" Error in isal stateful inflate\n");
@@ -828,5 +936,7 @@ int main(int argc, char *argv[])
828936
free(compressbuf);
829937
free(decompbuf);
830938
free(filebuf);
939+
if (dict_buf != NULL)
940+
free(dict_buf);
831941
return 0;
832942
}

0 commit comments

Comments
 (0)