@@ -20,7 +20,7 @@ struct FlameGraphItem
2020 Vector<FlameGraphItem> children;
2121};
2222
23- static void BuildFlameGraph ( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<short_ptr<ZoneEvent>>& zones )
23+ void View:: BuildFlameGraph ( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<short_ptr<ZoneEvent>>& zones )
2424{
2525 FlameGraphItem* it;
2626 int16_t last = 0 ;
@@ -112,7 +112,104 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
112112 }
113113}
114114
115- static void BuildFlameGraph ( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<SampleData>& samples )
115+ void View::BuildFlameGraph ( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<short_ptr<ZoneEvent>>& zones, const ContextSwitch* ctx )
116+ {
117+ assert ( ctx );
118+ FlameGraphItem* it;
119+ int16_t last = 0 ;
120+
121+ if ( zones.is_magic () )
122+ {
123+ auto & vec = *(Vector<ZoneEvent>*)&zones;
124+ for ( auto & v : vec )
125+ {
126+ if ( !v.IsEndValid () ) break ;
127+ const auto srcloc = v.SrcLoc ();
128+ int64_t duration;
129+ uint64_t cnt;
130+ if ( !GetZoneRunningTime ( ctx, v, duration, cnt ) ) break ;
131+ if ( srcloc == last )
132+ {
133+ it->time += duration;
134+ if ( v.HasChildren () )
135+ {
136+ auto & children = worker.GetZoneChildren ( v.Child () );
137+ BuildFlameGraph ( worker, it->children , children, ctx );
138+ }
139+ }
140+ else
141+ {
142+ it = std::find_if ( data.begin (), data.end (), [srcloc]( const auto & v ) { return v.srcloc == srcloc; } );
143+ if ( it == data.end () )
144+ {
145+ data.push_back ( FlameGraphItem { srcloc, duration } );
146+ if ( v.HasChildren () )
147+ {
148+ auto & children = worker.GetZoneChildren ( v.Child () );
149+ BuildFlameGraph ( worker, data.back ().children , children, ctx );
150+ }
151+ it = &data.back ();
152+ }
153+ else
154+ {
155+ it->time += duration;
156+ if ( v.HasChildren () )
157+ {
158+ auto & children = worker.GetZoneChildren ( v.Child () );
159+ BuildFlameGraph ( worker, it->children , children, ctx );
160+ }
161+ }
162+ last = srcloc;
163+ }
164+ }
165+ }
166+ else
167+ {
168+ for ( auto & v : zones )
169+ {
170+ if ( !v->IsEndValid () ) break ;
171+ const auto srcloc = v->SrcLoc ();
172+ int64_t duration;
173+ uint64_t cnt;
174+ if ( !GetZoneRunningTime ( ctx, *v, duration, cnt ) ) break ;
175+ if ( srcloc == last )
176+ {
177+ it->time += duration;
178+ if ( v->HasChildren () )
179+ {
180+ auto & children = worker.GetZoneChildren ( v->Child () );
181+ BuildFlameGraph ( worker, it->children , children, ctx );
182+ }
183+ }
184+ else
185+ {
186+ it = std::find_if ( data.begin (), data.end (), [srcloc]( const auto & v ) { return v.srcloc == srcloc; } );
187+ if ( it == data.end () )
188+ {
189+ data.push_back ( FlameGraphItem { srcloc, duration } );
190+ if ( v->HasChildren () )
191+ {
192+ auto & children = worker.GetZoneChildren ( v->Child () );
193+ BuildFlameGraph ( worker, data.back ().children , children, ctx );
194+ }
195+ it = &data.back ();
196+ }
197+ else
198+ {
199+ it->time += duration;
200+ if ( v->HasChildren () )
201+ {
202+ auto & children = worker.GetZoneChildren ( v->Child () );
203+ BuildFlameGraph ( worker, it->children , children, ctx );
204+ }
205+ }
206+ last = srcloc;
207+ }
208+ }
209+ }
210+ }
211+
212+ void View::BuildFlameGraph ( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<SampleData>& samples )
116213{
117214 for ( auto & v : samples )
118215 {
@@ -522,7 +619,18 @@ void View::DrawFlameGraph()
522619 {
523620 for ( auto & thread : m_worker.GetThreadData () )
524621 {
525- if ( FlameGraphThread ( thread->id ) ) BuildFlameGraph ( m_worker, data, thread->timeline );
622+ if ( FlameGraphThread ( thread->id ) )
623+ {
624+ if ( m_flameRunningTime )
625+ {
626+ const auto ctx = m_worker.GetContextSwitchData ( thread->id );
627+ if ( ctx ) BuildFlameGraph ( m_worker, data, thread->timeline , ctx );
628+ }
629+ else
630+ {
631+ BuildFlameGraph ( m_worker, data, thread->timeline );
632+ }
633+ }
526634 }
527635 }
528636 else
0 commit comments