Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress debug support #109

Merged
merged 4 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ Options
Generate a LAZ file with spatially arranged data and hierarchy information
[(COPC)](https://github.com/copcio/copcio.github.io). [Default: false]

- progress_fd

File descriptor number of a pipe using the Untwine API to send progress and error messages.
[Default: -1]

- progress_debug

Set to true to have progress messages written to standard output. Disabled if 'progress_fd'
is set to '1'. [Default: false]
1 change: 1 addition & 0 deletions untwine/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct Options
size_t fileLimit;
int level;
int progressFd;
bool progressDebug;
StringList dimNames;
bool stats;
std::string a_srs;
Expand Down
53 changes: 24 additions & 29 deletions untwine/ProgressWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,31 @@
namespace untwine
{

ProgressWriter::ProgressWriter() : m_progressFd(-1), m_percent(0.0), m_increment(.01)
ProgressWriter::ProgressWriter() : m_fd(-1), m_debug(false), m_percent(0.0), m_increment(.01)
{}

ProgressWriter::ProgressWriter(int fd) : m_progressFd(fd), m_percent(0.0), m_increment(.01)
{}

void ProgressWriter::setFd(int fd)
void ProgressWriter::init(int fd, bool debug)
{
m_progressFd = fd;
m_fd = fd;
m_debug = debug;
}

void ProgressWriter::setIncrement(double increment)
{
if (m_progressFd < 0)
return;
std::unique_lock<std::mutex> lock(m_mutex);

m_increment = increment;
}

void ProgressWriter::setPercent(double percent)
{
if (m_progressFd < 0)
return;
std::unique_lock<std::mutex> lock(m_mutex);

m_percent = (std::max)(0.0, ((std::min)(1.0, percent)));
}

void ProgressWriter::writeIncrement(const std::string& message)
{
if (m_progressFd < 0)
return;
std::unique_lock<std::mutex> lock(m_mutex);

m_percent += m_increment;
Expand All @@ -57,8 +49,6 @@ void ProgressWriter::writeIncrement(const std::string& message)

void ProgressWriter::write(double percent, const std::string& message)
{
if (m_progressFd < 0)
return;
std::unique_lock<std::mutex> lock(m_mutex);

m_percent = (std::min)(0.0, ((std::max)(1.0, percent)));
Expand All @@ -69,22 +59,27 @@ void ProgressWriter::write(double percent, const std::string& message)

void ProgressWriter::writeMessage(uint32_t percent, const std::string& message)
{
if (m_debug)
std::cout << "Untwine progress (" << percent << "% done): " << message << "\n";
if (m_fd < 0)
return;

const int32_t msgId = 1000;
#ifndef _WIN32
bool err = false;
err = (::write(m_progressFd, &msgId, sizeof(msgId)) == -1);
err |= (::write(m_progressFd, &percent, sizeof(percent)) == -1);
err = (::write(m_fd, &msgId, sizeof(msgId)) == -1);
err |= (::write(m_fd, &percent, sizeof(percent)) == -1);
uint32_t ssize = (uint32_t)message.size();
err |= (::write(m_progressFd, &ssize, sizeof(ssize)) == -1);
err |= (::write(m_progressFd, message.data(), ssize) == -1);
err |= (::write(m_fd, &ssize, sizeof(ssize)) == -1);
err |= (::write(m_fd, message.data(), ssize) == -1);
if (err)
{
::close(m_progressFd);
m_progressFd = -1;
::close(m_fd);
m_fd = -1;
}
#else
DWORD numWritten;
HANDLE h = reinterpret_cast<HANDLE>((intptr_t)m_progressFd);
HANDLE h = reinterpret_cast<HANDLE>((intptr_t)m_fd);
WriteFile(h, &msgId, sizeof(msgId), &numWritten, NULL);
WriteFile(h, &percent, sizeof(percent), &numWritten, NULL);
uint32_t ssize = (uint32_t)message.size();
Expand All @@ -95,27 +90,27 @@ void ProgressWriter::writeMessage(uint32_t percent, const std::string& message)

void ProgressWriter::writeErrorMessage(const std::string& message)
{
if (m_progressFd < 0)
if (m_fd < 0)
{
std::cerr << message << "\n";
std::cerr << "Untwine Error: " << message << "\n";
return;
}

const int32_t msgId = 1001;
#ifndef _WIN32
bool err = false;
err = (::write(m_progressFd, &msgId, sizeof(msgId)) == -1);
err = (::write(m_fd, &msgId, sizeof(msgId)) == -1);
uint32_t ssize = (uint32_t)message.size();
err |= (::write(m_progressFd, &ssize, sizeof(ssize)) == -1);
err |= (::write(m_progressFd, message.data(), ssize) == -1);
err |= (::write(m_fd, &ssize, sizeof(ssize)) == -1);
err |= (::write(m_fd, message.data(), ssize) == -1);
if (err)
{
::close(m_progressFd);
m_progressFd = -1;
::close(m_fd);
m_fd = -1;
}
#else
DWORD numWritten;
HANDLE h = reinterpret_cast<HANDLE>((intptr_t)m_progressFd);
HANDLE h = reinterpret_cast<HANDLE>((intptr_t)m_fd);
WriteFile(h, &msgId, sizeof(msgId), &numWritten, NULL);
uint32_t ssize = (uint32_t)message.size();
WriteFile(h, &ssize, sizeof(ssize), &numWritten, NULL);
Expand Down
8 changes: 4 additions & 4 deletions untwine/ProgressWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ class ProgressWriter
static const PointCount ChunkSize = 100'000;

ProgressWriter();
ProgressWriter(int fd);

// Set the progress file descriptor.
void setFd(int fd);
// Set the progress config..
void init(int fd = -1, bool debug = false);

/// Set the increment to use on the next call to setIncrement.
void setIncrement(double increment);
Expand All @@ -36,7 +35,8 @@ class ProgressWriter

private:
std::mutex m_mutex;
int m_progressFd;
int m_fd;
bool m_debug;
double m_percent; // Current percent.
double m_increment; // Current increment.

Expand Down
12 changes: 9 additions & 3 deletions untwine/Untwine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ void addArgs(pdal::ProgramArgs& programArgs, Options& options, pdal::Arg * &temp
options.outputName);
programArgs.add("single_file,s", "Create a single output file", options.singleFile);
tempArg = &(programArgs.add("temp_dir", "Temp directory", options.tempDir));
programArgs.add("clean_temp_dir", "Remove files from the temp directory",
options.cleanTempDir, true);
programArgs.add("cube", "Make a cube, rather than a rectangular solid", options.doCube, true);
programArgs.add("level", "Set an initial tree level, rather than guess based on data",
options.level, -1);
programArgs.add("file_limit", "Only load 'file_limit' files, even if more exist",
options.fileLimit, (size_t)10000000);
programArgs.add("progress_fd", "File descriptor on which to write progress messages.",
options.progressFd, -1);
programArgs.add("progress_debug", "Send progress info to stdout.", options.progressDebug);
programArgs.add("dims", "Dimensions to load. Note that X, Y and Z are always "
"loaded.", options.dimNames);
programArgs.add("stats", "Generate statistics for dimensions in the manner of Entwine.",
Expand Down Expand Up @@ -86,6 +85,13 @@ bool handleOptions(pdal::StringList& arglist, Options& options)
}
if (options.singleFile)
options.stats = true;

//
if (options.progressFd == 1 && options.progressDebug)
{
std::cerr << "'--progress_fd' set to 1. Disabling '--progressDebug'.\n";
options.progressDebug = false;
}
}
catch (const pdal::arg_error& err)
{
Expand Down Expand Up @@ -139,7 +145,7 @@ int main(int argc, char *argv[])
{
if (!handleOptions(arglist, options))
return 0;
progress.setFd(options.progressFd);
progress.init(options.progressFd, options.progressDebug);
createDirs(options);

epf::Epf preflight(common);
Expand Down