[Libguestfs] [PATCH common] mlutils: Simple wrapper around sysconf (_SC_NPROCESSORS_ONLN).

Richard W.M. Jones rjones at redhat.com
Fri Sep 18 09:42:31 UTC 2020


---
 mlutils/unix_utils-c.c | 15 +++++++++++++++
 mlutils/unix_utils.ml  |  5 +++++
 mlutils/unix_utils.mli |  9 +++++++++
 3 files changed, 29 insertions(+)

diff --git a/mlutils/unix_utils-c.c b/mlutils/unix_utils-c.c
index 3309961..8acf039 100644
--- a/mlutils/unix_utils-c.c
+++ b/mlutils/unix_utils-c.c
@@ -77,6 +77,7 @@ extern value guestfs_int_mllib_mkdtemp (value val_pattern);
 extern value guestfs_int_mllib_realpath (value pathv);
 extern value guestfs_int_mllib_statvfs_statvfs (value pathv);
 extern value guestfs_int_mllib_statvfs_is_network_filesystem (value pathv);
+extern value guestfs_int_mllib_sysconf_nr_processors_online (value unitv);
 
 /* NB: This is a "noalloc" call. */
 value
@@ -368,3 +369,17 @@ guestfs_int_mllib_statvfs_is_network_filesystem (value pathv)
   return Val_bool (0);
 #endif
 }
+
+/* NB: This is a "noalloc" call. */
+value
+guestfs_int_mllib_sysconf_nr_processors_online (value unitv)
+{
+#ifdef _SC_NPROCESSORS_ONLN
+  long n;
+
+  n = sysconf (_SC_NPROCESSORS_ONLN);
+  if (n > 0) return Val_int (n);
+#endif
+  /* Return a safe value so that callers don't need to deal with errors. */
+  return Val_int (1);
+}
diff --git a/mlutils/unix_utils.ml b/mlutils/unix_utils.ml
index 52eb824..2bdda12 100644
--- a/mlutils/unix_utils.ml
+++ b/mlutils/unix_utils.ml
@@ -84,3 +84,8 @@ module StatVFS = struct
   external is_network_filesystem : string -> bool =
     "guestfs_int_mllib_statvfs_is_network_filesystem" "noalloc"
 end
+
+module Sysconf = struct
+  external nr_processors_online : unit -> int =
+    "guestfs_int_mllib_sysconf_nr_processors_online" "noalloc"
+end
diff --git a/mlutils/unix_utils.mli b/mlutils/unix_utils.mli
index 4fcea4a..aead4df 100644
--- a/mlutils/unix_utils.mli
+++ b/mlutils/unix_utils.mli
@@ -121,3 +121,12 @@ module StatVFS : sig
   (** [is_network_filesystem path] returns true if [path] is located on
       a network filesystem such as NFS or CIFS. *)
 end
+
+module Sysconf : sig
+  val nr_processors_online : unit -> int
+  (** [nr_processors_online ()] returns the number of processors
+      currently online, from [sysconf (_SC_NPROCESSORS_ONLN)].
+
+      Note this never fails.  In case we cannot get the number of
+      cores it returns 1. *)
+end
-- 
2.27.0




More information about the Libguestfs mailing list