mem_topo.go 4.6 KB
Newer Older
D
dogsheng 已提交
1 2
/*
 * Copyright (c) 2019 Huawei Technologies Co., Ltd.
3 4 5
 * A-Tune is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
6
 *     http://license.coscl.org.cn/MulanPSL2
D
dogsheng 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
D
dogsheng 已提交
11 12 13 14 15 16 17
 * Create: 2019-10-29
 */

package checker

import (
	PB "atune/api/profile"
Z
Zhipeng Xie 已提交
18
	"atune/common/config"
D
dogsheng 已提交
19
	"atune/common/log"
Z
Zhipeng Xie 已提交
20 21
	"atune/common/models"
	"atune/common/registry"
D
dogsheng 已提交
22 23 24 25 26
	"atune/common/utils"
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
Z
Zhipeng Xie 已提交
27
	"path"
D
dogsheng 已提交
28 29 30 31
	"regexp"
	"strconv"
)

Z
Zhipeng Xie 已提交
32 33 34 35 36 37 38
func init() {
	registry.RegisterCheckerService("mem_topo", &MemTopo{
		Path:           path.Join(config.DefaultCheckerPath, "mem_topo.xml"),
		DisableChecker: true,
	})
}

D
dogsheng 已提交
39 40
// MemTopo represent the memory topology type
type MemTopo struct {
Z
Zhipeng Xie 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	Path           string
	DisableChecker bool
}

// Init Mem
func (m *MemTopo) Init() error {
	exist, err := utils.PathExist(m.Path)
	if err != nil {
		return err
	}
	if exist {
		return nil
	}
	if _, err := models.MonitorGet("mem", "topo", "xml", m.Path, ""); err != nil {
		return err
	}

	return nil
}

// IsCheckDisabled method disable check method when run check command
func (m *MemTopo) IsCheckDisabled() bool {
	return m.DisableChecker
D
dogsheng 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

type children struct {
	ID          string `xml:"id"`
	Class       string `xml:"class"`
	Claimed     bool   `xml:"claimed"`
	Handle      string `xml:"handle"`
	Description string `xml:"description"`
	Product     string `xml:"product"`
	Vendor      string `xml:"vendor"`
	Physid      string `xml:"physid"`
	Serial      string `xml:"serial"`
	Slot        string `xml:"slot"`
	Units       string `xml:"units"`
	Size        int64  `xml:"size"`
	Width       int    `xml:"width"`
	Clock       int64  `xml:"clock"`
}

type memorysInfo struct {
	ID        string     `xml:"id"`
	Physid    string     `xml:"physid"`
	Childrens []children `xml:"children"`
}

type topology struct {
	XMLName xml.Name      `xml:"topology"`
	Memorys []memorysInfo `xml:"memorys"`
}

/*
Check method check the memory topolog, whether the memory interpolation is balanced.
*/
func (m *MemTopo) Check(ch chan *PB.AckCheck) error {
	file, err := os.Open(m.Path)
	if err != nil {
		return err
	}

	defer file.Close()

	data, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}

	topology := topology{}
	err = xml.Unmarshal(data, &topology)
	if err != nil {
		return err
	}

	reg := regexp.MustCompile(`DIMM.*?(\d)(\d)(\d)\s.*`)
	memNum := 0

	maxSocket := 0
	maxChannel := 0
	maxSlot := 0

	for _, memory := range topology.Memorys {
		for _, child := range memory.Childrens {
			if params := reg.FindStringSubmatch(child.Slot); params != nil {
				socket, _ := strconv.Atoi(params[1])
				channel, _ := strconv.Atoi(params[2])
				slot, _ := strconv.Atoi(params[3])
				if socket > maxSocket {
					maxSocket = socket
				}
				if channel > maxChannel {
					maxChannel = channel
				}
				if slot > maxSlot {
					maxSlot = slot
				}
			}
		}
	}

	memTotal := (maxSocket + 1) * (maxChannel + 1) * (maxSlot + 1)
	memLocation := make([]bool, memTotal)

	for _, memory := range topology.Memorys {
		for _, child := range memory.Childrens {
			if child.Size != 0 {
				if params := reg.FindStringSubmatch(child.Slot); params != nil {
					socket, _ := strconv.Atoi(params[1])
					channel, _ := strconv.Atoi(params[2])
					slot, _ := strconv.Atoi(params[3])
					index := socket*(maxChannel+1)*(maxSlot+1) + channel*(maxSlot+1) + slot
					memLocation[index] = true
				}

				memNum++
			}
		}
	}

	log.Infof("memory total num is : %d", memNum)

	if memNum == memTotal {
Z
Zhipeng Xie 已提交
164
		sendChanToAdm(ch, "memory", utils.SUCCESS, fmt.Sprintf("memory num is %d, the memory slot is full", memNum))
D
dogsheng 已提交
165 166 167 168
		return nil
	}

	if memNum%(maxChannel+1) != 0 {
169
		sendChanToAdm(ch, "memory", utils.SUGGEST, fmt.Sprintf("memory num is %d, not recommend, recommand 8,16 or 32", memNum))
D
dogsheng 已提交
170 171 172 173 174 175 176
		return nil
	}

	memHalf := memTotal / 2

	for i := 0; i < memHalf; i++ {
		if memLocation[i] != memLocation[i+memHalf] {
177 178
			sendChanToAdm(ch, "memory", utils.SUGGEST,
				fmt.Sprintf("memory location is not balanced, recommand to balance memory location"))
D
dogsheng 已提交
179 180 181
			return nil
		}
	}
Z
Zhipeng Xie 已提交
182
	sendChanToAdm(ch, "memory", utils.SUCCESS, fmt.Sprintf("memory num is %d, memory interpolation is correct", memNum))
D
dogsheng 已提交
183 184 185 186 187 188 189 190 191 192
	return nil
}

func sendChanToAdm(ch chan *PB.AckCheck, item string, status string, description string) {
	if ch == nil {
		return
	}

	ch <- &PB.AckCheck{Name: item, Status: status, Description: description}
}