Skip to content

Commit d5166d5

Browse files
authored
Merge pull request #1648 from callumfare/callum/tracing_logger
Add ability to log from the tracing layer directly rather than use XPTI
2 parents 689c8c8 + 9d4d091 commit d5166d5

File tree

10 files changed

+1389
-30
lines changed

10 files changed

+1389
-30
lines changed

scripts/core/INTRO.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ Unified Runtime loader implements tracing support through the `XPTI framework <h
182182
| **user_data**: A pointer to `function_with_args_t` object, that includes function ID, name, arguments, and return value.
183183
- None
184184

185+
The Unified Runtime tracing layer also supports logging tracing output directly, rather than using XPTI. Use the `UR_LOG_TRACING` environment variable to control this output. See the `Logging`_ section below for details of the syntax. All traces are logged at the *info* log level.
186+
185187
Sanitizers
186188
---------------------
187189

@@ -318,6 +320,10 @@ Specific environment variables can be set to control the behavior of unified run
318320

319321
Holds parameters for setting Unified Runtime validation logging. The syntax is described in the Logging_ section.
320322

323+
.. envvar:: UR_LOG_TRACING
324+
325+
Holds parameters for setting Unified Runtime tracing logging. The syntax is described in the Logging_ section.
326+
321327
.. envvar:: UR_ADAPTERS_FORCE_LOAD
322328

323329
Holds a comma-separated list of library paths used by the loader for adapter discovery. By setting this value you can

scripts/templates/trcddi.cpp.mako

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ namespace ur_tracing_layer
4545
${th.make_pfncb_param_type(n, tags, obj)} params = { &${",&".join(th.make_param_lines(n, tags, obj, format=["name"]))} };
4646
uint64_t instance = context.notify_begin(${th.make_func_etor(n, tags, obj)}, "${th.make_func_name(n, tags, obj)}", &params);
4747

48+
context.logger.info("---> ${th.make_func_name(n, tags, obj)}");
49+
4850
${x}_result_t result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} );
4951

5052
context.notify_end(${th.make_func_etor(n, tags, obj)}, "${th.make_func_name(n, tags, obj)}", &params, &result, instance);
5153

54+
std::ostringstream args_str;
55+
ur::extras::printFunctionParams(args_str, ${th.make_func_etor(n, tags, obj)}, &params);
56+
context.logger.info("({}) -> {};\n", args_str.str(), result);
57+
5258
return result;
5359
}
5460
%if 'condition' in obj:
@@ -112,6 +118,10 @@ namespace ur_tracing_layer
112118
return result;
113119
}
114120

121+
// Recreate the logger in case env variables have been modified between
122+
// program launch and the call to `urLoaderInit`
123+
logger = logger::create_logger("tracing", true, true);
124+
115125
ur_tracing_layer::context.codelocData = codelocData;
116126

117127
%for tbl in th.get_pfntables(specs, meta, n, tags):

source/common/logger/ur_logger.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
namespace logger {
1616

17-
Logger create_logger(std::string logger_name, bool skip_prefix = false);
17+
Logger create_logger(std::string logger_name, bool skip_prefix = false,
18+
bool skip_linebreak = false);
1819

1920
inline Logger &get_logger(std::string name = "common") {
2021
static Logger logger = create_logger(std::move(name));
@@ -105,7 +106,8 @@ template <typename T> inline std::string toHex(T t) {
105106
/// - flush level: error, meaning that only error messages are guaranteed
106107
/// to be printed immediately as they occur
107108
/// - output: stderr
108-
inline Logger create_logger(std::string logger_name, bool skip_prefix) {
109+
inline Logger create_logger(std::string logger_name, bool skip_prefix,
110+
bool skip_linebreak) {
109111
std::transform(logger_name.begin(), logger_name.end(), logger_name.begin(),
110112
::toupper);
111113
std::stringstream env_var_name;
@@ -121,7 +123,7 @@ inline Logger create_logger(std::string logger_name, bool skip_prefix) {
121123
auto map = getenv_to_map(env_var_name.str().c_str());
122124
if (!map.has_value()) {
123125
return Logger(std::make_unique<logger::StderrSink>(
124-
std::move(logger_name), skip_prefix));
126+
std::move(logger_name), skip_prefix, skip_linebreak));
125127
}
126128

127129
auto kv = map->find("level");
@@ -150,19 +152,20 @@ inline Logger create_logger(std::string logger_name, bool skip_prefix) {
150152
<< map->begin()->first
151153
<< "'. Default logger options are set.";
152154
return Logger(std::make_unique<logger::StderrSink>(
153-
std::move(logger_name), skip_prefix));
155+
std::move(logger_name), skip_prefix, skip_linebreak));
154156
}
155157

156-
sink =
157-
values.size() == 2
158-
? sink_from_str(logger_name, values[0], values[1], skip_prefix)
159-
: sink_from_str(logger_name, values[0], "", skip_prefix);
158+
sink = values.size() == 2
159+
? sink_from_str(logger_name, values[0], values[1],
160+
skip_prefix, skip_linebreak)
161+
: sink_from_str(logger_name, values[0], "", skip_prefix,
162+
skip_linebreak);
160163
} catch (const std::invalid_argument &e) {
161164
std::cerr << "Error when creating a logger instance from the '"
162165
<< env_var_name.str() << "' environment variable:\n"
163166
<< e.what() << std::endl;
164167
return Logger(std::make_unique<logger::StderrSink>(
165-
std::move(logger_name), skip_prefix));
168+
std::move(logger_name), skip_prefix, skip_linebreak));
166169
}
167170
sink->setFlushLevel(flush_level);
168171

source/common/logger/ur_logger_details.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Logger {
2525
Logger(logger::Level level, std::unique_ptr<logger::Sink> sink)
2626
: level(level), sink(std::move(sink)) {}
2727

28+
Logger &operator=(Logger &&) = default;
2829
~Logger() = default;
2930

3031
void setLevel(logger::Level level) { this->level = level; }

source/common/logger/ur_sinks.hpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class Sink {
3939
std::ostream *ostream;
4040
logger::Level flush_level;
4141

42-
Sink(std::string logger_name, bool skip_prefix = false)
43-
: logger_name(std::move(logger_name)), skip_prefix(skip_prefix) {
42+
Sink(std::string logger_name, bool skip_prefix = false,
43+
bool skip_linebreak = false)
44+
: logger_name(std::move(logger_name)), skip_prefix(skip_prefix),
45+
skip_linebreak(skip_linebreak) {
4446
ostream = nullptr;
4547
flush_level = logger::Level::ERR;
4648
}
@@ -56,6 +58,7 @@ class Sink {
5658
private:
5759
std::string logger_name;
5860
bool skip_prefix;
61+
bool skip_linebreak;
5962
std::mutex output_mutex;
6063
const char *error_prefix = "Log message syntax error: ";
6164

@@ -83,7 +86,9 @@ class Sink {
8386
}
8487
}
8588
}
86-
buffer << "\n";
89+
if (!skip_linebreak) {
90+
buffer << "\n";
91+
}
8792
}
8893

8994
template <typename Arg, typename... Args>
@@ -129,14 +134,15 @@ class Sink {
129134

130135
class StdoutSink : public Sink {
131136
public:
132-
StdoutSink(std::string logger_name, bool skip_prefix = false)
133-
: Sink(std::move(logger_name), skip_prefix) {
137+
StdoutSink(std::string logger_name, bool skip_prefix = false,
138+
bool skip_linebreak = false)
139+
: Sink(std::move(logger_name), skip_prefix, skip_linebreak) {
134140
this->ostream = &std::cout;
135141
}
136142

137143
StdoutSink(std::string logger_name, Level flush_lvl,
138-
bool skip_prefix = false)
139-
: StdoutSink(std::move(logger_name), skip_prefix) {
144+
bool skip_prefix = false, bool skip_linebreak = false)
145+
: StdoutSink(std::move(logger_name), skip_prefix, skip_linebreak) {
140146
this->flush_level = flush_lvl;
141147
}
142148

@@ -145,13 +151,15 @@ class StdoutSink : public Sink {
145151

146152
class StderrSink : public Sink {
147153
public:
148-
StderrSink(std::string logger_name, bool skip_prefix = false)
149-
: Sink(std::move(logger_name), skip_prefix) {
154+
StderrSink(std::string logger_name, bool skip_prefix = false,
155+
bool skip_linebreak = false)
156+
: Sink(std::move(logger_name), skip_prefix, skip_linebreak) {
150157
this->ostream = &std::cerr;
151158
}
152159

153-
StderrSink(std::string logger_name, Level flush_lvl, bool skip_prefix)
154-
: StderrSink(std::move(logger_name), skip_prefix) {
160+
StderrSink(std::string logger_name, Level flush_lvl, bool skip_prefix,
161+
bool skip_linebreak)
162+
: StderrSink(std::move(logger_name), skip_prefix, skip_linebreak) {
155163
this->flush_level = flush_lvl;
156164
}
157165

@@ -161,8 +169,8 @@ class StderrSink : public Sink {
161169
class FileSink : public Sink {
162170
public:
163171
FileSink(std::string logger_name, filesystem::path file_path,
164-
bool skip_prefix = false)
165-
: Sink(std::move(logger_name), skip_prefix) {
172+
bool skip_prefix = false, bool skip_linebreak = false)
173+
: Sink(std::move(logger_name), skip_prefix, skip_linebreak) {
166174
ofstream = std::ofstream(file_path);
167175
if (!ofstream.good()) {
168176
std::stringstream ss;
@@ -174,8 +182,10 @@ class FileSink : public Sink {
174182
}
175183

176184
FileSink(std::string logger_name, filesystem::path file_path,
177-
Level flush_lvl, bool skip_prefix = false)
178-
: FileSink(std::move(logger_name), std::move(file_path), skip_prefix) {
185+
Level flush_lvl, bool skip_prefix = false,
186+
bool skip_linebreak = false)
187+
: FileSink(std::move(logger_name), std::move(file_path), skip_prefix,
188+
skip_linebreak) {
179189
this->flush_level = flush_lvl;
180190
}
181191

@@ -188,14 +198,17 @@ class FileSink : public Sink {
188198
inline std::unique_ptr<Sink> sink_from_str(std::string logger_name,
189199
std::string name,
190200
filesystem::path file_path = "",
191-
bool skip_prefix = false) {
201+
bool skip_prefix = false,
202+
bool skip_linebreak = false) {
192203
if (name == "stdout" && file_path.empty()) {
193-
return std::make_unique<logger::StdoutSink>(logger_name, skip_prefix);
204+
return std::make_unique<logger::StdoutSink>(logger_name, skip_prefix,
205+
skip_linebreak);
194206
} else if (name == "stderr" && file_path.empty()) {
195-
return std::make_unique<logger::StderrSink>(logger_name, skip_prefix);
207+
return std::make_unique<logger::StderrSink>(logger_name, skip_prefix,
208+
skip_linebreak);
196209
} else if (name == "file" && !file_path.empty()) {
197210
return std::make_unique<logger::FileSink>(logger_name, file_path,
198-
skip_prefix);
211+
skip_prefix, skip_linebreak);
199212
}
200213

201214
throw std::invalid_argument(

source/loader/layers/tracing/ur_tracing_layer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ constexpr auto STREAM_VER_MINOR = UR_MINOR_VERSION(UR_API_VERSION_CURRENT);
2727
static thread_local xpti_td *activeEvent;
2828

2929
///////////////////////////////////////////////////////////////////////////////
30-
context_t::context_t() {
30+
context_t::context_t() : logger(logger::create_logger("tracing", true, true)) {
3131
xptiFrameworkInitialize();
3232

3333
call_stream_id = xptiRegisterStream(CALL_STREAM_NAME);
@@ -37,7 +37,7 @@ context_t::context_t() {
3737
streamv.str().data());
3838
}
3939

40-
bool context_t::isAvailable() const { return xptiTraceEnabled(); }
40+
bool context_t::isAvailable() const { return true; }
4141

4242
void context_t::notify(uint16_t trace_type, uint32_t id, const char *name,
4343
void *args, ur_result_t *resultp, uint64_t instance) {

source/loader/layers/tracing/ur_tracing_layer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef UR_TRACING_LAYER_H
1414
#define UR_TRACING_LAYER_H 1
1515

16+
#include "logger/ur_logger.hpp"
1617
#include "ur_ddi.h"
1718
#include "ur_proxy_layer.hpp"
1819
#include "ur_util.hpp"
@@ -25,6 +26,7 @@ class __urdlllocal context_t : public proxy_layer_context_t {
2526
public:
2627
ur_dditable_t urDdiTable = {};
2728
codeloc_data codelocData;
29+
logger::Logger logger;
2830

2931
context_t();
3032
~context_t();

0 commit comments

Comments
 (0)