Exemple #1
0
func cachedReadFile(path string) ([]byte, error) {
	key := []byte(path)
	if v, err := fileCache.Get(key); err == nil {
		metrics.IncrCounter(hitMetricsKey, 1.0)
		return v, nil
	}

	buf, err := fs.ReadFile(path)
	fileCache.Set(key, buf, generalTimeout)
	metrics.IncrCounter(missMetricsKey, 1.0)
	return buf, err
}
Exemple #2
0
func (p dir) ReadFile(path string) ([]byte, error) {
	if path == "/" {
		return nil, fmt.Errorf("I'm a directory!")
	}

	head, tail := split(path)
	fs, ok := p.entries[head]
	if !ok {
		return nil, fmt.Errorf("Not found: %s", path)
	}

	return fs.ReadFile(tail)
}
Exemple #3
0
func readStats(path string) (ppid, threads int, jiffies, rss, rssLimit uint64, err error) {
	var (
		buf                               []byte
		userJiffies, sysJiffies, rssPages uint64
	)
	buf, err = fs.ReadFile(path)
	if err != nil {
		return
	}
	splits := strings.Fields(string(buf))
	if len(splits) < 25 {
		err = fmt.Errorf("Invalid /proc/PID/stat")
		return
	}
	ppid, err = strconv.Atoi(splits[3])
	if err != nil {
		return
	}
	threads, err = strconv.Atoi(splits[19])
	if err != nil {
		return
	}
	userJiffies, err = strconv.ParseUint(splits[13], 10, 64)
	if err != nil {
		return
	}
	sysJiffies, err = strconv.ParseUint(splits[14], 10, 64)
	if err != nil {
		return
	}
	jiffies = userJiffies + sysJiffies
	rssPages, err = strconv.ParseUint(splits[23], 10, 64)
	if err != nil {
		return
	}
	rss = rssPages * uint64(os.Getpagesize())
	rssLimit, err = strconv.ParseUint(splits[24], 10, 64)
	return
}