1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2016 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions #include <kperf/task_samplers.h>
30*2c2f96dcSApple OSS Distributions #include <kperf/context.h>
31*2c2f96dcSApple OSS Distributions #include <kperf/buffer.h>
32*2c2f96dcSApple OSS Distributions #include <kern/thread.h>
33*2c2f96dcSApple OSS Distributions
34*2c2f96dcSApple OSS Distributions #include <kern/task.h>
35*2c2f96dcSApple OSS Distributions
36*2c2f96dcSApple OSS Distributions extern void memorystatus_proc_flags_unsafe(void * v, boolean_t *is_dirty,
37*2c2f96dcSApple OSS Distributions boolean_t *is_dirty_tracked, boolean_t *allow_idle_exit);
38*2c2f96dcSApple OSS Distributions
39*2c2f96dcSApple OSS Distributions void
kperf_task_snapshot_sample(task_t task,struct kperf_task_snapshot * tksn)40*2c2f96dcSApple OSS Distributions kperf_task_snapshot_sample(task_t task, struct kperf_task_snapshot *tksn)
41*2c2f96dcSApple OSS Distributions {
42*2c2f96dcSApple OSS Distributions BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_START);
43*2c2f96dcSApple OSS Distributions
44*2c2f96dcSApple OSS Distributions assert(tksn != NULL);
45*2c2f96dcSApple OSS Distributions
46*2c2f96dcSApple OSS Distributions tksn->kptksn_flags = 0;
47*2c2f96dcSApple OSS Distributions if (task->effective_policy.tep_darwinbg) {
48*2c2f96dcSApple OSS Distributions tksn->kptksn_flags |= KPERF_TASK_FLAG_DARWIN_BG;
49*2c2f96dcSApple OSS Distributions }
50*2c2f96dcSApple OSS Distributions if (task->requested_policy.trp_role == TASK_FOREGROUND_APPLICATION) {
51*2c2f96dcSApple OSS Distributions tksn->kptksn_flags |= KPERF_TASK_FLAG_FOREGROUND;
52*2c2f96dcSApple OSS Distributions }
53*2c2f96dcSApple OSS Distributions if (task->requested_policy.trp_boosted == 1) {
54*2c2f96dcSApple OSS Distributions tksn->kptksn_flags |= KPERF_TASK_FLAG_BOOSTED;
55*2c2f96dcSApple OSS Distributions }
56*2c2f96dcSApple OSS Distributions #if CONFIG_MEMORYSTATUS
57*2c2f96dcSApple OSS Distributions boolean_t dirty = FALSE, dirty_tracked = FALSE, allow_idle_exit = FALSE;
58*2c2f96dcSApple OSS Distributions memorystatus_proc_flags_unsafe(get_bsdtask_info(task), &dirty, &dirty_tracked, &allow_idle_exit);
59*2c2f96dcSApple OSS Distributions if (dirty) {
60*2c2f96dcSApple OSS Distributions tksn->kptksn_flags |= KPERF_TASK_FLAG_DIRTY;
61*2c2f96dcSApple OSS Distributions }
62*2c2f96dcSApple OSS Distributions if (dirty_tracked) {
63*2c2f96dcSApple OSS Distributions tksn->kptksn_flags |= KPERF_TASK_FLAG_DIRTY_TRACKED;
64*2c2f96dcSApple OSS Distributions }
65*2c2f96dcSApple OSS Distributions if (allow_idle_exit) {
66*2c2f96dcSApple OSS Distributions tksn->kptksn_flags |= KPERF_TASK_ALLOW_IDLE_EXIT;
67*2c2f96dcSApple OSS Distributions }
68*2c2f96dcSApple OSS Distributions #endif
69*2c2f96dcSApple OSS Distributions
70*2c2f96dcSApple OSS Distributions tksn->kptksn_suspend_count = task->suspend_count;
71*2c2f96dcSApple OSS Distributions tksn->kptksn_pageins = (integer_t) MIN(counter_load(&task->pageins), INT32_MAX);
72*2c2f96dcSApple OSS Distributions struct recount_times_mach times = recount_task_terminated_times(task);
73*2c2f96dcSApple OSS Distributions tksn->kptksn_user_time_in_terminated_threads = times.rtm_user;
74*2c2f96dcSApple OSS Distributions tksn->kptksn_system_time_in_terminated_threads = times.rtm_system;
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS Distributions BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_END);
77*2c2f96dcSApple OSS Distributions }
78*2c2f96dcSApple OSS Distributions
79*2c2f96dcSApple OSS Distributions void
kperf_task_snapshot_log(struct kperf_task_snapshot * tksn)80*2c2f96dcSApple OSS Distributions kperf_task_snapshot_log(struct kperf_task_snapshot *tksn)
81*2c2f96dcSApple OSS Distributions {
82*2c2f96dcSApple OSS Distributions assert(tksn != NULL);
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions #if defined(__LP64__)
85*2c2f96dcSApple OSS Distributions BUF_DATA(PERF_TK_SNAP_DATA, tksn->kptksn_flags,
86*2c2f96dcSApple OSS Distributions ENCODE_UPPER_64(tksn->kptksn_suspend_count) |
87*2c2f96dcSApple OSS Distributions ENCODE_LOWER_64(tksn->kptksn_pageins),
88*2c2f96dcSApple OSS Distributions tksn->kptksn_user_time_in_terminated_threads,
89*2c2f96dcSApple OSS Distributions tksn->kptksn_system_time_in_terminated_threads);
90*2c2f96dcSApple OSS Distributions #else
91*2c2f96dcSApple OSS Distributions BUF_DATA(PERF_TK_SNAP_DATA1_32, UPPER_32(tksn->kptksn_flags),
92*2c2f96dcSApple OSS Distributions LOWER_32(tksn->kptksn_flags),
93*2c2f96dcSApple OSS Distributions tksn->kptksn_suspend_count,
94*2c2f96dcSApple OSS Distributions tksn->kptksn_pageins);
95*2c2f96dcSApple OSS Distributions BUF_DATA(PERF_TK_SNAP_DATA2_32, UPPER_32(tksn->kptksn_user_time_in_terminated_threads),
96*2c2f96dcSApple OSS Distributions LOWER_32(tksn->kptksn_user_time_in_terminated_threads),
97*2c2f96dcSApple OSS Distributions UPPER_32(tksn->kptksn_system_time_in_terminated_threads),
98*2c2f96dcSApple OSS Distributions LOWER_32(tksn->kptksn_system_time_in_terminated_threads));
99*2c2f96dcSApple OSS Distributions #endif /* defined(__LP64__) */
100*2c2f96dcSApple OSS Distributions }
101*2c2f96dcSApple OSS Distributions
102*2c2f96dcSApple OSS Distributions void
kperf_task_info_log(struct kperf_context * ctx)103*2c2f96dcSApple OSS Distributions kperf_task_info_log(struct kperf_context *ctx)
104*2c2f96dcSApple OSS Distributions {
105*2c2f96dcSApple OSS Distributions assert(ctx != NULL);
106*2c2f96dcSApple OSS Distributions
107*2c2f96dcSApple OSS Distributions BUF_DATA(PERF_TK_INFO_DATA, ctx->cur_pid);
108*2c2f96dcSApple OSS Distributions }
109