📄 trace.cpp
字号:
#include "stdafx.h"#include "trace.h"#include "cust_debug.h" // debug environment#include <stdio.h>#include <memory.h>DWORD dummyTimer;volatile DWORD* pGpsTimer = &dummyTimer;//------------------------------------------------------------------------------// convert ticks (1/3.25 microcseconds per) to microseconds.//------------------------------------------------------------------------------int tickToMicrosecond(DWORD deltaTime){ double dTime = static_cast<double> (deltaTime); return static_cast<int> ((dTime / 3.25) + 0.5);}#ifndef _ENABLE_TRACE_ // {void traceDump(bool closeTrace) { }void traceInit () { } void traceResume() { }void traceSave(unsigned short line, char cmd, DWORD val) { }void traceParamSet(const PBYTE pBufIn, DWORD dwLenIn) { }void traceStats(void) { }void traceIsr(int isr, DWORD time) { }DWORD traceCopy(PBYTE pBufOut, DWORD dwLenOut, int maj, int min){ return 0;}#else // } { ENABLE_TRACE#define BIT(b) (1 << (b))#define GL_SPI_WRITE_DATA ( BIT(6))#define GL_SPI_READ_DATA (BIT(7) + BIT(6))#define GL_SPI_WRITE_CONTROL ( 0 )#define GL_SPI_READ_STATUS (BIT(7) ) // Never used//------------------------------------------------------------------------------//// Trace Structure and storage////------------------------------------------------------------------------------struct TraceStruct{ DWORD time; unsigned short line; unsigned char isr; unsigned char cmd; DWORD val; inline bool operator== (const TraceStruct& rhs) { // NOTE: do not compare time. if (line != rhs.line) return false; if (cmd != rhs.cmd ) return false; if (val != rhs.val ) return false; return true; }};enum{ MAX_TRACES = 1500};#ifdef _TRACE_BODY_ // {static TraceStruct traceArray[MAX_TRACES];#endifstatic TraceStruct* traceEnd = 0;static TraceStruct* traceCur = 0;static TraceStruct* tracePrev = 0;static DWORD traceCounter = 0;static DWORD traceResumeCounter = 0;static unsigned short traceRepeat = 0;static bool tracePause;static unsigned short traceSwLineMin = ~0;static DWORD totalReads = 0;static DWORD totalReadBytes = 0;static DWORD totalReadTime = 0;static DWORD totalWrites = 0;static DWORD totalWriteBytes = 0;static DWORD totalWriteTime = 0;static DWORD totalInterrupts = 0;static DWORD totalIntAck = 0;static DWORD totalIntAckReal = 0;static DWORD traceIsrTime;const TCHAR* glFormatTime(void);void traceStats(DWORD ticks){ DWORD readMicroseconds = tickToMicrosecond(totalReadTime); DWORD writeMicroseconds = tickToMicrosecond(totalWriteTime); DWORD time = readMicroseconds + writeMicroseconds; GL_ERROR((TEXT("spi_read() called %d times. %d bytes %d uSec\n\r"), totalReads, totalReadBytes, readMicroseconds)); GL_ERROR((TEXT("spi_write() called %d times. %d bytes %d uSec\n\r"), totalWrites, totalWriteBytes, writeMicroseconds)); GL_ERROR((TEXT("spi_interrupts %d int_ack %d/%d\n\r"), totalInterrupts, totalIntAckReal, totalIntAck)); DWORD xfer = totalReadBytes + totalWriteBytes; double xfer_rate = static_cast<double> (xfer) / static_cast<double> (time); xfer_rate *= 1000000; xfer_rate += 0.5; DWORD i_xfer_rate = static_cast<DWORD> (xfer_rate); GL_ERROR((TEXT("spi transfer rate = %d/%d = %d bytes/sec\n\r"), xfer, time, i_xfer_rate)); xfer_rate -= 0.5; xfer_rate /= 1024.0; xfer_rate *= 10.0; xfer_rate += 0.5; i_xfer_rate = static_cast<DWORD> (xfer_rate); GL_ERROR((TEXT("spi transfer rate = %d.%1d KB/sec\n\r"), i_xfer_rate / 10, i_xfer_rate % 10)); DWORD open = tickToMicrosecond(ticks); double d_cpu_percent_tenths = 0.5 + (time * 1000.0)/open; int i_cpu_percent_tenths = static_cast<int> (d_cpu_percent_tenths); GL_ERROR((TEXT("spi driver open for %d us. %d.%1d%% of CPU time.\n\r"), open, (int) i_cpu_percent_tenths / 10, (int) i_cpu_percent_tenths % 10)); totalReads = 0; totalReadBytes = 0; totalReadTime = 0; totalWrites = 0; totalWriteBytes = 0; totalWriteTime = 0; totalInterrupts = 0; totalIntAck = 0; totalIntAckReal = 0;}//------------------------------------------------------------------------------//// traceToString////------------------------------------------------------------------------------static const char* traceToString(int type){ switch(type) { case TR_SPI_SR: return "spi_sr << "; case TR_SPI_CMD: return "spi_cmd >> "; case TR_SW: return "sw "; case TR_WR_CMD: return "cmd -->"; case TR_RD_STATUS: return "status <-- "; case TR_WR_DATA: return "DATA ==>"; case TR_RD_DATA: return "DATA <== "; case TR_TIME: return "millisecond"; case TR_STATS_READ: return "read() < "; case TR_STATS_WRITE: return "write() > "; case TR_INT_ACK: return "int ack x "; case TR_ISR: return "isr ! "; case TR_REPEAT: return " << repeat >> "; default: break; } return "???????????";}//------------------------------------------------------------------------------//// traceNext()////------------------------------------------------------------------------------#ifdef _TRACE_BODY_ // {inline TraceStruct* traceNext(TraceStruct* p){ if (++p >= traceEnd) return traceArray; return p;}#endif // } _TRACE_BODY_ //------------------------------------------------------------------------------//// traceDump()////------------------------------------------------------------------------------extern const char* pDriverLabel;void traceDump(bool closeTrace){#ifdef _TRACE_BODY_ // { if (tracePause) return; if (!traceCur) { printf("\n\rTracing disabled\n\r"); return; } printf("\n\r%s\n\r", pDriverLabel); printf("\n\rusec Ticks) Line Direction Data\n"); DWORD trNum = traceCounter - MAX_TRACES; TraceStruct* p = traceNext(traceCur); if (traceCounter <= MAX_TRACES) { trNum = 1; // Special case - not-filled traceArray[] p = traceArray + 1; // skip the first one (due to the pre- // increment in the traceSave(). } DWORD baseTime; DWORD prevTime; bool setFirst = true; while (p != traceCur) { while (trNum >= traceResumeCounter) { if (setFirst) { setFirst = false; baseTime = p->time; prevTime = p->time; } // convert ticks (1/3.25 microcseconds per) to microseconds. int iTime = tickToMicrosecond(p->time - prevTime); if (p->cmd == TR_REPEAT) { printf(" repeat %lu (%d usec)", p->line, iTime); break; } if (p->cmd == TR_SW) { if (p->line <= traceSwLineMin) { printf("\n"); traceSwLineMin = p->line; } } prevTime = p->time; printf("\n%8d %6lu) %4u %s ", iTime, p->time-baseTime, p->line, traceToString(p->cmd)); switch (p->cmd) { case TR_STATS_READ: case TR_STATS_WRITE: case TR_INT_ACK: printf("%8lu", tickToMicrosecond(p->val)); break; case TR_TIME: case TR_ISR: printf("%8lu ms ticks", p->val); break; default: printf("%8lx", p->val); break; case TR_WR_CMD: printf("%8lx", p->val); const char* msg = " <bad GPS cmd>"; switch (p->val & 0xFF) { case GL_SPI_WRITE_DATA: msg = " WRITE DATA"; break; case GL_SPI_READ_DATA: msg = " READ DATA"; break; case GL_SPI_WRITE_CONTROL: msg = " WRITE CONTROL"; break; case GL_SPI_READ_STATUS: msg = " READ STATUS"; break; default: break; } printf(msg); } break; } if (trNum >= traceResumeCounter) { if (p->isr & 1) printf(" ISR"); if (p->isr & 2) printf(" badISR"); if (p->isr & 4) printf(" ISR Done"); if (p->isr & 8) printf(" ISR Occur"); } ++trNum; p = traceNext(p); } printf("\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -