Skip to content

Commit

Permalink
* Added debug_to_kb to Threader
Browse files Browse the repository at this point in the history
* Added tests for checking KB and control contents in Threader
  • Loading branch information
jredmondson committed Sep 23, 2018
1 parent c5251cb commit e43b44a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 14 deletions.
15 changes: 15 additions & 0 deletions include/madara/threads/Threader.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ namespace madara
**/
void terminate (const std::string name);

/**
* Requests all debugging for threads go into the data plane
* KB instead of the control plane. This will impact performance
* of your main knowledge base, so you should use it sparingly,
* if possible.
* @param prefix prefix to save debug info into data plane KB
**/
void debug_to_kb (const std::string prefix = ".threader");

/**
* Requests a specific thread to disable debug mode. Debug mode
* prints thread performance information such as durations
Expand Down Expand Up @@ -274,6 +283,12 @@ namespace madara
* indicates if threads should be started in debug mode
**/
bool debug_;

/**
* if not empty, user has specified debug information should
* go to the data plane at this prefix
**/
std::string debug_to_kb_prefix_;
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions include/madara/threads/Threader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ madara::threads::Threader::disable_debug (const std::string name)
control_.set (name + ".debug", false);
}

inline void
madara::threads::Threader::debug_to_kb (const std::string prefix)
{
debug_ = true;
control_.set (".debug_to_kb", prefix);
}

inline void
madara::threads::Threader::enable_debug (void)
{
Expand Down
24 changes: 17 additions & 7 deletions include/madara/threads/WorkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ WorkerThread::WorkerThread (
if (thread)
{
std::stringstream base_string;

knowledge::KnowledgeBase * kb = &control;
knowledge::KnowledgeRecord debug_to_kb = control_.get (".debug_to_kb");
if (debug_to_kb.exists ())
{
base_string << debug_to_kb.to_string () << ".";
kb = &data;
}
base_string << name;

thread->name = name;
thread->init_control_vars (control);

control_.get (".debug_to_kb").to_string ();

finished_.set_name (
base_string.str () + ".finished", control);
started_.set_name (
Expand All @@ -39,20 +49,20 @@ WorkerThread::WorkerThread (
base_string.str () + ".hertz", control);

executions_.set_name (
base_string.str () + ".executions", control);
base_string.str () + ".executions", *kb);
start_time_.set_name (
base_string.str () + ".start_time", control);
base_string.str () + ".start_time", *kb);
last_start_time_.set_name (
base_string.str () + ".last_start_time", control);
base_string.str () + ".last_start_time", *kb);
end_time_.set_name (
base_string.str () + ".end_time", control);
base_string.str () + ".end_time", *kb);

last_duration_.set_name (
base_string.str () + ".last_duration", control);
base_string.str () + ".last_duration", *kb);
min_duration_.set_name (
base_string.str () + ".min_duration", control);
base_string.str () + ".min_duration", *kb);
max_duration_.set_name (
base_string.str () + ".max_duration", control);
base_string.str () + ".max_duration", *kb);

debug_.set_name (
base_string.str () + ".debug", control);
Expand Down
84 changes: 77 additions & 7 deletions tests/threads/test_threader_introspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Integer target (50);

Integer counters (1);
Integer readers (0);
int madara_fails = 0;

// handle command line arguments
void handle_arguments (int argc, char ** argv)
Expand Down Expand Up @@ -166,13 +167,51 @@ class EmptyThread : public threads::BaseThread
std::string message;
};

int main (int argc, char ** argv)
void test_debug_to_kb_introspection (void)
{
knowledge::KnowledgeBase kb;
threads::Threader threader (kb);
threader.debug_to_kb (".threader");

madara_logger_ptr_log (logger::global_logger.get(), logger::LOG_ALWAYS,
"Testing debugging to kb for 5 seconds...\n");

for (Integer i = 0; i < counters; ++i)
{
std::stringstream buffer;
buffer << "thread";
buffer << i;

threader.run (hertz, buffer.str (), new EmptyThread (), true);
}

int64_t estimated_count = hertz * 4;

threader.resume ();

// sleep for 5 seconds before starting second hertz rate
utility::sleep (5.0);

kb.print ();

std::cerr << "Result of test was: ";

if (kb.get (".threader.thread0.executions") >= estimated_count)
{
std::cerr << "SUCCESS\n";
}
else
{
++madara_fails;
std::cerr << "FAIL. Knowledge was:\n";
kb.print ();
}
}

void test_debug_to_control (void)
{
// handle all user arguments
handle_arguments (argc, argv);

// create a knowledge base and setup our id
knowledge::KnowledgeBase knowledge;
knowledge::KnowledgeBase kb;

madara_logger_ptr_log (logger::global_logger.get(), logger::LOG_ALWAYS,
"Hertz rate set to %f\n"
Expand All @@ -190,7 +229,7 @@ int main (int argc, char ** argv)
estimated_time);

// create a threader for running threads
threads::Threader threader (knowledge);
threads::Threader threader (kb);

threader.enable_debug ();

Expand Down Expand Up @@ -272,5 +311,36 @@ int main (int argc, char ** argv)

threader.wait ();

return 0;
std::cerr << "Result of test was: ";

if (control.get ("thread0.executions") >= 0)
{
std::cerr << "SUCCESS\n";
}
else
{
++madara_fails;
std::cerr << "FAIL. Knowledge was:\n";
kb.print ();
}
}

int main (int argc, char ** argv)
{
// handle all user arguments
handle_arguments (argc, argv);

test_debug_to_kb_introspection ();
test_debug_to_control ();

if (madara_fails > 0)
{
std::cerr << "OVERALL: FAIL. " << madara_fails << " tests failed.\n";
}
else
{
std::cerr << "OVERALL: SUCCESS.\n";
}

return madara_fails;
}

0 comments on commit e43b44a

Please sign in to comment.