| 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); |
| 38 | |
| 39 | void |
| 40 | kperf_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(v: get_bsdtask_info(task), is_dirty: &dirty, is_dirty_tracked: &dirty_tracked, allow_idle_exit: &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 = (integer_t) MIN(counter_load(&task->pageins), INT32_MAX); |
| 72 | struct recount_times_mach times = recount_task_terminated_times(task); |
| 73 | tksn->kptksn_user_time_in_terminated_threads = times.rtm_user; |
| 74 | tksn->kptksn_system_time_in_terminated_threads = times.rtm_system; |
| 75 | |
| 76 | BUF_INFO(PERF_TK_SNAP_SAMPLE | DBG_FUNC_END); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | kperf_task_snapshot_log(struct kperf_task_snapshot *tksn) |
| 81 | { |
| 82 | assert(tksn != NULL); |
| 83 | |
| 84 | #if defined(__LP64__) |
| 85 | BUF_DATA(PERF_TK_SNAP_DATA, tksn->kptksn_flags, |
| 86 | ENCODE_UPPER_64(tksn->kptksn_suspend_count) | |
| 87 | ENCODE_LOWER_64(tksn->kptksn_pageins), |
| 88 | tksn->kptksn_user_time_in_terminated_threads, |
| 89 | tksn->kptksn_system_time_in_terminated_threads); |
| 90 | #else |
| 91 | BUF_DATA(PERF_TK_SNAP_DATA1_32, UPPER_32(tksn->kptksn_flags), |
| 92 | LOWER_32(tksn->kptksn_flags), |
| 93 | tksn->kptksn_suspend_count, |
| 94 | tksn->kptksn_pageins); |
| 95 | BUF_DATA(PERF_TK_SNAP_DATA2_32, UPPER_32(tksn->kptksn_user_time_in_terminated_threads), |
| 96 | LOWER_32(tksn->kptksn_user_time_in_terminated_threads), |
| 97 | UPPER_32(tksn->kptksn_system_time_in_terminated_threads), |
| 98 | LOWER_32(tksn->kptksn_system_time_in_terminated_threads)); |
| 99 | #endif /* defined(__LP64__) */ |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | kperf_task_info_log(struct kperf_context *ctx) |
| 104 | { |
| 105 | assert(ctx != NULL); |
| 106 | |
| 107 | BUF_DATA(PERF_TK_INFO_DATA, ctx->cur_pid); |
| 108 | } |
| 109 | |