Added TermboxScreen.ClearLine().
[pstop.git] / app / app.go
1 // app - pstop application package
2 //
3 // This file contains the library routines related to running the app.
4 package app
5
6 import (
7         "database/sql"
8         "errors"
9         "fmt"
10         "log"
11         "os"
12         "os/signal"
13         "regexp"
14         "strings"
15         "syscall"
16         "time"
17
18         "github.com/nsf/termbox-go"
19
20         "github.com/sjmudd/pstop/i_s/processlist"
21         "github.com/sjmudd/pstop/lib"
22         essgben "github.com/sjmudd/pstop/p_s/events_stages_summary_global_by_event_name"
23         ewsgben "github.com/sjmudd/pstop/p_s/events_waits_summary_global_by_event_name"
24         fsbi "github.com/sjmudd/pstop/p_s/file_summary_by_instance"
25         "github.com/sjmudd/pstop/p_s/ps_table"
26         "github.com/sjmudd/pstop/p_s/setup_instruments"
27         tiwsbt "github.com/sjmudd/pstop/p_s/table_io_waits_summary_by_table"
28         tlwsbt "github.com/sjmudd/pstop/p_s/table_lock_waits_summary_by_table"
29         "github.com/sjmudd/pstop/screen"
30         "github.com/sjmudd/pstop/version"
31         "github.com/sjmudd/pstop/wait_info"
32 )
33
34 // what information to show
35 type Show int
36
37 const (
38         showLatency = iota
39         showOps     = iota
40         showIO      = iota
41         showLocks   = iota
42         showUsers   = iota
43         showMutex   = iota
44         showStages  = iota
45 )
46
47 var (
48         re_valid_version = regexp.MustCompile(`^(5\.[67]\.|10\.[01])`)
49 )
50
51 type App struct {
52         done                chan struct{}
53         sigChan             chan os.Signal
54         wi                  wait_info.WaitInfo
55         finished            bool
56         dbh                 *sql.DB
57         help                bool
58         hostname            string
59         fsbi                ps_table.Tabler // ufsbi.File_summary_by_instance
60         tiwsbt              tiwsbt.Object
61         tlwsbt              ps_table.Tabler // tlwsbt.Table_lock_waits_summary_by_table
62         ewsgben             ps_table.Tabler // ewsgben.Events_waits_summary_global_by_event_name
63         essgben             ps_table.Tabler // essgben.Events_stages_summary_global_by_event_name
64         users               processlist.Object
65         screen              screen.TermboxScreen
66         show                Show
67         mysql_version       string
68         want_relative_stats bool
69         wait_info.WaitInfo  // embedded
70         setup_instruments   setup_instruments.SetupInstruments
71 }
72
73 func (app *App) Setup(dbh *sql.DB) {
74         app.dbh = dbh
75
76         if err := app.validate_mysql_version(); err != nil {
77                 log.Fatal(err)
78         }
79
80         app.finished = false
81         app.screen.Initialise()
82         app.setup_instruments = setup_instruments.NewSetupInstruments(dbh)
83         app.setup_instruments.EnableMonitoring()
84
85         _, variables := lib.SelectAllGlobalVariablesByVariableName(app.dbh)
86         // setup to their initial types/values
87         app.fsbi = fsbi.NewFileSummaryByInstance(variables)
88         app.tlwsbt = new(tlwsbt.Object)
89         app.ewsgben = new(ewsgben.Object)
90         app.essgben = new(essgben.Object)
91
92         app.want_relative_stats = true // we show info from the point we start collecting data
93         app.fsbi.SetWantRelativeStats(app.want_relative_stats)
94         app.fsbi.SetNow()
95         app.tlwsbt.SetWantRelativeStats(app.want_relative_stats)
96         app.tlwsbt.SetNow()
97         app.tiwsbt.SetWantRelativeStats(app.want_relative_stats)
98         app.tiwsbt.SetNow()
99         app.users.SetWantRelativeStats(app.want_relative_stats) // ignored
100         app.users.SetNow()                                      // ignored
101         app.essgben.SetWantRelativeStats(app.want_relative_stats)
102         app.essgben.SetNow()
103         app.ewsgben.SetWantRelativeStats(app.want_relative_stats) // ignored
104         app.ewsgben.SetNow()                                      // ignored
105
106         app.ResetDBStatistics()
107
108         app.SetHelp(false)
109         app.show = showLatency
110         app.tiwsbt.SetWantsLatency(true)
111
112         // get short name (to save space)
113         _, hostname := lib.SelectGlobalVariableByVariableName(app.dbh, "HOSTNAME")
114         if index := strings.Index(hostname, "."); index >= 0 {
115                 hostname = hostname[0:index]
116         }
117         _, mysql_version := lib.SelectGlobalVariableByVariableName(app.dbh, "VERSION")
118         app.SetHostname(hostname)
119         app.SetMySQLVersion(mysql_version)
120 }
121
122 // have we finished ?
123 func (app App) Finished() bool {
124         return app.finished
125 }
126
127 // indicate we have finished
128 func (app *App) SetFinished() {
129         app.finished = true
130 }
131
132 // do a fresh collection of data and then update the initial values based on that.
133 func (app *App) ResetDBStatistics() {
134         app.CollectAll()
135         app.SyncReferenceValues()
136 }
137
138 func (app *App) SyncReferenceValues() {
139         start := time.Now()
140         app.fsbi.SyncReferenceValues()
141         app.tlwsbt.SyncReferenceValues()
142         app.tiwsbt.SyncReferenceValues()
143         app.essgben.SyncReferenceValues()
144         lib.Logger.Println("app.SyncReferenceValues() took", time.Duration(time.Since(start)).String())
145 }
146
147 // collect all initial values on startup / reset
148 func (app *App) CollectAll() {
149         app.fsbi.Collect(app.dbh)
150         app.tlwsbt.Collect(app.dbh)
151         app.tiwsbt.Collect(app.dbh)
152 }
153
154 // Only collect the data we are looking at.
155 func (app *App) Collect() {
156         start := time.Now()
157
158         switch app.show {
159         case showLatency, showOps:
160                 app.tiwsbt.Collect(app.dbh)
161         case showIO:
162                 app.fsbi.Collect(app.dbh)
163         case showLocks:
164                 app.tlwsbt.Collect(app.dbh)
165         case showUsers:
166                 app.users.Collect(app.dbh)
167         case showMutex:
168                 app.ewsgben.Collect(app.dbh)
169         case showStages:
170                 app.essgben.Collect(app.dbh)
171         }
172         app.wi.CollectedNow()
173         lib.Logger.Println("app.Collect() took", time.Duration(time.Since(start)).String())
174 }
175
176 func (app App) MySQLVersion() string {
177         return app.mysql_version
178 }
179
180 func (app *App) SetHelp(newHelp bool) {
181         app.help = newHelp
182
183         app.screen.Clear()
184         app.screen.Flush()
185 }
186
187 func (app *App) SetMySQLVersion(mysql_version string) {
188         app.mysql_version = mysql_version
189 }
190
191 func (app *App) SetHostname(hostname string) {
192         app.hostname = hostname
193 }
194
195 func (app App) Help() bool {
196         return app.help
197 }
198
199 // apps go: showLatency -> showOps -> showIO -> showLocks -> showUsers -> showMutex -> showStages
200
201 // display the output according to the mode we are in
202 func (app *App) Display() {
203         if app.help {
204                 app.screen.DisplayHelp()
205         } else {
206                 app.displayHeading()
207                 switch app.show {
208                 case showLatency, showOps:
209                         app.displayOpsOrLatency()
210                 case showIO:
211                         app.displayIO()
212                 case showLocks:
213                         app.displayLocks()
214                 case showUsers:
215                         app.displayUsers()
216                 case showMutex:
217                         app.displayMutex()
218                 case showStages:
219                         app.displayStages()
220                 }
221         }
222 }
223
224 // fix_latency_setting() ensures the SetWantsLatency() value is
225 // correct. This needs to be done more cleanly.
226 func (app *App) fix_latency_setting() {
227         if app.show == showLatency {
228                 app.tiwsbt.SetWantsLatency(true)
229         }
230         if app.show == showOps {
231                 app.tiwsbt.SetWantsLatency(false)
232         }
233 }
234
235 // change to the previous display mode
236 func (app *App) DisplayPrevious() {
237         if app.show == showLatency {
238                 app.show = showStages
239         } else {
240                 app.show--
241         }
242         app.fix_latency_setting()
243         app.screen.Clear()
244         app.screen.Flush()
245 }
246
247 // change to the next display mode
248 func (app *App) DisplayNext() {
249         if app.show == showStages {
250                 app.show = showLatency
251         } else {
252                 app.show++
253         }
254         app.fix_latency_setting()
255         app.screen.Clear()
256         app.screen.Flush()
257 }
258
259 func (app App) displayHeading() {
260         app.displayLine0()
261         app.displayDescription()
262 }
263
264 func (app App) displayLine0() {
265         _, uptime := lib.SelectGlobalStatusByVariableName(app.dbh, "UPTIME")
266         top_line := lib.MyName() + " " + version.Version() + " - " + now_hhmmss() + " " + app.hostname + " / " + app.mysql_version + ", up " + fmt.Sprintf("%-16s", lib.Uptime(uptime))
267         if app.want_relative_stats {
268                 now := time.Now()
269
270                 var initial time.Time
271
272                 switch app.show {
273                 case showLatency, showOps:
274                         initial = app.tiwsbt.Last()
275                 case showIO:
276                         initial = app.fsbi.Last()
277                 case showLocks:
278                         initial = app.tlwsbt.Last()
279                 case showUsers:
280                         initial = app.users.Last()
281                 case showStages:
282                         initial = app.essgben.Last()
283                 case showMutex:
284                         initial = app.ewsgben.Last()
285                 default:
286                         // should not get here !
287                 }
288
289                 d := now.Sub(initial)
290
291                 top_line = top_line + " [REL] " + fmt.Sprintf("%.0f seconds", d.Seconds())
292         } else {
293                 top_line = top_line + " [ABS]             "
294         }
295         app.screen.PrintAt(0, 0, top_line)
296 }
297
298 func (app App) displayDescription() {
299         description := "UNKNOWN"
300
301         switch app.show {
302         case showLatency, showOps:
303                 description = app.tiwsbt.Description()
304         case showIO:
305                 description = app.fsbi.Description()
306         case showLocks:
307                 description = app.tlwsbt.Description()
308         case showUsers:
309                 description = app.users.Description()
310         case showMutex:
311                 description = app.ewsgben.Description()
312         case showStages:
313                 description = app.essgben.Description()
314         }
315
316         app.screen.PrintAt(0, 1, description)
317 }
318
319 func (app *App) displayOpsOrLatency() {
320         app.screen.BoldPrintAt(0, 2, app.tiwsbt.Headings())
321
322         max_rows := app.screen.Height() - 3
323         row_content := app.tiwsbt.RowContent(max_rows)
324
325         // print out rows
326         for k := range row_content {
327                 y := 3 + k
328                 app.screen.PrintAt(0, y, row_content[k])
329         }
330         // print out empty rows
331         for k := len(row_content); k < (app.screen.Height() - 3); k++ {
332                 y := 3 + k
333                 if y < app.screen.Height()-1 {
334                         app.screen.PrintAt(0, y, app.tiwsbt.EmptyRowContent())
335                 }
336         }
337
338         // print out the totals at the bottom
339         app.screen.BoldPrintAt(0, app.screen.Height()-1, app.tiwsbt.TotalRowContent())
340 }
341
342 // show actual I/O latency values
343 func (app App) displayIO() {
344         app.screen.BoldPrintAt(0, 2, app.fsbi.Headings())
345
346         // print out the data
347         max_rows := app.screen.Height() - 3
348         row_content := app.fsbi.RowContent(max_rows)
349
350         // print out rows
351         for k := range row_content {
352                 y := 3 + k
353                 app.screen.PrintAt(0, y, row_content[k])
354         }
355         // print out empty rows
356         for k := len(row_content); k < (app.screen.Height() - 3); k++ {
357                 y := 3 + k
358                 if y < app.screen.Height()-1 {
359                         app.screen.PrintAt(0, y, app.fsbi.EmptyRowContent())
360                 }
361         }
362
363         // print out the totals at the bottom
364         app.screen.BoldPrintAt(0, app.screen.Height()-1, app.fsbi.TotalRowContent())
365 }
366
367 func (app *App) displayLocks() {
368         app.screen.BoldPrintAt(0, 2, app.tlwsbt.Headings())
369
370         // print out the data
371         max_rows := app.screen.Height() - 3
372         row_content := app.tlwsbt.RowContent(max_rows)
373
374         // print out rows
375         for k := range row_content {
376                 y := 3 + k
377                 app.screen.PrintAt(0, y, row_content[k])
378         }
379         // print out empty rows
380         for k := len(row_content); k < (app.screen.Height() - 3); k++ {
381                 y := 3 + k
382                 if y < app.screen.Height()-1 {
383                         app.screen.PrintAt(0, y, app.tlwsbt.EmptyRowContent())
384                 }
385         }
386
387         // print out the totals at the bottom
388         app.screen.BoldPrintAt(0, app.screen.Height()-1, app.tlwsbt.TotalRowContent())
389 }
390
391 func (app *App) displayUsers() {
392         app.screen.BoldPrintAt(0, 2, app.users.Headings())
393
394         // print out the data
395         max_rows := app.screen.Height() - 3
396         row_content := app.users.RowContent(max_rows)
397
398         // print out rows
399         for k := range row_content {
400                 y := 3 + k
401                 app.screen.PrintAt(0, y, row_content[k])
402         }
403         // print out empty rows
404         for k := len(row_content); k < (app.screen.Height() - 3); k++ {
405                 y := 3 + k
406                 if y < app.screen.Height()-1 {
407                         app.screen.PrintAt(0, y, app.users.EmptyRowContent())
408                 }
409         }
410
411         // print out the totals at the bottom
412         app.screen.BoldPrintAt(0, app.screen.Height()-1, app.users.TotalRowContent())
413 }
414
415 func (app *App) displayMutex() {
416         app.screen.BoldPrintAt(0, 2, app.ewsgben.Headings())
417
418         // print out the data
419         max_rows := app.screen.Height() - 3
420         row_content := app.ewsgben.RowContent(max_rows)
421
422         // print out rows
423         for k := range row_content {
424                 y := 3 + k
425                 app.screen.PrintAt(0, y, row_content[k])
426         }
427         // print out empty rows
428         for k := len(row_content); k < (app.screen.Height() - 3); k++ {
429                 y := 3 + k
430                 if y < app.screen.Height()-1 {
431                         app.screen.PrintAt(0, y, app.ewsgben.EmptyRowContent())
432                 }
433         }
434
435         // print out the totals at the bottom
436         app.screen.BoldPrintAt(0, app.screen.Height()-1, app.ewsgben.TotalRowContent())
437 }
438
439 func (app *App) displayStages() {
440         app.screen.BoldPrintAt(0, 2, app.essgben.Headings())
441
442         // print out the data
443         max_rows := app.screen.Height() - 3
444         row_content := app.essgben.RowContent(max_rows)
445
446         // print out rows
447         for k := range row_content {
448                 y := 3 + k
449                 app.screen.PrintAt(0, y, row_content[k])
450         }
451         // print out empty rows
452         for k := len(row_content); k < (app.screen.Height() - 3); k++ {
453                 y := 3 + k
454                 if y < app.screen.Height()-1 {
455                         app.screen.PrintAt(0, y, app.essgben.EmptyRowContent())
456                 }
457         }
458
459         // print out the totals at the bottom
460         app.screen.BoldPrintAt(0, app.screen.Height()-1, app.essgben.TotalRowContent())
461 }
462
463 // do we want to show all p_s data?
464 func (app App) WantRelativeStats() bool {
465         return app.want_relative_stats
466 }
467
468 // set if we want data from when we started/reset stats.
469 func (app *App) SetWantRelativeStats(want_relative_stats bool) {
470         app.want_relative_stats = want_relative_stats
471
472         app.fsbi.SetWantRelativeStats(want_relative_stats)
473         app.tlwsbt.SetWantRelativeStats(app.want_relative_stats)
474         app.tiwsbt.SetWantRelativeStats(app.want_relative_stats)
475         app.ewsgben.SetWantRelativeStats(app.want_relative_stats)
476         app.essgben.SetWantRelativeStats(app.want_relative_stats)
477 }
478
479 // if there's a better way of doing this do it better ...
480 func now_hhmmss() string {
481         t := time.Now()
482         return fmt.Sprintf("%2d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
483 }
484
485 // record the latest screen size
486 func (app *App) ScreenSetSize(width, height int) {
487         app.screen.SetSize(width, height)
488 }
489
490 // clean up screen and disconnect database
491 func (app *App) Cleanup() {
492         app.screen.Close()
493         if app.dbh != nil {
494                 app.setup_instruments.RestoreConfiguration()
495                 _ = app.dbh.Close()
496         }
497 }
498
499 // get into a run loop
500 func (app *App) Run() {
501         app.done = make(chan struct{})
502         defer close(app.done)
503
504         app.sigChan = make(chan os.Signal, 1)
505         signal.Notify(app.sigChan, syscall.SIGINT, syscall.SIGTERM)
506
507         app.wi.SetWaitInterval(time.Second)
508
509         termboxChan := app.screen.TermBoxChan()
510
511         for !app.Finished() {
512                 select {
513                 case <-app.done:
514                         fmt.Println("app.done(): exiting")
515                         app.SetFinished()
516                 case sig := <-app.sigChan:
517                         fmt.Println("Caught a signal", sig)
518                         app.done <- struct{}{}
519                 case <-app.wi.WaitNextPeriod():
520                         app.Collect()
521                         app.Display()
522                 case event := <-termboxChan:
523                         // switch on event type
524                         switch event.Type {
525                         case termbox.EventKey: // actions depend on key
526                                 switch event.Key {
527                                 case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
528                                         app.SetFinished()
529                                 case termbox.KeyArrowLeft: // left arrow change to previous display mode
530                                         app.DisplayPrevious()
531                                         app.Display()
532                                 case termbox.KeyTab, termbox.KeyArrowRight: // tab or right arrow - change to next display mode
533                                         app.DisplayNext()
534                                         app.Display()
535                                 }
536                                 switch event.Ch {
537                                 case '-': // decrease the interval if > 1
538                                         if app.wi.WaitInterval() > time.Second {
539                                                 app.wi.SetWaitInterval(app.wi.WaitInterval() - time.Second)
540                                         }
541                                 case '+': // increase interval by creating a new ticker
542                                         app.wi.SetWaitInterval(app.wi.WaitInterval() + time.Second)
543                                 case 'h', '?': // help
544                                         app.SetHelp(!app.Help())
545                                 case 'q': // quit
546                                         app.SetFinished()
547                                 case 't': // toggle between absolute/relative statistics
548                                         app.SetWantRelativeStats(!app.WantRelativeStats())
549                                         app.Display()
550                                 case 'z': // reset the statistics to now by taking a query of current values
551                                         app.ResetDBStatistics()
552                                         app.Display()
553                                 }
554                         case termbox.EventResize: // set sizes
555                                 app.ScreenSetSize(event.Width, event.Height)
556                                 app.Display()
557                         case termbox.EventError: // quit
558                                 log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
559                         }
560                 }
561         }
562 }
563
564 // pstop requires MySQL 5.6+ or MariaDB 10.0+. Check the version
565 // rather than giving an error message if the requires P_S tables can't
566 // be found.
567 func (app *App) validate_mysql_version() error {
568         var tables = [...]string{
569                 "performance_schema.events_stages_summary_global_by_event_name",
570                 "performance_schema.events_waits_summary_global_by_event_name",
571                 "performance_schema.file_summary_by_instance",
572                 "performance_schema.table_io_waits_summary_by_table",
573                 "performance_schema.table_lock_waits_summary_by_table",
574         }
575
576         lib.Logger.Println("validate_mysql_version()")
577
578         lib.Logger.Println("- Getting MySQL version")
579         err, mysql_version := lib.SelectGlobalVariableByVariableName(app.dbh, "VERSION")
580         if err != nil {
581                 return err
582         }
583         lib.Logger.Println("- mysql_version: '" + mysql_version + "'")
584
585         if !re_valid_version.MatchString(mysql_version) {
586                 return errors.New(lib.MyName() + " does not work with MySQL version " + mysql_version)
587         }
588         lib.Logger.Println("OK: MySQL version is valid, continuing")
589
590         lib.Logger.Println("Checking access to required tables:")
591         for i := range tables {
592                 if err := lib.CheckTableAccess(app.dbh, tables[i]); err == nil {
593                         lib.Logger.Println("OK: " + tables[i] + " found")
594                 } else {
595                         return err
596                 }
597         }
598         lib.Logger.Println("OK: all table checks passed")
599
600         return nil
601 }