Adding rocprofilerv2
Change-Id: Ic0cc280ba207d2b8f6ccae1cd4ac3184152fc1ad
Этот коммит содержится в:
@@ -0,0 +1,116 @@
|
||||
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#ifndef SRC_UTILS_ACCESS_CONTROL_H_
|
||||
#define SRC_UTILS_ACCESS_CONTROL_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
// TODO(aelwazir): To be implemented
|
||||
|
||||
// type for reference counter
|
||||
typedef std::atomic<uint64_t> RocmToolsRefCount;
|
||||
|
||||
// type for access/release control flag
|
||||
typedef std::atomic<bool> RocmToolsARControl;
|
||||
|
||||
namespace rocmtools {
|
||||
|
||||
// the access/release control flag can be shared or exclusive
|
||||
static RocmToolsARControl kARShared = ATOMIC_VAR_INIT(0);
|
||||
static RocmToolsARControl kARExclusive = ATOMIC_VAR_INIT(1);
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @Synopsis A class for defining access mode for any resource and
|
||||
* maintaining the refernce count for accesses to the resource.
|
||||
* All the resources must include this class object for corectness
|
||||
*/
|
||||
/*--------------------------------------------------------------------------*/
|
||||
class AccessControl {
|
||||
public:
|
||||
//!< constructor that defaults the RefCount and ARControl
|
||||
AccessControl(RocmToolsRefCount rrc = ATOMIC_VAR_INIT(0),
|
||||
RocmToolsARControl rac = ATOMIC_VAR_INIT(kARShared.load()));
|
||||
|
||||
//!< naive destructor
|
||||
~AccessControl();
|
||||
|
||||
bool Acquire(RocmToolsARControl access_mode);
|
||||
void Release(RocmToolsARControl access_mode);
|
||||
|
||||
private:
|
||||
RocmToolsRefCount rcount_; //!< refernce count, every successful access
|
||||
//! increments it
|
||||
RocmToolsARControl arcontrol_; //!< shared access or exclusive?
|
||||
|
||||
std::mutex accessmutex_;
|
||||
};
|
||||
|
||||
//<!
|
||||
// AccessControl::AccessControl(RocmToolsRefCount rrc, RocmToolsARControl rac)
|
||||
// {
|
||||
// rcount_ = rrc.load();
|
||||
// arcontrol_ = rac.load();
|
||||
// }
|
||||
|
||||
//<!
|
||||
AccessControl::~AccessControl() {
|
||||
// we cannot decrement reference count here, we can only hope it is 0
|
||||
#ifdef ROCMTOOL_DEBUG_PRINT
|
||||
int zero = 0;
|
||||
if (rcount_.compare_exchange_strong(zero&, 0, std::memory_order_acq_rel))
|
||||
cout << endl
|
||||
<< __FILE__ << __FUNC__ << __LINE__
|
||||
<< ": reference count not"
|
||||
" zero"
|
||||
<< endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0
|
||||
//!< three steps, 1) get scoped lock, 2) check if it is shared to shared
|
||||
//!< acquire, and 3) if it is exclusive, check if refcount is 0
|
||||
bool AccessControl::Acquire(RocmToolsARControl rac) {
|
||||
// first, ensure we have a lock_guard
|
||||
std::lock_guard<std::mutex> lg(accessmutex_);
|
||||
// we should always expect the current value to be shared
|
||||
RocmToolsARControl rac_expected = kARShared.load();
|
||||
// if what we have is what we expect then we are done
|
||||
if (rac.load(std::memory_order_relaxed) == rac_expected.load()) {
|
||||
// rcount_.store(std::memory_order_acq_rel,
|
||||
// rcount_load(std::memory_order_acq_rel)+1));
|
||||
return true;
|
||||
}
|
||||
// now, we have to switch from shared to exclusive, but we need the refcount
|
||||
// to be 0. If it is 0, return true, otherwise false
|
||||
// int zero = 0;
|
||||
// return rcount_.compare_exchange_strong(zero&, 0,
|
||||
// std::memory_order_acq_rel);
|
||||
}
|
||||
#endif
|
||||
//!< always exchange to kARShared and reduce refcount
|
||||
// void AccessControl::Release(
|
||||
|
||||
} // namespace rocmtools
|
||||
|
||||
#endif // SRC_UTILS_ACCESS_CONTROL_H_
|
||||
@@ -0,0 +1,53 @@
|
||||
/* Copyright (c) 2018-2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
#ifndef SRC_UTILS_EXCEPTION_H_
|
||||
#define SRC_UTILS_EXCEPTION_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "helper.h"
|
||||
#include "inc/rocprofiler.h"
|
||||
|
||||
// TODO(aelwazir): namespace rocmtool
|
||||
namespace rocmtools {
|
||||
|
||||
class Exception : public std::runtime_error {
|
||||
public:
|
||||
Exception() = delete;
|
||||
|
||||
explicit Exception(rocprofiler_status_t status, const char* what_arg = "")
|
||||
: std::runtime_error(std::string(rocprofiler_error_str(status)) + " " +
|
||||
what_arg),
|
||||
status_(status) {}
|
||||
|
||||
rocprofiler_status_t status() const noexcept { return status_; }
|
||||
|
||||
protected:
|
||||
const rocprofiler_status_t status_;
|
||||
};
|
||||
|
||||
}; // namespace rocmtool
|
||||
|
||||
// TODO(aelwazir): throw instead of the macros
|
||||
|
||||
#endif // SRC_UTILS_EXCEPTION_H_
|
||||
@@ -0,0 +1,114 @@
|
||||
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#ifndef UTILS_HANDLE_H_
|
||||
#define UTILS_HANDLE_H_
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <cstddef>
|
||||
|
||||
namespace rocmtools {
|
||||
|
||||
template <typename T, typename D>
|
||||
class handle_t {
|
||||
T value_;
|
||||
bool will_delete_;
|
||||
public:
|
||||
handle_t() noexcept
|
||||
: will_delete_(false)
|
||||
{}
|
||||
|
||||
template
|
||||
< typename U
|
||||
, std::enable_if_t<std::is_same<T, U>::value, bool> = true
|
||||
>
|
||||
handle_t(U &&v, bool const will_delete = true) noexcept
|
||||
: value_(std::move(v))
|
||||
, will_delete_(will_delete)
|
||||
{}
|
||||
|
||||
// No copy construction or copy assignment of handles
|
||||
handle_t(handle_t const &) = delete;
|
||||
handle_t& operator=(handle_t const &) = delete;
|
||||
|
||||
handle_t& operator=(handle_t &&h) noexcept
|
||||
{
|
||||
reset();
|
||||
value_ = std::move(h.value_);
|
||||
will_delete_ = h.will_delete_;
|
||||
h.release();
|
||||
return *this;
|
||||
}
|
||||
|
||||
handle_t(handle_t &&h) noexcept
|
||||
: value_(std::move(h.value_))
|
||||
{
|
||||
h.release();
|
||||
}
|
||||
|
||||
~handle_t() noexcept
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
T const& get() const noexcept
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
typename std::add_lvalue_reference
|
||||
< typename std::remove_pointer<T>::type
|
||||
>::type
|
||||
operator*() const noexcept
|
||||
{
|
||||
return *value_;
|
||||
}
|
||||
|
||||
T operator->() const noexcept
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
T const& release() noexcept
|
||||
{
|
||||
will_delete_ = false;
|
||||
return get();
|
||||
}
|
||||
|
||||
void reset() noexcept
|
||||
{
|
||||
if (will_delete_) {
|
||||
will_delete_ = false;
|
||||
D{}(value_);
|
||||
}
|
||||
}
|
||||
|
||||
void reset(T &&v) noexcept
|
||||
{
|
||||
reset();
|
||||
value_ = std::move(v);
|
||||
will_delete_ = true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rocmtools
|
||||
|
||||
#endif // UTILS_HANDLE_H_
|
||||
@@ -0,0 +1,238 @@
|
||||
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#include "helper.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <amd_comgr/amd_comgr.h>
|
||||
|
||||
#define amd_comgr_(call) \
|
||||
do { \
|
||||
if (amd_comgr_status_t status = amd_comgr_##call; status != AMD_COMGR_STATUS_SUCCESS) { \
|
||||
const char* reason = ""; \
|
||||
amd_comgr_status_string(status, &reason); \
|
||||
fatal(#call " failed: %s", reason); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
#if defined(ENABLE_BACKTRACE)
|
||||
|
||||
#include <backtrace.h>
|
||||
#include <cxxabi.h>
|
||||
|
||||
namespace {
|
||||
|
||||
struct BackTraceInfo {
|
||||
struct ::backtrace_state* state = nullptr;
|
||||
std::stringstream sstream{};
|
||||
int depth = 0;
|
||||
int error = 0;
|
||||
};
|
||||
|
||||
void errorCallback(void* data, const char* message, int errnum) {
|
||||
BackTraceInfo* info = static_cast<BackTraceInfo*>(data);
|
||||
info->sstream << "ROCMTools: error: " << message << '(' << errnum << ')';
|
||||
info->error = 1;
|
||||
}
|
||||
|
||||
void syminfoCallback(void* data, uintptr_t /* pc */, const char* symname, uintptr_t /* symval */,
|
||||
uintptr_t /* symsize */) {
|
||||
BackTraceInfo* info = static_cast<BackTraceInfo*>(data);
|
||||
|
||||
if (symname == nullptr) return;
|
||||
|
||||
int status;
|
||||
char* demangled = abi::__cxa_demangle(symname, nullptr, nullptr, &status);
|
||||
info->sstream << ' ' << (status == 0 ? demangled : symname);
|
||||
free(demangled);
|
||||
}
|
||||
|
||||
int fullCallback(void* data, uintptr_t pc, const char* filename, int lineno, const char* function) {
|
||||
BackTraceInfo* info = static_cast<BackTraceInfo*>(data);
|
||||
|
||||
info->sstream << std::endl
|
||||
<< " #" << std::dec << info->depth++ << ' ' << "0x" << std::noshowbase
|
||||
<< std::hex << std::setfill('0') << std::setw(sizeof(pc) * 2) << pc;
|
||||
if (function == nullptr) {
|
||||
backtrace_syminfo(info->state, pc, syminfoCallback, errorCallback, data);
|
||||
} else {
|
||||
int status;
|
||||
char* demangled = abi::__cxa_demangle(function, nullptr, nullptr, &status);
|
||||
info->sstream << ' ' << (status == 0 ? demangled : function);
|
||||
free(demangled);
|
||||
|
||||
if (filename != nullptr) {
|
||||
info->sstream << " in " << filename;
|
||||
if (lineno) info->sstream << ':' << std::dec << lineno;
|
||||
}
|
||||
}
|
||||
|
||||
return info->error;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
#endif // defined (ENABLE_BACKTRACE)
|
||||
|
||||
namespace rocmtools {
|
||||
|
||||
std::string string_vprintf(const char* format, va_list va) {
|
||||
va_list copy;
|
||||
|
||||
va_copy(copy, va);
|
||||
size_t size = vsnprintf(NULL, 0, format, copy);
|
||||
va_end(copy);
|
||||
|
||||
std::string str(size, '\0');
|
||||
vsprintf(&str[0], format, va);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string string_printf(const char* format, ...) {
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
std::string str(string_vprintf(format, va));
|
||||
va_end(va);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
[[maybe_unused]] void warning(const char* format, ...) {
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
vfprintf(stderr, format, va);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
[[maybe_unused]] void fatal [[noreturn]] (const char* format, ...) {
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
std::string message = string_vprintf(format, va);
|
||||
va_end(va);
|
||||
|
||||
#if defined(ENABLE_BACKTRACE)
|
||||
BackTraceInfo info;
|
||||
|
||||
info.sstream << std::endl << "Backtrace:";
|
||||
info.state = ::backtrace_create_state("/proc/self/exe", 0, errorCallback, &info);
|
||||
::backtrace_full(info.state, 1, fullCallback, errorCallback, &info);
|
||||
|
||||
message += info.sstream.str();
|
||||
#endif /* defined (ENABLE_BACKTRACE) */
|
||||
|
||||
std::string errmsg("ROCMTools: fatal error: " + message);
|
||||
fputs(errmsg.c_str(), stderr);
|
||||
|
||||
throw(errmsg);
|
||||
}
|
||||
|
||||
/* The function extracts the kernel name from
|
||||
input string. By using the iterators it finds the
|
||||
window in the string which contains only the kernel name.
|
||||
For example 'Foo<int, float>::foo(a[], int (int))' -> 'foo'*/
|
||||
std::string truncate_name(const std::string& name) {
|
||||
auto rit = name.rbegin();
|
||||
auto rend = name.rend();
|
||||
uint32_t counter = 0;
|
||||
char open_token = 0;
|
||||
char close_token = 0;
|
||||
while (rit != rend) {
|
||||
if (counter == 0) {
|
||||
switch (*rit) {
|
||||
case ')':
|
||||
counter = 1;
|
||||
open_token = ')';
|
||||
close_token = '(';
|
||||
break;
|
||||
case '>':
|
||||
counter = 1;
|
||||
open_token = '>';
|
||||
close_token = '<';
|
||||
break;
|
||||
case ']':
|
||||
counter = 1;
|
||||
open_token = ']';
|
||||
close_token = '[';
|
||||
break;
|
||||
case ' ':
|
||||
++rit;
|
||||
continue;
|
||||
}
|
||||
if (counter == 0) break;
|
||||
} else {
|
||||
if (*rit == open_token) counter++;
|
||||
if (*rit == close_token) counter--;
|
||||
}
|
||||
++rit;
|
||||
}
|
||||
auto rbeg = rit;
|
||||
while ((rit != rend) && (*rit != ' ') && (*rit != ':')) rit++;
|
||||
return name.substr(rend - rit, rit - rbeg);
|
||||
}
|
||||
|
||||
// C++ symbol demangle
|
||||
std::string cxx_demangle(const std::string& symbol) {
|
||||
amd_comgr_data_t mangled_data;
|
||||
amd_comgr_(create_data(AMD_COMGR_DATA_KIND_BYTES, &mangled_data));
|
||||
amd_comgr_(set_data(mangled_data, symbol.size(), symbol.data()));
|
||||
|
||||
amd_comgr_data_t demangled_data;
|
||||
amd_comgr_(demangle_symbol_name(mangled_data, &demangled_data));
|
||||
|
||||
size_t demangled_size = 0;
|
||||
amd_comgr_(get_data(demangled_data, &demangled_size, nullptr));
|
||||
|
||||
std::string demangled_str;
|
||||
demangled_str.resize(demangled_size);
|
||||
amd_comgr_(get_data(demangled_data, &demangled_size, demangled_str.data()));
|
||||
|
||||
amd_comgr_(release_data(mangled_data));
|
||||
amd_comgr_(release_data(demangled_data));
|
||||
return demangled_str;
|
||||
}
|
||||
|
||||
// check if string has special char
|
||||
bool has_special_char(std::string const& str) {
|
||||
return std::find_if(str.begin(), str.end(), [](unsigned char ch) {
|
||||
return !(isalnum(ch) || ch == '_' || ch == ':' || ch == ' ');
|
||||
}) != str.end();
|
||||
}
|
||||
|
||||
// check if string has correct counter format
|
||||
bool has_counter_format(std::string const& str) {
|
||||
return std::find_if(str.begin(), str.end(), [](unsigned char ch) {
|
||||
return (isalnum(ch) || ch == '_' || ch != ':');
|
||||
}) != str.end();
|
||||
}
|
||||
|
||||
// trims the begining of the line for spaces
|
||||
std::string left_trim(const std::string& s) {
|
||||
const std::string WHITESPACE = " \n\r\t\f\v";
|
||||
size_t start = s.find_first_not_of(WHITESPACE);
|
||||
return (start == std::string::npos) ? "" : s.substr(start);
|
||||
}
|
||||
|
||||
|
||||
} // namespace rocmtools
|
||||
@@ -0,0 +1,72 @@
|
||||
/* Copyright (c) 2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <cxxabi.h>
|
||||
#include "exception.h"
|
||||
|
||||
namespace rocmtools {
|
||||
|
||||
std::string string_vprintf(const char* format, va_list va);
|
||||
|
||||
std::string string_printf(const char* format, ...);
|
||||
|
||||
[[maybe_unused]] void warning(const char* format, ...)
|
||||
#if defined(__GNUC__)
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif /* defined (__GNUC__) */
|
||||
;
|
||||
[[maybe_unused]] void fatal [[noreturn]] (const char* format, ...)
|
||||
#if defined(__GNUC__)
|
||||
__attribute__((format(printf, 1, 2)))
|
||||
#endif /* defined (__GNUC__) */
|
||||
;
|
||||
|
||||
[[maybe_unused]] void warning(const char* format, ...);
|
||||
|
||||
[[maybe_unused]] void fatal [[noreturn]] (const char* format, ...);
|
||||
|
||||
/* The function extracts the kernel name from
|
||||
input string. By using the iterators it finds the
|
||||
window in the string which contains only the kernel name.
|
||||
For example 'Foo<int, float>::foo(a[], int (int))' -> 'foo'*/
|
||||
std::string truncate_name(const std::string& name);
|
||||
|
||||
// C++ symbol demangle
|
||||
std::string cxx_demangle(const std::string& symbol);
|
||||
|
||||
// check if string has special char
|
||||
bool has_special_char(std::string const& str);
|
||||
|
||||
// check if string has correct counter format
|
||||
bool has_counter_format(std::string const& str);
|
||||
|
||||
// trims the begining of the line for spaces
|
||||
std::string left_trim(const std::string& s);
|
||||
|
||||
} // namespace rocmtools
|
||||
@@ -0,0 +1,171 @@
|
||||
/* Copyright (c) 2018-2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#ifndef SRC_UTILS_LOGGER_H_
|
||||
#define SRC_UTILS_LOGGER_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace rocmtools {
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
template <typename T>
|
||||
Logger& operator<<(T&& m) {
|
||||
std::ostringstream oss;
|
||||
oss << std::forward<T>(m);
|
||||
if (!streaming_)
|
||||
Log(oss.str());
|
||||
else
|
||||
Put(oss.str());
|
||||
streaming_ = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
using manip_t = void (*)();
|
||||
Logger& operator<<(manip_t f) {
|
||||
f();
|
||||
return *this;
|
||||
}
|
||||
|
||||
static void begm() { Instance().ResetStreaming(true); }
|
||||
static void endl() { Instance().ResetStreaming(false); }
|
||||
|
||||
const std::string& LastMessage() {
|
||||
std::lock_guard lock(mutex_);
|
||||
return message_[GetTid()];
|
||||
}
|
||||
|
||||
static Logger& Instance() {
|
||||
static Logger instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
static uint32_t GetPid() { return syscall(__NR_getpid); }
|
||||
static uint32_t GetTid() { return syscall(__NR_gettid); }
|
||||
|
||||
private:
|
||||
Logger()
|
||||
: file_(nullptr), dirty_(false), streaming_(false), messaging_(false) {
|
||||
const char* var = getenv("ROCPROFILER_LOG");
|
||||
if (var != nullptr) file_ = fopen("/tmp/rocprofiler_log.txt", "a");
|
||||
ResetStreaming(false);
|
||||
}
|
||||
|
||||
~Logger() {
|
||||
if (file_ != nullptr) {
|
||||
if (dirty_) Put("\n");
|
||||
fclose(file_);
|
||||
}
|
||||
}
|
||||
|
||||
void ResetStreaming(const bool messaging) {
|
||||
std::lock_guard lock(mutex_);
|
||||
if (messaging) {
|
||||
message_[GetTid()] = "";
|
||||
} else if (streaming_) {
|
||||
Put("\n");
|
||||
dirty_ = false;
|
||||
}
|
||||
messaging_ = messaging;
|
||||
streaming_ = messaging;
|
||||
}
|
||||
|
||||
void Put(const std::string& m) {
|
||||
std::lock_guard lock(mutex_);
|
||||
if (messaging_) {
|
||||
message_[GetTid()] += m;
|
||||
}
|
||||
if (file_ != nullptr) {
|
||||
dirty_ = true;
|
||||
flock(fileno(file_), LOCK_EX);
|
||||
fprintf(file_, "%s", m.c_str());
|
||||
fflush(file_);
|
||||
flock(fileno(file_), LOCK_UN);
|
||||
}
|
||||
}
|
||||
|
||||
void Log(const std::string& m) {
|
||||
const time_t rawtime = time(nullptr);
|
||||
tm tm_info;
|
||||
localtime_r(&rawtime, &tm_info);
|
||||
char tm_str[26];
|
||||
strftime(tm_str, 26, "%Y-%m-%d %H:%M:%S", &tm_info);
|
||||
std::ostringstream oss;
|
||||
oss << "<" << tm_str << std::dec << " pid" << GetPid() << " tid"
|
||||
<< GetTid() << "> " << m;
|
||||
Put(oss.str());
|
||||
}
|
||||
|
||||
FILE* file_;
|
||||
bool dirty_;
|
||||
bool streaming_;
|
||||
bool messaging_;
|
||||
|
||||
std::recursive_mutex mutex_;
|
||||
std::map<uint32_t, std::string> message_;
|
||||
};
|
||||
|
||||
} // namespace rocmtools
|
||||
|
||||
#define FATAL_LOGGING(stream) \
|
||||
do { \
|
||||
rocmtools::Logger::Instance() << "fatal: " << rocmtools::Logger::begm \
|
||||
<< stream << rocmtools::Logger::endl; \
|
||||
throw(ROCPROFILER_STATUS_ERROR, "Error: " << stream); \
|
||||
} while (false)
|
||||
|
||||
#define ERR_LOGGING(stream) \
|
||||
do { \
|
||||
rocmtools::Logger::Instance() << "error: " << rocmtools::Logger::begm \
|
||||
<< stream << rocmtools::Logger::endl; \
|
||||
} while (false)
|
||||
|
||||
#define INFO_LOGGING(stream) \
|
||||
do { \
|
||||
rocmtools::Logger::Instance() << "info: " << rocmtools::Logger::begm \
|
||||
<< stream << rocmtools::Logger::endl; \
|
||||
} while (false)
|
||||
|
||||
#define WARN_LOGGING(stream) \
|
||||
do { \
|
||||
std::cerr << "ROCmTools: " << stream << std::endl; \
|
||||
rocmtools::Logger::Instance() << "warning: " << rocmtools::Logger::begm \
|
||||
<< stream << rocmtools::Logger::endl; \
|
||||
} while (false)
|
||||
|
||||
#endif // SRC_UTILS_LOGGER_H_
|
||||
Ссылка в новой задаче
Block a user