关键词搜索

源码搜索 ×
×

tars源码分析之11

发布2022-07-10浏览588次

详情内容

time provider函数,来看一下,用了单例模式:

  1. #include "util/tc_timeprovider.h"
  2. namespace tars
  3. {
  4. TC_ThreadLock TC_TimeProvider::g_tl;
  5. TC_TimeProviderPtr TC_TimeProvider::g_tp = NULL;
  6. TC_TimeProvider* TC_TimeProvider::getInstance()
  7. {
  8. if(!g_tp)
  9. {
  10. TC_ThreadLock::Lock lock(g_tl);
  11. if(!g_tp)
  12. {
  13. g_tp = new TC_TimeProvider();
  14. g_tp->start();
  15. }
  16. }
  17. return g_tp.get();
  18. }
  19. TC_TimeProvider::~TC_TimeProvider()
  20. {
  21. {
  22. TC_ThreadLock::Lock lock(g_tl);
  23. _terminate = true;
  24. g_tl.notify();
  25. }
  26. getThreadControl().join();
  27. }
  28. void TC_TimeProvider::getNow(timeval *tv)
  29. {
  30. int idx = _buf_idx;
  31. *tv = _t[idx];
  32. if(_cpu_cycle != 0 && _use_tsc) //cpu-cycle在两个interval周期后采集完成
  33. {
  34. addTimeOffset(*tv,idx);
  35. }
  36. else
  37. {
  38. ::gettimeofday(tv, NULL);
  39. }
  40. }
  41. int64_t TC_TimeProvider::getNowMs()
  42. {
  43. struct timeval tv;
  44. getNow(&tv);
  45. return tv.tv_sec * (int64_t)1000 + tv.tv_usec/1000;
  46. }
  47. void TC_TimeProvider::run()
  48. {
  49. while(!_terminate)
  50. {
  51. timeval& tt = _t[!_buf_idx];
  52. ::gettimeofday(&tt, NULL);
  53. setTsc(tt);
  54. _buf_idx = !_buf_idx;
  55. TC_ThreadLock::Lock lock(g_tl);
  56. g_tl.timedWait(800); //修改800时 需对应修改addTimeOffset中offset判读值
  57. }
  58. }
  59. float TC_TimeProvider::cpuMHz()
  60. {
  61. if(_cpu_cycle != 0)
  62. return 1.0/_cpu_cycle;
  63. return 0;
  64. }
  65. void TC_TimeProvider::setTsc(timeval& tt)
  66. {
  67. uint32_t low = 0;
  68. uint32_t high = 0;
  69. rdtsc(low,high);
  70. uint64_t current_tsc = ((uint64_t)high << 32) | low;
  71. uint64_t& last_tsc = _tsc[!_buf_idx];
  72. timeval& last_tt = _t[_buf_idx];
  73. if(_tsc[_buf_idx] == 0 || _tsc[!_buf_idx] == 0 )
  74. {
  75. _cpu_cycle = 0;
  76. last_tsc = current_tsc;
  77. }
  78. else
  79. {
  80. time_t sptime = (tt.tv_sec - last_tt.tv_sec)*1000*1000 + (tt.tv_usec - last_tt.tv_usec);
  81. _cpu_cycle = (float)sptime/(current_tsc - _tsc[_buf_idx]); //us
  82. last_tsc = current_tsc;
  83. }
  84. }
  85. void TC_TimeProvider::addTimeOffset(timeval& tt,const int &idx)
  86. {
  87. uint32_t low = 0;
  88. uint32_t high = 0;
  89. rdtsc(low,high);
  90. uint64_t current_tsc = ((uint64_t)high << 32) | low;
  91. int64_t t = (int64_t)(current_tsc - _tsc[idx]);
  92. time_t offset = (time_t)(t*_cpu_cycle);
  93. if(t < -1000 || offset > 1000000)//毫秒级别
  94. {
  95. //cerr<< "TC_TimeProvider add_time_offset error,correct it by use gettimeofday. current_tsc|"<<current_tsc<<"|last_tsc|"<<_tsc[idx]<<endl;
  96. _use_tsc = false;
  97. ::gettimeofday(&tt, NULL);
  98. return;
  99. }
  100. tt.tv_usec += offset;
  101. while (tt.tv_usec >= 1000000) { tt.tv_usec -= 1000000; tt.tv_sec++;}
  102. }
  103. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载