github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/bench/microbenchmarks/disk/compare/diskstat.go (about)

     1  // Package main
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package main
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"os"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  
    15  	"github.com/NVIDIA/aistore/cmn/nlog"
    16  )
    17  
    18  type DiskStat struct {
    19  	// based on https://www.kernel.org/doc/Documentation/iostats.txt
    20  	ReadComplete  int64 // 1 - # of reads completed
    21  	ReadMerged    int64 // 2 - # of reads merged
    22  	ReadSectors   int64 // 3 - # of sectors read
    23  	ReadMs        int64 // 4 - # ms spent reading
    24  	WriteComplete int64 // 5 - # writes completed
    25  	WriteMerged   int64 // 6 - # writes merged
    26  	WriteSectors  int64 // 7 - # of sectors written
    27  	WriteMs       int64 // 8 - # of milliseconds spent writing
    28  	IOPending     int64 // 9 - # of I/Os currently in progress
    29  	IOMs          int64 // 10 - # of milliseconds spent doing I/Os
    30  	IOMsWeighted  int64 // 11 - weighted # of milliseconds spent doing I/Os
    31  }
    32  
    33  func timeTrack(start time.Time, name string) {
    34  	elapsed := time.Since(start)
    35  	fmt.Println(strings.Join([]string{time.Now().Format(time.RFC3339Nano), name,
    36  		strconv.FormatInt(elapsed.Nanoseconds(), 10)}, ","))
    37  }
    38  
    39  func GetDiskstats() (output map[string]DiskStat) {
    40  	defer timeTrack(time.Now(), "Get DiskStats Duration")
    41  
    42  	output = make(map[string]DiskStat)
    43  
    44  	file, err := os.Open("/proc/diskstats")
    45  	if err != nil {
    46  		nlog.Errorln(err)
    47  		return
    48  	}
    49  
    50  	scanner := bufio.NewScanner(file)
    51  
    52  	for scanner.Scan() {
    53  		line := scanner.Text()
    54  		if line == "" {
    55  			continue
    56  		}
    57  
    58  		fields := strings.Fields(line)
    59  		if len(fields) < 14 {
    60  			continue
    61  		}
    62  
    63  		deviceName := fields[2]
    64  		output[deviceName] = DiskStat{
    65  			_exI64(fields[3]),
    66  			_exI64(fields[4]),
    67  			_exI64(fields[5]),
    68  			_exI64(fields[6]),
    69  			_exI64(fields[7]),
    70  			_exI64(fields[8]),
    71  			_exI64(fields[9]),
    72  			_exI64(fields[10]),
    73  			_exI64(fields[11]),
    74  			_exI64(fields[12]),
    75  			_exI64(fields[13]),
    76  		}
    77  	}
    78  
    79  	return output
    80  }
    81  
    82  func _exI64(field string) int64 {
    83  	val, err := strconv.ParseInt(field, 10, 64)
    84  	if err != nil {
    85  		nlog.Errorf("Failed to convert field value %q to int: %v \n",
    86  			field, err)
    87  		return 0
    88  	}
    89  	return val
    90  }