Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 migration #938

Merged
merged 16 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
v3:
- v3/**/*.go
package:cpu:
- cpu/*
- v3/cpu/*
package:disk:
- disk/*
- v3/disk/*
package:docker:
- docker/*
- v3/docker/*
package:host:
- host/*
- v3/host/*
package:load:
- load/*
- v3/load/*
package:mem:
- mem/*
- v3/mem/*
package:net:
- net/*
- v3/net/*
package:process:
- process/*
- v3/process/*
package:winservices:
- winservices/*
- v3/winservices/*
os:linux:
- ./**/*_linux.go
- ./**/*_linux_mips64.go
Expand Down
10 changes: 7 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ gopsutil: psutil for golang
This is a port of psutil (https://github.com/giampaolo/psutil). The challenge is porting all
psutil functions on some architectures.

v3 migration
---------------

from v3.20.10, gopsutil becomes v3 which breaks backawards compatiblity. See [v3Changes.md](_tools/v3migration/v3Changes.md) more detail changes.

Tag semantics
-------------------------
Expand Down Expand Up @@ -51,7 +55,6 @@ All works are implemented without cgo by porting c struct to golang struct.
Usage
---------

Note: gopsutil v2 breaks compatibility. If you want to stay with compatibility, please use v1 branch and vendoring.

.. code:: go

Expand All @@ -60,7 +63,8 @@ Note: gopsutil v2 breaks compatibility. If you want to stay with compatibility,
import (
"fmt"

"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/v3/mem"
// "github.com/shirou/gopsutil/mem" // to use v2
)

func main() {
Expand Down Expand Up @@ -101,7 +105,7 @@ see http://godoc.org/github.com/shirou/gopsutil
Requirements
-----------------

- go1.7 or above is required.
- go1.11 or above is required.


More Info
Expand Down
17 changes: 17 additions & 0 deletions _tools/v3migration/v3Changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# v2 to v3 changes

- v3 is in the `v3` directory

- [process] RLimit is now uint64 (#364)
- [process] Remove process.NetIOCounters (#429)
- [docker] fix typo of memoryLimitInBbytes (#464)
- [mem] VirtualMemoryStat JSON fields capitalization (#545)
- various JSON field name and some of Variable name have been changed. see v3migration.sh
- [all] various kind of platform dependent values/constants such as process.GetWin32Proc is now private. see v3migration.sh
- [process] process.Status() now returns []string. and status string is "Running", not just "R". defined in process.go. (#596)
- [docker] `CgroupCPU()` now returns `*CgroupCPUStat` with Usage (#590 and #581)
- [disk] `disk.Opts` is now string[], not string. (related to #955)
- [host] Fixed temperature sensors detection in Linux (#905)
- [disk] `GetDiskSerialNumber()` is now `SerialNumber()` and spread to all platform
- [disk] `GetLabel ()` is now `Label()` and spread to all platform
- [net] Change net.InterfaceStat.Addrs to InterfaceAddrList (#226)
107 changes: 107 additions & 0 deletions _tools/v3migration/v3migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"os"

"golang.org/x/tools/go/ast/astutil"
)

// https://github.com/shirou/gopsutil/issues/429
func issue429() error {
f := func(filename string) error {
fset := token.NewFileSet()
expr, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return err
}
n := astutil.Apply(expr, func(cr *astutil.Cursor) bool {
if cr.Name() == "Decls" {
switch n := cr.Node().(type) {
case *ast.FuncDecl:
if n.Name.Name == "NetIOCounters" || n.Name.Name == ("NetIOCountersWithContext") {
cr.Delete()
}
}
}
return true
}, nil)
return replace(filename, fset, n)
}

root := "process/"
fnames := []string{"process.go", "process_darwin.go", "process_fallback.go", "process_freebsd.go", "process_linux.go", "process_openbsd.go", "process_bsd.go", "process_posix.go", "process_windows.go", "process_test.go"}
for _, fname := range fnames {
if err := f(root + fname); err != nil {
log.Fatalln("run 429:", err)
}
}
return nil
}

func issueRemoveUnusedValue() error {
f := func(filename string) error {
fset := token.NewFileSet()
expr, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return err
}
n := astutil.Apply(expr, func(cr *astutil.Cursor) bool {
if cr.Name() == "Decls" {
switch n := cr.Node().(type) {
case *ast.GenDecl:
if n.Tok != token.TYPE {
break
}
ts := n.Specs[0].(*ast.TypeSpec)
if ts.Name.Name == "SystemProcessInformation" {
cr.Delete()
}
}
}
return true
}, nil)
return replace(filename, fset, n)
}

if err := f("process/process_windows.go"); err != nil {
log.Fatalln("run 429:", err)
}
return nil
}

func replace(filename string, fset *token.FileSet, n ast.Node) error {
if err := os.Remove(filename); err != nil {
return err
}
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
if err := format.Node(fp, fset, n); err != nil {
return err
}
fp.WriteString("\n")
return nil
}

func main() {
flag.Parse()
for _, n := range flag.Args() {
fmt.Println("issue:" + n)
switch n {
case "429":
issue429()
case "issueRemoveUnusedValue":
issueRemoveUnusedValue()
}
}

}
174 changes: 174 additions & 0 deletions _tools/v3migration/v3migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/usr/bin/env bash

set -eu

# this scripts is used when migrating v2 to v3.
# usage: cd ${GOPATH}/src/github.com/shirou/gopsutil && bash tools/v3migration/v3migration.sh



DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT=$(cd ${DIR}/../.. && pwd)


## 1. refresh
cd ${ROOT}

/bin/rm -rf v3

## 2. copy directories
# docker is removed, #464 will be fixed
mkdir -p v3
cp -rp cpu disk docker host internal load mem net process winservices v3
cp Makefile v3

# build migartion tool
go build -o v3/v3migration ${DIR}/v3migration.go


V3DIR=$(cd ${ROOT}/v3 && pwd)
cd ${V3DIR}

## 3. mod
go mod init

### change import path
find . -name "*.go" | xargs -I@ sed -i 's|"github.com/shirou/gopsutil/|"github.com/shirou/gopsutil/v3/|g' @

############ Issues

# #429 process.NetIOCounters is pointless on Linux
./v3migration `pwd` 429
sed -i '/NetIOCounters/d' process/process.go
sed -i "/github.com\/shirou\/gopsutil\/v3\/net/d" process/process_bsd.go


# #464 CgroupMem : fix typo and wrong file names
sed -i 's|memoryLimitInBbytes|memoryLimitInBytes|g' docker/docker.go
sed -i 's|memoryLimitInBbytes|memory.limit_in_bytes|g' docker/docker_linux.go
sed -i 's|memoryFailcnt|memory.failcnt|g' docker/docker_linux.go


# fix #346
sed -i 's/Soft int32/Soft uint64/' process/process.go
sed -i 's/Hard int32/Hard uint64/' process/process.go
sed -i 's| //TODO too small. needs to be uint64||' process/process.go
sed -i 's|limitToInt(val string) (int32, error)|limitToUint(val string) (uint64, error)|' process/process_*.go
sed -i 's|limitToInt|limitToUint|' process/process_*.go
sed -i 's|return int32(res), nil|return uint64(res), nil|' process/process_*.go
sed -i 's|math.MaxInt32|math.MaxUint64|' process/process_*.go

# fix #545
# variable names
sed -i 's|WritebackTmp|WriteBackTmp|g' mem/*.go
sed -i 's|Writeback|WriteBack|g' mem/*.go
sed -i 's|SReclaimable|Sreclaimable|g' mem/*.go
sed -i 's|SUnreclaim|Sunreclaim|g' mem/*.go
sed -i 's|VMallocTotal|VmallocTotal|g' mem/*.go
sed -i 's|VMallocUsed|VmallocUsed|g' mem/*.go
sed -i 's|VMallocChunk|VmallocChunk|g' mem/*.go

# json field name
sed -i 's|hostid|hostId|g' host/host.go
sed -i 's|hostid|hostId|g' host/host_test.go
sed -i 's|sensorTemperature|temperature|g' host/host.go
sed -i 's|sensorTemperature|temperature|g' host/host_test.go

sed -i 's|writeback|writeBack|g' mem/*.go
sed -i 's|writeBacktmp|writeBackTmp|g' mem/*.go
sed -i 's|pagetables|pageTables|g' mem/*.go
sed -i 's|swapcached|swapCached|g' mem/*.go
sed -i 's|commitlimit|commitLimit|g' mem/*.go
sed -i 's|committedas|committedAS|g' mem/*.go
sed -i 's|hightotal|highTotal|g' mem/*.go
sed -i 's|highfree|highFree|g' mem/*.go
sed -i 's|lowtotal|lowTotal|g' mem/*.go
sed -i 's|lowfree|lowFree|g' mem/*.go
sed -i 's|swaptotal|swapTotal|g' mem/*.go
sed -i 's|swapfree|swapFree|g' mem/*.go
sed -i 's|vmalloctotal|vmallocTotal|g' mem/*.go
sed -i 's|vmallocused|vmallocUsed|g' mem/*.go
sed -i 's|vmallocchunk|vmallocChunk|g' mem/*.go
sed -i 's|hugepagestotal|hugePagesTotal|g' mem/*.go
sed -i 's|hugepagesfree|hugePagesFree|g' mem/*.go
sed -i 's|hugepagesize|hugePageSize|g' mem/*.go
sed -i 's|pgin|pgIn|g' mem/*.go
sed -i 's|pgout|pgOut|g' mem/*.go
sed -i 's|pgfault|pgFault|g' mem/*.go
sed -i 's|pgmajfault|pgMajFault|g' mem/*.go

sed -i 's|hardwareaddr|hardwareAddr|g' net/*.go
sed -i 's|conntrackCount|connTrackCount|g' net/*.go
sed -i 's|conntrackMax|connTrackMax|g' net/*.go
sed -i 's|delete_list|deleteList|g' net/*.go
sed -i 's|insert_failed|insertFailed|g' net/*.go
sed -i 's|early_drop|earlyDrop|g' net/*.go
sed -i 's|expect_create|expectCreate|g' net/*.go
sed -i 's|expect_delete|expectDelete|g' net/*.go
sed -i 's|search_restart|searchRestart|g' net/*.go
sed -i 's|icmp_error|icmpError|g' net/*.go
sed -i 's|expect_new|expectNew|g' net/*.go



# fix no more public API/types/constants defined only for some platforms

sed -i 's|CTLKern|ctlKern|g' cpu/*.go
sed -i 's|CPNice|cpNice|g' cpu/*.go
sed -i 's|CPSys|cpSys|g' cpu/*.go
sed -i 's|CPIntr|cpIntr|g' cpu/*.go
sed -i 's|CPIdle|cpIdle|g' cpu/*.go
sed -i 's|CPUStates|cpUStates|g' cpu/*.go
sed -i 's|CTLKern|ctlKern|g' cpu/cpu_openbsd.go
sed -i 's|CTLHw|ctlHw|g' cpu/cpu_openbsd.go
sed -i 's|SMT|sMT|g' cpu/cpu_openbsd.go
sed -i 's|KernCptime|kernCptime|g' cpu/cpu_openbsd.go
sed -i 's|KernCptime2|kernCptime2|g' cpu/cpu_openbsd.go
sed -i 's|Win32_Processor|win32Processor|g' cpu/cpu_windows.go

sed -i 's|DEVSTAT_NO_DATA|devstat_NO_DATA|g' disk/*.go
sed -i 's|DEVSTAT_READ|devstat_READ|g' disk/*.go
sed -i 's|DEVSTAT_WRITE|devstat_WRITE|g' disk/*.go
sed -i 's|DEVSTAT_FREE|devstat_FREE|g' disk/*.go
sed -i 's|Devstat|devstat|g' disk/*.go
sed -i 's|Bintime|bintime|g' disk/*.go
sed -i 's|SectorSize|sectorSize|g' disk/disk_linux.go
sed -i 's|FileFileCompression|fileFileCompression|g' disk/disk_windows.go
sed -i 's|FileReadOnlyVolume|fileReadOnlyVolume|g' disk/disk_windows.go

sed -i 's|USER_PROCESS|user_PROCESS|g' host/host_*.go
sed -i 's|LSB|lsbStruct|g' host/host_linux*

sed -i 's| BcacheStats | bcacheStats |g' mem/*.go

sed -i 's|TCPStatuses|tcpStatuses|g' net/*.go
sed -i 's|CT_ENTRIES|ctENTRIES|g' net/net_linux.go
sed -i 's|CT_SEARCHED|ctSEARCHED|g' net/net_linux.go
sed -i 's|CT_FOUND|ctFOUND|g' net/net_linux.go
sed -i 's|CT_NEW|ctNEW|g' net/net_linux.go
sed -i 's|CT_INVALID|ctINVALID|g' net/net_linux.go
sed -i 's|CT_IGNORE|ctIGNORE|g' net/net_linux.go
sed -i 's|CT_DELETE|ctDELETE|g' net/net_linux.go
sed -i 's|CT_DELETE_LIST|ctDELETE_LIST|g' net/net_linux.go
sed -i 's|CT_INSERT|ctINSERT|g' net/net_linux.go
sed -i 's|CT_INSERT_FAILED|ctINSERT_FAILED|g' net/net_linux.go
sed -i 's|CT_DROP|ctDROP|g' net/net_linux.go
sed -i 's|CT_EARLY_DROP|ctEARLY_DROP|g' net/net_linux.go
sed -i 's|CT_ICMP_ERROR|ctICMP_ERROR|g' net/net_linux.go
sed -i 's|CT_EXPECT_NEW|ctEXPECT_NEW|g' net/net_linux.go
sed -i 's|CT_EXPECT_CREATE|ctEXPECT_CREATE|g' net/net_linux.go
sed -i 's|CT_EXPECT_DELETE|ctEXPECT_DELETE|g' net/net_linux.go
sed -i 's|CT_SEARCH_RESTART|ctSEARCH_RESTART|g' net/net_linux.go

sed -i 's|PageSize|pageSize|g' process/process_*.go
sed -i 's|PrioProcess|prioProcess|g' process/process_*.go
sed -i 's|ClockTicks|clockTicks|g' process/process_*.go


./v3migration `pwd` issueRemoveUnusedValue


############ SHOULD BE FIXED BY HAND



Loading