Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 34ebccd

Browse files
committed
Display explain buffers measurements in memory units rather than blocks. Also show "Total Buffer Usage" to hint that these are totals not averages per loop
1 parent f8c183a commit 34ebccd

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

src/backend/commands/explain.c

+74-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.200 2010/02/01 15:43:35 rhaas Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.201 2010/02/15 02:36:26 stark Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -98,6 +98,7 @@ static void ExplainJSONLineEnding(ExplainState *es);
9898
static void ExplainYAMLLineStarting(ExplainState *es);
9999
static void escape_json(StringInfo buf, const char *str);
100100
static void escape_yaml(StringInfo buf, const char *str);
101+
static double normalize_memory(double amount, char **unit, int *precision);
101102

102103

103104
/*
@@ -1081,47 +1082,63 @@ ExplainNode(Plan *plan, PlanState *planstate,
10811082
if (has_shared || has_local || has_temp)
10821083
{
10831084
appendStringInfoSpaces(es->str, es->indent * 2);
1084-
appendStringInfoString(es->str, "Buffers:");
1085+
appendStringInfoString(es->str, "Total Buffer Usage:");
10851086

10861087
if (has_shared)
10871088
{
1089+
char *hit_unit, *read_unit, *written_unit;
1090+
int hit_prec, read_prec, written_prec;
1091+
double hit_mem = normalize_memory((double)usage->shared_blks_hit * BLCKSZ, &hit_unit, &hit_prec);
1092+
double read_mem = normalize_memory((double)usage->shared_blks_read * BLCKSZ, &read_unit, &read_prec);
1093+
double written_mem = normalize_memory((double)usage->shared_blks_written * BLCKSZ, &written_unit, &written_prec);
1094+
10881095
appendStringInfoString(es->str, " shared");
1089-
if (usage->shared_blks_hit > 0)
1090-
appendStringInfo(es->str, " hit=%ld",
1091-
usage->shared_blks_hit);
1096+
appendStringInfo(es->str, " hit=%.*f%s",
1097+
hit_prec, hit_mem, hit_unit);
10921098
if (usage->shared_blks_read > 0)
1093-
appendStringInfo(es->str, " read=%ld",
1094-
usage->shared_blks_read);
1099+
appendStringInfo(es->str, " read=%.*f%s",
1100+
read_prec, read_mem, read_unit);
10951101
if (usage->shared_blks_written > 0)
1096-
appendStringInfo(es->str, " written=%ld",
1097-
usage->shared_blks_written);
1102+
appendStringInfo(es->str, " written=%.*f%s",
1103+
written_prec, written_mem, written_unit);
10981104
if (has_local || has_temp)
10991105
appendStringInfoChar(es->str, ',');
11001106
}
11011107
if (has_local)
11021108
{
1103-
appendStringInfoString(es->str, " local");
1104-
if (usage->local_blks_hit > 0)
1105-
appendStringInfo(es->str, " hit=%ld",
1106-
usage->local_blks_hit);
1107-
if (usage->local_blks_read > 0)
1108-
appendStringInfo(es->str, " read=%ld",
1109-
usage->local_blks_read);
1110-
if (usage->local_blks_written > 0)
1111-
appendStringInfo(es->str, " written=%ld",
1112-
usage->local_blks_written);
1109+
char *hit_unit, *read_unit, *written_unit;
1110+
int hit_prec, read_prec, written_prec;
1111+
double hit_mem = normalize_memory((double)usage->local_blks_hit * BLCKSZ, &hit_unit, &hit_prec);
1112+
double read_mem = normalize_memory((double)usage->local_blks_read * BLCKSZ, &read_unit, &read_prec);
1113+
double written_mem = normalize_memory((double)usage->local_blks_written * BLCKSZ, &written_unit, &written_prec);
1114+
1115+
appendStringInfoString(es->str, " local");
1116+
if (usage->local_blks_hit > 0)
1117+
appendStringInfo(es->str, " hit=%.*f%s",
1118+
hit_prec, hit_mem, hit_unit);
1119+
if (usage->local_blks_read > 0)
1120+
appendStringInfo(es->str, " read=%.*f%s",
1121+
read_prec, read_mem, read_unit);
1122+
if (usage->local_blks_written > 0)
1123+
appendStringInfo(es->str, " written=%.*f%s",
1124+
written_prec, written_mem, written_unit);
11131125
if (has_temp)
11141126
appendStringInfoChar(es->str, ',');
11151127
}
11161128
if (has_temp)
11171129
{
1130+
char *read_unit, *written_unit;
1131+
int read_prec, written_prec;
1132+
double read_mem = normalize_memory((double)usage->temp_blks_read * BLCKSZ, &read_unit, &read_prec);
1133+
double written_mem = normalize_memory((double)usage->temp_blks_written * BLCKSZ, &written_unit, &written_prec);
1134+
11181135
appendStringInfoString(es->str, " temp");
11191136
if (usage->temp_blks_read > 0)
1120-
appendStringInfo(es->str, " read=%ld",
1121-
usage->temp_blks_read);
1122-
if (usage->temp_blks_written > 0)
1123-
appendStringInfo(es->str, " written=%ld",
1124-
usage->temp_blks_written);
1137+
appendStringInfo(es->str, " read=%.*f%s",
1138+
read_prec, read_mem, read_unit);
1139+
if (usage->temp_blks_written > 0)
1140+
appendStringInfo(es->str, " written=%.*f%s",
1141+
written_prec, written_mem, written_unit);
11251142
}
11261143
appendStringInfoChar(es->str, '\n');
11271144
}
@@ -2153,3 +2170,36 @@ escape_yaml(StringInfo buf, const char *str)
21532170

21542171
appendStringInfo(buf, "%s", str);
21552172
}
2173+
2174+
/*
2175+
* For a quantity of bytes pick a reasonable display unit for it and
2176+
* return the quantity in that unit. Also return the unit name and a
2177+
* reasonable precision via the reference parameters.
2178+
*/
2179+
2180+
static double normalize_memory(double amount, char **unit, int *precision)
2181+
{
2182+
static char *units[] = {"bytes", "kB", "MB", "GB", "TB", "PB"};
2183+
char **u = units, **last = units + (sizeof(units)/sizeof(*units)-1);
2184+
2185+
while (amount > 1024.0 && u < last)
2186+
{
2187+
amount /= 1024.0;
2188+
u += 1;
2189+
}
2190+
2191+
*unit = *u;
2192+
2193+
/* if it's bytes or kB then don't print decimals since that's less
2194+
* than blocksize, otherwise always print 3 significant digits */
2195+
if (u == units || u == units+1 )
2196+
*precision = 0;
2197+
else if (amount < 10)
2198+
*precision = 2;
2199+
else if (amount < 100)
2200+
*precision = 1;
2201+
else
2202+
*precision = 0;
2203+
2204+
return amount;
2205+
}

0 commit comments

Comments
 (0)