2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Checksum.h>
20 #include <folly/Benchmark.h>
21 #include <folly/Hash.h>
22 #include <folly/detail/ChecksumDetail.h>
23 #include <folly/portability/GFlags.h>
24 #include <folly/portability/GTest.h>
27 const unsigned int BUFFER_SIZE = 512 * 1024 * sizeof(uint64_t);
28 uint8_t buffer[BUFFER_SIZE];
30 struct ExpectedResult {
36 ExpectedResult expectedResults[] = {
39 // Small aligned inputs to test special cases in SIMD implementations
47 // Small unaligned inputs
50 { 11, 3, 1479707779 },
51 { 12, 4, 2237687071 },
52 { 13, 5, 4063855784 },
53 { 14, 6, 2553454047 },
54 { 15, 7, 1349220140 },
55 // Larger inputs to test leftover chunks at the end of aligned blocks
58 { 8, 10, 2447068514 },
61 { 8, 13, 1411837737 },
62 { 8, 14, 2614515001 },
63 { 8, 15, 3579076296 },
64 { 8, 16, 2897079161 },
67 { 0, BUFFER_SIZE, 2096790750 },
68 { 1, BUFFER_SIZE / 2, 3854797577 },
72 std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
73 for (auto expected : expectedResults) {
74 uint32_t result = impl(buffer + expected.offset, expected.length, ~0U);
75 EXPECT_EQ(expected.crc32c, result);
79 void testCRC32CContinuation(
80 std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
81 for (auto expected : expectedResults) {
82 size_t partialLength = expected.length / 2;
83 uint32_t partialChecksum = impl(
84 buffer + expected.offset, partialLength, ~0U);
85 uint32_t result = impl(
86 buffer + expected.offset + partialLength,
87 expected.length - partialLength, partialChecksum);
88 EXPECT_EQ(expected.crc32c, result);
94 TEST(Checksum, crc32c_software) {
95 testCRC32C(folly::detail::crc32c_sw);
98 TEST(Checksum, crc32c_continuation_software) {
99 testCRC32CContinuation(folly::detail::crc32c_sw);
103 TEST(Checksum, crc32c_hardware) {
104 if (folly::detail::crc32c_hw_supported()) {
105 testCRC32C(folly::detail::crc32c_hw);
107 LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
108 " (not supported on this CPU)";
112 TEST(Checksum, crc32c_continuation_hardware) {
113 if (folly::detail::crc32c_hw_supported()) {
114 testCRC32CContinuation(folly::detail::crc32c_hw);
116 LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
117 " (not supported on this CPU)";
121 TEST(Checksum, crc32c_autodetect) {
122 testCRC32C(folly::crc32c);
125 TEST(Checksum, crc32c_continuation_autodetect) {
126 testCRC32CContinuation(folly::crc32c);
129 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
130 if (folly::detail::crc32c_hw_supported()) {
132 for (unsigned long i = 0; i < iters; i++) {
133 checksum = folly::detail::crc32c_hw(buffer, blockSize);
134 folly::doNotOptimizeAway(checksum);
137 LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
138 " (not supported on this CPU)";
142 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
144 for (unsigned long i = 0; i < iters; i++) {
145 checksum = folly::detail::crc32c_sw(buffer, blockSize);
146 folly::doNotOptimizeAway(checksum);
150 // This test fits easily in the L1 cache on modern server processors,
151 // and thus it mainly measures the speed of the checksum computation.
152 BENCHMARK(crc32c_hardware_1KB_block, iters) {
153 benchmarkHardwareCRC32C(iters, 1024);
156 BENCHMARK(crc32c_software_1KB_block, iters) {
157 benchmarkSoftwareCRC32C(iters, 1024);
160 BENCHMARK_DRAW_LINE();
162 // This test is too big for the L1 cache but fits in L2
163 BENCHMARK(crc32c_hardware_64KB_block, iters) {
164 benchmarkHardwareCRC32C(iters, 64 * 1024);
167 BENCHMARK(crc32c_software_64KB_block, iters) {
168 benchmarkSoftwareCRC32C(iters, 64 * 1024);
171 BENCHMARK_DRAW_LINE();
173 // This test is too big for the L2 cache but fits in L3
174 BENCHMARK(crc32c_hardware_512KB_block, iters) {
175 benchmarkHardwareCRC32C(iters, 512 * 1024);
178 BENCHMARK(crc32c_software_512KB_block, iters) {
179 benchmarkSoftwareCRC32C(iters, 512 * 1024);
183 int main(int argc, char** argv) {
184 testing::InitGoogleTest(&argc, argv);
185 gflags::ParseCommandLineFlags(&argc, &argv, true);
187 // Populate a buffer with a deterministic pattern
188 // on which to compute checksums
189 const uint8_t* src = buffer;
190 uint64_t* dst = (uint64_t*)buffer;
191 const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
194 *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
195 src += sizeof(uint64_t);
198 auto ret = RUN_ALL_TESTS();
199 if (!ret && FLAGS_benchmark) {
200 folly::runBenchmarks();