| 1 |
// Copyright 2015-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build cgo && freebsd |
| 15 |
|
| 16 |
package pse |
| 17 |
|
| 18 |
/* |
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/sysctl.h> |
| 21 |
#include <sys/user.h> |
| 22 |
#include <stddef.h> |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
long pagetok(long size) |
| 26 |
{ |
| 27 |
int pageshift, pagesize; |
| 28 |
|
| 29 |
pagesize = getpagesize(); |
| 30 |
pageshift = 0; |
| 31 |
|
| 32 |
while (pagesize > 1) { |
| 33 |
pageshift++; |
| 34 |
pagesize >>= 1; |
| 35 |
} |
| 36 |
|
| 37 |
return (size << pageshift); |
| 38 |
} |
| 39 |
|
| 40 |
int getusage(double *pcpu, unsigned int *rss, unsigned int *vss) |
| 41 |
{ |
| 42 |
int mib[4], ret; |
| 43 |
size_t len; |
| 44 |
struct kinfo_proc kp; |
| 45 |
|
| 46 |
len = 4; |
| 47 |
sysctlnametomib("kern.proc.pid", mib, &len); |
| 48 |
|
| 49 |
mib[3] = getpid(); |
| 50 |
len = sizeof(kp); |
| 51 |
|
| 52 |
ret = sysctl(mib, 4, &kp, &len, NULL, 0); |
| 53 |
if (ret != 0) { |
| 54 |
return (errno); |
| 55 |
} |
| 56 |
|
| 57 |
*rss = pagetok(kp.ki_rssize); |
| 58 |
*vss = kp.ki_size; |
| 59 |
*pcpu = (double)kp.ki_pctcpu / FSCALE; |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
*/ |
| 65 |
import "C" |
| 66 |
|
| 67 |
import ( |
| 68 |
"syscall" |
| 69 |
) |
| 70 |
|
| 71 |
// This is a placeholder for now. |
| 72 |
func ProcUsage(pcpu *float64, rss, vss *int64) error { |
| 73 |
var r, v C.uint |
| 74 |
var c C.double |
| 75 |
|
| 76 |
if ret := C.getusage(&c, &r, &v); ret != 0 { |
| 77 |
return syscall.Errno(ret) |
| 78 |
} |
| 79 |
|
| 80 |
*pcpu = float64(c) |
| 81 |
*rss = int64(r) |
| 82 |
*vss = int64(v) |
| 83 |
|
| 84 |
return nil |
| 85 |
} |