concurrent_calculator.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef JTJAI_MEDIA_CONCURRENT_CALCULATOR_H
  2. #define JTJAI_MEDIA_CONCURRENT_CALCULATOR_H
  3. #include "config.h"
  4. #include <vector>
  5. #include <unordered_map>
  6. namespace jtjai_media {
  7. struct StreamScheduleInfo {
  8. int stream_index; // 流索引
  9. int start_time; // 开始时间(相对于轮询开始的秒数)
  10. int duration; // 持续时间(秒)
  11. int end_time; // 结束时间
  12. double priority_score; // 优先级分数(基于权重计算)
  13. StreamScheduleInfo(int idx, int start, int dur, double score)
  14. : stream_index(idx), start_time(start), duration(dur), end_time(start + dur), priority_score(score) {}
  15. };
  16. class ConcurrentCalculator {
  17. public:
  18. ConcurrentCalculator() = default;
  19. // 计算最优的并发调度方案
  20. std::vector<StreamScheduleInfo> calculate_schedule(
  21. const std::vector<StreamConfig>& streams,
  22. int total_duration,
  23. int max_concurrent
  24. );
  25. // 获取任意时刻的并发流数量
  26. int get_concurrent_count_at_time(
  27. const std::vector<StreamScheduleInfo>& schedule,
  28. int time_point
  29. ) const;
  30. // 验证调度方案是否满足约束条件
  31. bool validate_schedule(
  32. const std::vector<StreamScheduleInfo>& schedule,
  33. int total_duration,
  34. int max_concurrent
  35. ) const;
  36. // 获取调度统计信息
  37. struct ScheduleStats {
  38. int total_streams;
  39. int max_concurrent_used;
  40. double average_concurrent;
  41. int total_scheduled_duration;
  42. double utilization_rate;
  43. };
  44. ScheduleStats get_schedule_stats(
  45. const std::vector<StreamScheduleInfo>& schedule,
  46. int total_duration
  47. ) const;
  48. // 输出调度方案的可视化信息
  49. std::string format_schedule(
  50. const std::vector<StreamScheduleInfo>& schedule,
  51. const std::vector<StreamConfig>& streams,
  52. int total_duration
  53. ) const;
  54. private:
  55. // 计算流的优先级分数
  56. double calculate_priority_score(const StreamConfig& stream) const;
  57. // 贪心算法调度
  58. std::vector<StreamScheduleInfo> greedy_schedule(
  59. const std::vector<StreamConfig>& streams,
  60. int total_duration,
  61. int max_concurrent
  62. );
  63. // 寻找最早可用的时间段
  64. int find_earliest_slot(
  65. const std::vector<StreamScheduleInfo>& current_schedule,
  66. int duration,
  67. int max_concurrent,
  68. int total_duration
  69. ) const;
  70. // 检查指定时间段是否可以调度新流
  71. bool can_schedule_at_time(
  72. const std::vector<StreamScheduleInfo>& current_schedule,
  73. int start_time,
  74. int duration,
  75. int max_concurrent
  76. ) const;
  77. // 优化调度方案(尝试减少碎片化)
  78. std::vector<StreamScheduleInfo> optimize_schedule(
  79. std::vector<StreamScheduleInfo> schedule,
  80. int total_duration,
  81. int max_concurrent
  82. );
  83. };
  84. } // namespace jtjai_media
  85. #endif // JTJAI_MEDIA_CONCURRENT_CALCULATOR_H