-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HPCC-30156 Instrument CriticalSection #17706
Open
richardkchapman
wants to merge
2
commits into
hpcc-systems:master
Choose a base branch
from
richardkchapman:contendedLock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,8 +118,135 @@ int Mutex::unlockAll() | |
return ret; | ||
} | ||
|
||
static CriticalBlockInstrumentation *critBlocks[10000]; | ||
static RelaxedAtomic<unsigned> nextCritBlock; | ||
|
||
int compareCI (const void * a, const void * b) | ||
{ | ||
return - (*(CriticalBlockInstrumentation **)a)->compare(*(CriticalBlockInstrumentation **)b); | ||
} | ||
|
||
MODULE_EXIT() | ||
{ | ||
unsigned numCritBlocks = nextCritBlock; | ||
qsort(critBlocks, numCritBlocks, sizeof(CriticalBlockInstrumentation*), compareCI); | ||
for (unsigned i = 0; i < numCritBlocks; i++) | ||
{ | ||
critBlocks[i]->describe(i < 10); | ||
} | ||
} | ||
|
||
thread_local CriticalBlockInstrumentation *__cbinst = nullptr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where does this get created ? |
||
|
||
std::size_t CriticalBlockInstrumentation::StackHash::operator()(const Stack& k) const | ||
{ | ||
return hashc((const byte *) k.stack, sizeof(k.stack), 0); | ||
} | ||
|
||
std::string humanTime(unsigned long cycles) | ||
{ | ||
if (!cycles) | ||
return "0 ns"; | ||
unsigned long v = cycle_to_nanosec(cycles); | ||
if (v < 1000) | ||
return std::to_string(v) + " ns"; | ||
else if (v < 1000000) | ||
return std::to_string(v/1000) + " us"; | ||
else | ||
return std::to_string(v/1000000) + " ms"; | ||
} | ||
|
||
CriticalBlockInstrumentation::CriticalBlockInstrumentation(const char *_file, const char *_func, unsigned _line) | ||
: file(strdup(_file)), func(strdup(_func)), line(_line), waitCycles(0), holdCycles(0), uncontended(0), contended(0) | ||
{ | ||
unsigned idx = nextCritBlock++; | ||
if (idx < 10000) | ||
critBlocks[idx] = this; | ||
} | ||
|
||
void CriticalBlockInstrumentation::addStat(cycle_t wait, cycle_t hold, bool wasContended) | ||
{ | ||
#ifdef HAS_BACKTRACE | ||
void *lbtBuff[numStackEntries+3]; | ||
unsigned nFrames = backtrace(lbtBuff, numStackEntries+3); | ||
NonReentrantSpinBlock b(lock); | ||
if (nFrames == numStackEntries+3) | ||
{ | ||
Stack *a = (Stack *) (lbtBuff+3); | ||
stacks[*a]++; | ||
} | ||
#else | ||
NonReentrantSpinBlock b(lock); | ||
#endif | ||
if (wasContended) | ||
contended++; | ||
else | ||
uncontended++; | ||
waitCycles += wait; | ||
holdCycles += hold; | ||
} | ||
|
||
CriticalBlockInstrumentation::~CriticalBlockInstrumentation() | ||
{ | ||
} | ||
|
||
void CriticalBlockInstrumentation::describe(bool includeStack) const | ||
{ | ||
DBGLOG("CritBlock %s (%s:%u): wait %s hold %s, entered %u times, contended %u times", func, strrchr(file, PATHSEPCHAR)+1, line, humanTime(waitCycles).c_str(), humanTime(holdCycles).c_str(), contended+uncontended, contended); | ||
if (includeStack) | ||
{ | ||
std::vector<std::pair<Stack, int>> sorted_stacks(stacks.begin(), stacks.end()); | ||
std::sort(sorted_stacks.begin(), sorted_stacks.end(), [](auto &left, auto &right) { return left.second > right.second; }); | ||
unsigned printed = 0; | ||
for (auto &stack: sorted_stacks) | ||
{ | ||
if (printed == 3) | ||
break; | ||
DBGLOG("Stack appeared %u times:", stack.second); | ||
char** strs = backtrace_symbols(stack.first.stack, 4); | ||
for (unsigned i = 0; i < 4; ++i) | ||
DBGLOG("%s", strs[i]); | ||
free(strs); | ||
printed++; | ||
} | ||
} | ||
} | ||
|
||
int CriticalBlockInstrumentation::compare(const CriticalBlockInstrumentation *other) const | ||
{ | ||
if (waitCycles < other->waitCycles) | ||
return -1; | ||
if (waitCycles > other->waitCycles) | ||
return 1; | ||
if (holdCycles < other->holdCycles) | ||
return -1; | ||
if (holdCycles > other->holdCycles) | ||
return 1; | ||
if (contended < other->contended) | ||
return -1; | ||
if (contended > other->contended) | ||
return 1; | ||
if (uncontended < other->uncontended) | ||
return -1; | ||
if (uncontended > other->uncontended) | ||
return 1; | ||
return 0; | ||
} | ||
|
||
InstrumentedCriticalBlock::InstrumentedCriticalBlock(InstrumentedCriticalSection &c) : crit(c) | ||
{ | ||
inst = __cbinst; | ||
start = get_cycles_now(); | ||
c.enter(); | ||
in = get_cycles_now(); | ||
} | ||
|
||
InstrumentedCriticalBlock::~InstrumentedCriticalBlock() | ||
{ | ||
bool wasContended = crit.leave(); | ||
cycle_t out = get_cycles_now(); | ||
inst->addStat(in-start, out-in, wasContended); | ||
} | ||
|
||
inline bool read_data(int fd, void *buf, size_t nbytes) | ||
{ | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a separate flag to control the new functionality. (I set this on in my release builds by default.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I intended the option to be controlled via editing the definition of USE_INSTRUMENTED_CRITSECS in jmutex.hpp - it's something that will be set temporarily while investigating issues rather than left on.