Sort #include lines
[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_continuation_hardware) {
125   if (folly::detail::crc32c_hw_supported()) {
126     testCRC32CContinuation(folly::detail::crc32c_hw);
127   } else {
128     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
129         " (not supported on this CPU)";
130   }
131 }
132
133 TEST(Checksum, crc32c_autodetect) {
134   testCRC32C(folly::crc32c);
135 }
136
137 TEST(Checksum, crc32c_continuation_autodetect) {
138   testCRC32CContinuation(folly::crc32c);
139 }
140
141 TEST(Checksum, crc32) {
142   if (folly::detail::crc32c_hw_supported()) {
143     // Just check that sw and hw match
144     for (auto expected : expectedResults) {
145       uint32_t sw_res =
146           folly::detail::crc32_sw(buffer + expected.offset, expected.length, 0);
147       uint32_t hw_res =
148           folly::detail::crc32_hw(buffer + expected.offset, expected.length, 0);
149       EXPECT_EQ(sw_res, hw_res);
150     }
151   } else {
152     LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
153                  << " (not supported on this CPU)";
154   }
155 }
156
157 TEST(Checksum, crc32_continuation) {
158   if (folly::detail::crc32c_hw_supported()) {
159     // Just check that sw and hw match
160     for (auto expected : expectedResults) {
161       auto halflen = expected.length / 2;
162       uint32_t sw_res =
163           folly::detail::crc32_sw(buffer + expected.offset, halflen, 0);
164       sw_res = folly::detail::crc32_sw(
165           buffer + expected.offset + halflen, halflen, sw_res);
166       uint32_t hw_res =
167           folly::detail::crc32_hw(buffer + expected.offset, halflen, 0);
168       hw_res = folly::detail::crc32_hw(
169           buffer + expected.offset + halflen, halflen, hw_res);
170       EXPECT_EQ(sw_res, hw_res);
171       uint32_t sw_res2 =
172           folly::detail::crc32_sw(buffer + expected.offset, halflen * 2, 0);
173       EXPECT_EQ(sw_res, sw_res2);
174       uint32_t hw_res2 =
175           folly::detail::crc32_hw(buffer + expected.offset, halflen * 2, 0);
176       EXPECT_EQ(hw_res, hw_res2);
177     }
178   } else {
179     LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
180                  << " (not supported on this CPU)";
181   }
182 }
183
184 TEST(Checksum, crc32_type) {
185   // Test that crc32_type matches boost::crc_32_type
186   testMatchesBoost32Type();
187 }
188
189 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
190   if (folly::detail::crc32c_hw_supported()) {
191     uint32_t checksum;
192     for (unsigned long i = 0; i < iters; i++) {
193       checksum = folly::detail::crc32c_hw(buffer, blockSize);
194       folly::doNotOptimizeAway(checksum);
195     }
196   } else {
197     LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
198         " (not supported on this CPU)";
199   }
200 }
201
202 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
203   uint32_t checksum;
204   for (unsigned long i = 0; i < iters; i++) {
205     checksum = folly::detail::crc32c_sw(buffer, blockSize);
206     folly::doNotOptimizeAway(checksum);
207   }
208 }
209
210 void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize) {
211   if (folly::detail::crc32_hw_supported()) {
212     uint32_t checksum;
213     for (unsigned long i = 0; i < iters; i++) {
214       checksum = folly::detail::crc32_hw(buffer, blockSize);
215       folly::doNotOptimizeAway(checksum);
216     }
217   } else {
218     LOG(WARNING) << "skipping hardware-accelerated CRC-32 benchmarks"
219                  << " (not supported on this CPU)";
220   }
221 }
222
223 void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize) {
224   uint32_t checksum;
225   for (unsigned long i = 0; i < iters; i++) {
226     checksum = folly::detail::crc32_sw(buffer, blockSize);
227     folly::doNotOptimizeAway(checksum);
228   }
229 }
230
231 // This test fits easily in the L1 cache on modern server processors,
232 // and thus it mainly measures the speed of the checksum computation.
233 BENCHMARK(crc32c_hardware_1KB_block, iters) {
234   benchmarkHardwareCRC32C(iters, 1024);
235 }
236
237 BENCHMARK(crc32c_software_1KB_block, iters) {
238   benchmarkSoftwareCRC32C(iters, 1024);
239 }
240
241 BENCHMARK(crc32_hardware_1KB_block, iters) {
242   benchmarkHardwareCRC32(iters, 1024);
243 }
244
245 BENCHMARK(crc32_software_1KB_block, iters) {
246   benchmarkSoftwareCRC32(iters, 1024);
247 }
248
249 BENCHMARK_DRAW_LINE();
250
251 // This test is too big for the L1 cache but fits in L2
252 BENCHMARK(crc32c_hardware_64KB_block, iters) {
253   benchmarkHardwareCRC32C(iters, 64 * 1024);
254 }
255
256 BENCHMARK(crc32c_software_64KB_block, iters) {
257   benchmarkSoftwareCRC32C(iters, 64 * 1024);
258 }
259
260 BENCHMARK(crc32_hardware_64KB_block, iters) {
261   benchmarkHardwareCRC32(iters, 64 * 1024);
262 }
263
264 BENCHMARK(crc32_software_64KB_block, iters) {
265   benchmarkSoftwareCRC32(iters, 64 * 1024);
266 }
267
268 BENCHMARK_DRAW_LINE();
269
270 // This test is too big for the L2 cache but fits in L3
271 BENCHMARK(crc32c_hardware_512KB_block, iters) {
272   benchmarkHardwareCRC32C(iters, 512 * 1024);
273 }
274
275 BENCHMARK(crc32c_software_512KB_block, iters) {
276   benchmarkSoftwareCRC32C(iters, 512 * 1024);
277 }
278
279 BENCHMARK(crc32_hardware_512KB_block, iters) {
280   benchmarkHardwareCRC32(iters, 512 * 1024);
281 }
282
283 BENCHMARK(crc32_software_512KB_block, iters) {
284   benchmarkSoftwareCRC32(iters, 512 * 1024);
285 }
286
287 int main(int argc, char** argv) {
288   testing::InitGoogleTest(&argc, argv);
289   gflags::ParseCommandLineFlags(&argc, &argv, true);
290
291   // Populate a buffer with a deterministic pattern
292   // on which to compute checksums
293   const uint8_t* src = buffer;
294   uint64_t* dst = (uint64_t*)buffer;
295   const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
296   *dst++ = 0;
297   while (dst < end) {
298     *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
299     src += sizeof(uint64_t);
300   }
301
302   auto ret = RUN_ALL_TESTS();
303   if (!ret && FLAGS_benchmark) {
304     folly::runBenchmarks();
305   }
306   return ret;
307 }