Skip to content

Commit

Permalink
Merge pull request #5 from TencentBlueKing/sync-upstream
Browse files Browse the repository at this point in the history
Sync upstream
  • Loading branch information
chenjiandongx authored May 5, 2023
2 parents 3edd174 + e045dc7 commit dbf5afe
Show file tree
Hide file tree
Showing 41 changed files with 280 additions and 89 deletions.
13 changes: 13 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Security Policy

## Supported Versions

Security updates are applied only to the latest release.

## Reporting a Vulnerability

If you have discovered a security vulnerability in this project, please report it privately. **Do not disclose it as a public issue.** This gives us time to work with you to fix the issue before public exposure, reducing the chance that the exploit will be used before a patch is released.

Please disclose it at [Security Advisories](https://github.com/shirou/gopsutil/security/advisories/new).

This project is maintained by a team of volunteers on a reasonable-effort basis. As such, vulnerability reports will be investigated and fixed or disclosed as soon as possible.
4 changes: 2 additions & 2 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
fail-fast: false
matrix:
go-version: ${{fromJson(needs.go-versions.outputs.versions)}}
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Setup go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: 1.17
- name: Checkout repository
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/sbom_generator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: SBOM Generator

on:
push:
branches: [ "master" ]

workflow_dispatch:

permissions: read-all

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: advanced-security/sbom-generator-action@v0.0.1
id: sbom
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: actions/upload-artifact@v3.1.2
with:
path: ${{steps.sbom.outputs.fileName }}
name: "SBOM"
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
fail-fast: false
matrix:
go-version: ${{fromJson(needs.go-versions.outputs.versions)}}
os: [ubuntu-20.04, ubuntu-18.04, windows-2019, macos-11]
os: [ubuntu-22.04, ubuntu-20.04, windows-2022, windows-2019, macos-11, macos-12]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
Expand Down
19 changes: 16 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ issues:
exclude-rules:
- linters:
- gosec
text: "G204"
text: "G204"
- linters:
- revive
text: "var-naming"
text: "var-naming"
- linters:
- revive
text: "exported"
text: "exported"
- linters:
- revive
text: "empty-block"
- linters:
- revive
text: "unused-parameter"
linters:
enable:
- asciicheck
Expand Down Expand Up @@ -40,3 +46,10 @@ linters:
- structcheck
- unused
- varcheck

linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/shirou)
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ build_test: ## test only buildable
GOOS=linux GOARCH=386 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=arm go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=arm64 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=loong64 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=riscv64 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=linux GOARCH=s390x go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=freebsd GOARCH=amd64 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=freebsd GOARCH=386 go test ./... | $(BUILD_FAIL_PATTERN)
GOOS=freebsd GOARCH=arm go test ./... | $(BUILD_FAIL_PATTERN)
Expand Down Expand Up @@ -51,6 +53,7 @@ vet:
GOOS=linux GOARCH=amd64 go vet ./...
GOOS=linux GOARCH=arm64 go vet ./...
GOOS=linux GOARCH=arm go vet ./...
GOOS=linux GOARCH=loong64 go vet ./...
GOOS=linux GOARCH=mips64 go vet ./...
GOOS=linux GOARCH=mips64le go vet ./...
GOOS=linux GOARCH=mips go vet ./...
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ will provide useful information.
- system wide stats on netfilter conntrack module
- sourced from /proc/sys/net/netfilter/nf_conntrack_count

Some code is ported from Ohai. many thanks.
Some code is ported from Ohai. Many thanks.

## Current Status

Expand Down
22 changes: 11 additions & 11 deletions cpu/cpu_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package cpu
import (
"context"
"regexp"
"strings"
"strconv"
"strings"

"github.com/shirou/gopsutil/v3/internal/common"
)
Expand All @@ -28,19 +28,19 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
}

ret := TimesStat{CPU: "cpu-total"}
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values
for i, header := range h {
if t, err := strconv.ParseFloat(v[i], 64); err == nil {
switch header {
case `%usr`:
ret.User = t
case `%sys`:
ret.System = t
case `%wio`:
ret.Iowait = t
case `%idle`:
ret.Idle = t
case `%usr`:
ret.User = t
case `%sys`:
ret.System = t
case `%wio`:
ret.Iowait = t
case `%idle`:
ret.Idle = t
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"github.com/shoenig/go-m1cpu"
"github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -85,11 +86,15 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
c.CacheSize = int32(cacheSize)
c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor")

// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
if err == nil {
c.Mhz = float64(cpuFrequency) / 1000000.0
if m1cpu.IsAppleSilicon() {
c.Mhz = float64(m1cpu.PCoreHz() / 1_000_000)
} else {
// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
if err == nil {
c.Mhz = float64(cpuFrequency) / 1000000.0
}
}

return append(ret, c), nil
Expand Down
33 changes: 33 additions & 0 deletions cpu/cpu_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build darwin
// +build darwin

package cpu

import (
"testing"

"github.com/shoenig/go-m1cpu"
)

func Test_CpuInfo_AppleSilicon(t *testing.T) {
if !m1cpu.IsAppleSilicon() {
t.Skip("wrong cpu type")
}

v, err := Info()
if err != nil {
t.Errorf("cpu info should be implemented on darwin systems")
}

for _, vv := range v {
if vv.ModelName == "" {
t.Errorf("could not get CPU info: %v", vv)
}
if vv.Mhz <= 0 {
t.Errorf("could not get frequency of: %s", vv.ModelName)
}
if vv.Mhz > 6000 {
t.Errorf("cpu frequency is absurdly high value: %f MHz", vv.Mhz)
}
}
}
12 changes: 8 additions & 4 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"strconv"
"strings"

"github.com/shirou/gopsutil/v3/internal/common"
"github.com/tklauser/go-sysconf"

"github.com/shirou/gopsutil/v3/internal/common"
)

var ClocksPerSec = float64(100)
Expand Down Expand Up @@ -190,7 +191,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
switch key {
case "Processor":
processorName = value
case "processor":
case "processor", "cpu number":
if c.CPU >= 0 {
finishCPUInfo(&c)
ret = append(ret, c)
Expand All @@ -203,6 +204,9 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
c.CPU = int32(t)
case "vendorId", "vendor_id":
c.VendorID = value
if strings.Contains(value, "S390") {
processorName = "S390"
}
case "CPU implementer":
if v, err := strconv.ParseUint(value, 0, 8); err == nil {
switch v {
Expand Down Expand Up @@ -253,7 +257,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
}
}
}
case "model name", "cpu":
case "Model Name", "model name", "cpu":
c.ModelName = value
if strings.Contains(value, "POWER8") ||
strings.Contains(value, "POWER7") {
Expand All @@ -273,7 +277,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
return ret, err
}
c.Stepping = int32(t)
case "cpu MHz", "clock":
case "cpu MHz", "clock", "cpu MHz dynamic":
// treat this as the fallback value, thus we ignore error
if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil {
c.Mhz = t
Expand Down
12 changes: 9 additions & 3 deletions cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
}
t.Errorf("error executing lscpu: %v", err)
}
var threadsPerCore, coresPerSocket, sockets int
var threadsPerCore, coresPerSocket, sockets, books, drawers int
books = 1
drawers = 1
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Split(line, ":")
Expand All @@ -60,14 +62,18 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
threadsPerCore, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Core(s) per socket":
coresPerSocket, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Socket(s)":
case "Socket(s)", "Socket(s) per book":
sockets, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Book(s) per drawer":
books, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Drawer(s)":
drawers, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
}
}
if threadsPerCore == 0 || coresPerSocket == 0 || sockets == 0 {
t.Errorf("missing info from lscpu: threadsPerCore=%d coresPerSocket=%d sockets=%d", threadsPerCore, coresPerSocket, sockets)
}
expectedPhysical := coresPerSocket * sockets
expectedPhysical := coresPerSocket * sockets * books * drawers
expectedLogical := expectedPhysical * threadsPerCore
physical, err := Counts(false)
skipIfNotImplementedErr(t, err)
Expand Down
3 changes: 2 additions & 1 deletion cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"testing"
"time"

"github.com/shirou/gopsutil/v3/internal/common"
"github.com/stretchr/testify/assert"

"github.com/shirou/gopsutil/v3/internal/common"
)

func skipIfNotImplementedErr(t *testing.T, err error) {
Expand Down
2 changes: 2 additions & 0 deletions disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

var invoke common.Invoker = common.Invoke{}

type Warnings = common.Warnings

type UsageStat struct {
Path string `json:"path"`
Fstype string `json:"fstype"`
Expand Down
14 changes: 7 additions & 7 deletions disk/disk_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
"regexp"
"strings"

"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix"
)

var whiteSpaces = regexp.MustCompile(`\s+`)
var startBlank = regexp.MustCompile(`^\s+`)

var ignoreFSType = map[string]bool{"procfs": true}
var FSType = map[int]string{
0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc",
16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs",
33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs",
39: "ahafs", 40: "sterm-nfs", 41: "asmfs",
}
0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc",
16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs",
33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs",
39: "ahafs", 40: "sterm-nfs", 41: "asmfs",
}

func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
var ret []PartitionStat
Expand All @@ -42,7 +42,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
start := 0
finished := false
for pos, ch := range lines[1] {
if ch == ' ' && ! finished {
if ch == ' ' && !finished {
name := strings.TrimSpace(lines[0][start:pos])
colidx[name] = idx
finished = true
Expand Down
3 changes: 1 addition & 2 deletions disk/disk_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
"strconv"
"strings"

"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix"
)

// PartitionsWithContext returns disk partition.
Expand Down
Loading

0 comments on commit dbf5afe

Please sign in to comment.