1 // Top like progream which collects information from MySQL's
2 // performance_schema database.
18 _ "github.com/go-sql-driver/mysql"
19 "github.com/nsf/termbox-go"
21 "github.com/sjmudd/mysql_defaults_file"
22 "github.com/sjmudd/pstop/lib"
23 "github.com/sjmudd/pstop/state"
24 "github.com/sjmudd/pstop/version"
29 db = "performance_schema"
33 flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
34 flag_debug = flag.Bool("debug", false, "Enabling debug logging")
35 flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
36 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
38 re_valid_version = regexp.MustCompile(`^(5\.[67]\.|10\.[01])`)
41 func get_db_handle() *sql.DB {
44 lib.Logger.Println("get_db_handle() connecting to database")
46 dbh, err = mysql_defaults_file.OpenUsingDefaultsFile(sql_driver, "", "performance_schema")
50 if err = dbh.Ping(); err != nil {
57 // make chan for termbox events and run a poller to send events to the channel
58 // - return the channel
59 func new_tb_chan() chan termbox.Event {
60 termboxChan := make(chan termbox.Event)
63 termboxChan <- termbox.PollEvent()
70 fmt.Println(lib.MyName() + " - " + lib.Copyright())
72 fmt.Println("Top-like program to show MySQL activity by using information collected")
73 fmt.Println("from performance_schema.")
75 fmt.Println("Usage: " + lib.MyName() + " <options>")
77 fmt.Println("Options:")
78 fmt.Println("-help show this help message")
79 fmt.Println("-version show the version")
82 // pstop requires MySQL 5.6+ or MariaDB 10.0+. Check the version
83 // rather than giving an error message if the requires P_S tables can't
85 func validate_mysql_version(dbh *sql.DB) error {
86 var tables = [...]string{
87 "performance_schema.file_summary_by_instance",
88 "performance_Schema.table_io_waits_summary_by_table",
89 "performance_schema.table_lock_waits_summary_by_table",
92 lib.Logger.Println("validate_mysql_version()")
94 lib.Logger.Println("- Getting MySQL version")
95 err, mysql_version := lib.SelectGlobalVariableByVariableName(dbh, "VERSION")
99 lib.Logger.Println("- mysql_version: '" + mysql_version + "'")
101 if !re_valid_version.MatchString(mysql_version) {
102 err := errors.New(lib.MyName() + " does not work with MySQL version " + mysql_version)
105 lib.Logger.Println("OK: MySQL version is valid, continuing")
107 lib.Logger.Println("Checking access to required tables:")
108 for i := range tables {
109 if err := lib.CheckTableAccess(dbh, tables[i]); err == nil {
110 lib.Logger.Println("OK: " + tables[i] + " found")
115 lib.Logger.Println("OK: all table checks passed")
124 if *cpuprofile != "" {
125 f, err := os.Create(*cpuprofile)
129 pprof.StartCPUProfile(f)
130 defer pprof.StopCPUProfile()
134 lib.Logger.EnableLogging(true)
137 fmt.Println(lib.MyName() + " version " + version.Version())
145 lib.Logger.Println("Starting " + lib.MyName())
147 dbh := get_db_handle()
148 if err := validate_mysql_version(dbh); err != nil {
152 var state state.State
153 interval := time.Second
154 sigChan := make(chan os.Signal, 1)
155 done := make(chan struct{})
157 termboxChan := new_tb_chan()
159 signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
161 ticker := time.NewTicker(interval) // generate a periodic signal
169 fmt.Println("exiting")
171 case sig := <-sigChan:
172 fmt.Println("Caught a signal", sig)
177 case event := <-termboxChan:
178 // switch on event type
180 case termbox.EventKey: // actions depend on key
182 case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
184 case termbox.KeyTab: // tab - change display modes
189 case '-': // decrease the interval if > 1
190 if interval > time.Second {
192 interval -= time.Second
193 ticker = time.NewTicker(interval)
195 case '+': // increase interval by creating a new ticker
197 interval += time.Second
198 ticker = time.NewTicker(interval)
200 state.SetHelp(!state.Help())
203 case 't': // toggle between absolute/relative statistics
204 state.SetWantRelativeStats(!state.WantRelativeStats())
206 case 'z': // reset the statistics to now by taking a query of current values
207 state.ResetDBStatistics()
210 case termbox.EventResize: // set sizes
211 state.ScreenSetSize(event.Width, event.Height)
213 case termbox.EventError: // quit
214 log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
220 lib.Logger.Println("Terminating " + lib.MyName())