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