|
16 | 16 |
|
17 | 17 | #include <algorithm>
|
18 | 18 | #include <chrono>
|
| 19 | +#include <cstring> |
19 | 20 | #include <functional>
|
20 | 21 | #include <memory>
|
21 | 22 | #include <stdexcept>
|
|
33 | 34 | #include "rosbag2_storage/storage_interfaces/read_write_interface.hpp"
|
34 | 35 |
|
35 | 36 | #include "logging.hpp"
|
| 37 | +#ifdef _WIN32 |
| 38 | +#include <windows.h> |
| 39 | +#else |
| 40 | +#include <unistd.h> |
| 41 | +#include <sys/resource.h> |
| 42 | +#endif |
36 | 43 |
|
37 | 44 | namespace rosbag2_compression
|
38 | 45 | {
|
@@ -62,6 +69,47 @@ SequentialCompressionWriter::~SequentialCompressionWriter()
|
62 | 69 |
|
63 | 70 | void SequentialCompressionWriter::compression_thread_fn()
|
64 | 71 | {
|
| 72 | + if (compression_options_.thread_nice_value) { |
| 73 | + |
| 74 | +#ifdef _WIN32 |
| 75 | + ROSBAG2_COMPRESSION_LOG_WARN_STREAM( |
| 76 | + "Lowering of process priority is not implemented for windows"); |
| 77 | + |
| 78 | + /** |
| 79 | + * The implementation should look something like this |
| 80 | +
|
| 81 | + uint8_t nice_value = *compression_options_.thread_nice_value; |
| 82 | +
|
| 83 | + // this must match THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST... |
| 84 | + DWORD dwThreadPri = *compression_options_.thread_nice_value; |
| 85 | +
|
| 86 | + if(!SetThreadPriority(GetCurrentThread(), dwThreadPri)) |
| 87 | + { |
| 88 | + ROSBAG2_COMPRESSION_LOG_WARN_STREAM( |
| 89 | + "Could not set nice value of compression thread to " << nice_value << " : " << std::strerror(GetLastError())); |
| 90 | + } |
| 91 | + */ |
| 92 | + |
| 93 | +#else |
| 94 | + int wanted_nice_value = *compression_options_.thread_nice_value; |
| 95 | + |
| 96 | + errno = 0; |
| 97 | + int cur_nice_value = getpriority(PRIO_PROCESS, 0); |
| 98 | + if (cur_nice_value != -1 && errno != 0) { |
| 99 | + int new_nice_value = nice(wanted_nice_value - cur_nice_value); |
| 100 | + if ((new_nice_value == -1 && errno != 0) || new_nice_value != wanted_nice_value) { |
| 101 | + ROSBAG2_COMPRESSION_LOG_WARN_STREAM( |
| 102 | + "Could not set nice value of compression thread to " << wanted_nice_value << " : " << std::strerror( |
| 103 | + errno)); |
| 104 | + } |
| 105 | + } else { |
| 106 | + ROSBAG2_COMPRESSION_LOG_WARN_STREAM( |
| 107 | + "Could not set nice value of compression thread to " << wanted_nice_value << |
| 108 | + " : Could not determine cur nice value"); |
| 109 | + } |
| 110 | +#endif |
| 111 | + } |
| 112 | + |
65 | 113 | // Every thread needs to have its own compression context for thread safety.
|
66 | 114 | auto compressor = compression_factory_->create_compressor(
|
67 | 115 | compression_options_.compression_format);
|
|
0 commit comments