Skip to content

Commit

Permalink
valgrind: Enable running processes under valgrind
Browse files Browse the repository at this point in the history
Assumes /usr/bin/valgrind is the right location.
Create a file:  XORP_USE_VALGRIND in the local directory
that you are running xorp.  Start xorp_rtrmgr as normal
(possibly under valgrind as well).  Anything that xorp_rtrmgr
runs will be started under valgrind as well.

Also:  If xorp_do_run is false (shutting down process),
don't let eventloop sleep more than 1 second per loop.
This should fix hangs on shutdown that I (still) see in bgp.

Signed-off-by: Ben Greear <greearb@candelatech.com>
  • Loading branch information
greearb committed Jul 30, 2010
1 parent de48857 commit 37ded81
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
9 changes: 9 additions & 0 deletions xorp/libxorp/eventloop.cc
Expand Up @@ -197,6 +197,15 @@ EventLoop::do_work(bool can_block)
}
}

// If we are trying to shut down..make sure the even loop
// doesn't hang forever.
if (!xorp_do_run) {
if ((t == TimeVal::MAXIMUM()) ||
(t.to_ms() > 1000)) {
t = TimeVal(1, 0); // one sec
}
}

#ifdef HOST_OS_WINDOWS
_win_dispatcher.wait_and_dispatch(t);
#else
Expand Down
58 changes: 45 additions & 13 deletions xorp/libxorp/run_command.cc
Expand Up @@ -238,14 +238,6 @@ RunCommandBase::execute()
if (_is_running)
return (XORP_OK); // XXX: already running

// Create a single string with the command name and the arguments
string final_command = _command;
list<string>::const_iterator iter;
for (iter = _argument_list.begin(); iter != _argument_list.end(); ++iter) {
final_command += " ";
final_command += *iter;
}

//
// Save the current execution ID, and set the new execution ID
//
Expand All @@ -267,10 +259,50 @@ RunCommandBase::execute()
//
// Run the command
//
_pid = popen2(_command, _argument_list, _stdout_stream, _stderr_stream,
redirect_stderr_to_stdout());

ifstream tst("XORP_USE_VALGRIND");
if (tst) {
string vcmd("/usr/bin/valgrind");
list<string> args(_argument_list);

string uniq;
size_t slash = _command.find_last_of("/");
if (slash != string::npos) {
uniq = _command.substr(slash + 1);
}
else {
uniq = _command;
}

char* value = getenv("XORP_FINDER_SERVER_PORT");
if (value != NULL) {
uniq.append("-");
uniq.append(value);
}

// Move any old valgrind logs out of the way.
string lf0("valgrind_" + uniq + ".txt");
string lf1("valgrind_" + uniq + ".txt.1");
string lf2("valgrind_" + uniq + ".txt.2");
rename(lf1.c_str(), lf2.c_str());
rename(lf0.c_str(), lf1.c_str());

args.push_front(_command);
args.push_front("--track-origins=yes");
args.push_front("--leak-check=full");
args.push_front("--log-file=" + lf0);
tst.close();

_pid = popen2(vcmd, args, _stdout_stream, _stderr_stream,
redirect_stderr_to_stdout());
}
else {
_pid = popen2(_command, _argument_list, _stdout_stream, _stderr_stream,
redirect_stderr_to_stdout());
}

if (_stdout_stream == NULL) {
XLOG_ERROR("Failed to execute command \"%s\"", final_command.c_str());
XLOG_ERROR("Failed to execute command \"%s\"", _command.c_str());
cleanup();
_exec_id.restore_saved_exec_id(error_msg);
return (XORP_ERROR);
Expand All @@ -289,7 +321,7 @@ RunCommandBase::execute()
callback(this, &RunCommandBase::append_data));
if (! _stdout_file_reader->start()) {
XLOG_ERROR("Failed to start a stdout reader for command \"%s\"",
final_command.c_str());
_command.c_str());
cleanup();
_exec_id.restore_saved_exec_id(error_msg);
return (XORP_ERROR);
Expand All @@ -304,7 +336,7 @@ RunCommandBase::execute()
callback(this, &RunCommandBase::append_data));
if (! _stderr_file_reader->start()) {
XLOG_ERROR("Failed to start a stderr reader for command \"%s\"",
final_command.c_str());
_command.c_str());
cleanup();
_exec_id.restore_saved_exec_id(error_msg);
return (XORP_ERROR);
Expand Down
1 change: 0 additions & 1 deletion xorp/libxorp/timeval.cc
Expand Up @@ -18,7 +18,6 @@
// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net

// $XORP: xorp/libxorp/timeval.hh,v 1.40 2009/01/05 18:30:58 jtc Exp $

#include "timeval.hh"
#include "c_format.hh"
Expand Down
21 changes: 13 additions & 8 deletions xorp/libxorp/timeval.hh
Expand Up @@ -19,7 +19,6 @@
// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net

// $XORP: xorp/libxorp/timeval.hh,v 1.40 2009/01/05 18:30:58 jtc Exp $

#ifndef __LIBXORP_TIMEVAL_HH__
#define __LIBXORP_TIMEVAL_HH__
Expand All @@ -33,6 +32,10 @@

#include <math.h>


// TODO: Move this out-of-line (let compiler make the decision)
// TODO: Allow setting values, and remove return-by-value where possible.

/**
* @short TimeVal class
*
Expand Down Expand Up @@ -161,14 +164,14 @@ public:
#endif

/**
* Return an int32_t containing the total number of milliseconds
* Return an int64_t containing the total number of milliseconds
* in the underlying structure.
* This is intended for convenience when working with Win32 APIs.
* XXX: This may overflow if _sec is too big.
*
* @return the number of milliseconds in total.
*/
int32_t to_ms() const;
int64_t to_ms() const;

/**
* Convert a TimeVal value to a double-float value.
Expand Down Expand Up @@ -358,14 +361,16 @@ TimeVal::copy_out(timespec& timespec) const

#endif

inline int32_t
inline int64_t
TimeVal::to_ms() const
{
int32_t ms = _usec / 1000;
int64_t ms = _usec / 1000;
// Round a truncated fraction of <1ms to 1ms, not zero.
if (_sec == 0 && ms == 0)
return (1);
ms += _sec * 1000;
if (_sec == 0 && ms == 0 && _usec != 0)
ms = 1;
else {
ms += (int64_t)(_sec) * 1000LL;
}
return (ms);
}

Expand Down

0 comments on commit 37ded81

Please sign in to comment.