int main(int argc, char **argv) { int four[4]; int five[5]; uint32_t bitset = 0; grpc_test_init(argc, argv); GPR_ASSERT(GPR_MIN(1, 2) == 1); GPR_ASSERT(GPR_MAX(1, 2) == 2); GPR_ASSERT(GPR_MIN(2, 1) == 1); GPR_ASSERT(GPR_MAX(2, 1) == 2); GPR_ASSERT(GPR_CLAMP(1, 0, 2) == 1); GPR_ASSERT(GPR_CLAMP(0, 0, 2) == 0); GPR_ASSERT(GPR_CLAMP(2, 0, 2) == 2); GPR_ASSERT(GPR_CLAMP(-1, 0, 2) == 0); GPR_ASSERT(GPR_CLAMP(3, 0, 2) == 2); GPR_ASSERT(GPR_ROTL((uint32_t)0x80000001, 1) == 3); GPR_ASSERT(GPR_ROTR((uint32_t)0x80000001, 1) == 0xc0000000); GPR_ASSERT(GPR_ARRAY_SIZE(four) == 4); GPR_ASSERT(GPR_ARRAY_SIZE(five) == 5); GPR_ASSERT(GPR_BITCOUNT((1u << 31) - 1) == 31); GPR_ASSERT(GPR_BITCOUNT(1u << 3) == 1); GPR_ASSERT(GPR_BITCOUNT(0) == 0); GPR_ASSERT(GPR_BITSET(&bitset, 3) == 8); GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); GPR_ASSERT(GPR_BITGET(bitset, 3) == 1); GPR_ASSERT(GPR_BITSET(&bitset, 1) == 10); GPR_ASSERT(GPR_BITCOUNT(bitset) == 2); GPR_ASSERT(GPR_BITCLEAR(&bitset, 3) == 2); GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); GPR_ASSERT(GPR_BITGET(bitset, 3) == 0); return 0; }
gpr_timespec gpr_backoff_step(gpr_backoff *backoff, gpr_timespec now) { double new_timeout_millis = backoff->multiplier * (double)backoff->current_timeout_millis; double jitter_range = backoff->jitter * new_timeout_millis; double jitter = (2 * generate_uniform_random_number(&backoff->rng_state) - 1) * jitter_range; backoff->current_timeout_millis = GPR_CLAMP((int64_t)(new_timeout_millis + jitter), backoff->min_timeout_millis, backoff->max_timeout_millis); return gpr_time_add( now, gpr_time_from_millis(backoff->current_timeout_millis, GPR_TIMESPAN)); }
static void push_setting(grpc_chttp2_transport *t, grpc_chttp2_setting_id id, gpr_uint32 value) { const grpc_chttp2_setting_parameters *sp = &grpc_chttp2_settings_parameters[id]; gpr_uint32 use_value = GPR_CLAMP(value, sp->min_value, sp->max_value); if (use_value != value) { gpr_log(GPR_INFO, "Requested parameter %s clamped from %d to %d", sp->name, value, use_value); } if (use_value != t->global.settings[GRPC_LOCAL_SETTINGS][id]) { t->global.settings[GRPC_LOCAL_SETTINGS][id] = use_value; t->global.dirtied_local_settings = 1; } }
static double threshold_for_count_below(gpr_histogram *h, double count_below) { double count_so_far; double lower_bound; double upper_bound; size_t lower_idx; size_t upper_idx; if (h->count == 0) { return 0.0; } if (count_below <= 0) { return h->min_seen; } if (count_below >= h->count) { return h->max_seen; } /* find the lowest bucket that gets us above count_below */ count_so_far = 0.0; for (lower_idx = 0; lower_idx < h->num_buckets; lower_idx++) { count_so_far += h->buckets[lower_idx]; if (count_so_far >= count_below) { break; } } if (count_so_far == count_below) { /* this bucket hits the threshold exactly... we should be midway through any run of zero values following the bucket */ for (upper_idx = lower_idx + 1; upper_idx < h->num_buckets; upper_idx++) { if (h->buckets[upper_idx]) { break; } } return (bucket_start(h, (double)lower_idx) + bucket_start(h, (double)upper_idx)) / 2.0; } else { /* treat values as uniform throughout the bucket, and find where this value should lie */ lower_bound = bucket_start(h, (double)lower_idx); upper_bound = bucket_start(h, (double)(lower_idx + 1)); return GPR_CLAMP(upper_bound - (upper_bound - lower_bound) * (count_so_far - count_below) / h->buckets[lower_idx], h->min_seen, h->max_seen); } }
double grpc_pid_controller_update(grpc_pid_controller *pid_controller, double error, double dt) { /* integrate error using the trapezoid rule */ pid_controller->error_integral += dt * (pid_controller->last_error + error) * 0.5; pid_controller->error_integral = GPR_CLAMP( pid_controller->error_integral, -pid_controller->args.integral_range, pid_controller->args.integral_range); double diff_error = (error - pid_controller->last_error) / dt; /* calculate derivative of control value vs time */ double dc_dt = pid_controller->args.gain_p * error + pid_controller->args.gain_i * pid_controller->error_integral + pid_controller->args.gain_d * diff_error; /* and perform trapezoidal integration */ double new_control_value = pid_controller->last_control_value + dt * (pid_controller->last_dc_dt + dc_dt) * 0.5; new_control_value = GPR_CLAMP(new_control_value, pid_controller->args.min_control_value, pid_controller->args.max_control_value); pid_controller->last_error = error; pid_controller->last_dc_dt = dc_dt; pid_controller->last_control_value = new_control_value; return new_control_value; }
/* bounds checked version of the above */ static size_t bucket_for(gpr_histogram *h, double x) { size_t bucket = bucket_for_unchecked(h, GPR_CLAMP(x, 0, h->max_possible)); GPR_ASSERT(bucket < h->num_buckets); return bucket; }