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() {
79 state.UpdateInitialValues()
82 func (state *State) UpdateInitialValues() {
83 state.fsbi.UpdateInitialValues()
84 state.tlwsbt.UpdateInitialValues()
85 state.tiwsbt.UpdateInitialValues()
88 func (state *State) Collect() {
89 state.fsbi.Collect(state.dbh)
90 state.tlwsbt.Collect(state.dbh)
91 state.tiwsbt.Collect(state.dbh)
94 func (state State) MySQLVersion() string {
95 return state.mysql_version
98 func (state State) Datadir() string {
102 func (state *State) SetHelp(newHelp bool) {
109 func (state *State) SetDatadir(datadir string) {
110 state.datadir = datadir
113 func (state *State) SetMySQLVersion(mysql_version string) {
114 state.mysql_version = mysql_version
117 func (state *State) SetHostname(hostname string) {
118 state.hostname = hostname
121 func (state State) Help() bool {
125 // display the output according to the mode we are in
126 func (state *State) Display() {
128 state.screen.DisplayHelp()
130 state.displayHeading()
132 case showLatency, showOps:
133 state.displayOpsOrLatency()
142 // change to the next display mode
143 func (state *State) DisplayNext() {
144 if state.show == showLocks {
145 state.show = showLatency
149 // this needs to be done more cleanly
150 if state.show == showLatency {
151 state.tiwsbt.SetWantsLatency(true)
153 if state.show == showOps {
154 state.tiwsbt.SetWantsLatency(false)
160 func (state State) displayHeading() {
165 func (state State) displayLine0() {
166 _, uptime := lib.SelectGlobalStatusByVariableName(state.dbh, "UPTIME")
167 top_line := lib.MyName() + " " + version.Version() + " - " + now_hhmmss() + " " + state.hostname + " / " + state.mysql_version + ", up " + fmt.Sprintf("%-16s", lib.Uptime(uptime))
168 if state.want_relative_stats {
171 var initial time.Time
174 case showLatency, showOps:
175 initial = state.tiwsbt.Last()
177 initial = state.fsbi.Last()
179 initial = state.tlwsbt.Last()
181 initial = time.Now() // THIS IS WRONG !!!
184 d := now.Sub(initial)
186 top_line = top_line + " [REL] " + fmt.Sprintf("%.0f seconds", d.Seconds())
188 top_line = top_line + " [ABS] "
190 state.screen.PrintAt(0, 0, top_line)
193 func (state State) displayLine1() {
195 case showLatency, showOps:
196 state.screen.PrintAt(0, 1, state.tiwsbt.Description())
198 state.screen.PrintAt(0, 1, state.fsbi.Description())
200 state.screen.PrintAt(0, 1, state.tlwsbt.Description())
202 state.screen.PrintAt(0, 1, "UNKNOWN")
206 func (state *State) displayOpsOrLatency() {
207 state.screen.PrintAt(0, 2, state.tiwsbt.Headings())
209 max_rows := state.screen.Height() - 3
210 row_content := state.tiwsbt.RowContent(max_rows)
213 for k := range row_content {
215 state.screen.PrintAt(0, y, row_content[k])
217 // print out empty rows
218 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
220 if y < state.screen.Height()-1 {
221 state.screen.PrintAt(0, y, state.tiwsbt.EmptyRowContent())
225 // print out the totals at the bottom
226 state.screen.PrintAt(0, state.screen.Height()-1, state.tiwsbt.TotalRowContent())
229 // show actual I/O latency values
230 func (state State) displayIO() {
231 state.screen.PrintAt(0, 2, state.fsbi.Headings())
233 // print out the data
234 max_rows := state.screen.Height() - 3
235 row_content := state.fsbi.RowContent(max_rows)
238 for k := range row_content {
240 state.screen.PrintAt(0, y, row_content[k])
242 // print out empty rows
243 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
245 if y < state.screen.Height()-1 {
246 state.screen.PrintAt(0, y, state.fsbi.EmptyRowContent())
250 // print out the totals at the bottom
251 state.screen.PrintAt(0, state.screen.Height()-1, state.fsbi.TotalRowContent())
254 func (state *State) displayLocks() {
255 state.screen.PrintAt(0, 2, state.tlwsbt.Headings())
257 // print out the data
258 max_rows := state.screen.Height() - 3
259 row_content := state.tlwsbt.RowContent(max_rows)
262 for k := range row_content {
264 state.screen.PrintAt(0, y, row_content[k])
266 // print out empty rows
267 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
269 if y < state.screen.Height()-1 {
270 state.screen.PrintAt(0, y, state.tlwsbt.EmptyRowContent())
274 // print out the totals at the bottom
275 state.screen.PrintAt(0, state.screen.Height()-1, state.tlwsbt.TotalRowContent())
278 // do we want to show all p_s data?
279 func (state State) WantRelativeStats() bool {
280 return state.want_relative_stats
283 // set if we want data from when we started/reset stats.
284 func (state *State) SetWantRelativeStats(want_relative_stats bool) {
285 state.want_relative_stats = want_relative_stats
287 state.fsbi.SetWantRelativeStats(want_relative_stats)
288 state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
289 state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
294 // if there's a better way of doing this do it better ...
295 func now_hhmmss() string {
297 return fmt.Sprintf("%2d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
300 // record the latest screen size
301 func (state *State) ScreenSetSize(width, height int) {
302 state.screen.SetSize(width, height)
305 // clean up screen and disconnect database
306 func (state *State) Cleanup() {
308 if state.dbh != nil {
309 _ = state.dbh.Close()