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>"
16 // myround converts this floating value to the right width etc.
17 // There must be a function in Go to do this. Find it.
18 func myround(f float64, width, decimals int) string {
19 format := "%" + fmt.Sprintf("%d", width) + "." + fmt.Sprintf("%d", decimals) + "f"
20 return fmt.Sprintf(format, f)
23 // MyName returns the program's name.
24 func MyName() string {
28 // Copyright provides a copyright message for pstop
29 func Copyright() string {
33 // sec_to_time() converts a number of hours, minutes and seconds into hh:mm:ss format.
34 // e.g. 7384 = 2h 3m 4s, 7200 + 180 + 4
35 func sec_to_time(d uint64) string {
36 hours := d / 3600 // integer value
37 minutes := (d - hours*3600) / 60 // integer value
38 seconds := d - hours*3600 - minutes*60
40 return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
43 // FormatTime is based on sys.format_time. It
44 // formats to 10 characters including space and suffix.
45 // All values have 2 decimal places. Zero is returned as
47 func FormatTime(picoseconds uint64) string {
51 if picoseconds >= 3600000000000000 {
52 return myround(float64(picoseconds)/3600000000000000, 8, 2) + " h"
54 if picoseconds >= 60000000000000 {
55 return sec_to_time(picoseconds / 1000000000000)
57 if picoseconds >= 1000000000000 {
58 return myround(float64(picoseconds)/1000000000000, 8, 2) + " s"
60 if picoseconds >= 1000000000 {
61 return myround(float64(picoseconds)/1000000000, 7, 2) + " ms"
63 if picoseconds >= 1000000 {
64 return myround(float64(picoseconds)/1000000, 7, 2) + " us"
66 if picoseconds >= 1000 {
67 return myround(float64(picoseconds)/1000, 7, 2) + " ns"
69 return strconv.Itoa(int(picoseconds)) + " ps"
72 // FormatPct() formats a floating point number as a percentage
73 // including the trailing % sign. Print the value as a %5.1f with
74 // a % suffix if there's a value.
75 // If the value is 0 print as 6 spaces.
76 // if the value is > 999.9 then show +++.+% to indicate an overflow.
77 func FormatPct(pct float64) string {
81 } else if pct > 999.9 {
82 s = "+++.+%" // too large to fit! (probably a bug as we don't expect this value to be > 100.00)
84 s = fmt.Sprintf("%5.1f", 100.0*pct) + "%"
90 // FormatAmount() convert numbers to k = 1024 , M = 1024 x 1024, G = 1024 x 1024 x 1024, P = 1024x1024x1024x1024.
91 // For values = 0 return an empty string.
92 // For values < 1000 show 6,2 decimal places.
93 // For values >= 1000 show 6,1 decimal place.
94 func FormatAmount(amount uint64) string {
97 var decimal_amount float64
103 return strconv.Itoa(int(amount))
106 if amount > (1024 * 1024 * 1024 * 1024) {
108 decimal_amount = float64(amount) / 1024 / 1024 / 1024 / 1024
109 } else if amount > (1024 * 1024 * 1024) {
111 decimal_amount = float64(amount) / 1024 / 1024 / 1024
112 } else if amount > (1024 * 1024) {
114 decimal_amount = float64(amount) / 1024 / 1024
115 } else if amount > 1024 {
117 decimal_amount = float64(amount) / 1024
120 if decimal_amount > 1000.0 {
121 formatted = fmt.Sprintf("%6.1f %s", decimal_amount, suffix)
123 formatted = fmt.Sprintf("%6.2f %s", decimal_amount, suffix)
128 // MyDivide() divides a by b except if b is 0 in which case we return 0.
129 func MyDivide(a uint64, b uint64) float64 {
133 return float64(a) / float64(b)
137 // Uptime() provides a usable form of uptime.
138 // Note: this doesn't return a string of a fixed size!
139 // Minimum value: 1s.
140 // Maximum value: 100d 23h 59m 59s (sort of).
141 func Uptime(uptime int) string {
144 days := uptime / 24 / 60 / 60
145 hours := (uptime - days*86400) / 3600
146 minutes := (uptime - days*86400 - hours*3600) / 60
147 seconds := uptime - days*86400 - hours*3600 - minutes*60
149 result = strconv.Itoa(seconds) + "s"
152 result = strconv.Itoa(minutes) + "m " + result
155 result = strconv.Itoa(hours) + "h " + result
158 result = strconv.Itoa(days) + "d " + result