]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/igzip_inflate_perf.c
930377ebe95899bd0be7f2f377b163ac4b07a70c
[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 <zlib.h>
33 #include "huff_codes.h"
34 #include "igzip_inflate_ref.h"
35 #include "test.h"
36
37 #define BUF_SIZE 1024
38 #define MIN_TEST_LOOPS 100
39 #ifndef RUN_MEM_SIZE
40 # define RUN_MEM_SIZE 1000000000
41 #endif
42
43 int get_filesize(FILE * f)
44 {
45 int curr, end;
46
47 curr = ftell(f); /* Save current position */
48 fseek(f, 0L, SEEK_END);
49 end = ftell(f);
50 fseek(f, curr, SEEK_SET); /* Restore position */
51 return end;
52 }
53
54 int main(int argc, char *argv[])
55 {
56 FILE *in, *out = NULL;
57 unsigned char *inbuf, *outbuf, *tempbuf;
58 int i, infile_size, iterations, outbuf_size, check;
59 uint64_t inbuf_size;
60 struct inflate_state state;
61
62 if (argc > 3 || argc < 2) {
63 fprintf(stderr, "Usage: igzip_inflate_file_perf infile\n"
64 "\t - Runs multiple iterations of igzip on a file to "
65 "get more accurate time results.\n");
66 exit(0);
67 }
68 in = fopen(argv[1], "rb");
69 if (!in) {
70 fprintf(stderr, "Can't open %s for reading\n", argv[1]);
71 exit(0);
72 }
73 if (argc > 2) {
74 out = fopen(argv[2], "wb");
75 if (!out) {
76 fprintf(stderr, "Can't open %s for writing\n", argv[2]);
77 exit(0);
78 }
79 printf("outfile=%s\n", argv[2]);
80 }
81 printf("igzip_inflate_perf: \n");
82 fflush(0);
83 /* Allocate space for entire input file and output
84 * (assuming some possible expansion on output size)
85 */
86 infile_size = get_filesize(in);
87
88 if (infile_size != 0) {
89 outbuf_size = infile_size;
90 iterations = RUN_MEM_SIZE / infile_size;
91 } else {
92 printf("Error: input file has 0 size\n");
93 exit(0);
94 }
95 if (iterations < MIN_TEST_LOOPS)
96 iterations = MIN_TEST_LOOPS;
97
98 tempbuf = malloc(infile_size);
99 if (tempbuf == NULL) {
100 fprintf(stderr, "Can't allocate temp buffer memory\n");
101 exit(0);
102 }
103 inbuf_size = compressBound(infile_size);
104 inbuf = malloc(inbuf_size);
105 if (inbuf == NULL) {
106 fprintf(stderr, "Can't allocate input buffer memory\n");
107 exit(0);
108 }
109 outbuf = malloc(infile_size);
110 if (outbuf == NULL) {
111 fprintf(stderr, "Can't allocate output buffer memory\n");
112 exit(0);
113 }
114 fread(tempbuf, 1, infile_size, in);
115 i = compress2(inbuf, &inbuf_size, tempbuf, infile_size, 9);
116 if (i != Z_OK) {
117 printf("Compression of input file failed\n");
118 exit(0);
119 }
120 printf("igzip_inflate_perf: %s %d iterations\n", argv[1], iterations);
121 /* Read complete input file into buffer */
122 fclose(in);
123 struct perf start, stop;
124 perf_start(&start);
125
126 for (i = 0; i < iterations; i++) {
127 igzip_inflate_init(&state, inbuf + 2, inbuf_size - 2, outbuf, outbuf_size);
128
129 check = igzip_inflate(&state);
130 if (check) {
131 printf("Error in decompression with error %d\n", check);
132 break;
133 }
134 }
135 perf_stop(&stop);
136
137 printf(" file %s - in_size=%d out_size=%d iter=%d\n", argv[1],
138 infile_size, state.out_buffer.total_out, i);
139
140 printf("igzip_file: ");
141 perf_print(stop, start, (long long)infile_size * i);
142
143 printf("End of igzip_inflate_perf\n\n");
144 fflush(0);
145
146 free(inbuf);
147 free(outbuf);
148 free(tempbuf);
149
150 return 0;
151 }