]> git.proxmox.com Git - ceph.git/blame - ceph/src/isa-l/igzip/igzip_file_perf.c
update sources to v12.1.1
[ceph.git] / ceph / src / isa-l / igzip / igzip_file_perf.c
CommitLineData
7c673cae
FG
1/**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28**********************************************************************/
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <assert.h>
33#include <string.h>
224ce89b 34#include <getopt.h>
7c673cae
FG
35#include "igzip_lib.h"
36#include "test.h"
37
38#define BUF_SIZE 1024
224ce89b 39#define MIN_TEST_LOOPS 10
7c673cae
FG
40#ifndef RUN_MEM_SIZE
41# define RUN_MEM_SIZE 500000000
42#endif
43
44struct isal_zstream stream;
45
224ce89b
WB
46int usage(void)
47{
48 fprintf(stderr,
49 "Usage: igzip_file_perf [options] <infile>\n"
50 " -h help\n"
51 " -X use compression level X with 0 <= X <= 1\n"
52 " -b <size> input buffer size, 0 buffers all the input\n"
53 " -i <iter> number of iterations (at least 1)\n"
54 " -o <file> output file for compresed data\n");
55 exit(0);
56}
57
7c673cae
FG
58int get_filesize(FILE * f)
59{
60 int curr, end;
61
62 curr = ftell(f); /* Save current position */
63 fseek(f, 0L, SEEK_END);
64 end = ftell(f);
65 fseek(f, curr, SEEK_SET); /* Restore position */
66 return end;
67}
68
69int main(int argc, char *argv[])
70{
71 FILE *in, *out = NULL;
224ce89b
WB
72 unsigned char *inbuf, *outbuf, *level_buf = NULL;
73 int i, c, infile_size, iterations = 0, outbuf_size, inbuf_size = 0;
7c673cae
FG
74 struct isal_huff_histogram histogram;
75 struct isal_hufftables hufftables_custom;
224ce89b
WB
76 int level = 0, level_size = 0, avail_in;
77 char *in_file_name = NULL, *out_file_name = NULL;
7c673cae 78
224ce89b
WB
79 while ((c = getopt(argc, argv, "h01i:b:o:")) != -1) {
80 switch (c) {
81 case 'o':
82 out_file_name = optarg;
83 break;
84 case 'i':
85 iterations = atoi(optarg);
86 if (iterations < 1)
87 usage();
88 break;
89 case 'b':
90 inbuf_size = atoi(optarg);
91 break;
92 case '1':
93 level = 1;
94 level_size = ISAL_DEF_LVL1_LARGE;
95 break;
96 case '0':
97 break;
98 case 'h':
99 default:
100 usage();
101 break;
102 }
7c673cae 103 }
224ce89b
WB
104
105 if (optind < argc) {
106 in_file_name = argv[optind];
107 in = fopen(in_file_name, "rb");
108 } else
109 usage();
110
7c673cae 111 if (!in) {
224ce89b 112 fprintf(stderr, "Can't open %s for reading\n", in_file_name);
7c673cae
FG
113 exit(0);
114 }
224ce89b
WB
115 if (out_file_name != NULL) {
116 out = fopen(out_file_name, "wb");
7c673cae 117 if (!out) {
224ce89b 118 fprintf(stderr, "Can't open %s for writing\n", out_file_name);
7c673cae
FG
119 exit(0);
120 }
224ce89b 121 printf("outfile=%s\n", out_file_name);
7c673cae 122 }
224ce89b
WB
123
124 printf("Window Size: %d K\n", IGZIP_HIST_SIZE / 1024);
7c673cae
FG
125 printf("igzip_file_perf: \n");
126 fflush(0);
224ce89b 127
7c673cae
FG
128 /* Allocate space for entire input file and output
129 * (assuming some possible expansion on output size)
130 */
131 infile_size = get_filesize(in);
132
224ce89b
WB
133 outbuf_size = 2 * infile_size + BUF_SIZE;
134
135 if (iterations == 0) {
136 iterations = infile_size ? RUN_MEM_SIZE / infile_size : MIN_TEST_LOOPS;
137 if (iterations < MIN_TEST_LOOPS)
138 iterations = MIN_TEST_LOOPS;
7c673cae 139 }
7c673cae
FG
140
141 inbuf = malloc(infile_size);
142 if (inbuf == NULL) {
143 fprintf(stderr, "Can't allocate input buffer memory\n");
144 exit(0);
145 }
146 outbuf = malloc(outbuf_size);
147 if (outbuf == NULL) {
148 fprintf(stderr, "Can't allocate output buffer memory\n");
149 exit(0);
150 }
151
224ce89b
WB
152 if (level_size != 0) {
153 level_buf = malloc(level_size);
154 if (level_buf == NULL) {
155 fprintf(stderr, "Can't allocate level buffer memory\n");
156 exit(0);
157 }
158 }
159
160 inbuf_size = inbuf_size ? inbuf_size : infile_size;
161
162 printf("igzip_file_perf: %s %d iterations\n", in_file_name, iterations);
7c673cae
FG
163 /* Read complete input file into buffer */
164 stream.avail_in = (uint32_t) fread(inbuf, 1, infile_size, in);
165 if (stream.avail_in != infile_size) {
166 fprintf(stderr, "Couldn't fit all of input file into buffer\n");
167 exit(0);
168 }
169
170 struct perf start, stop;
171 perf_start(&start);
172
173 for (i = 0; i < iterations; i++) {
174 isal_deflate_init(&stream);
224ce89b 175 stream.end_of_stream = 0;
7c673cae 176 stream.flush = NO_FLUSH;
224ce89b
WB
177 stream.level = level;
178 stream.level_buf = level_buf;
179 stream.level_buf_size = level_size;
7c673cae
FG
180 stream.next_out = outbuf;
181 stream.avail_out = outbuf_size;
224ce89b
WB
182 stream.next_in = inbuf;
183 avail_in = infile_size;
184
185 while (avail_in > 0) {
186 stream.avail_in = avail_in >= inbuf_size ? inbuf_size : avail_in;
187 avail_in -= inbuf_size;
188
189 if (avail_in <= 0)
190 stream.end_of_stream = 1;
191
192 isal_deflate(&stream);
193
194 if (stream.avail_in != 0)
195 break;
196 }
7c673cae 197 }
224ce89b 198
7c673cae
FG
199 perf_stop(&stop);
200
201 if (stream.avail_in != 0) {
202 fprintf(stderr, "Could not compress all of inbuf\n");
203 exit(0);
204 }
205
224ce89b
WB
206 printf(" file %s - in_size=%d out_size=%d iter=%d ratio=%3.1f%%",
207 in_file_name, infile_size, stream.total_out, i,
208 100.0 * stream.total_out / infile_size);
7c673cae 209
224ce89b
WB
210 if (level == 0) {
211 memset(&histogram, 0, sizeof(histogram));
7c673cae 212
224ce89b
WB
213 isal_update_histogram(inbuf, infile_size, &histogram);
214 isal_create_hufftables(&hufftables_custom, &histogram);
7c673cae 215
224ce89b
WB
216 isal_deflate_init(&stream);
217 stream.end_of_stream = 0;
218 stream.flush = NO_FLUSH;
219 stream.level = level;
220 stream.level_buf = level_buf;
221 stream.level_buf_size = level_size;
222 stream.next_out = outbuf;
223 stream.avail_out = outbuf_size;
224 stream.next_in = inbuf;
225 stream.hufftables = &hufftables_custom;
226 avail_in = infile_size;
227
228 while (avail_in > 0) {
229 stream.avail_in = avail_in >= inbuf_size ? inbuf_size : avail_in;
230 avail_in -= inbuf_size;
231
232 if (avail_in <= 0)
233 stream.end_of_stream = 1;
234
235 isal_deflate(&stream);
236
237 if (stream.avail_in != 0)
238 break;
239 }
240
241 printf(" ratio_custom=%3.1f%%", 100.0 * stream.total_out / infile_size);
242 }
243 printf("\n");
7c673cae
FG
244
245 if (stream.avail_in != 0) {
246 fprintf(stderr, "Could not compress all of inbuf\n");
247 exit(0);
248 }
249
250 printf("igzip_file: ");
251 perf_print(stop, start, (long long)infile_size * i);
252
253 if (argc > 2 && out) {
224ce89b 254 printf("writing %s\n", out_file_name);
7c673cae
FG
255 fwrite(outbuf, 1, stream.total_out, out);
256 fclose(out);
257 }
258
259 fclose(in);
260 printf("End of igzip_file_perf\n\n");
261 fflush(0);
262 return 0;
263}