Add hardware crc impl
[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
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>
25
26 namespace {
27 const unsigned int BUFFER_SIZE = 512 * 1024 * sizeof(uint64_t);
28 uint8_t buffer[BUFFER_SIZE];
29
30 struct ExpectedResult {
31   size_t offset;
32   size_t length;
33   uint32_t crc32c;
34 };
35
36 ExpectedResult expectedResults[] = {
37     // Zero-byte input
38     { 0, 0, ~0U },
39     // Small aligned inputs to test special cases in SIMD implementations
40     { 8, 1, 1543413366 },
41     { 8, 2, 523493126 },
42     { 8, 3, 1560427360 },
43     { 8, 4, 3422504776 },
44     { 8, 5, 447841138 },
45     { 8, 6, 3910050499 },
46     { 8, 7, 3346241981 },
47     // Small unaligned inputs
48     { 9, 1, 3855826643 },
49     { 10, 2, 560880875 },
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
56     { 8, 8, 627613930 },
57     { 8, 9, 2105929409 },
58     { 8, 10, 2447068514 },
59     { 8, 11, 863807079 },
60     { 8, 12, 292050879 },
61     { 8, 13, 1411837737 },
62     { 8, 14, 2614515001 },
63     { 8, 15, 3579076296 },
64     { 8, 16, 2897079161 },
65     { 8, 17, 675168386 },
66     // Much larger inputs
67     { 0, BUFFER_SIZE, 2096790750 },
68     { 1, BUFFER_SIZE / 2, 3854797577 },
69 };
70
71 void testCRC32C(
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);
76   }
77 }
78
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);
89   }
90 }
91
92 } // namespace
93
94 TEST(Checksum, crc32c_software) {
95   testCRC32C(folly::detail::crc32c_sw);
96 }
97
98 TEST(Checksum, crc32c_continuation_software) {
99   testCRC32CContinuation(folly::detail::crc32c_sw);
100 }
101
102
103 TEST(Checksum, crc32c_hardware) {
104   if (folly::detail::crc32c_hw_supported()) {
105     testCRC32C(folly::detail::crc32c_hw);
106   } else {
107     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
108         " (not supported on this CPU)";
109   }
110 }
111
112 TEST(Checksum, crc32c_continuation_hardware) {
113   if (folly::detail::crc32c_hw_supported()) {
114     testCRC32CContinuation(folly::detail::crc32c_hw);
115   } else {
116     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
117         " (not supported on this CPU)";
118   }
119 }
120
121 TEST(Checksum, crc32c_autodetect) {
122   testCRC32C(folly::crc32c);
123 }
124
125 TEST(Checksum, crc32c_continuation_autodetect) {
126   testCRC32CContinuation(folly::crc32c);
127 }
128
129 TEST(Checksum, crc32) {
130   if (folly::detail::crc32c_hw_supported()) {
131     // Just check that sw and hw match
132     for (auto expected : expectedResults) {
133       uint32_t sw_res =
134           folly::detail::crc32_sw(buffer + expected.offset, expected.length, 0);
135       uint32_t hw_res =
136           folly::detail::crc32_hw(buffer + expected.offset, expected.length, 0);
137       EXPECT_EQ(sw_res, hw_res);
138     }
139   } else {
140     LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
141                  << " (not supported on this CPU)";
142   }
143 }
144
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;
150       uint32_t sw_res =
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);
154       uint32_t hw_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);
159       uint32_t sw_res2 =
160           folly::detail::crc32_sw(buffer + expected.offset, halflen * 2, 0);
161       EXPECT_EQ(sw_res, sw_res2);
162       uint32_t hw_res2 =
163           folly::detail::crc32_hw(buffer + expected.offset, halflen * 2, 0);
164       EXPECT_EQ(hw_res, hw_res2);
165     }
166   } else {
167     LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
168                  << " (not supported on this CPU)";
169   }
170 }
171
172 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
173   if (folly::detail::crc32c_hw_supported()) {
174     uint32_t checksum;
175     for (unsigned long i = 0; i < iters; i++) {
176       checksum = folly::detail::crc32c_hw(buffer, blockSize);
177       folly::doNotOptimizeAway(checksum);
178     }
179   } else {
180     LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
181         " (not supported on this CPU)";
182   }
183 }
184
185 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
186   uint32_t checksum;
187   for (unsigned long i = 0; i < iters; i++) {
188     checksum = folly::detail::crc32c_sw(buffer, blockSize);
189     folly::doNotOptimizeAway(checksum);
190   }
191 }
192
193 void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize) {
194   if (folly::detail::crc32_hw_supported()) {
195     uint32_t checksum;
196     for (unsigned long i = 0; i < iters; i++) {
197       checksum = folly::detail::crc32_hw(buffer, blockSize);
198       folly::doNotOptimizeAway(checksum);
199     }
200   } else {
201     LOG(WARNING) << "skipping hardware-accelerated CRC-32 benchmarks"
202                  << " (not supported on this CPU)";
203   }
204 }
205
206 void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize) {
207   uint32_t checksum;
208   for (unsigned long i = 0; i < iters; i++) {
209     checksum = folly::detail::crc32_sw(buffer, blockSize);
210     folly::doNotOptimizeAway(checksum);
211   }
212 }
213
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);
218 }
219
220 BENCHMARK(crc32c_software_1KB_block, iters) {
221   benchmarkSoftwareCRC32C(iters, 1024);
222 }
223
224 BENCHMARK(crc32_hardware_1KB_block, iters) {
225   benchmarkHardwareCRC32(iters, 1024);
226 }
227
228 BENCHMARK(crc32_software_1KB_block, iters) {
229   benchmarkSoftwareCRC32(iters, 1024);
230 }
231
232 BENCHMARK_DRAW_LINE();
233
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);
237 }
238
239 BENCHMARK(crc32c_software_64KB_block, iters) {
240   benchmarkSoftwareCRC32C(iters, 64 * 1024);
241 }
242
243 BENCHMARK(crc32_hardware_64KB_block, iters) {
244   benchmarkHardwareCRC32(iters, 64 * 1024);
245 }
246
247 BENCHMARK(crc32_software_64KB_block, iters) {
248   benchmarkSoftwareCRC32(iters, 64 * 1024);
249 }
250
251 BENCHMARK_DRAW_LINE();
252
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);
256 }
257
258 BENCHMARK(crc32c_software_512KB_block, iters) {
259   benchmarkSoftwareCRC32C(iters, 512 * 1024);
260 }
261
262 BENCHMARK(crc32_hardware_512KB_block, iters) {
263   benchmarkHardwareCRC32(iters, 512 * 1024);
264 }
265
266 BENCHMARK(crc32_software_512KB_block, iters) {
267   benchmarkSoftwareCRC32(iters, 512 * 1024);
268 }
269
270 int main(int argc, char** argv) {
271   testing::InitGoogleTest(&argc, argv);
272   gflags::ParseCommandLineFlags(&argc, &argv, true);
273
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);
279   *dst++ = 0;
280   while (dst < end) {
281     *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
282     src += sizeof(uint64_t);
283   }
284
285   auto ret = RUN_ALL_TESTS();
286   if (!ret && FLAGS_benchmark) {
287     folly::runBenchmarks();
288   }
289   return ret;
290 }