@@ -33,52 +33,75 @@ namespace Jrd {
3333
3434GlobalPtr<RuntimeStatistics> RuntimeStatistics::dummy;
3535
36- void RuntimeStatistics::adjustRelStats (const RuntimeStatistics& baseStats, const RuntimeStatistics& newStats)
36+ void RuntimeStatistics::adjust (const RuntimeStatistics& baseStats, const RuntimeStatistics& newStats)
3737{
38- if (baseStats.relChgNumber == newStats.relChgNumber )
38+ if (baseStats.allChgNumber == newStats.allChgNumber )
3939 return ;
4040
41- relChgNumber++;
41+ allChgNumber++;
42+ for (size_t i = 0 ; i < GLOBAL_ITEMS; ++i)
43+ values[i] += newStats.values [i] - baseStats.values [i];
44+
45+ if (baseStats.pageChgNumber != newStats.pageChgNumber )
46+ {
47+ pageChgNumber++;
48+ page_counts.adjust (baseStats.page_counts , newStats.page_counts );
49+ }
50+
51+ if (baseStats.relChgNumber != newStats.relChgNumber )
52+ {
53+ relChgNumber++;
54+ rel_counts.adjust (baseStats.rel_counts , newStats.rel_counts );
55+ }
56+ }
57+
58+ void RuntimeStatistics::adjustPageStats (RuntimeStatistics& baseStats, const RuntimeStatistics& newStats)
59+ {
60+ if (baseStats.allChgNumber == newStats.allChgNumber )
61+ return ;
4262
43- auto locate = [this ](SLONG relId) -> FB_SIZE_T
63+ allChgNumber++;
64+ for (size_t i = 0 ; i < PAGE_TOTAL_ITEMS; ++i)
4465 {
45- FB_SIZE_T pos;
46- if (!rel_counts.find (relId, pos))
47- rel_counts.insert (pos, RelationCounts (relId));
48- return pos;
49- };
66+ const SINT64 delta = newStats.values [i] - baseStats.values [i];
5067
51- auto baseIter = baseStats.rel_counts .begin (), newIter = newStats.rel_counts .begin ();
52- const auto baseEnd = baseStats.rel_counts .end (), newEnd = newStats.rel_counts .end ();
68+ values[i] += delta;
69+ baseStats.values [i] += delta;
70+ }
71+ }
72+
73+ template <class Counts , typename Key>
74+ void RuntimeStatistics::GroupedCountsArray<Counts, Key>::adjust(const GroupedCountsArray& baseStats, const GroupedCountsArray& newStats)
75+ {
76+ auto baseIter = baseStats.m_counts .begin (), newIter = newStats.m_counts .begin ();
77+ const auto baseEnd = baseStats.m_counts .end (), newEnd = newStats.m_counts .end ();
5378
54- // The loop below assumes that newStats cannot miss relations existing in baseStats,
79+ // The loop below assumes that newStats cannot miss objects existing in baseStats,
5580 // this must be always the case as long as newStats is an incremented version of baseStats
5681
5782 while (newIter != newEnd || baseIter != baseEnd)
5883 {
5984 if (baseIter == baseEnd)
6085 {
61- // Relation exists in newStats but missing in baseStats
62- const auto newRelId = newIter->getRelationId ();
63- rel_counts[ locate (newRelId) ] += *newIter++;
86+ // Object exists in newStats but missing in baseStats
87+ const auto newKey = newIter->getGroupKey ();
88+ (* this )[newKey ] += *newIter++;
6489 }
6590 else if (newIter != newEnd)
6691 {
67- const auto baseRelId = baseIter->getRelationId ();
68- const auto newRelId = newIter->getRelationId ();
92+ const auto baseKey = baseIter->getGroupKey ();
93+ const auto newKey = newIter->getGroupKey ();
6994
70- if (newRelId == baseRelId )
95+ if (newKey == baseKey )
7196 {
72- // Relation exists in both newStats and baseStats
73- fb_assert (baseRelId == newRelId);
74- const auto pos = locate (baseRelId);
75- rel_counts[pos] -= *baseIter++;
76- rel_counts[pos] += *newIter++;
97+ // Object exists in both newStats and baseStats
98+ (*this )[newKey] += *newIter++;
99+ (*this )[newKey] -= *baseIter++;
77100 }
78- else if (newRelId < baseRelId )
101+ else if (newKey < baseKey )
79102 {
80- // Relation exists in newStats but missing in baseStats
81- rel_counts[ locate (newRelId) ] += *newIter++;
103+ // Object exists in newStats but missing in baseStats
104+ (* this )[newKey ] += *newIter++;
82105 }
83106 else
84107 fb_assert (false ); // should never happen
@@ -89,116 +112,55 @@ void RuntimeStatistics::adjustRelStats(const RuntimeStatistics& baseStats, const
89112}
90113
91114PerformanceInfo* RuntimeStatistics::computeDifference (Attachment* att,
92- const RuntimeStatistics& new_stat ,
115+ const RuntimeStatistics& newStats ,
93116 PerformanceInfo& dest,
94- TraceCountsArray& temp ,
117+ TraceCountsArray& relStatsTemp ,
95118 ObjectsArray<string>& tempNames)
96119{
97120 // NOTE: we do not initialize dest.pin_time. This must be done by the caller
98121
99122 // Calculate database-level statistics
100- for (size_t i = 0 ; i < GLOBAL_ITEMS; i++)
101- values[i] = new_stat.values [i] - values[i];
102123
103- dest.pin_counters = values;
124+ for (size_t i = 0 ; i < GLOBAL_ITEMS; i++)
125+ values[i] = newStats.values [i] - values[i];
104126
105127 // Calculate relation-level statistics
106- temp.clear ();
107- tempNames.clear ();
108128
109- // This loop assumes that base array is smaller than new one
110- RelCounters::iterator base_cnts = rel_counts.begin ();
111- bool base_found = (base_cnts != rel_counts.end ());
129+ relStatsTemp.clear ();
112130
113- for (const auto & new_cnts : new_stat .rel_counts )
131+ for (const auto & newCounts : newStats .rel_counts )
114132 {
115- const SLONG rel_id = new_cnts.getRelationId ();
133+ const auto relationId = newCounts.getGroupKey ();
134+ auto & counts = rel_counts[relationId];
116135
117- if (base_found && base_cnts-> getRelationId () == rel_id )
136+ if (counts. setToDiff (newCounts) )
118137 {
119- // Point TraceCounts to counts array from baseline object
120- if (base_cnts->setToDiff (new_cnts))
121- {
122- jrd_rel* const relation =
123- rel_id < static_cast <SLONG>(att->att_relations ->count ()) ?
124- (*att->att_relations )[rel_id] : NULL ;
125-
126- TraceCounts traceCounts;
127- traceCounts.trc_relation_id = rel_id;
128- traceCounts.trc_counters = base_cnts->getCounterVector ();
138+ const char * relationName = nullptr ;
129139
130- if (relation)
140+ if (relationId < static_cast <SLONG>(att->att_relations ->count ()))
141+ {
142+ if (const auto relation = (*att->att_relations )[relationId])
131143 {
132144 auto & tempName = tempNames.add ();
133145 tempName = relation->rel_name .toQuotedString ();
134- traceCounts. trc_relation_name = tempName.c_str ();
146+ relationName = tempName.c_str ();
135147 }
136- else
137- traceCounts.trc_relation_name = nullptr ;
138-
139- temp.add (traceCounts);
140148 }
141149
142- ++base_cnts;
143- base_found = (base_cnts != rel_counts.end ());
144- }
145- else
146- {
147- jrd_rel* const relation =
148- rel_id < static_cast <SLONG>(att->att_relations ->count ()) ?
149- (*att->att_relations )[rel_id] : NULL ;
150-
151- // Point TraceCounts to counts array from object with updated counters
152150 TraceCounts traceCounts;
153- traceCounts.trc_relation_id = rel_id;
154- traceCounts.trc_counters = new_cnts.getCounterVector ();
155-
156- if (relation)
157- {
158- auto & tempName = tempNames.add ();
159- tempName = relation->rel_name .toQuotedString ();
160- traceCounts.trc_relation_name = tempName.c_str ();
161- }
162- else
163- traceCounts.trc_relation_name = nullptr ;
164-
165- temp.add (traceCounts);
151+ traceCounts.trc_relation_id = relationId;
152+ traceCounts.trc_relation_name = relationName;
153+ traceCounts.trc_counters = counts.getCounterVector ();
154+ relStatsTemp.add (traceCounts);
166155 }
167- };
156+ }
168157
169- dest.pin_count = temp .getCount ();
170- dest.pin_tables = temp .begin ();
158+ dest.pin_count = relStatsTemp .getCount ();
159+ dest.pin_tables = relStatsTemp .begin ();
171160
172161 return &dest;
173162}
174163
175- void RuntimeStatistics::adjust (const RuntimeStatistics& baseStats, const RuntimeStatistics& newStats)
176- {
177- if (baseStats.allChgNumber == newStats.allChgNumber )
178- return ;
179-
180- allChgNumber++;
181- for (size_t i = 0 ; i < GLOBAL_ITEMS; ++i)
182- values[i] += newStats.values [i] - baseStats.values [i];
183-
184- adjustRelStats (baseStats, newStats);
185- }
186-
187- void RuntimeStatistics::adjustPageStats (RuntimeStatistics& baseStats, const RuntimeStatistics& newStats)
188- {
189- if (baseStats.allChgNumber == newStats.allChgNumber )
190- return ;
191-
192- allChgNumber++;
193- for (size_t i = 0 ; i < PAGE_TOTAL_ITEMS; ++i)
194- {
195- const SINT64 delta = newStats.values [i] - baseStats.values [i];
196-
197- values[i] += delta;
198- baseStats.values [i] += delta;
199- }
200- }
201-
202164RuntimeStatistics::Accumulator::Accumulator (thread_db* tdbb, const jrd_rel* relation,
203165 const RecordStatType type)
204166 : m_tdbb(tdbb), m_type(type), m_id(relation->rel_id)
0 commit comments