8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 19:23:03 +01:00
firebird-mirror/src/jrd/trace/TraceDSQLHelpers.h

231 lines
5.5 KiB
C
Raw Normal View History

2009-02-01 23:07:35 +01:00
/*
* PROGRAM: Firebird Trace Services
* MODULE: TraceDSQLHelpers.h
* DESCRIPTION: Trace API manager support
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
2009-04-04 18:39:31 +02:00
* The Original Code was created by Khorsun Vladyslav
2009-02-01 23:07:35 +01:00
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2009 Khorsun Vladyslav <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#ifndef JRD_TRACE_DSQL_HELPERS_H
#define JRD_TRACE_DSQL_HELPERS_H
#include "../../jrd/trace/TraceObjects.h"
namespace Jrd {
class TraceDSQLPrepare
{
public:
TraceDSQLPrepare(Attachment* attachemnt, USHORT string_length, const TEXT* string) :
m_attachment(attachemnt),
2009-02-01 23:07:35 +01:00
m_request(NULL),
m_string_len(string_length),
m_string(string)
{
m_need_trace = TraceManager::need_dsql_prepare(m_attachment);
2009-02-01 23:07:35 +01:00
if (!m_need_trace)
return;
m_start_clock = fb_utils::query_performance_counter();
static const char empty_string[] = "";
2009-02-06 14:47:57 +01:00
if (!m_string_len || !string)
2009-02-01 23:07:35 +01:00
{
m_string = empty_string;
2009-02-10 10:01:19 +01:00
m_string_len = 0;
2009-02-01 23:07:35 +01:00
}
}
~TraceDSQLPrepare()
{
prepare(res_failed);
}
2009-02-02 15:46:24 +01:00
void setStatement(dsql_req* request)
{
2009-02-01 23:07:35 +01:00
m_request = request;
}
void prepare(ntrace_result_t result)
{
if (m_request) {
m_need_trace = (m_need_trace && m_request->req_traced);
}
if (!m_need_trace)
return;
m_need_trace = false;
2009-04-04 18:39:31 +02:00
const SINT64 millis = (fb_utils::query_performance_counter() - m_start_clock) * 1000 /
2009-02-01 23:07:35 +01:00
fb_utils::query_performance_frequency();
if ((result == res_successful) && m_request)
{
TraceSQLStatementImpl stmt(m_request, NULL);
2009-04-04 18:39:31 +02:00
TraceManager::event_dsql_prepare(m_attachment, m_request->req_transaction,
2009-02-01 23:07:35 +01:00
&stmt, millis, result);
}
else
{
Firebird::string str(*getDefaultMemoryPool(), m_string, m_string_len);
TraceFailedSQLStatement stmt(str);
2009-04-04 18:39:31 +02:00
TraceManager::event_dsql_prepare(m_attachment, m_request->req_transaction,
2009-02-01 23:07:35 +01:00
&stmt, millis, result);
}
}
private:
bool m_need_trace;
Attachment* const m_attachment;
2009-02-01 23:07:35 +01:00
dsql_req* m_request;
2009-02-02 15:46:24 +01:00
SINT64 m_start_clock;
2009-02-01 23:07:35 +01:00
size_t m_string_len;
const TEXT* m_string;
};
class TraceDSQLExecute
{
public:
TraceDSQLExecute(Attachment* attachment, dsql_req* request) :
m_attachment(attachment),
m_request(request)
{
m_need_trace = m_request->req_traced && TraceManager::need_dsql_execute(m_attachment);
if (!m_need_trace)
return;
2009-02-02 15:46:24 +01:00
{ // scope
2009-02-01 23:07:35 +01:00
TraceSQLStatementImpl stmt(request, NULL);
TraceManager::event_dsql_execute(m_attachment, request->req_transaction, &stmt, true, res_successful);
}
m_start_clock = fb_utils::query_performance_counter();
m_request->req_fetch_elapsed = 0;
m_request->req_fetch_rowcount = 0;
fb_assert(!m_request->req_fetch_baseline);
m_request->req_fetch_baseline = NULL;
jrd_req* jrd_request = m_request->req_request;
2009-04-04 18:39:31 +02:00
if (jrd_request)
2009-02-01 23:07:35 +01:00
{
MemoryPool* pool = MemoryPool::getContextPool();
m_request->req_fetch_baseline = FB_NEW(*pool) RuntimeStatistics(*pool, jrd_request->req_stats);
}
}
void finish(bool have_cursor, ntrace_result_t result)
{
if (!m_need_trace)
return;
m_need_trace = false;
if (have_cursor)
{
m_request->req_fetch_elapsed = fb_utils::query_performance_counter() - m_start_clock;
return;
}
2009-04-04 18:39:31 +02:00
TraceRuntimeStats stats(m_attachment->att_database, m_request->req_fetch_baseline,
2009-02-01 23:07:35 +01:00
&m_request->req_request->req_stats,
fb_utils::query_performance_counter() - m_start_clock,
m_request->req_fetch_rowcount);
TraceSQLStatementImpl stmt(m_request, stats.getPerf());
TraceManager::event_dsql_execute(m_attachment, m_request->req_transaction, &stmt, false, result);
m_request->req_fetch_baseline = NULL;
}
~TraceDSQLExecute()
{
finish(false, res_failed);
}
private:
bool m_need_trace;
Attachment* const m_attachment;
dsql_req* const m_request;
2009-02-01 23:07:35 +01:00
SINT64 m_start_clock;
};
class TraceDSQLFetch
{
public:
TraceDSQLFetch(Attachment* attachment, dsql_req* request) :
m_attachment(attachment),
m_request(request)
{
m_need_trace = m_request->req_traced && TraceManager::need_dsql_execute(m_attachment);
if (!m_need_trace)
2009-02-11 16:20:14 +01:00
{
m_request->req_fetch_baseline = NULL;
2009-02-01 23:07:35 +01:00
return;
2009-02-11 16:20:14 +01:00
}
2009-02-01 23:07:35 +01:00
m_start_clock = fb_utils::query_performance_counter();
}
~TraceDSQLFetch()
{
fetch(true, res_failed);
}
void fetch(bool eof, ntrace_result_t result)
{
if (!m_need_trace)
return;
m_need_trace = false;
m_request->req_fetch_elapsed += fb_utils::query_performance_counter() - m_start_clock;
if (!eof)
{
m_request->req_fetch_rowcount++;
return;
}
2009-04-04 18:39:31 +02:00
TraceRuntimeStats stats(m_attachment->att_database, m_request->req_fetch_baseline,
&m_request->req_request->req_stats, m_request->req_fetch_elapsed,
2009-02-01 23:07:35 +01:00
m_request->req_fetch_rowcount);
TraceSQLStatementImpl stmt(m_request, stats.getPerf());
2009-04-04 18:39:31 +02:00
TraceManager::event_dsql_execute(m_attachment, m_request->req_transaction,
2009-02-01 23:07:35 +01:00
&stmt, false, result);
m_request->req_fetch_elapsed = 0;
m_request->req_fetch_baseline = NULL;
}
private:
bool m_need_trace;
Attachment* const m_attachment;
dsql_req* const m_request;
2009-02-02 15:46:24 +01:00
SINT64 m_start_clock;
2009-02-01 23:07:35 +01:00
};
2009-04-04 18:39:31 +02:00
} // namespace Jrd
2009-02-01 23:07:35 +01:00
#endif // JRD_TRACE_DSQL_HELPERS_H