1 // lib - library routines for pstop.
3 // this file contains the library routines related to the stored state in pstop.
11 "github.com/sjmudd/pstop/lib"
12 fsbi "github.com/sjmudd/pstop/performance_schema/file_summary_by_instance"
13 "github.com/sjmudd/pstop/performance_schema/ps_table"
14 tiwsbt "github.com/sjmudd/pstop/performance_schema/table_io_waits_summary_by_table"
15 tlwsbt "github.com/sjmudd/pstop/performance_schema/table_lock_waits_summary_by_table"
16 "github.com/sjmudd/pstop/screen"
17 "github.com/sjmudd/pstop/version"
20 // what information to show
35 fsbi ps_table.Tabler // ufsbi.File_summary_by_instance
36 tiwsbt tiwsbt.Table_io_waits_summary_by_table
37 tlwsbt ps_table.Tabler // tlwsbt.Table_lock_waits_summary_by_table
38 screen screen.TermboxScreen
41 want_relative_stats bool
44 func (state *State) Setup(dbh *sql.DB) {
47 state.screen.Initialise()
49 _, variables := lib.SelectAllGlobalVariablesByVariableName(state.dbh)
50 // setup to their initial types/values
51 state.fsbi = fsbi.NewFileSummaryByInstance(variables)
52 state.tlwsbt = new(tlwsbt.Table_lock_waits_summary_by_table)
54 state.want_relative_stats = true // we show info from the point we start collecting data
55 state.fsbi.SetWantRelativeStats(state.want_relative_stats)
57 state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
59 state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
62 state.ResetDBStatistics()
65 state.show = showLatency
66 state.tiwsbt.SetWantsLatency(true)
68 _, hostname := lib.SelectGlobalVariableByVariableName(state.dbh, "HOSTNAME")
69 _, mysql_version := lib.SelectGlobalVariableByVariableName(state.dbh, "VERSION")
70 _, datadir := lib.SelectGlobalVariableByVariableName(state.dbh, "DATADIR")
71 state.SetHostname(hostname)
72 state.SetMySQLVersion(mysql_version)
73 state.SetDatadir(datadir)
76 // do a fresh collection of data and then update the initial values based on that.
77 func (state *State) ResetDBStatistics() {
78 // collect all initial values on startup / reset
79 state.fsbi.Collect(state.dbh)
80 state.tlwsbt.Collect(state.dbh)
81 state.tiwsbt.Collect(state.dbh)
83 state.UpdateInitialValues()
86 func (state *State) UpdateInitialValues() {
88 state.fsbi.UpdateInitialValues()
89 state.tlwsbt.UpdateInitialValues()
90 state.tiwsbt.UpdateInitialValues()
91 lib.Logger.Println("state.UpdateInitialValues() took", time.Duration(time.Since(start)).String())
94 // Only collect the data we are looking at.
95 func (state *State) Collect() {
99 case showLatency, showOps:
100 state.tiwsbt.Collect(state.dbh)
102 state.fsbi.Collect(state.dbh)
104 state.tlwsbt.Collect(state.dbh)
106 lib.Logger.Println("state.Collect() took", time.Duration(time.Since(start)).String())
109 func (state State) MySQLVersion() string {
110 return state.mysql_version
113 func (state State) Datadir() string {
117 func (state *State) SetHelp(newHelp bool) {
124 func (state *State) SetDatadir(datadir string) {
125 state.datadir = datadir
128 func (state *State) SetMySQLVersion(mysql_version string) {
129 state.mysql_version = mysql_version
132 func (state *State) SetHostname(hostname string) {
133 state.hostname = hostname
136 func (state State) Help() bool {
140 // display the output according to the mode we are in
141 func (state *State) Display() {
143 state.screen.DisplayHelp()
145 state.displayHeading()
147 case showLatency, showOps:
148 state.displayOpsOrLatency()
157 // change to the next display mode
158 func (state *State) DisplayNext() {
159 if state.show == showLocks {
160 state.show = showLatency
164 // this needs to be done more cleanly
165 if state.show == showLatency {
166 state.tiwsbt.SetWantsLatency(true)
168 if state.show == showOps {
169 state.tiwsbt.SetWantsLatency(false)
175 func (state State) displayHeading() {
180 func (state State) displayLine0() {
181 _, uptime := lib.SelectGlobalStatusByVariableName(state.dbh, "UPTIME")
182 top_line := lib.MyName() + " " + version.Version() + " - " + now_hhmmss() + " " + state.hostname + " / " + state.mysql_version + ", up " + fmt.Sprintf("%-16s", lib.Uptime(uptime))
183 if state.want_relative_stats {
186 var initial time.Time
189 case showLatency, showOps:
190 initial = state.tiwsbt.Last()
192 initial = state.fsbi.Last()
194 initial = state.tlwsbt.Last()
196 initial = time.Now() // THIS IS WRONG !!!
199 d := now.Sub(initial)
201 top_line = top_line + " [REL] " + fmt.Sprintf("%.0f seconds", d.Seconds())
203 top_line = top_line + " [ABS] "
205 state.screen.PrintAt(0, 0, top_line)
208 func (state State) displayLine1() {
210 case showLatency, showOps:
211 state.screen.PrintAt(0, 1, state.tiwsbt.Description())
213 state.screen.PrintAt(0, 1, state.fsbi.Description())
215 state.screen.PrintAt(0, 1, state.tlwsbt.Description())
217 state.screen.PrintAt(0, 1, "UNKNOWN")
221 func (state *State) displayOpsOrLatency() {
222 state.screen.PrintAt(0, 2, state.tiwsbt.Headings())
224 max_rows := state.screen.Height() - 3
225 row_content := state.tiwsbt.RowContent(max_rows)
228 for k := range row_content {
230 state.screen.PrintAt(0, y, row_content[k])
232 // print out empty rows
233 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
235 if y < state.screen.Height()-1 {
236 state.screen.PrintAt(0, y, state.tiwsbt.EmptyRowContent())
240 // print out the totals at the bottom
241 state.screen.PrintAt(0, state.screen.Height()-1, state.tiwsbt.TotalRowContent())
244 // show actual I/O latency values
245 func (state State) displayIO() {
246 state.screen.PrintAt(0, 2, state.fsbi.Headings())
248 // print out the data
249 max_rows := state.screen.Height() - 3
250 row_content := state.fsbi.RowContent(max_rows)
253 for k := range row_content {
255 state.screen.PrintAt(0, y, row_content[k])
257 // print out empty rows
258 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
260 if y < state.screen.Height()-1 {
261 state.screen.PrintAt(0, y, state.fsbi.EmptyRowContent())
265 // print out the totals at the bottom
266 state.screen.PrintAt(0, state.screen.Height()-1, state.fsbi.TotalRowContent())
269 func (state *State) displayLocks() {
270 state.screen.PrintAt(0, 2, state.tlwsbt.Headings())
272 // print out the data
273 max_rows := state.screen.Height() - 3
274 row_content := state.tlwsbt.RowContent(max_rows)
277 for k := range row_content {
279 state.screen.PrintAt(0, y, row_content[k])
281 // print out empty rows
282 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
284 if y < state.screen.Height()-1 {
285 state.screen.PrintAt(0, y, state.tlwsbt.EmptyRowContent())
289 // print out the totals at the bottom
290 state.screen.PrintAt(0, state.screen.Height()-1, state.tlwsbt.TotalRowContent())
293 // do we want to show all p_s data?
294 func (state State) WantRelativeStats() bool {
295 return state.want_relative_stats
298 // set if we want data from when we started/reset stats.
299 func (state *State) SetWantRelativeStats(want_relative_stats bool) {
300 state.want_relative_stats = want_relative_stats
302 state.fsbi.SetWantRelativeStats(want_relative_stats)
303 state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
304 state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
309 // if there's a better way of doing this do it better ...
310 func now_hhmmss() string {
312 return fmt.Sprintf("%2d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
315 // record the latest screen size
316 func (state *State) ScreenSetSize(width, height int) {
317 state.screen.SetSize(width, height)
320 // clean up screen and disconnect database
321 func (state *State) Cleanup() {
323 if state.dbh != nil {
324 _ = state.dbh.Close()