cdtime_t latency_counter_get_percentile (latency_counter_t *lc, double percent) { double percent_upper; double percent_lower; double ms_upper; double ms_lower; double ms_interpolated; int sum; size_t i; if ((lc == NULL) || (lc->num == 0) || !((percent > 0.0) && (percent < 100.0))) return (0); /* Find index i so that at least "percent" events are within i+1 ms. */ percent_upper = 0.0; percent_lower = 0.0; sum = 0; for (i = 0; i < LATENCY_HISTOGRAM_SIZE; i++) { percent_lower = percent_upper; sum += lc->histogram[i]; if (sum == 0) percent_upper = 0.0; else percent_upper = 100.0 * ((double) sum) / ((double) lc->num); if (percent_upper >= percent) break; } if (i >= LATENCY_HISTOGRAM_SIZE) return (0); assert (percent_upper >= percent); assert (percent_lower < percent); ms_upper = (double) (i + 1); ms_lower = (double) i; if (i == 0) return (MS_TO_CDTIME_T (ms_upper)); ms_interpolated = (((percent_upper - percent) * ms_lower) + ((percent - percent_lower) * ms_upper)) / (percent_upper - percent_lower); return (MS_TO_CDTIME_T (ms_interpolated)); } /* }}} cdtime_t latency_counter_get_percentile */
static int statsd_handle_timer (char const *name, /* {{{ */ char const *value_str, char const *extra) { statsd_metric_t *metric; value_t value_ms; value_t scale; cdtime_t value; int status; if ((extra != NULL) && (extra[0] != '@')) return (-1); scale.gauge = 1.0; if (extra != NULL) { status = statsd_parse_value (extra + 1, &scale); if (status != 0) return (status); if (!isfinite (scale.gauge) || (scale.gauge <= 0.0) || (scale.gauge > 1.0)) return (-1); } value_ms.derive = 0; status = statsd_parse_value (value_str, &value_ms); if (status != 0) return (status); value = MS_TO_CDTIME_T (value_ms.gauge / scale.gauge); pthread_mutex_lock (&metrics_lock); metric = statsd_metric_lookup_unsafe (name, STATSD_TIMER); if (metric == NULL) { pthread_mutex_unlock (&metrics_lock); return (-1); } if (metric->latency == NULL) metric->latency = latency_counter_create (); if (metric->latency == NULL) { pthread_mutex_unlock (&metrics_lock); return (-1); } latency_counter_add (metric->latency, value); metric->updates_num++; pthread_mutex_unlock (&metrics_lock); return (0); } /* }}} int statsd_handle_timer */
/** * Non blocking pause for the thread. */ static int cgps_thread_pause(cdtime_t pTime) { cdtime_t now; now = cdtime (); struct timespec pause_th; CDTIME_T_TO_TIMESPEC (MS_TO_CDTIME_T(10), &pause_th); while (CGPS_TRUE) { if ( (cdtime () - now) > pTime ) { break; } pthread_mutex_lock (&cgps_thread_lock); if (cgps_thread_shutdown == CGPS_TRUE) { return CGPS_FALSE; } pthread_mutex_unlock (&cgps_thread_lock); nanosleep (&pause_th, NULL); } return CGPS_TRUE; }