40
40
41
41
#define BUF_SIZE 1024
42
42
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: "
44
44
45
45
#define COMPRESSION_QUEUE_LIMIT 32
46
46
#define UNSET -1
@@ -109,6 +109,7 @@ int level_size_buf[10] = {
109
109
enum {
110
110
ISAL_STATELESS ,
111
111
ISAL_STATEFUL ,
112
+ ISAL_WITH_DICTIONARY ,
112
113
ZLIB
113
114
};
114
115
@@ -161,6 +162,7 @@ int usage(void)
161
162
" -s performance test isa-l stateful inflate\n"
162
163
" -t performance test isa-l stateless inflate\n"
163
164
" -u performance test zlib inflate\n"
165
+ " -D <file> use dictionary file\n"
164
166
" -o <file> output file to store compressed data (last one if multiple)\n"
165
167
" -b <size> input buffer size, applies to stateful options (-f,-z,-s)\n"
166
168
" -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)
187
189
printf (" isal_stateless_deflate-> " );
188
190
else if (info -> strategy .mode == ISAL_STATEFUL )
189
191
printf (" isal_stateful_deflate-> " );
192
+ else if (info -> strategy .mode == ISAL_WITH_DICTIONARY )
193
+ printf (" isal_dictionary_deflate-> " );
190
194
else if (info -> strategy .mode == ZLIB )
191
195
printf (" zlib_deflate-> " );
192
196
@@ -199,6 +203,8 @@ void print_inflate_perf_line(struct perf_info *info)
199
203
printf (" isal_stateless_inflate-> " );
200
204
else if (info -> inflate_mode == ISAL_STATEFUL )
201
205
printf (" isal_stateful_inflate-> " );
206
+ else if (info -> inflate_mode == ISAL_WITH_DICTIONARY )
207
+ printf (" isal_dictionary_inflate-> " );
202
208
else if (info -> inflate_mode == ZLIB )
203
209
printf (" zlib_inflate-> " );
204
210
@@ -235,6 +241,40 @@ int isal_deflate_round(struct isal_zstream *stream, uint8_t * outbuf, uint32_t o
235
241
return 0 ;
236
242
}
237
243
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
+
238
278
int isal_inflate_round (struct inflate_state * state , uint8_t * inbuf , uint32_t inbuf_size ,
239
279
uint8_t * outbuf , uint32_t outbuf_size , int hist_bits )
240
280
{
@@ -307,7 +347,8 @@ int isal_deflate_stateful_round(struct isal_zstream *stream, uint8_t * outbuf,
307
347
308
348
int isal_inflate_stateful_round (struct inflate_state * state , uint8_t * inbuf ,
309
349
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 )
311
352
{
312
353
int check = ISAL_DECOMP_OK ;
313
354
uint64_t inbuf_remaining ;
@@ -317,6 +358,9 @@ int isal_inflate_stateful_round(struct inflate_state *state, uint8_t * inbuf,
317
358
state -> next_out = outbuf ;
318
359
state -> avail_out = outbuf_size ;
319
360
state -> hist_bits = hist_bits ;
361
+ if (dict_file_size != 0 )
362
+ isal_inflate_set_dict (state , dict_buf , dict_file_size );
363
+
320
364
inbuf_remaining = inbuf_size ;
321
365
322
366
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,
414
458
return check ;
415
459
}
416
460
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
+
417
489
int isal_deflate_stateful_perf (uint8_t * outbuf , uint64_t * outbuf_size , uint8_t * inbuf ,
418
490
uint64_t inbuf_size , int level , int flush_type ,
419
491
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,
500
572
int isal_inflate_stateful_perf (uint8_t * inbuf , uint64_t inbuf_size , uint8_t * outbuf ,
501
573
uint64_t outbuf_size , uint8_t * filebuf , uint64_t file_size ,
502
574
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 )
504
576
{
505
577
struct inflate_state state ;
506
578
int check ;
507
579
508
580
if (in_block_size == 0 )
509
581
in_block_size = inbuf_size ;
510
582
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 );
512
585
if (check || state .total_out != file_size || memcmp (outbuf , filebuf , file_size ))
513
586
return 1 ;
514
587
515
588
BENCHMARK (start , time ,
516
589
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 ));
518
592
519
593
return 0 ;
520
594
@@ -555,9 +629,12 @@ int zlib_inflate_perf(uint8_t * inbuf, uint64_t inbuf_size, uint8_t * outbuf,
555
629
int main (int argc , char * argv [])
556
630
{
557
631
FILE * in = NULL ;
632
+ FILE * dict_fn = NULL ;
558
633
unsigned char * compressbuf , * decompbuf , * filebuf ;
559
634
char * outfile = NULL ;
560
635
int i , c , ret = 0 ;
636
+ int dict_file_size = 0 ;
637
+ uint8_t * dict_buf = NULL ;
561
638
uint64_t decompbuf_size , compressbuf_size ;
562
639
uint64_t block_count ;
563
640
@@ -628,6 +705,24 @@ int main(int argc, char *argv[])
628
705
if (info .deflate_time < 0 )
629
706
usage ();
630
707
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 ;
631
726
case 's' :
632
727
inflate_strat .stateful = 1 ;
633
728
break ;
@@ -745,26 +840,34 @@ int main(int argc, char *argv[])
745
840
746
841
info .deflate_size = compressbuf_size ;
747
842
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 )
749
852
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 ,
751
855
info .flush_type , info .hist_bits ,
752
856
info .deflate_time , & info .start );
857
+
753
858
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 );
761
865
else if (info .strategy .mode == ZLIB )
762
866
ret = zlib_deflate_perf (compressbuf , & info .deflate_size , filebuf ,
763
867
info .file_size , compression_queue [i ].level ,
764
868
info .flush_type , info .inblock_size ,
765
869
info .hist_bits , info .deflate_time ,
766
870
& info .start );
767
-
768
871
if (ret ) {
769
872
printf (" Error in compression\n" );
770
873
continue ;
@@ -786,6 +889,9 @@ int main(int argc, char *argv[])
786
889
continue ;
787
890
788
891
if (inflate_strat .stateless ) {
892
+ if (dict_file_size != 0 )
893
+ continue ;
894
+
789
895
info .inflate_mode = ISAL_STATELESS ;
790
896
ret = isal_inflate_perf (compressbuf , info .deflate_size , decompbuf ,
791
897
decompbuf_size , filebuf , info .file_size ,
@@ -798,13 +904,15 @@ int main(int argc, char *argv[])
798
904
}
799
905
800
906
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 );
808
916
809
917
if (ret )
810
918
printf (" Error in isal stateful inflate\n" );
@@ -828,5 +936,7 @@ int main(int argc, char *argv[])
828
936
free (compressbuf );
829
937
free (decompbuf );
830
938
free (filebuf );
939
+ if (dict_buf != NULL )
940
+ free (dict_buf );
831
941
return 0 ;
832
942
}
0 commit comments