+ }
+}
+
+#if DEVELOPMENT || DEBUG
+void __attribute__((noinline))
+SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(task_t task, int thread_count)
+{
+ mach_exception_data_type_t code[EXCEPTION_CODE_MAX] = {0};
+ int pid = task_pid(task);
+ char procname[MAXCOMLEN + 1] = "unknown";
+
+ if (pid == 1) {
+ /*
+ * Cannot suspend launchd
+ */
+ return;
+ }
+
+ proc_name(pid, procname, sizeof(procname));
+
+ if (disable_exc_resource) {
+ printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
+ "supressed by a boot-arg. \n", procname, pid, thread_count);
+ return;
+ }
+
+ if (audio_active) {
+ printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
+ "supressed due to audio playback.\n", procname, pid, thread_count);
+ return;
+ }
+
+ if (exc_via_corpse_forking == 0) {
+ printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
+ "supressed due to corpse forking being disabled.\n", procname, pid,
+ thread_count);
+ return;
+ }
+
+ printf("process %s[%d] crossed thread count high watermark (%d), sending "
+ "EXC_RESOURCE\n", procname, pid, thread_count);
+
+ EXC_RESOURCE_ENCODE_TYPE(code[0], RESOURCE_TYPE_THREADS);
+ EXC_RESOURCE_ENCODE_FLAVOR(code[0], FLAVOR_THREADS_HIGH_WATERMARK);
+ EXC_RESOURCE_THREADS_ENCODE_THREADS(code[0], thread_count);
+
+ task_enqueue_exception_with_corpse(task, EXC_RESOURCE, code, EXCEPTION_CODE_MAX, NULL);
+}
+#endif /* DEVELOPMENT || DEBUG */
+
+void
+thread_update_io_stats(thread_t thread, int size, int io_flags)
+{
+ int io_tier;
+
+ if (thread->thread_io_stats == NULL || thread->task->task_io_stats == NULL) {
+ return;
+ }
+
+ if (io_flags & DKIO_READ) {
+ UPDATE_IO_STATS(thread->thread_io_stats->disk_reads, size);
+ UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->disk_reads, size);
+ }
+
+ if (io_flags & DKIO_META) {
+ UPDATE_IO_STATS(thread->thread_io_stats->metadata, size);
+ UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->metadata, size);
+ }
+
+ if (io_flags & DKIO_PAGING) {
+ UPDATE_IO_STATS(thread->thread_io_stats->paging, size);
+ UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->paging, size);
+ }
+
+ io_tier = ((io_flags & DKIO_TIER_MASK) >> DKIO_TIER_SHIFT);
+ assert(io_tier < IO_NUM_PRIORITIES);
+
+ UPDATE_IO_STATS(thread->thread_io_stats->io_priority[io_tier], size);
+ UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->io_priority[io_tier], size);
+
+ /* Update Total I/O Counts */
+ UPDATE_IO_STATS(thread->thread_io_stats->total_io, size);
+ UPDATE_IO_STATS_ATOMIC(thread->task->task_io_stats->total_io, size);
+
+ if (!(io_flags & DKIO_READ)) {
+ DTRACE_IO3(physical_writes, struct task *, thread->task, uint32_t, size, int, io_flags);
+ ledger_credit(thread->task->ledger, task_ledgers.physical_writes, size);
+ }
+}
+
+static void
+init_thread_ledgers(void)
+{
+ ledger_template_t t;
+ int idx;
+
+ assert(thread_ledger_template == NULL);
+
+ if ((t = ledger_template_create("Per-thread ledger")) == NULL) {
+ panic("couldn't create thread ledger template");
+ }
+
+ if ((idx = ledger_entry_add(t, "cpu_time", "sched", "ns")) < 0) {
+ panic("couldn't create cpu_time entry for thread ledger template");
+ }
+
+ if (ledger_set_callback(t, idx, thread_cputime_callback, NULL, NULL) < 0) {
+ panic("couldn't set thread ledger callback for cpu_time entry");
+ }
+
+ thread_ledgers.cpu_time = idx;
+
+ ledger_template_complete(t);
+ thread_ledger_template = t;
+}
+
+/*
+ * Returns currently applied CPU usage limit, or 0/0 if none is applied.
+ */
+int
+thread_get_cpulimit(int *action, uint8_t *percentage, uint64_t *interval_ns)
+{
+ int64_t abstime = 0;
+ uint64_t limittime = 0;
+ thread_t thread = current_thread();
+
+ *percentage = 0;
+ *interval_ns = 0;
+ *action = 0;
+
+ if (thread->t_threadledger == LEDGER_NULL) {
+ /*
+ * This thread has no per-thread ledger, so it can't possibly
+ * have a CPU limit applied.
+ */
+ return KERN_SUCCESS;
+ }
+
+ ledger_get_period(thread->t_threadledger, thread_ledgers.cpu_time, interval_ns);
+ ledger_get_limit(thread->t_threadledger, thread_ledgers.cpu_time, &abstime);
+
+ if ((abstime == LEDGER_LIMIT_INFINITY) || (*interval_ns == 0)) {
+ /*
+ * This thread's CPU time ledger has no period or limit; so it
+ * doesn't have a CPU limit applied.
+ */
+ return KERN_SUCCESS;
+ }
+
+ /*
+ * This calculation is the converse to the one in thread_set_cpulimit().
+ */
+ absolutetime_to_nanoseconds(abstime, &limittime);
+ *percentage = (limittime * 100ULL) / *interval_ns;
+ assert(*percentage <= 100);
+
+ if (thread->options & TH_OPT_PROC_CPULIMIT) {
+ assert((thread->options & TH_OPT_PRVT_CPULIMIT) == 0);
+
+ *action = THREAD_CPULIMIT_BLOCK;
+ } else if (thread->options & TH_OPT_PRVT_CPULIMIT) {
+ assert((thread->options & TH_OPT_PROC_CPULIMIT) == 0);
+
+ *action = THREAD_CPULIMIT_EXCEPTION;
+ } else {
+ *action = THREAD_CPULIMIT_DISABLE;
+ }
+
+ return KERN_SUCCESS;
+}
+
+/*
+ * Set CPU usage limit on a thread.
+ *
+ * Calling with percentage of 0 will unset the limit for this thread.
+ */
+int
+thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns)
+{
+ thread_t thread = current_thread();
+ ledger_t l;
+ uint64_t limittime = 0;
+ uint64_t abstime = 0;
+
+ assert(percentage <= 100);
+
+ if (action == THREAD_CPULIMIT_DISABLE) {
+ /*
+ * Remove CPU limit, if any exists.
+ */
+ if (thread->t_threadledger != LEDGER_NULL) {
+ l = thread->t_threadledger;
+ ledger_set_limit(l, thread_ledgers.cpu_time, LEDGER_LIMIT_INFINITY, 0);
+ ledger_set_action(l, thread_ledgers.cpu_time, LEDGER_ACTION_IGNORE);
+ thread->options &= ~(TH_OPT_PROC_CPULIMIT | TH_OPT_PRVT_CPULIMIT);
+ }
+
+ return 0;
+ }
+
+ if (interval_ns < MINIMUM_CPULIMIT_INTERVAL_MS * NSEC_PER_MSEC) {
+ return KERN_INVALID_ARGUMENT;
+ }
+
+ l = thread->t_threadledger;
+ if (l == LEDGER_NULL) {
+ /*
+ * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
+ */
+ if ((l = ledger_instantiate(thread_ledger_template, LEDGER_CREATE_INACTIVE_ENTRIES)) == LEDGER_NULL) {
+ return KERN_RESOURCE_SHORTAGE;
+ }
+
+ /*
+ * We are the first to create this thread's ledger, so only activate our entry.
+ */
+ ledger_entry_setactive(l, thread_ledgers.cpu_time);
+ thread->t_threadledger = l;
+ }
+
+ /*
+ * The limit is specified as a percentage of CPU over an interval in nanoseconds.
+ * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
+ */
+ limittime = (interval_ns * percentage) / 100;
+ nanoseconds_to_absolutetime(limittime, &abstime);
+ ledger_set_limit(l, thread_ledgers.cpu_time, abstime, cpumon_ustackshots_trigger_pct);
+ /*
+ * Refill the thread's allotted CPU time every interval_ns nanoseconds.
+ */
+ ledger_set_period(l, thread_ledgers.cpu_time, interval_ns);
+
+ if (action == THREAD_CPULIMIT_EXCEPTION) {
+ /*
+ * We don't support programming the CPU usage monitor on a task if any of its
+ * threads have a per-thread blocking CPU limit configured.
+ */
+ if (thread->options & TH_OPT_PRVT_CPULIMIT) {
+ panic("CPU usage monitor activated, but blocking thread limit exists");
+ }
+
+ /*
+ * Make a note that this thread's CPU limit is being used for the task-wide CPU
+ * usage monitor. We don't have to arm the callback which will trigger the
+ * exception, because that was done for us in ledger_instantiate (because the
+ * ledger template used has a default callback).
+ */
+ thread->options |= TH_OPT_PROC_CPULIMIT;
+ } else {
+ /*
+ * We deliberately override any CPU limit imposed by a task-wide limit (eg
+ * CPU usage monitor).
+ */
+ thread->options &= ~TH_OPT_PROC_CPULIMIT;