2019-06-24 21:05:12 -05:00
|
|
|
#ifndef SRC_CORE_TRACE_BUFFER_H_
|
|
|
|
|
#define SRC_CORE_TRACE_BUFFER_H_
|
|
|
|
|
|
2019-10-02 15:29:09 -05:00
|
|
|
#include <atomic>
|
2020-01-27 14:30:44 -06:00
|
|
|
#include <iostream>
|
2019-07-13 00:38:12 -05:00
|
|
|
#include <list>
|
|
|
|
|
#include <mutex>
|
2020-01-27 14:30:44 -06:00
|
|
|
#include <sstream>
|
|
|
|
|
|
2019-07-13 00:38:12 -05:00
|
|
|
#include <pthread.h>
|
2019-08-10 00:14:04 -05:00
|
|
|
#include <string.h>
|
2019-08-11 08:56:39 -05:00
|
|
|
#include <unistd.h>
|
2019-07-13 00:38:12 -05:00
|
|
|
|
2020-01-27 14:30:44 -06:00
|
|
|
#define FATAL(stream) \
|
|
|
|
|
do { \
|
|
|
|
|
std::ostringstream oss; \
|
|
|
|
|
oss << __FUNCTION__ << "(), " << stream; \
|
|
|
|
|
std::cout << oss.str() << std::endl; \
|
|
|
|
|
abort(); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2019-07-13 00:38:12 -05:00
|
|
|
#define PTHREAD_CALL(call) \
|
|
|
|
|
do { \
|
|
|
|
|
int err = call; \
|
|
|
|
|
if (err != 0) { \
|
|
|
|
|
errno = err; \
|
|
|
|
|
perror(#call); \
|
|
|
|
|
abort(); \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2019-06-24 21:05:12 -05:00
|
|
|
namespace roctracer {
|
|
|
|
|
enum {
|
|
|
|
|
TRACE_ENTRY_INV = 0,
|
|
|
|
|
TRACE_ENTRY_INIT = 1,
|
|
|
|
|
TRACE_ENTRY_COMPL = 2
|
|
|
|
|
};
|
2019-07-13 00:38:12 -05:00
|
|
|
|
2019-06-24 21:05:12 -05:00
|
|
|
enum {
|
|
|
|
|
API_ENTRY_TYPE,
|
|
|
|
|
COPY_ENTRY_TYPE,
|
|
|
|
|
KERNEL_ENTRY_TYPE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct trace_entry_t {
|
|
|
|
|
std::atomic<uint32_t> valid;
|
|
|
|
|
uint32_t type;
|
|
|
|
|
uint64_t dispatch;
|
|
|
|
|
uint64_t begin; // kernel begin timestamp, ns
|
|
|
|
|
uint64_t end; // kernel end timestamp, ns
|
|
|
|
|
uint64_t complete;
|
|
|
|
|
hsa_agent_t agent;
|
2019-08-11 08:56:39 -05:00
|
|
|
uint32_t dev_index;
|
2019-06-24 21:05:12 -05:00
|
|
|
hsa_signal_t orig;
|
|
|
|
|
hsa_signal_t signal;
|
|
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
} copy;
|
|
|
|
|
struct {
|
|
|
|
|
const char* name;
|
|
|
|
|
hsa_agent_t agent;
|
|
|
|
|
uint32_t tid;
|
|
|
|
|
} kernel;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-27 14:30:44 -06:00
|
|
|
template <class T>
|
|
|
|
|
struct push_element_fun {
|
|
|
|
|
T* const elem_;
|
|
|
|
|
void fun(T* node) { if (node->next_elem_ == NULL) node->next_elem_ = elem_; }
|
|
|
|
|
push_element_fun(T* elem) : elem_(elem) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
struct call_element_fun {
|
|
|
|
|
void (T::*fptr_)();
|
|
|
|
|
void fun(T* node) { (node->*fptr_)(); }
|
|
|
|
|
call_element_fun(void (T::*f)()) : fptr_(f) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct TraceBufferBase {
|
|
|
|
|
typedef std::mutex mutex_t;
|
|
|
|
|
|
|
|
|
|
virtual void StartWorkerThread() = 0;
|
|
|
|
|
virtual void Flush() = 0;
|
|
|
|
|
|
|
|
|
|
static void StartWorkerThreadAll() { foreach(call_element_fun<TraceBufferBase>(&TraceBufferBase::StartWorkerThread)); }
|
|
|
|
|
static void FlushAll() { foreach(call_element_fun<TraceBufferBase>(&TraceBufferBase::Flush)); }
|
|
|
|
|
|
|
|
|
|
static void Push(TraceBufferBase* elem) {
|
|
|
|
|
if (head_elem_ == NULL) head_elem_ = elem;
|
|
|
|
|
else foreach(push_element_fun<TraceBufferBase>(elem));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TraceBufferBase() : next_elem_(NULL) {}
|
|
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
|
static void foreach(const F& f_in) {
|
|
|
|
|
std::lock_guard<mutex_t> lck(mutex_);
|
|
|
|
|
F f = f_in;
|
|
|
|
|
TraceBufferBase* p = head_elem_;
|
|
|
|
|
while (p != NULL) {
|
|
|
|
|
TraceBufferBase* next = p->next_elem_;
|
|
|
|
|
f.fun(p);
|
|
|
|
|
p = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TraceBufferBase* next_elem_;
|
|
|
|
|
static TraceBufferBase* head_elem_;
|
|
|
|
|
static mutex_t mutex_;
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-24 21:05:12 -05:00
|
|
|
template <typename Entry>
|
2020-01-27 14:30:44 -06:00
|
|
|
class TraceBuffer : protected TraceBufferBase {
|
2019-06-24 21:05:12 -05:00
|
|
|
public:
|
2019-07-13 00:38:12 -05:00
|
|
|
typedef void (*callback_t)(Entry*);
|
|
|
|
|
typedef TraceBuffer<Entry> Obj;
|
|
|
|
|
typedef uint64_t pointer_t;
|
2019-08-11 08:56:39 -05:00
|
|
|
typedef std::mutex mutex_t;
|
2019-07-13 00:38:12 -05:00
|
|
|
|
|
|
|
|
struct flush_prm_t {
|
|
|
|
|
uint32_t type;
|
|
|
|
|
callback_t fun;
|
|
|
|
|
};
|
2019-06-24 21:05:12 -05:00
|
|
|
|
2019-08-10 00:14:04 -05:00
|
|
|
TraceBuffer(const char* name, uint32_t size, flush_prm_t* flush_prm_arr, uint32_t flush_prm_count) :
|
2020-01-27 14:30:44 -06:00
|
|
|
is_flushed_(false),
|
|
|
|
|
work_thread_started_(false)
|
2019-08-10 00:14:04 -05:00
|
|
|
{
|
|
|
|
|
name_ = strdup(name);
|
2019-06-24 21:05:12 -05:00
|
|
|
size_ = size;
|
2019-07-13 00:38:12 -05:00
|
|
|
data_ = allocate_fun();
|
|
|
|
|
next_ = NULL;
|
|
|
|
|
read_pointer_ = 0;
|
|
|
|
|
end_pointer_ = size;
|
|
|
|
|
buf_list_.push_back(data_);
|
|
|
|
|
|
|
|
|
|
flush_prm_arr_ = flush_prm_arr;
|
|
|
|
|
flush_prm_count_ = flush_prm_count;
|
|
|
|
|
|
2020-01-27 14:30:44 -06:00
|
|
|
TraceBufferBase::Push(this);
|
2019-07-13 00:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TraceBuffer() {
|
2020-01-27 14:30:44 -06:00
|
|
|
StopWorkerThread();
|
2019-08-19 23:18:07 -05:00
|
|
|
Flush();
|
2019-06-24 21:05:12 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-27 14:30:44 -06:00
|
|
|
void StartWorkerThread() {
|
|
|
|
|
std::lock_guard<mutex_t> lck(mutex_);
|
|
|
|
|
if (work_thread_started_ == false) {
|
|
|
|
|
PTHREAD_CALL(pthread_mutex_init(&work_mutex_, NULL));
|
|
|
|
|
PTHREAD_CALL(pthread_cond_init(&work_cond_, NULL));
|
|
|
|
|
PTHREAD_CALL(pthread_create(&work_thread_, NULL, allocate_worker, this));
|
|
|
|
|
work_thread_started_ = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StopWorkerThread() {
|
|
|
|
|
std::lock_guard<mutex_t> lck(mutex_);
|
|
|
|
|
if (work_thread_started_ == true) {
|
|
|
|
|
PTHREAD_CALL(pthread_cancel(work_thread_));
|
|
|
|
|
void *res;
|
|
|
|
|
PTHREAD_CALL(pthread_join(work_thread_, &res));
|
|
|
|
|
if (res != PTHREAD_CANCELED) FATAL("consumer thread wasn't stopped correctly");
|
|
|
|
|
work_thread_started_ = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-13 00:38:12 -05:00
|
|
|
|
2019-06-24 21:05:12 -05:00
|
|
|
Entry* GetEntry() {
|
2019-07-13 00:38:12 -05:00
|
|
|
const pointer_t pointer = read_pointer_.fetch_add(1);
|
|
|
|
|
if (pointer >= end_pointer_) wrap_buffer(pointer);
|
2020-01-27 14:30:44 -06:00
|
|
|
if (pointer >= end_pointer_) FATAL("pointer >= end_pointer_ after buffer wrap");
|
2019-08-11 08:56:39 -05:00
|
|
|
return data_ + (pointer + size_ - end_pointer_);
|
2019-07-13 00:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-27 14:30:44 -06:00
|
|
|
void Flush() { flush_buf(); }
|
2019-07-13 00:38:12 -05:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void flush_buf() {
|
2019-08-19 23:18:07 -05:00
|
|
|
std::lock_guard<mutex_t> lck(mutex_);
|
2019-10-02 15:29:09 -05:00
|
|
|
const bool is_flushed = is_flushed_.exchange(true, std::memory_order_acquire);
|
2019-08-19 23:18:07 -05:00
|
|
|
|
2019-08-10 00:14:04 -05:00
|
|
|
if (is_flushed == false) {
|
|
|
|
|
for (flush_prm_t* prm = flush_prm_arr_; prm < flush_prm_arr_ + flush_prm_count_; prm++) {
|
2020-03-07 19:09:54 -06:00
|
|
|
// Flushed entries type
|
2019-08-10 00:14:04 -05:00
|
|
|
uint32_t type = prm->type;
|
2020-03-07 19:09:54 -06:00
|
|
|
// Flushing function
|
2019-08-10 00:14:04 -05:00
|
|
|
callback_t fun = prm->fun;
|
2020-03-07 19:09:54 -06:00
|
|
|
if (fun == NULL) FATAL("flush function is not set");
|
|
|
|
|
|
2019-08-10 00:14:04 -05:00
|
|
|
pointer_t pointer = 0;
|
|
|
|
|
for (Entry* ptr : buf_list_) {
|
|
|
|
|
Entry* end = ptr + size_;
|
|
|
|
|
while ((ptr < end) && (pointer < read_pointer_)) {
|
|
|
|
|
if (ptr->type == type) {
|
|
|
|
|
if (ptr->valid == TRACE_ENTRY_COMPL) {
|
|
|
|
|
fun(ptr);
|
|
|
|
|
}
|
2019-07-13 00:38:12 -05:00
|
|
|
}
|
2019-08-10 00:14:04 -05:00
|
|
|
ptr++;
|
|
|
|
|
pointer++;
|
2019-07-13 00:38:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-24 21:05:12 -05:00
|
|
|
}
|
2019-07-13 00:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Entry* allocate_fun() {
|
2019-08-19 23:18:07 -05:00
|
|
|
Entry* ptr = (Entry*) malloc(size_ * sizeof(Entry));
|
2020-01-27 14:30:44 -06:00
|
|
|
if (ptr == NULL) FATAL("malloc failed");
|
2019-07-13 00:38:12 -05:00
|
|
|
//memset(ptr, 0, size_ * sizeof(Entry));
|
2019-06-24 21:05:12 -05:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 00:38:12 -05:00
|
|
|
static void* allocate_worker(void* arg) {
|
|
|
|
|
Obj* obj = (Obj*)arg;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
PTHREAD_CALL(pthread_mutex_lock(&(obj->work_mutex_)));
|
|
|
|
|
while (obj->next_ != NULL) {
|
|
|
|
|
PTHREAD_CALL(pthread_cond_wait(&(obj->work_cond_), &(obj->work_mutex_)));
|
2019-06-24 21:05:12 -05:00
|
|
|
}
|
2019-07-13 00:38:12 -05:00
|
|
|
obj->next_ = obj->allocate_fun();
|
|
|
|
|
PTHREAD_CALL(pthread_mutex_unlock(&(obj->work_mutex_)));
|
2019-06-24 21:05:12 -05:00
|
|
|
}
|
2019-07-13 00:38:12 -05:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wrap_buffer(const pointer_t pointer) {
|
2019-08-11 08:56:39 -05:00
|
|
|
std::lock_guard<mutex_t> lck(mutex_);
|
2020-05-07 06:45:29 -05:00
|
|
|
if (work_thread_started_ == false) StartWorkerThread();
|
2020-01-27 14:30:44 -06:00
|
|
|
|
2019-07-13 00:38:12 -05:00
|
|
|
PTHREAD_CALL(pthread_mutex_lock(&work_mutex_));
|
|
|
|
|
if (pointer >= end_pointer_) {
|
|
|
|
|
data_ = next_;
|
|
|
|
|
next_ = NULL;
|
|
|
|
|
PTHREAD_CALL(pthread_cond_signal(&work_cond_));
|
|
|
|
|
end_pointer_ += size_;
|
2020-01-27 14:30:44 -06:00
|
|
|
if (end_pointer_ == 0) FATAL("pointer overflow");
|
2019-07-13 00:38:12 -05:00
|
|
|
buf_list_.push_back(data_);
|
2019-06-24 21:05:12 -05:00
|
|
|
}
|
2019-07-13 00:38:12 -05:00
|
|
|
PTHREAD_CALL(pthread_mutex_unlock(&work_mutex_));
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 00:14:04 -05:00
|
|
|
const char* name_;
|
2019-06-24 21:05:12 -05:00
|
|
|
uint32_t size_;
|
2019-07-13 00:38:12 -05:00
|
|
|
Entry* data_;
|
|
|
|
|
Entry* next_;
|
2019-08-11 08:56:39 -05:00
|
|
|
volatile std::atomic<pointer_t> read_pointer_;
|
|
|
|
|
volatile std::atomic<pointer_t> end_pointer_;
|
2019-07-13 00:38:12 -05:00
|
|
|
std::list<Entry*> buf_list_;
|
|
|
|
|
|
|
|
|
|
flush_prm_t* flush_prm_arr_;
|
|
|
|
|
uint32_t flush_prm_count_;
|
2019-10-02 15:29:09 -05:00
|
|
|
volatile std::atomic<bool> is_flushed_;
|
2019-07-13 00:38:12 -05:00
|
|
|
|
|
|
|
|
pthread_t work_thread_;
|
|
|
|
|
pthread_mutex_t work_mutex_;
|
|
|
|
|
pthread_cond_t work_cond_;
|
2020-01-27 14:30:44 -06:00
|
|
|
bool work_thread_started_;
|
2019-08-11 08:56:39 -05:00
|
|
|
|
|
|
|
|
mutex_t mutex_;
|
2019-06-24 21:05:12 -05:00
|
|
|
};
|
|
|
|
|
} // namespace roctracer
|
|
|
|
|
|
2020-01-27 14:30:44 -06:00
|
|
|
#define TRACE_BUFFER_INSTANTIATE() \
|
|
|
|
|
roctracer::TraceBufferBase* roctracer::TraceBufferBase::head_elem_ = NULL; \
|
|
|
|
|
roctracer::TraceBufferBase::mutex_t roctracer::TraceBufferBase::mutex_;
|
|
|
|
|
|
2019-06-24 21:05:12 -05:00
|
|
|
#endif // SRC_CORE_TRACE_BUFFER_H_
|