-
Notifications
You must be signed in to change notification settings - Fork 123
/
bmqp_crc32c.h
321 lines (300 loc) · 14.4 KB
/
bmqp_crc32c.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2016-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// bmqp_crc32c.h -*-C++-*-
#ifndef INCLUDED_BMQP_CRC32C
#define INCLUDED_BMQP_CRC32C
//@PURPOSE: Provide utilities to calculate the CRC32-C checksum of a dataset.
//
//@CLASSES:
// bmqp::Crc32c : calculates CRC32-C checksum
// bmqp::Crc32c_Impl: calculates CRC32-C checksum with alternative impl.
//
//@SEE_ALSO: bdlde::crc32
//
//@DESCRIPTION: This component defines a struct, 'bmqp::Crc32c', to calculate a
// CRC32-C checksum (a cyclic redundancy check, comprised of 32 bits, that uses
// the Castagnoli polynomial), using a hardware-accelerated implementation if
// supported or a software implementation otherwise. It additionally defines
// the struct 'bmqp::Crc32c_Impl' to expose alternative implementations that
// should not be used other than to test and benchmark. Note that a CRC32-C
// checksum is a strong and fast technique for determining whether or not a
// message was received without errors. Note, that you also need to check the
// number of bytes in the message, otherwise it is possible to add bytes at the
// end of what is being checked to get the checksums to come out the same.
// Also note that a CRC-32 checksum does not aid in error correction and is not
// naively useful in any sort of cryptography application.
//
/// Thread Safety
///-------------
// Thread safe.
//
/// Support for Hardware Acceleration
///---------------------------------
// Hardware-accelerated implementation is enabled at compile time when building
// on a supported architecture with a compatible compiler. In addition,
// runtime checks are performed to detect whether the running platform has the
// required hardware support:
//: o x86: SSE4.2 instructions are required
//: o sparc: runtime check is detected by the 'is_sparc_crc32c_avail' system
//: call
//
/// Performance
///-----------
// Below are performance comparisons of the hardware-accelerated and software
// implementations against various alternative implementations that compute a
// 32-bit CRC checksum. They were obtained on a Linux machine with the
// following CPU architecture:
//..
// $ lscpu
// Architecture: x86_64
// CPU op-mode(s): 32-bit, 64-bit
// Byte Order: Little Endian
// CPU(s): 40
// On-line CPU(s) list: 0-39
// Thread(s) per core: 2
// Core(s) per socket: 10
// Socket(s): 2
// NUMA node(s): 2
// Vendor ID: GenuineIntel
// CPU family: 6
// Model: 62
// Stepping: 4
// CPU MHz: 3001.000
// BogoMIPS: 5982.81
// Virtualization: VT-x
// L1d cache: 32K
// L1i cache: 32K
// L2 cache: 256K
// L3 cache: 25600K
// NUMA node0 CPU(s): 0-9,20-29
// NUMA node1 CPU(s): 10-19,30-39
//..
//
/// Throughput
/// - - - - -
//..
// Default (Hardware Acceleration)| 20.363 GB per second
// Software | 1.582 GB per second
// BDE 'bdlde::crc32' | 374.265 MB per second
//..
//
/// Calculation Time
/// - - - - - - - -
// In the tables below:
//: o !Time! is an average (in absolute nanoseconds) measured over a tight loop
//: of 100,000 iterations.
//:
//: o !Size! is the size (in bytes) of a 'char *' of random input. Note that it
//: uses IEC base2 notation (e.g. 1Ki = 2^10 = 1024, 1Mi = 2^20 = 1,048,576).
//
/// 64-bit Default (Hardware Acceleration) vs. BDE's 'bdlde::crc32'
/// - - - - - - - - - - - - - - - - - - - -
//..
// ===========================================================================
// | Size(B) | Def time(ns) | bdlde::crc32 time(ns)| Ratio(bdlde::crc32 / Def)
// ===========================================================================
// | 11| 9| 16| 1.783
// | 16| 9| 28| 3.089
// | 21| 10| 39| 3.932
// | 59| 13| 136| 10.323
// | 64| 12| 150| 11.675
// | 69| 13| 161| 11.669
// | 251| 30| 632| 20.509
// | 256| 30| 640| 20.660
// | 261| 37| 654| 17.415
// | 1019| 155| 2588| 16.698
// | 1 Ki| 45| 2602| 57.324
// | 1029| 50| 2614| 51.443
// | 4091| 299| 10436| 34.865
// | 4 Ki| 176| 10448| 59.040
// | 4101| 190| 10456| 54.763
// | 16379| 864| 41806| 48.366
// | 16 Ki| 724| 41829| 57.721
// | 16389| 754| 40699| 53.944
// | 64 Ki| 2858| 162340| 56.787
// | 256 Ki| 11925| 654410| 54.875
// | 1 Mi| 50937| 2664898| 52.317
// | 4 Mi| 198662| 10562189| 53.167
// | 16 Mi| 796534| 42570294| 53.444
// | 64 Mi| 9976933| 169051561| 16.944
//..
//
/// 64-bit Software (SW) vs. BDE's 'bdlde::crc32'
/// - - - - - - - - - - - - - -
//..
// ==========================================================================
// | Size(B) | SW time(ns) | bdlde::crc32 time(ns) | Ratio(bdlde::crc32 / SW)
// ==========================================================================
// | 11| 13| 17| 1.229
// | 16| 15| 29| 1.895
// | 21| 26| 39| 1.500
// | 59| 44| 137| 3.082
// | 64| 43| 150| 3.428
// | 69| 53| 161| 3.017
// | 251| 158| 629| 3.977
// | 256| 158| 640| 4.053
// | 261| 173| 654| 3.777
// | 1019| 621| 2592| 4.170
// | 1 Ki| 621| 2602| 4.185
// | 1029| 633| 2614| 4.128
// | 4091| 2456| 10435| 4.248
// | 4 Ki| 2457| 10447| 4.252
// | 4101| 2464| 10462| 4.246
// | 16379| 9795| 41820| 4.270
// | 16 Ki| 9798| 41838| 4.270
// | 16389| 9798| 41846| 4.271
// | 64 Ki| 39222| 167394| 4.268
// | 256 Ki| 156894| 665589| 4.242
// | 1 Mi| 629828| 2656343| 4.218
// | 4 Mi| 2575623| 10601903| 4.116
// | 16 Mi| 10085862| 42775171| 4.241
// | 64 Mi| 40705975| 169682572| 4.168
//..
//
/// Performance (sparc)
///-------------------
// Below are software vs hardware performance comparison for different sparc
// CPUs:
//..
// SPARC T5: 10.1465 times faster, at 710,138 iterations per second
// SPARC T7: 10.1007 times faster, at 808,625 iterations per second
// SPARC T8: 7.66392 times faster, at 1,013,937 iterations per second
//
/// Usage
///-----
// This section illustrates intended use of this component.
//
/// Example 1: Computing and updating a checksum
/// - - - - - - - - - - - - - - - - - - - - - -
// The following code illustrates how to calculate and update a CRC32-C
// checksum for a message over the course of building the full message.
//
// First, let's initialize the utility.
//..
// bmqp::Crc32c::initialize();
//..
// Now, compute a checksum.
//..
// // Prepare a message
// bsl::string message = "This is a test message.";
//
// // Generate a checksum for 'message'
// unsigned int checksum = bmqp::Crc32c::calculate(message.c_str(),
// message.size());
//..
// Finally, if we learn that our message has grown by another chunk and we want
// to compute the checksum of the original message plus the new chunk, let's
// update the checksum by using it as a starting point.
//..
// // New chunk
// bsl::string newChunk = "This is a chunk appended to original message";
// message += newChunk;
//
// // Update checksum using previous value as starting point
// checksum = bmqp::Crc32c::calculate(newChunk.c_str(),
// newChunk.size(),
// checksum);
//..
//
// BMQ
// BDE
#include <bdlbb_blob.h>
namespace BloombergLP {
namespace bmqp {
// =============
// struct Crc32c
// =============
/// This class provides runtime-efficient utilities to calculate a CRC32-C
/// checksum.
struct Crc32c {
public:
// TYPES
/// Signature of the function for the calculation of CRC32-C in the
/// default implementation methods (`calculate`) .
typedef unsigned int (*Crc32cFn)(const unsigned char* data,
unsigned int length,
unsigned int crc);
// CONSTANTS
/// CRC32-C value for a 0 length input. Note that a buffer with this
/// CRC32-C value need not be a 0 length input.
static const unsigned int k_NULL_CRC32C = 0U;
// CLASS METHODS
/// Initialize the utilities with a platform-dependent mechanism to
/// compute crc32c checksums. This method only needs to be called once
/// before any other method, but can be called multiple times.
static void initialize();
/// Return the CRC32-C value calculated for the specified `data` over
/// the specified `length` number of bytes, using the optionally
/// specified `crc` value as the starting point for the calculation.
/// This utilizes the default implementation as set by `initialize()`.
/// The behavior is undefined unless `initialize()` has been called
/// prior to calling this method at least once. Note that if `data` is
/// 0, then `length` also must be 0.
static unsigned int calculate(const void* data,
unsigned int length,
unsigned int crc = k_NULL_CRC32C);
/// Return the CRC32-C value calculated over the buffers in the
/// specified `blob` (in order of `blob.buffer(idx)` for increasing
/// values of `idx`), using the optionally specified `crc` value as the
/// starting point for the calculation. This utilizes the default
/// implementation as set by `initialize()`. The behavior is undefined
/// unless `initialize()` has been called prior to calling this method
/// at least once.
static unsigned int calculate(const bdlbb::Blob& blob,
unsigned int crc = k_NULL_CRC32C);
};
// ==================
// struct Crc32c_Impl
// ==================
/// This class provides alternative implementations of utilities to
/// calculate a CRC32-C checksum.
struct Crc32c_Impl {
public:
// CLASS METHODS
/// Return the CRC32-C value calculated for the specified `data` over
/// the specified `length` number of bytes, using the optionally
/// specified `crc` value as the starting point for the calculation.
/// This utilizes a portable software-based implementation to perform
/// the calculation. Note that if `data` is 0, then `length` must also
/// be 0.
static unsigned int
calculateSoftware(const void* data,
unsigned int length,
unsigned int crc = Crc32c::k_NULL_CRC32C);
/// Return the CRC32-C value calculated over all the buffers in the
/// specified `blob` (in order of `blob.buffer(idx)` for increasing
/// values of `idx`), using the optionally specified `crc` value as the
/// starting point for the calculation. This utilizes a portable
/// software-based implementation to perform the calculation.
static unsigned int
calculateSoftware(const bdlbb::Blob& blob,
unsigned int crc = Crc32c::k_NULL_CRC32C);
/// Return the CRC32-C value calculated for the specified `data` over
/// the specified `length` number of bytes, using the optionally
/// specified `crc` value as the starting point for the calculation.
/// This utilizes a hardware-based implementation that does not leverage
/// instruction level parallelism to perform the calculation (hence it
/// calculates the crc32c in "serial"). Note that this function will
/// fall back to the software version when running on unsupported
/// platforms. Also note that if `data` is 0, then `length` must also
/// be 0.
static unsigned int
calculateHardwareSerial(const void* data,
unsigned int length,
unsigned int crc = Crc32c::k_NULL_CRC32C);
};
} // close package namespace
} // close enterprise namespace
#endif