]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kperf/task_samplers.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / osfmk / kperf / task_samplers.c
1 /*
2 * Copyright (c) 2016 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <kperf/task_samplers.h>
30 #include <kperf/context.h>
31 #include <kperf/buffer.h>
32 #include <kern/thread.h>
33
34 #include <kern/task.h>
35
36 extern boolean_t memorystatus_proc_is_dirty_unsafe(void *v);
37
38 void
39 kperf_task_snapshot_sample(struct kperf_task_snapshot *tksn,
40 struct kperf_context *ctx)
41 {
42 thread_t thread;
43 task_t task;
44
45 BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_START);
46
47 assert(tksn != NULL);
48 assert(ctx != NULL);
49
50 thread = ctx->cur_thread;
51 task = get_threadtask(thread);
52
53 tksn->kptksn_flags = 0;
54 if (task->effective_policy.tep_darwinbg) {
55 tksn->kptksn_flags |= KPERF_TASK_FLAG_DARWIN_BG;
56 }
57 if (task->requested_policy.trp_role == TASK_FOREGROUND_APPLICATION) {
58 tksn->kptksn_flags |= KPERF_TASK_FLAG_FOREGROUND;
59 }
60 if (task->requested_policy.trp_boosted == 1) {
61 tksn->kptksn_flags |= KPERF_TASK_FLAG_BOOSTED;
62 }
63 #if CONFIG_MEMORYSTATUS
64 if (memorystatus_proc_is_dirty_unsafe(task->bsd_info)) {
65 tksn->kptksn_flags |= KPERF_TASK_FLAG_DIRTY;
66 }
67 #endif
68
69 tksn->kptksn_suspend_count = task->suspend_count;
70 tksn->kptksn_pageins = task->pageins;
71 tksn->kptksn_user_time_in_terminated_threads = task->total_user_time;
72 tksn->kptksn_system_time_in_terminated_threads = task->total_system_time;
73
74 BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_END);
75 }
76
77 void
78 kperf_task_snapshot_log(struct kperf_task_snapshot *tksn)
79 {
80 assert(tksn != NULL);
81
82 #if defined(__LP64__)
83 BUF_DATA(PERF_TK_SNAP_DATA, tksn->kptksn_flags,
84 ENCODE_UPPER_64(tksn->kptksn_suspend_count) |
85 ENCODE_LOWER_64(tksn->kptksn_pageins),
86 tksn->kptksn_user_time_in_terminated_threads,
87 tksn->kptksn_system_time_in_terminated_threads);
88 #else
89 BUF_DATA(PERF_TK_SNAP_DATA1_32, UPPER_32(tksn->kptksn_flags),
90 LOWER_32(tksn->kptksn_flags),
91 tksn->kptksn_suspend_count,
92 tksn->kptksn_pageins);
93 BUF_DATA(PERF_TK_SNAP_DATA2_32, UPPER_32(tksn->kptksn_user_time_in_terminated_threads),
94 LOWER_32(tksn->kptksn_user_time_in_terminated_threads),
95 UPPER_32(tksn->kptksn_system_time_in_terminated_threads),
96 LOWER_32(tksn->kptksn_system_time_in_terminated_threads));
97 #endif /* defined(__LP64__) */
98 }