func fillStatStruct()

in metric/system/cgroup/cgv2/memory.go [272:319]


func fillStatStruct(path string) (MemoryStat, error) {
	statPath := filepath.Join(path, "memory.stat")
	raw, err := os.ReadFile(statPath)
	if err != nil {
		return MemoryStat{}, fmt.Errorf("error reading memory.stat: %w", err)
	}

	stats := MemoryStat{}
	refValues := reflect.ValueOf(&stats).Elem()
	refTypes := reflect.TypeOf(stats)

	sc := bufio.NewScanner(bytes.NewReader(raw))
	for sc.Scan() {
		//break apart the lines
		parts := bytes.SplitN(sc.Bytes(), []byte(" "), 2)
		if len(parts) != 2 {
			continue
		}
		intVal, err := cgcommon.ParseUint(parts[1])
		if err != nil {
			return stats, fmt.Errorf("error parsing value %v: %w", parts[1], err)
		}
		for i := 0; i < refValues.NumField(); i++ {
			idxVal := refValues.Field(i)
			idxType := refTypes.Field(i)
			tagStr := idxType.Tag.Get("orig")
			if tagStr == string(parts[0]) {
				if idxVal.CanSet() {
					if idxVal.Kind() == reflect.Uint64 {
						idxVal.SetUint(intVal)
					} else if idxType.Type == reflect.TypeOf(opt.Bytes{}) {
						byteVal := opt.Bytes{Bytes: intVal}
						byteRef := reflect.ValueOf(byteVal)
						idxVal.Set(byteRef)
					} else if idxType.Type == reflect.TypeOf(opt.BytesOpt{}) {
						byteVal := opt.BytesOpt{Bytes: opt.UintWith(intVal)}
						byteRef := reflect.ValueOf(byteVal)
						idxVal.Set(byteRef)
					}

				}
			}
		}

	}

	return stats, nil
}