xref: /isa-l/igzip/igzip_file_perf.c (revision f97de75fc6bc2f96ce148e81f89d01fd135ccd9d)
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>
34 #include "igzip_lib.h"
35 #include "test.h"
36 
37 #define BUF_SIZE 1024
38 #define MIN_TEST_LOOPS   8
39 #ifndef RUN_MEM_SIZE
40 # define RUN_MEM_SIZE 500000000
41 #endif
42 
43 struct isal_zstream stream;
44 
45 int get_filesize(FILE * f)
46 {
47 	int curr, end;
48 
49 	curr = ftell(f);	/* Save current position */
50 	fseek(f, 0L, SEEK_END);
51 	end = ftell(f);
52 	fseek(f, curr, SEEK_SET);	/* Restore position */
53 	return end;
54 }
55 
56 int main(int argc, char *argv[])
57 {
58 	FILE *in, *out = NULL;
59 	unsigned char *inbuf, *outbuf;
60 	int i, infile_size, iterations, outbuf_size;
61 	struct isal_huff_histogram histogram;
62 	struct isal_hufftables hufftables_custom;
63 
64 	memset(&histogram, 0, sizeof(histogram));
65 
66 	if (argc > 3 || argc < 2) {
67 		fprintf(stderr, "Usage: igzip_file_perf  infile [outfile]\n"
68 			"\t - Runs multiple iterations of igzip on a file to "
69 			"get more accurate time results.\n");
70 		exit(0);
71 	}
72 	in = fopen(argv[1], "rb");
73 	if (!in) {
74 		fprintf(stderr, "Can't open %s for reading\n", argv[1]);
75 		exit(0);
76 	}
77 	if (argc > 2) {
78 		out = fopen(argv[2], "wb");
79 		if (!out) {
80 			fprintf(stderr, "Can't open %s for writing\n", argv[2]);
81 			exit(0);
82 		}
83 		printf("outfile=%s\n", argv[2]);
84 	}
85 	printf("Window Size: %d K\n", HIST_SIZE);
86 	printf("igzip_file_perf: \n");
87 	fflush(0);
88 	/* Allocate space for entire input file and output
89 	 * (assuming some possible expansion on output size)
90 	 */
91 	infile_size = get_filesize(in);
92 
93 	if (infile_size != 0) {
94 		outbuf_size = infile_size * 2;
95 		iterations = RUN_MEM_SIZE / infile_size;
96 	} else {
97 		outbuf_size = BUF_SIZE;
98 		iterations = MIN_TEST_LOOPS;
99 	}
100 	if (iterations < MIN_TEST_LOOPS)
101 		iterations = MIN_TEST_LOOPS;
102 
103 	inbuf = malloc(infile_size);
104 	if (inbuf == NULL) {
105 		fprintf(stderr, "Can't allocate input buffer memory\n");
106 		exit(0);
107 	}
108 	outbuf = malloc(outbuf_size);
109 	if (outbuf == NULL) {
110 		fprintf(stderr, "Can't allocate output buffer memory\n");
111 		exit(0);
112 	}
113 
114 	printf("igzip_file_perf: %s %d iterations\n", argv[1], iterations);
115 	/* Read complete input file into buffer */
116 	stream.avail_in = (uint32_t) fread(inbuf, 1, infile_size, in);
117 	if (stream.avail_in != infile_size) {
118 		fprintf(stderr, "Couldn't fit all of input file into buffer\n");
119 		exit(0);
120 	}
121 
122 	struct perf start, stop;
123 	perf_start(&start);
124 
125 	for (i = 0; i < iterations; i++) {
126 		isal_deflate_init(&stream);
127 		stream.end_of_stream = 1;	/* Do the entire file at once */
128 		stream.flush = NO_FLUSH;
129 		stream.next_in = inbuf;
130 		stream.avail_in = infile_size;
131 		stream.next_out = outbuf;
132 		stream.avail_out = outbuf_size;
133 		isal_deflate(&stream);
134 		if (stream.avail_in != 0)
135 			break;
136 	}
137 	perf_stop(&stop);
138 
139 	if (stream.avail_in != 0) {
140 		fprintf(stderr, "Could not compress all of inbuf\n");
141 		exit(0);
142 	}
143 
144 	printf("  file %s - in_size=%d out_size=%d iter=%d ratio_default=%3.1f%%", argv[1],
145 	       infile_size, stream.total_out, i, 100.0 * stream.total_out / infile_size);
146 
147 	isal_update_histogram(inbuf, infile_size, &histogram);
148 	isal_create_hufftables(&hufftables_custom, &histogram);
149 
150 	isal_deflate_init(&stream);
151 	stream.end_of_stream = 1;	/* Do the entire file at once */
152 	stream.flush = NO_FLUSH;
153 	stream.next_in = inbuf;
154 	stream.avail_in = infile_size;
155 	stream.next_out = outbuf;
156 	stream.avail_out = outbuf_size;
157 	stream.hufftables = &hufftables_custom;
158 	isal_deflate(&stream);
159 
160 	printf(" ratio_custom=%3.1f%%\n", 100.0 * stream.total_out / infile_size);
161 
162 	if (stream.avail_in != 0) {
163 		fprintf(stderr, "Could not compress all of inbuf\n");
164 		exit(0);
165 	}
166 
167 	printf("igzip_file: ");
168 	perf_print(stop, start, (long long)infile_size * i);
169 
170 	if (argc > 2 && out) {
171 		printf("writing %s\n", argv[2]);
172 		fwrite(outbuf, 1, stream.total_out, out);
173 		fclose(out);
174 	}
175 
176 	fclose(in);
177 	printf("End of igzip_file_perf\n\n");
178 	fflush(0);
179 	return 0;
180 }
181