2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <mach/mach_types.h>
29 #include <mach/machine/vm_param.h>
30 #include <mach/task.h>
32 #include <kern/kern_types.h>
33 #include <kern/ledger.h>
34 #include <kern/processor.h>
35 #include <kern/thread.h>
36 #include <kern/task.h>
39 #include <ipc/ipc_port.h>
40 #include <ipc/ipc_object.h>
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
44 #include <vm/vm_protos.h> /* last */
45 #include <sys/resource.h>
46 #include <sys/signal.h>
47 #include <sys/errno.h>
50 #include <kern/monotonic.h>
51 #include <machine/monotonic.h>
52 #endif /* MONOTONIC */
54 #include <machine/limits.h>
55 #include <sys/codesign.h> /* CS_CDHASH_LEN */
57 #undef thread_should_halt
59 /* BSD KERN COMPONENT INTERFACE */
61 extern unsigned int not_in_kdp
; /* Skip acquiring locks if we're in kdp */
63 thread_t
get_firstthread(task_t
);
64 int get_task_userstop(task_t
);
65 int get_thread_userstop(thread_t
);
66 boolean_t
current_thread_aborted(void);
67 void task_act_iterate_wth_args(task_t
, void (*)(thread_t
, void *), void *);
68 kern_return_t
get_signalact(task_t
, thread_t
*, int);
69 int fill_task_rusage(task_t task
, rusage_info_current
*ri
);
70 int fill_task_io_rusage(task_t task
, rusage_info_current
*ri
);
71 int fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
);
72 void fill_task_monotonic_rusage(task_t task
, rusage_info_current
*ri
);
73 uint64_t get_task_logical_writes(task_t task
, boolean_t external
);
74 void fill_task_billed_usage(task_t task
, rusage_info_current
*ri
);
75 void task_bsdtask_kill(task_t
);
77 extern uint64_t get_dispatchqueue_serialno_offset_from_proc(void *p
);
78 extern uint64_t get_dispatchqueue_label_offset_from_proc(void *p
);
79 extern uint64_t proc_uniqueid(void *p
);
80 extern int proc_pidversion(void *p
);
81 extern int proc_getcdhash(void *p
, char *cdhash
);
84 extern void psignal(void *, int);
91 get_bsdtask_info(task_t t
)
97 task_bsdtask_kill(task_t t
)
99 void * bsd_info
= get_bsdtask_info(t
);
100 if (bsd_info
!= NULL
) {
101 psignal(bsd_info
, SIGKILL
);
108 get_bsdthreadtask_info(thread_t th
)
110 return th
->task
!= TASK_NULL
? th
->task
->bsd_info
: NULL
;
117 set_bsdtask_info(task_t t
, void * v
)
126 get_bsdthread_info(thread_t th
)
132 * This is used to remember any FS error from VNOP_PAGEIN code when
133 * invoked under vm_fault(). The value is an errno style value. It can
134 * be retrieved by exception handlers using thread_get_state().
137 set_thread_pagein_error(thread_t th
, int error
)
139 assert(th
== current_thread());
140 if (error
== 0 || th
->t_pagein_error
== 0) {
141 th
->t_pagein_error
= error
;
145 #if defined(__x86_64__)
147 * Returns non-zero if the thread has a non-NULL task
148 * and that task has an LDT.
151 thread_task_has_ldt(thread_t th
)
153 return th
->task
&& th
->task
->i386_ldt
!= 0;
155 #endif /* __x86_64__ */
160 int get_thread_lock_count(thread_t th
); /* forced forward */
162 get_thread_lock_count(thread_t th
)
164 return th
->mutex_count
;
168 * XXX: wait for BSD to fix signal code
169 * Until then, we cannot block here. We know the task
170 * can't go away, so we make sure it is still active after
171 * retrieving the first thread for extra safety.
174 get_firstthread(task_t task
)
176 thread_t thread
= (thread_t
)(void *)queue_first(&task
->threads
);
178 if (queue_end(&task
->threads
, (queue_entry_t
)thread
)) {
179 thread
= THREAD_NULL
;
192 thread_t
*result_out
,
195 kern_return_t result
= KERN_SUCCESS
;
196 thread_t inc
, thread
= THREAD_NULL
;
206 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
207 !queue_end(&task
->threads
, (queue_entry_t
)inc
);) {
208 thread_mtx_lock(inc
);
210 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
214 thread_mtx_unlock(inc
);
216 inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
);
220 *result_out
= thread
;
225 act_set_astbsd(thread
);
228 thread_mtx_unlock(thread
);
230 result
= KERN_FAILURE
;
245 kern_return_t result
= KERN_FAILURE
;
256 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
257 !queue_end(&task
->threads
, (queue_entry_t
)inc
);) {
259 thread_mtx_lock(inc
);
262 (inc
->sched_flags
& TH_SFLAG_ABORTED_MASK
) != TH_SFLAG_ABORT
) {
263 result
= KERN_SUCCESS
;
267 thread_mtx_unlock(inc
);
271 inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
);
274 if (result
== KERN_SUCCESS
) {
276 act_set_astbsd(thread
);
279 thread_mtx_unlock(thread
);
288 get_task_ledger(task_t t
)
294 * This is only safe to call from a thread executing in
295 * in the task's context or if the task is locked. Otherwise,
296 * the map could be switched for the task (and freed) before
297 * we go to return it here.
300 get_task_map(task_t t
)
306 get_task_map_reference(task_t t
)
320 vm_map_reference_swap(m
);
329 get_task_ipcspace(task_t t
)
335 get_task_numacts(task_t t
)
337 return t
->thread_count
;
340 /* does this machine need 64bit register set for signal handler */
342 is_64signalregset(void)
344 if (task_has_64Bit_data(current_task())) {
352 * Swap in a new map for the task/thread pair; the old map reference is
353 * returned. Also does a pmap switch if thread provided is current thread.
356 swap_task_map(task_t task
, thread_t thread
, vm_map_t map
)
359 boolean_t doswitch
= (thread
== current_thread()) ? TRUE
: FALSE
;
361 if (task
!= thread
->task
) {
362 panic("swap_task_map");
366 mp_disable_preemption();
369 thread
->map
= task
->map
= map
;
370 vm_commit_pagezero_status(map
);
373 PMAP_SWITCH_USER(thread
, map
, cpu_number());
375 mp_enable_preemption();
378 #if defined(__x86_64__) && NCOPY_WINDOWS > 0
379 inval_copy_windows(thread
);
387 * This is only safe to call from a thread executing in
388 * in the task's context or if the task is locked. Otherwise,
389 * the map could be switched for the task (and freed) before
390 * we go to return it here.
393 get_task_pmap(task_t t
)
402 get_task_resident_size(task_t task
)
406 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
407 return (uint64_t)pmap_resident_count(map
->pmap
) * PAGE_SIZE_64
;
411 get_task_compressed(task_t task
)
415 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
416 return (uint64_t)pmap_compressed(map
->pmap
) * PAGE_SIZE_64
;
420 get_task_resident_max(task_t task
)
424 map
= (task
== kernel_task
) ? kernel_map
: task
->map
;
425 return (uint64_t)pmap_resident_max(map
->pmap
) * PAGE_SIZE_64
;
429 get_task_purgeable_size(task_t task
)
432 ledger_amount_t credit
, debit
;
433 uint64_t volatile_size
= 0;
435 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.purgeable_volatile
, &credit
, &debit
);
436 if (ret
!= KERN_SUCCESS
) {
440 volatile_size
+= (credit
- debit
);
442 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.purgeable_volatile_compressed
, &credit
, &debit
);
443 if (ret
!= KERN_SUCCESS
) {
447 volatile_size
+= (credit
- debit
);
449 return volatile_size
;
456 get_task_phys_footprint(task_t task
)
459 ledger_amount_t credit
, debit
;
461 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.phys_footprint
, &credit
, &debit
);
462 if (KERN_SUCCESS
== ret
) {
463 return credit
- debit
;
469 #if CONFIG_LEDGER_INTERVAL_MAX
474 get_task_phys_footprint_interval_max(task_t task
, int reset
)
479 ret
= ledger_get_interval_max(task
->ledger
, task_ledgers
.phys_footprint
, &max
, reset
);
481 if (KERN_SUCCESS
== ret
) {
487 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
493 get_task_phys_footprint_lifetime_max(task_t task
)
498 ret
= ledger_get_lifetime_max(task
->ledger
, task_ledgers
.phys_footprint
, &max
);
500 if (KERN_SUCCESS
== ret
) {
511 get_task_phys_footprint_limit(task_t task
)
516 ret
= ledger_get_limit(task
->ledger
, task_ledgers
.phys_footprint
, &max
);
517 if (KERN_SUCCESS
== ret
) {
525 get_task_internal(task_t task
)
528 ledger_amount_t credit
, debit
;
530 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.internal
, &credit
, &debit
);
531 if (KERN_SUCCESS
== ret
) {
532 return credit
- debit
;
539 get_task_internal_compressed(task_t task
)
542 ledger_amount_t credit
, debit
;
544 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.internal_compressed
, &credit
, &debit
);
545 if (KERN_SUCCESS
== ret
) {
546 return credit
- debit
;
553 get_task_purgeable_nonvolatile(task_t task
)
556 ledger_amount_t credit
, debit
;
558 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.purgeable_nonvolatile
, &credit
, &debit
);
559 if (KERN_SUCCESS
== ret
) {
560 return credit
- debit
;
567 get_task_purgeable_nonvolatile_compressed(task_t task
)
570 ledger_amount_t credit
, debit
;
572 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.purgeable_nonvolatile_compressed
, &credit
, &debit
);
573 if (KERN_SUCCESS
== ret
) {
574 return credit
- debit
;
581 get_task_alternate_accounting(task_t task
)
584 ledger_amount_t credit
, debit
;
586 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.alternate_accounting
, &credit
, &debit
);
587 if (KERN_SUCCESS
== ret
) {
588 return credit
- debit
;
595 get_task_alternate_accounting_compressed(task_t task
)
598 ledger_amount_t credit
, debit
;
600 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.alternate_accounting_compressed
, &credit
, &debit
);
601 if (KERN_SUCCESS
== ret
) {
602 return credit
- debit
;
609 get_task_page_table(task_t task
)
612 ledger_amount_t credit
, debit
;
614 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.page_table
, &credit
, &debit
);
615 if (KERN_SUCCESS
== ret
) {
616 return credit
- debit
;
623 get_task_iokit_mapped(task_t task
)
626 ledger_amount_t credit
, debit
;
628 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.iokit_mapped
, &credit
, &debit
);
629 if (KERN_SUCCESS
== ret
) {
630 return credit
- debit
;
637 get_task_network_nonvolatile(task_t task
)
640 ledger_amount_t credit
, debit
;
642 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.network_nonvolatile
, &credit
, &debit
);
643 if (KERN_SUCCESS
== ret
) {
644 return credit
- debit
;
651 get_task_network_nonvolatile_compressed(task_t task
)
654 ledger_amount_t credit
, debit
;
656 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.network_nonvolatile_compressed
, &credit
, &debit
);
657 if (KERN_SUCCESS
== ret
) {
658 return credit
- debit
;
665 get_task_wired_mem(task_t task
)
668 ledger_amount_t credit
, debit
;
670 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.wired_mem
, &credit
, &debit
);
671 if (KERN_SUCCESS
== ret
) {
672 return credit
- debit
;
680 get_task_cpu_time(task_t task
)
683 ledger_amount_t credit
, debit
;
685 ret
= ledger_get_entries(task
->ledger
, task_ledgers
.cpu_time
, &credit
, &debit
);
686 if (KERN_SUCCESS
== ret
) {
687 return credit
- debit
;
694 get_task_loadTag(task_t task
)
696 return os_atomic_load(&task
->loadTag
, relaxed
);
700 set_task_loadTag(task_t task
, uint32_t loadTag
)
702 return os_atomic_xchg(&task
->loadTag
, loadTag
, relaxed
);
709 get_threadtask(thread_t th
)
721 return vm_map_min(map
);
731 return vm_map_max(map
);
743 get_vmsubmap_entries(
745 vm_object_offset_t start
,
746 vm_object_offset_t end
)
748 int total_entries
= 0;
749 vm_map_entry_t entry
;
754 entry
= vm_map_first_entry(map
);
755 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< start
)) {
756 entry
= entry
->vme_next
;
759 while ((entry
!= vm_map_to_entry(map
)) && (entry
->vme_start
< end
)) {
760 if (entry
->is_sub_map
) {
762 get_vmsubmap_entries(VME_SUBMAP(entry
),
770 entry
= entry
->vme_next
;
775 return total_entries
;
782 int total_entries
= 0;
783 vm_map_entry_t entry
;
788 entry
= vm_map_first_entry(map
);
790 while (entry
!= vm_map_to_entry(map
)) {
791 if (entry
->is_sub_map
) {
793 get_vmsubmap_entries(VME_SUBMAP(entry
),
801 entry
= entry
->vme_next
;
806 return total_entries
;
808 #endif /* CONFIG_COREDUMP */
820 return task
->user_stop_count
;
830 return th
->user_stop_count
;
837 get_task_pidsuspended(
840 return task
->pidsuspended
;
860 return (th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
;
864 * This routine is like thread_should_abort() above. It checks to
865 * see if the current thread is aborted. But unlike above, it also
866 * checks to see if thread is safely aborted. If so, it returns
867 * that fact, and clears the condition (safe aborts only should
868 * have a single effect, and a poll of the abort status
872 current_thread_aborted(
875 thread_t th
= current_thread();
878 if ((th
->sched_flags
& TH_SFLAG_ABORTED_MASK
) == TH_SFLAG_ABORT
&&
879 (th
->options
& TH_OPT_INTMASK
) != THREAD_UNINT
) {
882 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
) {
885 if (th
->sched_flags
& TH_SFLAG_ABORTSAFELY
) {
886 th
->sched_flags
&= ~TH_SFLAG_ABORTED_MASK
;
898 task_act_iterate_wth_args(
900 void (*func_callback
)(thread_t
, void *),
907 for (inc
= (thread_t
)(void *)queue_first(&task
->threads
);
908 !queue_end(&task
->threads
, (queue_entry_t
)inc
);) {
909 (void) (*func_callback
)(inc
, func_arg
);
910 inc
= (thread_t
)(void *)queue_next(&inc
->task_threads
);
917 #include <sys/bsdtask_info.h>
920 fill_taskprocinfo(task_t task
, struct proc_taskinfo_internal
* ptinfo
)
923 task_absolutetime_info_data_t tinfo
;
925 uint32_t cswitch
= 0, numrunning
= 0;
926 uint32_t syscalls_unix
= 0;
927 uint32_t syscalls_mach
= 0;
931 map
= (task
== kernel_task
)? kernel_map
: task
->map
;
933 ptinfo
->pti_virtual_size
= map
->size
;
934 ptinfo
->pti_resident_size
=
935 (mach_vm_size_t
)(pmap_resident_count(map
->pmap
))
938 ptinfo
->pti_policy
= ((task
!= kernel_task
)?
939 POLICY_TIMESHARE
: POLICY_RR
);
941 tinfo
.threads_user
= tinfo
.threads_system
= 0;
942 tinfo
.total_user
= task
->total_user_time
;
943 tinfo
.total_system
= task
->total_system_time
;
945 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
949 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
956 if ((thread
->state
& TH_RUN
) == TH_RUN
) {
959 cswitch
+= thread
->c_switch
;
960 tval
= timer_grab(&thread
->user_timer
);
961 tinfo
.threads_user
+= tval
;
962 tinfo
.total_user
+= tval
;
964 tval
= timer_grab(&thread
->system_timer
);
966 if (thread
->precise_user_kernel_time
) {
967 tinfo
.threads_system
+= tval
;
968 tinfo
.total_system
+= tval
;
970 /* system_timer may represent either sys or user */
971 tinfo
.threads_user
+= tval
;
972 tinfo
.total_user
+= tval
;
975 syscalls_unix
+= thread
->syscalls_unix
;
976 syscalls_mach
+= thread
->syscalls_mach
;
978 thread_unlock(thread
);
982 ptinfo
->pti_total_system
= tinfo
.total_system
;
983 ptinfo
->pti_total_user
= tinfo
.total_user
;
984 ptinfo
->pti_threads_system
= tinfo
.threads_system
;
985 ptinfo
->pti_threads_user
= tinfo
.threads_user
;
987 ptinfo
->pti_faults
= task
->faults
;
988 ptinfo
->pti_pageins
= task
->pageins
;
989 ptinfo
->pti_cow_faults
= task
->cow_faults
;
990 ptinfo
->pti_messages_sent
= task
->messages_sent
;
991 ptinfo
->pti_messages_received
= task
->messages_received
;
992 ptinfo
->pti_syscalls_mach
= task
->syscalls_mach
+ syscalls_mach
;
993 ptinfo
->pti_syscalls_unix
= task
->syscalls_unix
+ syscalls_unix
;
994 ptinfo
->pti_csw
= task
->c_switch
+ cswitch
;
995 ptinfo
->pti_threadnum
= task
->thread_count
;
996 ptinfo
->pti_numrunning
= numrunning
;
997 ptinfo
->pti_priority
= task
->priority
;
1003 fill_taskthreadinfo(task_t task
, uint64_t thaddr
, bool thuniqueid
, struct proc_threadinfo_internal
* ptinfo
, void * vpp
, int *vidp
)
1007 mach_msg_type_number_t count
;
1008 thread_basic_info_data_t basic_info
;
1014 for (thact
= (thread_t
)(void *)queue_first(&task
->threads
);
1015 !queue_end(&task
->threads
, (queue_entry_t
)thact
);) {
1016 addr
= (thuniqueid
) ? thact
->thread_id
: thact
->machine
.cthread_self
;
1017 if (addr
== thaddr
) {
1018 count
= THREAD_BASIC_INFO_COUNT
;
1019 if ((kret
= thread_info_internal(thact
, THREAD_BASIC_INFO
, (thread_info_t
)&basic_info
, &count
)) != KERN_SUCCESS
) {
1023 ptinfo
->pth_user_time
= (((uint64_t)basic_info
.user_time
.seconds
* NSEC_PER_SEC
) + ((uint64_t)basic_info
.user_time
.microseconds
* NSEC_PER_USEC
));
1024 ptinfo
->pth_system_time
= (((uint64_t)basic_info
.system_time
.seconds
* NSEC_PER_SEC
) + ((uint64_t)basic_info
.system_time
.microseconds
* NSEC_PER_USEC
));
1026 ptinfo
->pth_cpu_usage
= basic_info
.cpu_usage
;
1027 ptinfo
->pth_policy
= basic_info
.policy
;
1028 ptinfo
->pth_run_state
= basic_info
.run_state
;
1029 ptinfo
->pth_flags
= basic_info
.flags
;
1030 ptinfo
->pth_sleep_time
= basic_info
.sleep_time
;
1031 ptinfo
->pth_curpri
= thact
->sched_pri
;
1032 ptinfo
->pth_priority
= thact
->base_pri
;
1033 ptinfo
->pth_maxpriority
= thact
->max_priority
;
1035 if ((vpp
!= NULL
) && (thact
->uthread
!= NULL
)) {
1036 bsd_threadcdir(thact
->uthread
, vpp
, vidp
);
1038 bsd_getthreadname(thact
->uthread
, ptinfo
->pth_name
);
1042 thact
= (thread_t
)(void *)queue_next(&thact
->task_threads
);
1052 fill_taskthreadlist(task_t task
, void * buffer
, int thcount
, bool thuniqueid
)
1059 uptr
= (uint64_t *)buffer
;
1063 for (thact
= (thread_t
)(void *)queue_first(&task
->threads
);
1064 !queue_end(&task
->threads
, (queue_entry_t
)thact
);) {
1065 thaddr
= (thuniqueid
) ? thact
->thread_id
: thact
->machine
.cthread_self
;
1068 if (numthr
>= thcount
) {
1071 thact
= (thread_t
)(void *)queue_next(&thact
->task_threads
);
1076 return (int)(numthr
* sizeof(uint64_t));
1080 get_numthreads(task_t task
)
1082 return task
->thread_count
;
1086 * Gather the various pieces of info about the designated task,
1087 * and collect it all into a single rusage_info.
1090 fill_task_rusage(task_t task
, rusage_info_current
*ri
)
1092 struct task_power_info powerinfo
;
1094 uint64_t runnable_time
= 0;
1096 assert(task
!= TASK_NULL
);
1099 task_power_info_locked(task
, &powerinfo
, NULL
, NULL
, &runnable_time
);
1100 ri
->ri_pkg_idle_wkups
= powerinfo
.task_platform_idle_wakeups
;
1101 ri
->ri_interrupt_wkups
= powerinfo
.task_interrupt_wakeups
;
1102 ri
->ri_user_time
= powerinfo
.total_user
;
1103 ri
->ri_system_time
= powerinfo
.total_system
;
1104 ri
->ri_runnable_time
= runnable_time
;
1106 ledger_get_balance(task
->ledger
, task_ledgers
.phys_footprint
,
1107 (ledger_amount_t
*)&ri
->ri_phys_footprint
);
1108 ledger_get_balance(task
->ledger
, task_ledgers
.phys_mem
,
1109 (ledger_amount_t
*)&ri
->ri_resident_size
);
1110 ledger_get_balance(task
->ledger
, task_ledgers
.wired_mem
,
1111 (ledger_amount_t
*)&ri
->ri_wired_size
);
1113 ri
->ri_pageins
= task
->pageins
;
1120 fill_task_billed_usage(task_t task __unused
, rusage_info_current
*ri
)
1122 bank_billed_balance_safe(task
, &ri
->ri_billed_system_time
, &ri
->ri_billed_energy
);
1123 bank_serviced_balance_safe(task
, &ri
->ri_serviced_system_time
, &ri
->ri_serviced_energy
);
1127 fill_task_io_rusage(task_t task
, rusage_info_current
*ri
)
1129 assert(task
!= TASK_NULL
);
1132 if (task
->task_io_stats
) {
1133 ri
->ri_diskio_bytesread
= task
->task_io_stats
->disk_reads
.size
;
1134 ri
->ri_diskio_byteswritten
= (task
->task_io_stats
->total_io
.size
- task
->task_io_stats
->disk_reads
.size
);
1136 /* I/O Stats unavailable */
1137 ri
->ri_diskio_bytesread
= 0;
1138 ri
->ri_diskio_byteswritten
= 0;
1145 fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
)
1149 assert(task
!= TASK_NULL
);
1152 /* Rollup QoS time of all the threads to task */
1153 queue_iterate(&task
->threads
, thread
, thread_t
, task_threads
) {
1154 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
1158 thread_update_qos_cpu_time(thread
);
1160 ri
->ri_cpu_time_qos_default
= task
->cpu_time_eqos_stats
.cpu_time_qos_default
;
1161 ri
->ri_cpu_time_qos_maintenance
= task
->cpu_time_eqos_stats
.cpu_time_qos_maintenance
;
1162 ri
->ri_cpu_time_qos_background
= task
->cpu_time_eqos_stats
.cpu_time_qos_background
;
1163 ri
->ri_cpu_time_qos_utility
= task
->cpu_time_eqos_stats
.cpu_time_qos_utility
;
1164 ri
->ri_cpu_time_qos_legacy
= task
->cpu_time_eqos_stats
.cpu_time_qos_legacy
;
1165 ri
->ri_cpu_time_qos_user_initiated
= task
->cpu_time_eqos_stats
.cpu_time_qos_user_initiated
;
1166 ri
->ri_cpu_time_qos_user_interactive
= task
->cpu_time_eqos_stats
.cpu_time_qos_user_interactive
;
1173 fill_task_monotonic_rusage(task_t task
, rusage_info_current
*ri
)
1176 if (!mt_core_supported
) {
1180 assert(task
!= TASK_NULL
);
1182 uint64_t counts
[MT_CORE_NFIXED
] = { 0 };
1183 mt_fixed_task_counts(task
, counts
);
1184 #ifdef MT_CORE_INSTRS
1185 ri
->ri_instructions
= counts
[MT_CORE_INSTRS
];
1186 #endif /* defined(MT_CORE_INSTRS) */
1187 ri
->ri_cycles
= counts
[MT_CORE_CYCLES
];
1188 #else /* MONOTONIC */
1189 #pragma unused(task, ri)
1190 #endif /* !MONOTONIC */
1194 get_task_logical_writes(task_t task
, boolean_t external
)
1196 assert(task
!= TASK_NULL
);
1197 struct ledger_entry_info lei
;
1201 if (external
== FALSE
) {
1202 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
1204 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes_to_external
, &lei
);
1207 ledger_get_entry_info(task
->ledger
, task_ledgers
.logical_writes
, &lei
);
1210 return lei
.lei_balance
;
1214 get_task_dispatchqueue_serialno_offset(task_t task
)
1216 uint64_t dq_serialno_offset
= 0;
1218 if (task
->bsd_info
) {
1219 dq_serialno_offset
= get_dispatchqueue_serialno_offset_from_proc(task
->bsd_info
);
1222 return dq_serialno_offset
;
1226 get_task_dispatchqueue_label_offset(task_t task
)
1228 uint64_t dq_label_offset
= 0;
1230 if (task
->bsd_info
) {
1231 dq_label_offset
= get_dispatchqueue_label_offset_from_proc(task
->bsd_info
);
1234 return dq_label_offset
;
1238 get_task_uniqueid(task_t task
)
1240 if (task
->bsd_info
) {
1241 return proc_uniqueid(task
->bsd_info
);
1248 get_task_version(task_t task
)
1250 if (task
->bsd_info
) {
1251 return proc_pidversion(task
->bsd_info
);
1259 get_task_crash_label(task_t task
)
1261 return task
->crash_label
;
1266 fill_taskipctableinfo(task_t task
, uint32_t *table_size
, uint32_t *table_free
)
1268 ipc_space_t space
= task
->itk_space
;
1269 if (space
== NULL
) {
1273 is_read_lock(space
);
1274 if (!is_active(space
)) {
1275 is_read_unlock(space
);
1279 *table_size
= space
->is_table_size
;
1280 *table_free
= space
->is_table_free
;
1282 is_read_unlock(space
);
1288 get_task_cdhash(task_t task
, char cdhash
[static CS_CDHASH_LEN
])
1293 result
= task
->bsd_info
? proc_getcdhash(task
->bsd_info
, cdhash
) : ESRCH
;