1 // package lib - common routines for pstop
6 _ "github.com/go-sql-driver/mysql"
7 _ "github.com/sjmudd/pstop/version"
13 copyright = "Copyright (C) 2014 Simon J Mudd <sjmudd@pobox.com>"
14 i_1024_2 = 1024 * 1024
15 i_1024_3 = 1024 * 1024 * 1024
16 i_1024_4 = 1024 * 1024 * 1024 * 1024
19 // myround converts this floating value to the right width etc.
20 // There must be a function in Go to do this. Find it.
21 func myround(f float64, width, decimals int) string {
22 format := "%" + fmt.Sprintf("%d", width) + "." + fmt.Sprintf("%d", decimals) + "f"
23 return fmt.Sprintf(format, f)
26 // MyName returns the program's name.
27 func MyName() string {
31 // Copyright provides a copyright message for pstop
32 func Copyright() string {
36 // sec_to_time() converts a number of hours, minutes and seconds into hh:mm:ss format.
37 // e.g. 7384 = 2h 3m 4s, 7200 + 180 + 4
38 func sec_to_time(d uint64) string {
39 hours := d / 3600 // integer value
40 minutes := (d - hours*3600) / 60 // integer value
41 seconds := d - hours*3600 - minutes*60
43 return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
46 // similar to sec_to_time() spaces if 0 and takes seconds as input.
47 func FormatSeconds(seconds uint64) string {
51 return sec_to_time(seconds)
55 // FormatTime is based on sys.format_time. It
56 // formats to 10 characters including space and suffix.
57 // All values have 2 decimal places. Zero is returned as
59 func FormatTime(picoseconds uint64) string {
63 if picoseconds >= 3600000000000000 {
64 return myround(float64(picoseconds)/3600000000000000, 8, 2) + " h"
66 if picoseconds >= 60000000000000 {
67 return sec_to_time(picoseconds / 1000000000000)
69 if picoseconds >= 1000000000000 {
70 return myround(float64(picoseconds)/1000000000000, 8, 2) + " s"
72 if picoseconds >= 1000000000 {
73 return myround(float64(picoseconds)/1000000000, 7, 2) + " ms"
75 if picoseconds >= 1000000 {
76 return myround(float64(picoseconds)/1000000, 7, 2) + " us"
78 if picoseconds >= 1000 {
79 return myround(float64(picoseconds)/1000, 7, 2) + " ns"
81 return strconv.Itoa(int(picoseconds)) + " ps"
84 // FormatPct() formats a floating point number as a percentage
85 // including the trailing % sign. Print the value as a %5.1f with
86 // a % suffix if there's a value.
87 // If the value is 0 print as 6 spaces.
88 // if the value is > 999.9 then show +++.+% to indicate an overflow.
89 func FormatPct(pct float64) string {
93 } else if pct > 999.9 {
94 s = "+++.+%" // too large to fit! (probably a bug as we don't expect this value to be > 100.00)
96 s = fmt.Sprintf("%5.1f", 100.0*pct) + "%"
102 // FormatAmount() convert numbers to k = 1024 , M = 1024 x 1024, G = 1024 x 1024 x 1024, P = 1024x1024x1024x1024.
103 // For values = 0 return an empty string.
104 // For values < 1000 show 6,2 decimal places.
105 // For values >= 1000 show 6,1 decimal place.
106 func FormatAmount(amount uint64) string {
109 var decimal_amount float64
115 return strconv.Itoa(int(amount))
118 if amount > i_1024_4 {
120 decimal_amount = float64(amount) / i_1024_4
121 } else if amount > i_1024_3 {
123 decimal_amount = float64(amount) / i_1024_3
124 } else if amount > i_1024_2 {
126 decimal_amount = float64(amount) / i_1024_2
127 } else if amount > 1024 {
129 decimal_amount = float64(amount) / 1024
132 if decimal_amount > 1000.0 {
133 formatted = fmt.Sprintf("%6.1f %s", decimal_amount, suffix)
135 formatted = fmt.Sprintf("%6.2f %s", decimal_amount, suffix)
140 // like Amount but tigher in space
141 func FormatCounter( counter int, width int ) string {
143 pattern := "%" + fmt.Sprintf("%d", width) + "s"
144 return fmt.Sprintf( pattern, " " )
146 pattern := "%" + fmt.Sprintf("%d", width) + "d"
147 return fmt.Sprintf( pattern, counter )
151 // MyDivide() divides a by b except if b is 0 in which case we return 0.
152 func MyDivide(a uint64, b uint64) float64 {
156 return float64(a) / float64(b)
160 // Uptime() provides a usable form of uptime.
161 // Note: this doesn't return a string of a fixed size!
162 // Minimum value: 1s.
163 // Maximum value: 100d 23h 59m 59s (sort of).
164 func Uptime(uptime int) string {
167 days := uptime / 24 / 60 / 60
168 hours := (uptime - days*86400) / 3600
169 minutes := (uptime - days*86400 - hours*3600) / 60
170 seconds := uptime - days*86400 - hours*3600 - minutes*60
172 result = strconv.Itoa(seconds) + "s"
175 result = strconv.Itoa(minutes) + "m " + result
178 result = strconv.Itoa(hours) + "h " + result
181 result = strconv.Itoa(days) + "d " + result