proxygen
ChecksumTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-present 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/hash/Checksum.h>
18 
19 #include <boost/crc.hpp>
20 
21 #include <folly/Benchmark.h>
22 #include <folly/Random.h>
23 #include <folly/hash/Hash.h>
27 
28 namespace {
29 const unsigned int BUFFER_SIZE = 512 * 1024 * sizeof(uint64_t);
30 uint8_t buffer[BUFFER_SIZE];
31 
32 struct ExpectedResult {
33  size_t offset;
34  size_t length;
36 };
37 
38 ExpectedResult expectedResults[] = {
39  // Zero-byte input
40  {0, 0, ~0U},
41  // Small aligned inputs to test special cases in SIMD implementations
42  {8, 1, 1543413366},
43  {8, 2, 523493126},
44  {8, 3, 1560427360},
45  {8, 4, 3422504776},
46  {8, 5, 447841138},
47  {8, 6, 3910050499},
48  {8, 7, 3346241981},
49  // Small unaligned inputs
50  {9, 1, 3855826643},
51  {10, 2, 560880875},
52  {11, 3, 1479707779},
53  {12, 4, 2237687071},
54  {13, 5, 4063855784},
55  {14, 6, 2553454047},
56  {15, 7, 1349220140},
57  // Larger inputs to test leftover chunks at the end of aligned blocks
58  {8, 8, 627613930},
59  {8, 9, 2105929409},
60  {8, 10, 2447068514},
61  {8, 11, 863807079},
62  {8, 12, 292050879},
63  {8, 13, 1411837737},
64  {8, 14, 2614515001},
65  {8, 15, 3579076296},
66  {8, 16, 2897079161},
67  {8, 17, 675168386},
68  // Much larger inputs
69  {0, BUFFER_SIZE, 2096790750},
70  {1, BUFFER_SIZE / 2, 3854797577},
71 };
72 
73 void testCRC32C(
74  std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
75  for (auto expected : expectedResults) {
76  uint32_t result = impl(buffer + expected.offset, expected.length, ~0U);
77  EXPECT_EQ(expected.crc32c, result);
78  }
79 }
80 
81 void testCRC32CContinuation(
82  std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
83  for (auto expected : expectedResults) {
84  size_t partialLength = expected.length / 2;
85  uint32_t partialChecksum =
86  impl(buffer + expected.offset, partialLength, ~0U);
87  uint32_t result = impl(
88  buffer + expected.offset + partialLength,
89  expected.length - partialLength,
90  partialChecksum);
91  EXPECT_EQ(expected.crc32c, result);
92  }
93 }
94 
95 void testMatchesBoost32Type() {
96  for (auto expected : expectedResults) {
97  boost::crc_32_type result;
98  result.process_bytes(buffer + expected.offset, expected.length);
99  const uint32_t boostResult = result.checksum();
100  const uint32_t follyResult =
101  folly::crc32_type(buffer + expected.offset, expected.length);
102  EXPECT_EQ(follyResult, boostResult);
103  }
104 }
105 
106 } // namespace
107 
108 TEST(Checksum, crc32c_software) {
109  testCRC32C(folly::detail::crc32c_sw);
110 }
111 
112 TEST(Checksum, crc32c_continuation_software) {
113  testCRC32CContinuation(folly::detail::crc32c_sw);
114 }
115 
116 TEST(Checksum, crc32c_hardware) {
118  testCRC32C(folly::detail::crc32c_hw);
119  } else {
120  LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests"
121  << " (not supported on this CPU)";
122  }
123 }
124 
125 TEST(Checksum, crc32c_hardware_eq) {
127  for (int i = 0; i < 1000; i++) {
128  auto sw = folly::detail::crc32c_sw(buffer, i, 0);
129  auto hw = folly::detail::crc32c_hw(buffer, i, 0);
130  EXPECT_EQ(sw, hw);
131  }
132  } else {
133  LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests"
134  << " (not supported on this CPU)";
135  }
136 }
137 
138 TEST(Checksum, crc32c_continuation_hardware) {
140  testCRC32CContinuation(folly::detail::crc32c_hw);
141  } else {
142  LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests"
143  << " (not supported on this CPU)";
144  }
145 }
146 
147 TEST(Checksum, crc32c_autodetect) {
148  testCRC32C(folly::crc32c);
149 }
150 
151 TEST(Checksum, crc32c_continuation_autodetect) {
152  testCRC32CContinuation(folly::crc32c);
153 }
154 
155 TEST(Checksum, crc32) {
157  // Just check that sw and hw match
158  for (auto expected : expectedResults) {
159  uint32_t sw_res =
160  folly::detail::crc32_sw(buffer + expected.offset, expected.length, 0);
161  uint32_t hw_res =
162  folly::detail::crc32_hw(buffer + expected.offset, expected.length, 0);
163  EXPECT_EQ(sw_res, hw_res);
164  }
165  } else {
166  LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
167  << " (not supported on this CPU)";
168  }
169 }
170 
171 TEST(Checksum, crc32_continuation) {
173  // Just check that sw and hw match
174  for (auto expected : expectedResults) {
175  auto halflen = expected.length / 2;
176  uint32_t sw_res =
177  folly::detail::crc32_sw(buffer + expected.offset, halflen, 0);
178  sw_res = folly::detail::crc32_sw(
179  buffer + expected.offset + halflen, halflen, sw_res);
180  uint32_t hw_res =
181  folly::detail::crc32_hw(buffer + expected.offset, halflen, 0);
182  hw_res = folly::detail::crc32_hw(
183  buffer + expected.offset + halflen, halflen, hw_res);
184  EXPECT_EQ(sw_res, hw_res);
185  uint32_t sw_res2 =
186  folly::detail::crc32_sw(buffer + expected.offset, halflen * 2, 0);
187  EXPECT_EQ(sw_res, sw_res2);
188  uint32_t hw_res2 =
189  folly::detail::crc32_hw(buffer + expected.offset, halflen * 2, 0);
190  EXPECT_EQ(hw_res, hw_res2);
191  }
192  } else {
193  LOG(WARNING) << "skipping hardware-accelerated CRC-32 tests"
194  << " (not supported on this CPU)";
195  }
196 }
197 
198 TEST(Checksum, crc32_type) {
199  // Test that crc32_type matches boost::crc_32_type
200  testMatchesBoost32Type();
201 }
202 
203 TEST(Checksum, crc32_combine) {
204  for (size_t totlen = 1024; totlen < BUFFER_SIZE; totlen += BUFFER_SIZE / 8) {
205  auto mid = folly::Random::rand64(0, totlen);
206  auto crc1 = folly::crc32(&buffer[0], mid, 0);
207  auto crc2 = folly::crc32(&buffer[mid], totlen - mid, 0);
208  auto crcfull = folly::crc32(&buffer[0], totlen, 0);
209  auto combined = folly::crc32_combine(crc1, crc2, totlen - mid);
210  EXPECT_EQ(combined, crcfull);
211  }
212 }
213 
214 TEST(Checksum, crc32c_combine) {
215  for (size_t totlen = 1024; totlen < BUFFER_SIZE; totlen += BUFFER_SIZE / 8) {
216  auto mid = folly::Random::rand64(0, totlen);
217  auto crc1 = folly::crc32c(&buffer[0], mid, 0);
218  auto crc2 = folly::crc32c(&buffer[mid], totlen - mid, 0);
219  auto crcfull = folly::crc32c(&buffer[0], totlen, 0);
220  auto combined = folly::crc32c_combine(crc1, crc2, totlen - mid);
221  EXPECT_EQ(combined, crcfull);
222  }
223 }
224 
225 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
227  uint32_t checksum;
228  for (unsigned long i = 0; i < iters; i++) {
229  checksum = folly::detail::crc32c_hw(buffer, blockSize);
230  folly::doNotOptimizeAway(checksum);
231  }
232  } else {
233  LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks"
234  << " (not supported on this CPU)";
235  }
236 }
237 
238 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
239  uint32_t checksum;
240  for (unsigned long i = 0; i < iters; i++) {
241  checksum = folly::detail::crc32c_sw(buffer, blockSize);
242  folly::doNotOptimizeAway(checksum);
243  }
244 }
245 
246 void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize) {
248  uint32_t checksum;
249  for (unsigned long i = 0; i < iters; i++) {
250  checksum = folly::detail::crc32_hw(buffer, blockSize);
251  folly::doNotOptimizeAway(checksum);
252  }
253  } else {
254  LOG(WARNING) << "skipping hardware-accelerated CRC-32 benchmarks"
255  << " (not supported on this CPU)";
256  }
257 }
258 
259 void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize) {
260  uint32_t checksum;
261  for (unsigned long i = 0; i < iters; i++) {
262  checksum = folly::detail::crc32_sw(buffer, blockSize);
263  folly::doNotOptimizeAway(checksum);
264  }
265 }
266 
267 void benchmarkCombineHardwareCrc32(unsigned long iters, size_t blockSize) {
268  // Arbitrarily chosen checksums
269  uint32_t checksum1 = 0xEDB88320;
270  uint32_t checksum2 = 0x82F63B78;
271  uint32_t result;
272  for (unsigned long i = 0; i < iters; i++) {
273  result = folly::crc32_combine(checksum1, checksum2, blockSize);
274  folly::doNotOptimizeAway(result);
275  }
276 }
277 
278 void benchmarkCombineSoftwareLinear(unsigned long iters, size_t blockSize) {
279  // Arbitrarily chosen checksums
280  std::vector<uint8_t> zbuffer;
281  zbuffer.reserve(blockSize);
282  memset(zbuffer.data(), 0, blockSize);
283  uint32_t checksum1 = 0xEDB88320;
284  uint32_t checksum2 = 0x82F63B78;
285  uint32_t result;
286  for (unsigned long i = 0; i < iters; i++) {
287  result = folly::crc32c(zbuffer.data(), blockSize, checksum1);
288  result ^= checksum2;
289  folly::doNotOptimizeAway(result);
290  }
291 }
292 
293 void benchmarkCombineHardwareCrc32c(unsigned long iters, size_t blockSize) {
294  // Arbitrarily chosen checksums
295  uint32_t checksum1 = 0xEDB88320;
296  uint32_t checksum2 = 0x82F63B78;
297  uint32_t result;
298  for (unsigned long i = 0; i < iters; i++) {
299  result = folly::crc32c_combine(checksum1, checksum2, blockSize);
300  folly::doNotOptimizeAway(result);
301  }
302 }
303 
304 // This test fits easily in the L1 cache on modern server processors,
305 // and thus it mainly measures the speed of the checksum computation.
306 BENCHMARK(crc32c_hardware_1KB_block, iters) {
307  benchmarkHardwareCRC32C(iters, 1024);
308 }
309 
310 BENCHMARK(crc32c_software_1KB_block, iters) {
311  benchmarkSoftwareCRC32C(iters, 1024);
312 }
313 
314 BENCHMARK(crc32_hardware_1KB_block, iters) {
315  benchmarkHardwareCRC32(iters, 1024);
316 }
317 
318 BENCHMARK(crc32_software_1KB_block, iters) {
319  benchmarkSoftwareCRC32(iters, 1024);
320 }
321 
323 
324 // This test is too big for the L1 cache but fits in L2
325 BENCHMARK(crc32c_hardware_64KB_block, iters) {
326  benchmarkHardwareCRC32C(iters, 64 * 1024);
327 }
328 
329 BENCHMARK(crc32c_software_64KB_block, iters) {
330  benchmarkSoftwareCRC32C(iters, 64 * 1024);
331 }
332 
333 BENCHMARK(crc32_hardware_64KB_block, iters) {
334  benchmarkHardwareCRC32(iters, 64 * 1024);
335 }
336 
337 BENCHMARK(crc32_software_64KB_block, iters) {
338  benchmarkSoftwareCRC32(iters, 64 * 1024);
339 }
340 
342 
343 // This test is too big for the L2 cache but fits in L3
344 BENCHMARK(crc32c_hardware_512KB_block, iters) {
345  benchmarkHardwareCRC32C(iters, 512 * 1024);
346 }
347 
348 BENCHMARK(crc32c_software_512KB_block, iters) {
349  benchmarkSoftwareCRC32C(iters, 512 * 1024);
350 }
351 
352 BENCHMARK(crc32_hardware_512KB_block, iters) {
353  benchmarkHardwareCRC32(iters, 512 * 1024);
354 }
355 
356 BENCHMARK(crc32_software_512KB_block, iters) {
357  benchmarkSoftwareCRC32(iters, 512 * 1024);
358 }
359 
361 
362 BENCHMARK(crc32_combine_linear_512KB_block, iters) {
363  benchmarkCombineSoftwareLinear(iters, 512 * 1024);
364 }
365 
366 BENCHMARK(crc32_combine_512KB_block, iters) {
367  benchmarkCombineHardwareCrc32(iters, 512 * 1024);
368 }
369 
370 BENCHMARK(crc32c_combine_512KB_block, iters) {
371  benchmarkCombineHardwareCrc32c(iters, 512 * 1024);
372 }
373 
374 int main(int argc, char** argv) {
375  testing::InitGoogleTest(&argc, argv);
376  gflags::ParseCommandLineFlags(&argc, &argv, true);
377 
378  // Populate a buffer with a deterministic pattern
379  // on which to compute checksums
380  const uint8_t* src = buffer;
381  uint64_t* dst = (uint64_t*)buffer;
382  const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
383  *dst++ = 0;
384  while (dst < end) {
385  *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
386  src += sizeof(uint64_t);
387  }
388 
389  auto ret = RUN_ALL_TESTS();
390  if (!ret && FLAGS_benchmark) {
392  }
393  return ret;
394 }
uint32_t crc32_type(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
Definition: Checksum.cpp:145
std::vector< uint8_t > buffer(kBufferSize+16)
uint64_t fnv64_buf(const void *buf, size_t n, uint64_t hash=FNV_64_HASH_START) noexcept
Definition: Hash.h:199
BENCHMARK(crc32c_hardware_1KB_block, iters)
void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize)
void benchmarkCombineHardwareCrc32(unsigned long iters, size_t blockSize)
uint32_t crc32c(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
Definition: Checksum.cpp:128
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
BENCHMARK_DRAW_LINE()
uint32_t crc32_sw(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
Definition: Checksum.cpp:121
int main(int argc, char **argv)
void runBenchmarks()
Definition: Benchmark.cpp:456
void benchmarkSoftwareCRC32(unsigned long iters, size_t blockSize)
void benchmarkCombineHardwareCrc32c(unsigned long iters, size_t blockSize)
char ** argv
uint32_t crc32(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
Definition: Checksum.cpp:136
uint32_t crc32c_hw(const uint8_t *data, size_t nbytes, uint32_t startingChecksum=~0U)
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
bool crc32_hw_supported()
Definition: Checksum.cpp:87
void benchmarkCombineSoftwareLinear(unsigned long iters, size_t blockSize)
TEST(Checksum, crc32c_software)
uint32_t crc32c_sw(const uint8_t *data, size_t nbytes, uint32_t startingChecksum)
Definition: Checksum.cpp:115
uint32_t crc32c_combine(uint32_t crc1, uint32_t crc2, size_t crc2len)
Definition: Checksum.cpp:164
uint32_t crc32_hw(const uint8_t *, size_t, uint32_t)
Definition: Checksum.cpp:76
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
uint32_t crc32_combine(uint32_t crc1, uint32_t crc2, size_t crc2len)
Definition: Checksum.cpp:149
static uint64_t rand64()
Definition: Random.h:263
void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
bool crc32c_hw_supported()
Definition: Checksum.cpp:83
void benchmarkHardwareCRC32(unsigned long iters, size_t blockSize)