Skip to content

Commit

Permalink
[enhance] memory: Add FreeBSD support in the memory.c.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Messenger authored and Frederic Ye committed Jan 30, 2012
1 parent 550bdec commit fc9aae6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 4 additions & 1 deletion build/myocamlbuild_prefix.ml
Expand Up @@ -541,9 +541,12 @@ let _ = dispatch begin function
let file = dir / "lib" ^ name -.- !Options.ext_lib in
dep ["ocaml"; tag] [file];
flag ["ocaml"; "byte"; "link"; tag] (S[A"-ccopt";A("-L"^dir);A"-cclib";A("-l"^name);A"-custom"]);
flag ["ocaml"; "native"; "link"; tag] (S[A"-ccopt";A("-L"^dir);A"-cclib";A("-l"^name)])
flag ["ocaml"; "native"; "link"; tag] (S[A"-ccopt";A("-L"^dir);A"-cclib";A("-l"^name)]);
in

(* In the memory.c in FreeBSD part that uses kvm_getprocs() required link with -lkvm. *)
if is_fbsd then
flag ["use_stubs"; "link"] (S[A "-cclib";A "-lkvm"]);

(* -- Don't forget that the rest of the "mlstate build stdlib" is in --
-- myocamlbuild_suffix.ml. The rest comes from the build_rules*.ml in each repo -- *)
31 changes: 30 additions & 1 deletion libbase/memory.c
Expand Up @@ -38,7 +38,36 @@ int get_mem (unsigned int *rss)
return 0;
}

#else /* not MAC */
#elif (defined(__FreeBSD__) || defined(__FreeBSD_kernel__)) /* FreeBSD */

#include <kvm.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>

int get_mem (unsigned int *rss)
{
kvm_t *kv;
int cnt,pagesize,pid;

pagesize = getpagesize();
pid = getpid();
kv = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, NULL);

if (kv != NULL) {
struct kinfo_proc *kproc;
kproc = kvm_getprocs(kv, KERN_PROC_PID, pid, &cnt);
if (kproc && cnt > 0)
*rss = kproc->ki_rssize * pagesize;
else *rss = 0;
kvm_close(kv);
return 0;
} else return -1;
}

#else /* not MAC and FreeBSD */

#include <stdio.h>
#include <unistd.h>
Expand Down

0 comments on commit fc9aae6

Please sign in to comment.