root/software/photo_compress_archiver/PhotoCompressArchiver.cc @ 0b45b84e
| b216a5b0 | David Sorber | #include <chrono>
|
||
| 328d0fa5 | David Sorber | #include <cstdio>
|
||
| f8e600bc | David Sorber | #include <iomanip>
|
||
| b216a5b0 | David Sorber | #include <iostream>
|
||
| e0b7f8b9 | David Sorber | #include <limits.h>
|
||
| b216a5b0 | David Sorber | #include <sys/stat.h>
|
||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include "PhotoCompressArchiver.hh"
|
||||
#define OUT(x) {\
|
||||
std::lock_guard<std::mutex> lock(m_output_mutex);\
|
||||
x\
|
||||
}
|
||||
| 3eeb1485 | David Sorber | const uint32_t PTR_LEN = sizeof(char*);
|
||
const uint32_t BAILOUT_MAX = 600; // 30s in 50ms increments
|
||||
const uint32_t FILES_PER_WORK_UNIT = 40;
|
||||
| e0b7f8b9 | David Sorber | const char *PROG_NAME = "./packjpg";
|
||
| b216a5b0 | David Sorber | const char *PROG_OPT1 = "-np";
|
||
const char *PROG_OPT2 = "-o";
|
||||
const char *PROG_OPT3 = "-p";
|
||||
| e0b7f8b9 | David Sorber | const uint32_t ARGV_MAX_SIZE(sysconf(_SC_ARG_MAX));
|
||
| 57e6c22f | David Sorber | const uint32_t ARGV_EXTRA(4096);
|
||
| e0b7f8b9 | David Sorber | |||
| b216a5b0 | David Sorber | PhotoCompressArchiver::PhotoCompressArchiver(
|
||
| 328d0fa5 | David Sorber | const std::string& path,
|
||
uint32_t num_cores,
|
||||
bool decompress,
|
||||
| e0b7f8b9 | David Sorber | bool keep_orig,
|
||
boost::regex& filter_regex)
|
||||
| b216a5b0 | David Sorber | : m_path(path),
|
||
m_num_cores(num_cores),
|
||||
| 328d0fa5 | David Sorber | m_decompress(decompress),
|
||
m_keep_orig(keep_orig),
|
||||
| f8e600bc | David Sorber | m_files_processed(0),
|
||
| b216a5b0 | David Sorber | m_num_input_files(0),
|
||
m_total_uncompressed_size(0),
|
||||
m_total_compressed_size(0),
|
||||
| e0b7f8b9 | David Sorber | p_finder_thread(nullptr),
|
||
m_filter_empty(true),
|
||||
m_filter_regex(filter_regex)
|
||||
{
|
||||
// This is a somewhat crude way to determining if the filter regex is empty
|
||||
m_filter_empty = std::string("").compare(filter_regex.str()) == 0;
|
||||
| 57e6c22f | David Sorber | |||
// Remove this...
|
||||
| e0b7f8b9 | David Sorber | std::cout << "argv size: " << sysconf(_SC_ARG_MAX) << std::endl;
|
||
}
|
||||
| b216a5b0 | David Sorber | |||
PhotoCompressArchiver::~PhotoCompressArchiver()
|
||||
{}
|
||||
int PhotoCompressArchiver::execute()
|
||||
{
|
||||
// Start the file finder thread
|
||||
OUT(std::cout << "Starting file finder..." << std::flush;);
|
||||
p_finder_thread = new std::thread(&PhotoCompressArchiver::find_files, this,
|
||||
| 328d0fa5 | David Sorber | m_path,
|
||
(m_decompress ? PJG_REGEX : JPEG_REGEX));
|
||||
| b216a5b0 | David Sorber | OUT(std::cout << "DONE" << std::endl;)
|
||
| 3eeb1485 | David Sorber | |||
// Start worker threads
|
||||
OUT(std::cout << "Starting worker threads..." << std::flush;)
|
||||
uint32_t num_threads = m_num_cores;
|
||||
for (uint32_t idx = 0; idx < num_threads; ++idx)
|
||||
{
|
||||
// Spawn worker thread and add to list for bookkeeping
|
||||
std::thread* worker = new std::thread(&PhotoCompressArchiver::fork_worker,
|
||||
this, idx);
|
||||
m_worker_threads.push_back(worker);
|
||||
}
|
||||
OUT(std::cout << "DONE\n" << std::endl;)
|
||||
// Join the file finder thread
|
||||
| b216a5b0 | David Sorber | p_finder_thread->join();
|
||
OUT(std::cout << "File finder completed:" << std::endl;)
|
||||
| 57e6c22f | David Sorber | // Error out of no input files were found
|
||
if (m_num_input_files == 0)
|
||||
{
|
||||
OUT(std::cerr << BOLD << RED << "ERROR: " << ENDC << "No matching "
|
||||
<< "input files found. Please check the patch and try "
|
||||
<< "again. " << std::endl;)
|
||||
return -1;
|
||||
}
|
||||
| 3eeb1485 | David Sorber | OUT(std::cout << " Found " << m_num_input_files << " files" << std::endl;)
|
||
| 328d0fa5 | David Sorber | |||
if (! m_decompress)
|
||||
{
|
||||
OUT(std::cout << " Total uncompressed bytes: "
|
||||
<< m_total_uncompressed_size << "\n" << std::endl;)
|
||||
}
|
||||
| b216a5b0 | David Sorber | |||
| 3eeb1485 | David Sorber | // Build work unit queue
|
||
uint32_t input_list_idx = 0;
|
||||
uint32_t queue_size = 0;
|
||||
while (input_list_idx < m_file_list.size())
|
||||
| b216a5b0 | David Sorber | {
|
||
| 3eeb1485 | David Sorber | // Safely read the size of the queue
|
||
| b216a5b0 | David Sorber | {
|
||
| 3eeb1485 | David Sorber | std::lock_guard<std::mutex> lock(m_work_unit_mtx);
|
||
queue_size = m_work_unit_queue.size();
|
||||
| b216a5b0 | David Sorber | }
|
||
| 3eeb1485 | David Sorber | |||
// Wait until the queue has decreased in the size before proceeding,
|
||||
// otherwise we could chew up a bunch of memory
|
||||
if (queue_size > num_threads)
|
||||
| b216a5b0 | David Sorber | {
|
||
| 3eeb1485 | David Sorber | std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||
continue;
|
||||
| b216a5b0 | David Sorber | }
|
||
| 3eeb1485 | David Sorber | WorkUnit* work_unit = new WorkUnit();
|
||
work_unit->exec_argv.push_back(&PROG_NAME[2]);
|
||||
work_unit->exec_argv.push_back(PROG_OPT1);
|
||||
work_unit->exec_argv.push_back(PROG_OPT2);
|
||||
work_unit->exec_argv.push_back(PROG_OPT3);
|
||||
uint32_t argv_size = sizeof(PROG_NAME) + sizeof(PROG_OPT1) +
|
||||
sizeof(PROG_OPT2) + sizeof(PROG_OPT3) + ARGV_EXTRA;
|
||||
| b216a5b0 | David Sorber | |||
| 3eeb1485 | David Sorber | uint32_t local_count = 0;
|
||
while ((input_list_idx < m_file_list.size()) &&
|
||||
(local_count < FILES_PER_WORK_UNIT))
|
||||
{
|
||||
bfs::path& file_path = m_file_list[input_list_idx];
|
||||
uint32_t path_len = file_path.string().size();
|
||||
// Check if adding this next file would make the argv too big; if
|
||||
// so bail out
|
||||
if (argv_size + path_len + PTR_LEN > ARGV_MAX_SIZE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
// Add the file to the argv array
|
||||
work_unit->exec_argv.push_back(file_path.string().c_str());
|
||||
argv_size += path_len + PTR_LEN;
|
||||
// Create new filename for expected
|
||||
uint32_t end_pos = file_path.string().rfind(".");
|
||||
std::string* filename = new std::string(file_path.string().begin(),
|
||||
file_path.string().begin()
|
||||
+ end_pos);
|
||||
// Add extension depending on if decompress mode is enabled or not
|
||||
(*filename) += (m_decompress ? ".jpg" : ".pjg");
|
||||
work_unit->output_filenames.push_back(filename);
|
||||
// Increment our index to the next file
|
||||
++input_list_idx;
|
||||
++local_count;
|
||||
}
|
||||
work_unit->exec_argv.push_back(nullptr);
|
||||
// Add the create work unit to the queue; and notify the worker threads
|
||||
// that it has been handed off
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_work_unit_mtx);
|
||||
m_work_unit_queue.push_back(work_unit);
|
||||
}
|
||||
m_work_unit_cv.notify_all();
|
||||
| b216a5b0 | David Sorber | }
|
||
| 3eeb1485 | David Sorber | |||
// Add null terminate indicator for each thread
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_work_unit_mtx);
|
||||
for (uint32_t idx = 0; idx < num_threads; ++idx)
|
||||
{
|
||||
m_work_unit_queue.push_back(nullptr);
|
||||
}
|
||||
}
|
||||
m_work_unit_cv.notify_all();
|
||||
| b216a5b0 | David Sorber | |||
// Clean up the finder thread
|
||||
delete p_finder_thread;
|
||||
p_finder_thread = nullptr;
|
||||
// Now join the worker threads
|
||||
for (auto worker : m_worker_threads)
|
||||
{
|
||||
worker->join();
|
||||
delete worker;
|
||||
}
|
||||
| cec188e4 | David Sorber | // Print out compression information if compressing
|
||
| 328d0fa5 | David Sorber | if (! m_decompress)
|
||
{
|
||||
OUT(std::cout << "\n Total compressed bytes: "
|
||||
<< m_total_compressed_size
|
||||
<< std::endl;)
|
||||
| f6c21781 | David Sorber | |||
| 328d0fa5 | David Sorber | // Calculate and display ratio
|
||
double ratio = (double)m_total_compressed_size / m_total_uncompressed_size;
|
||||
OUT(std::cout << " Compression ratio: " << std::fixed
|
||||
<< std::setprecision(4) << (ratio * 100) << "%\n"
|
||||
<< std::endl;)
|
||||
}
|
||||
| b216a5b0 | David Sorber | |||
return 0;
|
||||
}
|
||||
void PhotoCompressArchiver::find_files(
|
||||
const bfs::path& dir_path,
|
||||
const boost::regex& file_regex)
|
||||
{
|
||||
bfs::directory_iterator end_iter;
|
||||
for (bfs::directory_iterator dir_iter(dir_path); dir_iter != end_iter; ++dir_iter)
|
||||
{
|
||||
if (bfs::is_directory(dir_iter->status()))
|
||||
{
|
||||
// Found directory recurse
|
||||
find_files(dir_iter->path(), file_regex);
|
||||
}
|
||||
else if (boost::regex_match(dir_iter->path().filename().string(), file_regex))
|
||||
{
|
||||
// Found file match
|
||||
| e0b7f8b9 | David Sorber | |||
// If the filter is not empty, then check against it before proceeding
|
||||
if (! m_filter_empty)
|
||||
{
|
||||
if (boost::regex_match(dir_iter->path().filename().string(),
|
||||
m_filter_regex))
|
||||
{
|
||||
// The filter regex matched, skip this file
|
||||
continue;
|
||||
}
|
||||
}
|
||||
| b216a5b0 | David Sorber | m_file_list.push_back(dir_iter->path());
|
||
++m_num_input_files;
|
||||
// Stat the file to get its size
|
||||
struct stat statbuf;
|
||||
int rc = stat(dir_iter->path().string().c_str(), &statbuf);
|
||||
if (rc)
|
||||
{
|
||||
| 328d0fa5 | David Sorber | OUT(std::cerr << BOLD << RED << "ERROR: " << ENDC
|
||
<< "unable to stat file " << dir_iter->path()
|
||||
<< std::endl;)
|
||||
| b216a5b0 | David Sorber | }
|
||
m_total_uncompressed_size += statbuf.st_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
void PhotoCompressArchiver::fork_worker(
|
||||
| 3eeb1485 | David Sorber | uint32_t tid)
|
||
| b216a5b0 | David Sorber | {
|
||
| 3eeb1485 | David Sorber | // Loop vars
|
||
| 57e6c22f | David Sorber | int status;
|
||
| 3eeb1485 | David Sorber | uint32_t work_unit_items = 0;
|
||
uint32_t bailout_ctr = 0;
|
||||
uint32_t output_idx = 1;
|
||||
int stat_rc = 0;
|
||||
struct stat statbuf;
|
||||
uint32_t files_processed = 0;
|
||||
double complete_percent = 0.0;
|
||||
| e0b7f8b9 | David Sorber | |||
| 3eeb1485 | David Sorber | while (true)
|
||
| b216a5b0 | David Sorber | {
|
||
| 57e6c22f | David Sorber | |||
| 3eeb1485 | David Sorber | std::unique_lock<std::mutex> mux_lock(m_work_unit_mtx);
|
||
while (m_work_unit_queue.empty())
|
||||
{
|
||||
m_work_unit_cv.wait_for(mux_lock, std::chrono::milliseconds(20));
|
||||
}
|
||||
// While holding the mutex grab the next chunk off of the add chunk
|
||||
// queue
|
||||
WorkUnit* work_unit = m_work_unit_queue.front();
|
||||
m_work_unit_queue.pop_front();
|
||||
mux_lock.unlock();
|
||||
| b216a5b0 | David Sorber | |||
| 3eeb1485 | David Sorber | if (work_unit == nullptr)
|
||
| e0b7f8b9 | David Sorber | {
|
||
| 3eeb1485 | David Sorber | break;
|
||
| e0b7f8b9 | David Sorber | }
|
||
| 57e6c22f | David Sorber | |||
| 3eeb1485 | David Sorber | work_unit_items = work_unit->output_filenames.size();
|
||
| 43c9a095 | David Sorber | #if 0
|
||
| c8352650 | David Sorber | // DEBUGGING
|
||
| 43c9a095 | David Sorber | uint32_t idx = 4;
|
||
| 2423ae90 | David Sorber | OUT(
|
||
| 3eeb1485 | David Sorber | for (auto outfile : work_unit->output_filenames)
|
||
| c8352650 | David Sorber | {
|
||
| 2423ae90 | David Sorber | std::cout << "T[" << tid << "] output file: " << *outfile
|
||
| 43c9a095 | David Sorber | << "\n input file: "
|
||
| 3eeb1485 | David Sorber | << static_cast<const char*>(work_unit->exec_argv[idx++])
|
||
<< std::endl;
|
||||
| c8352650 | David Sorber | }
|
||
| 2423ae90 | David Sorber | )
|
||
| 3eeb1485 | David Sorber | continue;
|
||
| c8352650 | David Sorber | #endif
|
||
| 57e6c22f | David Sorber | // Create a pipe to hold stdout from child process
|
||
int filedes[2];
|
||||
if (pipe(filedes) == -1)
|
||||
| b216a5b0 | David Sorber | {
|
||
| 57e6c22f | David Sorber | perror("pipe");
|
||
exit(1);
|
||||
| f8e600bc | David Sorber | }
|
||
| 57e6c22f | David Sorber | // Fork off the childprocess
|
||
//~ pid_t parent = getpid();
|
||||
pid_t pid = vfork();
|
||||
if (pid == -1)
|
||||
| f8e600bc | David Sorber | {
|
||
| 57e6c22f | David Sorber | // error, failed to fork()
|
||
OUT(std::cout << "T[" << tid << "] fork failed!" << std::endl;)
|
||||
}
|
||||
else if (pid == 0)
|
||||
{
|
||||
// This is the child process...
|
||||
// Set child process's stdout to the pipe entry
|
||||
| 2423ae90 | David Sorber | while ((dup2(filedes[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
|
||
close(filedes[0]);
|
||||
close(filedes[1]);
|
||||
| cec188e4 | David Sorber | |||
| 57e6c22f | David Sorber | // Child after fork
|
||
| 3eeb1485 | David Sorber | int rc = execv(PROG_NAME, (char **)work_unit->exec_argv.data());
|
||
| 57e6c22f | David Sorber | if (rc)
|
||
{
|
||||
std::cerr << "execv failed: " << errno << std::endl;
|
||||
}
|
||||
_exit(EXIT_FAILURE); // exec never returns
|
||||
| f8e600bc | David Sorber | }
|
||
| 57e6c22f | David Sorber | |||
// This is the parent process...
|
||||
// This is a little silly... but it works. We know the order in which
|
||||
// the output files will be created so we wait until output files X + 1
|
||||
// exists (i.e. we can stat() it) and then process file X.
|
||||
| 3eeb1485 | David Sorber | bailout_ctr = 0;
|
||
output_idx = 1;
|
||||
stat_rc = 0;
|
||||
files_processed = 0;
|
||||
complete_percent = 0.0;
|
||||
while (output_idx < work_unit_items)
|
||||
| 57e6c22f | David Sorber | {
|
||
| 3eeb1485 | David Sorber | // Poll, waiting for output file X + 1 (aka "z") to exist
|
||
| 57e6c22f | David Sorber | bailout_ctr = 0;
|
||
| 3eeb1485 | David Sorber | const char* filename_z = work_unit->output_filenames[output_idx]->c_str();
|
||
while ((stat(filename_z, &statbuf) != 0) && (++bailout_ctr < BAILOUT_MAX))
|
||||
| 57e6c22f | David Sorber | {
|
||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
// Check if timeout has occurred
|
||||
if (bailout_ctr == BAILOUT_MAX)
|
||||
{
|
||||
OUT(std::cerr << "T[" << tid << "] " << BOLD << RED << "ERROR: "
|
||||
<< ENDC << "timed out while waiting "
|
||||
<< "for output file: "
|
||||
| 3eeb1485 | David Sorber | << *work_unit->output_filenames[output_idx]
|
||
<< "; aborting" << std::endl;)
|
||||
| 57e6c22f | David Sorber | |||
// The subprocess timed out... get its status
|
||||
waitpid(pid, &status, 0);
|
||||
OUT(std::cerr << "T[" << tid << "] " << BOLD << RED << "ERROR: "
|
||||
<< ENDC << PURPLE << " RC => " << status << ENDC
|
||||
<< std::endl;)
|
||||
| f8e600bc | David Sorber | |||
| 57e6c22f | David Sorber | return;
|
||
}
|
||||
| 3eeb1485 | David Sorber | // Now stat output file X (aka "y") which should exist
|
||
const char* filename_y = work_unit->output_filenames[output_idx - 1]->c_str();
|
||||
stat_rc = stat(filename_y, &statbuf);
|
||||
| 57e6c22f | David Sorber | if (stat_rc)
|
||
{
|
||||
OUT(std::cerr << "T[" << tid << "] " << BOLD << RED << "ERROR "
|
||||
| 3eeb1485 | David Sorber | << ENDC << "while stating: " << filename_y
|
||
<< std::endl;)
|
||||
| 57e6c22f | David Sorber | }
|
||
// Increment counts
|
||||
m_total_compressed_size += statbuf.st_size;
|
||||
++m_files_processed;
|
||||
++files_processed;
|
||||
// Unless keeping original files, remove the source file
|
||||
if (! m_keep_orig)
|
||||
{
|
||||
| 3eeb1485 | David Sorber | std::remove(work_unit->exec_argv[4 + output_idx - 1]);
|
||
| 57e6c22f | David Sorber | }
|
||
// Calculate the local percent done
|
||||
| 3eeb1485 | David Sorber | complete_percent = (double)files_processed / work_unit_items;
|
||
| 57e6c22f | David Sorber | complete_percent *= 100;
|
||
OUT(std::cout << "T[" << tid << "] " << std::setw(6)
|
||||
<< std::fixed << std::setprecision(2)
|
||||
<< get_global_percent_done()
|
||||
| 3eeb1485 | David Sorber | << "% -- (" << std::setw(2) << files_processed << "/"
|
||
<< work_unit_items << " -- " << std::setw(6)
|
||||
| 57e6c22f | David Sorber | << complete_percent
|
||
| 3eeb1485 | David Sorber | << "%) file: " << filename_y
|
||
| 57e6c22f | David Sorber | << " -- " << statbuf.st_size << std::endl;)
|
||
++output_idx;
|
||||
}
|
||||
// Wait for forked child process to terminate
|
||||
waitpid(pid, &status, 0);
|
||||
| 2423ae90 | David Sorber | close(filedes[0]);
|
||
close(filedes[1]);
|
||||
| 57e6c22f | David Sorber | |||
// Now that the forked process has completed, we can handle the last
|
||||
// output file
|
||||
| 3eeb1485 | David Sorber | const char* filename_y = work_unit->output_filenames[output_idx - 1]->c_str();
|
||
stat_rc = stat(filename_y, &statbuf);
|
||||
| f8e600bc | David Sorber | if (stat_rc)
|
||
{
|
||||
| 57e6c22f | David Sorber | OUT(std::cerr << "T[" << tid << "] " << BOLD << RED << "ERROR "
|
||
| 3eeb1485 | David Sorber | << ENDC << "while stating: " << filename_y << std::endl;)
|
||
| f8e600bc | David Sorber | }
|
||
// Increment counts
|
||||
| b216a5b0 | David Sorber | m_total_compressed_size += statbuf.st_size;
|
||
| f8e600bc | David Sorber | ++m_files_processed;
|
||
++files_processed;
|
||||
| 328d0fa5 | David Sorber | // Unless keeping original files, remove the source file
|
||
if (! m_keep_orig)
|
||||
{
|
||||
| 3eeb1485 | David Sorber | std::remove(work_unit->exec_argv[4 + output_idx - 1]);
|
||
| 328d0fa5 | David Sorber | }
|
||
| f8e600bc | David Sorber | // Calculate the local percent done
|
||
| 3eeb1485 | David Sorber | complete_percent = (double)files_processed / work_unit_items;
|
||
| f8e600bc | David Sorber | complete_percent *= 100;
|
||
| 57e6c22f | David Sorber | OUT(std::cout << "T[" << tid << "] " << std::setw(6) << std::fixed
|
||
<< std::setprecision(2) << get_global_percent_done()
|
||||
| 3eeb1485 | David Sorber | << "% -- (" << std::setw(2) << files_processed
|
||
<< "/" << work_unit_items << " -- " << std::setw(5)
|
||||
<< complete_percent << "%) file: " << filename_y
|
||||
| f8e600bc | David Sorber | << " -- " << statbuf.st_size << std::endl;)
|
||
| 57e6c22f | David Sorber | // Free up the output filename list
|
||
| 3eeb1485 | David Sorber | for (auto output_filename : work_unit->output_filenames)
|
||
| 57e6c22f | David Sorber | {
|
||
delete output_filename;
|
||||
}
|
||||
| 3eeb1485 | David Sorber | delete work_unit;
|
||
| 328d0fa5 | David Sorber | }
|
||
| 57e6c22f | David Sorber | OUT(std::cout << "T[" << tid << "] " << PURPLE << "complete ==> RC: "
|
||
| 328d0fa5 | David Sorber | << status << ENDC << std::endl;)
|
||
| f8e600bc | David Sorber | }
|
||
double PhotoCompressArchiver::get_global_percent_done()
|
||||
{
|
||||
return ((double)m_files_processed / m_num_input_files) * 100;
|
||||
| b216a5b0 | David Sorber | }
|