checksum_impl.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*-------------------------------------------------------------------------
  2. *
  3. * checksum_impl.h
  4. * Checksum implementation for data pages.
  5. *
  6. * This file exists for the benefit of external programs that may wish to
  7. * check Postgres page checksums. They can #include this to get the code
  8. * referenced by storage/checksum.h. (Note: you may need to redefine
  9. * Assert() as empty to compile this successfully externally.)
  10. *
  11. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  12. * Portions Copyright (c) 1994, Regents of the University of California
  13. *
  14. * src/include/storage/checksum_impl.h
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. /*
  19. * The algorithm used to checksum pages is chosen for very fast calculation.
  20. * Workloads where the database working set fits into OS file cache but not
  21. * into shared buffers can read in pages at a very fast pace and the checksum
  22. * algorithm itself can become the largest bottleneck.
  23. *
  24. * The checksum algorithm itself is based on the FNV-1a hash (FNV is shorthand
  25. * for Fowler/Noll/Vo). The primitive of a plain FNV-1a hash folds in data 1
  26. * byte at a time according to the formula:
  27. *
  28. * hash = (hash ^ value) * FNV_PRIME
  29. *
  30. * FNV-1a algorithm is described at http://www.isthe.com/chongo/tech/comp/fnv/
  31. *
  32. * PostgreSQL doesn't use FNV-1a hash directly because it has bad mixing of
  33. * high bits - high order bits in input data only affect high order bits in
  34. * output data. To resolve this we xor in the value prior to multiplication
  35. * shifted right by 17 bits. The number 17 was chosen because it doesn't
  36. * have common denominator with set bit positions in FNV_PRIME and empirically
  37. * provides the fastest mixing for high order bits of final iterations quickly
  38. * avalanche into lower positions. For performance reasons we choose to combine
  39. * 4 bytes at a time. The actual hash formula used as the basis is:
  40. *
  41. * hash = (hash ^ value) * FNV_PRIME ^ ((hash ^ value) >> 17)
  42. *
  43. * The main bottleneck in this calculation is the multiplication latency. To
  44. * hide the latency and to make use of SIMD parallelism multiple hash values
  45. * are calculated in parallel. The page is treated as a 32 column two
  46. * dimensional array of 32 bit values. Each column is aggregated separately
  47. * into a partial checksum. Each partial checksum uses a different initial
  48. * value (offset basis in FNV terminology). The initial values actually used
  49. * were chosen randomly, as the values themselves don't matter as much as that
  50. * they are different and don't match anything in real data. After initializing
  51. * partial checksums each value in the column is aggregated according to the
  52. * above formula. Finally two more iterations of the formula are performed with
  53. * value 0 to mix the bits of the last value added.
  54. *
  55. * The partial checksums are then folded together using xor to form a single
  56. * 32-bit checksum. The caller can safely reduce the value to 16 bits
  57. * using modulo 2^16-1. That will cause a very slight bias towards lower
  58. * values but this is not significant for the performance of the
  59. * checksum.
  60. *
  61. * The algorithm choice was based on what instructions are available in SIMD
  62. * instruction sets. This meant that a fast and good algorithm needed to use
  63. * multiplication as the main mixing operator. The simplest multiplication
  64. * based checksum primitive is the one used by FNV. The prime used is chosen
  65. * for good dispersion of values. It has no known simple patterns that result
  66. * in collisions. Test of 5-bit differentials of the primitive over 64bit keys
  67. * reveals no differentials with 3 or more values out of 100000 random keys
  68. * colliding. Avalanche test shows that only high order bits of the last word
  69. * have a bias. Tests of 1-4 uncorrelated bit errors, stray 0 and 0xFF bytes,
  70. * overwriting page from random position to end with 0 bytes, and overwriting
  71. * random segments of page with 0x00, 0xFF and random data all show optimal
  72. * 2e-16 false positive rate within margin of error.
  73. *
  74. * Vectorization of the algorithm requires 32bit x 32bit -> 32bit integer
  75. * multiplication instruction. As of 2013 the corresponding instruction is
  76. * available on x86 SSE4.1 extensions (pmulld) and ARM NEON (vmul.i32).
  77. * Vectorization requires a compiler to do the vectorization for us. For recent
  78. * GCC versions the flags -msse4.1 -funroll-loops -ftree-vectorize are enough
  79. * to achieve vectorization.
  80. *
  81. * The optimal amount of parallelism to use depends on CPU specific instruction
  82. * latency, SIMD instruction width, throughput and the amount of registers
  83. * available to hold intermediate state. Generally, more parallelism is better
  84. * up to the point that state doesn't fit in registers and extra load-store
  85. * instructions are needed to swap values in/out. The number chosen is a fixed
  86. * part of the algorithm because changing the parallelism changes the checksum
  87. * result.
  88. *
  89. * The parallelism number 32 was chosen based on the fact that it is the
  90. * largest state that fits into architecturally visible x86 SSE registers while
  91. * leaving some free registers for intermediate values. For future processors
  92. * with 256bit vector registers this will leave some performance on the table.
  93. * When vectorization is not available it might be beneficial to restructure
  94. * the computation to calculate a subset of the columns at a time and perform
  95. * multiple passes to avoid register spilling. This optimization opportunity
  96. * is not used. Current coding also assumes that the compiler has the ability
  97. * to unroll the inner loop to avoid loop overhead and minimize register
  98. * spilling. For less sophisticated compilers it might be beneficial to
  99. * manually unroll the inner loop.
  100. */
  101. #include "storage/bufpage.h"
  102. /* number of checksums to calculate in parallel */
  103. #define N_SUMS 32
  104. /* prime multiplier of FNV-1a hash */
  105. #define FNV_PRIME 16777619
  106. /*
  107. * Base offsets to initialize each of the parallel FNV hashes into a
  108. * different initial state.
  109. */
  110. static const uint32 checksumBaseOffsets[N_SUMS] = {
  111. 0x5B1F36E9, 0xB8525960, 0x02AB50AA, 0x1DE66D2A,
  112. 0x79FF467A, 0x9BB9F8A3, 0x217E7CD2, 0x83E13D2C,
  113. 0xF8D4474F, 0xE39EB970, 0x42C6AE16, 0x993216FA,
  114. 0x7B093B5D, 0x98DAFF3C, 0xF718902A, 0x0B1C9CDB,
  115. 0xE58F764B, 0x187636BC, 0x5D7B3BB1, 0xE73DE7DE,
  116. 0x92BEC979, 0xCCA6C0B2, 0x304A0979, 0x85AA43D4,
  117. 0x783125BB, 0x6CA8EAA2, 0xE407EAC6, 0x4B5CFC3E,
  118. 0x9FBF8C76, 0x15CA20BE, 0xF2CA9FD3, 0x959BD756
  119. };
  120. /*
  121. * Calculate one round of the checksum.
  122. */
  123. #define CHECKSUM_COMP(checksum, value) \
  124. do { \
  125. uint32 __tmp = (checksum) ^ (value); \
  126. (checksum) = __tmp * FNV_PRIME ^ (__tmp >> 17); \
  127. } while (0)
  128. /*
  129. * Block checksum algorithm. The data argument must be aligned on a 4-byte
  130. * boundary.
  131. */
  132. static uint32
  133. pg_checksum_block(char *data, uint32 size)
  134. {
  135. uint32 sums[N_SUMS];
  136. uint32 (*dataArr)[N_SUMS] = (uint32 (*)[N_SUMS]) data;
  137. uint32 result = 0;
  138. uint32 i,
  139. j;
  140. /* ensure that the size is compatible with the algorithm */
  141. Assert((size % (sizeof(uint32) * N_SUMS)) == 0);
  142. /* initialize partial checksums to their corresponding offsets */
  143. memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets));
  144. /* main checksum calculation */
  145. for (i = 0; i < size / sizeof(uint32) / N_SUMS; i++)
  146. for (j = 0; j < N_SUMS; j++)
  147. CHECKSUM_COMP(sums[j], dataArr[i][j]);
  148. /* finally add in two rounds of zeroes for additional mixing */
  149. for (i = 0; i < 2; i++)
  150. for (j = 0; j < N_SUMS; j++)
  151. CHECKSUM_COMP(sums[j], 0);
  152. /* xor fold partial checksums together */
  153. for (i = 0; i < N_SUMS; i++)
  154. result ^= sums[i];
  155. return result;
  156. }
  157. /*
  158. * Compute the checksum for a Postgres page. The page must be aligned on a
  159. * 4-byte boundary.
  160. *
  161. * The checksum includes the block number (to detect the case where a page is
  162. * somehow moved to a different location), the page header (excluding the
  163. * checksum itself), and the page data.
  164. */
  165. uint16
  166. pg_checksum_page(char *page, BlockNumber blkno)
  167. {
  168. PageHeader phdr = (PageHeader) page;
  169. uint16 save_checksum;
  170. uint32 checksum;
  171. /* We only calculate the checksum for properly-initialized pages */
  172. Assert(!PageIsNew(page));
  173. /*
  174. * Save pd_checksum and temporarily set it to zero, so that the checksum
  175. * calculation isn't affected by the old checksum stored on the page.
  176. * Restore it after, because actually updating the checksum is NOT part of
  177. * the API of this function.
  178. */
  179. save_checksum = phdr->pd_checksum;
  180. phdr->pd_checksum = 0;
  181. checksum = pg_checksum_block(page, BLCKSZ);
  182. phdr->pd_checksum = save_checksum;
  183. /* Mix in the block number to detect transposed pages */
  184. checksum ^= blkno;
  185. /*
  186. * Reduce to a uint16 (to fit in the pd_checksum field) with an offset of
  187. * one. That avoids checksums of zero, which seems like a good idea.
  188. */
  189. return (checksum % 65535) + 1;
  190. }