2020-03-27 05:07:39 +01:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package disk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2020-04-13 04:37:09 +02:00
|
|
|
"github.com/dustin/go-humanize"
|
2020-03-27 05:07:39 +01:00
|
|
|
"github.com/montanaflynn/stats"
|
|
|
|
)
|
|
|
|
|
|
|
|
var globalLatency = map[string]Latency{}
|
|
|
|
var globalThroughput = map[string]Throughput{}
|
|
|
|
|
|
|
|
// Latency holds latency information for write operations to the drive
|
|
|
|
type Latency struct {
|
|
|
|
Avg float64 `json:"avg_secs,omitempty"`
|
|
|
|
Percentile50 float64 `json:"percentile50_secs,omitempty"`
|
|
|
|
Percentile90 float64 `json:"percentile90_secs,omitempty"`
|
|
|
|
Percentile99 float64 `json:"percentile99_secs,omitempty"`
|
|
|
|
Min float64 `json:"min_secs,omitempty"`
|
|
|
|
Max float64 `json:"max_secs,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throughput holds throughput information for write operations to the drive
|
|
|
|
type Throughput struct {
|
|
|
|
Avg float64 `json:"avg_bytes_per_sec,omitempty"`
|
|
|
|
Percentile50 float64 `json:"percentile50_bytes_per_sec,omitempty"`
|
|
|
|
Percentile90 float64 `json:"percentile90_bytes_per_sec,omitempty"`
|
|
|
|
Percentile99 float64 `json:"percentile99_bytes_per_sec,omitempty"`
|
|
|
|
Min float64 `json:"min_bytes_per_sec,omitempty"`
|
|
|
|
Max float64 `json:"max_bytes_per_sec,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOBDInfo about the drive
|
2020-04-14 20:48:32 +02:00
|
|
|
func GetOBDInfo(ctx context.Context, drive, fsPath string) (Latency, Throughput, error) {
|
2020-03-27 05:07:39 +01:00
|
|
|
|
2020-04-13 04:37:09 +02:00
|
|
|
// Create a file with O_DIRECT flag, choose default umask and also make sure
|
|
|
|
// we are exclusively writing to a new file using O_EXCL.
|
|
|
|
w, err := OpenFileDirectIO(fsPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0666)
|
2020-03-27 05:07:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
2020-04-13 04:37:09 +02:00
|
|
|
|
2020-03-27 05:07:39 +01:00
|
|
|
defer func() {
|
2020-04-13 04:37:09 +02:00
|
|
|
w.Close()
|
|
|
|
os.Remove(fsPath)
|
2020-03-27 05:07:39 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
// going to leave this here incase we decide to go back to caching again
|
|
|
|
// if gl, ok := globalLatency[drive]; ok {
|
|
|
|
// if gt, ok := globalThroughput[drive]; ok {
|
|
|
|
// return gl, gt, nil
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2020-04-13 04:37:09 +02:00
|
|
|
blockSize := 4 * humanize.MiByte
|
|
|
|
fileSize := 256 * humanize.MiByte
|
2020-03-27 05:07:39 +01:00
|
|
|
|
|
|
|
latencies := make([]float64, fileSize/blockSize)
|
|
|
|
throughputs := make([]float64, fileSize/blockSize)
|
|
|
|
|
2020-04-13 04:37:09 +02:00
|
|
|
data := AlignedBlock(blockSize)
|
2020-03-27 05:07:39 +01:00
|
|
|
|
2020-04-13 04:37:09 +02:00
|
|
|
for i := 0; i < (fileSize / blockSize); i++ {
|
2020-03-27 05:07:39 +01:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return Latency{}, Throughput{}, ctx.Err()
|
|
|
|
}
|
|
|
|
startTime := time.Now()
|
2020-04-13 04:37:09 +02:00
|
|
|
if n, err := w.Write(data); err != nil {
|
2020-03-27 05:07:39 +01:00
|
|
|
return Latency{}, Throughput{}, err
|
2020-04-13 04:37:09 +02:00
|
|
|
} else if n != blockSize {
|
2020-03-27 05:07:39 +01:00
|
|
|
return Latency{}, Throughput{}, fmt.Errorf("Expected to write %d, but only wrote %d", blockSize, n)
|
|
|
|
}
|
2020-04-13 04:37:09 +02:00
|
|
|
latencyInSecs := time.Since(startTime).Seconds()
|
|
|
|
latencies[i] = float64(latencyInSecs)
|
2020-03-27 05:07:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range latencies {
|
|
|
|
throughput := float64(blockSize) / latencies[i]
|
|
|
|
throughputs[i] = throughput
|
|
|
|
}
|
|
|
|
|
|
|
|
var avgLatency float64
|
|
|
|
var percentile50Latency float64
|
|
|
|
var percentile90Latency float64
|
|
|
|
var percentile99Latency float64
|
|
|
|
var minLatency float64
|
|
|
|
var maxLatency float64
|
|
|
|
|
|
|
|
var avgThroughput float64
|
|
|
|
var percentile50Throughput float64
|
|
|
|
var percentile90Throughput float64
|
|
|
|
var percentile99Throughput float64
|
|
|
|
var minThroughput float64
|
|
|
|
var maxThroughput float64
|
|
|
|
|
|
|
|
if avgLatency, err = stats.Mean(latencies); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if percentile50Latency, err = stats.Percentile(latencies, 50); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if percentile90Latency, err = stats.Percentile(latencies, 90); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if percentile99Latency, err = stats.Percentile(latencies, 99); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if maxLatency, err = stats.Max(latencies); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if minLatency, err = stats.Min(latencies); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
l := Latency{
|
|
|
|
Avg: avgLatency,
|
|
|
|
Percentile50: percentile50Latency,
|
|
|
|
Percentile90: percentile90Latency,
|
|
|
|
Percentile99: percentile99Latency,
|
|
|
|
Min: minLatency,
|
|
|
|
Max: maxLatency,
|
|
|
|
}
|
|
|
|
|
|
|
|
if avgThroughput, err = stats.Mean(throughputs); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if percentile50Throughput, err = stats.Percentile(throughputs, 50); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if percentile90Throughput, err = stats.Percentile(throughputs, 90); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if percentile99Throughput, err = stats.Percentile(throughputs, 99); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if maxThroughput, err = stats.Max(throughputs); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
if minThroughput, err = stats.Min(throughputs); err != nil {
|
|
|
|
return Latency{}, Throughput{}, err
|
|
|
|
}
|
|
|
|
t := Throughput{
|
|
|
|
Avg: avgThroughput,
|
|
|
|
Percentile50: percentile50Throughput,
|
|
|
|
Percentile90: percentile90Throughput,
|
|
|
|
Percentile99: percentile99Throughput,
|
|
|
|
Min: minThroughput,
|
|
|
|
Max: maxThroughput,
|
|
|
|
}
|
|
|
|
|
|
|
|
globalLatency[drive] = l
|
|
|
|
globalThroughput[drive] = t
|
|
|
|
|
|
|
|
return l, t, nil
|
|
|
|
}
|