2 * Copyright 2017 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 TEST(Checksum, crc32) {
130 if (folly::detail::crc32c_hw_supported()) {
131 // Just check that sw and hw match
132 for (auto expected : expectedResults) {
134 folly::detail::crc32_sw(buffer + expected.offset, expected.length, 0);
136 folly::detail::crc32_hw(buffer + expected.offset, expected.length, 0);
137 EXPECT_EQ(sw_res, hw_res);
140 LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
141 << " (not supported on this CPU)";
145 TEST(Checksum, crc32_continuation) {
146 if (folly::detail::crc32c_hw_supported()) {
147 // Just check that sw and hw match
148 for (auto expected : expectedResults) {
149 auto halflen = expected.length / 2;
151 folly::detail::crc32_sw(buffer + expected.offset, halflen, 0);
152 sw_res = folly::detail::crc32_sw(
153 buffer + expected.offset + halflen, halflen, sw_res);
155 folly::detail::crc32_hw(buffer + expected.offset, halflen, 0);
156 hw_res = folly::detail::crc32_hw(
157 buffer + expected.offset + halflen, halflen, hw_res);
158 EXPECT_EQ(sw_res, hw_res);
160 folly::detail::crc32_sw(buffer + expected.offset, halflen * 2, 0);
161 EXPECT_EQ(sw_res, sw_res2);
163 folly::detail::crc32_hw(buffer + expected.offset, halflen * 2, 0);
164 EXPECT_EQ(hw_res, hw_res2);
167 LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
168 << " (not supported on this CPU)";
172 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
173 if (folly::detail::crc32c_hw_supported()) {
175 for (unsigned long i = 0; i < iters; i++) {
176 checksum = folly::detail::crc32c_hw(buffer, blockSize);
177 folly::doNotOptimizeAway(checksum);
180 LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
181 " (not supported on this CPU)";
185 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
187 for (unsigned long i = 0; i < iters; i++) {
188 checksum = folly::detail::crc32c_sw(buffer, blockSize);
189 folly::doNotOptimizeAway(checksum);
193 void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize) {
194 if (folly::detail::crc32_hw_supported()) {
196 for (unsigned long i = 0; i < iters; i++) {
197 checksum = folly::detail::crc32_hw(buffer, blockSize);
198 folly::doNotOptimizeAway(checksum);
201 LOG(WARNING) << "skipping hardware-accelerated CRC-32 benchmarks"
202 << " (not supported on this CPU)";
206 void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize) {
208 for (unsigned long i = 0; i < iters; i++) {
209 checksum = folly::detail::crc32_sw(buffer, blockSize);
210 folly::doNotOptimizeAway(checksum);
214 // This test fits easily in the L1 cache on modern server processors,
215 // and thus it mainly measures the speed of the checksum computation.
216 BENCHMARK(crc32c_hardware_1KB_block, iters) {
217 benchmarkHardwareCRC32C(iters, 1024);
220 BENCHMARK(crc32c_software_1KB_block, iters) {
221 benchmarkSoftwareCRC32C(iters, 1024);
224 BENCHMARK(crc32_hardware_1KB_block, iters) {
225 benchmarkHardwareCRC32(iters, 1024);
228 BENCHMARK(crc32_software_1KB_block, iters) {
229 benchmarkSoftwareCRC32(iters, 1024);
232 BENCHMARK_DRAW_LINE();
234 // This test is too big for the L1 cache but fits in L2
235 BENCHMARK(crc32c_hardware_64KB_block, iters) {
236 benchmarkHardwareCRC32C(iters, 64 * 1024);
239 BENCHMARK(crc32c_software_64KB_block, iters) {
240 benchmarkSoftwareCRC32C(iters, 64 * 1024);
243 BENCHMARK(crc32_hardware_64KB_block, iters) {
244 benchmarkHardwareCRC32(iters, 64 * 1024);
247 BENCHMARK(crc32_software_64KB_block, iters) {
248 benchmarkSoftwareCRC32(iters, 64 * 1024);
251 BENCHMARK_DRAW_LINE();
253 // This test is too big for the L2 cache but fits in L3
254 BENCHMARK(crc32c_hardware_512KB_block, iters) {
255 benchmarkHardwareCRC32C(iters, 512 * 1024);
258 BENCHMARK(crc32c_software_512KB_block, iters) {
259 benchmarkSoftwareCRC32C(iters, 512 * 1024);
262 BENCHMARK(crc32_hardware_512KB_block, iters) {
263 benchmarkHardwareCRC32(iters, 512 * 1024);
266 BENCHMARK(crc32_software_512KB_block, iters) {
267 benchmarkSoftwareCRC32(iters, 512 * 1024);
270 int main(int argc, char** argv) {
271 testing::InitGoogleTest(&argc, argv);
272 gflags::ParseCommandLineFlags(&argc, &argv, true);
274 // Populate a buffer with a deterministic pattern
275 // on which to compute checksums
276 const uint8_t* src = buffer;
277 uint64_t* dst = (uint64_t*)buffer;
278 const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
281 *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
282 src += sizeof(uint64_t);
285 auto ret = RUN_ALL_TESTS();
286 if (!ret && FLAGS_benchmark) {
287 folly::runBenchmarks();