trendline.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "../estimator/trendline.h"
  2. #include "../cc/razor_log.h"
  3. /*计算曲线斜率来确定过载问题*/
  4. static double linear_fit_slope(delay_hist_t* que, int que_size)
  5. {
  6. int i;
  7. double sum_x, sum_y, avg_x, avg_y, numerator, denominator;
  8. sum_x = sum_y = avg_x = avg_y = 0;
  9. numerator = 0;
  10. denominator = 0;
  11. for (i = 0; i < que_size; ++i){
  12. sum_x += que[i].arrival_delta;
  13. sum_y += que[i].smoothed_delay;
  14. }
  15. avg_x = sum_x / que_size;
  16. avg_y = sum_y / que_size;
  17. for (i = 0; i < que_size; ++i){
  18. numerator += (que[i].arrival_delta - avg_x) * (que[i].smoothed_delay - avg_y);
  19. denominator += (que[i].arrival_delta - avg_x) * (que[i].arrival_delta - avg_x);
  20. }
  21. razor_debug("linear_fit_slope sum_x:%lf sum_y:%lf avg_x:%lf avg_y:%lf numerator:%lf denominator:%lf \n", sum_x, sum_y, avg_x, avg_y, numerator, denominator);
  22. if (denominator != 0)
  23. return numerator / denominator;
  24. else
  25. return 0;
  26. }
  27. #define TRENDLINE_MAX_COUNT 1000
  28. trendline_estimator_t* trendline_create(size_t wnd_size, double smoothing_coef, double threshold_gain)
  29. {
  30. trendline_estimator_t* est = (trendline_estimator_t*)calloc(1, sizeof(trendline_estimator_t));
  31. est->window_size = wnd_size;
  32. est->smoothing_coef = smoothing_coef;
  33. est->threshold_gain = threshold_gain;
  34. est->first_arrival_ts = -1;
  35. est->que = malloc(sizeof(delay_hist_t) * wnd_size);
  36. razor_info("trendline_create. \n");
  37. return est;
  38. }
  39. void trendline_destroy(trendline_estimator_t* est)
  40. {
  41. if (est != NULL){
  42. free(est->que);
  43. free((void *)est);
  44. }
  45. razor_info("trendline_destroy. \n");
  46. }
  47. void trendline_update(trendline_estimator_t* est, double recv_delta_ms, double send_delta_ms, int64_t arrival_ts)
  48. {
  49. double delta_ms = recv_delta_ms - send_delta_ms;
  50. delay_hist_t* hist;
  51. est->num_of_deltas++;
  52. if (est->num_of_deltas > TRENDLINE_MAX_COUNT)
  53. est->num_of_deltas = TRENDLINE_MAX_COUNT;
  54. if (est->first_arrival_ts == -1)
  55. est->first_arrival_ts = arrival_ts;
  56. est->acc_delay += delta_ms;
  57. est->smoothed_delay = est->smoothing_coef * est->smoothed_delay + (1 - est->smoothing_coef) * est->acc_delay;
  58. hist = &est->que[est->index % est->window_size];
  59. est->index++;
  60. hist->arrival_delta = (double)(arrival_ts - est->first_arrival_ts);
  61. hist->smoothed_delay = est->smoothed_delay;
  62. if (est->index >= est->window_size){
  63. est->trendline = linear_fit_slope(est->que, est->window_size);
  64. razor_debug("trendline = %f \n", est->trendline);
  65. }
  66. }
  67. double trendline_slope(trendline_estimator_t* est)
  68. {
  69. return est->threshold_gain * est->trendline;
  70. }