cpusetutils.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cgroupv1
  15. import (
  16. "encoding/json"
  17. "io/ioutil"
  18. "regexp"
  19. "strconv"
  20. "sync"
  21. "yunion.io/x/log"
  22. "yunion.io/x/onecloud/pkg/util/fileutils2"
  23. )
  24. const (
  25. utilHistoryFile = "/tmp/util.history"
  26. MAX_HISTORY_UTIL_COUNT = 5
  27. )
  28. var (
  29. rebalanceProcessesLock = sync.Mutex{}
  30. rebalanceProcessesRunning = false
  31. utilHistory map[string][]float64
  32. )
  33. func (m *cgroupManager) RebalanceProcesses(pids []string) {
  34. rebalanceProcessesLock.Lock()
  35. if rebalanceProcessesRunning {
  36. rebalanceProcessesLock.Unlock()
  37. return
  38. } else {
  39. rebalanceProcessesRunning = true
  40. rebalanceProcessesLock.Unlock()
  41. }
  42. err := rebalanceProcesses(pids)
  43. if err != nil {
  44. log.Errorf("rebalance processes error: %s", err)
  45. }
  46. rebalanceProcessesRunning = false
  47. }
  48. func rebalanceProcesses(pids []string) error {
  49. FetchHistoryUtil()
  50. cpu, err := GetSystemCpu()
  51. if err != nil {
  52. log.Errorln(err)
  53. return err
  54. }
  55. cpuCount := cpu.GetPhysicalNum()
  56. if cpuCount <= 1 {
  57. return nil
  58. }
  59. info, err := GetProcessesCpuinfo(pids)
  60. if err != nil {
  61. return err
  62. }
  63. ret := ArrangeProcesses(info, cpuCount)
  64. if len(ret) != 0 {
  65. CommitProcessesCpuset(ret)
  66. }
  67. SaveHistoryUtil()
  68. return nil
  69. }
  70. func CommitProcessesCpuset(cpus []CPULoad) {
  71. for i, cpu := range cpus {
  72. for _, proc := range cpu.Processes {
  73. if proc.Cpuset == nil || *proc.Cpuset != i {
  74. CommitProcessCpuset(proc, i)
  75. }
  76. }
  77. }
  78. }
  79. func CommitProcessCpuset(proc *ProcessCPUinfo, idx int) {
  80. cpu, _ := GetSystemCpu()
  81. sets := cpu.GetCpuset(idx)
  82. if len(sets) > 0 {
  83. cpuset := manager.NewCGroupCPUSetTask(strconv.Itoa(proc.Pid), "", sets, "")
  84. cpuset.SetTask()
  85. }
  86. }
  87. func GetMaxLoadDiff(cpus []CPULoad) float64 {
  88. var maxLd, minLd = -1.0, -1.0
  89. for _, cpu := range cpus {
  90. wt := cpu.GetWeight()
  91. if maxLd < 0 || maxLd < wt {
  92. maxLd = wt
  93. }
  94. if minLd < 0 || minLd > wt {
  95. minLd = wt
  96. }
  97. }
  98. if minLd > 0 {
  99. return (maxLd - minLd) / minLd
  100. } else {
  101. return 0.0
  102. }
  103. }
  104. func GetMinLoadCpu(cpus []CPULoad, cpuset *int) *CPULoad {
  105. var (
  106. mintWt = -1.0
  107. minCpu *CPULoad
  108. )
  109. for i := 0; i < len(cpus); i++ {
  110. wt := cpus[i].GetWeight()
  111. if mintWt < 0.0 || mintWt > wt || (mintWt == wt && cpuset != nil && *cpuset == i) {
  112. mintWt = wt
  113. minCpu = &cpus[i]
  114. }
  115. }
  116. return minCpu
  117. }
  118. func ArrangeProcesses(infos []*ProcessCPUinfo, cpuCount int) []CPULoad {
  119. var (
  120. newProc = false
  121. cpus = make([]CPULoad, cpuCount)
  122. procs = CPULoad{}
  123. )
  124. for _, info := range infos {
  125. procs.AddProcess(info)
  126. if info.Cpuset != nil && *info.Cpuset < cpuCount {
  127. cpus[*info.Cpuset].AddProcess(info)
  128. } else {
  129. newProc = true
  130. }
  131. }
  132. prevDiff := GetMaxLoadDiff(cpus)
  133. if !newProc && prevDiff < 0.2 {
  134. return nil
  135. }
  136. for _, c := range cpus {
  137. c.Processes = nil
  138. }
  139. procs.Sort()
  140. for _, p := range procs.Processes {
  141. mincpu := GetMinLoadCpu(cpus, p.Cpuset)
  142. mincpu.AddProcess(p)
  143. }
  144. return cpus
  145. }
  146. func GetProcessesCpuinfo(pids []string) ([]*ProcessCPUinfo, error) {
  147. if len(pids) == 0 {
  148. var err error
  149. pids, err = GetAllPids()
  150. if err != nil {
  151. log.Errorln(err)
  152. return nil, err
  153. }
  154. }
  155. cpu, err := GetSystemCpu()
  156. if err != nil {
  157. log.Errorln(err)
  158. return nil, err
  159. }
  160. coreCnt := len(cpu.DieList[0].CoreList)
  161. var ret = []*ProcessCPUinfo{}
  162. for _, pid := range pids {
  163. ipid, err := strconv.Atoi(pid)
  164. if err != nil {
  165. log.Errorln(err)
  166. return nil, err
  167. }
  168. info, err := NewProcessCPUinfo(ipid)
  169. if err != nil {
  170. log.Errorln(err)
  171. return nil, err
  172. }
  173. if info.Share != nil && *info.Share < float64(coreCnt) {
  174. ret = append(ret, info)
  175. }
  176. }
  177. return ret, nil
  178. }
  179. func GetAllPids() ([]string, error) {
  180. var pids = []string{}
  181. files, err := ioutil.ReadDir("/proc")
  182. if err != nil {
  183. return nil, err
  184. }
  185. re := regexp.MustCompile(`^\d+$`)
  186. for _, f := range files {
  187. if re.MatchString(f.Name()) {
  188. pids = append(pids, f.Name())
  189. }
  190. }
  191. return pids, nil
  192. }
  193. func FetchHistoryUtil() map[string][]float64 {
  194. if utilHistory == nil {
  195. utilHistory = make(map[string][]float64)
  196. if fileutils2.Exists(utilHistoryFile) {
  197. contents, err := fileutils2.FileGetContents(utilHistoryFile)
  198. if err != nil {
  199. log.Errorf("FetchHistoryUtil error: %s", err)
  200. return utilHistory
  201. }
  202. var objmap map[string]*json.RawMessage
  203. if err := json.Unmarshal([]byte(contents), &objmap); err != nil {
  204. log.Errorf("FetchHistoryUtil error: %s", err)
  205. return utilHistory
  206. }
  207. for k, v := range objmap {
  208. var s = []float64{}
  209. if err := json.Unmarshal(*v, &s); err != nil {
  210. log.Errorf("FetchHistoryUtil error: %s", err)
  211. break
  212. }
  213. utilHistory[k] = s
  214. }
  215. }
  216. }
  217. return utilHistory
  218. }
  219. func SaveHistoryUtil() {
  220. content, err := json.Marshal(utilHistory)
  221. if err != nil {
  222. log.Errorf("SaveHistoryUtil error: %s", err)
  223. } else {
  224. fileutils2.FilePutContents(utilHistoryFile, string(content), false)
  225. }
  226. }