Various adjustments
[pstop.git] / lib / common.go
1 // package lib - common routines for pstop
2 package lib
3
4 import (
5         "fmt"
6         _ "github.com/go-sql-driver/mysql"
7         _ "github.com/sjmudd/pstop/version"
8         "strconv"
9 )
10
11 const (
12         myname    = "pstop"
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
17 )
18
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)
24 }
25
26 // MyName returns the program's name.
27 func MyName() string {
28         return myname
29 }
30
31 // Copyright provides a copyright message for pstop
32 func Copyright() string {
33         return copyright
34 }
35
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
42
43         return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
44 }
45
46 // FormatTime is based on sys.format_time. It
47 // formats to 10 characters including space and suffix.
48 // All values have 2 decimal places. Zero is returned as
49 // an empty string.
50 func FormatTime(picoseconds uint64) string {
51         if picoseconds == 0 {
52                 return ""
53         }
54         if picoseconds >= 3600000000000000 {
55                 return myround(float64(picoseconds)/3600000000000000, 8, 2) + " h"
56         }
57         if picoseconds >= 60000000000000 {
58                 return sec_to_time(picoseconds / 1000000000000)
59         }
60         if picoseconds >= 1000000000000 {
61                 return myround(float64(picoseconds)/1000000000000, 8, 2) + " s"
62         }
63         if picoseconds >= 1000000000 {
64                 return myround(float64(picoseconds)/1000000000, 7, 2) + " ms"
65         }
66         if picoseconds >= 1000000 {
67                 return myround(float64(picoseconds)/1000000, 7, 2) + " us"
68         }
69         if picoseconds >= 1000 {
70                 return myround(float64(picoseconds)/1000, 7, 2) + " ns"
71         }
72         return strconv.Itoa(int(picoseconds)) + " ps"
73 }
74
75 // FormatPct() formats a floating point number as a percentage
76 // including the trailing % sign. Print the value as a %5.1f with
77 // a % suffix if there's a value.
78 // If the value is 0 print as 6 spaces.
79 // if the value is > 999.9 then show +++.+% to indicate an overflow.
80 func FormatPct(pct float64) string {
81         var s string
82         if pct < 0.0001 {
83                 s = "      "
84         } else if pct > 999.9 {
85                 s = "+++.+%" // too large to fit! (probably a bug as we don't expect this value to be > 100.00)
86         } else {
87                 s = fmt.Sprintf("%5.1f", 100.0*pct) + "%"
88         }
89
90         return s
91 }
92
93 // FormatAmount() convert numbers to k = 1024 , M = 1024 x 1024, G = 1024 x 1024 x 1024, P = 1024x1024x1024x1024.
94 // For values = 0 return an empty string.
95 // For values < 1000 show 6,2 decimal places.
96 // For values >= 1000 show 6,1 decimal place.
97 func FormatAmount(amount uint64) string {
98         var suffix string
99         var formatted string
100         var decimal_amount float64
101
102         if amount == 0 {
103                 return ""
104         }
105         if amount <= 1024 {
106                 return strconv.Itoa(int(amount))
107         }
108
109         if amount > i_1024_4 {
110                 suffix = "P"
111                 decimal_amount = float64(amount) / i_1024_4
112         } else if amount > i_1024_3 {
113                 suffix = "G"
114                 decimal_amount = float64(amount) / i_1024_3
115         } else if amount > i_1024_2 {
116                 suffix = "M"
117                 decimal_amount = float64(amount) / i_1024_2
118         } else if amount > 1024 {
119                 suffix = "k"
120                 decimal_amount = float64(amount) / 1024
121         }
122
123         if decimal_amount > 1000.0 {
124                 formatted = fmt.Sprintf("%6.1f %s", decimal_amount, suffix)
125         } else {
126                 formatted = fmt.Sprintf("%6.2f %s", decimal_amount, suffix)
127         }
128         return formatted
129 }
130
131 // MyDivide() divides a by b except if b is 0 in which case we return 0.
132 func MyDivide(a uint64, b uint64) float64 {
133         if b == 0 {
134                 return float64(0)
135         } else {
136                 return float64(a) / float64(b)
137         }
138 }
139
140 // Uptime() provides a  usable form of uptime.
141 // Note: this doesn't return a string of a fixed size!
142 // Minimum value: 1s.
143 // Maximum value: 100d 23h 59m 59s (sort of).
144 func Uptime(uptime int) string {
145         var result string
146
147         days := uptime / 24 / 60 / 60
148         hours := (uptime - days*86400) / 3600
149         minutes := (uptime - days*86400 - hours*3600) / 60
150         seconds := uptime - days*86400 - hours*3600 - minutes*60
151
152         result = strconv.Itoa(seconds) + "s"
153
154         if minutes > 0 {
155                 result = strconv.Itoa(minutes) + "m " + result
156         }
157         if hours > 0 {
158                 result = strconv.Itoa(hours) + "h " + result
159         }
160         if days > 0 {
161                 result = strconv.Itoa(days) + "d " + result
162         }
163
164         return result
165 }