overuse_detector.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "../estimator/overuse_detector.h"
  2. #include "../common/cf_platform.h"
  3. #include "../cc/razor_log.h"
  4. #include <math.h>
  5. #include <stdlib.h>
  6. static double kMaxAdaptOffsetMs = 15.0;
  7. static double kOverUsingTimeThreshold = 20;
  8. static int kMinNumDeltas = 60;
  9. static int kMaxTimeDeltaMs = 100;
  10. overuse_detector_t* overuse_create()
  11. {
  12. overuse_detector_t* detector = (overuse_detector_t*)calloc(1, sizeof(overuse_detector_t));
  13. detector->k_up = 0.0187;
  14. detector->k_down = 0.039;
  15. detector->ouveusing_time_threshold = kOverUsingTimeThreshold;
  16. detector->threshold = 12.5;
  17. detector->update_ts = -1;
  18. detector->time_over_using = -1;
  19. detector->state = kBwNormal;
  20. razor_info("overuse_create. \n");
  21. return detector;
  22. }
  23. void overuse_destroy(overuse_detector_t* detector)
  24. {
  25. if (detector != NULL)
  26. free((void *)detector);
  27. razor_info("overuse_destroy. \n");
  28. }
  29. /*更新过载的阈值*/
  30. static void overuse_update_threshold(overuse_detector_t* detector, double modified_offset, int64_t cur_ts)
  31. {
  32. double k;
  33. int64_t time_delta;
  34. razor_debug("start overuse_update_threshold. modified_offset: %lf cur_ts: %I64d threshold: %lf. \n",
  35. modified_offset, cur_ts, detector->threshold);
  36. if (detector->update_ts == -1)
  37. detector->update_ts = cur_ts;
  38. if (fabs(modified_offset) > detector->threshold + kMaxAdaptOffsetMs){
  39. detector->update_ts = cur_ts;
  40. return;
  41. }
  42. k = fabs(modified_offset) < detector->threshold ? detector->k_down : detector->k_up;
  43. time_delta = SU_MIN(cur_ts - detector->update_ts, kMaxTimeDeltaMs);
  44. detector->threshold += k * (fabs(modified_offset) - detector->threshold) * time_delta;
  45. detector->threshold = SU_MAX(6.0f, SU_MIN(600.0f, detector->threshold));
  46. detector->update_ts = cur_ts;
  47. razor_debug("end overuse_update_threshold. threshold: %lf. \n", detector->threshold);
  48. }
  49. /*过载检测*/
  50. int overuse_detect(overuse_detector_t* detector, double offset, double ts_delta, int num_of_deltas, int64_t cur_ts)
  51. {
  52. double T;
  53. razor_debug("start overuse_detect. offset: %lf ts_delta: %lf num_of_deltas: %d cur_ts: %I64d. \n",
  54. offset, ts_delta, num_of_deltas, cur_ts);
  55. if (num_of_deltas < 2)
  56. return kBwNormal;
  57. T = (double)(SU_MIN(num_of_deltas, kMinNumDeltas) * offset);
  58. if (T > detector->threshold){ /*计算累计的overusing值*/
  59. if (detector->time_over_using == -1)
  60. detector->time_over_using = ts_delta / 2;
  61. else
  62. detector->time_over_using += ts_delta;
  63. detector->overuse_counter++;
  64. if (detector->time_over_using > detector->ouveusing_time_threshold && detector->overuse_counter > 1){
  65. if (offset >= detector->prev_offset){ /*连续两次以上传输延迟增量增大,表示网络已经过载了,需要进行带宽减小*/
  66. detector->time_over_using = 0;
  67. detector->overuse_counter = 0;
  68. detector->state = kBwOverusing;
  69. }
  70. }
  71. }
  72. else if (T < -detector->threshold){ /*网络延迟增量逐步缩小,需要加大带宽码率*/
  73. detector->time_over_using = -1;
  74. detector->overuse_counter = 0;
  75. detector->state = kBwUnderusing;
  76. }
  77. else{
  78. detector->time_over_using = -1;
  79. detector->overuse_counter = 0;
  80. detector->state = kBwNormal;
  81. }
  82. detector->prev_offset = offset;
  83. overuse_update_threshold(detector, T, cur_ts);
  84. razor_debug("end overuse_detect. state: %d threshold: %lf. \n", detector->state, detector->threshold);
  85. return detector->state;
  86. }