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