4 * Compress chunks of data (parallel version).
8 * Copyright (C) 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free Software
14 * Foundation; either version 3 of the License, or (at your option) any later
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along with
22 * wimlib; if not, see http://www.gnu.org/licenses/.
29 #ifdef ENABLE_MULTITHREADED_COMPRESSION
31 #include "wimlib/assert.h"
32 #include "wimlib/chunk_compressor.h"
33 #include "wimlib/error.h"
34 #include "wimlib/list.h"
35 #include "wimlib/util.h"
37 # include "wimlib/win32.h" /* win32_get_number_of_processors() */
47 struct message_queue {
48 struct list_head list;
50 pthread_cond_t msg_avail_cond;
51 pthread_cond_t space_avail_cond;
55 struct compressor_thread_data {
57 struct message_queue *chunks_to_compress_queue;
58 struct message_queue *compressed_chunks_queue;
59 struct wimlib_compressor *compressor;
62 #define MAX_CHUNKS_PER_MSG 2
65 u8 *uncompressed_chunks[MAX_CHUNKS_PER_MSG];
66 u8 *compressed_chunks[MAX_CHUNKS_PER_MSG];
67 unsigned uncompressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
68 unsigned compressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
69 size_t num_filled_chunks;
70 size_t num_alloc_chunks;
71 struct list_head list;
73 struct list_head submission_list;
76 struct parallel_chunk_compressor {
77 struct chunk_compressor base;
79 struct message_queue chunks_to_compress_queue;
80 struct message_queue compressed_chunks_queue;
81 struct compressor_thread_data *thread_data;
83 unsigned num_started_threads;
88 struct list_head available_msgs;
89 struct list_head submitted_msgs;
90 struct message *next_submit_msg;
91 struct message *next_ready_msg;
92 size_t next_chunk_idx;
96 get_default_num_threads(void)
100 n = win32_get_number_of_processors();
102 n = sysconf(_SC_NPROCESSORS_ONLN);
104 if (n < 1 || n >= UINT_MAX) {
105 WARNING("Failed to determine number of processors; assuming 1.");
112 get_avail_memory(void)
115 u64 phys_bytes = win32_get_avail_memory();
120 long page_size = sysconf(_SC_PAGESIZE);
121 long num_pages = sysconf(_SC_PHYS_PAGES);
122 if (page_size <= 0 || num_pages <= 0)
124 return ((u64)page_size * (u64)num_pages);
128 WARNING("Failed to determine available memory; assuming 1 GiB");
133 message_queue_init(struct message_queue *q)
135 if (pthread_mutex_init(&q->lock, NULL)) {
136 ERROR_WITH_ERRNO("Failed to initialize mutex");
139 if (pthread_cond_init(&q->msg_avail_cond, NULL)) {
140 ERROR_WITH_ERRNO("Failed to initialize condition variable");
141 goto err_destroy_lock;
143 if (pthread_cond_init(&q->space_avail_cond, NULL)) {
144 ERROR_WITH_ERRNO("Failed to initialize condition variable");
145 goto err_destroy_msg_avail_cond;
147 INIT_LIST_HEAD(&q->list);
150 err_destroy_msg_avail_cond:
151 pthread_cond_destroy(&q->msg_avail_cond);
153 pthread_mutex_destroy(&q->lock);
155 return WIMLIB_ERR_NOMEM;
159 message_queue_destroy(struct message_queue *q)
161 if (q->list.next != NULL) {
162 pthread_mutex_destroy(&q->lock);
163 pthread_cond_destroy(&q->msg_avail_cond);
164 pthread_cond_destroy(&q->space_avail_cond);
169 message_queue_put(struct message_queue *q, struct message *msg)
171 pthread_mutex_lock(&q->lock);
172 list_add_tail(&msg->list, &q->list);
173 pthread_cond_signal(&q->msg_avail_cond);
174 pthread_mutex_unlock(&q->lock);
177 static struct message *
178 message_queue_get(struct message_queue *q)
182 pthread_mutex_lock(&q->lock);
183 while (list_empty(&q->list) && !q->terminating)
184 pthread_cond_wait(&q->msg_avail_cond, &q->lock);
185 if (!q->terminating) {
186 msg = list_entry(q->list.next, struct message, list);
187 list_del(&msg->list);
190 pthread_mutex_unlock(&q->lock);
195 message_queue_terminate(struct message_queue *q)
197 pthread_mutex_lock(&q->lock);
198 q->terminating = true;
199 pthread_cond_broadcast(&q->msg_avail_cond);
200 pthread_mutex_unlock(&q->lock);
204 init_message(struct message *msg, size_t num_chunks, u32 out_chunk_size)
206 msg->num_alloc_chunks = num_chunks;
207 for (size_t i = 0; i < num_chunks; i++) {
208 msg->compressed_chunks[i] = MALLOC(out_chunk_size - 1);
209 msg->uncompressed_chunks[i] = MALLOC(out_chunk_size);
210 if (msg->compressed_chunks[i] == NULL ||
211 msg->uncompressed_chunks[i] == NULL)
212 return WIMLIB_ERR_NOMEM;
218 destroy_message(struct message *msg)
220 for (size_t i = 0; i < msg->num_alloc_chunks; i++) {
221 FREE(msg->compressed_chunks[i]);
222 FREE(msg->uncompressed_chunks[i]);
227 free_messages(struct message *msgs, size_t num_messages)
230 for (size_t i = 0; i < num_messages; i++)
231 destroy_message(&msgs[i]);
236 static struct message *
237 allocate_messages(size_t count, size_t chunks_per_msg, u32 out_chunk_size)
239 struct message *msgs;
241 msgs = CALLOC(count, sizeof(struct message));
244 for (size_t i = 0; i < count; i++) {
245 if (init_message(&msgs[i], chunks_per_msg, out_chunk_size)) {
246 free_messages(msgs, count);
254 compress_chunks(struct message *msg, struct wimlib_compressor *compressor)
257 for (size_t i = 0; i < msg->num_filled_chunks; i++) {
258 wimlib_assert(msg->uncompressed_chunk_sizes[i] != 0);
259 msg->compressed_chunk_sizes[i] =
260 wimlib_compress(msg->uncompressed_chunks[i],
261 msg->uncompressed_chunk_sizes[i],
262 msg->compressed_chunks[i],
263 msg->uncompressed_chunk_sizes[i] - 1,
269 compressor_thread_proc(void *arg)
271 struct compressor_thread_data *params = arg;
274 while ((msg = message_queue_get(params->chunks_to_compress_queue)) != NULL) {
275 compress_chunks(msg, params->compressor);
276 message_queue_put(params->compressed_chunks_queue, msg);
282 parallel_chunk_compressor_destroy(struct chunk_compressor *_ctx)
284 struct parallel_chunk_compressor *ctx = (struct parallel_chunk_compressor *)_ctx;
290 if (ctx->num_started_threads != 0) {
291 DEBUG("Terminating %u compressor threads", ctx->num_started_threads);
292 message_queue_terminate(&ctx->chunks_to_compress_queue);
294 for (i = 0; i < ctx->num_started_threads; i++)
295 pthread_join(ctx->thread_data[i].thread, NULL);
298 message_queue_destroy(&ctx->chunks_to_compress_queue);
299 message_queue_destroy(&ctx->compressed_chunks_queue);
301 if (ctx->thread_data != NULL)
302 for (i = 0; i < ctx->num_threads; i++)
303 wimlib_free_compressor(ctx->thread_data[i].compressor);
305 FREE(ctx->thread_data);
307 free_messages(ctx->msgs, ctx->num_messages);
313 submit_compression_msg(struct parallel_chunk_compressor *ctx)
315 struct message *msg = ctx->next_submit_msg;
317 msg->complete = false;
318 list_add_tail(&msg->submission_list, &ctx->submitted_msgs);
319 message_queue_put(&ctx->chunks_to_compress_queue, msg);
320 ctx->next_submit_msg = NULL;
324 parallel_chunk_compressor_submit_chunk(struct chunk_compressor *_ctx,
325 const void *chunk, size_t size)
327 struct parallel_chunk_compressor *ctx = (struct parallel_chunk_compressor *)_ctx;
330 wimlib_assert(size > 0);
331 wimlib_assert(size <= ctx->base.out_chunk_size);
333 if (ctx->next_submit_msg) {
334 msg = ctx->next_submit_msg;
336 if (list_empty(&ctx->available_msgs))
339 msg = list_entry(ctx->available_msgs.next, struct message, list);
340 list_del(&msg->list);
341 ctx->next_submit_msg = msg;
342 msg->num_filled_chunks = 0;
345 memcpy(msg->uncompressed_chunks[msg->num_filled_chunks], chunk, size);
346 msg->uncompressed_chunk_sizes[msg->num_filled_chunks] = size;
347 if (++msg->num_filled_chunks == msg->num_alloc_chunks)
348 submit_compression_msg(ctx);
353 parallel_chunk_compressor_get_chunk(struct chunk_compressor *_ctx,
354 const void **cdata_ret, unsigned *csize_ret,
357 struct parallel_chunk_compressor *ctx = (struct parallel_chunk_compressor *)_ctx;
361 if (ctx->next_submit_msg)
362 submit_compression_msg(ctx);
364 if (ctx->next_ready_msg) {
365 msg = ctx->next_ready_msg;
367 if (list_empty(&ctx->submitted_msgs))
370 while (!(msg = list_entry(ctx->submitted_msgs.next,
372 submission_list))->complete)
373 message_queue_get(&ctx->compressed_chunks_queue)->complete = true;
375 ctx->next_ready_msg = msg;
376 ctx->next_chunk_idx = 0;
379 if (msg->compressed_chunk_sizes[ctx->next_chunk_idx]) {
380 *cdata_ret = msg->compressed_chunks[ctx->next_chunk_idx];
381 *csize_ret = msg->compressed_chunk_sizes[ctx->next_chunk_idx];
383 *cdata_ret = msg->uncompressed_chunks[ctx->next_chunk_idx];
384 *csize_ret = msg->uncompressed_chunk_sizes[ctx->next_chunk_idx];
386 *usize_ret = msg->uncompressed_chunk_sizes[ctx->next_chunk_idx];
388 if (++ctx->next_chunk_idx == msg->num_filled_chunks) {
389 list_del(&msg->submission_list);
390 list_add_tail(&msg->list, &ctx->available_msgs);
391 ctx->next_ready_msg = NULL;
397 new_parallel_chunk_compressor(int out_ctype, u32 out_chunk_size,
398 unsigned num_threads, u64 max_memory,
399 struct chunk_compressor **compressor_ret)
401 u64 approx_mem_required;
402 size_t chunks_per_msg;
403 size_t msgs_per_thread;
404 struct parallel_chunk_compressor *ctx;
407 unsigned desired_num_threads;
409 wimlib_assert(out_chunk_size > 0);
410 wimlib_assert(out_ctype != WIMLIB_COMPRESSION_TYPE_NONE);
412 if (num_threads == 0)
413 num_threads = get_default_num_threads();
415 if (num_threads == 1) {
416 DEBUG("Only 1 thread; Not bothering with "
417 "parallel chunk compressor.");
422 max_memory = get_avail_memory();
424 desired_num_threads = num_threads;
426 if (out_chunk_size < ((u32)1 << 23)) {
427 chunks_per_msg = MAX_CHUNKS_PER_MSG;
430 /* Big chunks: Just have one buffer per thread --- more would
431 * just waste memory. */
436 approx_mem_required =
437 (u64)chunks_per_msg *
438 (u64)msgs_per_thread *
443 + num_threads * wimlib_get_compressor_needed_memory(out_ctype,
446 if (approx_mem_required <= max_memory)
449 if (chunks_per_msg > 1)
451 else if (msgs_per_thread > 1)
453 else if (num_threads > 1)
459 if (num_threads < desired_num_threads) {
460 WARNING("Wanted to use %u threads, but limiting to %u "
461 "to fit in available memory!",
462 desired_num_threads, num_threads);
465 if (num_threads == 1) {
466 DEBUG("Only 1 thread; Not bothering with "
467 "parallel chunk compressor.");
471 ret = WIMLIB_ERR_NOMEM;
472 ctx = CALLOC(1, sizeof(*ctx));
476 ctx->base.out_ctype = out_ctype;
477 ctx->base.out_chunk_size = out_chunk_size;
478 ctx->base.num_threads = num_threads;
479 ctx->base.destroy = parallel_chunk_compressor_destroy;
480 ctx->base.submit_chunk = parallel_chunk_compressor_submit_chunk;
481 ctx->base.get_chunk = parallel_chunk_compressor_get_chunk;
483 ctx->num_threads = num_threads;
485 ret = message_queue_init(&ctx->chunks_to_compress_queue);
489 ret = message_queue_init(&ctx->compressed_chunks_queue);
493 ret = WIMLIB_ERR_NOMEM;
494 ctx->thread_data = CALLOC(num_threads, sizeof(ctx->thread_data[0]));
495 if (ctx->thread_data == NULL)
498 for (i = 0; i < num_threads; i++) {
499 struct compressor_thread_data *dat;
501 dat = &ctx->thread_data[i];
503 dat->chunks_to_compress_queue = &ctx->chunks_to_compress_queue;
504 dat->compressed_chunks_queue = &ctx->compressed_chunks_queue;
505 ret = wimlib_create_compressor(out_ctype, out_chunk_size,
506 NULL, &dat->compressor);
511 for (ctx->num_started_threads = 0;
512 ctx->num_started_threads < num_threads;
513 ctx->num_started_threads++)
515 DEBUG("pthread_create thread %u of %u",
516 ctx->num_started_threads + 1, num_threads);
517 ret = pthread_create(&ctx->thread_data[ctx->num_started_threads].thread,
519 compressor_thread_proc,
520 &ctx->thread_data[ctx->num_started_threads]);
523 ret = WIMLIB_ERR_NOMEM;
524 WARNING_WITH_ERRNO("Failed to create compressor thread %u of %u");
529 ret = WIMLIB_ERR_NOMEM;
530 ctx->num_messages = num_threads * msgs_per_thread;
531 ctx->msgs = allocate_messages(ctx->num_messages,
532 chunks_per_msg, out_chunk_size);
533 if (ctx->msgs == NULL)
536 INIT_LIST_HEAD(&ctx->available_msgs);
537 for (size_t i = 0; i < ctx->num_messages; i++)
538 list_add_tail(&ctx->msgs[i].list, &ctx->available_msgs);
540 INIT_LIST_HEAD(&ctx->submitted_msgs);
542 *compressor_ret = &ctx->base;
546 parallel_chunk_compressor_destroy(&ctx->base);
550 #endif /* ENABLE_MULTITHREADED_COMPRESSION */