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 #include "wimlib/assert.h"
30 #include "wimlib/chunk_compressor.h"
31 #include "wimlib/error.h"
32 #include "wimlib/list.h"
33 #include "wimlib/util.h"
35 # include "wimlib/win32.h" /* win32_get_number_of_processors() */
45 struct message_queue {
46 struct list_head list;
48 pthread_cond_t msg_avail_cond;
49 pthread_cond_t space_avail_cond;
53 struct compressor_thread_data {
55 struct message_queue *chunks_to_compress_queue;
56 struct message_queue *compressed_chunks_queue;
57 struct wimlib_compressor *compressor;
60 #define MAX_CHUNKS_PER_MSG 2
63 u8 *uncompressed_chunks[MAX_CHUNKS_PER_MSG];
64 u8 *compressed_chunks[MAX_CHUNKS_PER_MSG];
65 unsigned uncompressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
66 unsigned compressed_chunk_sizes[MAX_CHUNKS_PER_MSG];
67 size_t num_filled_chunks;
68 size_t num_alloc_chunks;
69 struct list_head list;
71 struct list_head submission_list;
74 struct parallel_chunk_compressor {
75 struct chunk_compressor base;
77 struct message_queue chunks_to_compress_queue;
78 struct message_queue compressed_chunks_queue;
79 struct compressor_thread_data *thread_data;
81 unsigned num_started_threads;
86 struct list_head available_msgs;
87 struct list_head submitted_msgs;
88 struct message *next_submit_msg;
89 struct message *next_ready_msg;
90 size_t next_chunk_idx;
94 get_default_num_threads(void)
98 n = win32_get_number_of_processors();
100 n = sysconf(_SC_NPROCESSORS_ONLN);
102 if (n < 1 || n >= UINT_MAX) {
103 WARNING("Failed to determine number of processors; assuming 1.");
110 get_avail_memory(void)
113 u64 phys_bytes = win32_get_avail_memory();
118 long page_size = sysconf(_SC_PAGESIZE);
119 long num_pages = sysconf(_SC_PHYS_PAGES);
120 if (page_size <= 0 || num_pages <= 0)
122 return ((u64)page_size * (u64)num_pages);
126 WARNING("Failed to determine available memory; assuming 1 GiB");
131 message_queue_init(struct message_queue *q)
133 if (pthread_mutex_init(&q->lock, NULL)) {
134 ERROR_WITH_ERRNO("Failed to initialize mutex");
137 if (pthread_cond_init(&q->msg_avail_cond, NULL)) {
138 ERROR_WITH_ERRNO("Failed to initialize condition variable");
139 goto err_destroy_lock;
141 if (pthread_cond_init(&q->space_avail_cond, NULL)) {
142 ERROR_WITH_ERRNO("Failed to initialize condition variable");
143 goto err_destroy_msg_avail_cond;
145 INIT_LIST_HEAD(&q->list);
148 err_destroy_msg_avail_cond:
149 pthread_cond_destroy(&q->msg_avail_cond);
151 pthread_mutex_destroy(&q->lock);
153 return WIMLIB_ERR_NOMEM;
157 message_queue_destroy(struct message_queue *q)
159 if (q->list.next != NULL) {
160 pthread_mutex_destroy(&q->lock);
161 pthread_cond_destroy(&q->msg_avail_cond);
162 pthread_cond_destroy(&q->space_avail_cond);
167 message_queue_put(struct message_queue *q, struct message *msg)
169 pthread_mutex_lock(&q->lock);
170 list_add_tail(&msg->list, &q->list);
171 pthread_cond_signal(&q->msg_avail_cond);
172 pthread_mutex_unlock(&q->lock);
175 static struct message *
176 message_queue_get(struct message_queue *q)
180 pthread_mutex_lock(&q->lock);
181 while (list_empty(&q->list) && !q->terminating)
182 pthread_cond_wait(&q->msg_avail_cond, &q->lock);
183 if (!q->terminating) {
184 msg = list_entry(q->list.next, struct message, list);
185 list_del(&msg->list);
188 pthread_mutex_unlock(&q->lock);
193 message_queue_terminate(struct message_queue *q)
195 pthread_mutex_lock(&q->lock);
196 q->terminating = true;
197 pthread_cond_broadcast(&q->msg_avail_cond);
198 pthread_mutex_unlock(&q->lock);
202 init_message(struct message *msg, size_t num_chunks, u32 out_chunk_size)
204 msg->num_alloc_chunks = num_chunks;
205 for (size_t i = 0; i < num_chunks; i++) {
206 msg->compressed_chunks[i] = MALLOC(out_chunk_size - 1);
207 msg->uncompressed_chunks[i] = MALLOC(out_chunk_size);
208 if (msg->compressed_chunks[i] == NULL ||
209 msg->uncompressed_chunks[i] == NULL)
210 return WIMLIB_ERR_NOMEM;
216 destroy_message(struct message *msg)
218 for (size_t i = 0; i < msg->num_alloc_chunks; i++) {
219 FREE(msg->compressed_chunks[i]);
220 FREE(msg->uncompressed_chunks[i]);
225 free_messages(struct message *msgs, size_t num_messages)
228 for (size_t i = 0; i < num_messages; i++)
229 destroy_message(&msgs[i]);
234 static struct message *
235 allocate_messages(size_t count, size_t chunks_per_msg, u32 out_chunk_size)
237 struct message *msgs;
239 msgs = CALLOC(count, sizeof(struct message));
242 for (size_t i = 0; i < count; i++) {
243 if (init_message(&msgs[i], chunks_per_msg, out_chunk_size)) {
244 free_messages(msgs, count);
252 compress_chunks(struct message *msg, struct wimlib_compressor *compressor)
255 for (size_t i = 0; i < msg->num_filled_chunks; i++) {
256 wimlib_assert(msg->uncompressed_chunk_sizes[i] != 0);
257 msg->compressed_chunk_sizes[i] =
258 wimlib_compress(msg->uncompressed_chunks[i],
259 msg->uncompressed_chunk_sizes[i],
260 msg->compressed_chunks[i],
261 msg->uncompressed_chunk_sizes[i] - 1,
267 compressor_thread_proc(void *arg)
269 struct compressor_thread_data *params = arg;
272 while ((msg = message_queue_get(params->chunks_to_compress_queue)) != NULL) {
273 compress_chunks(msg, params->compressor);
274 message_queue_put(params->compressed_chunks_queue, msg);
280 parallel_chunk_compressor_destroy(struct chunk_compressor *_ctx)
282 struct parallel_chunk_compressor *ctx = (struct parallel_chunk_compressor *)_ctx;
288 if (ctx->num_started_threads != 0) {
289 DEBUG("Terminating %u compressor threads", ctx->num_started_threads);
290 message_queue_terminate(&ctx->chunks_to_compress_queue);
292 for (i = 0; i < ctx->num_started_threads; i++)
293 pthread_join(ctx->thread_data[i].thread, NULL);
296 message_queue_destroy(&ctx->chunks_to_compress_queue);
297 message_queue_destroy(&ctx->compressed_chunks_queue);
299 if (ctx->thread_data != NULL)
300 for (i = 0; i < ctx->num_threads; i++)
301 wimlib_free_compressor(ctx->thread_data[i].compressor);
303 FREE(ctx->thread_data);
305 free_messages(ctx->msgs, ctx->num_messages);
311 submit_compression_msg(struct parallel_chunk_compressor *ctx)
313 struct message *msg = ctx->next_submit_msg;
315 msg->complete = false;
316 list_add_tail(&msg->submission_list, &ctx->submitted_msgs);
317 message_queue_put(&ctx->chunks_to_compress_queue, msg);
318 ctx->next_submit_msg = NULL;
322 parallel_chunk_compressor_submit_chunk(struct chunk_compressor *_ctx,
323 const void *chunk, size_t size)
325 struct parallel_chunk_compressor *ctx = (struct parallel_chunk_compressor *)_ctx;
328 wimlib_assert(size > 0);
329 wimlib_assert(size <= ctx->base.out_chunk_size);
331 if (ctx->next_submit_msg) {
332 msg = ctx->next_submit_msg;
334 if (list_empty(&ctx->available_msgs))
337 msg = list_entry(ctx->available_msgs.next, struct message, list);
338 list_del(&msg->list);
339 ctx->next_submit_msg = msg;
340 msg->num_filled_chunks = 0;
343 memcpy(msg->uncompressed_chunks[msg->num_filled_chunks], chunk, size);
344 msg->uncompressed_chunk_sizes[msg->num_filled_chunks] = size;
345 if (++msg->num_filled_chunks == msg->num_alloc_chunks)
346 submit_compression_msg(ctx);
351 parallel_chunk_compressor_get_chunk(struct chunk_compressor *_ctx,
352 const void **cdata_ret, unsigned *csize_ret,
355 struct parallel_chunk_compressor *ctx = (struct parallel_chunk_compressor *)_ctx;
359 if (ctx->next_submit_msg)
360 submit_compression_msg(ctx);
362 if (ctx->next_ready_msg) {
363 msg = ctx->next_ready_msg;
365 if (list_empty(&ctx->submitted_msgs))
368 while (!(msg = list_entry(ctx->submitted_msgs.next,
370 submission_list))->complete)
371 message_queue_get(&ctx->compressed_chunks_queue)->complete = true;
373 ctx->next_ready_msg = msg;
374 ctx->next_chunk_idx = 0;
377 if (msg->compressed_chunk_sizes[ctx->next_chunk_idx]) {
378 *cdata_ret = msg->compressed_chunks[ctx->next_chunk_idx];
379 *csize_ret = msg->compressed_chunk_sizes[ctx->next_chunk_idx];
381 *cdata_ret = msg->uncompressed_chunks[ctx->next_chunk_idx];
382 *csize_ret = msg->uncompressed_chunk_sizes[ctx->next_chunk_idx];
384 *usize_ret = msg->uncompressed_chunk_sizes[ctx->next_chunk_idx];
386 if (++ctx->next_chunk_idx == msg->num_filled_chunks) {
387 list_del(&msg->submission_list);
388 list_add_tail(&msg->list, &ctx->available_msgs);
389 ctx->next_ready_msg = NULL;
395 new_parallel_chunk_compressor(int out_ctype, u32 out_chunk_size,
396 unsigned num_threads, u64 max_memory,
397 struct chunk_compressor **compressor_ret)
399 u64 approx_mem_required;
400 size_t chunks_per_msg;
401 size_t msgs_per_thread;
402 struct parallel_chunk_compressor *ctx;
405 unsigned desired_num_threads;
407 wimlib_assert(out_chunk_size > 0);
408 wimlib_assert(out_ctype != WIMLIB_COMPRESSION_TYPE_NONE);
410 if (num_threads == 0)
411 num_threads = get_default_num_threads();
413 if (num_threads == 1) {
414 DEBUG("Only 1 thread; Not bothering with "
415 "parallel chunk compressor.");
420 max_memory = get_avail_memory();
422 desired_num_threads = num_threads;
424 chunks_per_msg = MAX_CHUNKS_PER_MSG;
427 approx_mem_required =
428 (u64)chunks_per_msg *
429 (u64)msgs_per_thread *
433 + (out_chunk_size * num_threads * 4);
434 if (approx_mem_required <= max_memory)
437 if (chunks_per_msg > 1)
439 else if (msgs_per_thread > 1)
441 else if (num_threads > 1)
447 if (num_threads < desired_num_threads) {
448 WARNING("Wanted to use %u threads, but limiting to %u "
449 "to fit in available memory!",
450 desired_num_threads, num_threads);
453 if (num_threads == 1) {
454 DEBUG("Only 1 thread; Not bothering with "
455 "parallel chunk compressor.");
459 ret = WIMLIB_ERR_NOMEM;
460 ctx = CALLOC(1, sizeof(*ctx));
464 ctx->base.out_ctype = out_ctype;
465 ctx->base.out_chunk_size = out_chunk_size;
466 ctx->base.num_threads = num_threads;
467 ctx->base.destroy = parallel_chunk_compressor_destroy;
468 ctx->base.submit_chunk = parallel_chunk_compressor_submit_chunk;
469 ctx->base.get_chunk = parallel_chunk_compressor_get_chunk;
471 ctx->num_threads = num_threads;
473 ret = message_queue_init(&ctx->chunks_to_compress_queue);
477 ret = message_queue_init(&ctx->compressed_chunks_queue);
481 ret = WIMLIB_ERR_NOMEM;
482 ctx->thread_data = CALLOC(num_threads, sizeof(ctx->thread_data[0]));
483 if (ctx->thread_data == NULL)
486 for (i = 0; i < num_threads; i++) {
487 struct compressor_thread_data *dat;
489 dat = &ctx->thread_data[i];
491 dat->chunks_to_compress_queue = &ctx->chunks_to_compress_queue;
492 dat->compressed_chunks_queue = &ctx->compressed_chunks_queue;
493 ret = wimlib_create_compressor(out_ctype, out_chunk_size,
494 NULL, &dat->compressor);
499 for (ctx->num_started_threads = 0;
500 ctx->num_started_threads < num_threads;
501 ctx->num_started_threads++)
503 DEBUG("pthread_create thread %u of %u",
504 ctx->num_started_threads + 1, num_threads);
505 ret = pthread_create(&ctx->thread_data[ctx->num_started_threads].thread,
507 compressor_thread_proc,
508 &ctx->thread_data[ctx->num_started_threads]);
511 ret = WIMLIB_ERR_NOMEM;
512 WARNING_WITH_ERRNO("Failed to create compressor thread %u of %u");
517 ret = WIMLIB_ERR_NOMEM;
518 ctx->num_messages = num_threads * msgs_per_thread;
519 ctx->msgs = allocate_messages(ctx->num_messages,
520 chunks_per_msg, out_chunk_size);
521 if (ctx->msgs == NULL)
524 INIT_LIST_HEAD(&ctx->available_msgs);
525 for (size_t i = 0; i < ctx->num_messages; i++)
526 list_add_tail(&ctx->msgs[i].list, &ctx->available_msgs);
528 INIT_LIST_HEAD(&ctx->submitted_msgs);
530 *compressor_ret = &ctx->base;
534 parallel_chunk_compressor_destroy(&ctx->base);