+
+#if CONFIG_ZLEAKS
+
+SYSCTL_DECL(_kern_zleak);
+SYSCTL_NODE(_kern, OID_AUTO, zleak, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "zleak");
+
+/*
+ * kern.zleak.active
+ *
+ * Show the status of the zleak subsystem (0 = enabled, 1 = active,
+ * and -1 = failed), and if enabled, allow it to be activated immediately.
+ */
+static int
+sysctl_zleak_active SYSCTL_HANDLER_ARGS
+{
+#pragma unused(arg1, arg2)
+ int oldval, val, error;
+
+ val = oldval = get_zleak_state();
+ error = sysctl_handle_int(oidp, &val, 0, req);
+ if (error || !req->newptr)
+ return (error);
+ /*
+ * Can only be activated if it's off (and not failed.)
+ * Cannot be deactivated once it's on.
+ */
+ if (val == 1 && oldval == 0) {
+ kern_return_t kr = zleak_activate();
+
+ if (KERN_SUCCESS != kr)
+ printf("zleak_active: failed to activate "
+ "live zone leak debugging (%d).\n", kr);
+ } if (val == 0 && oldval == 1) {
+ printf("zleak_active: active, cannot be disabled.\n");
+ return (EINVAL);
+ }
+ return (0);
+}
+
+SYSCTL_PROC(_kern_zleak, OID_AUTO, active,
+ CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
+ 0, 0, sysctl_zleak_active, "I", "zleak activity");
+
+/*
+ * kern.zleak.max_zonemap_size
+ *
+ * Read the value of the maximum zonemap size in bytes; useful
+ * as the maximum size that zleak.global_threshold and
+ * zleak.zone_threshold should be set to.
+ */
+static int
+sysctl_zleak_max_zonemap_size SYSCTL_HANDLER_ARGS
+{
+ uint64_t zmap_max_size = *(vm_size_t *)arg1;
+
+ return sysctl_handle_quad(oidp, &zmap_max_size, arg2, req);
+}
+
+SYSCTL_PROC(_kern_zleak, OID_AUTO, max_zonemap_size,
+ CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
+ &zleak_max_zonemap_size, 0,
+ sysctl_zleak_max_zonemap_size, "Q", "zleak max zonemap size");
+
+
+static int
+sysctl_zleak_threshold SYSCTL_HANDLER_ARGS
+{
+#pragma unused(oidp, arg2)
+ int error;
+ uint64_t value = *(vm_size_t *)arg1;
+
+ error = sysctl_io_number(req, value, sizeof (value), &value, NULL);
+
+ if (error || !req->newptr)
+ return (error);
+
+ if (value > (uint64_t)zleak_max_zonemap_size)
+ return (ERANGE);
+
+ *(vm_size_t *)arg1 = value;
+ return (0);
+}
+
+/*
+ * kern.zleak.global_threshold
+ *
+ * Set the global zleak threshold size (in bytes). If the zone map
+ * grows larger than this value, zleaks are automatically activated.
+ *
+ * The default value is set in zleak_init().
+ */
+SYSCTL_PROC(_kern_zleak, OID_AUTO, global_threshold,
+ CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ &zleak_global_tracking_threshold, 0,
+ sysctl_zleak_threshold, "Q", "zleak global threshold");
+
+/*
+ * kern.zleak.zone_threshold
+ *
+ * Set the per-zone threshold size (in bytes) above which any
+ * zone will automatically start zleak tracking.
+ *
+ * The default value is set in zleak_init().
+ *
+ * Setting this variable will have no effect until zleak tracking is
+ * activated (See above.)
+ */
+SYSCTL_PROC(_kern_zleak, OID_AUTO, zone_threshold,
+ CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
+ &zleak_per_zone_tracking_threshold, 0,
+ sysctl_zleak_threshold, "Q", "zleak per-zone threshold");
+
+#endif /* CONFIG_ZLEAKS */