void BucketManager::RegisterThread(const std::string& name)
{
- // lazy evaluate threadviz knob
- if (!mThreadViz && KNOB_BUCKETS_ENABLE_THREADVIZ)
- {
- uint32_t pid = GetCurrentProcessId();
- std::stringstream str;
- str << "threadviz." << pid;
- mThreadVizDir = str.str();
- CreateDirectory(mThreadVizDir.c_str(), NULL);
-
- mThreadViz = KNOB_BUCKETS_ENABLE_THREADVIZ;
- }
BUCKET_THREAD newThread;
newThread.name = name;
newThread.id = (UINT)id;
tlsThreadId = (UINT)id;
- // open threadviz file if enabled
- if (mThreadViz)
- {
- std::stringstream ss;
- ss << mThreadVizDir << PATH_SEPARATOR;
- ss << "threadviz_thread." << newThread.id << ".dat";
- newThread.vizFile = fopen(ss.str().c_str(), "wb");
- }
-
// store new thread
mThreads.push_back(newThread);
}
}
-void BucketManager::DumpThreadViz()
-{
- // ensure all thread data is flushed
- mThreadMutex.lock();
- for (auto& thread : mThreads)
- {
- fflush(thread.vizFile);
- fclose(thread.vizFile);
- thread.vizFile = nullptr;
- }
- mThreadMutex.unlock();
-
- // dump bucket descriptions
- std::stringstream ss;
- ss << mThreadVizDir << PATH_SEPARATOR << "threadviz_buckets.dat";
-
- FILE* f = fopen(ss.str().c_str(), "wb");
- for (auto& bucket : mBuckets)
- {
- Serialize(f, bucket);
- }
- fclose(f);
-}
-
void BucketManager::PrintReport(const std::string& filename)
{
- if (mThreadViz)
- {
- DumpThreadViz();
- }
- else
{
FILE* f = fopen(filename.c_str(), "w");
void ClearThreads()
{
mThreadMutex.lock();
- // close out the threadviz files if threadviz is enabled
- if (KNOB_BUCKETS_ENABLE_THREADVIZ)
- {
- for (auto& thread : mThreads)
- {
- if (thread.vizFile != nullptr)
- {
- fclose(thread.vizFile);
- }
- }
- }
mThreads.clear();
mThreadMutex.unlock();
}
/// @return unique id
UINT RegisterBucket(const BUCKET_DESC& desc);
- // dump threadviz data
- void DumpThreadViz();
-
// print report
void PrintReport(const std::string& filename);
uint64_t tsc = __rdtsc();
- // if threadviz is enabled, only need to dump start info to threads viz file
- if (mThreadViz)
- {
- SWR_ASSERT(bt.vizFile != nullptr);
- if (mBuckets[id].enableThreadViz)
- {
- VIZ_START_DATA data{ VIZ_START, id, tsc };
- Serialize(bt.vizFile, data);
- }
- }
- else
{
if (bt.pCurrent->children.size() < mBuckets.size())
{
uint64_t tsc = __rdtsc();
- if (mThreadViz)
- {
- SWR_ASSERT(bt.vizFile != nullptr);
- if (mBuckets[id].enableThreadViz)
- {
- VIZ_STOP_DATA data{ VIZ_STOP, tsc };
- Serialize(bt.vizFile, data);
- }
- }
- else
{
if (bt.pCurrent->start == 0) return;
SWR_ASSERT(bt.pCurrent->id == id, "Mismatched buckets detected");
BUCKET_THREAD& bt = mThreads[tlsThreadId];
// don't record events for threadviz
- if (!mThreadViz)
{
if (bt.pCurrent->children.size() < mBuckets.size())
{
std::mutex mThreadMutex;
- // enable threadviz
- bool mThreadViz{ false };
std::string mThreadVizDir;
};