Add ServerCPULoadInfo() and ServerMemUsageInfo() admin API (#7038)
parent
de1d39e436
commit
f3f47d8cd3
@ -0,0 +1,29 @@ |
|||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 cpu |
||||||
|
|
||||||
|
import ( |
||||||
|
"errors" |
||||||
|
) |
||||||
|
|
||||||
|
func newCounter() (counter, error) { |
||||||
|
return counter{}, errors.New("cpu metrics not implemented for darwin platform") |
||||||
|
} |
||||||
|
|
||||||
|
func (c counter) now() time.Time { |
||||||
|
return time.Time{} |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 cpu |
||||||
|
|
||||||
|
import ( |
||||||
|
"syscall" |
||||||
|
"time" |
||||||
|
"unsafe" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
// ProcessClock corresponds to the High-resolution per-process
|
||||||
|
// timer from the CPU represented in stdlib as CLOCK_PROCESS_CPUTIME_ID
|
||||||
|
processClock = 2 |
||||||
|
) |
||||||
|
|
||||||
|
func newCounter() (counter, error) { |
||||||
|
return counter{}, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (c counter) now() time.Time { |
||||||
|
var ts syscall.Timespec |
||||||
|
syscall.Syscall(syscall.SYS_CLOCK_GETTIME, processClock, uintptr(unsafe.Pointer(&ts)), 0) |
||||||
|
sec, nsec := ts.Unix() |
||||||
|
return time.Unix(sec, nsec) |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 cpu |
||||||
|
|
||||||
|
import ( |
||||||
|
"errors" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
func newCounter() (counter, error) { |
||||||
|
return counter{}, errors.New("cpu metrics not implemented for windows platform") |
||||||
|
} |
||||||
|
|
||||||
|
func (c counter) now() time.Time { |
||||||
|
return time.Time{} |
||||||
|
} |
@ -0,0 +1,91 @@ |
|||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 cpu |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"sync" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
// cpuLoadWindow is the interval of time for which the
|
||||||
|
// cpu utilization is measured
|
||||||
|
cpuLoadWindow = 200 * time.Millisecond |
||||||
|
|
||||||
|
// cpuLoadSampleSize is the number of samples measured
|
||||||
|
// for calculating cpu utilization
|
||||||
|
cpuLoadSampleSize = 3 |
||||||
|
|
||||||
|
// endOfTime represents the end of time
|
||||||
|
endOfTime = time.Duration(1<<63 - 1) |
||||||
|
) |
||||||
|
|
||||||
|
// Load holds CPU utilization % measured in three intervals of 200ms each
|
||||||
|
type Load struct { |
||||||
|
Avg string `json:"avg"` |
||||||
|
Max string `json:"max"` |
||||||
|
Min string `json:"min"` |
||||||
|
Error string `json:"error,omitempty"` |
||||||
|
} |
||||||
|
|
||||||
|
type counter struct{} |
||||||
|
|
||||||
|
// GetLoad returns the CPU utilization % of the current process
|
||||||
|
func GetLoad() Load { |
||||||
|
vals := make(chan time.Duration, 3) |
||||||
|
wg := sync.WaitGroup{} |
||||||
|
for i := 0; i < cpuLoadSampleSize; i++ { |
||||||
|
cpuCounter, err := newCounter() |
||||||
|
if err != nil { |
||||||
|
return Load{ |
||||||
|
Error: err.Error(), |
||||||
|
} |
||||||
|
} |
||||||
|
wg.Add(1) |
||||||
|
go func() { |
||||||
|
start := cpuCounter.now() |
||||||
|
time.Sleep(cpuLoadWindow) |
||||||
|
end := cpuCounter.now() |
||||||
|
vals <- end.Sub(start) |
||||||
|
wg.Done() |
||||||
|
}() |
||||||
|
} |
||||||
|
wg.Wait() |
||||||
|
|
||||||
|
sum := time.Duration(0) |
||||||
|
max := time.Duration(0) |
||||||
|
min := (endOfTime) |
||||||
|
for i := 0; i < cpuLoadSampleSize; i++ { |
||||||
|
val := <-vals |
||||||
|
sum = sum + val |
||||||
|
if val > max { |
||||||
|
max = val |
||||||
|
} |
||||||
|
if val < min { |
||||||
|
min = val |
||||||
|
} |
||||||
|
} |
||||||
|
close(vals) |
||||||
|
avg := sum / 3 |
||||||
|
return Load{ |
||||||
|
Avg: fmt.Sprintf("%.2f%%", toFixed4(float64(avg)/float64(200*time.Millisecond))*100), |
||||||
|
Max: fmt.Sprintf("%.2f%%", toFixed4(float64(max)/float64(200*time.Millisecond))*100), |
||||||
|
Min: fmt.Sprintf("%.2f%%", toFixed4(float64(min)/float64(200*time.Millisecond))*100), |
||||||
|
Error: "", |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 cpu |
||||||
|
|
||||||
|
import ( |
||||||
|
"math" |
||||||
|
) |
||||||
|
|
||||||
|
func round(num float64) int { |
||||||
|
return int(num + math.Copysign(0.5, num)) |
||||||
|
} |
||||||
|
|
||||||
|
func toFixed4(num float64) float64 { |
||||||
|
output := math.Pow(10, float64(4)) |
||||||
|
return float64(round(num*output)) / output |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 main |
||||||
|
|
||||||
|
import ( |
||||||
|
"log" |
||||||
|
|
||||||
|
"github.com/minio/minio/pkg/madmin" |
||||||
|
) |
||||||
|
|
||||||
|
func main() { |
||||||
|
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
|
||||||
|
// dummy values, please replace them with original values.
|
||||||
|
|
||||||
|
// API requests are secure (HTTPS) if secure=true and insecure (HTTPS) otherwise.
|
||||||
|
// New returns an Minio Admin client object.
|
||||||
|
madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true) |
||||||
|
if err != nil { |
||||||
|
log.Fatalln(err) |
||||||
|
} |
||||||
|
|
||||||
|
st, err := madmClnt.ServerCPULoadInfo() |
||||||
|
if err != nil { |
||||||
|
log.Fatalln(err) |
||||||
|
} |
||||||
|
log.Println(st) |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
// +build ignore
|
||||||
|
|
||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 main |
||||||
|
|
||||||
|
import ( |
||||||
|
"log" |
||||||
|
|
||||||
|
"github.com/minio/minio/pkg/madmin" |
||||||
|
) |
||||||
|
|
||||||
|
func main() { |
||||||
|
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY and my-bucketname are
|
||||||
|
// dummy values, please replace them with original values.
|
||||||
|
|
||||||
|
// API requests are secure (HTTPS) if secure=true and insecure (HTTPS) otherwise.
|
||||||
|
// New returns an Minio Admin client object.
|
||||||
|
madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true) |
||||||
|
if err != nil { |
||||||
|
log.Fatalln(err) |
||||||
|
} |
||||||
|
|
||||||
|
st, err := madmClnt.ServerMemUsageInfo() |
||||||
|
if err != nil { |
||||||
|
log.Fatalln(err) |
||||||
|
} |
||||||
|
log.Println(st) |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
/* |
||||||
|
* Minio Cloud Storage, (C) 2019 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 mem |
||||||
|
|
||||||
|
import ( |
||||||
|
"runtime" |
||||||
|
|
||||||
|
humanize "github.com/dustin/go-humanize" |
||||||
|
) |
||||||
|
|
||||||
|
// Usage holds memory utilization information in human readable format
|
||||||
|
type Usage struct { |
||||||
|
Mem string `json:"mem"` |
||||||
|
Error string `json:"error,omitempty"` |
||||||
|
} |
||||||
|
|
||||||
|
// GetUsage measures the total memory provisioned for the current process
|
||||||
|
// from the OS
|
||||||
|
func GetUsage() Usage { |
||||||
|
memStats := new(runtime.MemStats) |
||||||
|
runtime.ReadMemStats(memStats) |
||||||
|
return Usage{ |
||||||
|
Mem: humanize.IBytes(memStats.Sys), |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue