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 void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty,
37 boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit, boolean_t *is_active,
38 boolean_t *is_managed, boolean_t *has_assertion);
39
40 void
kperf_task_snapshot_sample(task_t task,struct kperf_task_snapshot * tksn)41 kperf_task_snapshot_sample(task_t task, struct kperf_task_snapshot *tksn)
42 {
43 BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_START);
44
45 assert(tksn != NULL);
46
47 tksn->kptksn_flags = 0;
48 if (task->effective_policy.tep_darwinbg) {
49 tksn->kptksn_flags |= KPERF_TASK_FLAG_DARWIN_BG;
50 }
51 if (task->requested_policy.trp_role == TASK_FOREGROUND_APPLICATION) {
52 tksn->kptksn_flags |= KPERF_TASK_FLAG_FOREGROUND;
53 }
54 if (task->requested_policy.trp_boosted == 1) {
55 tksn->kptksn_flags |= KPERF_TASK_FLAG_BOOSTED;
56 }
57 #if CONFIG_MEMORYSTATUS
58 boolean_t dirty = FALSE, dirty_tracked = FALSE, allow_idle_exit = FALSE;
59 boolean_t is_active = FALSE, is_managed = FALSE, has_assertion = FALSE;
60 memorystatus_proc_flags_unsafe(get_bsdtask_info(task), &dirty, &dirty_tracked, &allow_idle_exit, &is_active, &is_managed, &has_assertion);
61 if (dirty) {
62 tksn->kptksn_flags |= KPERF_TASK_FLAG_DIRTY;
63 }
64 if (dirty_tracked) {
65 tksn->kptksn_flags |= KPERF_TASK_FLAG_DIRTY_TRACKED;
66 }
67 if (allow_idle_exit) {
68 tksn->kptksn_flags |= KPERF_TASK_ALLOW_IDLE_EXIT;
69 }
70 if (is_active) {
71 tksn->kptksn_flags |= KPERF_TASK_FLAG_ACTIVE;
72 }
73 if (is_managed) {
74 tksn->kptksn_flags |= KPERF_TASK_FLAG_MANAGED;
75 }
76 if (has_assertion) {
77 tksn->kptksn_flags |= KPERF_TASK_FLAG_HAS_ASSERTION;
78 }
79 #endif
80
81 tksn->kptksn_suspend_count = task->suspend_count;
82 tksn->kptksn_pageins = (integer_t) MIN(counter_load(&task->pageins), INT32_MAX);
83 struct recount_times_mach times = recount_task_terminated_times(task);
84 tksn->kptksn_user_time_in_terminated_threads = times.rtm_user;
85 tksn->kptksn_system_time_in_terminated_threads = times.rtm_system;
86
87 BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_END);
88 }
89
90 void
kperf_task_snapshot_log(struct kperf_task_snapshot * tksn)91 kperf_task_snapshot_log(struct kperf_task_snapshot *tksn)
92 {
93 assert(tksn != NULL);
94
95 #if defined(__LP64__)
96 BUF_DATA(PERF_TK_SNAP_DATA, tksn->kptksn_flags,
97 ENCODE_UPPER_64(tksn->kptksn_suspend_count) |
98 ENCODE_LOWER_64(tksn->kptksn_pageins),
99 tksn->kptksn_user_time_in_terminated_threads,
100 tksn->kptksn_system_time_in_terminated_threads);
101 #else
102 BUF_DATA(PERF_TK_SNAP_DATA1_32, UPPER_32(tksn->kptksn_flags),
103 LOWER_32(tksn->kptksn_flags),
104 tksn->kptksn_suspend_count,
105 tksn->kptksn_pageins);
106 BUF_DATA(PERF_TK_SNAP_DATA2_32, UPPER_32(tksn->kptksn_user_time_in_terminated_threads),
107 LOWER_32(tksn->kptksn_user_time_in_terminated_threads),
108 UPPER_32(tksn->kptksn_system_time_in_terminated_threads),
109 LOWER_32(tksn->kptksn_system_time_in_terminated_threads));
110 #endif /* defined(__LP64__) */
111 }
112
113 void
kperf_task_info_log(struct kperf_context * ctx)114 kperf_task_info_log(struct kperf_context *ctx)
115 {
116 assert(ctx != NULL);
117
118 BUF_DATA(PERF_TK_INFO_DATA, ctx->cur_pid);
119 }
120