You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
16 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*
  5. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. PATENT RIGHTS GRANT:
  42. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  43. Tokutek as part of the Fractal Tree project.
  44. "PATENT CLAIMS" means the claims of patents that are owned or
  45. licensable by Tokutek, both currently or in the future; and that in
  46. the absence of this license would be infringed by THIS
  47. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  48. "PATENT CHALLENGE" shall mean a challenge to the validity,
  49. patentability, enforceability and/or non-infringement of any of the
  50. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  51. Tokutek hereby grants to you, for the term and geographical scope of
  52. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  53. irrevocable (except as stated in this section) patent license to
  54. make, have made, use, offer to sell, sell, import, transfer, and
  55. otherwise run, modify, and propagate the contents of THIS
  56. IMPLEMENTATION, where such license applies only to the PATENT
  57. CLAIMS. This grant does not include claims that would be infringed
  58. only as a consequence of further modifications of THIS
  59. IMPLEMENTATION. If you or your agent or licensee institute or order
  60. or agree to the institution of patent litigation against any entity
  61. (including a cross-claim or counterclaim in a lawsuit) alleging that
  62. THIS IMPLEMENTATION constitutes direct or contributory patent
  63. infringement, or inducement of patent infringement, then any rights
  64. granted to you under this License shall terminate as of the date
  65. such litigation is filed. If you or your agent or exclusive
  66. licensee institute or order or agree to the institution of a PATENT
  67. CHALLENGE, then Tokutek may terminate any rights granted to you
  68. under this License.
  69. */
  70. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  71. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  72. #include "sub_block.h"
  73. #include "compress.h"
  74. #include "quicklz.h"
  75. #include "x1764.h"
  76. #include <memory.h>
  77. #include <toku_assert.h>
  78. #include <toku_portability.h>
  79. #include <util/threadpool.h>
  80. #include <stdio.h>
  81. #include <string.h>
  82. #include <errno.h>
  83. #include <zlib.h>
  84. SUB_BLOCK sub_block_creat(void) {
  85. SUB_BLOCK XMALLOC(sb);
  86. sub_block_init(sb);
  87. return sb;
  88. }
  89. void sub_block_init(SUB_BLOCK sub_block) {
  90. sub_block->uncompressed_ptr = 0;
  91. sub_block->uncompressed_size = 0;
  92. sub_block->compressed_ptr = 0;
  93. sub_block->compressed_size_bound = 0;
  94. sub_block->compressed_size = 0;
  95. sub_block->xsum = 0;
  96. }
  97. // get the size of the compression header
  98. size_t
  99. sub_block_header_size(int n_sub_blocks) {
  100. return sizeof (uint32_t) + n_sub_blocks * sizeof (struct stored_sub_block);
  101. }
  102. void
  103. set_compressed_size_bound(struct sub_block *se, enum toku_compression_method method) {
  104. se->compressed_size_bound = toku_compress_bound(method, se->uncompressed_size);
  105. }
  106. // get the sum of the sub block compressed sizes
  107. size_t
  108. get_sum_compressed_size_bound(int n_sub_blocks, struct sub_block sub_block[], enum toku_compression_method method) {
  109. size_t compressed_size_bound = 0;
  110. for (int i = 0; i < n_sub_blocks; i++) {
  111. sub_block[i].compressed_size_bound = toku_compress_bound(method, sub_block[i].uncompressed_size);
  112. compressed_size_bound += sub_block[i].compressed_size_bound;
  113. }
  114. return compressed_size_bound;
  115. }
  116. // get the sum of the sub block uncompressed sizes
  117. size_t
  118. get_sum_uncompressed_size(int n_sub_blocks, struct sub_block sub_block[]) {
  119. size_t uncompressed_size = 0;
  120. for (int i = 0; i < n_sub_blocks; i++)
  121. uncompressed_size += sub_block[i].uncompressed_size;
  122. return uncompressed_size;
  123. }
  124. // round up n
  125. static inline int
  126. alignup32(int a, int b) {
  127. return ((a+b-1) / b) * b;
  128. }
  129. // Choose n_sub_blocks and sub_block_size such that the product is >= total_size and the sub_block_size is at
  130. // least >= the target_sub_block_size.
  131. int
  132. choose_sub_block_size(int total_size, int n_sub_blocks_limit, int *sub_block_size_ret, int *n_sub_blocks_ret) {
  133. if (total_size < 0 || n_sub_blocks_limit < 1)
  134. return EINVAL;
  135. const int alignment = 32;
  136. int n_sub_blocks, sub_block_size;
  137. n_sub_blocks = total_size / target_sub_block_size;
  138. if (n_sub_blocks <= 1) {
  139. if (total_size > 0 && n_sub_blocks_limit > 0)
  140. n_sub_blocks = 1;
  141. sub_block_size = total_size;
  142. } else {
  143. if (n_sub_blocks > n_sub_blocks_limit) // limit the number of sub-blocks
  144. n_sub_blocks = n_sub_blocks_limit;
  145. sub_block_size = alignup32(total_size / n_sub_blocks, alignment);
  146. while (sub_block_size * n_sub_blocks < total_size) // round up the sub-block size until big enough
  147. sub_block_size += alignment;
  148. }
  149. *sub_block_size_ret = sub_block_size;
  150. *n_sub_blocks_ret = n_sub_blocks;
  151. return 0;
  152. }
  153. // Choose the right size of basement nodes. For now, just align up to
  154. // 256k blocks and hope it compresses well enough.
  155. int
  156. choose_basement_node_size(int total_size, int *sub_block_size_ret, int *n_sub_blocks_ret) {
  157. if (total_size < 0)
  158. return EINVAL;
  159. *n_sub_blocks_ret = (total_size + max_basement_node_uncompressed_size - 1) / max_basement_node_uncompressed_size;
  160. *sub_block_size_ret = max_basement_node_uncompressed_size;
  161. return 0;
  162. }
  163. void
  164. set_all_sub_block_sizes(int total_size, int sub_block_size, int n_sub_blocks, struct sub_block sub_block[]) {
  165. int size_left = total_size;
  166. int i;
  167. for (i = 0; i < n_sub_blocks-1; i++) {
  168. sub_block[i].uncompressed_size = sub_block_size;
  169. size_left -= sub_block_size;
  170. }
  171. if (i == 0 || size_left > 0)
  172. sub_block[i].uncompressed_size = size_left;
  173. }
  174. // find the index of the first sub block that contains offset
  175. // Returns the sub block index, else returns -1
  176. int
  177. get_sub_block_index(int n_sub_blocks, struct sub_block sub_block[], size_t offset) {
  178. size_t start_offset = 0;
  179. for (int i = 0; i < n_sub_blocks; i++) {
  180. size_t size = sub_block[i].uncompressed_size;
  181. if (offset < start_offset + size)
  182. return i;
  183. start_offset += size;
  184. }
  185. return -1;
  186. }
  187. #include "workset.h"
  188. void
  189. compress_work_init(struct compress_work *w, enum toku_compression_method method, struct sub_block *sub_block) {
  190. w->method = method;
  191. w->sub_block = sub_block;
  192. }
  193. //
  194. // takes the uncompressed contents of sub_block
  195. // and compresses them into sb_compressed_ptr
  196. // cs_bound is the compressed size bound
  197. // Returns the size of the compressed data
  198. //
  199. uint32_t
  200. compress_nocrc_sub_block(
  201. struct sub_block *sub_block,
  202. void* sb_compressed_ptr,
  203. uint32_t cs_bound,
  204. enum toku_compression_method method
  205. )
  206. {
  207. // compress it
  208. Bytef *uncompressed_ptr = (Bytef *) sub_block->uncompressed_ptr;
  209. Bytef *compressed_ptr = (Bytef *) sb_compressed_ptr;
  210. uLongf uncompressed_len = sub_block->uncompressed_size;
  211. uLongf real_compressed_len = cs_bound;
  212. toku_compress(method,
  213. compressed_ptr, &real_compressed_len,
  214. uncompressed_ptr, uncompressed_len);
  215. return real_compressed_len;
  216. }
  217. void
  218. compress_sub_block(struct sub_block *sub_block, enum toku_compression_method method) {
  219. sub_block->compressed_size = compress_nocrc_sub_block(
  220. sub_block,
  221. sub_block->compressed_ptr,
  222. sub_block->compressed_size_bound,
  223. method
  224. );
  225. // checksum it
  226. sub_block->xsum = x1764_memory(sub_block->compressed_ptr, sub_block->compressed_size);
  227. }
  228. void *
  229. compress_worker(void *arg) {
  230. struct workset *ws = (struct workset *) arg;
  231. while (1) {
  232. struct compress_work *w = (struct compress_work *) workset_get(ws);
  233. if (w == NULL)
  234. break;
  235. compress_sub_block(w->sub_block, w->method);
  236. }
  237. workset_release_ref(ws);
  238. return arg;
  239. }
  240. size_t
  241. compress_all_sub_blocks(int n_sub_blocks, struct sub_block sub_block[], char *uncompressed_ptr, char *compressed_ptr, int num_cores, struct toku_thread_pool *pool, enum toku_compression_method method) {
  242. char *compressed_base_ptr = compressed_ptr;
  243. size_t compressed_len;
  244. // This is a complex way to write a parallel loop. Cilk would be better.
  245. if (n_sub_blocks == 1) {
  246. // single sub-block
  247. sub_block[0].uncompressed_ptr = uncompressed_ptr;
  248. sub_block[0].compressed_ptr = compressed_ptr;
  249. compress_sub_block(&sub_block[0], method);
  250. compressed_len = sub_block[0].compressed_size;
  251. } else {
  252. // multiple sub-blocks
  253. int T = num_cores; // T = min(num_cores, n_sub_blocks) - 1
  254. if (T > n_sub_blocks)
  255. T = n_sub_blocks;
  256. if (T > 0)
  257. T = T - 1; // threads in addition to the running thread
  258. struct workset ws;
  259. ZERO_STRUCT(ws);
  260. workset_init(&ws);
  261. struct compress_work work[n_sub_blocks];
  262. workset_lock(&ws);
  263. for (int i = 0; i < n_sub_blocks; i++) {
  264. sub_block[i].uncompressed_ptr = uncompressed_ptr;
  265. sub_block[i].compressed_ptr = compressed_ptr;
  266. compress_work_init(&work[i], method, &sub_block[i]);
  267. workset_put_locked(&ws, &work[i].base);
  268. uncompressed_ptr += sub_block[i].uncompressed_size;
  269. compressed_ptr += sub_block[i].compressed_size_bound;
  270. }
  271. workset_unlock(&ws);
  272. // compress the sub-blocks
  273. if (0) printf("%s:%d T=%d N=%d\n", __FUNCTION__, __LINE__, T, n_sub_blocks);
  274. toku_thread_pool_run(pool, 0, &T, compress_worker, &ws);
  275. workset_add_ref(&ws, T);
  276. compress_worker(&ws);
  277. // wait for all of the work to complete
  278. workset_join(&ws);
  279. workset_destroy(&ws);
  280. // squeeze out the holes not used by the compress bound
  281. compressed_ptr = compressed_base_ptr + sub_block[0].compressed_size;
  282. for (int i = 1; i < n_sub_blocks; i++) {
  283. memmove(compressed_ptr, sub_block[i].compressed_ptr, sub_block[i].compressed_size);
  284. compressed_ptr += sub_block[i].compressed_size;
  285. }
  286. compressed_len = compressed_ptr - compressed_base_ptr;
  287. }
  288. return compressed_len;
  289. }
  290. // initialize the decompression work
  291. void
  292. decompress_work_init(struct decompress_work *dw,
  293. void *compress_ptr, uint32_t compress_size,
  294. void *uncompress_ptr, uint32_t uncompress_size,
  295. uint32_t xsum) {
  296. dw->compress_ptr = compress_ptr;
  297. dw->compress_size = compress_size;
  298. dw->uncompress_ptr = uncompress_ptr;
  299. dw->uncompress_size = uncompress_size;
  300. dw->xsum = xsum;
  301. dw->error = 0;
  302. }
  303. int verbose_decompress_sub_block = 1;
  304. // decompress one block
  305. int
  306. decompress_sub_block(void *compress_ptr, uint32_t compress_size, void *uncompress_ptr, uint32_t uncompress_size, uint32_t expected_xsum) {
  307. int result = 0;
  308. // verify checksum
  309. uint32_t xsum = x1764_memory(compress_ptr, compress_size);
  310. if (xsum != expected_xsum) {
  311. if (verbose_decompress_sub_block) fprintf(stderr, "%s:%d xsum %u expected %u\n", __FUNCTION__, __LINE__, xsum, expected_xsum);
  312. result = EINVAL;
  313. } else {
  314. // decompress
  315. toku_decompress((Bytef *) uncompress_ptr, uncompress_size, (Bytef *) compress_ptr, compress_size);
  316. }
  317. return result;
  318. }
  319. // decompress blocks until there is no more work to do
  320. void *
  321. decompress_worker(void *arg) {
  322. struct workset *ws = (struct workset *) arg;
  323. while (1) {
  324. struct decompress_work *dw = (struct decompress_work *) workset_get(ws);
  325. if (dw == NULL)
  326. break;
  327. dw->error = decompress_sub_block(dw->compress_ptr, dw->compress_size, dw->uncompress_ptr, dw->uncompress_size, dw->xsum);
  328. }
  329. workset_release_ref(ws);
  330. return arg;
  331. }
  332. int
  333. decompress_all_sub_blocks(int n_sub_blocks, struct sub_block sub_block[], unsigned char *compressed_data, unsigned char *uncompressed_data, int num_cores, struct toku_thread_pool *pool) {
  334. int r;
  335. if (n_sub_blocks == 1) {
  336. r = decompress_sub_block(compressed_data, sub_block[0].compressed_size, uncompressed_data, sub_block[0].uncompressed_size, sub_block[0].xsum);
  337. } else {
  338. // compute the number of additional threads needed for decompressing this node
  339. int T = num_cores; // T = min(#cores, #blocks) - 1
  340. if (T > n_sub_blocks)
  341. T = n_sub_blocks;
  342. if (T > 0)
  343. T = T - 1; // threads in addition to the running thread
  344. // init the decompression work set
  345. struct workset ws;
  346. ZERO_STRUCT(ws);
  347. workset_init(&ws);
  348. // initialize the decompression work and add to the work set
  349. struct decompress_work decompress_work[n_sub_blocks];
  350. workset_lock(&ws);
  351. for (int i = 0; i < n_sub_blocks; i++) {
  352. decompress_work_init(&decompress_work[i], compressed_data, sub_block[i].compressed_size, uncompressed_data, sub_block[i].uncompressed_size, sub_block[i].xsum);
  353. workset_put_locked(&ws, &decompress_work[i].base);
  354. uncompressed_data += sub_block[i].uncompressed_size;
  355. compressed_data += sub_block[i].compressed_size;
  356. }
  357. workset_unlock(&ws);
  358. // decompress the sub-blocks
  359. if (0) printf("%s:%d Cores=%d Blocks=%d T=%d\n", __FUNCTION__, __LINE__, num_cores, n_sub_blocks, T);
  360. toku_thread_pool_run(pool, 0, &T, decompress_worker, &ws);
  361. workset_add_ref(&ws, T);
  362. decompress_worker(&ws);
  363. // cleanup
  364. workset_join(&ws);
  365. workset_destroy(&ws);
  366. r = 0;
  367. for (int i = 0; i < n_sub_blocks; i++) {
  368. r = decompress_work[i].error;
  369. if (r != 0)
  370. break;
  371. }
  372. }
  373. return r;
  374. }