Permalink
Browse files

Added ability to scale the histogram as if the sampling rate was 100%.

  • Loading branch information...
1 parent 7c405fd commit 10e7af43a61d0e71ecf658e360ba417ba37c0c05 @tvondra committed Oct 30, 2011
Showing with 17 additions and 5 deletions.
  1. +3 −1 query_histogram--1.0.sql
  2. +2 −2 query_histogram.c
  3. +11 −1 queryhist.c
  4. +1 −1 queryhist.h
View
@@ -1,4 +1,4 @@
-CREATE OR REPLACE FUNCTION query_histogram( OUT bin_from INT, OUT bin_to INT, OUT bin_count INT, OUT bin_count_pct REAL,
+CREATE OR REPLACE FUNCTION query_histogram( IN scale BOOLEAN DEFAULT TRUE, OUT bin_from INT, OUT bin_to INT, OUT bin_count INT, OUT bin_count_pct REAL,
OUT bin_time REAL, OUT bin_time_pct REAL)
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'query_histogram'
@@ -13,3 +13,5 @@ CREATE OR REPLACE FUNCTION query_histogram_refresh()
RETURNS void
AS 'MODULE_PATHNAME', 'query_histogram_refresh'
LANGUAGE C IMMUTABLE;
+
+CREATE OR REPLACE VIEW query_histogram AS SELECT * FROM query_histogram(true);
View
@@ -35,8 +35,8 @@ query_histogram(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
-
- data = query_hist_get_data();
+
+ data = query_hist_get_data(PG_GETARG_BOOL(0));
/* init (open file, etc.), maybe read all the data in memory
* so that the file is not kept open for a long time */
View
@@ -444,9 +444,10 @@ void query_hist_add_query(hist_bin_time_t duration) {
}
-histogram_data * query_hist_get_data() {
+histogram_data * query_hist_get_data(bool scale) {
int i = 0;
+ double coeff = 0;
histogram_data * tmp = (histogram_data *)palloc(sizeof(histogram_data));
@@ -465,6 +466,15 @@ histogram_data * query_hist_get_data() {
memcpy(tmp->count_data, histogram_count_bins, sizeof(hist_bin_count_t) * ((*histogram_bins)+1));
memcpy(tmp->time_data, histogram_time_bins, sizeof(hist_bin_time_t) * ((*histogram_bins)+1));
+ /* check if we need to scale the histogram */
+ if (scale && ((*histogram_sample_pct) < 100)) {
+ coeff = (100.0 / (*histogram_sample_pct));
+ for (i = 0; i < (*histogram_bins)+1; i++) {
+ tmp->count_data[i] = tmp->count_data[i] * coeff;
+ tmp->time_data[i] = tmp->time_data[i] * coeff;
+ }
+ }
+
for (i = 0; i < (*histogram_bins)+1; i++) {
tmp->total_count += tmp->count_data[i];
tmp->total_time += tmp->time_data[i];
View
@@ -19,6 +19,6 @@ typedef enum {
HISTOGRAM_LOG
} histogram_type_t;
-histogram_data * query_hist_get_data();
+histogram_data * query_hist_get_data(bool scale);
void query_hist_reset(bool locked);
void query_hist_refresh();

0 comments on commit 10e7af4

Please sign in to comment.