]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kperf/task_samplers.c
xnu-3789.1.32.tar.gz
[apple/xnu.git] / osfmk / kperf / task_samplers.c
CommitLineData
39037602
A
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 boolean_t workqueue_get_pwq_exceeded(void *v, boolean_t *exceeded_total,
37 boolean_t *exceeded_constrained);
38extern boolean_t memorystatus_proc_is_dirty_unsafe(void *v);
39
40void
41kperf_task_snapshot_sample(struct kperf_task_snapshot *tksn,
42 struct kperf_context *ctx)
43{
44 thread_t thread;
45 task_t task;
46 boolean_t wq_state_available = FALSE;
47 boolean_t exceeded_total, exceeded_constrained;
48
49 BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_START);
50
51 assert(tksn != NULL);
52 assert(ctx != NULL);
53
54 thread = ctx->cur_thread;
55 task = get_threadtask(thread);
56
57 tksn->kptksn_flags = 0;
58 if (task->effective_policy.tep_darwinbg) {
59 tksn->kptksn_flags |= KPERF_TASK_FLAG_DARWIN_BG;
60 }
61 if (task->requested_policy.trp_role == TASK_FOREGROUND_APPLICATION) {
62 tksn->kptksn_flags |= KPERF_TASK_FLAG_FOREGROUND;
63 }
64 if (task->requested_policy.trp_boosted == 1) {
65 tksn->kptksn_flags |= KPERF_TASK_FLAG_BOOSTED;
66 }
67#if CONFIG_MEMORYSTATUS
68 if (memorystatus_proc_is_dirty_unsafe(task->bsd_info)) {
69 tksn->kptksn_flags |= KPERF_TASK_FLAG_DIRTY;
70 }
71#endif
72
73 if (task->bsd_info) {
74 wq_state_available =
75 workqueue_get_pwq_exceeded(task->bsd_info, &exceeded_total,
76 &exceeded_constrained);
77 }
78 if (wq_state_available) {
79 tksn->kptksn_flags |= KPERF_TASK_FLAG_WQ_FLAGS_VALID;
80
81 if (exceeded_total) {
82 tksn->kptksn_flags |= KPERF_TASK_FLAG_WQ_EXCEEDED_TOTAL;
83 }
84 if (exceeded_constrained) {
85 tksn->kptksn_flags |= KPERF_TASK_FLAG_WQ_EXCEEDED_CONSTRAINED;
86 }
87 }
88
89 tksn->kptksn_suspend_count = task->suspend_count;
90 tksn->kptksn_pageins = task->pageins;
91 tksn->kptksn_user_time_in_terminated_threads = task->total_user_time;
92 tksn->kptksn_system_time_in_terminated_threads = task->total_system_time;
93
94 BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_END);
95}
96
97void
98kperf_task_snapshot_log(struct kperf_task_snapshot *tksn)
99{
100 assert(tksn != NULL);
101
102#if defined(__LP64__)
103 BUF_DATA(PERF_TK_SNAP_DATA, tksn->kptksn_flags,
104 ENCODE_UPPER_64(tksn->kptksn_suspend_count) |
105 ENCODE_LOWER_64(tksn->kptksn_pageins),
106 tksn->kptksn_user_time_in_terminated_threads,
107 tksn->kptksn_system_time_in_terminated_threads);
108#else
109 BUF_DATA(PERF_TK_SNAP_DATA1_32, UPPER_32(tksn->kptksn_flags),
110 LOWER_32(tksn->kptksn_flags),
111 tksn->kptksn_suspend_count,
112 tksn->kptksn_pageins);
113 BUF_DATA(PERF_TK_SNAP_DATA2_32, UPPER_32(tksn->kptksn_user_time_in_terminated_threads),
114 LOWER_32(tksn->kptksn_user_time_in_terminated_threads),
115 UPPER_32(tksn->kptksn_system_time_in_terminated_threads),
116 LOWER_32(tksn->kptksn_system_time_in_terminated_threads));
117#endif /* defined(__LP64__) */
118}