freebsd-nq/usr.bin/mkuzip/mkuz_blockcache.c
Maxim Sobolev 4fc55e3e46 Improve performance in a few key areas:
o Split the compression across several worker threads. By default, "several"
   matches number of CPUs, capped at 24 for sanity when running on a very big
   hardwares. Provide option to set that number manually;

 o Fix bug inherited from the mkulzma (R.I.P) which degraded already slow LZMA
   compression even further by calling function to release compression state
   after processing each block.

   It is neither documented as required nor actually required by the LZMA
   library. This caused spree of system calls to release memory and then map
   it again for every block. LZMA compression is more than 2x faster after this
   change alone;

 o Record time it takes to do compression and report throughput achieved.

 o Add simple first-level 256 entry hash table for de-dup code, so it's not
   becoming a bottleneck at big files.
2016-04-23 07:23:43 +00:00

149 lines
4.2 KiB
C

/*
* Copyright (c) 2016 Maxim Sobolev <sobomax@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(MKUZ_DEBUG)
# include <assert.h>
# include <stdio.h>
#endif
#include "mkuz_blockcache.h"
#include "mkuz_blk.h"
struct mkuz_blkcache_itm {
struct mkuz_blk_info hit;
struct mkuz_blkcache_itm *next;
};
static struct mkuz_blkcache {
struct mkuz_blkcache_itm first[256];
} blkcache;
static int
verify_match(int fd, const struct mkuz_blk *cbp, struct mkuz_blkcache_itm *bcep)
{
void *vbuf;
ssize_t rlen;
int rval;
rval = -1;
vbuf = malloc(cbp->info.len);
if (vbuf == NULL) {
goto e0;
}
if (lseek(fd, bcep->hit.offset, SEEK_SET) < 0) {
goto e1;
}
rlen = read(fd, vbuf, cbp->info.len);
if (rlen < 0 || (unsigned)rlen != cbp->info.len) {
goto e2;
}
rval = (memcmp(cbp->data, vbuf, cbp->info.len) == 0) ? 1 : 0;
e2:
lseek(fd, cbp->info.offset, SEEK_SET);
e1:
free(vbuf);
e0:
return (rval);
}
#define I2J(x) ((intmax_t)(x))
#define U2J(x) ((uintmax_t)(x))
static unsigned char
digest_fold(const unsigned char *mdigest)
{
int i;
unsigned char rval;
rval = mdigest[0];
for (i = 1; i < 16; i++) {
rval = rval ^ mdigest[i];
}
return (rval);
}
struct mkuz_blk_info *
mkuz_blkcache_regblock(int fd, const struct mkuz_blk *bp)
{
struct mkuz_blkcache_itm *bcep;
int rval;
unsigned char h;
#if defined(MKUZ_DEBUG)
assert((unsigned)lseek(fd, 0, SEEK_CUR) == bp->info.offset);
#endif
h = digest_fold(bp->info.digest);
if (blkcache.first[h].hit.len == 0) {
bcep = &blkcache.first[h];
} else {
for (bcep = &blkcache.first[h]; bcep != NULL; bcep = bcep->next) {
if (bcep->hit.len != bp->info.len)
continue;
if (memcmp(bp->info.digest, bcep->hit.digest,
sizeof(bp->info.digest)) == 0) {
break;
}
}
if (bcep != NULL) {
rval = verify_match(fd, bp, bcep);
if (rval == 1) {
#if defined(MKUZ_DEBUG)
fprintf(stderr, "cache hit %jd, %jd, %jd, %jd\n",
I2J(bcep->hit.blkno), I2J(bcep->hit.offset),
I2J(bp->info.offset), I2J(bp->info.len));
#endif
return (&bcep->hit);
}
if (rval == 0) {
#if defined(MKUZ_DEBUG)
fprintf(stderr, "block MD5 collision, you should try lottery, "
"man!\n");
#endif
return (NULL);
}
warn("verify_match");
return (NULL);
}
bcep = malloc(sizeof(struct mkuz_blkcache_itm));
if (bcep == NULL)
return (NULL);
memset(bcep, '\0', sizeof(struct mkuz_blkcache_itm));
bcep->next = blkcache.first[h].next;
blkcache.first[h].next = bcep;
}
bcep->hit = bp->info;
return (NULL);
}