From 25dde50cd4ea20b6a5587151df7c81bec6b5c77c Mon Sep 17 00:00:00 2001
From: andreykproductengine <andreykproductengine@lindenlab.com>
Date: Thu, 16 May 2019 18:50:19 +0300
Subject: [PATCH] SL-11106 Time-limit benchmark test

---
 indra/newview/llglsandbox.cpp | 37 ++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)

diff --git a/indra/newview/llglsandbox.cpp b/indra/newview/llglsandbox.cpp
index a9b15fc8b62..fea01786f35 100644
--- a/indra/newview/llglsandbox.cpp
+++ b/indra/newview/llglsandbox.cpp
@@ -1005,7 +1005,10 @@ F32 gpu_benchmark()
 
 	//number of samples to take
 	const S32 samples = 64;
-		
+
+	//time limit, allocation operations shouldn't take longer then 30 seconds, same for actual benchmark.
+	const F32 time_limit = 30;
+
 	ShaderProfileHelper initProfile;
 	
 	std::vector<LLRenderTarget> dest(count);
@@ -1023,12 +1026,14 @@ F32 gpu_benchmark()
 	gGL.setColorMask(true, true);
 	LLGLDepthTest depth(GL_FALSE);
 
+	LLTimer alloc_timer;
+	alloc_timer.start();
 	for (U32 i = 0; i < count; ++i)
 	{
 		//allocate render targets and textures
 		if (!dest[i].allocate(res, res, GL_RGBA, false, false, LLTexUnit::TT_TEXTURE, true))
 		{
-			LL_WARNS() << "Failed to allocate render target." << LL_ENDL;
+			LL_WARNS("Benchmark") << "Failed to allocate render target." << LL_ENDL;
 			// abandon the benchmark test
 			delete[] pixels;
 			return -1.f;
@@ -1040,12 +1045,20 @@ F32 gpu_benchmark()
 		if (!texHolder.bind(i))
 		{
 			// can use a dummy value mDummyTexUnit = new LLTexUnit(-1);
-			LL_WARNS() << "Failed to bind tex unit." << LL_ENDL;
+			LL_WARNS("Benchmark") << "Failed to bind tex unit." << LL_ENDL;
 			// abandon the benchmark test
 			delete[] pixels;
 			return -1.f;
 		}
 		LLImageGL::setManualImage(GL_TEXTURE_2D, 0, GL_RGBA, res,res,GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+
+		if (alloc_timer.getElapsedTimeF32() > time_limit)
+		{
+			// abandon the benchmark test
+			LL_WARNS("Benchmark") << "Allocation operation took longer then 30 seconds, stopping." << LL_ENDL;
+			delete[] pixels;
+			return -1.f;
+		}
 	}
 
     delete [] pixels;
@@ -1055,7 +1068,7 @@ F32 gpu_benchmark()
 
 	if (!buff->allocateBuffer(3, 0, true))
 	{
-		LL_WARNS() << "Failed to allocate buffer during benchmark." << LL_ENDL;
+		LL_WARNS("Benchmark") << "Failed to allocate buffer during benchmark." << LL_ENDL;
 		// abandon the benchmark test
 		return -1.f;
 	}
@@ -1065,7 +1078,7 @@ F32 gpu_benchmark()
 
 	if (! buff->getVertexStrider(v))
 	{
-		LL_WARNS() << "GL LLVertexBuffer::getVertexStrider() returned false, "
+		LL_WARNS("Benchmark") << "GL LLVertexBuffer::getVertexStrider() returned false, "
 				   << "buff->getMappedData() is"
 				   << (buff->getMappedData()? " not" : "")
 				   << " NULL" << LL_ENDL;
@@ -1086,7 +1099,8 @@ F32 gpu_benchmark()
 	buff->setBuffer(LLVertexBuffer::MAP_VERTEX);
 	glFinish();
 
-	for (S32 c = -1; c < samples; ++c)
+	F32 time_passed = 0; // seconds
+	for (S32 c = -1; c < samples && time_passed < time_limit; ++c)
 	{
 		LLTimer timer;
 		timer.start();
@@ -1103,6 +1117,7 @@ F32 gpu_benchmark()
 		glFinish();
 
 		F32 time = timer.getElapsedTimeF32();
+		time_passed += time;
 
 		if (c >= 0) // <-- ignore the first sample as it tends to be artificially slow
 		{ 
@@ -1117,12 +1132,12 @@ F32 gpu_benchmark()
 
 	F32 gbps = results[results.size()/2];
 
-	LL_INFOS() << "Memory bandwidth is " << llformat("%.3f", gbps) << "GB/sec according to CPU timers" << LL_ENDL;
-  
+	LL_INFOS("Benchmark") << "Memory bandwidth is " << llformat("%.3f", gbps) << "GB/sec according to CPU timers, " << (F32)results.size() << " tests took " << time_passed << " seconds" << LL_ENDL;
+
 #if LL_DARWIN
     if (gbps > 512.f)
     { 
-        LL_WARNS() << "Memory bandwidth is improbably high and likely incorrect; discarding result." << LL_ENDL;
+        LL_WARNS("Benchmark") << "Memory bandwidth is improbably high and likely incorrect; discarding result." << LL_ENDL;
         //OSX is probably lying, discard result
         return -1.f;
     }
@@ -1131,11 +1146,11 @@ F32 gpu_benchmark()
 	F32 ms = gBenchmarkProgram.mTimeElapsed/1000000.f;
 	F32 seconds = ms/1000.f;
 
-	F64 samples_drawn = res*res*count*samples;
+	F64 samples_drawn = res*res*count*results.size();
 	F32 samples_sec = (samples_drawn/1000000000.0)/seconds;
 	gbps = samples_sec*8;
 
-	LL_INFOS() << "Memory bandwidth is " << llformat("%.3f", gbps) << "GB/sec according to ARB_timer_query" << LL_ENDL;
+	LL_INFOS("Benchmark") << "Memory bandwidth is " << llformat("%.3f", gbps) << "GB/sec according to ARB_timer_query, total time " << seconds << " seconds" << LL_ENDL;
 
 	return gbps;
 }
-- 
GitLab