Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.8 KiB

  1. #include "inspch.h"
  2. #include "timetrak.h"
  3. #define NUMBYTESFORUPDATE 200000
  4. #define DECAY_FACTOR 85
  5. CTimeTracker::CTimeTracker(DWORD dwDefaultRate)
  6. {
  7. m_hasave = FALSE;
  8. m_defaultrate = dwDefaultRate;
  9. m_bTiming = FALSE;
  10. m_dwBytesSoFar = 0;
  11. m_dwTicksSoFar = 0;
  12. }
  13. CTimeTracker::~CTimeTracker()
  14. {
  15. }
  16. void CTimeTracker::AddEntry(DWORD dwTime, DWORD dwBytes, BOOL bAccumulate)
  17. {
  18. DWORD dwBPS;
  19. m_dwBytesSoFar += dwBytes;
  20. m_dwTicksSoFar += dwTime;
  21. if(m_dwBytesSoFar > NUMBYTESFORUPDATE || !bAccumulate)
  22. {
  23. if(m_dwBytesSoFar < 4000000)
  24. dwBPS = (m_dwBytesSoFar * 1000) / m_dwTicksSoFar;
  25. else if (m_dwTicksSoFar > 20000)
  26. dwBPS = m_dwBytesSoFar / (m_dwTicksSoFar / 1000);
  27. else
  28. dwBPS = (m_dwBytesSoFar/m_dwTicksSoFar)*1000;
  29. if(!m_hasave)
  30. {
  31. m_ave = dwBPS;
  32. m_hasave = TRUE;
  33. }
  34. else
  35. {
  36. m_ave = ((dwBPS * (100 - DECAY_FACTOR)) + (m_ave * DECAY_FACTOR))/100;
  37. }
  38. m_dwBytesSoFar = 0;
  39. m_dwTicksSoFar = 0;
  40. }
  41. }
  42. DWORD CTimeTracker::GetBytesPerSecond()
  43. {
  44. if(!m_hasave)
  45. return m_defaultrate;
  46. else
  47. return m_ave;
  48. }
  49. void CTimeTracker::StartClock()
  50. {
  51. m_dwStartTick = GetTickCount();
  52. m_bTiming = TRUE;
  53. }
  54. void CTimeTracker::StopClock()
  55. {
  56. m_bTiming = FALSE;
  57. }
  58. void CTimeTracker::SetBytes(DWORD dwBytes, BOOL bAccumulate)
  59. {
  60. DWORD dwTickChange;
  61. DWORD dwCurrentTick;
  62. if(!m_bTiming)
  63. return;
  64. dwCurrentTick = GetTickCount();
  65. dwTickChange = dwCurrentTick - m_dwStartTick;
  66. if(m_dwStartTick > dwCurrentTick)
  67. dwTickChange -= 0xffffffff;
  68. if(dwTickChange != 0)
  69. AddEntry(dwTickChange, dwBytes, bAccumulate);
  70. m_dwStartTick = dwCurrentTick;
  71. }