8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 20:03:02 +01:00
firebird-mirror/src/jrd/RuntimeStatistics.cpp

299 lines
8.3 KiB
C++
Raw Normal View History

/*
* 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.
*
* The Original Code was created by Dmitry Yemanov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2006 Dmitry Yemanov <dimitr@users.sf.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "firebird.h"
2009-02-01 23:10:12 +01:00
#include "../jrd/gdsassert.h"
#include "../jrd/req.h"
#include "../jrd/RuntimeStatistics.h"
using namespace Firebird;
namespace Jrd {
2009-02-01 23:10:12 +01:00
GlobalPtr<RuntimeStatistics> RuntimeStatistics::dummy;
#ifdef REL_COUNTS_TREE
2009-02-01 23:10:12 +01:00
void RuntimeStatistics::bumpValue(const StatType index, SLONG relation_id)
2009-02-01 23:10:12 +01:00
{
fb_assert(index >= 0);
++relChgNumber;
2009-04-04 18:39:31 +02:00
RelCounters::Accessor accessor(&rel_counts);
2009-02-02 04:35:52 +01:00
if (accessor.locate(relation_id))
2009-02-01 23:10:12 +01:00
accessor.current().rlc_counter[index]++;
2009-02-02 04:35:52 +01:00
else
{
2009-02-01 23:10:12 +01:00
RelationCounts counts;
memset(&counts, 0, sizeof(counts));
counts.rlc_relation_id = relation_id;
counts.rlc_counter[index]++;
rel_counts.add(counts);
}
}
void RuntimeStatistics::addRelCounts(const RelCounters& other, bool add)
2009-02-01 23:10:12 +01:00
{
RelCounters::Accessor first(&rel_counts);
RelCounters::ConstAccessor second(&other);
2009-02-01 23:10:12 +01:00
if (second.getFirst())
2009-02-02 04:35:52 +01:00
{
2009-04-04 18:39:31 +02:00
do
2009-02-01 23:10:12 +01:00
{
2009-02-02 04:35:52 +01:00
const RelationCounts& src = second.current();
2009-02-01 23:10:12 +01:00
if (!first.locate(src.rlc_relation_id))
{
RelationCounts counts;
memset(&counts, 0, sizeof(counts));
counts.rlc_relation_id = src.rlc_relation_id;
first.add(counts);
first.locate(src.rlc_relation_id);
}
2009-02-02 04:35:52 +01:00
RelationCounts& dst = first.current();
2009-02-01 23:10:12 +01:00
fb_assert(src.rlc_relation_id == dst.rlc_relation_id);
if (add)
2009-02-01 23:10:12 +01:00
{
for (int index = 0; index < FB_NELEM(src.rlc_counter); index++)
2009-02-01 23:10:12 +01:00
dst.rlc_counter[index] += src.rlc_counter[index];
}
else
{
for (int index = 0; index < FB_NELEM(src.rlc_counter); index++)
2009-02-01 23:10:12 +01:00
dst.rlc_counter[index] -= src.rlc_counter[index];
}
} while(second.getNext());
2009-02-02 04:35:52 +01:00
}
2009-02-01 23:10:12 +01:00
}
2009-02-01 23:10:12 +01:00
2009-02-02 04:35:52 +01:00
PerformanceInfo* RuntimeStatistics::computeDifference(
Database* dbb, const RuntimeStatistics& new_stat, PerformanceInfo& dest, TraceCountsArray& temp)
2009-02-01 23:10:12 +01:00
{
// NOTE: we do not initialize dest.pin_time. This must be done by the caller
// Calculate database-level statistics
2009-02-02 04:35:52 +01:00
for (int i = 0; i < TOTAL_ITEMS; i++)
2009-02-01 23:10:12 +01:00
values[i] = new_stat.values[i] - values[i];
2009-04-04 18:39:31 +02:00
2009-02-01 23:10:12 +01:00
dest.pin_counters = values;
// Calculate relation-level statistics
temp.clear();
RelCounters::ConstAccessor new_acc(&new_stat.rel_counts);
2009-02-02 04:35:52 +01:00
if (new_acc.getFirst())
{
// This loop assumes that base array is smaller than new one
RelCounters::Accessor base_acc(&rel_counts);
bool base_found = base_acc.getFirst();
2009-02-02 04:35:52 +01:00
do
2009-02-01 23:10:12 +01:00
{
2009-02-02 04:35:52 +01:00
const RelationCounts* counts = &new_acc.current();
2009-04-04 18:39:31 +02:00
if (base_found && base_acc.current().rlc_relation_id == counts->rlc_relation_id)
2009-02-02 04:35:52 +01:00
{
RelationCounts* base_counts = &base_acc.current();
bool all_zeros = true;
for (int i = 0; i < DBB_max_rel_count; i++)
{
if ((base_counts->rlc_counter[i] = counts->rlc_counter[i] - base_counts->rlc_counter[i]))
all_zeros = false;
}
// Point TraceCounts to counts array from baseline object
2009-04-04 18:39:31 +02:00
if (!all_zeros)
2009-02-02 04:35:52 +01:00
{
jrd_rel* relation = counts->rlc_relation_id < dbb->dbb_relations->count() ?
(*dbb->dbb_relations)[counts->rlc_relation_id] : NULL;
TraceCounts traceCounts;
traceCounts.trc_relation_id = counts->rlc_relation_id;
traceCounts.trc_counters = base_counts->rlc_counter;
traceCounts.trc_relation_name = relation ? relation->rel_name.c_str() : NULL;
temp.add(traceCounts);
}
base_found = base_acc.getNext();
2009-02-01 23:10:12 +01:00
}
2009-04-04 18:39:31 +02:00
else
2009-02-01 23:10:12 +01:00
{
2009-02-02 04:35:52 +01:00
jrd_rel* relation = counts->rlc_relation_id < dbb->dbb_relations->count() ?
(*dbb->dbb_relations)[counts->rlc_relation_id] : NULL;
// Point TraceCounts to counts array from object with updated counters
2009-02-01 23:10:12 +01:00
TraceCounts traceCounts;
traceCounts.trc_relation_id = counts->rlc_relation_id;
2009-02-02 04:35:52 +01:00
traceCounts.trc_counters = counts->rlc_counter;
2009-02-01 23:10:12 +01:00
traceCounts.trc_relation_name = relation ? relation->rel_name.c_str() : NULL;
temp.add(traceCounts);
}
2009-02-02 04:35:52 +01:00
} while (new_acc.getNext());
}
2009-02-01 23:10:12 +01:00
dest.pin_count = temp.getCount();
dest.pin_tables = temp.begin();
2009-02-02 04:35:52 +01:00
2009-02-01 23:10:12 +01:00
return &dest;
}
#else // REL_COUNTS_TREE
2009-04-28 12:54:45 +02:00
void RuntimeStatistics::bumpValue(const StatType index, SLONG relation_id)
{
fb_assert(index >= 0);
++relChgNumber;
2009-04-04 18:39:31 +02:00
size_t pos;
if (rel_counts.find(relation_id, pos))
rel_counts[pos].rlc_counter[index]++;
else
{
RelationCounts counts;
memset(&counts, 0, sizeof(counts));
counts.rlc_relation_id = relation_id;
counts.rlc_counter[index]++;
rel_counts.add(counts);
}
}
2009-02-07 16:20:34 +01:00
void RuntimeStatistics::addRelCounts(const RelCounters& other, bool add)
{
2009-02-07 16:20:34 +01:00
if (other.isEmpty())
return;
RelCounters::const_iterator src(other.begin());
const RelCounters::const_iterator end(other.end());
size_t pos;
rel_counts.find(src->rlc_relation_id, pos);
for (; src != end; ++src)
{
const size_t cnt = rel_counts.getCount();
while (pos < cnt && rel_counts[pos].rlc_relation_id < src->rlc_relation_id)
pos++;
2009-04-04 18:39:31 +02:00
if (pos >= cnt || rel_counts[pos].rlc_relation_id > src->rlc_relation_id)
{
RelationCounts counts;
memset(&counts, 0, sizeof(counts));
counts.rlc_relation_id = src->rlc_relation_id;
rel_counts.insert(pos, counts);
}
fb_assert(pos >= 0 && pos < rel_counts.getCount());
2009-02-07 16:20:34 +01:00
RelationCounts* dst = &(rel_counts[pos]);
fb_assert(dst->rlc_relation_id == src->rlc_relation_id);
if (add)
{
for (int index = 0; index < FB_NELEM(src->rlc_counter); index++)
dst->rlc_counter[index] += src->rlc_counter[index];
}
else
{
for (int index = 0; index < FB_NELEM(src->rlc_counter); index++)
dst->rlc_counter[index] -= src->rlc_counter[index];
}
}
}
2009-02-07 13:30:25 +01:00
PerformanceInfo* RuntimeStatistics::computeDifference(Database* dbb,
const RuntimeStatistics& new_stat,
PerformanceInfo& dest,
TraceCountsArray& temp)
{
// NOTE: we do not initialize dest.pin_time. This must be done by the caller
// Calculate database-level statistics
for (int i = 0; i < TOTAL_ITEMS; i++)
values[i] = new_stat.values[i] - values[i];
2009-04-04 18:39:31 +02:00
dest.pin_counters = values;
// Calculate relation-level statistics
temp.clear();
// This loop assumes that base array is smaller than new one
2009-02-11 12:37:43 +01:00
RelCounters::iterator base_cnts = rel_counts.begin();
bool base_found = (base_cnts != rel_counts.end());
RelCounters::const_iterator new_cnts = new_stat.rel_counts.begin();
const RelCounters::const_iterator end = new_stat.rel_counts.end();
for (; new_cnts != end; ++new_cnts)
{
2009-04-04 18:39:31 +02:00
if (base_found && base_cnts->rlc_relation_id == new_cnts->rlc_relation_id)
{
bool all_zeros = true;
for (int i = 0; i < DBB_max_rel_count; i++)
{
if ((base_cnts->rlc_counter[i] = new_cnts->rlc_counter[i] - base_cnts->rlc_counter[i]))
all_zeros = false;
}
// Point TraceCounts to counts array from baseline object
2009-04-04 18:39:31 +02:00
if (!all_zeros)
{
2009-06-04 17:17:18 +02:00
jrd_rel* relation = size_t(new_cnts->rlc_relation_id) < dbb->dbb_relations->count() ?
(*dbb->dbb_relations)[new_cnts->rlc_relation_id] : NULL;
TraceCounts traceCounts;
traceCounts.trc_relation_id = new_cnts->rlc_relation_id;
traceCounts.trc_counters = base_cnts->rlc_counter;
traceCounts.trc_relation_name = relation ? relation->rel_name.c_str() : NULL;
temp.add(traceCounts);
}
++base_cnts;
base_found = (base_cnts != rel_counts.end());
}
2009-04-04 18:39:31 +02:00
else
{
2009-06-04 17:17:18 +02:00
jrd_rel* relation = size_t(new_cnts->rlc_relation_id) < dbb->dbb_relations->count() ?
(*dbb->dbb_relations)[new_cnts->rlc_relation_id] : NULL;
// Point TraceCounts to counts array from object with updated counters
TraceCounts traceCounts;
traceCounts.trc_relation_id = new_cnts->rlc_relation_id;
traceCounts.trc_counters = new_cnts->rlc_counter;
traceCounts.trc_relation_name = relation ? relation->rel_name.c_str() : NULL;
temp.add(traceCounts);
}
};
dest.pin_count = temp.getCount();
dest.pin_tables = temp.begin();
return &dest;
}
#endif // REL_COUNTS_TREE
} // namespace