get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/server/pse/pse_freebsd.go (about)

     1  // Copyright 2015-2018 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 !amd64
    15  // +build !amd64
    16  
    17  package pse
    18  
    19  /*
    20  #include <sys/types.h>
    21  #include <sys/sysctl.h>
    22  #include <sys/user.h>
    23  #include <stddef.h>
    24  #include <unistd.h>
    25  
    26  long pagetok(long size)
    27  {
    28      int pageshift, pagesize;
    29  
    30      pagesize = getpagesize();
    31      pageshift = 0;
    32  
    33      while (pagesize > 1) {
    34          pageshift++;
    35          pagesize >>= 1;
    36      }
    37  
    38      return (size << pageshift);
    39  }
    40  
    41  int getusage(double *pcpu, unsigned int *rss, unsigned int *vss)
    42  {
    43      int mib[4], ret;
    44      size_t len;
    45      struct kinfo_proc kp;
    46  
    47      len = 4;
    48      sysctlnametomib("kern.proc.pid", mib, &len);
    49  
    50      mib[3] = getpid();
    51      len = sizeof(kp);
    52  
    53      ret = sysctl(mib, 4, &kp, &len, NULL, 0);
    54      if (ret != 0) {
    55          return (errno);
    56      }
    57  
    58      *rss = pagetok(kp.ki_rssize);
    59      *vss = kp.ki_size;
    60      *pcpu = (double)kp.ki_pctcpu / FSCALE;
    61  
    62      return 0;
    63  }
    64  
    65  */
    66  import "C"
    67  
    68  import (
    69  	"syscall"
    70  )
    71  
    72  // This is a placeholder for now.
    73  func ProcUsage(pcpu *float64, rss, vss *int64) error {
    74  	var r, v C.uint
    75  	var c C.double
    76  
    77  	if ret := C.getusage(&c, &r, &v); ret != 0 {
    78  		return syscall.Errno(ret)
    79  	}
    80  
    81  	*pcpu = float64(c)
    82  	*rss = int64(r)
    83  	*vss = int64(v)
    84  
    85  	return nil
    86  }