Skip to content
Open
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
13 changes: 13 additions & 0 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/googlecloudplatform/gcsfuse/v3/internal/canned"
"github.com/googlecloudplatform/gcsfuse/v3/internal/locker"
"github.com/googlecloudplatform/gcsfuse/v3/internal/logger"
"github.com/googlecloudplatform/gcsfuse/v3/internal/mgmt"
"github.com/googlecloudplatform/gcsfuse/v3/internal/monitor"
"github.com/googlecloudplatform/gcsfuse/v3/internal/mount"
"github.com/googlecloudplatform/gcsfuse/v3/internal/profiler"
Expand Down Expand Up @@ -351,6 +352,18 @@ func Mount(newConfig *cfg.Config, bucketName, mountPoint string) (err error) {

logger.Infof("Start gcsfuse/%s for app %q using mount point: %s\n", common.GetVersion(), newConfig.AppName, mountPoint)

//Log management information about memory, cpu, disk
//space on node and limit set by kubarnates POD
//if any e.g. cpu limit or mem limit.
svc := mgmt.NewGCSFuseMgmtService()
svc.Refresh()

if newConfig.Foreground || newConfig.Logging.FilePath == "" {
logger.Info("GCSFuse enviornment", "data", svc.GetConfig())
} else {
logger.Infof("GCSFuse enviornment mount point: %s %s", mountPoint, svc.String())
}

// Log mount-config and the CLI flags in the log-file.
// If there is no log-file, then log these to stdout.
// Do not log these in stdout in case of daemonized run
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/prometheus/client_golang v1.22.0
github.com/prometheus/client_model v0.6.2
github.com/prometheus/common v0.65.0
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.7
github.com/spf13/viper v1.20.1
Expand Down Expand Up @@ -79,6 +80,7 @@ require (
github.com/go-jose/go-jose/v4 v4.1.1 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/pprof v0.0.0-20250630185457-6e76a2b096b5 // indirect
github.com/google/renameio/v2 v2.0.0 // indirect
Expand All @@ -101,6 +103,7 @@ require (
github.com/spiffe/go-spiffe/v2 v2.5.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zeebo/errs v1.4.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
Expand Down Expand Up @@ -203,6 +205,8 @@ github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.9.0 h1:GbgQGNtTrEmddYDSAH9QLRyfAHY12md+8YFTqyMTC9k=
github.com/sagikazarmark/locafero v0.9.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA=
Expand Down Expand Up @@ -230,6 +234,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zeebo/errs v1.4.0 h1:XNdoD/RRMKP7HD0UhJnIzUy74ISdGGxURlYG8HSWSfM=
github.com/zeebo/errs v1.4.0/go.mod h1:sgbWHsvVuTPHcqJJGQ1WhI5KbWlHYz+2+2C/LSEtCw4=
go.einride.tech/aip v0.68.1 h1:16/AfSxcQISGN5z9C5lM+0mLYXihrHbQ1onvYTr93aQ=
Expand Down Expand Up @@ -293,6 +299,7 @@ golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
Expand Down
164 changes: 164 additions & 0 deletions internal/mgmt/GCSFuseMgmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2025 Google LLC
//
// 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 mgmt

import (
"encoding/json"
"fmt"
"sync"

"os"

"strconv"
"strings"
)

const (
cgroupV2Base = "/sys/fs/cgroup"
cgroupV1Base = "/sys/fs/cgroup"
dockerProcess = "/proc/1/cgroup"
)

type DiskPartitions struct {
Name string `json:"name"`
MountPoints map[string]DiskMount `json:"mountPoints"`
}

type DiskMount struct {
Name string `json:"name"`
FileSystem string `json:"fileSystem"`
TotUsage uint64 `json:"totUsage"`
UsedUsage uint64 `json:"usedUsage"`
FreeUsage uint64 `json:"freeUsage"`
UsagePct float64 `json:"usagePct"`
}

type GCSFuseMgmt struct {
IsContainer bool `json:"isContainer"`
CgroupVersion int8 `json:"cgroupVersion"`
NodeMemTotalBytes int64 `json:"nodeMemTotalBytes"`
NodeMemUtilBytes int64 `json:"nodeMemUtilBytes"`
NodeSwapTotalBytes int64 `json:"nodeSwapTotalBytes"`
NodeSwapUtilBytes int64 `json:"nodeSwapUtilBytes"`
PodMemLimitBytes int64 `json:"podMemLimitBytes"`
NodeCPUAvail int `json:"nodeCPUAvail"`
ProcCPUAvail int `json:"procCPUAvail"`
PodCPULimit float64 `json:"podCPULimit"`
Disks map[string]DiskPartitions `json:"disks"`
}

type GCSFuseMgmtService struct {
mgmtLock *sync.RWMutex
svc GCSFuseMgmt
}

func NewGCSFuseMgmtService() *GCSFuseMgmtService {
mgmt := new(GCSFuseMgmtService)
mgmt.mgmtLock = &sync.RWMutex{}
mgmt.svc.Disks = make(map[string]DiskPartitions)
return mgmt
}

func (mgmt *GCSFuseMgmtService) addDiskUsage(partName string, diskName string, fileSystem string,
totUsage uint64, usedUsage uint64, freeUsage uint64, usagePct float64) {

mp := DiskMount{
Name: diskName,
FileSystem: fileSystem,
TotUsage: totUsage,
UsedUsage: usedUsage,
FreeUsage: freeUsage,
UsagePct: usagePct,
}

if _, ok := mgmt.svc.Disks[partName]; !ok {
mgmt.svc.Disks[partName] = DiskPartitions{
Name: partName,
MountPoints: make(map[string]DiskMount),
}
}

mgmt.svc.Disks[partName].MountPoints[diskName] = mp

}

func (mgmt *GCSFuseMgmtService) readFile(filePath string) (string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
return strings.TrimSpace(string(data)), nil
}

func (mgmt *GCSFuseMgmtService) parseInt64(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}

func (mgmt *GCSFuseMgmtService) getMgmt() GCSFuseMgmtService {
mgmt.mgmtLock.RLock()
defer mgmt.mgmtLock.RUnlock()

// Create a shallow copy of the main struct.
mgmt_new := *mgmt

// Deep copy the Disks map to ensure thread safety.
mgmt_new.svc.Disks = make(map[string]DiskPartitions, len(mgmt.svc.Disks))
for k, v := range mgmt.svc.Disks {
// Deep copy the inner map as well.
newMountPoints := make(map[string]DiskMount, len(v.MountPoints))
for mk, mv := range v.MountPoints {
newMountPoints[mk] = mv
}
v.MountPoints = newMountPoints
mgmt_new.svc.Disks[k] = v
}

return mgmt_new
}

func (mgmt *GCSFuseMgmtService) PrettyString() string {
safeCopy := mgmt.getMgmt().svc
jsonData, err := json.MarshalIndent(safeCopy, "", " ")
if err != nil {
return fmt.Sprintf("failed to marshal to JSON: %+v", mgmt.svc)
}
return string(jsonData)
}

func (mgmt *GCSFuseMgmtService) String() string {
safeCopy := mgmt.getMgmt().svc
jsonData, err := json.Marshal(safeCopy)
if err != nil {
return fmt.Sprintf("failed to marshal to JSON: %+v", mgmt.svc)
}
return string(jsonData)
}

func (mgmt *GCSFuseMgmtService) GetConfig() GCSFuseMgmt {
safeCopy := mgmt.getMgmt().svc
return safeCopy
}

func (mgmt *GCSFuseMgmtService) Refresh() {

mgmt.mgmtLock.Lock()
defer mgmt.mgmtLock.Unlock()

mgmt.detectEnv()
mgmt.fetchMemory()
mgmt.fetchCPU()
mgmt.fetchDisks()

}
107 changes: 107 additions & 0 deletions internal/mgmt/GCSFuseMgmtCPU.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2025 Google LLC
//
// 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 mgmt

import (
"fmt"
"path/filepath"
"runtime"
"strings"

"github.com/googlecloudplatform/gcsfuse/v3/internal/logger"
)

func (mgmt *GCSFuseMgmtService) fetchCPU() {

mgmt.svc.NodeCPUAvail = -1
mgmt.svc.PodCPULimit = -1

mgmt.svc.NodeCPUAvail = runtime.NumCPU()
mgmt.svc.ProcCPUAvail = runtime.GOMAXPROCS(0)

if !mgmt.svc.IsContainer {
return
}

cpuQuota, cpuPeriod, err := mgmt.getCgroupV2CPULimit()

if err != nil {
cpuQuota, cpuPeriod, err = mgmt.getCgroupV1CPULimit()
if err != nil {
logger.Warnf("[cgroup v2/v1] Not available: %v\n", err)
return
}

}

if cpuPeriod > 0 && cpuQuota > 0 {
mgmt.svc.PodCPULimit = float64(cpuQuota) / float64(cpuPeriod)
}

}

func (mgmt *GCSFuseMgmtService) getCgroupV2CPULimit() (quota int64, period int64, err error) {
filePath := filepath.Join(cgroupV2Base, "cpu.max")
content, err := mgmt.readFile(filePath)
if err != nil {
return 0, 0, fmt.Errorf("cgroup v2: could not read %s: %w", filePath, err)
}

parts := strings.Fields(content)
if len(parts) != 2 {
return 0, 0, fmt.Errorf("cgroup v2: invalid format in cpu.max: expected '$QUOTA $PERIOD', got '%s'", content)
}

periodVal, err := mgmt.parseInt64(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("cgroup v2: could not parse period from cpu.max '%s': %w", content, err)
}

if parts[0] == "max" {
return -1, periodVal, nil // No limit
}

quotaVal, err := mgmt.parseInt64(parts[0])
if err != nil {
return 0, 0, fmt.Errorf("cgroup v2: could not parse quota from cpu.max '%s': %w", content, err)
}

return quotaVal, periodVal, nil
}

func (mgmt *GCSFuseMgmtService) getCgroupV1CPULimit() (quota int64, period int64, err error) {
quotaPath := filepath.Join(cgroupV1Base, "cpu", "cpu.cfs_quota_us")
periodPath := filepath.Join(cgroupV1Base, "cpu", "cpu.cfs_period_us")

quotaStr, err := mgmt.readFile(quotaPath)
if err != nil {
return 0, 0, fmt.Errorf("cgroup v1: could not read %s: %w", quotaPath, err)
}
quotaVal, err := mgmt.parseInt64(quotaStr)
if err != nil {
return 0, 0, fmt.Errorf("cgroup v1: could not parse cpu.cfs_quota_us value '%s': %w", quotaStr, err)
}

periodStr, err := mgmt.readFile(periodPath)
if err != nil {
return 0, 0, fmt.Errorf("cgroup v1: could not read %s: %w", periodPath, err)
}
periodVal, err := mgmt.parseInt64(periodStr)
if err != nil {
return 0, 0, fmt.Errorf("cgroup v1: could not parse cpu.cfs_period_us value '%s': %w", periodStr, err)
}

return quotaVal, periodVal, nil
}
Loading