Skip to content

Commit

Permalink
Change cache hit ratio to reflect change since last snap
Browse files Browse the repository at this point in the history
This fixes #272
  • Loading branch information
blogh committed Jun 10, 2022
1 parent b422649 commit f074d9d
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 45 deletions.
6 changes: 3 additions & 3 deletions docs/man/pg_activity.1
Expand Up @@ -133,7 +133,7 @@
.\" ========================================================================
.\"
.IX Title "PG_ACTIVITY 1"
.TH PG_ACTIVITY 1 "2022-06-09" "pg_activity 3.0.0a1" "Command line tool for PostgreSQL server activity monitoring."
.TH PG_ACTIVITY 1 "2022-06-10" "pg_activity 3.0.0a1" "Command line tool for PostgreSQL server activity monitoring."
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
Expand Down Expand Up @@ -172,8 +172,8 @@ Global :
.IX Item "- dbs size: total size of the databases (filtered);"
.IP "\- \fBgrowth\fR: groth in B/s of the databases (filtered);" 2
.IX Item "- growth: groth in B/s of the databases (filtered);"
.IP "\- \fBcache hit ratio\fR: the percentage of page read from the PostgreSQL's cache (filtered)." 2
.IX Item "- cache hit ratio: the percentage of page read from the PostgreSQL's cache (filtered)."
.IP "\- \fBcache hit ratio\fR: the percentage of page read from the PostgreSQL's cache since last snapshot(filtered)." 2
.IX Item "- cache hit ratio: the percentage of page read from the PostgreSQL's cache since last snapshot(filtered)."
.PD
.PP
Sessions :
Expand Down
2 changes: 1 addition & 1 deletion docs/man/pg_activity.pod
Expand Up @@ -38,7 +38,7 @@ Global :

=item - B<growth>: groth in B/s of the databases (filtered);

=item - B<cache hit ratio>: the percentage of page read from the PostgreSQL's cache (filtered).
=item - B<cache hit ratio>: the percentage of page read from the PostgreSQL's cache since last snapshot(filtered).

=back

Expand Down
11 changes: 11 additions & 0 deletions pgactivity/data.py
Expand Up @@ -21,6 +21,7 @@
BlockingProcess,
Filters,
WaitingProcess,
Pct,
RunningProcess,
ServerInformation,
NO_FILTER,
Expand Down Expand Up @@ -232,11 +233,13 @@ def pg_get_server_information(
)
ret = cur.fetchone()

hr: Optional[Pct] = Pct(0.0)
tps, ips, ups, dps, rps = 0, 0, 0, 0, 0
size_ev = 0.0
if prev_server_info is not None:
dt = float(ret["epoch"] - prev_server_info.epoch)
try:
# Note: All this could be negative just after a stat reset
tps = int((ret["xact_count"] - prev_server_info.xact_count) / dt)
size_ev = float(ret["total_size"] - prev_server_info.total_size) / dt
ips = int((ret["insert"] - prev_server_info.insert) / dt)
Expand All @@ -245,6 +248,13 @@ def pg_get_server_information(
rps = int(
(ret["tuples_returned"] - prev_server_info.tuples_returned) / dt
)
deltaread = ret["blks_read"] - prev_server_info.blks_read
deltahit = ret["blks_hit"] - prev_server_info.blks_hit
hr = (
Pct(100 * deltahit / (deltaread + deltahit))
if deltaread + deltahit != 0
else None
)
except ZeroDivisionError:
pass

Expand All @@ -255,6 +265,7 @@ def pg_get_server_information(
update_per_second=ups,
delete_per_second=dps,
tuples_returned_per_second=rps,
cache_hit_ratio_last_snap=hr,
**ret,
)

Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_oldest.sql
Expand Up @@ -9,10 +9,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_090000.sql
Expand Up @@ -10,10 +10,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_090200.sql
Expand Up @@ -10,10 +10,8 @@ WITH dbinfos AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_090400.sql
Expand Up @@ -12,10 +12,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_090600.sql
Expand Up @@ -14,10 +14,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_100000.sql
Expand Up @@ -12,10 +12,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_110000.sql
Expand Up @@ -11,10 +11,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_120000.sql
Expand Up @@ -10,10 +10,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/queries/get_server_info_post_140000.sql
Expand Up @@ -10,10 +10,8 @@ WITH dbinfo AS(
WHEN %(skip_db_size)s THEN %(prev_total_size)s
ELSE SUM(pg_database_size(d.datname))
END, 0) AS total_size,
COALESCE(CASE WHEN sum(sd.blks_read) + sum(sd.blks_hit) = 0
THEN 0
ELSE trunc(100 * sum(sd.blks_hit) / (sum(sd.blks_read) + sum(sd.blks_hit)), 2)
END, 0) AS cache_hit_ratio,
COALESCE(SUM(sd.blks_read), 0) AS blks_read,
COALESCE(SUM(sd.blks_hit), 0) AS blks_hit,
COALESCE(MAX(LENGTH(d.datname)), 0) AS max_dbname_length,
current_timestamp - pg_postmaster_start_time() AS uptime,
EXTRACT(EPOCH FROM NOW()) AS epoch
Expand Down
6 changes: 5 additions & 1 deletion pgactivity/types.py
Expand Up @@ -795,7 +795,8 @@ class ServerInformation:
delete: int
tuples_returned: int
total_size: int
cache_hit_ratio: Pct = attr.ib(converter=Pct)
blks_read: int
blks_hit: int
max_dbname_length: int
uptime: timedelta
epoch: int # an epoch, used for the calculation of the tps & size_evolution
Expand Down Expand Up @@ -827,6 +828,9 @@ class ServerInformation:
update_per_second: int
delete_per_second: int
tuples_returned_per_second: int
cache_hit_ratio_last_snap: Optional[Pct] = attr.ib(
converter=attr.converters.optional(Pct)
)

@property
def worker_processes(self) -> Optional[int]:
Expand Down
2 changes: 1 addition & 1 deletion pgactivity/views.py
Expand Up @@ -242,7 +242,7 @@ def indent(text: str) -> str:
columns = [
[f"* Global: {render(uptime)} uptime"],
[f"{render(total_size)} dbs size - {render(size_ev)} growth"],
[f"{render(si.cache_hit_ratio)} cache hit ratio"],
[f"{render(si.cache_hit_ratio_last_snap)} cache hit ratio"],
]
yield from render_columns(columns, delimiter=f" {term.bold_blue('⋅')} ")

Expand Down
4 changes: 3 additions & 1 deletion tests/test_ui.txt
Expand Up @@ -124,7 +124,9 @@ Force local mode and mock server and system info
... xact_count=1000,
... total_size=20480000,
... size_evolution=1024,
... cache_hit_ratio=90.1222,
... blks_read=1,
... blks_hit=9,
... cache_hit_ratio_last_snap=90.1222,
... epoch=10000,
... uptime=timedelta(days=125, seconds=25, minutes=21, hours=3, weeks=1),
... tps=15,
Expand Down
8 changes: 6 additions & 2 deletions tests/test_views.txt
Expand Up @@ -98,7 +98,9 @@ Remote host:
... xact_count=1000,
... total_size=20480000,
... size_evolution=1024,
... cache_hit_ratio=90.1222,
... blks_read=1,
... blks_hit=9,
... cache_hit_ratio_last_snap=90.1222,
... epoch=10000,
... uptime=timedelta(days=125, seconds=25, minutes=21, hours=3, weeks=1),
... tps=15,
Expand Down Expand Up @@ -422,7 +424,9 @@ Tests for screen()
... xact_count=1000,
... total_size=20480000,
... size_evolution=1024,
... cache_hit_ratio=90.1222,
... blks_read=1,
... blks_hit=9,
... cache_hit_ratio_last_snap=90.1222,
... epoch=10000,
... uptime=timedelta(days=125, seconds=25, minutes=21, hours=3, weeks=1),
... tps=15,
Expand Down

0 comments on commit f074d9d

Please sign in to comment.