16489dce970410fa9d54e974945f00671dede894
[folly.git] / folly / test / ChecksumTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/Checksum.h>
18
19 #include <boost/crc.hpp>
20
21 #include <folly/Benchmark.h>
22 #include <folly/Hash.h>
23 #include <folly/detail/ChecksumDetail.h>
24 #include <folly/portability/GFlags.h>
25 #include <folly/portability/GTest.h>
26
27 namespace {
28 const unsigned int BUFFER_SIZE = 512 * 1024 * sizeof(uint64_t);
29 uint8_t buffer[BUFFER_SIZE];
30
31 struct ExpectedResult {
32   size_t offset;
33   size_t length;
34   uint32_t crc32c;
35 };
36
37 ExpectedResult expectedResults[] = {
38     // Zero-byte input
39     { 0, 0, ~0U },
40     // Small aligned inputs to test special cases in SIMD implementations
41     { 8, 1, 1543413366 },
42     { 8, 2, 523493126 },
43     { 8, 3, 1560427360 },
44     { 8, 4, 3422504776 },
45     { 8, 5, 447841138 },
46     { 8, 6, 3910050499 },
47     { 8, 7, 3346241981 },
48     // Small unaligned inputs
49     { 9, 1, 3855826643 },
50     { 10, 2, 560880875 },
51     { 11, 3, 1479707779 },
52     { 12, 4, 2237687071 },
53     { 13, 5, 4063855784 },
54     { 14, 6, 2553454047 },
55     { 15, 7, 1349220140 },
56     // Larger inputs to test leftover chunks at the end of aligned blocks
57     { 8, 8, 627613930 },
58     { 8, 9, 2105929409 },
59     { 8, 10, 2447068514 },
60     { 8, 11, 863807079 },
61     { 8, 12, 292050879 },
62     { 8, 13, 1411837737 },
63     { 8, 14, 2614515001 },
64     { 8, 15, 3579076296 },
65     { 8, 16, 2897079161 },
66     { 8, 17, 675168386 },
67     // Much larger inputs
68     { 0, BUFFER_SIZE, 2096790750 },
69     { 1, BUFFER_SIZE / 2, 3854797577 },
70 };
71
72 void testCRC32C(
73     std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
74   for (auto expected : expectedResults) {
75     uint32_t result = impl(buffer + expected.offset, expected.length, ~0U);
76     EXPECT_EQ(expected.crc32c, result);
77   }
78 }
79
80 void testCRC32CContinuation(
81     std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
82   for (auto expected : expectedResults) {
83     size_t partialLength = expected.length / 2;
84     uint32_t partialChecksum = impl(
85         buffer + expected.offset, partialLength, ~0U);
86     uint32_t result = impl(
87         buffer + expected.offset + partialLength,
88         expected.length - partialLength, partialChecksum);
89     EXPECT_EQ(expected.crc32c, result);
90   }
91 }
92
93 void testMatchesBoost32Type() {
94   for (auto expected : expectedResults) {
95     boost::crc_32_type result;
96     result.process_bytes(buffer + expected.offset, expected.length);
97     const uint32_t boostResult = result.checksum();
98     const uint32_t follyResult =
99         folly::crc32_type(buffer + expected.offset, expected.length);
100     EXPECT_EQ(follyResult, boostResult);
101   }
102 }
103
104 } // namespace
105
106 TEST(Checksum, crc32c_software) {
107   testCRC32C(folly::detail::crc32c_sw);
108 }
109
110 TEST(Checksum, crc32c_continuation_software) {
111   testCRC32CContinuation(folly::detail::crc32c_sw);
112 }
113
114
115 TEST(Checksum, crc32c_hardware) {
116   if (folly::detail::crc32c_hw_supported()) {
117     testCRC32C(folly::detail::crc32c_hw);
118   } else {
119     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
120         " (not supported on this CPU)";
121   }
122 }
123
124 TEST(Checksum, crc32c_hardware_eq) {
125   if (folly::detail::crc32c_hw_supported()) {
126     for (int i = 0; i < 1000; i++) {
127       auto sw = folly::detail::crc32c_sw(buffer, i, 0);
128       auto hw = folly::detail::crc32c_hw(buffer, i, 0);
129       EXPECT_EQ(sw, hw);
130     }
131   } else {
132     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests"
133                  << " (not supported on this CPU)";
134   }
135 }
136
137 TEST(Checksum, crc32c_continuation_hardware) {
138   if (folly::detail::crc32c_hw_supported()) {
139     testCRC32CContinuation(folly::detail::crc32c_hw);
140   } else {
141     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
142         " (not supported on this CPU)";
143   }
144 }
145
146 TEST(Checksum, crc32c_autodetect) {
147   testCRC32C(folly::crc32c);
148 }
149
150 TEST(Checksum, crc32c_continuation_autodetect) {
151   testCRC32CContinuation(folly::crc32c);
152 }
153
154 TEST(Checksum, crc32) {
155   if (folly::detail::crc32c_hw_supported()) {
156     // Just check that sw and hw match
157     for (auto expected : expectedResults) {
158       uint32_t sw_res =
159           folly::detail::crc32_sw(buffer + expected.offset, expected.length, 0);
160       uint32_t hw_res =
161           folly::detail::crc32_hw(buffer + expected.offset, expected.length, 0);
162       EXPECT_EQ(sw_res, hw_res);
163     }
164   } else {
165     LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
166                  << " (not supported on this CPU)";
167   }
168 }
169
170 TEST(Checksum, crc32_continuation) {
171   if (folly::detail::crc32c_hw_supported()) {
172     // Just check that sw and hw match
173     for (auto expected : expectedResults) {
174       auto halflen = expected.length / 2;
175       uint32_t sw_res =
176           folly::detail::crc32_sw(buffer + expected.offset, halflen, 0);
177       sw_res = folly::detail::crc32_sw(
178           buffer + expected.offset + halflen, halflen, sw_res);
179       uint32_t hw_res =
180           folly::detail::crc32_hw(buffer + expected.offset, halflen, 0);
181       hw_res = folly::detail::crc32_hw(
182           buffer + expected.offset + halflen, halflen, hw_res);
183       EXPECT_EQ(sw_res, hw_res);
184       uint32_t sw_res2 =
185           folly::detail::crc32_sw(buffer + expected.offset, halflen * 2, 0);
186       EXPECT_EQ(sw_res, sw_res2);
187       uint32_t hw_res2 =
188           folly::detail::crc32_hw(buffer + expected.offset, halflen * 2, 0);
189       EXPECT_EQ(hw_res, hw_res2);
190     }
191   } else {
192     LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
193                  << " (not supported on this CPU)";
194   }
195 }
196
197 TEST(Checksum, crc32_type) {
198   // Test that crc32_type matches boost::crc_32_type
199   testMatchesBoost32Type();
200 }
201
202 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
203   if (folly::detail::crc32c_hw_supported()) {
204     uint32_t checksum;
205     for (unsigned long i = 0; i < iters; i++) {
206       checksum = folly::detail::crc32c_hw(buffer, blockSize);
207       folly::doNotOptimizeAway(checksum);
208     }
209   } else {
210     LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
211         " (not supported on this CPU)";
212   }
213 }
214
215 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
216   uint32_t checksum;
217   for (unsigned long i = 0; i < iters; i++) {
218     checksum = folly::detail::crc32c_sw(buffer, blockSize);
219     folly::doNotOptimizeAway(checksum);
220   }
221 }
222
223 void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize) {
224   if (folly::detail::crc32_hw_supported()) {
225     uint32_t checksum;
226     for (unsigned long i = 0; i < iters; i++) {
227       checksum = folly::detail::crc32_hw(buffer, blockSize);
228       folly::doNotOptimizeAway(checksum);
229     }
230   } else {
231     LOG(WARNING) << "skipping hardware-accelerated CRC-32 benchmarks"
232                  << " (not supported on this CPU)";
233   }
234 }
235
236 void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize) {
237   uint32_t checksum;
238   for (unsigned long i = 0; i < iters; i++) {
239     checksum = folly::detail::crc32_sw(buffer, blockSize);
240     folly::doNotOptimizeAway(checksum);
241   }
242 }
243
244 // This test fits easily in the L1 cache on modern server processors,
245 // and thus it mainly measures the speed of the checksum computation.
246 BENCHMARK(crc32c_hardware_1KB_block, iters) {
247   benchmarkHardwareCRC32C(iters, 1024);
248 }
249
250 BENCHMARK(crc32c_software_1KB_block, iters) {
251   benchmarkSoftwareCRC32C(iters, 1024);
252 }
253
254 BENCHMARK(crc32_hardware_1KB_block, iters) {
255   benchmarkHardwareCRC32(iters, 1024);
256 }
257
258 BENCHMARK(crc32_software_1KB_block, iters) {
259   benchmarkSoftwareCRC32(iters, 1024);
260 }
261
262 BENCHMARK_DRAW_LINE();
263
264 // This test is too big for the L1 cache but fits in L2
265 BENCHMARK(crc32c_hardware_64KB_block, iters) {
266   benchmarkHardwareCRC32C(iters, 64 * 1024);
267 }
268
269 BENCHMARK(crc32c_software_64KB_block, iters) {
270   benchmarkSoftwareCRC32C(iters, 64 * 1024);
271 }
272
273 BENCHMARK(crc32_hardware_64KB_block, iters) {
274   benchmarkHardwareCRC32(iters, 64 * 1024);
275 }
276
277 BENCHMARK(crc32_software_64KB_block, iters) {
278   benchmarkSoftwareCRC32(iters, 64 * 1024);
279 }
280
281 BENCHMARK_DRAW_LINE();
282
283 // This test is too big for the L2 cache but fits in L3
284 BENCHMARK(crc32c_hardware_512KB_block, iters) {
285   benchmarkHardwareCRC32C(iters, 512 * 1024);
286 }
287
288 BENCHMARK(crc32c_software_512KB_block, iters) {
289   benchmarkSoftwareCRC32C(iters, 512 * 1024);
290 }
291
292 BENCHMARK(crc32_hardware_512KB_block, iters) {
293   benchmarkHardwareCRC32(iters, 512 * 1024);
294 }
295
296 BENCHMARK(crc32_software_512KB_block, iters) {
297   benchmarkSoftwareCRC32(iters, 512 * 1024);
298 }
299
300 int main(int argc, char** argv) {
301   testing::InitGoogleTest(&argc, argv);
302   gflags::ParseCommandLineFlags(&argc, &argv, true);
303
304   // Populate a buffer with a deterministic pattern
305   // on which to compute checksums
306   const uint8_t* src = buffer;
307   uint64_t* dst = (uint64_t*)buffer;
308   const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
309   *dst++ = 0;
310   while (dst < end) {
311     *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
312     src += sizeof(uint64_t);
313   }
314
315   auto ret = RUN_ALL_TESTS();
316   if (!ret && FLAGS_benchmark) {
317     folly::runBenchmarks();
318   }
319   return ret;
320 }