drbd_vli.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. -*- linux-c -*-
  3. drbd_receiver.c
  4. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  5. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  6. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  7. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  8. drbd is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12. drbd is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with drbd; see the file COPYING. If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #ifndef _DRBD_VLI_H
  21. #define _DRBD_VLI_H
  22. /*
  23. * At a granularity of 4KiB storage represented per bit,
  24. * and stroage sizes of several TiB,
  25. * and possibly small-bandwidth replication,
  26. * the bitmap transfer time can take much too long,
  27. * if transmitted in plain text.
  28. *
  29. * We try to reduce the transferred bitmap information
  30. * by encoding runlengths of bit polarity.
  31. *
  32. * We never actually need to encode a "zero" (runlengths are positive).
  33. * But then we have to store the value of the first bit.
  34. * The first bit of information thus shall encode if the first runlength
  35. * gives the number of set or unset bits.
  36. *
  37. * We assume that large areas are either completely set or unset,
  38. * which gives good compression with any runlength method,
  39. * even when encoding the runlength as fixed size 32bit/64bit integers.
  40. *
  41. * Still, there may be areas where the polarity flips every few bits,
  42. * and encoding the runlength sequence of those areas with fix size
  43. * integers would be much worse than plaintext.
  44. *
  45. * We want to encode small runlength values with minimum code length,
  46. * while still being able to encode a Huge run of all zeros.
  47. *
  48. * Thus we need a Variable Length Integer encoding, VLI.
  49. *
  50. * For some cases, we produce more code bits than plaintext input.
  51. * We need to send incompressible chunks as plaintext, skip over them
  52. * and then see if the next chunk compresses better.
  53. *
  54. * We don't care too much about "excellent" compression ratio for large
  55. * runlengths (all set/all clear): whether we achieve a factor of 100
  56. * or 1000 is not that much of an issue.
  57. * We do not want to waste too much on short runlengths in the "noisy"
  58. * parts of the bitmap, though.
  59. *
  60. * There are endless variants of VLI, we experimented with:
  61. * * simple byte-based
  62. * * various bit based with different code word length.
  63. *
  64. * To avoid yet an other configuration parameter (choice of bitmap compression
  65. * algorithm) which was difficult to explain and tune, we just chose the one
  66. * variant that turned out best in all test cases.
  67. * Based on real world usage patterns, with device sizes ranging from a few GiB
  68. * to several TiB, file server/mailserver/webserver/mysql/postgress,
  69. * mostly idle to really busy, the all time winner (though sometimes only
  70. * marginally better) is:
  71. */
  72. /*
  73. * encoding is "visualised" as
  74. * __little endian__ bitstream, least significant bit first (left most)
  75. *
  76. * this particular encoding is chosen so that the prefix code
  77. * starts as unary encoding the level, then modified so that
  78. * 10 levels can be described in 8bit, with minimal overhead
  79. * for the smaller levels.
  80. *
  81. * Number of data bits follow fibonacci sequence, with the exception of the
  82. * last level (+1 data bit, so it makes 64bit total). The only worse code when
  83. * encoding bit polarity runlength is 1 plain bits => 2 code bits.
  84. prefix data bits max val Nº data bits
  85. 0 x 0x2 1
  86. 10 x 0x4 1
  87. 110 xx 0x8 2
  88. 1110 xxx 0x10 3
  89. 11110 xxx xx 0x30 5
  90. 111110 xx xxxxxx 0x130 8
  91. 11111100 xxxxxxxx xxxxx 0x2130 13
  92. 11111110 xxxxxxxx xxxxxxxx xxxxx 0x202130 21
  93. 11111101 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xx 0x400202130 34
  94. 11111111 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx 56
  95. * maximum encodable value: 0x100000400202130 == 2**56 + some */
  96. /* compression "table":
  97. transmitted x 0.29
  98. as plaintext x ........................
  99. x ........................
  100. x ........................
  101. x 0.59 0.21........................
  102. x ........................................................
  103. x .. c ...................................................
  104. x 0.44.. o ...................................................
  105. x .......... d ...................................................
  106. x .......... e ...................................................
  107. X............. ...................................................
  108. x.............. b ...................................................
  109. 2.0x............... i ...................................................
  110. #X................ t ...................................................
  111. #................. s ........................... plain bits ..........
  112. -+-----------------------------------------------------------------------
  113. 1 16 32 64
  114. */
  115. /* LEVEL: (total bits, prefix bits, prefix value),
  116. * sorted ascending by number of total bits.
  117. * The rest of the code table is calculated at compiletime from this. */
  118. /* fibonacci data 1, 1, ... */
  119. #define VLI_L_1_1() do { \
  120. LEVEL( 2, 1, 0x00); \
  121. LEVEL( 3, 2, 0x01); \
  122. LEVEL( 5, 3, 0x03); \
  123. LEVEL( 7, 4, 0x07); \
  124. LEVEL(10, 5, 0x0f); \
  125. LEVEL(14, 6, 0x1f); \
  126. LEVEL(21, 8, 0x3f); \
  127. LEVEL(29, 8, 0x7f); \
  128. LEVEL(42, 8, 0xbf); \
  129. LEVEL(64, 8, 0xff); \
  130. } while (0)
  131. /* finds a suitable level to decode the least significant part of in.
  132. * returns number of bits consumed.
  133. *
  134. * BUG() for bad input, as that would mean a buggy code table. */
  135. static inline int vli_decode_bits(u64 *out, const u64 in)
  136. {
  137. u64 adj = 1;
  138. #define LEVEL(t,b,v) \
  139. do { \
  140. if ((in & ((1 << b) -1)) == v) { \
  141. *out = ((in & ((~0ULL) >> (64-t))) >> b) + adj; \
  142. return t; \
  143. } \
  144. adj += 1ULL << (t - b); \
  145. } while (0)
  146. VLI_L_1_1();
  147. /* NOT REACHED, if VLI_LEVELS code table is defined properly */
  148. BUG();
  149. #undef LEVEL
  150. }
  151. /* return number of code bits needed,
  152. * or negative error number */
  153. static inline int __vli_encode_bits(u64 *out, const u64 in)
  154. {
  155. u64 max = 0;
  156. u64 adj = 1;
  157. if (in == 0)
  158. return -EINVAL;
  159. #define LEVEL(t,b,v) do { \
  160. max += 1ULL << (t - b); \
  161. if (in <= max) { \
  162. if (out) \
  163. *out = ((in - adj) << b) | v; \
  164. return t; \
  165. } \
  166. adj = max + 1; \
  167. } while (0)
  168. VLI_L_1_1();
  169. return -EOVERFLOW;
  170. #undef LEVEL
  171. }
  172. #undef VLI_L_1_1
  173. /* code from here down is independend of actually used bit code */
  174. /*
  175. * Code length is determined by some unique (e.g. unary) prefix.
  176. * This encodes arbitrary bit length, not whole bytes: we have a bit-stream,
  177. * not a byte stream.
  178. */
  179. /* for the bitstream, we need a cursor */
  180. struct bitstream_cursor {
  181. /* the current byte */
  182. u8 *b;
  183. /* the current bit within *b, nomalized: 0..7 */
  184. unsigned int bit;
  185. };
  186. /* initialize cursor to point to first bit of stream */
  187. static inline void bitstream_cursor_reset(struct bitstream_cursor *cur, void *s)
  188. {
  189. cur->b = s;
  190. cur->bit = 0;
  191. }
  192. /* advance cursor by that many bits; maximum expected input value: 64,
  193. * but depending on VLI implementation, it may be more. */
  194. static inline void bitstream_cursor_advance(struct bitstream_cursor *cur, unsigned int bits)
  195. {
  196. bits += cur->bit;
  197. cur->b = cur->b + (bits >> 3);
  198. cur->bit = bits & 7;
  199. }
  200. /* the bitstream itself knows its length */
  201. struct bitstream {
  202. struct bitstream_cursor cur;
  203. unsigned char *buf;
  204. size_t buf_len; /* in bytes */
  205. /* for input stream:
  206. * number of trailing 0 bits for padding
  207. * total number of valid bits in stream: buf_len * 8 - pad_bits */
  208. unsigned int pad_bits;
  209. };
  210. static inline void bitstream_init(struct bitstream *bs, void *s, size_t len, unsigned int pad_bits)
  211. {
  212. bs->buf = s;
  213. bs->buf_len = len;
  214. bs->pad_bits = pad_bits;
  215. bitstream_cursor_reset(&bs->cur, bs->buf);
  216. }
  217. static inline void bitstream_rewind(struct bitstream *bs)
  218. {
  219. bitstream_cursor_reset(&bs->cur, bs->buf);
  220. memset(bs->buf, 0, bs->buf_len);
  221. }
  222. /* Put (at most 64) least significant bits of val into bitstream, and advance cursor.
  223. * Ignores "pad_bits".
  224. * Returns zero if bits == 0 (nothing to do).
  225. * Returns number of bits used if successful.
  226. *
  227. * If there is not enough room left in bitstream,
  228. * leaves bitstream unchanged and returns -ENOBUFS.
  229. */
  230. static inline int bitstream_put_bits(struct bitstream *bs, u64 val, const unsigned int bits)
  231. {
  232. unsigned char *b = bs->cur.b;
  233. unsigned int tmp;
  234. if (bits == 0)
  235. return 0;
  236. if ((bs->cur.b + ((bs->cur.bit + bits -1) >> 3)) - bs->buf >= bs->buf_len)
  237. return -ENOBUFS;
  238. /* paranoia: strip off hi bits; they should not be set anyways. */
  239. if (bits < 64)
  240. val &= ~0ULL >> (64 - bits);
  241. *b++ |= (val & 0xff) << bs->cur.bit;
  242. for (tmp = 8 - bs->cur.bit; tmp < bits; tmp += 8)
  243. *b++ |= (val >> tmp) & 0xff;
  244. bitstream_cursor_advance(&bs->cur, bits);
  245. return bits;
  246. }
  247. /* Fetch (at most 64) bits from bitstream into *out, and advance cursor.
  248. *
  249. * If more than 64 bits are requested, returns -EINVAL and leave *out unchanged.
  250. *
  251. * If there are less than the requested number of valid bits left in the
  252. * bitstream, still fetches all available bits.
  253. *
  254. * Returns number of actually fetched bits.
  255. */
  256. static inline int bitstream_get_bits(struct bitstream *bs, u64 *out, int bits)
  257. {
  258. u64 val;
  259. unsigned int n;
  260. if (bits > 64)
  261. return -EINVAL;
  262. if (bs->cur.b + ((bs->cur.bit + bs->pad_bits + bits -1) >> 3) - bs->buf >= bs->buf_len)
  263. bits = ((bs->buf_len - (bs->cur.b - bs->buf)) << 3)
  264. - bs->cur.bit - bs->pad_bits;
  265. if (bits == 0) {
  266. *out = 0;
  267. return 0;
  268. }
  269. /* get the high bits */
  270. val = 0;
  271. n = (bs->cur.bit + bits + 7) >> 3;
  272. /* n may be at most 9, if cur.bit + bits > 64 */
  273. /* which means this copies at most 8 byte */
  274. if (n) {
  275. memcpy(&val, bs->cur.b+1, n - 1);
  276. val = le64_to_cpu(val) << (8 - bs->cur.bit);
  277. }
  278. /* we still need the low bits */
  279. val |= bs->cur.b[0] >> bs->cur.bit;
  280. /* and mask out bits we don't want */
  281. val &= ~0ULL >> (64 - bits);
  282. bitstream_cursor_advance(&bs->cur, bits);
  283. *out = val;
  284. return bits;
  285. }
  286. /* encodes @in as vli into @bs;
  287. * return values
  288. * > 0: number of bits successfully stored in bitstream
  289. * -ENOBUFS @bs is full
  290. * -EINVAL input zero (invalid)
  291. * -EOVERFLOW input too large for this vli code (invalid)
  292. */
  293. static inline int vli_encode_bits(struct bitstream *bs, u64 in)
  294. {
  295. u64 code = code;
  296. int bits = __vli_encode_bits(&code, in);
  297. if (bits <= 0)
  298. return bits;
  299. return bitstream_put_bits(bs, code, bits);
  300. }
  301. #endif