ps-top

command module
v2.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: BSD-2-Clause Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ps-top - a top-like program for MySQL

ps-top collects information from the performance_schema database of MySQL and MariaDB and shows where the server is spending its time, in real time: by table, by file, by user, by mutex, by SQL stage and by memory area. Values are shown as the difference over a recent window (by default the last 10 seconds), or as the current values.

It runs full screen, one view at a time, or prints the views as text with --batch, for logging or for sharing.

This README describes ps-top v2, a rewrite. For v1, see the v1.3.3 README.

Installation

go install github.com/sjmudd/ps-top/v2@latest

This needs Go 1.26 or later. The binary is installed in $(go env GOPATH)/bin.

Usage

ps-top                                         # full screen: one view at a time, h for the keys
ps-top --interval=5s --window=30s              # redraw every 5s, differences over 30s
ps-top --batch                                 # all views as text, every --interval, until interrupted
ps-top --batch --interval=10s --count=6        # every 10s, 6 outputs
ps-top --batch --view=table_io_latency:current,file_io_latency
ps-top --batch --view=user_latency --limit=10  # top 10 lines
ps-top --batch --totals                        # totals lines only
ps-top --batch --anonymise                     # schema1.table2, user1: output to share
ps-top --batch --database-filter=shop,crm      # only the shop and crm databases

Metrics are collected every --collect-interval (default 1s). The screen or output is redrawn every --interval (default 2s). Differences cover --window (default 10s): the collect intervals that fit in it, so --window=10s at a 3s collect interval covers 9s. Durations also take whole seconds: --interval=3 is --interval=3s.

With --batch, the first output shows values up to now, as vmstat's first line does. The following ones show each view in its style: the difference over the window, or current values. --view takes a list of view names, each optionally followed by :current or :difference.

ps-top --help lists every option.

Connecting

ps-top                                         # ~/.my.cnf if it exists, else $USER on 127.0.0.1:3306
ps-top --defaults-file=/path/to/my.cnf         # the [client] group of this option file
ps-top --host=db1 --port=3307 --user=app --askpass
ps-top --socket=/run/mysqld/mysqld.sock
ps-top --dsn='app:secret@tcp(db1:3306)/?interpolateParams=true'
MYSQL_DSN='app:secret@tcp(db1:3306)/' ps-top --use-environment
  • --askpass asks for the password at startup, so it is not given on the command line.
  • --dsn and --use-environment (MYSQL_DSN) take a go-sql-driver DSN, used as given, and cannot be combined with the other connection options. Use them for settings the other options do not cover, such as TLS: ?tls=skip-verify.
  • The DSN built from the option file and options sets interpolateParams=true, so a query with arguments is one round trip rather than a prepared statement's three. Add it to a DSN you give.

Configuration

Tables named alike, such as one table per day, can be shown as one row in the File I/O view. Put the rules in the [munge] section of ~/.pstoprc: a regular expression, then its replacement.

[munge]
_[0-9]{8}$ = _YYYYMMDD
_[0-9]{6}$ = _YYYYMM

With these, shop.orders_20260101 is shown as shop.orders_YYYYMMDD. The rules are applied in no fixed order, not the order written: a known issue. A rule that is not a valid regular expression is skipped, with a warning in the log.

Logging

Nothing is logged unless asked for:

ps-top --log                                   # info to ./ps-top.log
ps-top --log=debug                             # debug to ./ps-top.log
ps-top --log-file=/tmp/p.log                   # info to /tmp/p.log
ps-top --log=help                              # list the levels
Level Logs
error ps-top or a view stops
warn ps-top carries on with less or suspect data: a skipped metric, a slow collection, counters reset at the source
info once per run: settings, connection, server, instruments enabled and restored, queries chosen, views; TUI messages and view changes
debug per collection and render: each query's SQL, rows and time, each render, each instrument changed

Each level includes those above it. Records are text, one per line, e.g.:

time=2026-11-28T11:36:18.564+01:00 level=INFO msg="found disabled instruments" pattern=stage/sql/% disabled=151
  • The file is appended to, and created with mode 0600.
  • Records are not anonymised: --anonymise changes only what is shown.
  • The password is never logged, only whether one is set.
  • Startup errors are shown on stderr as well as logged.

Server setup and grants

performance_schema must be enabled. It is on by default in MySQL and off by default in MariaDB. To enable it, set this in the server's configuration and restart the server:

[mysqld]
performance_schema = 1

The user ps-top connects as needs:

  • SELECT on performance_schema.
  • UPDATE on performance_schema.setup_instruments, for the Mutex Latency and SQL Stage Latency views. ps-top enables the instruments these views need if they are off, and restores their settings when it stops. Without this grant, or on a read-only server, these views may be empty.
  • PROCESS, for the Activity by Username view to show other users' connections. Without it only ps-top's own connection is shown.

Views

Name Title Shows
table_io_latency Table I/O Latency time spent on table operations, by table
table_io_ops Table I/O Ops the number of table operations, by table
file_io_latency File I/O Latency time spent on file I/O, by table or file
table_lock_latency Locks by Table Name time spent waiting for table locks, by table
user_latency Activity by Username connections, run and sleep times and statements, by user
mutex_latency Mutex Latency time spent waiting for mutexes
stages_latency SQL Stage Latency time spent in each SQL stage
memory_usage Memory Usage memory in use, by memory area

A view whose table the server does not have is skipped, and ps-top says so.

ps-top also shows its own costs: for each metric, the count, time and rows of its collection stages, the time of each render, and its own memory, garbage collection and goroutines. With --batch these are three views, ps_top_stages, ps_top_renders and ps_top_process, printed after the others. Full screen they are one view, ps_top, after Memory Usage.

Keys

Key Action
+ / - redraw less or more often: the interval 1s longer or shorter, down to the collect interval
Tab, Right / Shift-Tab, Left next or previous view
w / W shorter or longer window: 5, 10, 30 or 60s
r show the difference over the window, or current values
a anonymise schema, table and user names, on or off
z reset: windows start again from now
h, ? help, on or off
q, Esc, Ctrl-C quit
Ctrl-Z suspend: fg returns to ps-top

Tested versions

Server Versions
MySQL 8.0, 8.4, 9.7, 26.10
MariaDB 10.6, 11.8, 12.3

See test/integration/README.md for the Docker test harness.

Licensing

BSD 2-Clause License

Feedback

Feedback and patches welcome. I am especially interested in hearing from you if you are using ps-top, or if you have ideas of how I can better use other information from the performance_schema tables to provide a more complete vision of what MySQL is doing or where it's busy. The tool has been used by myself and colleagues and helped quickly identify bottlenecks and problems in several systems.

Simon J Mudd sjmudd@pobox.com

Code Documentation

pkg.go.dev/github.com/sjmudd/ps-top/v2

Documentation

Overview

Command ps-top collects performance_schema metrics on one interval and shows table views on another, full screen (the TUI, see internal/tui) or printed as text with --batch:

ps-top                                                 # the TUI: one view at a time, h for the keys
ps-top --interval=5s --window=30s                      # redraw every 5 s, differences over 30 s
ps-top --batch                                         # all views, every --interval, until interrupted
ps-top --batch --interval=10s --count=6                # every 10 s, 6 outputs
ps-top --batch --view=table_io_latency:current,file_io_latency
ps-top --batch --view=user_latency --limit=10          # top 10 lines
ps-top --batch --totals                                # totals lines only
ps-top --batch --anonymise                             # schema1.table2, user1: output to share
ps-top --batch --database-filter=shop,crm              # only the shop and crm databases

The first output shows values up to now, as vmstat's first line does, the following ones each view in its style (by default the difference over --window, or current values).

File I/O's table names are renamed by the munge section of ~/.pstoprc, if it exists (see internal/munge), so tables named alike show as one row:

[munge]
_[0-9]{8}$ = _YYYYMMDD                                 # shop.orders_20260101 shows as shop.orders_YYYYMMDD

Connecting (see internal/connect):

ps-top --batch                                         # ~/.my.cnf if it exists, else $USER on 127.0.0.1:3306
ps-top --batch --defaults-file=/path/to/my.cnf
ps-top --batch --host=db1 --port=3307 --user=app --askpass
ps-top --batch --socket=/run/mysqld/mysqld.sock
ps-top --batch --dsn='app:secret@tcp(db1:3306)/?interpolateParams=true'
MYSQL_DSN='app:secret@tcp(db1:3306)/?interpolateParams=true' ps-top --batch --use-environment

A DSN given with --dsn or MYSQL_DSN is used as given. The DSN built from the option file and flags sets interpolateParams=true, so a query with arguments is one round trip, not a prepared statement's three: add it to a DSN given.

Logging (none by default):

ps-top --log                                           # info to ./ps-top.log
ps-top --log=debug                                     # debug to ./ps-top.log
ps-top --log-file=/tmp/p.log                           # info to /tmp/p.log
ps-top --log=help                                      # list the levels

Directories

Path Synopsis
internal
anonymise
Package anonymise replaces the names of objects, such as schemas, tables and users, with anonymous equivalents, so output can be shared without showing them.
Package anonymise replaces the names of objects, such as schemas, tables and users, with anonymous equivalents, so output can be shared without showing them.
app
Package app wires the program: it probes the server, builds the metrics, starts the collector, and presents views on its own interval.
Package app wires the program: it probes the server, builds the metrics, starts the collector, and presents views on its own interval.
collector
Package collector collects every metric on its own ticker, one ring of snapshots per metric, and answers requests for the snapshots a metric type needs: the latest for CurrentValue, the start and end of a window for DifferenceValues.
Package collector collects every metric on its own ticker, one ring of snapshots per metric, and answers requests for the snapshots a metric type needs: the latest for CurrentValue, the start and end of a window for DifferenceValues.
connect
Package connect works out the DSN ps-top connects with, from its command line connection options.
Package connect works out the DSN ps-top connects with, from its command line connection options.
derive
Package derive computes what to display from the snapshots of a metric type: for DifferenceValues the difference between a start and an end snapshot (Window), for CurrentValue one snapshot's values as they are (Current).
Package derive computes what to display from the snapshots of a metric type: for DifferenceValues the difference between a start and an end snapshot (Window), for CurrentValue one snapshot's values as they are (Current).
fakesql
Package fakesql is a database/sql driver for tests.
Package fakesql is a database/sql driver for tests.
formatter
Package formatter turns values into display strings: times in picoseconds, counts, amounts and percentages.
Package formatter turns values into display strings: times in picoseconds, counts, amounts and percentages.
logging
Package logging sets up slog's default logger from the --log and --log-file flags.
Package logging sets up slog's default logger from the --log and --log-file flags.
mapper
Package mapper turns a raw key, as collected, into the key a view shows.
Package mapper turns a raw key, as collected, into the key a view shows.
metric
Package metric describes the shape of a collected metric: its value columns, how each behaves over time, and the metric types (current values or a difference) its views can ask for.
Package metric describes the shape of a collected metric: its value columns, how each behaves over time, and the metric types (current values or a difference) its views can ask for.
metrics
Package metrics lists the metrics the program collects, and reads one snapshot of a metric from the server.
Package metrics lists the metrics the program collects, and reads one snapshot of a metric from the server.
metrics/fileio
Package fileio is file I/O per file name: operation counts, wait times and bytes, in total and per operation, from file_summary_by_instance.
Package fileio is file I/O per file name: operation counts, wait times and bytes, in total and per operation, from file_summary_by_instance.
metrics/memory
Package memory is memory in use per instrument, from memory_summary_global_by_event_name: current and highest allocation counts and bytes, and the number of allocations and frees.
Package memory is memory in use per instrument, from memory_summary_global_by_event_name: current and highest allocation counts and bytes, and the number of allocations and frees.
metrics/stages
Package stages is time spent per statement execution stage, from events_stages_summary_global_by_event_name.
Package stages is time spent per statement execution stage, from events_stages_summary_global_by_event_name.
metrics/synchwaits
Package synchwaits is waits on server mutexes and InnoDB sx-locks per instrument, from events_waits_summary_global_by_event_name.
Package synchwaits is waits on server mutexes and InnoDB sx-locks per instrument, from events_waits_summary_global_by_event_name.
metrics/tableio
Package tableio is table I/O per table: operation counts and wait times, in total and per operation, from table_io_waits_summary_by_table.
Package tableio is table I/O per table: operation counts and wait times, in total and per operation, from table_io_waits_summary_by_table.
metrics/tablelocks
Package tablelocks is table lock wait time per table, in total and per lock type, from table_lock_waits_summary_by_table.
Package tablelocks is table lock wait time per table, in total and per lock type, from table_lock_waits_summary_by_table.
metrics/userlatency
Package userlatency is activity per user from the processlist.
Package userlatency is activity per user from the processlist.
munge
Package munge renames table names by the rules of the munge section of ~/.pstoprc, as v1 does, so that tables named alike show as one row.
Package munge renames table names by the rules of the munge section of ~/.pstoprc, as v1 does, so that tables named alike show as one row.
presenter
Package presenter turns the difference over a window into table lines.
Package presenter turns the difference over a window into table lines.
probe
Package probe finds out once, at connect, what the server provides: flavour, version, which metric queries run, which tables may be updated, and the values of named variables and status counters.
Package probe finds out once, at connect, what the server provides: flavour, version, which metric queries run, which tables may be updated, and the values of named variables and status counters.
query
Package query runs a SQL statement and hands each result row to the caller, with each column scanned by its declared type.
Package query runs a SQL statement and hands each result row to the caller, with each column scanned by its declared type.
registry
Package registry is the metrics registry: what ps-top's own pipeline does, so its costs can be seen.
Package registry is the metrics registry: what ps-top's own pipeline does, so its costs can be seen.
setupinstruments
Package setupinstruments makes sure the performance_schema instruments some views need are enabled and timed, and puts back what it changed.
Package setupinstruments makes sure the performance_schema instruments some views need are enabled and timed, and puts back what it changed.
snapshot
Package snapshot holds the transformed entries returned by a single query taken at time Snapshot.Time, indexed by key.
Package snapshot holds the transformed entries returned by a single query taken at time Snapshot.Time, indexed by key.
store
Package store keeps the most recent snapshots of one metric in a ring buffer.
Package store keeps the most recent snapshots of one metric in a ring buffer.
transform
Package transform turns raw query entries into snapshot entries, and maps the keys of a snapshot for a view.
Package transform turns raw query entries into snapshot entries, and maps the keys of a snapshot for a view.
tui
Package tui is ps-top's terminal interface: a Bubble Tea program that draws one view at a time, full screen, and redraws it every interval.
Package tui is ps-top's terminal interface: a Bubble Tea program that draws one view at a time, full screen, and redraws it every interval.
test
integration/load/loadgen command
loadgen is a sysbench-alternative load generator using go-sql-driver/mysql, which supports caching_sha2_password natively.
loadgen is a sysbench-alternative load generator using go-sql-driver/mysql, which supports caching_sha2_password natively.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL