From 710be682ca04f309b055a8dd848917be9aee57b8 Mon Sep 17 00:00:00 2001 From: Ben Sander Date: Tue, 25 Oct 2016 16:20:37 -0500 Subject: [PATCH] Add HIP_PROFILE_START_API, HIP_PROFILE_STOP_API Refactor HIP_INIT_API to call recordApiTrace. Change-Id: Ieff4b5018236f59e49e1b9841474440a34f821df --- src/hip_hcc.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ src/hip_hcc.h | 35 +++++++++++++--- 2 files changed, 138 insertions(+), 5 deletions(-) diff --git a/src/hip_hcc.cpp b/src/hip_hcc.cpp index 29d83e43fb..6ea07adb87 100644 --- a/src/hip_hcc.cpp +++ b/src/hip_hcc.cpp @@ -49,6 +49,7 @@ THE SOFTWARE. + //================================================================================================= //Global variables: //================================================================================================= @@ -63,11 +64,16 @@ int HIP_PRINT_ENV = 0; int HIP_TRACE_API= 0; std::string HIP_TRACE_API_COLOR("green"); int HIP_ATP_MARKER= 0; +std::string HIP_PROFILE_START_API; +std::string HIP_PROFILE_STOP_API; int HIP_DB= 0; int HIP_VISIBLE_DEVICES = 0; /* Contains a comma-separated sequence of GPU identifiers */ int HIP_NUM_KERNELS_INFLIGHT = 128; int HIP_WAIT_MODE = 0; + + + #define HIP_USE_PRODUCT_NAME 0 //#define DISABLE_COPY_EXT 1 @@ -86,6 +92,11 @@ unsigned g_numLogicalThreads; std::atomic g_lastShortTid(1); +// Indexed by short-tid: +// +std::vector g_profStartTriggers; +std::vector g_profStopTriggers; + /* @@ -185,6 +196,30 @@ thread_local ShortTid tls_shortTid; //================================================================================================= // Top-level "free" functions: //================================================================================================= +void recordApiTrace(const std::string &s) +{ + auto apiSeqNum = tls_shortTid.incApiSeqNum(); + auto tid = tls_shortTid.tid(); + + if ((tid < g_profStartTriggers.size()) && (apiSeqNum >= g_profStartTriggers[tid].nextTrigger())) { + printf ("info: resume profiling at %lu\n", apiSeqNum); + RESUME_PROFILING; + g_profStartTriggers.pop_back(); + }; + if ((tid < g_profStopTriggers.size()) && (apiSeqNum >= g_profStopTriggers[tid].nextTrigger())) { + printf ("info: stop profiling at %lu\n", apiSeqNum); + STOP_PROFILING; + g_profStopTriggers.pop_back(); + }; + + + if (COMPILE_HIP_DB && HIP_TRACE_API) { + fprintf (stderr, "%s< 0 @@ -1112,6 +1147,71 @@ void ihipReadEnv_S(std::string *var_ptr, const char *var_name1, const char *var_ #endif +static void tokenize(const std::string &s, char delim, std::vector *tokens) +{ + std::stringstream ss; + ss.str(s); + std::string item; + while (getline(ss, item, delim)) { + item.erase (std::remove (item.begin(), item.end(), ' '), item.end()); // remove whitespace. + tokens->push_back(item); + } +} + +static void trim(std::string *s) +{ + // trim whitespace from beginning and end: + const char *t = "\t\n\r\f\v"; + s->erase(0, s->find_first_not_of(t)); + s->erase(s->find_last_not_of(t)+1); +} + +static void ltrim(std::string *s) +{ + // trim whitespace from beginning + const char *t = "\t\n\r\f\v"; + s->erase(0, s->find_first_not_of(t)); +} + + +// TODO - change last arg to pointer. +void parseTrigger(std::string triggerString, std::vector &profTriggers ) +{ + std::vector tidApiTokens; + tokenize(std::string(triggerString), ',', &tidApiTokens); + for (auto t=tidApiTokens.begin(); t != tidApiTokens.end(); t++) { + std::vector oneToken; + //std::cout << "token=" << *t << "\n"; + tokenize(std::string(*t), '.', &oneToken); + int tid = 1; + uint64_t apiTrigger = 0; + if (oneToken.size() == 1) { + // the case with just apiNum + apiTrigger = std::strtoull(oneToken[0].c_str(), nullptr, 0); + } else if (oneToken.size() == 2) { + // the case with tid.apiNum + tid = std::strtoul(oneToken[0].c_str(), nullptr, 0); + apiTrigger = std::strtoull(oneToken[1].c_str(), nullptr, 0); + } else { + throw ihipException(hipErrorRuntimeOther); // TODO -> bad env var? + } + + if (tid > 10000) { + throw ihipException(hipErrorRuntimeOther); // TODO -> bad env var? + } else { + profTriggers.resize(tid+1); + //std::cout << "tid:" << tid << " add: " << apiTrigger << "\n"; + profTriggers[tid].add(apiTrigger); + } + } + + + for (int tid=1; tidlockclose_postKernelCommand(lp.av); } diff --git a/src/hip_hcc.h b/src/hip_hcc.h index 668011a8c8..9997552135 100644 --- a/src/hip_hcc.h +++ b/src/hip_hcc.h @@ -77,11 +77,34 @@ private: uint64_t _apiSeqNum; }; +struct ProfTrigger { + + static const uint64_t MAX_TRIGGER = std::numeric_limits::max(); + + void print (int tid) { + std::cout << "Enabling tracing for "; + for (auto iter=_profTrigger.begin(); iter != _profTrigger.end(); iter++) { + std::cout << "tid:" << tid << "." << *iter << ","; + } + std::cout << "\n"; + }; + + uint64_t nextTrigger() { return _profTrigger.empty() ? MAX_TRIGGER : _profTrigger.back(); }; + void add(uint64_t trigger) { _profTrigger.push_back(trigger); }; + void sort() { std::sort (_profTrigger.begin(), _profTrigger.end(), std::greater()); }; +private: + std::vector _profTrigger; +}; + + + //--- //Extern tls extern thread_local hipError_t tls_lastHipError; extern thread_local ShortTid tls_shortTid; +extern std::vector g_profStartTriggers; +extern std::vector g_profStopTriggers; //--- //Forward defs: @@ -138,21 +161,23 @@ extern const char *API_COLOR_END; #if COMPILE_HIP_ATP_MARKER #include "CXLActivityLogger.h" #define SCOPED_MARKER(markerName,group,userString) amdtScopedMarker(markerName, group, userString) +#define RESUME_PROFILING amdtResumeProfiling(AMDT_ALL_PROFILING); +#define STOP_PROFILING amdtStopProfiling(AMDT_ALL_PROFILING); #else // Swallow scoped markers: #define SCOPED_MARKER(markerName,group,userString) +#define RESUME_PROFILING +#define STOP_PROFILING #endif +extern void recordApiTrace(const std::string &s); + #if COMPILE_HIP_ATP_MARKER || (COMPILE_HIP_TRACE_API & 0x1) #define API_TRACE(...)\ {\ if (HIP_ATP_MARKER || (COMPILE_HIP_DB && HIP_TRACE_API)) {\ - std::string s = std::string(__func__) + " (" + ToString(__VA_ARGS__) + ')';\ - if (COMPILE_HIP_DB && HIP_TRACE_API) {\ - fprintf (stderr, "%s<