fork download
  1. #include <iostream> /* C++ iostream C++98/11 */
  2. #include <string>
  3. #include <vector>
  4.  
  5. #define SAMPLE_TIME_MIN_N_SEC 128
  6.  
  7. /* FADIII START */
  8.  
  9. #define HIST_NUM_BINS 10
  10. #define IS_CONDOR_ONLY 0
  11.  
  12. inline struct timespec getTimespecNow (void) {
  13. struct timespec now;
  14. return now;
  15. }
  16.  
  17. /* FADIII END */
  18.  
  19. struct Histogram {
  20. u_int32_t sample_time;
  21. u_int8_t mode;
  22. struct timespec last_update;
  23. u_int8_t last_bin_updated;
  24. std::vector<size_t> histogram_bins;
  25. size_t hist_type;
  26. virtual ~Histogram() {}
  27. Histogram() : sample_time(0), mode(0), last_bin_updated(0), histogram_bins(HIST_NUM_BINS, 0), hist_type(4096) {
  28. last_update = getTimespecNow();
  29. }
  30. virtual void clear() {
  31. for (size_t i = 0; i < HIST_NUM_BINS; i++) {
  32. histogram_bins[i] = 0;
  33. }
  34. }
  35. virtual void updateForTimeInterval() {}
  36. virtual void updateCurrentBin(size_t value) = 0;
  37. protected:
  38. // Common function for both SB and Port Counter histograms, that will be called from each instance according to its own prm arguments
  39. size_t getCurrentBin(size_t value, size_t bin_size, size_t first_bin_threshold) {
  40. size_t upper_bound = first_bin_threshold, lower_bound = 0;
  41. int index = 0;
  42. while ((index < (HIST_NUM_BINS - 1)) && (value > lower_bound)) {
  43. if (value < upper_bound) {
  44. break;
  45. }
  46. ++index;
  47. lower_bound = upper_bound;
  48. if (mode) {
  49. upper_bound = first_bin_threshold + ((2 << (index - 1)) * bin_size);
  50. }
  51. else {
  52. upper_bound = first_bin_threshold + (index * bin_size);
  53. }
  54. }
  55. return index;
  56. }
  57. };
  58.  
  59. struct SharedBufferHistogram : public Histogram {
  60. bool enabled;
  61. bool is_bind;
  62. size_t local_port;
  63. size_t hist_parameter;
  64. size_t hist_min_value;
  65. size_t hist_max_value;
  66. SharedBufferHistogram() : Histogram(), enabled(false), is_bind(true), local_port(0), hist_parameter(0), hist_min_value(0), hist_max_value(0) {}
  67. virtual ~SharedBufferHistogram() {}
  68.  
  69. virtual void updateForTimeInterval(size_t global_sample_time) {
  70. double diff = 0;//CalculateTimeDiff(&last_update); //FADIII
  71. u_int64_t num_of_intervals = diff / ((2 << (IS_CONDOR_ONLY ? global_sample_time : sample_time)) * SAMPLE_TIME_MIN_N_SEC);
  72. last_update = getTimespecNow();
  73. histogram_bins[last_bin_updated] += num_of_intervals;
  74. }
  75.  
  76. virtual void updateCurrentBin(size_t value) {
  77. size_t index = Histogram::getCurrentBin(value, (hist_max_value - hist_min_value) / (mode ? 255 : 8), hist_min_value);
  78. histogram_bins[index]++;
  79. last_bin_updated = index;
  80. }
  81. };
  82.  
  83. struct PortCounterHistogram : public Histogram {
  84. enum PortHistogramBindGroup {
  85. CRC_Errors,
  86. Packet_Received_Size,
  87. Buffer_Overflow_Packet_PG,
  88. Packet_Received_PG,
  89. Bytes_Received_Ok,
  90. Packet_Transmit_Ok_TC,
  91. PAUSE_Transmit_Prio,
  92. ARN_Transmit,
  93. Packet_Transmit_ECN_TC,
  94. Receive_PAUSE_Duration_Prio,
  95. Transmit_Cong_TC
  96. };
  97. static constexpr size_t CounterHistogramInfinite = 0xffffff;
  98. size_t first_bin_threshold;
  99. size_t bin_size;
  100. int hist_repeat_num;
  101. size_t current_counter_value;
  102. size_t min_watermark;
  103. size_t max_watermark;
  104. PortCounterHistogram() : Histogram(), first_bin_threshold(0), bin_size(0), hist_repeat_num(0), current_counter_value(0), min_watermark(0xffffffffffffffff), max_watermark(0) {}
  105. virtual ~PortCounterHistogram() {}
  106. virtual void updateCurrentBin(size_t value) {
  107. if (hist_repeat_num <= 0) {
  108. return;
  109. }
  110. size_t diff = 0; // FADIII: CalculateTimeDiff(&last_update);
  111. if (diff < sample_time) {
  112. current_counter_value += value;
  113. }
  114. else {
  115. std::cout << "Selected bin=" << getCurrentBin(current_counter_value, bin_size, first_bin_threshold) << std::endl;
  116. histogram_bins[getCurrentBin(current_counter_value, bin_size, first_bin_threshold)]++;
  117. if (hist_repeat_num != CounterHistogramInfinite) {
  118. hist_repeat_num -= (diff / sample_time);
  119. }
  120. diff -= sample_time;
  121. if (diff > sample_time) {
  122. histogram_bins[0] += diff / sample_time;
  123. }
  124. current_counter_value = value;
  125. last_update = getTimespecNow();
  126. min_watermark = std::min(value, min_watermark);
  127. max_watermark = std::max(value, max_watermark);
  128. }
  129. }
  130. static size_t packet_length_to_hist_type(size_t length) {
  131. length >>= 6;
  132. size_t reps = 0;
  133. while (length) {
  134. length >>= 1;
  135. reps++;
  136. }
  137. return reps;
  138. }
  139. virtual void updateForTimeInterval() {
  140. updateCurrentBin(current_counter_value);
  141. current_counter_value = 0;
  142. double diff = 0; //FADIII: CalculateTimeDiff(&last_update);
  143. u_int64_t num_of_intervals = 1;
  144. if (sample_time != 0) {
  145. num_of_intervals = diff / (sample_time * SAMPLE_TIME_MIN_N_SEC);
  146. }
  147. histogram_bins[0] += num_of_intervals;
  148. last_update = getTimespecNow();
  149. }
  150. virtual void clear() {
  151. Histogram::clear();
  152. first_bin_threshold = 0;
  153. bin_size = 0;
  154. hist_repeat_num = 0;
  155. current_counter_value = 0;
  156. min_watermark = 0xffffffffffffffff;
  157. max_watermark = 0;
  158. }
  159. };
  160.  
  161. int main() {
  162. std::cout << "fadi" << std::endl;
  163. }
  164.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
fadi