2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-03-23 17:51:27 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-07-05 17:45:49 -04:00
|
|
|
"sync/atomic"
|
2021-03-23 17:51:27 -04:00
|
|
|
"time"
|
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/disk"
|
2022-07-05 17:45:49 -04:00
|
|
|
ioutilx "github.com/minio/minio/internal/ioutil"
|
2021-03-23 17:51:27 -04:00
|
|
|
)
|
|
|
|
|
2021-03-27 02:24:07 -04:00
|
|
|
//go:generate stringer -type=osMetric -trimprefix=osMetric $GOFILE
|
|
|
|
|
|
|
|
type osMetric uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
osMetricRemoveAll osMetric = iota
|
|
|
|
osMetricMkdirAll
|
2022-07-24 03:43:11 -04:00
|
|
|
osMetricMkdir
|
2021-03-27 02:24:07 -04:00
|
|
|
osMetricRename
|
2022-08-01 16:22:43 -04:00
|
|
|
osMetricOpenFileW
|
|
|
|
osMetricOpenFileR
|
2024-01-10 11:48:50 -05:00
|
|
|
osMetricOpenFileWFd
|
|
|
|
osMetricOpenFileRFd
|
2021-03-27 02:24:07 -04:00
|
|
|
osMetricOpen
|
|
|
|
osMetricOpenFileDirectIO
|
|
|
|
osMetricLstat
|
|
|
|
osMetricRemove
|
|
|
|
osMetricStat
|
2021-03-29 11:07:23 -04:00
|
|
|
osMetricAccess
|
2022-07-05 17:45:49 -04:00
|
|
|
osMetricCreate
|
|
|
|
osMetricReadDirent
|
|
|
|
osMetricFdatasync
|
|
|
|
osMetricSync
|
2023-08-27 12:57:11 -04:00
|
|
|
osMetricRename2 // Linux specific
|
2021-03-27 02:24:07 -04:00
|
|
|
// .... add more
|
|
|
|
|
|
|
|
osMetricLast
|
2021-03-23 17:51:27 -04:00
|
|
|
)
|
|
|
|
|
2022-07-05 17:45:49 -04:00
|
|
|
var globalOSMetrics osMetrics
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Inject metrics.
|
|
|
|
ioutilx.OsOpenFile = OpenFile
|
|
|
|
ioutilx.OpenFileDirectIO = OpenFileDirectIO
|
|
|
|
ioutilx.OsOpen = Open
|
|
|
|
}
|
|
|
|
|
|
|
|
type osMetrics struct {
|
|
|
|
// All fields must be accessed atomically and aligned.
|
|
|
|
operations [osMetricLast]uint64
|
|
|
|
latency [osMetricLast]lockedLastMinuteLatency
|
|
|
|
}
|
|
|
|
|
|
|
|
// time an os action.
|
|
|
|
func (o *osMetrics) time(s osMetric) func() {
|
|
|
|
startTime := time.Now()
|
|
|
|
return func() {
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
|
|
|
|
atomic.AddUint64(&o.operations[s], 1)
|
|
|
|
o.latency[s].add(duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// incTime will increment time on metric s with a specific duration.
|
|
|
|
func (o *osMetrics) incTime(s osMetric, d time.Duration) {
|
|
|
|
atomic.AddUint64(&o.operations[s], 1)
|
|
|
|
o.latency[s].add(d)
|
|
|
|
}
|
|
|
|
|
2023-08-27 12:57:11 -04:00
|
|
|
func osTrace(s osMetric, startTime time.Time, duration time.Duration, path string, err error) madmin.TraceInfo {
|
|
|
|
var errStr string
|
|
|
|
if err != nil {
|
|
|
|
errStr = err.Error()
|
|
|
|
}
|
2021-05-06 11:52:02 -04:00
|
|
|
return madmin.TraceInfo{
|
|
|
|
TraceType: madmin.TraceOS,
|
2021-03-27 02:24:07 -04:00
|
|
|
Time: startTime,
|
|
|
|
NodeName: globalLocalNodeName,
|
2021-03-27 13:07:07 -04:00
|
|
|
FuncName: "os." + s.String(),
|
2022-07-05 17:45:49 -04:00
|
|
|
Duration: duration,
|
|
|
|
Path: path,
|
2023-08-27 12:57:11 -04:00
|
|
|
Error: errStr,
|
2021-03-27 02:24:07 -04:00
|
|
|
}
|
2021-03-23 17:51:27 -04:00
|
|
|
}
|
|
|
|
|
2023-08-27 12:57:11 -04:00
|
|
|
func updateOSMetrics(s osMetric, paths ...string) func(err error) {
|
2022-07-05 17:45:49 -04:00
|
|
|
if globalTrace.NumSubscribers(madmin.TraceOS) == 0 {
|
2023-10-12 03:08:13 -04:00
|
|
|
osAction := globalOSMetrics.time(s)
|
|
|
|
return func(err error) { osAction() }
|
2021-03-27 02:24:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
2023-08-27 12:57:11 -04:00
|
|
|
return func(err error) {
|
2021-03-27 02:24:07 -04:00
|
|
|
duration := time.Since(startTime)
|
2022-07-05 17:45:49 -04:00
|
|
|
globalOSMetrics.incTime(s, duration)
|
2023-08-27 12:57:11 -04:00
|
|
|
globalTrace.Publish(osTrace(s, startTime, duration, strings.Join(paths, " -> "), err))
|
2021-03-23 17:51:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll captures time taken to call the underlying os.RemoveAll
|
2023-08-27 12:57:11 -04:00
|
|
|
func RemoveAll(dirPath string) (err error) {
|
|
|
|
defer updateOSMetrics(osMetricRemoveAll, dirPath)(err)
|
2021-03-23 17:51:27 -04:00
|
|
|
return os.RemoveAll(dirPath)
|
|
|
|
}
|
|
|
|
|
2022-07-24 03:43:11 -04:00
|
|
|
// Mkdir captures time taken to call os.Mkdir
|
2023-08-27 12:57:11 -04:00
|
|
|
func Mkdir(dirPath string, mode os.FileMode) (err error) {
|
|
|
|
defer updateOSMetrics(osMetricMkdir, dirPath)(err)
|
2022-07-24 03:43:11 -04:00
|
|
|
return os.Mkdir(dirPath, mode)
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:51:27 -04:00
|
|
|
// MkdirAll captures time taken to call os.MkdirAll
|
2023-09-13 11:14:36 -04:00
|
|
|
func MkdirAll(dirPath string, mode os.FileMode, baseDir string) (err error) {
|
2023-08-27 12:57:11 -04:00
|
|
|
defer updateOSMetrics(osMetricMkdirAll, dirPath)(err)
|
2023-09-13 11:14:36 -04:00
|
|
|
return osMkdirAll(dirPath, mode, baseDir)
|
2021-03-23 17:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename captures time taken to call os.Rename
|
2023-08-27 12:57:11 -04:00
|
|
|
func Rename(src, dst string) (err error) {
|
|
|
|
defer updateOSMetrics(osMetricRename, src, dst)(err)
|
2023-09-05 23:22:23 -04:00
|
|
|
return RenameSys(src, dst)
|
2021-03-23 17:51:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile captures time taken to call os.OpenFile
|
2023-08-27 12:57:11 -04:00
|
|
|
func OpenFile(name string, flag int, perm os.FileMode) (f *os.File, err error) {
|
2022-08-01 16:22:43 -04:00
|
|
|
switch flag & writeMode {
|
|
|
|
case writeMode:
|
2023-08-27 12:57:11 -04:00
|
|
|
defer updateOSMetrics(osMetricOpenFileW, name)(err)
|
2022-08-01 16:22:43 -04:00
|
|
|
default:
|
2023-08-27 12:57:11 -04:00
|
|
|
defer updateOSMetrics(osMetricOpenFileR, name)(err)
|
2022-08-01 16:22:43 -04:00
|
|
|
}
|
2021-03-23 17:51:27 -04:00
|
|
|
return os.OpenFile(name, flag, perm)
|
|
|
|
}
|
|
|
|
|
2021-03-29 11:07:23 -04:00
|
|
|
// Access captures time taken to call syscall.Access()
|
|
|
|
// on windows, plan9 and solaris syscall.Access uses
|
|
|
|
// os.Lstat()
|
2023-08-27 12:57:11 -04:00
|
|
|
func Access(name string) (err error) {
|
|
|
|
defer updateOSMetrics(osMetricAccess, name)(err)
|
2021-03-29 11:07:23 -04:00
|
|
|
return access(name)
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:51:27 -04:00
|
|
|
// Open captures time taken to call os.Open
|
2023-08-27 12:57:11 -04:00
|
|
|
func Open(name string) (f *os.File, err error) {
|
|
|
|
defer updateOSMetrics(osMetricOpen, name)(err)
|
2021-03-23 17:51:27 -04:00
|
|
|
return os.Open(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFileDirectIO captures time taken to call disk.OpenFileDirectIO
|
2023-08-27 12:57:11 -04:00
|
|
|
func OpenFileDirectIO(name string, flag int, perm os.FileMode) (f *os.File, err error) {
|
|
|
|
defer updateOSMetrics(osMetricOpenFileDirectIO, name)(err)
|
2021-03-23 17:51:27 -04:00
|
|
|
return disk.OpenFileDirectIO(name, flag, perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lstat captures time taken to call os.Lstat
|
2023-08-27 12:57:11 -04:00
|
|
|
func Lstat(name string) (info os.FileInfo, err error) {
|
|
|
|
defer updateOSMetrics(osMetricLstat, name)(err)
|
2021-03-23 17:51:27 -04:00
|
|
|
return os.Lstat(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove captures time taken to call os.Remove
|
2023-08-27 12:57:11 -04:00
|
|
|
func Remove(deletePath string) (err error) {
|
|
|
|
defer updateOSMetrics(osMetricRemove, deletePath)(err)
|
2021-03-23 17:51:27 -04:00
|
|
|
return os.Remove(deletePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat captures time taken to call os.Stat
|
2023-08-27 12:57:11 -04:00
|
|
|
func Stat(name string) (info os.FileInfo, err error) {
|
|
|
|
defer updateOSMetrics(osMetricStat, name)(err)
|
2021-03-23 17:51:27 -04:00
|
|
|
return os.Stat(name)
|
|
|
|
}
|
2022-07-05 17:45:49 -04:00
|
|
|
|
|
|
|
// Create captures time taken to call os.Create
|
2023-08-27 12:57:11 -04:00
|
|
|
func Create(name string) (f *os.File, err error) {
|
|
|
|
defer updateOSMetrics(osMetricCreate, name)(err)
|
2022-07-05 17:45:49 -04:00
|
|
|
return os.Create(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fdatasync captures time taken to call Fdatasync
|
2023-08-27 12:57:11 -04:00
|
|
|
func Fdatasync(f *os.File) (err error) {
|
2022-07-05 17:45:49 -04:00
|
|
|
fn := ""
|
|
|
|
if f != nil {
|
|
|
|
fn = f.Name()
|
|
|
|
}
|
2023-08-27 12:57:11 -04:00
|
|
|
defer updateOSMetrics(osMetricFdatasync, fn)(err)
|
2022-07-05 17:45:49 -04:00
|
|
|
return disk.Fdatasync(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// report returns all os metrics.
|
|
|
|
func (o *osMetrics) report() madmin.OSMetrics {
|
|
|
|
var m madmin.OSMetrics
|
|
|
|
m.CollectedAt = time.Now()
|
|
|
|
m.LifeTimeOps = make(map[string]uint64, osMetricLast)
|
|
|
|
for i := osMetric(0); i < osMetricLast; i++ {
|
|
|
|
if n := atomic.LoadUint64(&o.operations[i]); n > 0 {
|
|
|
|
m.LifeTimeOps[i.String()] = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m.LifeTimeOps) == 0 {
|
|
|
|
m.LifeTimeOps = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.LastMinute.Operations = make(map[string]madmin.TimedAction, osMetricLast)
|
|
|
|
for i := osMetric(0); i < osMetricLast; i++ {
|
|
|
|
lm := o.latency[i].total()
|
|
|
|
if lm.N > 0 {
|
|
|
|
m.LastMinute.Operations[i.String()] = lm.asTimedAction()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(m.LastMinute.Operations) == 0 {
|
|
|
|
m.LastMinute.Operations = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|