diff --git a/main.c b/main.c index 799b28b..44c9c56 100644 --- a/main.c +++ b/main.c @@ -130,7 +130,7 @@ static void preserve_perms(int fd_in, int fd_out) if (unlikely(fstat(fd_in, &st))) fatal("Failed to fstat input file\n"); if (fchmod(fd_out, (st.st_mode & 0777))) - print_output("Warning, unable to set permissions on %s\n", control.outfile); + print_err("Warning, unable to set permissions on %s\n", control.outfile); /* chown fail is not fatal */ fchown(fd_out, st.st_uid, st.st_gid); @@ -430,7 +430,7 @@ static void compress_file(void) if (!STDIN) { /* is extension at end of infile? */ if ((tmp = strrchr(control.infile, '.')) && !strcmp(tmp, control.suffix)) { - print_output("%s: already has %s suffix. Skipping...\n", control.infile, control.suffix); + print_err("%s: already has %s suffix. Skipping...\n", control.infile, control.suffix); return; } @@ -518,32 +518,6 @@ static void compress_file(void) free(control.outfile); } -/* - * Returns ram size in bytes on linux/darwin. - */ -#ifdef __APPLE__ -static i64 get_ram(void) -{ - int mib[2]; - size_t len; - i64 *p, ramsize; - - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - sysctl(mib, 2, NULL, &len, NULL, 0); - p = malloc(len); - sysctl(mib, 2, p, &len, NULL, 0); - ramsize = *p; - - return ramsize; -} -#else -static i64 get_ram(void) -{ - return (i64)sysconf(_SC_PHYS_PAGES) * (i64)sysconf(_SC_PAGE_SIZE); -} -#endif - int main(int argc, char *argv[]) { struct timeval start_time, end_time; @@ -569,11 +543,8 @@ int main(int argc, char *argv[]) control.window = 0; control.threshold = 1.0; /* default lzo test compression threshold (level 1) with LZMA compression */ /* for testing single CPU */ -#ifndef NOTHREAD - control.threads = sysconf(_SC_NPROCESSORS_ONLN); /* get CPUs for LZMA */ -#else - control.threads = 1; -#endif + control.threads = PROCESSORS; /* get CPUs for LZMA */ + control.page_size = PAGE_SIZE; control.nice_val = 19; @@ -720,57 +691,55 @@ int main(int argc, char *argv[]) control.flags |= FLAG_SHOW_PROGRESS; } + if (argc < 1) + control.flags |= FLAG_STDIN; + if (UNLIMITED && STDIN) { print_err("Cannot have -U and stdin, unlimited mode disabled.\n"); control.flags &= ~ FLAG_UNLIMITED; } - if (argc < 1) - control.flags |= FLAG_STDIN; - /* OK, if verbosity set, print summary of options selected */ - if (VERBOSE && !INFO) { - print_err("The following options are in effect for this %s.\n", + if (!INFO) { + print_verbose("The following options are in effect for this %s.\n", DECOMPRESS ? "DECOMPRESSION" : "COMPRESSION"); if (LZMA_COMPRESS) - print_err("Threading is %s. Number of CPUs detected: %lu\n", control.threads > 1? "ENABLED" : "DISABLED", + print_verbose("Threading is %s. Number of CPUs detected: %lu\n", control.threads > 1? "ENABLED" : "DISABLED", control.threads); - print_err("Nice Value: %d\n", control.nice_val); - if (SHOW_PROGRESS) - print_err("Show Progress\n"); - if (VERBOSITY) - print_err("Verbose\n"); - else if (MAX_VERBOSE) - print_err("Max Verbosity\n"); + print_verbose("Detected %lld bytes ram\n", control.ramsize); + print_verbose("Nice Value: %d\n", control.nice_val); + print_progress("Show Progress\n"); + print_maxverbose("Max "); + print_verbose("Verbose\n"); if (FORCE_REPLACE) - print_err("Overwrite Files\n"); + print_verbose("Overwrite Files\n"); if (!KEEP_FILES) - print_err("Remove input files on completion\n"); + print_verbose("Remove input files on completion\n"); if (control.outdir) - print_err("Output Directory Specified: %s\n", control.outdir); + print_verbose("Output Directory Specified: %s\n", control.outdir); else if (control.outname) - print_err("Output Filename Specified: %s\n", control.outname); + print_verbose("Output Filename Specified: %s\n", control.outname); if (TEST_ONLY) - print_err("Test file integrity\n"); + print_verbose("Test file integrity\n"); /* show compression options */ if (!DECOMPRESS) { - print_err("Compression mode is: "); + print_verbose("Compression mode is: "); if (LZMA_COMPRESS) - print_err("LZMA. LZO Test Compression Threshold: %.f\n", + print_verbose("LZMA. LZO Test Compression Threshold: %.f\n", (control.threshold < 1.05 ? 21 - control.threshold * 20 : 0)); else if (LZO_COMPRESS) - print_err("LZO\n"); + print_verbose("LZO\n"); else if (BZIP2_COMPRESS) - print_err("BZIP2. LZO Test Compression Threshold: %.f\n", + print_verbose("BZIP2. LZO Test Compression Threshold: %.f\n", (control.threshold < 1.05 ? 21 - control.threshold * 20 : 0)); else if (ZLIB_COMPRESS) - print_err("GZIP\n"); + print_verbose("GZIP\n"); else if (ZPAQ_COMPRESS) - print_err("ZPAQ. LZO Test Compression Threshold: %.f\n", + print_verbose("ZPAQ. LZO Test Compression Threshold: %.f\n", (control.threshold < 1.05 ? 21 - control.threshold * 20 : 0)); else if (NO_COMPRESS) - print_err("RZIP\n"); + print_verbose("RZIP pre-processing only\n"); if (control.window) { print_verbose("Compression Window: %lld = %lldMB\n", control.window, control.window * 100ull); print_verbose("Compression Level: %d\n", control.compression_level); @@ -779,7 +748,7 @@ int main(int argc, char *argv[]) } if (unlikely(setpriority(PRIO_PROCESS, 0, control.nice_val) == -1)) - fatal("Unable to set nice value\n"); + print_err("Warning, unable to set nice value\n"); /* One extra iteration for the case of no parameters means we will default to stdin/out */ for (i = 0; i <= argc; i++) { diff --git a/rzip.c b/rzip.c index c5a2159..3235e39 100644 --- a/rzip.c +++ b/rzip.c @@ -113,7 +113,7 @@ static void remap_low_sb(void) new_offset = sb.orig_size - sb.size_low; top = 1; } - new_offset -= new_offset % 4096; /* Round to page size */ + new_offset -= new_offset % control.page_size; /* Round to page size */ print_maxverbose("Sliding main buffer\n"); if (unlikely(munmap(sb.buf_low, sb.size_low))) fatal("Failed to munmap in remap_low_sb\n"); @@ -130,7 +130,7 @@ static inline void remap_high_sb(i64 p) sb.size_high = sb.high_length; /* In case we shrunk it when we hit the end of the file */ sb.offset_high = p; /* Make sure offset is rounded to page size of total offset */ - sb.offset_high -= (sb.offset_high + sb.orig_offset) % 4096; + sb.offset_high -= (sb.offset_high + sb.orig_offset) % control.page_size; if (unlikely(sb.offset_high + sb.size_high > sb.orig_size)) sb.size_high = sb.orig_size - sb.offset_high; sb.buf_high = (uchar *)mmap(sb.buf_high, sb.size_high, PROT_READ, MAP_SHARED, sb.fd, sb.orig_offset + sb.offset_high); @@ -478,7 +478,7 @@ static void show_distrib(struct rzip_state *st) } if (total != st->hash_count) - print_output("/tWARNING: hash_count says total %lld\n", st->hash_count); + print_err("/tWARNING: hash_count says total %lld\n", st->hash_count); print_output("\t%lld total hashes -- %lld in primary bucket (%-2.3f%%)\n", total, primary, primary*100.0/total); @@ -577,9 +577,8 @@ static void hash_search(struct rzip_state *st, double pct_base, double pct_multi chunk_pct = p / (end / 100); if (pct != lastpct || chunk_pct != last_chunkpct) { if (!STDIN) - print_progress("Total: %2d%% Chunk: %2d%%\r", pct, chunk_pct); - else - print_progress("Chunk: %2d%%\r", chunk_pct); + print_progress("Total: %2d%% ", pct); + print_progress("Chunk: %2d%%\r", chunk_pct); lastpct = pct; last_chunkpct = chunk_pct; } @@ -594,7 +593,10 @@ static void hash_search(struct rzip_state *st, double pct_base, double pct_multi } } - print_progress("\n"); + /* Fake that we got to 100% since we're done :D */ + if (!STDIN) + print_progress("Total: 100%% "); + print_progress("Chunk: 100%%\n"); if (MAX_VERBOSE) show_distrib(st); @@ -763,7 +765,7 @@ void rzip_fd(int fd_in, int fd_out) chunk_window = len; } if (chunk_window < len) - chunk_window -= chunk_window % 4096; + chunk_window -= chunk_window % control.page_size; st->chunk_size = chunk_window; st->level = &levels[control.compression_level]; @@ -808,7 +810,7 @@ retry: /* Better to shrink the window to the largest size that works than fail */ if (sb.buf_low == MAP_FAILED) { st->mmap_size = st->mmap_size / 10 * 9; - st->mmap_size -= st->mmap_size % 4096; + st->mmap_size -= st->mmap_size % control.page_size; if (unlikely(!st->mmap_size)) fatal("Unable to mmap any ram\n"); goto retry; @@ -834,7 +836,7 @@ retry: if (unlikely(!MAXRAM)) fatal("Failed to remap ram\n"); st->mmap_size = st->mmap_size / 10 * 9; - st->mmap_size -= st->mmap_size % 4096; + st->mmap_size -= st->mmap_size % control.page_size; if (unlikely(!st->mmap_size)) fatal("Unable to mmap any ram\n"); goto retry; diff --git a/rzip.h b/rzip.h index 8dbe6e4..0daa87b 100644 --- a/rzip.h +++ b/rzip.h @@ -43,19 +43,6 @@ #include #include -#ifdef __APPLE__ -#include -#define fmemopen fake_fmemopen -#define open_memstream fake_open_memstream -#define memstream_update_buffer fake_open_memstream_update_buffer -#define mremap fake_mremap -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif -#else /* __APPLE__ */ -#define memstream_update_buffer(A, B, C) (0) -#endif - #include #include @@ -136,6 +123,51 @@ typedef long long int i64; typedef uint16_t u16; typedef uint32_t u32; +#ifndef MAP_ANONYMOUS + #define MAP_ANONYMOUS MAP_ANON +#endif + +#if defined(NOTHREAD) || !defined(_SC_NPROCESSORS_ONLN) + #define PROCESSORS (1) +#else + #define PROCESSORS (sysconf(_SC_NPROCESSORS_ONLN)) +#endif + +#ifdef _SC_PAGE_SIZE + #define PAGE_SIZE (sysconf(_SC_PAGE_SIZE)) +#else + #define PAGE_SIZE (4096) +#endif + +#ifdef __APPLE__ + #include + #define fmemopen fake_fmemopen + #define open_memstream fake_open_memstream + #define memstream_update_buffer fake_open_memstream_update_buffer + #define mremap fake_mremap +static inline i64 get_ram(void) +{ + int mib[2]; + size_t len; + i64 *p, ramsize; + + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + sysctl(mib, 2, NULL, &len, NULL, 0); + p = malloc(len); + sysctl(mib, 2, p, &len, NULL, 0); + ramsize = *p; + + return ramsize; +} +#else /* __APPLE__ */ + #define memstream_update_buffer(A, B, C) (0) +static inline i64 get_ram(void) +{ + return (i64)sysconf(_SC_PHYS_PAGES) * (i64)sysconf(_SC_PAGE_SIZE); +} +#endif + #define FLAG_SHOW_PROGRESS 2 #define FLAG_KEEP_FILES 4 #define FLAG_TEST_ONLY 8 @@ -204,6 +236,7 @@ struct rzip_control { int major_version; int minor_version; i64 st_size; + long page_size; } control; struct stream { diff --git a/stream.c b/stream.c index c1d982b..c7ed1cd 100644 --- a/stream.c +++ b/stream.c @@ -235,8 +235,8 @@ static void lzma_compress_buf(struct stream *s, int *c_type, i64 *c_len) out: if (MAX_VERBOSE) print_output("\n"); - else if (SHOW_PROGRESS || VERBOSE) - print_output("\r\t \r"); + else + print_progress("\r\t \r"); } static void lzo_compress_buf(struct stream *s, int *c_type, i64 *c_len) @@ -1006,7 +1006,8 @@ static int lzo_compresses(struct stream *s) 100 * ((double) best_dlen / (double) in_len), workcounter); else if (VERBOSE) print_output("%s\r", (ret == 0? "FAILED - below threshold" : "OK")); - else print_progress("\r\t \r"); + else + print_progress("\r\t \r"); free(wrkmem); free(c_buf);