Add stubs for abseil flags.

This CL also removes support for using the gflags library to modify the
flags.

PiperOrigin-RevId: 361583626
This commit is contained in:
Victor Costan
2021-03-08 17:24:08 +00:00
parent 80a2a10c8c
commit 5e7c14bd05
7 changed files with 42 additions and 70 deletions

View File

@@ -182,11 +182,6 @@ include(CheckSymbolExists)
check_symbol_exists("mmap" "sys/mman.h" HAVE_FUNC_MMAP) check_symbol_exists("mmap" "sys/mman.h" HAVE_FUNC_MMAP)
check_symbol_exists("sysconf" "unistd.h" HAVE_FUNC_SYSCONF) check_symbol_exists("sysconf" "unistd.h" HAVE_FUNC_SYSCONF)
find_package(Gflags QUIET)
if(GFLAGS_FOUND OR GFLAGS_TARGET)
set(HAVE_GFLAGS 1)
endif(GFLAGS_FOUND OR GFLAGS_TARGET)
configure_file( configure_file(
"cmake/config.h.in" "cmake/config.h.in"
"${PROJECT_BINARY_DIR}/config.h" "${PROJECT_BINARY_DIR}/config.h"
@@ -268,7 +263,7 @@ if(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)
# Test files include snappy-test.h, HAVE_CONFIG_H must be defined. # Test files include snappy-test.h, HAVE_CONFIG_H must be defined.
target_compile_definitions(snappy_test_support PUBLIC -DHAVE_CONFIG_H) target_compile_definitions(snappy_test_support PUBLIC -DHAVE_CONFIG_H)
target_link_libraries(snappy_test_support snappy ${GFLAGS_LIBRARIES}) target_link_libraries(snappy_test_support snappy)
if(HAVE_LIBZ) if(HAVE_LIBZ)
target_link_libraries(snappy_test_support z) target_link_libraries(snappy_test_support z)
@@ -283,7 +278,6 @@ if(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)
target_include_directories(snappy_test_support target_include_directories(snappy_test_support
BEFORE PUBLIC BEFORE PUBLIC
"${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}"
"${GFLAGS_INCLUDE_DIRS}"
) )
endif(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS) endif(SNAPPY_BUILD_TESTS OR SNAPPY_BUILD_BENCHMARKS)

View File

@@ -132,11 +132,6 @@ should provide a reasonably balanced starting point for benchmarking. (Note that
baddata[1-3].snappy are not intended as benchmarks; they are used to verify baddata[1-3].snappy are not intended as benchmarks; they are used to verify
correctness in the presence of corrupted data in the unit test.) correctness in the presence of corrupted data in the unit test.)
The gflags library for handling of command-line flags is used if it's installed.
You can find it at
https://gflags.github.io/gflags/
Contact Contact
======= =======

View File

@@ -16,9 +16,6 @@
/* Define to 1 if you have a definition for sysconf() in <unistd.h>. */ /* Define to 1 if you have a definition for sysconf() in <unistd.h>. */
#cmakedefine HAVE_FUNC_SYSCONF 1 #cmakedefine HAVE_FUNC_SYSCONF 1
/* Define to 1 to use the gflags package for command-line parsing. */
#cmakedefine HAVE_GFLAGS 1
/* Define to 1 if you have the `lzo2' library (-llzo2). */ /* Define to 1 if you have the `lzo2' library (-llzo2). */
#cmakedefine HAVE_LIBLZO2 1 #cmakedefine HAVE_LIBLZO2 1

View File

@@ -105,17 +105,18 @@
#define SNAPPY_ATTRIBUTE_ALWAYS_INLINE #define SNAPPY_ATTRIBUTE_ALWAYS_INLINE
#endif #endif
// This is only used for recomputing the tag byte table used during // Stubbed version of ABSL_FLAG.
// decompression; for simplicity we just remove it from the open-source //
// version (anyone who wants to regenerate it can just do the call // In the open source version, flags can only be changed at compile time.
// themselves within main()). #define SNAPPY_FLAG(flag_type, flag_name, default_value, help) \
#define DEFINE_bool(flag_name, default_value, description) \ flag_type FLAGS_ ## flag_name = default_value
bool FLAGS_ ## flag_name = default_value
#define DECLARE_bool(flag_name) \
extern bool FLAGS_ ## flag_name
namespace snappy { namespace snappy {
// Stubbed version of absl::GetFlag().
template <typename T>
inline T GetFlag(T flag) { return flag; }
static const uint32_t kuint32max = std::numeric_limits<uint32_t>::max(); static const uint32_t kuint32max = std::numeric_limits<uint32_t>::max();
static const int64_t kint64max = std::numeric_limits<int64_t>::max(); static const int64_t kint64max = std::numeric_limits<int64_t>::max();

View File

@@ -56,27 +56,8 @@
#include <windows.h> #include <windows.h>
#endif #endif
#ifdef HAVE_GFLAGS
#include <gflags/gflags.h>
// This is tricky; both gflags and Google Test want to look at the command line
// arguments. Google Test seems to be the most happy with unknown arguments,
// though, so we call it first and hope for the best.
#define InitGoogle(argv0, argc, argv, remove_flags) \
google::ParseCommandLineFlags(argc, argv, remove_flags);
#else
// If we don't have the gflags package installed, these can only be
// changed at compile time.
#define DEFINE_int32(flag_name, default_value, description) \
static int FLAGS_ ## flag_name = default_value;
#define InitGoogle(argv0, argc, argv, remove_flags) ((void)(0)) #define InitGoogle(argv0, argc, argv, remove_flags) ((void)(0))
#endif
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
#include "zlib.h" #include "zlib.h"
#endif #endif

View File

@@ -28,6 +28,7 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <random> #include <random>
#include <string> #include <string>
@@ -41,23 +42,24 @@
#include "snappy.h" #include "snappy.h"
#include "snappy_test_data.h" #include "snappy_test_data.h"
DEFINE_int32(start_len, -1, SNAPPY_FLAG(int32_t, start_len, -1,
"Starting prefix size for testing (-1: just full file contents)"); "Starting prefix size for testing (-1: just full file contents)");
DEFINE_int32(end_len, -1, SNAPPY_FLAG(int32_t, end_len, -1,
"Starting prefix size for testing (-1: just full file contents)"); "Starting prefix size for testing (-1: just full file contents)");
DEFINE_int32(bytes, 10485760, SNAPPY_FLAG(int32_t, bytes, 10485760,
"How many bytes to compress/uncompress per file for timing"); "How many bytes to compress/uncompress per file for timing");
DEFINE_bool(zlib, true, SNAPPY_FLAG(bool, zlib, true,
"Run zlib compression (http://www.zlib.net)"); "Run zlib compression (http://www.zlib.net)");
DEFINE_bool(lzo, true, SNAPPY_FLAG(bool, lzo, true,
"Run LZO compression (http://www.oberhumer.com/opensource/lzo/)"); "Run LZO compression (http://www.oberhumer.com/opensource/lzo/)");
DEFINE_bool(lz4, true, "Run LZ4 compression (https://github.com/lz4/lz4)"); SNAPPY_FLAG(bool, lz4, true,
DEFINE_bool(snappy, true, "Run snappy compression"); "Run LZ4 compression (https://github.com/lz4/lz4)");
SNAPPY_FLAG(bool, snappy, true, "Run snappy compression");
DEFINE_bool(write_compressed, false, SNAPPY_FLAG(bool, write_compressed, false,
"Write compressed versions of each file to <file>.comp"); "Write compressed versions of each file to <file>.comp");
DEFINE_bool(write_uncompressed, false, SNAPPY_FLAG(bool, write_uncompressed, false,
"Write uncompressed versions of each file to <file>.uncomp"); "Write uncompressed versions of each file to <file>.uncomp");
namespace snappy { namespace snappy {
@@ -416,25 +418,27 @@ void MeasureFile(const char* fname) {
CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults())); CHECK_OK(file::GetContents(fname, &fullinput, file::Defaults()));
std::printf("%-40s :\n", fname); std::printf("%-40s :\n", fname);
int start_len = (absl::GetFlag(FLAGS_start_len) < 0) int start_len = (snappy::GetFlag(FLAGS_start_len) < 0)
? fullinput.size() ? fullinput.size()
: absl::GetFlag(FLAGS_start_len); : snappy::GetFlag(FLAGS_start_len);
int end_len = fullinput.size(); int end_len = fullinput.size();
if (absl::GetFlag(FLAGS_end_len) >= 0) { if (snappy::GetFlag(FLAGS_end_len) >= 0) {
end_len = std::min<int>(fullinput.size(), absl::GetFlag(FLAGS_end_len)); end_len = std::min<int>(fullinput.size(), snappy::GetFlag(FLAGS_end_len));
} }
for (int len = start_len; len <= end_len; ++len) { for (int len = start_len; len <= end_len; ++len) {
const char* const input = fullinput.data(); const char* const input = fullinput.data();
int repeats = (absl::GetFlag(FLAGS_bytes) + len) / (len + 1); int repeats = (snappy::GetFlag(FLAGS_bytes) + len) / (len + 1);
if (absl::GetFlag(FLAGS_zlib)) if (snappy::GetFlag(FLAGS_zlib))
Measure(input, len, ZLIB, repeats, 1024 << 10); Measure(input, len, ZLIB, repeats, 1024 << 10);
if (absl::GetFlag(FLAGS_lzo)) Measure(input, len, LZO, repeats, 1024 << 10); if (snappy::GetFlag(FLAGS_lzo))
if (absl::GetFlag(FLAGS_lz4)) Measure(input, len, LZ4, repeats, 1024 << 10); Measure(input, len, LZO, repeats, 1024 << 10);
if (absl::GetFlag(FLAGS_snappy)) if (snappy::GetFlag(FLAGS_lz4))
Measure(input, len, LZ4, repeats, 1024 << 10);
if (snappy::GetFlag(FLAGS_snappy))
Measure(input, len, SNAPPY, repeats, 4096 << 10); Measure(input, len, SNAPPY, repeats, 4096 << 10);
// For block-size based measurements // For block-size based measurements
if (0 && absl::GetFlag(FLAGS_snappy)) { if (0 && snappy::GetFlag(FLAGS_snappy)) {
Measure(input, len, SNAPPY, repeats, 8<<10); Measure(input, len, SNAPPY, repeats, 8<<10);
Measure(input, len, SNAPPY, repeats, 16<<10); Measure(input, len, SNAPPY, repeats, 16<<10);
Measure(input, len, SNAPPY, repeats, 32<<10); Measure(input, len, SNAPPY, repeats, 32<<10);
@@ -453,9 +457,9 @@ int main(int argc, char** argv) {
InitGoogle(argv[0], &argc, &argv, true); InitGoogle(argv[0], &argc, &argv, true);
for (int arg = 1; arg < argc; ++arg) { for (int arg = 1; arg < argc; ++arg) {
if (absl::GetFlag(FLAGS_write_compressed)) { if (snappy::GetFlag(FLAGS_write_compressed)) {
snappy::CompressFile(argv[arg]); snappy::CompressFile(argv[arg]);
} else if (absl::GetFlag(FLAGS_write_uncompressed)) { } else if (snappy::GetFlag(FLAGS_write_uncompressed)) {
snappy::UncompressFile(argv[arg]); snappy::UncompressFile(argv[arg]);
} else { } else {
snappy::MeasureFile(argv[arg]); snappy::MeasureFile(argv[arg]);

View File

@@ -43,7 +43,7 @@
#include "snappy.h" #include "snappy.h"
#include "snappy_test_data.h" #include "snappy_test_data.h"
DEFINE_bool(snappy_dump_decompression_table, false, SNAPPY_FLAG(bool, snappy_dump_decompression_table, false,
"If true, we print the decompression table during tests."); "If true, we print the decompression table during tests.");
namespace snappy { namespace snappy {
@@ -461,7 +461,7 @@ TEST(Snappy, MaxBlowup) {
} }
TEST(Snappy, RandomData) { TEST(Snappy, RandomData) {
std::minstd_rand0 rng(absl::GetFlag(FLAGS_test_random_seed)); std::minstd_rand0 rng(snappy::GetFlag(FLAGS_test_random_seed));
std::uniform_int_distribution<int> uniform_0_to_3(0, 3); std::uniform_int_distribution<int> uniform_0_to_3(0, 3);
std::uniform_int_distribution<int> uniform_0_to_8(0, 8); std::uniform_int_distribution<int> uniform_0_to_8(0, 8);
std::uniform_int_distribution<int> uniform_byte(0, 255); std::uniform_int_distribution<int> uniform_byte(0, 255);
@@ -834,7 +834,7 @@ TEST(Snappy, FindMatchLength) {
TEST(Snappy, FindMatchLengthRandom) { TEST(Snappy, FindMatchLengthRandom) {
constexpr int kNumTrials = 10000; constexpr int kNumTrials = 10000;
constexpr int kTypicalLength = 10; constexpr int kTypicalLength = 10;
std::minstd_rand0 rng(absl::GetFlag(FLAGS_test_random_seed)); std::minstd_rand0 rng(snappy::GetFlag(FLAGS_test_random_seed));
std::uniform_int_distribution<int> uniform_byte(0, 255); std::uniform_int_distribution<int> uniform_byte(0, 255);
std::bernoulli_distribution one_in_two(1.0 / 2); std::bernoulli_distribution one_in_two(1.0 / 2);
std::bernoulli_distribution one_in_typical_length(1.0 / kTypicalLength); std::bernoulli_distribution one_in_typical_length(1.0 / kTypicalLength);
@@ -938,7 +938,7 @@ TEST(Snappy, VerifyCharTable) {
EXPECT_NE(0xffff, dst[i]) << "Did not assign byte " << i; EXPECT_NE(0xffff, dst[i]) << "Did not assign byte " << i;
} }
if (absl::GetFlag(FLAGS_snappy_dump_decompression_table)) { if (snappy::GetFlag(FLAGS_snappy_dump_decompression_table)) {
std::printf("static const uint16_t char_table[256] = {\n "); std::printf("static const uint16_t char_table[256] = {\n ");
for (int i = 0; i < 256; ++i) { for (int i = 0; i < 256; ++i) {
std::printf("0x%04x%s", std::printf("0x%04x%s",