From f850ae03b399a5cc7aa32f82b8ed996518a86a2a Mon Sep 17 00:00:00 2001
From: Richard Linden <none@none>
Date: Mon, 20 May 2013 00:01:57 -0700
Subject: [PATCH] SH-3931 WIP Interesting: Add graphs to visualize scene load
 metrics fixed copy construction of Recorders, eliminated most zero-length
 frames fixed reset behavior of periodic recordings and extendable recordings
 to clear entire history removed busy-loop recording of stats from worker
 threads...stats reported only when work is done

---
 indra/llcommon/llqueuedthread.cpp   | 11 +++++----
 indra/llcommon/lltracerecording.cpp | 38 +++++++++++++++++++++--------
 indra/newview/llscenemonitor.cpp    |  4 +--
 indra/newview/llviewerdisplay.cpp   |  2 ++
 indra/newview/pipeline.cpp          |  2 --
 5 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/indra/llcommon/llqueuedthread.cpp b/indra/llcommon/llqueuedthread.cpp
index 956642e97a9..4339f203db7 100644
--- a/indra/llcommon/llqueuedthread.cpp
+++ b/indra/llcommon/llqueuedthread.cpp
@@ -404,6 +404,7 @@ S32 LLQueuedThread::processNextRequest()
 	QueuedRequest *req;
 	// Get next request from pool
 	lockData();
+	
 	while(1)
 	{
 		req = NULL;
@@ -468,10 +469,11 @@ S32 LLQueuedThread::processNextRequest()
 				ms_sleep(1); // sleep the thread a little
 			}
 		}
+		
+		LLTrace::get_thread_recorder()->pushToMaster();
 	}
 
 	S32 pending = getPending();
-
 	return pending;
 }
 
@@ -500,6 +502,7 @@ void LLQueuedThread::run()
 		
 		if (isQuitting())
 		{
+			LLTrace::get_thread_recorder()->pushToMaster();
 			endThread();
 			break;
 		}
@@ -508,11 +511,9 @@ void LLQueuedThread::run()
 
 		threadedUpdate();
 		
-		int res = processNextRequest();
-
-		LLTrace::get_thread_recorder()->pushToMaster();
+		int pending_work = processNextRequest();
 
-		if (res == 0)
+		if (pending_work == 0)
 		{
 			mIdleThread = TRUE;
 			ms_sleep(1);
diff --git a/indra/llcommon/lltracerecording.cpp b/indra/llcommon/lltracerecording.cpp
index 6b5c6c7d3ef..4aa3a5a0f7c 100644
--- a/indra/llcommon/lltracerecording.cpp
+++ b/indra/llcommon/lltracerecording.cpp
@@ -133,11 +133,19 @@ Recording::Recording()
 {}
 
 Recording::Recording( const Recording& other )
-:	RecordingBuffers(other),
-	mElapsedSeconds(other.mElapsedSeconds),
-	mSamplingTimer(other.mSamplingTimer)
+:	mSamplingTimer(other.mSamplingTimer)
 {
-	LLStopWatchControlsMixin<Recording>::setPlayState(other.getPlayState());
+	Recording& mutable_other = const_cast<Recording&>(other);
+	EPlayState other_play_state = other.getPlayState();
+	mutable_other.pause();
+
+	appendBuffers(other);
+
+	LLStopWatchControlsMixin<Recording>::setPlayState(other_play_state);
+	mutable_other.setPlayState(other_play_state);
+
+	// above call will clear mElapsedSeconds as a side effect, so copy it here
+	mElapsedSeconds = other.mElapsedSeconds;
 }
 
 
@@ -380,9 +388,7 @@ void PeriodicRecording::nextPeriod()
 
 	Recording& old_recording = getCurRecording();
 
-	mCurPeriod = mRecordingPeriods.empty()
-				? mCurPeriod + 1
-				: (mCurPeriod + 1) % mRecordingPeriods.size();
+	mCurPeriod = (mCurPeriod + 1) % mRecordingPeriods.size();
 	old_recording.splitTo(getCurRecording());
 }
 
@@ -526,9 +532,22 @@ void PeriodicRecording::handleStop()
 
 void PeriodicRecording::handleReset()
 {
-	mRecordingPeriods.clear();
-	mRecordingPeriods.push_back(Recording());
+	if (mAutoResize)
+	{
+		mRecordingPeriods.clear();
+		mRecordingPeriods.push_back(Recording());
+	}
+	else
+	{
+		for (std::vector<Recording>::iterator it = mRecordingPeriods.begin(), end_it = mRecordingPeriods.end();
+			it != end_it;
+			++it)
+		{
+			it->reset();
+		}
+	}
 	mCurPeriod = 0;
+	getCurRecording().setPlayState(getPlayState());
 }
 
 void PeriodicRecording::handleSplitTo(PeriodicRecording& other)
@@ -656,7 +675,6 @@ void LLStopWatchControlsMixinCommon::stop()
 	case STOPPED:
 		break;
 	case PAUSED:
-		handleStop();
 		break;
 	case STARTED:
 		handleStop();
diff --git a/indra/newview/llscenemonitor.cpp b/indra/newview/llscenemonitor.cpp
index c101fe7debf..c2e00384a16 100644
--- a/indra/newview/llscenemonitor.cpp
+++ b/indra/newview/llscenemonitor.cpp
@@ -481,13 +481,13 @@ void LLSceneMonitor::fetchQueryResult()
 			LL_DEBUGS("SceneMonitor") << "Frame difference: " << std::setprecision(4) << mDiffResult << LL_ENDL;
 			sample(sFramePixelDiff, mDiffResult);
 
-			mRecording->getPotentialRecording().nextPeriod();
-
 			static LLCachedControl<F32> diff_threshold(gSavedSettings,"SceneLoadingPixelDiffThreshold");
 			if(mDiffResult > diff_threshold())
 			{
 				mRecording->extend();
 			}
+
+			mRecording->getPotentialRecording().nextPeriod();
 		}
 	}
 }
diff --git a/indra/newview/llviewerdisplay.cpp b/indra/newview/llviewerdisplay.cpp
index 970862892cb..5974c84596e 100644
--- a/indra/newview/llviewerdisplay.cpp
+++ b/indra/newview/llviewerdisplay.cpp
@@ -803,6 +803,8 @@ void display(BOOL rebuild, F32 zoom_factor, int subfield, BOOL for_snapshot)
 			}
 		}
 
+		LLSceneMonitor::getInstance()->fetchQueryResult();
+		
 		LLGLState::checkStates();
 		LLGLState::checkClientArrays();
 
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index acf3e7aa077..3f6269e7683 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -3233,8 +3233,6 @@ void LLPipeline::stateSort(LLCamera& camera, LLCullResult &result)
 	}
 		
 	postSort(camera);	
-
-	LLSceneMonitor::getInstance()->fetchQueryResult();
 }
 
 void LLPipeline::stateSort(LLSpatialGroup* group, LLCamera& camera)
-- 
GitLab