info.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2016 ego authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package riot
  15. import (
  16. "sync"
  17. "github.com/go-vgo/gt/info"
  18. )
  19. var (
  20. lck sync.RWMutex
  21. // InitMemUsed init mem used
  22. InitMemUsed uint64
  23. // InitDiskUsed init disk used
  24. InitDiskUsed uint64
  25. )
  26. func init() {
  27. lck.Lock()
  28. InitMemUsed, _ = MemUsed()
  29. InitDiskUsed, _ = DiskUsed()
  30. lck.Unlock()
  31. }
  32. // MemPercent returns the amount of use memory in percent.
  33. func MemPercent() (string, error) {
  34. return info.MemPercent()
  35. }
  36. // MemUsed returns the amount of used memory in bytes.
  37. func MemUsed() (uint64, error) {
  38. return info.MemUsed()
  39. }
  40. // UsedMem returns the amount of riot used memory in bytes
  41. // after init() func.
  42. func (engine *Engine) UsedMem() (uint64, error) {
  43. memUsed, err := MemUsed()
  44. if err != nil {
  45. return 0, err
  46. }
  47. return memUsed - InitMemUsed, err
  48. }
  49. // MemTotal returns the amount of total memory in bytes.
  50. func MemTotal() (uint64, error) {
  51. return info.MemTotal()
  52. }
  53. // MemFree returns the amount of free memory in bytes.
  54. func MemFree() (uint64, error) {
  55. return info.MemFree()
  56. }
  57. // ToKB bytes to kb
  58. func ToKB(data uint64) uint64 {
  59. return data / 1024
  60. }
  61. // ToMB bytes to mb
  62. func ToMB(data uint64) uint64 {
  63. return data / 1024 / 1024
  64. }
  65. // ToGB bytes to gb
  66. func ToGB(data uint64) uint64 {
  67. return data / 1024 / 1024 / 1024
  68. }
  69. // Disk init the disk
  70. // func Disk(pt ...bool) ([]*disk.UsageStat, error) {
  71. // return info.Disk(pt...)
  72. // }
  73. // DiskPercent returns the amount of use disk in percent.
  74. func DiskPercent() (string, error) {
  75. return info.DiskPercent()
  76. }
  77. // DiskUsed returns the amount of use disk in bytes.
  78. func DiskUsed() (uint64, error) {
  79. return info.DiskUsed()
  80. }
  81. // UsedDisk returns the amount of use disk in bytes
  82. // after init() func.
  83. func (engine *Engine) UsedDisk() (uint64, error) {
  84. diskUsed, err := DiskUsed()
  85. if err != nil {
  86. return 0, err
  87. }
  88. return diskUsed - InitDiskUsed, err
  89. }
  90. // DiskTotal returns the amount of total disk in bytes.
  91. func DiskTotal() (uint64, error) {
  92. return info.DiskTotal()
  93. }
  94. // DiskFree returns the amount of free disk in bytes.
  95. func DiskFree() (uint64, error) {
  96. return info.DiskFree()
  97. }
  98. // CPUInfo returns the cpu info
  99. func CPUInfo(args ...int) (string, error) {
  100. return info.CPUInfo(args...)
  101. }
  102. // CPUPercent returns the amount of use cpu in percent.
  103. func CPUPercent() ([]float64, error) {
  104. return info.CPUPercent()
  105. }
  106. // Uptime returns the system uptime in seconds.
  107. func Uptime() (uptime uint64, err error) {
  108. return info.Uptime()
  109. }
  110. // PlatformInfo fetches system platform information.
  111. func PlatformInfo() (platform, family, osVersion string, err error) {
  112. return info.PlatformInfo()
  113. }
  114. // Platform returns the platform name and OS Version.
  115. func Platform() (string, error) {
  116. return info.Platform()
  117. }
  118. // KernelVer returns the kernel version as a string.
  119. func KernelVer() (string, error) {
  120. return info.KernelVer()
  121. }