Skip to content

Commit

Permalink
#1933: Utils: Change TraceSpec to more generic FileSpec, so it can be…
Browse files Browse the repository at this point in the history
… used by both Trace and LB
  • Loading branch information
JacobDomagala committed Sep 29, 2022
1 parent 98a5ffa commit ce53e75
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 71 deletions.
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ set(
serialization
serialization/messaging serialization/traits serialization/auto_dispatch
serialization/sizing
utils/demangle utils/container utils/bits utils/mutex
utils/demangle utils/container utils/bits utils/mutex utils/file_spec
utils/hash utils/atomic utils/tls utils/static_checks utils/string
utils/memory utils/mpi_limits utils/compress utils/json utils/strong
registry/auto
Expand All @@ -107,7 +107,6 @@ set(
objgroup/proxy objgroup/holder objgroup/active_func objgroup/dispatch
objgroup/type_registry
trace
trace/file_spec
)
list(APPEND PROJECT_SUBDIRS_LIST ${TOP_LEVEL_SUBDIRS})

Expand Down
12 changes: 7 additions & 5 deletions src/vt/trace/trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include "vt/timing/timing.h"
#include "vt/trace/trace.h"
#include "vt/trace/trace_user.h"
#include "vt/trace/file_spec/spec.h"
#include "vt/utils/file_spec/spec.h"
#include "vt/objgroup/headers.h"
#include "vt/utils/memory/memory_usage.h"
#include "vt/phase/phase_manager.h"
Expand Down Expand Up @@ -122,8 +122,10 @@ void Trace::finalize() /*override*/ {
}

void Trace::loadAndBroadcastSpec() {
using namespace ::vt::utils::file_spec;

if (theConfig()->vt_trace_spec) {
auto spec_proxy = file_spec::TraceSpec::construct();
auto spec_proxy = FileSpec::construct(FileSpecType::TRACE);

theTerm()->produce();
if (theContext()->getNode() == 0) {
Expand Down Expand Up @@ -501,9 +503,9 @@ TraceEventIDType Trace::messageRecv(
void Trace::setTraceEnabledCurrentPhase(PhaseType cur_phase) {
if (spec_proxy_ != vt::no_obj_group) {
// SpecIndex is signed due to negative/positive, phase is not signed
auto spec_index = static_cast<file_spec::TraceSpec::SpecIndex>(cur_phase);
vt::objgroup::proxy::Proxy<file_spec::TraceSpec> proxy(spec_proxy_);
bool ret = proxy.get()->checkTraceEnabled(spec_index);
auto spec_index = static_cast<::vt::utils::file_spec::FileSpec::SpecIndex>(cur_phase);
vt::objgroup::proxy::Proxy<utils::file_spec::FileSpec> proxy(spec_proxy_);
bool ret = proxy.get()->checkEnabled(spec_index);

if (trace_enabled_cur_phase_ != ret) {
// N.B. Future endProcessing calls are required to close the current
Expand Down
91 changes: 54 additions & 37 deletions src/vt/trace/file_spec/spec.cc → src/vt/utils/file_spec/spec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,65 +42,82 @@
*/

#include "vt/config.h"
#include "vt/trace/file_spec/spec.h"
#include "vt/utils/file_spec/spec.h"
#include "vt/objgroup/manager.h"

#include <fstream>

namespace vt { namespace trace { namespace file_spec {
namespace vt { namespace utils { namespace file_spec {

void TraceSpec::init(ProxyType in_proxy) {
void FileSpec::init(ProxyType in_proxy, FileSpecType in_type) {
proxy_ = in_proxy;
type_ = in_type;
}

bool TraceSpec::checkTraceEnabled(SpecIndex in_phase) {
bool FileSpec::checkEnabled(SpecIndex in_phase) {
vtAssertExpr(has_spec_);

for (auto&& elm : spec_mod_) {
if (elm.second.testTraceEnabledFor(in_phase)) {
if (elm.second.testEnabledFor(in_phase)) {
return true;
}
}
for (auto&& elm : spec_exact_) {
if (elm.second.testTraceEnabledFor(in_phase)) {
if (elm.second.testEnabledFor(in_phase)) {
return true;
}
}
return false;
}

bool TraceSpec::hasSpec() {
if (theConfig()->vt_trace_spec) {
if (theConfig()->vt_trace_spec_file == "") {
vtAbort(
"--vt_trace_spec enabled but no file name is specified:"
" --vt_trace_spec_file"
);
return false;
} else {
auto& filename = theConfig()->vt_trace_spec_file;
std::ifstream file(filename);
if (not file.good()) {
auto str = fmt::format(
"--vt_trace_spec_file={} is not a valid file", filename
);
vtAbort(str);
bool FileSpec::hasSpec() {
auto checkSpecFile = [](
bool spec_enabled, std::string const& spec_file,
std::string const& spec_type) {
if (spec_enabled) {
if (spec_file == "") {
vtAbort(fmt::format(
"--vt_{}_spec enabled but no file name is specified:"
" --vt_{}_spec_file",
spec_type, spec_type));
return false;
} else {
return true;
std::ifstream file(spec_file);
if (not file.good()) {
auto str = fmt::format(
"--vt_{}_spec_file={} is not a valid file", spec_type, spec_file);
vtAbort(str);
return false;
} else {
return true;
}
}
} else {
return false;
}
} else {
};

if (type_ == FileSpecType::TRACE) {
return checkSpecFile(
theConfig()->vt_trace_spec, theConfig()->vt_trace_spec_file, "trace");
} else if (type_ == FileSpecType::LB) {
return checkSpecFile(
theConfig()->vt_lb_spec, theConfig()->vt_lb_spec_file, "lb");
}
else{
vtAbort("Unknown Spec Type");
return false;
}
}

void TraceSpec::parse() {
void FileSpec::parse() {
if (not hasSpec()) {
return;
}

auto& filename = theConfig()->vt_trace_spec_file;
auto& filename = type_ == FileSpecType::TRACE ?
theConfig()->vt_trace_spec_file :
theConfig()->vt_lb_spec_file;
std::ifstream file(filename);
vtAssertExpr(file.good());

Expand Down Expand Up @@ -182,7 +199,7 @@ void TraceSpec::parse() {

vt_debug_print(
verbose, trace,
"TraceSpec::parser: is_mod={}, phase={}, neg={}, pos={}\n",
"FileSpec::parser: is_mod={}, phase={}, neg={}, pos={}\n",
is_mod, phase, phase_negative_offset, phase_positive_offset
);

Expand All @@ -196,14 +213,14 @@ void TraceSpec::parse() {
has_spec_ = true;
}

void TraceSpec::broadcastSpec() {
void FileSpec::broadcastSpec() {
auto root = theContext()->getNode();
proxy_.template broadcast<SpecMsg, &TraceSpec::transferSpec>(
proxy_.template broadcast<SpecMsg, &FileSpec::transferSpec>(
spec_mod_, spec_exact_, root
);
}

void TraceSpec::transferSpec(SpecMsg* msg) {
void FileSpec::transferSpec(SpecMsg* msg) {
// The broadcast will hit all nodes, so the node that it exists on will
// ignore it
if (not has_spec_) {
Expand All @@ -213,7 +230,7 @@ void TraceSpec::transferSpec(SpecMsg* msg) {
}
}

void TraceSpec::insertSpec(
void FileSpec::insertSpec(
SpecIndex phase, SpecIndex neg, SpecIndex pos, bool is_mod, SpecMapType& map
) {
vtAbortIf(
Expand All @@ -222,7 +239,7 @@ void TraceSpec::insertSpec(
"Parsing file \"{}\" error: multiple lines start with the same {}:"
" value \"{}{}\"",
is_mod ? "mod phase" : "phase",
theConfig()->vt_trace_spec_file,
type_ == FileSpecType::TRACE ? theConfig()->vt_trace_spec_file : theConfig()->vt_lb_spec_file,
is_mod ? "%" : "",
phase
)
Expand All @@ -234,17 +251,17 @@ void TraceSpec::insertSpec(
);
}

int TraceSpec::eatWhitespace(std::ifstream& file) {
int FileSpec::eatWhitespace(std::ifstream& file) {
while (not file.eof() and std::isspace(file.peek()) and file.peek() != '\n') {
file.get();
}
return file.eof() ? 0 : file.peek();
}

/*static*/ typename TraceSpec::ProxyType TraceSpec::construct() {
auto proxy = theObjGroup()->makeCollective<TraceSpec>("TraceSpec");
proxy.get()->init(proxy);
/*static*/ typename FileSpec::ProxyType FileSpec::construct(FileSpecType type) {
auto proxy = theObjGroup()->makeCollective<FileSpec>(GetSpecName(type));
proxy.get()->init(proxy, type);
return proxy;
}

}}} /* end namespace vt::trace::file_spec */
}}} /* end namespace vt::utils::file_spec */
44 changes: 30 additions & 14 deletions src/vt/trace/file_spec/spec.h → src/vt/utils/file_spec/spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,33 @@
//@HEADER
*/

#if !defined INCLUDED_VT_TRACE_FILE_SPEC_SPEC_H
#define INCLUDED_VT_TRACE_FILE_SPEC_SPEC_H
#if !defined INCLUDED_VT_UTILS_FILE_SPEC_SPEC_H
#define INCLUDED_VT_UTILS_FILE_SPEC_SPEC_H

#include "vt/config.h"
#include "vt/objgroup/proxy/proxy_objgroup.h"
#include "vt/collective/reduce/operators/default_msg.h"

#include <unordered_map>

namespace vt { namespace trace { namespace file_spec {
namespace vt { namespace utils { namespace file_spec {

enum class FileSpecType { TRACE, LB };

inline std::string GetSpecName(FileSpecType type) {
switch (type) {
case FileSpecType::TRACE:
return "TraceSpec";
break;
case FileSpecType::LB:
return "LbSpec";
break;
default:
return "UnknownSpec";
}
}
/**
* \struct TraceSpec spec.h vt/trace/file_spec/spec.h
* \struct FileSpec spec.h vt/trace/file_spec/spec.h
*
* \brief Parses trace spec file when available and tests when its enabled. A
* single node parses the specification; all others receive the spec from a
Expand Down Expand Up @@ -93,8 +107,8 @@ namespace vt { namespace trace { namespace file_spec {
* }
*
*/
struct TraceSpec {
using ProxyType = vt::objgroup::proxy::Proxy<TraceSpec>;
struct FileSpec {
using ProxyType = vt::objgroup::proxy::Proxy<FileSpec>;
using SpecIndex = int64_t;
using DoneMsg = collective::ReduceNoneMsg;

Expand Down Expand Up @@ -128,7 +142,7 @@ struct TraceSpec {
*
* \return whether tracing is enabled based on this entry
*/
bool testTraceEnabledFor(SpecIndex in_idx) const {
bool testEnabledFor(SpecIndex in_idx) const {
if (is_mod_) {
// Check if mod idx_ is within range, by dividing out idx_ we get the
// pos/neg offset from that mod value
Expand Down Expand Up @@ -160,19 +174,20 @@ struct TraceSpec {

private:
/**
* \brief Initialize the \c TraceSpec objgroup
* \brief Initialize the \c FileSpec objgroup
*
* \param[in] in_proxy the objgroup proxy
* \param[in] in_type type of Spec
*/
void init(ProxyType in_proxy);
void init(ProxyType in_proxy, FileSpecType in_type);

public:
/**
* \brief Construct a new \c TraceSpec objgroup
* \brief Construct a new \c FileSpec objgroup
*
* \return the proxy
*/
static ProxyType construct();
static ProxyType construct(FileSpecType type);

/**
* \brief Check entire spec to see if tracing is enabled on any of the entries
Expand All @@ -181,7 +196,7 @@ struct TraceSpec {
*
* \return whether tracing is enabled
*/
bool checkTraceEnabled(SpecIndex in_phase);
bool checkEnabled(SpecIndex in_phase);

/**
* \brief Check if a specification is enabled, file specified, and file
Expand Down Expand Up @@ -269,9 +284,10 @@ struct TraceSpec {
ProxyType proxy_;
SpecMapType spec_mod_;
SpecMapType spec_exact_;
FileSpecType type_;
bool has_spec_ = false;
};

}}} /* end namespace vt::trace::file_spec */
}}} /* end namespace vt::utils::file_spec */

#endif /*INCLUDED_VT_TRACE_FILE_SPEC_SPEC_H*/
#endif /*INCLUDED_VT_UTILS_FILE_SPEC_SPEC_H*/
Loading

0 comments on commit ce53e75

Please sign in to comment.