2015-12-07 17:12:47 -05:00
|
|
|
/*
|
2020-03-12 21:57:41 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018-2020 MinIO, Inc.
|
2015-12-07 17:12:47 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-17 22:09:43 -04:00
|
|
|
package disk
|
|
|
|
|
2018-12-31 12:46:44 -05:00
|
|
|
import (
|
|
|
|
"os"
|
2019-03-05 20:28:14 -05:00
|
|
|
)
|
2018-12-31 12:46:44 -05:00
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
// Info stat fs struct is container which holds following values
|
2015-10-17 22:09:43 -04:00
|
|
|
// Total - total size of the volume / disk
|
|
|
|
// Free - free size of the volume / disk
|
2017-08-03 23:07:22 -04:00
|
|
|
// Files - total inodes available
|
|
|
|
// Ffree - free inodes available
|
|
|
|
// FSType - file system type
|
2016-01-25 02:03:38 -05:00
|
|
|
type Info struct {
|
2017-08-03 23:07:22 -04:00
|
|
|
Total uint64
|
|
|
|
Free uint64
|
|
|
|
Files uint64
|
|
|
|
Ffree uint64
|
2015-10-17 22:09:43 -04:00
|
|
|
FSType string
|
2018-05-23 06:11:29 -04:00
|
|
|
|
|
|
|
// Usage is calculated per tenant.
|
|
|
|
Usage uint64
|
2015-10-17 22:09:43 -04:00
|
|
|
}
|
2018-12-31 12:46:44 -05:00
|
|
|
|
|
|
|
// Performance holds informantion about read and write speed of a disk
|
|
|
|
type Performance struct {
|
|
|
|
Path string `json:"path"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
WriteSpeed float64 `json:"writeSpeed"`
|
|
|
|
ReadSpeed float64 `json:"readSpeed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPerformance returns given disk's read and write performance
|
2019-09-12 17:52:30 -04:00
|
|
|
func GetPerformance(path string, size int64) Performance {
|
2018-12-31 12:46:44 -05:00
|
|
|
perf := Performance{}
|
2019-09-12 17:52:30 -04:00
|
|
|
write, read, err := doPerfMeasure(path, size)
|
2018-12-31 12:46:44 -05:00
|
|
|
if err != nil {
|
|
|
|
perf.Error = err.Error()
|
|
|
|
return perf
|
|
|
|
}
|
|
|
|
perf.WriteSpeed = write
|
|
|
|
perf.ReadSpeed = read
|
|
|
|
return perf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the write and read performance - write and read 10 tmp (1 MiB)
|
|
|
|
// files and find the average time taken (Bytes / Sec)
|
2019-09-12 17:52:30 -04:00
|
|
|
func doPerfMeasure(fsPath string, size int64) (writeSpeed, readSpeed float64, err error) {
|
|
|
|
// Remove the file created for speed test purposes
|
2018-12-31 12:46:44 -05:00
|
|
|
defer os.RemoveAll(fsPath)
|
|
|
|
|
2019-09-12 17:52:30 -04:00
|
|
|
// Create a file with O_DIRECT flag
|
|
|
|
w, err := OpenFileDirectIO(fsPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0666)
|
2018-12-31 12:46:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
2019-09-12 17:52:30 -04:00
|
|
|
// Fetch aligned buf for direct-io
|
2020-03-12 21:57:41 -04:00
|
|
|
buf := AlignedBlock(speedTestBlockSize)
|
2018-12-31 12:46:44 -05:00
|
|
|
|
2019-09-12 17:52:30 -04:00
|
|
|
writeSpeed, err = speedTestWrite(w, buf, size)
|
|
|
|
w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2018-12-31 12:46:44 -05:00
|
|
|
|
2019-09-12 17:52:30 -04:00
|
|
|
// Open file to compute read speed
|
|
|
|
r, err := OpenFileDirectIO(fsPath, os.O_RDONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
2018-12-31 12:46:44 -05:00
|
|
|
}
|
2019-09-12 17:52:30 -04:00
|
|
|
defer r.Close()
|
2018-12-31 12:46:44 -05:00
|
|
|
|
2019-09-12 17:52:30 -04:00
|
|
|
readSpeed, err = speedTestRead(r, buf, size)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2018-12-31 12:46:44 -05:00
|
|
|
|
2019-09-12 17:52:30 -04:00
|
|
|
return writeSpeed, readSpeed, nil
|
2018-12-31 12:46:44 -05:00
|
|
|
}
|