Skip to content

Commit

Permalink
Improvement #7688 - Profiler should not miss query's top-level access…
Browse files Browse the repository at this point in the history
… paths nodes.
  • Loading branch information
asfernandes committed Jul 30, 2023
1 parent 3586017 commit 670d351
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/dsql/BoolNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,7 @@ void RseBoolNode::pass2Boolean(thread_db* tdbb, CompilerScratch* csb, std::funct
rsb->setAnyBoolean(rse->rse_boolean, ansiAny, ansiNot);
}

subQuery = FB_NEW_POOL(*tdbb->getDefaultPool()) SubQuery(rsb, rse);
subQuery = FB_NEW_POOL(*tdbb->getDefaultPool()) SubQuery(csb, rsb, rse);
csb->csb_fors.add(subQuery);
}

Expand Down
2 changes: 1 addition & 1 deletion src/dsql/ExprNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11385,7 +11385,7 @@ ValueExprNode* SubQueryNode::pass2(thread_db* tdbb, CompilerScratch* csb)
// Finish up processing of record selection expressions.

RecordSource* const rsb = CMP_post_rse(tdbb, csb, rse);
subQuery = FB_NEW_POOL(*tdbb->getDefaultPool()) SubQuery(rsb, rse);
subQuery = FB_NEW_POOL(*tdbb->getDefaultPool()) SubQuery(csb, rsb, rse);
csb->csb_fors.add(subQuery);

return this;
Expand Down
2 changes: 1 addition & 1 deletion src/dsql/StmtNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ DeclareCursorNode* DeclareCursorNode::pass2(thread_db* tdbb, CompilerScratch* cs
csb->csb_fors.add(cursor);

StreamList cursorStreams;
cursor->getAccessPath()->findUsedStreams(cursorStreams);
cursor->getRootRecordSource()->findUsedStreams(cursorStreams);

// Activate cursor streams to allow index usage for <cursor>.<field> references, see CORE-4675.
// It's also useful for correlated sub-queries in the select list, see CORE-4379.
Expand Down
9 changes: 9 additions & 0 deletions src/jrd/Attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,15 @@ ProfilerManager* Attachment::getProfilerManager(thread_db* tdbb)
return profilerManager;
}

ProfilerManager* Attachment::getActiveProfilerManagerForNonInternalStatement(thread_db* tdbb)
{
const auto request = tdbb->getRequest();

return isProfilerActive() && !request->hasInternalStatement() ?
getProfilerManager(tdbb) :
nullptr;
}

bool Attachment::isProfilerActive()
{
return att_profiler_manager && att_profiler_manager->isActive();
Expand Down
1 change: 1 addition & 0 deletions src/jrd/Attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ class Attachment : public pool_alloc<type_att>
void invalidateReplSet(thread_db* tdbb, bool broadcast);

ProfilerManager* getProfilerManager(thread_db* tdbb);
ProfilerManager* getActiveProfilerManagerForNonInternalStatement(thread_db* tdbb);
bool isProfilerActive();
void releaseProfilerManager();

Expand Down
18 changes: 8 additions & 10 deletions src/jrd/ProfilerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,22 +483,20 @@ void ProfilerManager::prepareCursor(thread_db* tdbb, Request* request, const Sel
profileStatement->definedCursors.add(cursorId);
}

const auto recordSource = select->getAccessPath();

prepareRecSource(tdbb, request, recordSource);
prepareRecSource(tdbb, request, select);
}

void ProfilerManager::prepareRecSource(thread_db* tdbb, Request* request, const RecordSource* rsb)
void ProfilerManager::prepareRecSource(thread_db* tdbb, Request* request, const AccessPath* recordSource)
{
auto profileStatement = getStatement(request);

if (!profileStatement)
return;

if (profileStatement->recSourceSequence.exist(rsb->getRecSourceId()))
if (profileStatement->recSourceSequence.exist(recordSource->getRecSourceId()))
return;

fb_assert(profileStatement->definedCursors.exist(rsb->getCursorId()));
fb_assert(profileStatement->definedCursors.exist(recordSource->getCursorId()));

struct PlanItem : PermanentStorage
{
Expand All @@ -507,14 +505,14 @@ void ProfilerManager::prepareRecSource(thread_db* tdbb, Request* request, const
{
}

const RecordSource* recordSource = nullptr;
const RecordSource* parentRecordSource = nullptr;
const AccessPath* recordSource = nullptr;
const AccessPath* parentRecordSource = nullptr;
string accessPath{getPool()};
unsigned level = 0;
};

ObjectsArray<PlanItem> planItems;
planItems.add().recordSource = rsb;
planItems.add().recordSource = recordSource;

for (unsigned pos = 0; pos < planItems.getCount(); ++pos)
{
Expand Down Expand Up @@ -559,7 +557,7 @@ void ProfilerManager::prepareRecSource(thread_db* tdbb, Request* request, const
}

NonPooledMap<ULONG, ULONG> idSequenceMap;
auto sequencePtr = profileStatement->cursorNextSequence.getOrPut(rsb->getCursorId());
auto sequencePtr = profileStatement->cursorNextSequence.getOrPut(recordSource->getCursorId());

for (const auto& planItem : planItems)
{
Expand Down
48 changes: 25 additions & 23 deletions src/jrd/ProfilerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ class ProfilerManager final : public Firebird::PerformanceStopWatch
};

public:
RecordSourceStopWatcher(thread_db* tdbb, const RecordSource* aRecordSource, Event aEvent)
: recordSource(aRecordSource),
event(aEvent)
RecordSourceStopWatcher(thread_db* tdbb, const RecordSource* recordSource, Event aEvent)
: RecordSourceStopWatcher(tdbb, tdbb->getAttachment()->getActiveProfilerManagerForNonInternalStatement(tdbb),
recordSource, aEvent)
{
const auto attachment = tdbb->getAttachment();
request = tdbb->getRequest();

profilerManager = attachment->isProfilerActive() && !request->hasInternalStatement() ?
attachment->getProfilerManager(tdbb) :
nullptr;
}

RecordSourceStopWatcher(thread_db* tdbb, ProfilerManager* aProfilerManager,
const AccessPath* recordSource, Event aEvent)
: request(tdbb->getRequest()),
profilerManager(aProfilerManager),
recordSource(recordSource),
event(aEvent)
{
if (profilerManager)
{
lastTicks = profilerManager->queryTicks();
Expand Down Expand Up @@ -123,9 +125,9 @@ class ProfilerManager final : public Firebird::PerformanceStopWatch
}

private:
const RecordSource* recordSource;
Request* request;
ProfilerManager* profilerManager;
const AccessPath* recordSource;
SINT64 lastTicks;
SINT64 lastAccumulatedOverhead;
Event event;
Expand Down Expand Up @@ -210,58 +212,58 @@ class ProfilerManager final : public Firebird::PerformanceStopWatch
}
}

void beforeRecordSourceOpen(Request* request, const RecordSource* rsb)
void beforeRecordSourceOpen(Request* request, const AccessPath* recordSource)
{
if (const auto profileRequestId = getRequest(request, Firebird::IProfilerSession::FLAG_BEFORE_EVENTS))
{
const auto profileStatement = getStatement(request);

if (const auto sequencePtr = profileStatement->recSourceSequence.get(rsb->getRecSourceId()))
if (const auto sequencePtr = profileStatement->recSourceSequence.get(recordSource->getRecSourceId()))
{
currentSession->pluginSession->beforeRecordSourceOpen(
profileStatement->id, profileRequestId, rsb->getCursorId(), *sequencePtr);
profileStatement->id, profileRequestId, recordSource->getCursorId(), *sequencePtr);
}
}
}

void afterRecordSourceOpen(Request* request, const RecordSource* rsb, Stats& stats)
void afterRecordSourceOpen(Request* request, const AccessPath* recordSource, Stats& stats)
{
if (const auto profileRequestId = getRequest(request, Firebird::IProfilerSession::FLAG_AFTER_EVENTS))
{
const auto profileStatement = getStatement(request);

if (const auto sequencePtr = profileStatement->recSourceSequence.get(rsb->getRecSourceId()))
if (const auto sequencePtr = profileStatement->recSourceSequence.get(recordSource->getRecSourceId()))
{
currentSession->pluginSession->afterRecordSourceOpen(
profileStatement->id, profileRequestId, rsb->getCursorId(), *sequencePtr, &stats);
profileStatement->id, profileRequestId, recordSource->getCursorId(), *sequencePtr, &stats);
}
}
}

void beforeRecordSourceGetRecord(Request* request, const RecordSource* rsb)
void beforeRecordSourceGetRecord(Request* request, const AccessPath* recordSource)
{
if (const auto profileRequestId = getRequest(request, Firebird::IProfilerSession::FLAG_BEFORE_EVENTS))
{
const auto profileStatement = getStatement(request);

if (const auto sequencePtr = profileStatement->recSourceSequence.get(rsb->getRecSourceId()))
if (const auto sequencePtr = profileStatement->recSourceSequence.get(recordSource->getRecSourceId()))
{
currentSession->pluginSession->beforeRecordSourceGetRecord(
profileStatement->id, profileRequestId, rsb->getCursorId(), *sequencePtr);
profileStatement->id, profileRequestId, recordSource->getCursorId(), *sequencePtr);
}
}
}

void afterRecordSourceGetRecord(Request* request, const RecordSource* rsb, Stats& stats)
void afterRecordSourceGetRecord(Request* request, const AccessPath* recordSource, Stats& stats)
{
if (const auto profileRequestId = getRequest(request, Firebird::IProfilerSession::FLAG_AFTER_EVENTS))
{
const auto profileStatement = getStatement(request);

if (const auto sequencePtr = profileStatement->recSourceSequence.get(rsb->getRecSourceId()))
if (const auto sequencePtr = profileStatement->recSourceSequence.get(recordSource->getRecSourceId()))
{
currentSession->pluginSession->afterRecordSourceGetRecord(
profileStatement->id, profileRequestId, rsb->getCursorId(), *sequencePtr, &stats);
profileStatement->id, profileRequestId, recordSource->getCursorId(), *sequencePtr, &stats);
}
}
}
Expand All @@ -283,7 +285,7 @@ class ProfilerManager final : public Firebird::PerformanceStopWatch
}

private:
void prepareRecSource(thread_db* tdbb, Request* request, const RecordSource* rsb);
void prepareRecSource(thread_db* tdbb, Request* request, const AccessPath* recordSource);

void cancelSession();
void finishSession(thread_db* tdbb, bool flushData);
Expand Down
Loading

0 comments on commit 670d351

Please sign in to comment.