diff --git a/server/disk_avail.go b/server/disk_avail.go index b879330e850..e27c1a71cc1 100644 --- a/server/disk_avail.go +++ b/server/disk_avail.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !windows && !openbsd && !netbsd && !wasm +//go:build !windows && !openbsd && !netbsd && !wasm && !illumos && !solaris package server diff --git a/server/disk_avail_solaris.go b/server/disk_avail_solaris.go new file mode 100644 index 00000000000..e0febd1fee3 --- /dev/null +++ b/server/disk_avail_solaris.go @@ -0,0 +1,38 @@ +// Copyright 2025 The NATS Authors +// 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. + +//go:build illumos || solaris + +package server + +import ( + "os" + "golang.org/x/sys/unix" +) + +func diskAvailable(storeDir string) int64 { + var ba int64 + if _, err := os.Stat(storeDir); os.IsNotExist(err) { + os.MkdirAll(storeDir, defaultDirPerms) + } + var fs unix.Statvfs_t + if err := unix.Statvfs(storeDir, &fs); err == nil { + // Estimate 75% of available storage. + ba = int64(uint64(fs.Frsize) * uint64(fs.Bavail) / 4 * 3) + } else { + // Used 1TB default as a guess if all else fails. + ba = JetStreamMaxStoreDefault + } + return ba +} + diff --git a/server/pse/pse_solaris.go b/server/pse/pse_solaris.go index 8e40d2ed306..f812ba487f5 100644 --- a/server/pse/pse_solaris.go +++ b/server/pse/pse_solaris.go @@ -1,4 +1,4 @@ -// Copyright 2015-2018 The NATS Authors +// Copyright 2015-2025 The NATS Authors // 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 @@ -10,14 +10,36 @@ // 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. +// +// Copied from pse_openbsd.go + +//go:build illumos || solaris package pse -// This is a placeholder for now. -func ProcUsage(pcpu *float64, rss, vss *int64) error { - *pcpu = 0.0 - *rss = 0 - *vss = 0 +import ( + "fmt" + "os" + "os/exec" + "strings" +) +// ProcUsage returns CPU usage +func ProcUsage(pcpu *float64, rss, vss *int64) error { + pidStr := fmt.Sprintf("%d", os.Getpid()) + out, err := exec.Command("ps", "-o", "pcpu,rss,vsz", "-p", pidStr).Output() + if err != nil { + *rss, *vss = -1, -1 + return fmt.Errorf("ps call failed:%v", err) + } + lines := strings.Split(string(out), "\n") + if len(lines) < 2 { + *rss, *vss = -1, -1 + return fmt.Errorf("no ps output") + } + output := lines[1] + fmt.Sscanf(output, "%f %d %d", pcpu, rss, vss) + *rss *= 1024 // 1k blocks, want bytes. + *vss *= 1024 // 1k blocks, want bytes. return nil } diff --git a/server/sysmem/mem_solaris.go b/server/sysmem/mem_solaris.go new file mode 100644 index 00000000000..afba03641e5 --- /dev/null +++ b/server/sysmem/mem_solaris.go @@ -0,0 +1,37 @@ +// Copyright 2025 The NATS Authors +// 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. + +//go:build illumos || solaris + +package sysmem + +import ( + "golang.org/x/sys/unix" +) + +const ( + _SC_PHYS_PAGES = 500 + _SC_PAGESIZE = 11 +) + +func Memory() int64 { + pages, err := unix.Sysconf(_SC_PHYS_PAGES) + if err != nil { + return 0 + } + pageSize, err := unix.Sysconf(_SC_PAGESIZE) + if err != nil { + return 0 + } + return int64(pages) * int64(pageSize) +}