0c4747d7868903820fc12e467c0a5bfd880a92f9
[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 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
130   if (folly::detail::crc32c_hw_supported()) {
131     uint32_t checksum;
132     for (unsigned long i = 0; i < iters; i++) {
133       checksum = folly::detail::crc32c_hw(buffer, blockSize);
134       folly::doNotOptimizeAway(checksum);
135     }
136   } else {
137     LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
138         " (not supported on this CPU)";
139   }
140 }
141
142 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
143   uint32_t checksum;
144   for (unsigned long i = 0; i < iters; i++) {
145     checksum = folly::detail::crc32c_sw(buffer, blockSize);
146     folly::doNotOptimizeAway(checksum);
147   }
148 }
149
150 // This test fits easily in the L1 cache on modern server processors,
151 // and thus it mainly measures the speed of the checksum computation.
152 BENCHMARK(crc32c_hardware_1KB_block, iters) {
153   benchmarkHardwareCRC32C(iters, 1024);
154 }
155
156 BENCHMARK(crc32c_software_1KB_block, iters) {
157   benchmarkSoftwareCRC32C(iters, 1024);
158 }
159
160 BENCHMARK_DRAW_LINE();
161
162 // This test is too big for the L1 cache but fits in L2
163 BENCHMARK(crc32c_hardware_64KB_block, iters) {
164   benchmarkHardwareCRC32C(iters, 64 * 1024);
165 }
166
167 BENCHMARK(crc32c_software_64KB_block, iters) {
168   benchmarkSoftwareCRC32C(iters, 64 * 1024);
169 }
170
171 BENCHMARK_DRAW_LINE();
172
173 // This test is too big for the L2 cache but fits in L3
174 BENCHMARK(crc32c_hardware_512KB_block, iters) {
175   benchmarkHardwareCRC32C(iters, 512 * 1024);
176 }
177
178 BENCHMARK(crc32c_software_512KB_block, iters) {
179   benchmarkSoftwareCRC32C(iters, 512 * 1024);
180 }
181
182
183 int main(int argc, char** argv) {
184   testing::InitGoogleTest(&argc, argv);
185   gflags::ParseCommandLineFlags(&argc, &argv, true);
186
187   // Populate a buffer with a deterministic pattern
188   // on which to compute checksums
189   const uint8_t* src = buffer;
190   uint64_t* dst = (uint64_t*)buffer;
191   const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
192   *dst++ = 0;
193   while (dst < end) {
194     *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
195     src += sizeof(uint64_t);
196   }
197
198   auto ret = RUN_ALL_TESTS();
199   if (!ret && FLAGS_benchmark) {
200     folly::runBenchmarks();
201   }
202   return ret;
203 }