From 1493d8af7d95bf5c8fb006e642eda7e815195021 Mon Sep 17 00:00:00 2001 From: Lorenz Brun Date: Mon, 13 May 2019 03:01:35 +0200 Subject: [PATCH] Remove cgo use and lots of ad-hoc defined kernel constants --- utils/cpuload/netlink/defs.go | 26 -------------------------- utils/cpuload/netlink/netlink.go | 21 ++++++--------------- 2 files changed, 6 insertions(+), 41 deletions(-) delete mode 100644 utils/cpuload/netlink/defs.go diff --git a/utils/cpuload/netlink/defs.go b/utils/cpuload/netlink/defs.go deleted file mode 100644 index a45d8703..00000000 --- a/utils/cpuload/netlink/defs.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2015 Google Inc. All Rights Reserved. -// -// 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 netlink - -/* -#include -*/ -import "C" - -type TaskStats C.struct_taskstats - -const ( - __TASKSTATS_CMD_MAX = C.__TASKSTATS_CMD_MAX -) diff --git a/utils/cpuload/netlink/netlink.go b/utils/cpuload/netlink/netlink.go index e32b3fd9..e2d9d72d 100644 --- a/utils/cpuload/netlink/netlink.go +++ b/utils/cpuload/netlink/netlink.go @@ -22,16 +22,7 @@ import ( "syscall" info "github.com/google/cadvisor/info/v1" -) - -const ( - // Kernel constants for tasks stats. - genlIdCtrl = syscall.NLMSG_MIN_TYPE // GENL_ID_CTRL - taskstatsGenlName = "TASKSTATS" // TASKSTATS_GENL_NAME - cgroupStatsCmdAttrFd = 0x1 // CGROUPSTATS_CMD_ATTR_FD - ctrlAttrFamilyId = 0x1 // CTRL_ATTR_FAMILY_ID - ctrlAttrFamilyName = 0x2 // CTRL_ATTR_FAMILY_NAME - ctrlCmdGetFamily = 0x3 // CTRL_CMD_GETFAMILY + "golang.org/x/sys/unix" ) var ( @@ -124,15 +115,15 @@ func prepareMessage(headerType uint16, cmd uint8, attributes []byte) (msg netlin // Prepares message to query family id for task stats. func prepareFamilyMessage() (msg netlinkMessage) { buf := bytes.NewBuffer([]byte{}) - addAttribute(buf, ctrlAttrFamilyName, taskstatsGenlName, len(taskstatsGenlName)+1) - return prepareMessage(genlIdCtrl, ctrlCmdGetFamily, buf.Bytes()) + addAttribute(buf, unix.CTRL_ATTR_FAMILY_NAME, unix.TASKSTATS_GENL_NAME, len(unix.TASKSTATS_GENL_NAME)+1) + return prepareMessage(unix.GENL_ID_CTRL, unix.CTRL_CMD_GETFAMILY, buf.Bytes()) } // Prepares message to query task stats for a task group. func prepareCmdMessage(id uint16, cfd uintptr) (msg netlinkMessage) { buf := bytes.NewBuffer([]byte{}) - addAttribute(buf, cgroupStatsCmdAttrFd, uint32(cfd), 4) - return prepareMessage(id, __TASKSTATS_CMD_MAX+1, buf.Bytes()) + addAttribute(buf, unix.CGROUPSTATS_CMD_ATTR_FD, uint32(cfd), 4) + return prepareMessage(id, unix.CGROUPSTATS_CMD_GET, buf.Bytes()) } // Extracts returned family id from the response. @@ -158,7 +149,7 @@ func parseFamilyResp(msg syscall.NetlinkMessage) (uint16, error) { if err != nil { return 0, err } - if attr.Type == ctrlAttrFamilyId { + if attr.Type == unix.CTRL_ATTR_FAMILY_ID { err = binary.Read(buf, Endian, &id) if err != nil { return 0, err