]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/igzip_inflate_perf.c
update sources to v12.1.1
[ceph.git] / ceph / src / isa-l / igzip / igzip_inflate_perf.c
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 <assert.h>
32 #include "huff_codes.h"
33 #include "igzip_lib.h"
34 #include "test.h"
35
36 #if defined(ZLIB_9) || defined(ZLIB_1) || defined(ZLIB_COMPARE)
37 # include <zlib.h>
38 #endif
39
40 #define BUF_SIZE 1024
41 #define MIN_TEST_LOOPS 8
42 #ifndef RUN_MEM_SIZE
43 # define RUN_MEM_SIZE 1000000000
44 #endif
45
46 int get_filesize(FILE * f)
47 {
48 int curr, end;
49
50 curr = ftell(f); /* Save current position */
51 fseek(f, 0L, SEEK_END);
52 end = ftell(f);
53 fseek(f, curr, SEEK_SET); /* Restore position */
54 return end;
55 }
56
57 int main(int argc, char *argv[])
58 {
59 FILE *in, *out = NULL;
60 unsigned char *inbuf, *outbuf, *tempbuf;
61 int i, infile_size, iterations, outbuf_size, check;
62 uint64_t inbuf_size;
63 struct inflate_state state;
64
65 if (argc > 3 || argc < 2) {
66 fprintf(stderr, "Usage: isal_inflate_file_perf infile\n"
67 "\t - Runs multiple iterations of igzip on a file to "
68 "get more accurate time results.\n");
69 exit(0);
70 }
71 in = fopen(argv[1], "rb");
72 if (!in) {
73 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
74 exit(0);
75 }
76 if (argc > 2) {
77 out = fopen(argv[2], "wb");
78 if (!out) {
79 fprintf(stderr, "Can't open %s for writing\n", argv[2]);
80 exit(0);
81 }
82 printf("outfile=%s\n", argv[2]);
83 }
84 printf("isal_inflate_perf: \n");
85 fflush(0);
86 /* Allocate space for entire input file and output
87 * (assuming some possible expansion on output size)
88 */
89 infile_size = get_filesize(in);
90
91 if (infile_size != 0) {
92 outbuf_size = infile_size;
93 iterations = RUN_MEM_SIZE / infile_size;
94 } else {
95 printf("Error: input file has 0 size\n");
96 exit(0);
97 }
98 if (iterations < MIN_TEST_LOOPS)
99 iterations = MIN_TEST_LOOPS;
100
101 tempbuf = malloc(infile_size);
102 if (tempbuf == NULL) {
103 fprintf(stderr, "Can't allocate temp buffer memory\n");
104 exit(0);
105 }
106 inbuf_size = 2 * infile_size;
107 inbuf = malloc(inbuf_size);
108 if (inbuf == NULL) {
109 fprintf(stderr, "Can't allocate input buffer memory\n");
110 exit(0);
111 }
112 outbuf = malloc(outbuf_size);
113 if (outbuf == NULL) {
114 fprintf(stderr, "Can't allocate output buffer memory\n");
115 exit(0);
116 }
117
118 fread(tempbuf, 1, infile_size, in);
119
120 #ifdef ZLIB_9
121 printf("Using zlib -9 compression\n");
122 i = compress2(inbuf, &inbuf_size, tempbuf, infile_size, 9);
123 if (i != Z_OK) {
124 printf("Compression of input file failed\n");
125 exit(0);
126 }
127
128 inbuf += 2;
129 inbuf_size -= 2;
130 #elif defined(ZLIB_1)
131 printf("Using zlib -1 compression\n");
132 i = compress2(inbuf, &inbuf_size, tempbuf, infile_size, 1);
133 if (i != Z_OK) {
134 printf("Compression of input file failed\n");
135 exit(0);
136 }
137
138 inbuf += 2;
139 inbuf_size -= 2;
140 #else
141 printf("Using igzip compression\n");
142 struct isal_zstream stream;
143 isal_deflate_init(&stream);
144 stream.end_of_stream = 1; /* Do the entire file at once */
145 stream.flush = NO_FLUSH;
146 stream.next_in = tempbuf;
147 stream.avail_in = infile_size;
148 stream.next_out = inbuf;
149 stream.avail_out = inbuf_size;
150 #ifdef IGZIP_CUSTOM
151 struct isal_huff_histogram histogram;
152 struct isal_hufftables hufftables_custom;
153
154 memset(&histogram, 0, sizeof(histogram));
155 isal_update_histogram(tempbuf, infile_size, &histogram);
156 isal_create_hufftables(&hufftables_custom, &histogram);
157 stream.hufftables = &hufftables_custom;
158 #endif
159 isal_deflate(&stream);
160 if (stream.avail_in != 0) {
161 printf("Compression of input file failed\n");
162 exit(0);
163 }
164 #endif
165
166 #ifdef ZLIB_COMPARE
167 {
168 printf("igzip_zlib_inflate_perf: %s %d iterations\n", argv[1], iterations);
169 /* Read complete input file into buffer */
170
171 struct perf start, stop;
172 perf_start(&start);
173
174 z_stream gstream;
175
176 for (i = 0; i < iterations; i++) {
177 gstream.next_in = inbuf;
178 gstream.avail_in = inbuf_size;
179 gstream.zalloc = Z_NULL;
180 gstream.zfree = Z_NULL;
181 gstream.opaque = Z_NULL;
182 if (0 != inflateInit2(&gstream, -15)) {
183 printf("Fail zlib init\n");
184 exit(-1);
185 }
186 gstream.next_out = outbuf;
187 gstream.avail_out = outbuf_size;
188 check = inflate(&gstream, Z_FINISH);
189 if (check != 1) {
190 printf("Error in decompression with error %d\n", check);
191 break;
192 }
193 }
194 perf_stop(&stop);
195 printf(" file %s - in_size=%d out_size=%lu iter=%d\n", argv[1],
196 infile_size, gstream.total_out, i);
197
198 printf("igzip_file: ");
199 perf_print(stop, start, (long long)infile_size * i);
200
201 printf("End of igzip_zlib_inflate_perf\n\n");
202 }
203 #endif
204 printf("isal_inflate_stateless_perf: %s %d iterations\n", argv[1], iterations);
205 /* Read complete input file into buffer */
206 fclose(in);
207 struct perf start, stop;
208 perf_start(&start);
209
210 for (i = 0; i < iterations; i++) {
211 isal_inflate_init(&state);
212 state.next_in = inbuf;
213 state.avail_in = inbuf_size;
214 state.next_out = outbuf;
215 state.avail_out = outbuf_size;
216
217 check = isal_inflate(&state);
218 if (check) {
219 printf("Error in decompression with error %d\n", check);
220 break;
221 }
222 }
223 perf_stop(&stop);
224 printf(" file %s - in_size=%d out_size=%d iter=%d\n", argv[1],
225 infile_size, state.total_out, i);
226
227 printf("igzip_file: ");
228 perf_print(stop, start, (long long)infile_size * i);
229
230 printf("End of isal_inflate_stateless_perf\n\n");
231 fflush(0);
232
233 #if defined(ZLIB_1) || defined(ZLIB_9)
234 inbuf -= 2;
235 #endif
236 free(inbuf);
237 free(outbuf);
238 free(tempbuf);
239
240 return 0;
241 }