]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/contrib/long_distance_matching/main.c
15 // #define DECOMPRESS_AND_VERIFY
17 /* Compress file given by fname and output to oname.
18 * Returns 0 if successful, error code otherwise.
20 * This adds a header from LDM_writeHeader to the beginning of the output.
22 * This might seg fault if the compressed size is > the decompress
23 * size due to the mmapping and output file size allocated to be the input size
24 * The compress function should check before writing or buffer writes.
26 static int compress(const char *fname
, const char *oname
) {
30 size_t maxCompressedSize
, compressedSize
;
32 struct timeval tv1
, tv2
;
36 /* Open the input file. */
37 if ((fdin
= open(fname
, O_RDONLY
)) < 0) {
38 perror("Error in file opening");
42 /* Open the output file. */
43 if ((fdout
= open(oname
, O_RDWR
| O_CREAT
| O_TRUNC
, (mode_t
)0600)) < 0) {
44 perror("Can't create output file");
48 /* Find the size of the input file. */
49 if (fstat (fdin
, &statbuf
) < 0) {
50 perror("Fstat error");
54 maxCompressedSize
= (statbuf
.st_size
+ LDM_HEADER_SIZE
);
56 // Handle case where compressed size is > decompressed size.
57 // TODO: The compress function should check before writing or buffer writes.
58 maxCompressedSize
+= statbuf
.st_size
/ 255;
60 ftruncate(fdout
, maxCompressedSize
);
62 /* mmap the input file. */
63 if ((src
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_SHARED
, fdin
, 0))
65 perror("mmap error for input");
69 /* mmap the output file. */
70 if ((dst
= mmap(0, maxCompressedSize
, PROT_READ
| PROT_WRITE
,
71 MAP_SHARED
, fdout
, 0)) == (caddr_t
) - 1) {
72 perror("mmap error for output");
76 gettimeofday(&tv1
, NULL
);
78 compressedSize
= LDM_HEADER_SIZE
+
79 LDM_compress(src
, statbuf
.st_size
,
80 dst
+ LDM_HEADER_SIZE
, maxCompressedSize
);
82 gettimeofday(&tv2
, NULL
);
85 LDM_writeHeader(dst
, compressedSize
, statbuf
.st_size
);
87 // Truncate file to compressedSize.
88 ftruncate(fdout
, compressedSize
);
90 printf("%25s : %10lu -> %10lu - %s \n", fname
,
91 (size_t)statbuf
.st_size
, (size_t)compressedSize
, oname
);
92 printf("Compression ratio: %.2fx --- %.1f%%\n",
93 (double)statbuf
.st_size
/ (double)compressedSize
,
94 (double)compressedSize
/ (double)(statbuf
.st_size
) * 100.0);
96 timeTaken
= (double) (tv2
.tv_usec
- tv1
.tv_usec
) / 1000000 +
97 (double) (tv2
.tv_sec
- tv1
.tv_sec
),
99 printf("Total compress time = %.3f seconds, Average scanning speed: %.3f MB/s\n",
101 ((double)statbuf
.st_size
/ (double) (1 << 20)) / timeTaken
);
109 #ifdef DECOMPRESS_AND_VERIFY
110 /* Decompress file compressed using LDM_compress.
111 * The input file should have the LDM_HEADER followed by payload.
112 * Returns 0 if succesful, and an error code otherwise.
114 static int decompress(const char *fname
, const char *oname
) {
118 U64 compressedSize
, decompressedSize
;
121 /* Open the input file. */
122 if ((fdin
= open(fname
, O_RDONLY
)) < 0) {
123 perror("Error in file opening");
127 /* Open the output file. */
128 if ((fdout
= open(oname
, O_RDWR
| O_CREAT
| O_TRUNC
, (mode_t
)0600)) < 0) {
129 perror("Can't create output file");
133 /* Find the size of the input file. */
134 if (fstat (fdin
, &statbuf
) < 0) {
135 perror("Fstat error");
139 /* mmap the input file. */
140 if ((src
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_SHARED
, fdin
, 0))
142 perror("mmap error for input");
146 /* Read the header. */
147 LDM_readHeader(src
, &compressedSize
, &decompressedSize
);
149 ftruncate(fdout
, decompressedSize
);
151 /* mmap the output file */
152 if ((dst
= mmap(0, decompressedSize
, PROT_READ
| PROT_WRITE
,
153 MAP_SHARED
, fdout
, 0)) == (caddr_t
) - 1) {
154 perror("mmap error for output");
158 outSize
= LDM_decompress(
159 src
+ LDM_HEADER_SIZE
, statbuf
.st_size
- LDM_HEADER_SIZE
,
160 dst
, decompressedSize
);
161 printf("Ret size out: %zu\n", outSize
);
168 /* Compare two files.
169 * Returns 0 iff they are the same.
171 static int compare(FILE *fp0
, FILE *fp1
) {
173 while (result
== 0) {
176 const size_t r0
= fread(b0
, 1, sizeof(b0
), fp0
);
177 const size_t r1
= fread(b1
, 1, sizeof(b1
), fp1
);
179 result
= (int)r0
- (int)r1
;
181 if (0 == r0
|| 0 == r1
) break;
183 if (0 == result
) result
= memcmp(b0
, b1
, r0
);
188 /* Verify the input file is the same as the decompressed file. */
189 static int verify(const char *inpFilename
, const char *decFilename
) {
192 if ((inpFp
= fopen(inpFilename
, "rb")) == NULL
) {
193 perror("Could not open input file\n");
197 if ((decFp
= fopen(decFilename
, "rb")) == NULL
) {
198 perror("Could not open decompressed file\n");
202 printf("verify : %s <-> %s\n", inpFilename
, decFilename
);
204 const int cmp
= compare(inpFp
, decFp
);
206 printf("verify : OK\n");
208 printf("verify : NG\n");
219 int main(int argc
, const char *argv
[]) {
220 const char * const exeName
= argv
[0];
221 char inpFilename
[256] = { 0 };
222 char ldmFilename
[256] = { 0 };
223 char decFilename
[256] = { 0 };
226 printf("Wrong arguments\n");
228 printf("%s FILE\n", exeName
);
232 snprintf(inpFilename
, 256, "%s", argv
[1]);
233 snprintf(ldmFilename
, 256, "%s.ldm", argv
[1]);
234 snprintf(decFilename
, 256, "%s.ldm.dec", argv
[1]);
236 printf("inp = [%s]\n", inpFilename
);
237 printf("ldm = [%s]\n", ldmFilename
);
238 printf("dec = [%s]\n", decFilename
);
242 if (compress(inpFilename
, ldmFilename
)) {
243 printf("Compress error\n");
248 #ifdef DECOMPRESS_AND_VERIFY
251 struct timeval tv1
, tv2
;
252 gettimeofday(&tv1
, NULL
);
253 if (decompress(ldmFilename
, decFilename
)) {
254 printf("Decompress error\n");
257 gettimeofday(&tv2
, NULL
);
258 printf("Total decompress time = %f seconds\n",
259 (double) (tv2
.tv_usec
- tv1
.tv_usec
) / 1000000 +
260 (double) (tv2
.tv_sec
- tv1
.tv_sec
));
263 if (verify(inpFilename
, decFilename
)) {
264 printf("Verification error\n");