e563d3f91dc117b3e7926e9ef26874d0ccd37265
[folly.git] / folly / test / ChecksumTest.cpp
1 /*
2  * Copyright 2014 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 #include <gflags/gflags.h>
19 #include <gtest/gtest.h>
20 #include <folly/Benchmark.h>
21 #include <folly/Hash.h>
22 #include <folly/detail/ChecksumDetail.h>
23
24 namespace {
25 const unsigned int BUFFER_SIZE = 512 * 1024 * sizeof(uint64_t);
26 uint8_t buffer[BUFFER_SIZE];
27
28 struct ExpectedResult {
29   size_t offset;
30   size_t length;
31   uint32_t crc32c;
32 };
33
34 ExpectedResult expectedResults[] = {
35     // Zero-byte input
36     { 0, 0, ~0U },
37     // Small aligned inputs to test special cases in SIMD implementations
38     { 8, 1, 1543413366 },
39     { 8, 2, 523493126 },
40     { 8, 3, 1560427360 },
41     { 8, 4, 3422504776 },
42     { 8, 5, 447841138 },
43     { 8, 6, 3910050499 },
44     { 8, 7, 3346241981 },
45     // Small unaligned inputs
46     { 9, 1, 3855826643 },
47     { 10, 2, 560880875 },
48     { 11, 3, 1479707779 },
49     { 12, 4, 2237687071 },
50     { 13, 5, 4063855784 },
51     { 14, 6, 2553454047 },
52     { 15, 7, 1349220140 },
53     // Larger inputs to test leftover chunks at the end of aligned blocks
54     { 8, 8, 627613930 },
55     { 8, 9, 2105929409 },
56     { 8, 10, 2447068514 },
57     { 8, 11, 863807079 },
58     { 8, 12, 292050879 },
59     { 8, 13, 1411837737 },
60     { 8, 14, 2614515001 },
61     { 8, 15, 3579076296 },
62     { 8, 16, 2897079161 },
63     { 8, 17, 675168386 },
64     // Much larger inputs
65     { 0, BUFFER_SIZE, 2096790750 },
66     { 1, BUFFER_SIZE / 2, 3854797577 },
67 };
68
69 void testCRC32C(
70     std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
71   for (auto expected : expectedResults) {
72     uint32_t result = impl(buffer + expected.offset, expected.length, ~0U);
73     EXPECT_EQ(expected.crc32c, result);
74   }
75 }
76
77 void testCRC32CContinuation(
78     std::function<uint32_t(const uint8_t*, size_t, uint32_t)> impl) {
79   for (auto expected : expectedResults) {
80     size_t partialLength = expected.length / 2;
81     uint32_t partialChecksum = impl(
82         buffer + expected.offset, partialLength, ~0U);
83     uint32_t result = impl(
84         buffer + expected.offset + partialLength,
85         expected.length - partialLength, partialChecksum);
86     EXPECT_EQ(expected.crc32c, result);
87   }
88 }
89
90 } // namespace
91
92 TEST(Checksum, crc32c_software) {
93   testCRC32C(folly::detail::crc32c_sw);
94 }
95
96 TEST(Checksum, crc32c_continuation_software) {
97   testCRC32CContinuation(folly::detail::crc32c_sw);
98 }
99
100
101 TEST(Checksum, crc32c_hardware) {
102   if (folly::detail::crc32c_hw_supported()) {
103     testCRC32C(folly::detail::crc32c_hw);
104   } else {
105     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
106         " (not supported on this CPU)";
107   }
108 }
109
110 TEST(Checksum, crc32c_continuation_hardware) {
111   if (folly::detail::crc32c_hw_supported()) {
112     testCRC32CContinuation(folly::detail::crc32c_hw);
113   } else {
114     LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests" <<
115         " (not supported on this CPU)";
116   }
117 }
118
119 TEST(Checksum, crc32c_autodetect) {
120   testCRC32C(folly::crc32c);
121 }
122
123 TEST(Checksum, crc32c_continuation_autodetect) {
124   testCRC32CContinuation(folly::crc32c);
125 }
126
127 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
128   if (folly::detail::crc32c_hw_supported()) {
129     uint32_t checksum;
130     for (unsigned long i = 0; i < iters; i++) {
131       checksum = folly::detail::crc32c_hw(buffer, blockSize);
132       folly::doNotOptimizeAway(checksum);
133     }
134   } else {
135     LOG(WARNING) << "skipping hardware-accelerated CRC-32C benchmarks" <<
136         " (not supported on this CPU)";
137   }
138 }
139
140 void benchmarkSoftwareCRC32C(unsigned long iters, size_t blockSize) {
141   uint32_t checksum;
142   for (unsigned long i = 0; i < iters; i++) {
143     checksum = folly::detail::crc32c_sw(buffer, blockSize);
144     folly::doNotOptimizeAway(checksum);
145   }
146 }
147
148 // This test fits easily in the L1 cache on modern server processors,
149 // and thus it mainly measures the speed of the checksum computation.
150 BENCHMARK(crc32c_hardware_1KB_block, iters) {
151   benchmarkHardwareCRC32C(iters, 1024);
152 }
153
154 BENCHMARK(crc32c_software_1KB_block, iters) {
155   benchmarkSoftwareCRC32C(iters, 1024);
156 }
157
158 BENCHMARK_DRAW_LINE();
159
160 // This test is too big for the L1 cache but fits in L2
161 BENCHMARK(crc32c_hardware_64KB_block, iters) {
162   benchmarkHardwareCRC32C(iters, 64 * 1024);
163 }
164
165 BENCHMARK(crc32c_software_64KB_block, iters) {
166   benchmarkSoftwareCRC32C(iters, 64 * 1024);
167 }
168
169 BENCHMARK_DRAW_LINE();
170
171 // This test is too big for the L2 cache but fits in L3
172 BENCHMARK(crc32c_hardware_512KB_block, iters) {
173   benchmarkHardwareCRC32C(iters, 512 * 1024);
174 }
175
176 BENCHMARK(crc32c_software_512KB_block, iters) {
177   benchmarkSoftwareCRC32C(iters, 512 * 1024);
178 }
179
180
181 int main(int argc, char** argv) {
182   testing::InitGoogleTest(&argc, argv);
183   google::ParseCommandLineFlags(&argc, &argv, true);
184
185   // Populate a buffer with a deterministic pattern
186   // on which to compute checksums
187   const uint8_t* src = buffer;
188   uint64_t* dst = (uint64_t*)buffer;
189   const uint64_t* end = (const uint64_t*)(buffer + BUFFER_SIZE);
190   *dst++ = 0;
191   while (dst < end) {
192     *dst++ = folly::hash::fnv64_buf((const char*)src, sizeof(uint64_t));
193     src += sizeof(uint64_t);
194   }
195
196   auto ret = RUN_ALL_TESTS();
197   if (!ret && FLAGS_benchmark) {
198     folly::runBenchmarks();
199   }
200   return ret;
201 }