From ce6a95f47b7f82705c63287ebb7907b28b5626ae Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Fri, 28 Dec 2018 10:25:44 -0600 Subject: [PATCH] programs: add test_util Move program utility functions that are used only by "test programs" (i.e. not by gzip/gunzip) from prog_util.{c,h} into test_util.{c,h}. This reduces the code that is compiled for the default build target, which excludes the test programs. --- Makefile | 42 +++--- programs/benchmark.c | 4 +- programs/checksum.c | 4 +- programs/prog_util.c | 164 ----------------------- programs/prog_util.h | 14 -- programs/test_checksums.c | 3 +- programs/test_slow_decompression.c | 4 +- programs/test_util.c | 204 +++++++++++++++++++++++++++++ programs/test_util.h | 55 ++++++++ 9 files changed, 289 insertions(+), 205 deletions(-) create mode 100644 programs/test_util.c create mode 100644 programs/test_util.h diff --git a/Makefile b/Makefile index 415fdec..bc9f005 100644 --- a/Makefile +++ b/Makefile @@ -168,29 +168,37 @@ PROG_CFLAGS += $(CFLAGS) \ -D_FILE_OFFSET_BITS=64 \ -DHAVE_CONFIG_H -PROG_COMMON_HEADERS := programs/prog_util.h programs/config.h -PROG_COMMON_SRC := programs/prog_util.c programs/tgetopt.c -NONTEST_PROGRAM_SRC := programs/gzip.c -TEST_PROGRAM_SRC := programs/benchmark.c \ - programs/checksum.c \ - programs/test_checksums.c \ - programs/test_slow_decompression.c +ALL_PROG_COMMON_HEADERS := programs/config.h \ + programs/prog_util.h \ + programs/test_util.h +PROG_COMMON_SRC := programs/prog_util.c \ + programs/tgetopt.c +NONTEST_PROG_SRC := programs/gzip.c +TEST_PROG_COMMON_SRC := programs/test_util.c +TEST_PROG_SRC := programs/benchmark.c \ + programs/checksum.c \ + programs/test_checksums.c \ + programs/test_slow_decompression.c -NONTEST_PROGRAMS := $(NONTEST_PROGRAM_SRC:programs/%.c=%$(PROG_SUFFIX)) +NONTEST_PROGRAMS := $(NONTEST_PROG_SRC:programs/%.c=%$(PROG_SUFFIX)) DEFAULT_TARGETS += $(NONTEST_PROGRAMS) -TEST_PROGRAMS := $(TEST_PROGRAM_SRC:programs/%.c=%$(PROG_SUFFIX)) +TEST_PROGRAMS := $(TEST_PROG_SRC:programs/%.c=%$(PROG_SUFFIX)) -PROG_COMMON_OBJ := $(PROG_COMMON_SRC:%.c=%.o) -NONTEST_PROGRAM_OBJ := $(NONTEST_PROGRAM_SRC:%.c=%.o) -TEST_PROGRAM_OBJ := $(TEST_PROGRAM_SRC:%.c=%.o) -PROG_OBJ := $(PROG_COMMON_OBJ) $(NONTEST_PROGRAM_OBJ) $(TEST_PROGRAM_OBJ) +PROG_COMMON_OBJ := $(PROG_COMMON_SRC:%.c=%.o) +NONTEST_PROG_OBJ := $(NONTEST_PROG_SRC:%.c=%.o) +TEST_PROG_COMMON_OBJ := $(TEST_PROG_COMMON_SRC:%.c=%.o) +TEST_PROG_OBJ := $(TEST_PROG_SRC:%.c=%.o) + +ALL_PROG_OBJ := $(PROG_COMMON_OBJ) $(NONTEST_PROG_OBJ) \ + $(TEST_PROG_COMMON_OBJ) $(TEST_PROG_OBJ) # Generate autodetected configuration header programs/config.h:programs/detect.sh .prog-cflags $(QUIET_GEN) CC="$(CC)" CFLAGS="$(PROG_CFLAGS)" $< > $@ # Compile program object files -$(PROG_OBJ): %.o: %.c $(PROG_COMMON_HEADERS) $(COMMON_HEADERS) .prog-cflags +$(ALL_PROG_OBJ): %.o: %.c $(ALL_PROG_COMMON_HEADERS) $(COMMON_HEADERS) \ + .prog-cflags $(QUIET_CC) $(CC) -o $@ -c $(CPPFLAGS) $(PROG_CFLAGS) $< # Link the programs. @@ -198,10 +206,12 @@ $(PROG_OBJ): %.o: %.c $(PROG_COMMON_HEADERS) $(COMMON_HEADERS) .prog-cflags # Note: the test programs are not compiled by default. One reason is that the # test programs must be linked with zlib for doing comparisons. -$(NONTEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) $(STATIC_LIB) +$(NONTEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) \ + $(STATIC_LIB) $(QUIET_CCLD) $(CC) -o $@ $(LDFLAGS) $(PROG_CFLAGS) $+ -$(TEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) $(STATIC_LIB) +$(TEST_PROGRAMS): %$(PROG_SUFFIX): programs/%.o $(PROG_COMMON_OBJ) \ + $(TEST_PROG_COMMON_OBJ) $(STATIC_LIB) $(QUIET_CCLD) $(CC) -o $@ $(LDFLAGS) $(PROG_CFLAGS) $+ -lz ifdef HARD_LINKS diff --git a/programs/benchmark.c b/programs/benchmark.c index 51fd5b9..cb852a7 100644 --- a/programs/benchmark.c +++ b/programs/benchmark.c @@ -25,9 +25,7 @@ * OTHER DEALINGS IN THE SOFTWARE. */ -#include /* for comparison purposes */ - -#include "prog_util.h" +#include "test_util.h" static const tchar *const optstring = T("1::2::3::4::5::6::7::8::9::C:D:ghs:VYZz"); diff --git a/programs/checksum.c b/programs/checksum.c index 3e52ca4..a7f8688 100644 --- a/programs/checksum.c +++ b/programs/checksum.c @@ -25,9 +25,7 @@ * OTHER DEALINGS IN THE SOFTWARE. */ -#include - -#include "prog_util.h" +#include "test_util.h" static const tchar *const optstring = T("Ahs:tZ"); diff --git a/programs/prog_util.c b/programs/prog_util.c index 100fe54..7cc3bc6 100644 --- a/programs/prog_util.c +++ b/programs/prog_util.c @@ -30,13 +30,11 @@ #include #include #include -#include #ifdef _WIN32 # include #else # include # include -# include #endif #ifndef O_BINARY @@ -95,14 +93,6 @@ msg_errno(const char *format, ...) va_end(va); } -/* Abort with an error message */ -_noreturn void -assertion_failed(const char *expr, const char *file, int line) -{ - msg("Assertion failed: %s at %s:%d", expr, file, line); - abort(); -} - /* malloc() wrapper */ void * xmalloc(size_t size) @@ -115,160 +105,6 @@ xmalloc(size_t size) return p; } -static size_t -get_page_size(void) -{ -#ifdef _WIN32 - SYSTEM_INFO info; - - GetSystemInfo(&info); - return info.dwPageSize; -#else - return sysconf(_SC_PAGESIZE); -#endif -} - -/* Allocate a buffer with guard pages */ -int -alloc_guarded_buffer(size_t size, u8 **start_ret, u8 **end_ret) -{ - const size_t pagesize = get_page_size(); - const size_t nr_pages = (size + pagesize - 1) / pagesize; - u8 *base_addr; - u8 *start, *end; -#ifdef _WIN32 - DWORD oldProtect; -#else - int fd; -#endif - - *start_ret = NULL; - *end_ret = NULL; - -#ifdef _WIN32 - /* Allocate buffer and guard pages with no access. */ - base_addr = VirtualAlloc(NULL, (nr_pages + 2) * pagesize, - MEM_COMMIT | MEM_RESERVE, PAGE_NOACCESS); - if (!base_addr) { - msg("Unable to allocate memory (VirtualAlloc): Windows error %u", - (unsigned int)GetLastError()); - return -1; - } - start = base_addr + pagesize; - end = start + (nr_pages * pagesize); - - /* Grant read+write access to just the buffer. */ - if (!VirtualProtect(start, end - start, PAGE_READWRITE, &oldProtect)) { - msg("Unable to protect memory (VirtualProtect): Windows error %u", - (unsigned int)GetLastError()); - VirtualFree(base_addr, 0, MEM_RELEASE); - return -1; - } -#else - /* - * Allocate buffer and guard pages. - * For portability, use /dev/zero instead of MAP_ANONYMOUS. - */ - fd = open("/dev/zero", O_RDONLY); - if (fd < 0) { - msg_errno("Unable to open /dev/zero"); - return -1; - } - base_addr = mmap(NULL, (nr_pages + 2) * pagesize, - PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); - close(fd); - if (base_addr == (u8 *)MAP_FAILED) { - msg_errno("Unable to allocate memory (unable to mmap /dev/zero)"); - return -1; - } - start = base_addr + pagesize; - end = start + (nr_pages * pagesize); - - /* Unmap the guard pages. */ - munmap(base_addr, pagesize); - munmap(end, pagesize); -#endif - *start_ret = start; - *end_ret = end; - return 0; -} - -/* Free a buffer that was allocated by alloc_guarded_buffer() */ -void -free_guarded_buffer(u8 *start, u8 *end) -{ - if (!start) - return; -#ifdef _WIN32 - VirtualFree(start - get_page_size(), 0, MEM_RELEASE); -#else - munmap(start, end - start); -#endif -} - -/* - * Return the number of timer ticks that have elapsed since some unspecified - * point fixed at the start of program execution - */ -u64 -timer_ticks(void) -{ -#ifdef _WIN32 - LARGE_INTEGER count; - QueryPerformanceCounter(&count); - return count.QuadPart; -#elif defined(HAVE_CLOCK_GETTIME) - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return (1000000000 * (u64)ts.tv_sec) + ts.tv_nsec; -#else - struct timeval tv; - gettimeofday(&tv, NULL); - return (1000000 * (u64)tv.tv_sec) + tv.tv_usec; -#endif -} - -/* - * Return the number of timer ticks per second - */ -static u64 -timer_frequency(void) -{ -#ifdef _WIN32 - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - return freq.QuadPart; -#elif defined(HAVE_CLOCK_GETTIME) - return 1000000000; -#else - return 1000000; -#endif -} - -/* - * Convert a number of elapsed timer ticks to milliseconds - */ -u64 timer_ticks_to_ms(u64 ticks) -{ - return ticks * 1000 / timer_frequency(); -} - -/* - * Convert a byte count and a number of elapsed timer ticks to MB/s - */ -u64 timer_MB_per_s(u64 bytes, u64 ticks) -{ - return bytes * timer_frequency() / ticks / 1000000; -} - -/* - * Convert a byte count and a number of elapsed timer ticks to KB/s - */ -u64 timer_KB_per_s(u64 bytes, u64 ticks) -{ - return bytes * timer_frequency() / ticks / 1000; -} - /* * Retrieve a pointer to the filename component of the specified path. * diff --git a/programs/prog_util.h b/programs/prog_util.h index 2d79340..0265dd0 100644 --- a/programs/prog_util.h +++ b/programs/prog_util.h @@ -44,10 +44,8 @@ #ifdef __GNUC__ # define _printf(str_idx, args_idx) \ __attribute__((format(printf, str_idx, args_idx))) -# define _noreturn __attribute__((noreturn)) #else # define _printf(str_idx, args_idx) -# define _noreturn #endif #ifdef _WIN32 @@ -121,19 +119,7 @@ extern const tchar *program_invocation_name; extern void _printf(1, 2) msg(const char *fmt, ...); extern void _printf(1, 2) msg_errno(const char *fmt, ...); -extern void _noreturn -assertion_failed(const char *expr, const char *file, int line); - -#define ASSERT(expr) if (!(expr)) assertion_failed(#expr, __FILE__, __LINE__) - extern void *xmalloc(size_t size); -extern int alloc_guarded_buffer(size_t size, u8 **start_ret, u8 **end_ret); -extern void free_guarded_buffer(u8 *start, u8 *end); - -extern u64 timer_ticks(void); -extern u64 timer_ticks_to_ms(u64 ticks); -extern u64 timer_MB_per_s(u64 bytes, u64 ticks); -extern u64 timer_KB_per_s(u64 bytes, u64 ticks); extern const tchar *get_filename(const tchar *path); diff --git a/programs/test_checksums.c b/programs/test_checksums.c index 476ff25..82ada09 100644 --- a/programs/test_checksums.c +++ b/programs/test_checksums.c @@ -7,9 +7,8 @@ #include #include -#include -#include "prog_util.h" +#include "test_util.h" static unsigned int rng_seed; diff --git a/programs/test_slow_decompression.c b/programs/test_slow_decompression.c index de7393d..4890453 100644 --- a/programs/test_slow_decompression.c +++ b/programs/test_slow_decompression.c @@ -5,9 +5,7 @@ * streams that start new Huffman blocks extremely frequently. */ -#include /* for comparison purposes */ - -#include "prog_util.h" +#include "test_util.h" struct output_bitstream { unsigned long bitbuf; diff --git a/programs/test_util.c b/programs/test_util.c new file mode 100644 index 0000000..cadff14 --- /dev/null +++ b/programs/test_util.c @@ -0,0 +1,204 @@ +/* + * test_util.c - utility functions for test programs + * + * Copyright 2016 Eric Biggers + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "test_util.h" + +#include +#include +#ifdef _WIN32 +# include +#else +# include +# include +# include +#endif + +/* Abort with an error message */ +_noreturn void +assertion_failed(const char *expr, const char *file, int line) +{ + msg("Assertion failed: %s at %s:%d", expr, file, line); + abort(); +} + +static size_t +get_page_size(void) +{ +#ifdef _WIN32 + SYSTEM_INFO info; + + GetSystemInfo(&info); + return info.dwPageSize; +#else + return sysconf(_SC_PAGESIZE); +#endif +} + +/* Allocate a buffer with guard pages */ +int +alloc_guarded_buffer(size_t size, u8 **start_ret, u8 **end_ret) +{ + const size_t pagesize = get_page_size(); + const size_t nr_pages = (size + pagesize - 1) / pagesize; + u8 *base_addr; + u8 *start, *end; +#ifdef _WIN32 + DWORD oldProtect; +#else + int fd; +#endif + + *start_ret = NULL; + *end_ret = NULL; + +#ifdef _WIN32 + /* Allocate buffer and guard pages with no access. */ + base_addr = VirtualAlloc(NULL, (nr_pages + 2) * pagesize, + MEM_COMMIT | MEM_RESERVE, PAGE_NOACCESS); + if (!base_addr) { + msg("Unable to allocate memory (VirtualAlloc): Windows error %u", + (unsigned int)GetLastError()); + return -1; + } + start = base_addr + pagesize; + end = start + (nr_pages * pagesize); + + /* Grant read+write access to just the buffer. */ + if (!VirtualProtect(start, end - start, PAGE_READWRITE, &oldProtect)) { + msg("Unable to protect memory (VirtualProtect): Windows error %u", + (unsigned int)GetLastError()); + VirtualFree(base_addr, 0, MEM_RELEASE); + return -1; + } +#else + /* + * Allocate buffer and guard pages. + * For portability, use /dev/zero instead of MAP_ANONYMOUS. + */ + fd = open("/dev/zero", O_RDONLY); + if (fd < 0) { + msg_errno("Unable to open /dev/zero"); + return -1; + } + base_addr = mmap(NULL, (nr_pages + 2) * pagesize, + PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); + close(fd); + if (base_addr == (u8 *)MAP_FAILED) { + msg_errno("Unable to allocate memory (unable to mmap /dev/zero)"); + return -1; + } + start = base_addr + pagesize; + end = start + (nr_pages * pagesize); + + /* Unmap the guard pages. */ + munmap(base_addr, pagesize); + munmap(end, pagesize); +#endif + *start_ret = start; + *end_ret = end; + return 0; +} + +/* Free a buffer that was allocated by alloc_guarded_buffer() */ +void +free_guarded_buffer(u8 *start, u8 *end) +{ + if (!start) + return; +#ifdef _WIN32 + VirtualFree(start - get_page_size(), 0, MEM_RELEASE); +#else + munmap(start, end - start); +#endif +} + +/* + * Return the number of timer ticks that have elapsed since some unspecified + * point fixed at the start of program execution + */ +u64 +timer_ticks(void) +{ +#ifdef _WIN32 + LARGE_INTEGER count; + + QueryPerformanceCounter(&count); + return count.QuadPart; +#elif defined(HAVE_CLOCK_GETTIME) + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return (1000000000 * (u64)ts.tv_sec) + ts.tv_nsec; +#else + struct timeval tv; + + gettimeofday(&tv, NULL); + return (1000000 * (u64)tv.tv_sec) + tv.tv_usec; +#endif +} + +/* + * Return the number of timer ticks per second + */ +static u64 +timer_frequency(void) +{ +#ifdef _WIN32 + LARGE_INTEGER freq; + + QueryPerformanceFrequency(&freq); + return freq.QuadPart; +#elif defined(HAVE_CLOCK_GETTIME) + return 1000000000; +#else + return 1000000; +#endif +} + +/* + * Convert a number of elapsed timer ticks to milliseconds + */ +u64 timer_ticks_to_ms(u64 ticks) +{ + return ticks * 1000 / timer_frequency(); +} + +/* + * Convert a byte count and a number of elapsed timer ticks to MB/s + */ +u64 timer_MB_per_s(u64 bytes, u64 ticks) +{ + return bytes * timer_frequency() / ticks / 1000000; +} + +/* + * Convert a byte count and a number of elapsed timer ticks to KB/s + */ +u64 timer_KB_per_s(u64 bytes, u64 ticks) +{ + return bytes * timer_frequency() / ticks / 1000; +} diff --git a/programs/test_util.h b/programs/test_util.h new file mode 100644 index 0000000..c6a12f6 --- /dev/null +++ b/programs/test_util.h @@ -0,0 +1,55 @@ +/* + * test_util.h - utility functions for test programs + * + * Copyright 2016 Eric Biggers + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef PROGRAMS_TEST_UTIL_H +#define PROGRAMS_TEST_UTIL_H + +#include "prog_util.h" + +#include /* for comparison purposes */ + +#ifdef __GNUC__ +# define _noreturn __attribute__((noreturn)) +#else +# define _noreturn +#endif + +extern void _noreturn +assertion_failed(const char *expr, const char *file, int line); + +#define ASSERT(expr) { if (unlikely(!(expr))) \ + assertion_failed(#expr, __FILE__, __LINE__); } + +extern int alloc_guarded_buffer(size_t size, u8 **start_ret, u8 **end_ret); +extern void free_guarded_buffer(u8 *start, u8 *end); + +extern u64 timer_ticks(void); +extern u64 timer_ticks_to_ms(u64 ticks); +extern u64 timer_MB_per_s(u64 bytes, u64 ticks); +extern u64 timer_KB_per_s(u64 bytes, u64 ticks); + +#endif /* PROGRAMS_TEST_UTIL_H */