⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trace.cpp

📁 机甲指挥官2源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//===========================================================================//
// File:	analysis.cpp                                                     //
// Contents: Utilities for using logic analyzer with parallel port dongle    //
//---------------------------------------------------------------------------//
// Copyright (C) Microsoft Corporation. All rights reserved.                 //
//===========================================================================//

#include "StuffHeaders.hpp"
#include <ToolOS.hpp>

#if defined(TRACE_ENABLED)

	//##########################################################################
	//############################    Trace    #################################
	//##########################################################################

	BYTE
		Trace::NextTraceID = 0;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	Trace::Trace(
		const char* name,
		Type type 
	):
		Plug(DefaultData)
	{
		traceNumber = NextTraceID++;
		traceType = (BYTE)type;
		traceName = name;
		
		lastActivity = 0.0;

		Check_Object(TraceManager::Instance);
		TraceManager::Instance->Add(this);
	}

	#if defined(USE_TIME_ANALYSIS)
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//
		void
			Trace::PrintUsage(Scalar usage)
		{
			Check_Object(this);

			SPEW((GROUP_STUFF_TRACE, "%f+", usage));
		}
	#endif

	//##########################################################################
	//###########################    BitTrace    ###############################
	//##########################################################################

	BYTE
		BitTrace::NextActiveLine = 0;
	int
		BitTrace::NextBit = 0;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	BitTrace::BitTrace(const char* name):
		Trace(name, BitType)
	{
		activeLine = NextActiveLine++;
		bitFlag = (NextBit < 32) ? 1 << NextBit++ : 0;
		#if defined(USE_ACTIVE_PROFILE)
			DEBUG_STREAM << name << " used trace line "
			 << static_cast<int>(activeLine) << "!\n";
			if (!IsLineValidImplementation(activeLine))
			{
				STOP(("Invalid active trace line!"));
			}
		#endif

		BitTrace::ResetTrace();
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		BitTrace::DumpTraceStatus()
	{
		Check_Object(this);
		SPEW((
			GROUP_STUFF_TRACE,
			"%d = %d+",
			static_cast<int>(activeLine),
			traceUp
		));
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		BitTrace::ResetTrace()
	{
		#if defined(USE_TIME_ANALYSIS)
			traceUp = 0;
			lastUpTime = 0.0;
			totalUpTime = 0.0;
			TraceManager::Instance->activeBits &= ~bitFlag;
		#endif

		#if defined(USE_ACTIVE_PROFILE)
			ClearLineImplementation(activeLine);
		#endif
	}

	#if defined(USE_TIME_ANALYSIS)

		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//
		void
			BitTrace::StartTiming()
		{
			Check_Object(this);
			totalUpTime = 0.0;
		}

		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//
		Scalar
			BitTrace::CalculateUsage(
				Time when,
				Time sample_time
			)
		{
			if (traceUp > 0)
			{
				totalUpTime += when - lastActivity;
			}
			Scalar result = static_cast<Scalar>(totalUpTime / sample_time);
			SPEW((GROUP_STUFF_TRACE, "%4fs, +", totalUpTime));
			return result;
		}

		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//
		void
			BitTrace::PrintUsage(Scalar usage)
		{
			Check_Object(this);

			SPEW((GROUP_STUFF_TRACE, "%4f%% CPU+", (usage*100.0f)));
			#if defined(USE_ACTIVE_PROFILE)
			SPEW((GROUP_STUFF_TRACE, " (active on line %d)", static_cast<int>(activeLine)));
			#endif
		}

	#endif

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		BitTrace::Set()
	{
		Check_Object(this);

		if (!traceUp++)
		{
			#if defined(USE_ACTIVE_PROFILE)
				SetLineImplementation(activeLine);
			#endif

			TraceManager::Instance->activeBits |= bitFlag;

			#if defined(USE_TIME_ANALYSIS) || defined(USE_TRACE_LOG)
				Time now = gos_GetHiResTime();
			#endif

			#if defined(USE_TIME_ANALYSIS)
				lastActivity = now;
			#endif

			#if defined(USE_TRACE_LOG)
				// Check_Object(traceManager);
				IncrementSampleCount();
				MemoryStream *log = GetTraceLog();
				if (log)
				{
					Check_Object(log);
					TraceSample *sample =
						Cast_Pointer(TraceSample*, log->GetPointer());
					sample->sampleLength = sizeof(*sample);
					sample->sampleType = TraceSample::GoingUp;
					sample->traceNumber = traceNumber;
					sample->sampleTime = now;
					log->AdvancePointer(sample->sampleLength);
				}
			#endif
		}
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		BitTrace::Clear()
	{
		Check_Object(this);

		if (--traceUp == 0)
		{
			TraceManager::Instance->activeBits &= ~bitFlag;

			#if defined(USE_TIME_ANALYSIS) || defined(USE_TRACE_LOG)
				Time now = gos_GetHiResTime();
			#endif

			#if defined(USE_TIME_ANALYSIS)
				lastUpTime = now - lastActivity;
#if 0 // HACK	
				Verify(lastUpTime >= 0.0f)
				totalUpTime += lastUpTime;
#else
				if (lastUpTime >= 0.0f)
				{
					totalUpTime += lastUpTime;
				}
#endif
			#endif

			#if defined(USE_TRACE_LOG)
				//Check_Object(traceManager);
				IncrementSampleCount();
				MemoryStream *log = GetTraceLog();
				if (log)
				{
					Check_Object(log);
					TraceSample *sample =
						Cast_Pointer(TraceSample*, log->GetPointer());
					sample->sampleLength = sizeof(*sample);
					sample->sampleType = TraceSample::GoingDown;
					sample->traceNumber = traceNumber;
					sample->sampleTime = now;
					log->AdvancePointer(sample->sampleLength);
				}
			#endif

			#if defined(USE_ACTIVE_PROFILE)
				ClearLineImplementation(activeLine);
			#endif
		}
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		BitTrace::TestInstance()
	{
		Verify(traceUp >= 0);
	}

	//##########################################################################
	//########################    TraceManager    ##############################
	//##########################################################################

	TraceManager
		*TraceManager::Instance = NULL;

	void
		TraceManager::InitializeClass()
	{
		Verify(!TraceManager::Instance);
		TraceManager::Instance = new TraceManager;
		Register_Object(TraceManager::Instance);
	}

	void
		TraceManager::TerminateClass()
	{
		Unregister_Object(TraceManager::Instance);
		delete TraceManager::Instance;
		TraceManager::Instance = NULL;
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	TraceManager::TraceManager():
		traceChain(NULL)
	{
		sampleStart = 0.0;
		actualSampleCount = 0;
		ignoredSampleCount = 0;
		traceCount = 0;
		activeTraceLog = NULL;
		allocatedTraceLog = NULL;
		activeBits = 0;
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	TraceManager::~TraceManager()
	{
		Check_Object(this);

		if (allocatedTraceLog)
		{
			Check_Object(allocatedTraceLog);
			allocatedTraceLog->Rewind();
			TraceSample *samples =
				Cast_Pointer(TraceSample*, allocatedTraceLog->GetPointer());
			Unregister_Object(allocatedTraceLog);
			delete allocatedTraceLog;
			activeTraceLog = allocatedTraceLog = NULL;

			Unregister_Pointer(samples);
			delete[] samples;
		}
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		TraceManager::Add(Trace *trace)
	{
		Check_Object(this);

		traceCount = (BYTE)(traceCount + 1);
		traceChain.Add(trace);
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//
	void
		TraceManager::DumpTracesStatus()
	{
		ChainIteratorOf<Trace*> traces(&traceChain);
		Trace *trace;

		while ((trace = traces.ReadAndNext()) != NULL)
		{
			Check_Object(trace);
			SPEW((GROUP_STUFF_TRACE, "%s: +", trace->traceName));
			trace->DumpTraceStatus();
			SPEW((GROUP_STUFF_TRACE, ""));
		}
		SPEW((GROUP_STUFF_TRACE, ""));
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -