2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * File: vm/task_working_set.h
35 * Author: Chris Youngworth
38 * Working set detection and maintainence module
42 #ifndef _VM_TASK_WORKING_SET_H_
43 #define _VM_TASK_WORKING_SET_H_
45 #include <mach/mach_types.h>
49 #ifdef MACH_KERNEL_PRIVATE
51 #include <kern/queue.h>
52 #include <vm/vm_object.h>
54 /* task working set */
56 #define tws_lock(tws) mutex_lock(&(tws)->lock)
57 #define tws_lock_try(tws) mutex_try(&(tws)->lock)
58 #define tws_unlock(tws) mutex_unlock(&(tws)->lock)
61 #define TWS_ARRAY_SIZE 8
62 #define TWS_HASH_LINE_COUNT 32
63 /* start out size to allow monitoring of working set without excessive use */
64 /* of wired memory resource. */
65 #define TWS_SMALL_HASH_LINE_COUNT 4
68 * do not think of changing this hash unless you understand the implications
69 * for the hash element page_cache field
71 #define do_tws_hash(object,offset, rows, lines) \
72 ((((((natural_t)(object)) + \
73 (((natural_t)(object)) >> 6) + \
74 (((natural_t)(object)) >> 12) + \
75 (((natural_t)(object)) >> 18) + \
76 (((natural_t)(object)) >> 24)) << 5) + \
77 ((natural_t)(((vm_object_offset_t)(offset)) >> 17))) & \
81 #define alt_tws_hash(addr, rows, lines) \
82 ((((natural_t)(addr)) >> 17) & \
86 /* Long term startup data structures for initial cache filling */
88 #define TWS_STARTUP_MAX_HASH_RETRY 3
90 /* 87 is the wrap skew, its based on RETRY times the RETRY offset of 29 */
92 #define do_startup_hash(addr, hash_size) \
93 ((((((natural_t)(addr)) >> 17) & \
94 ((2 * (hash_size)) -1)) + \
95 (87 * (((addr) & TWS_ADDR_OFF_MASK)/(2 * (hash_size))))) & \
96 ((2 * (hash_size)) -1))
98 #define do_startup_hash(addr, hash_size) \
99 (((((natural_t)(addr)) >> 17) * 3) & \
104 struct tws_startup_ele
{
105 unsigned int page_cache
;
106 vm_offset_t page_addr
;
109 typedef struct tws_startup_ele
*tws_startup_ele_t
;
112 struct tws_startup_ptr
{
113 tws_startup_ele_t element
;
114 struct tws_startup_ptr
*next
;
117 typedef struct tws_startup_ptr
*tws_startup_ptr_t
;
120 unsigned int tws_hash_size
; /* total size of struct in bytes */
121 unsigned int ele_count
;
122 unsigned int array_size
; /* lines * rows * expansion_count */
123 unsigned int hash_count
;
125 tws_startup_ptr_t
*table
; /* hash table */
126 struct tws_startup_ptr
*ele
; /* hash elements */
127 struct tws_startup_ele
*array
;
130 typedef struct tws_startup
*tws_startup_t
;
133 /* Dynamic cache data structures for working set */
135 struct tws_hash_ele
{
137 vm_object_offset_t offset
;
138 unsigned int page_cache
;
139 vm_offset_t page_addr
;
143 typedef struct tws_hash_ele
*tws_hash_ele_t
;
145 #define TWS_HASH_OFF_MASK ((vm_object_offset_t)0xFFFFFFFFFFFE0000ULL)
146 #define TWS_ADDR_OFF_MASK ((vm_offset_t)0xFFFE0000)
147 #define TWS_INDEX_MASK ((vm_object_offset_t)0x000000000001F000ULL)
149 struct tws_hash_ptr
{
150 tws_hash_ele_t element
;
151 struct tws_hash_ptr
*next
;
153 typedef struct tws_hash_ptr
*tws_hash_ptr_t
;
155 struct tws_hash_line
{
156 unsigned int ele_count
;
157 struct tws_hash_ele list
[TWS_ARRAY_SIZE
];
159 typedef struct tws_hash_line
*tws_hash_line_t
;
161 #define TWS_HASH_STYLE_DEFAULT 0x0
162 #define TWS_HASH_STYLE_BASIC 0x1
163 #define TWS_HASH_STYLE_SIGNAL 0x2
166 #define TWS_ADDR_HASH 1
167 #define TWS_HASH_EXPANSION_MAX 10
168 #define TWS_MAX_REHASH 3
172 decl_mutex_data(,lock
) /* tws_hash's lock */
175 unsigned int current_line
;
176 unsigned int pageout_count
;
177 unsigned int line_count
;
179 unsigned int number_of_lines
;
180 unsigned int number_of_elements
;
181 unsigned int expansion_count
;
182 unsigned int time_of_creation
;
184 unsigned int lookup_count
;
185 unsigned int insert_count
;
187 tws_startup_t startup_cache
;
189 int startup_name_length
;
194 unsigned int obj_free_count
[TWS_HASH_EXPANSION_MAX
];
195 unsigned int addr_free_count
[TWS_HASH_EXPANSION_MAX
];
196 tws_hash_ptr_t free_hash_ele
[TWS_HASH_EXPANSION_MAX
];
197 tws_hash_ptr_t
*table
[TWS_HASH_EXPANSION_MAX
];
198 tws_hash_ptr_t table_ele
[TWS_HASH_EXPANSION_MAX
];
199 tws_hash_ptr_t alt_ele
[TWS_HASH_EXPANSION_MAX
];
200 struct tws_hash_line
*cache
[TWS_HASH_EXPANSION_MAX
];
203 typedef struct tws_hash
*tws_hash_t
;
206 extern kern_return_t
tws_lookup(
208 vm_object_offset_t offset
,
210 tws_hash_line_t
*line
);
212 extern kern_return_t
tws_insert(
214 vm_object_offset_t offset
,
216 vm_offset_t page_addr
,
219 extern void tws_build_cluster(
222 vm_object_offset_t
*start
,
223 vm_object_offset_t
*end
,
224 vm_size_t max_length
);
226 extern void tws_line_signal(
229 tws_hash_line_t hash_line
,
230 vm_offset_t target_page
);
232 extern void tws_hash_destroy(
235 extern void tws_hash_ws_flush(
238 extern kern_return_t
tws_expand_working_set(
240 unsigned int line_count
,
241 boolean_t dump_data
);
243 extern kern_return_t
task_working_set_create(
249 #endif /* MACH_KERNEL_PRIVATE */
251 extern kern_return_t
tws_handle_startup_file(
256 boolean_t
*new_info
);
258 extern kern_return_t
tws_send_startup_info(
261 #endif /* KERNEL_PRIVATE */
263 #endif /* _VM_TASK_WORKING_SET_H_ */