]>
Commit | Line | Data |
---|---|---|
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> | |
224ce89b WB |
33 | #include <string.h> |
34 | #include <getopt.h> | |
7c673cae FG |
35 | #include "igzip_lib.h" |
36 | #include "test.h" | |
37 | ||
38 | #define BUF_SIZE 1024 | |
39 | #define MIN_TEST_LOOPS 10 | |
40 | #ifndef RUN_MEM_SIZE | |
224ce89b | 41 | # define RUN_MEM_SIZE 500000000 |
7c673cae FG |
42 | #endif |
43 | ||
44 | struct isal_zstream stream; | |
45 | ||
224ce89b WB |
46 | int usage(void) |
47 | { | |
48 | fprintf(stderr, | |
49 | "Usage: igzip_stateless_file_perf [options] <infile>\n" | |
50 | " -h help\n" | |
51 | " -X use compression level X with 0 <= X <= 1\n" | |
52 | " -i <iter> number of iterations (at least 1)\n" | |
53 | " -o <file> output file for compresed data\n"); | |
54 | exit(0); | |
55 | } | |
56 | ||
7c673cae FG |
57 | int get_filesize(FILE * f) |
58 | { | |
59 | int curr, end; | |
60 | ||
61 | curr = ftell(f); /* Save current position */ | |
62 | fseek(f, 0L, SEEK_END); | |
63 | end = ftell(f); | |
64 | fseek(f, curr, SEEK_SET); /* Restore position */ | |
65 | return end; | |
66 | } | |
67 | ||
68 | int main(int argc, char *argv[]) | |
69 | { | |
70 | FILE *in, *out = NULL; | |
224ce89b WB |
71 | unsigned char *inbuf, *outbuf, *level_buf = NULL; |
72 | int i, c, infile_size, iterations = 0, outbuf_size; | |
73 | struct isal_huff_histogram histogram; | |
74 | struct isal_hufftables hufftables_custom; | |
75 | int level = 0, level_size = 0; | |
76 | char *in_file_name = NULL, *out_file_name = NULL; | |
77 | ||
78 | while ((c = getopt(argc, argv, "h01i:o:")) != -1) { | |
79 | switch (c) { | |
80 | case 'o': | |
81 | out_file_name = optarg; | |
82 | break; | |
83 | case 'i': | |
84 | iterations = atoi(optarg); | |
85 | if (iterations < 1) | |
86 | usage(); | |
87 | break; | |
88 | case '1': | |
89 | level = 1; | |
90 | level_size = ISAL_DEF_LVL1_LARGE; | |
91 | break; | |
92 | case '0': | |
93 | break; | |
94 | case 'h': | |
95 | default: | |
96 | usage(); | |
97 | break; | |
98 | } | |
7c673cae | 99 | } |
224ce89b WB |
100 | |
101 | if (optind < argc) { | |
102 | in_file_name = argv[optind]; | |
103 | in = fopen(in_file_name, "rb"); | |
104 | } else | |
105 | usage(); | |
106 | ||
7c673cae | 107 | if (!in) { |
224ce89b | 108 | fprintf(stderr, "Can't open %s for reading\n", in_file_name); |
7c673cae FG |
109 | exit(0); |
110 | } | |
224ce89b WB |
111 | if (out_file_name != NULL) { |
112 | out = fopen(out_file_name, "wb"); | |
7c673cae | 113 | if (!out) { |
224ce89b | 114 | fprintf(stderr, "Can't open %s for writing\n", out_file_name); |
7c673cae FG |
115 | exit(0); |
116 | } | |
224ce89b | 117 | printf("outfile=%s\n", out_file_name); |
7c673cae | 118 | } |
224ce89b WB |
119 | |
120 | printf("Window Size: %d K\n", IGZIP_HIST_SIZE / 1024); | |
7c673cae FG |
121 | printf("igzip_file_perf: \n"); |
122 | fflush(0); | |
123 | /* Allocate space for entire input file and output | |
124 | * (assuming some possible expansion on output size) | |
125 | */ | |
126 | infile_size = get_filesize(in); | |
127 | ||
224ce89b WB |
128 | outbuf_size = infile_size * 1.07 + BUF_SIZE; |
129 | ||
130 | if (iterations == 0) { | |
131 | iterations = infile_size ? RUN_MEM_SIZE / infile_size : MIN_TEST_LOOPS; | |
132 | if (iterations < MIN_TEST_LOOPS) | |
133 | iterations = MIN_TEST_LOOPS; | |
7c673cae | 134 | } |
7c673cae FG |
135 | |
136 | inbuf = malloc(infile_size); | |
137 | if (inbuf == NULL) { | |
138 | fprintf(stderr, "Can't allocate input buffer memory\n"); | |
139 | exit(0); | |
140 | } | |
141 | outbuf = malloc(outbuf_size); | |
142 | if (outbuf == NULL) { | |
143 | fprintf(stderr, "Can't allocate output buffer memory\n"); | |
144 | exit(0); | |
145 | } | |
146 | ||
224ce89b WB |
147 | if (level_size != 0) { |
148 | level_buf = malloc(level_size); | |
149 | if (level_buf == NULL) { | |
150 | fprintf(stderr, "Can't allocate level buffer memory\n"); | |
151 | exit(0); | |
152 | } | |
153 | } | |
154 | ||
155 | printf("igzip_file_perf: %s %d iterations\n", in_file_name, iterations); | |
7c673cae FG |
156 | /* Read complete input file into buffer */ |
157 | stream.avail_in = (uint32_t) fread(inbuf, 1, infile_size, in); | |
158 | if (stream.avail_in != infile_size) { | |
159 | fprintf(stderr, "Couldn't fit all of input file into buffer\n"); | |
160 | exit(0); | |
161 | } | |
162 | ||
163 | struct perf start, stop; | |
164 | perf_start(&start); | |
165 | ||
166 | for (i = 0; i < iterations; i++) { | |
167 | isal_deflate_init(&stream); | |
168 | stream.end_of_stream = 1; /* Do the entire file at once */ | |
169 | stream.flush = NO_FLUSH; | |
170 | stream.next_in = inbuf; | |
171 | stream.avail_in = infile_size; | |
172 | stream.next_out = outbuf; | |
173 | stream.avail_out = outbuf_size; | |
224ce89b WB |
174 | stream.level = level; |
175 | stream.level_buf = level_buf; | |
176 | stream.level_buf_size = level_size; | |
7c673cae FG |
177 | isal_deflate_stateless(&stream); |
178 | if (stream.avail_in != 0) | |
179 | break; | |
180 | } | |
181 | perf_stop(&stop); | |
182 | ||
183 | if (stream.avail_in != 0) { | |
184 | fprintf(stderr, "Could not compress all of inbuf\n"); | |
185 | exit(0); | |
186 | } | |
187 | ||
224ce89b | 188 | printf(" file %s - in_size=%d out_size=%d iter=%d ratio=%3.1f%%", in_file_name, |
7c673cae FG |
189 | infile_size, stream.total_out, i, 100.0 * stream.total_out / infile_size); |
190 | ||
224ce89b WB |
191 | if (level == 0) { |
192 | memset(&histogram, 0, sizeof(histogram)); | |
193 | ||
194 | isal_update_histogram(inbuf, infile_size, &histogram); | |
195 | isal_create_hufftables(&hufftables_custom, &histogram); | |
196 | ||
197 | isal_deflate_init(&stream); | |
198 | stream.end_of_stream = 1; /* Do the entire file at once */ | |
199 | stream.flush = NO_FLUSH; | |
200 | stream.next_in = inbuf; | |
201 | stream.avail_in = infile_size; | |
202 | stream.next_out = outbuf; | |
203 | stream.avail_out = outbuf_size; | |
204 | stream.level = level; | |
205 | stream.level_buf = level_buf; | |
206 | stream.level_buf_size = level_size; | |
207 | stream.hufftables = &hufftables_custom; | |
208 | isal_deflate_stateless(&stream); | |
209 | ||
210 | printf(" ratio_custom=%3.1f%%", 100.0 * stream.total_out / infile_size); | |
211 | } | |
212 | printf("\n"); | |
213 | ||
7c673cae FG |
214 | printf("igzip_file: "); |
215 | perf_print(stop, start, (long long)infile_size * i); | |
216 | ||
217 | if (argc > 2 && out) { | |
224ce89b | 218 | printf("writing %s\n", out_file_name); |
7c673cae FG |
219 | fwrite(outbuf, 1, stream.total_out, out); |
220 | fclose(out); | |
221 | } | |
222 | ||
223 | fclose(in); | |
224 | printf("End of igzip_file_perf\n\n"); | |
225 | fflush(0); | |
226 | return 0; | |
227 | } |