2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * File: vm/task_working_set.h
28 * Author: Chris Youngworth
31 * Working set detection and maintainence module
35 #ifndef _VM_TASK_WORKING_SET_H_
36 #define _VM_TASK_WORKING_SET_H_
38 #include <mach/mach_types.h>
42 #ifdef MACH_KERNEL_PRIVATE
44 #include <kern/queue.h>
45 #include <vm/vm_object.h>
47 /* task working set */
49 #define tws_lock(tws) mutex_lock(&(tws)->lock)
50 #define tws_lock_try(tws) mutex_try(&(tws)->lock)
51 #define tws_unlock(tws) mutex_unlock(&(tws)->lock)
54 #define TWS_ARRAY_SIZE 8
55 #define TWS_HASH_LINE_COUNT 32
56 /* start out size to allow monitoring of working set without excessive use */
57 /* of wired memory resource. */
58 #define TWS_SMALL_HASH_LINE_COUNT 4
61 * do not think of changing this hash unless you understand the implications
62 * for the hash element page_cache field
64 #define do_tws_hash(object,offset, rows, lines) \
65 ((((((natural_t)(object)) + \
66 (((natural_t)(object)) >> 6) + \
67 (((natural_t)(object)) >> 12) + \
68 (((natural_t)(object)) >> 18) + \
69 (((natural_t)(object)) >> 24)) << 5) + \
70 ((natural_t)(((vm_object_offset_t)(offset)) >> 17))) & \
74 #define alt_tws_hash(addr, rows, lines) \
75 ((((natural_t)(addr)) >> 17) & \
79 /* Long term startup data structures for initial cache filling */
81 #define TWS_STARTUP_MAX_HASH_RETRY 3
83 /* 87 is the wrap skew, its based on RETRY times the RETRY offset of 29 */
85 #define do_startup_hash(addr, hash_size) \
86 ((((((natural_t)(addr)) >> 17) & \
87 ((2 * (hash_size)) -1)) + \
88 (87 * (((addr) & TWS_ADDR_OFF_MASK)/(2 * (hash_size))))) & \
89 ((2 * (hash_size)) -1))
91 #define do_startup_hash(addr, hash_size) \
92 (((((natural_t)(addr)) >> 17) * 3) & \
97 struct tws_startup_ele
{
98 unsigned int page_cache
;
99 vm_offset_t page_addr
;
102 typedef struct tws_startup_ele
*tws_startup_ele_t
;
105 struct tws_startup_ptr
{
106 tws_startup_ele_t element
;
107 struct tws_startup_ptr
*next
;
110 typedef struct tws_startup_ptr
*tws_startup_ptr_t
;
113 unsigned int tws_hash_size
; /* total size of struct in bytes */
114 unsigned int ele_count
;
115 unsigned int array_size
; /* lines * rows * expansion_count */
116 unsigned int hash_count
;
118 tws_startup_ptr_t
*table
; /* hash table */
119 struct tws_startup_ptr
*ele
; /* hash elements */
120 struct tws_startup_ele
*array
;
123 typedef struct tws_startup
*tws_startup_t
;
126 /* Dynamic cache data structures for working set */
128 struct tws_hash_ele
{
130 vm_object_offset_t offset
;
131 unsigned int page_cache
;
132 vm_offset_t page_addr
;
136 typedef struct tws_hash_ele
*tws_hash_ele_t
;
138 #define TWS_HASH_OFF_MASK ((vm_object_offset_t)0xFFFFFFFFFFFE0000ULL)
139 #define TWS_ADDR_OFF_MASK ((vm_offset_t)0xFFFE0000)
140 #define TWS_INDEX_MASK ((vm_object_offset_t)0x000000000001F000ULL)
142 struct tws_hash_ptr
{
143 tws_hash_ele_t element
;
144 struct tws_hash_ptr
*next
;
146 typedef struct tws_hash_ptr
*tws_hash_ptr_t
;
148 struct tws_hash_line
{
149 unsigned int ele_count
;
150 struct tws_hash_ele list
[TWS_ARRAY_SIZE
];
152 typedef struct tws_hash_line
*tws_hash_line_t
;
154 #define TWS_HASH_STYLE_DEFAULT 0x0
155 #define TWS_HASH_STYLE_BASIC 0x1
156 #define TWS_HASH_STYLE_SIGNAL 0x2
159 #define TWS_ADDR_HASH 1
160 #define TWS_HASH_EXPANSION_MAX 10
161 #define TWS_MAX_REHASH 3
165 decl_mutex_data(,lock
) /* tws_hash's lock */
168 unsigned int current_line
;
169 unsigned int pageout_count
;
170 unsigned int line_count
;
172 unsigned int number_of_lines
;
173 unsigned int number_of_elements
;
174 unsigned int expansion_count
;
175 unsigned int time_of_creation
;
177 unsigned int lookup_count
;
178 unsigned int insert_count
;
180 tws_startup_t startup_cache
;
182 int startup_name_length
;
187 unsigned int obj_free_count
[TWS_HASH_EXPANSION_MAX
];
188 unsigned int addr_free_count
[TWS_HASH_EXPANSION_MAX
];
189 tws_hash_ptr_t free_hash_ele
[TWS_HASH_EXPANSION_MAX
];
190 tws_hash_ptr_t
*table
[TWS_HASH_EXPANSION_MAX
];
191 tws_hash_ptr_t table_ele
[TWS_HASH_EXPANSION_MAX
];
192 tws_hash_ptr_t alt_ele
[TWS_HASH_EXPANSION_MAX
];
193 struct tws_hash_line
*cache
[TWS_HASH_EXPANSION_MAX
];
196 typedef struct tws_hash
*tws_hash_t
;
199 extern kern_return_t
tws_lookup(
201 vm_object_offset_t offset
,
203 tws_hash_line_t
*line
);
205 extern kern_return_t
tws_insert(
207 vm_object_offset_t offset
,
209 vm_offset_t page_addr
,
212 extern void tws_build_cluster(
215 vm_object_offset_t
*start
,
216 vm_object_offset_t
*end
,
217 vm_size_t max_length
);
219 extern void tws_line_signal(
222 tws_hash_line_t hash_line
,
223 vm_offset_t target_page
);
225 extern void tws_hash_destroy(
228 extern void tws_hash_ws_flush(
231 extern kern_return_t
tws_expand_working_set(
233 unsigned int line_count
,
234 boolean_t dump_data
);
236 extern kern_return_t
task_working_set_create(
242 #endif /* MACH_KERNEL_PRIVATE */
244 extern kern_return_t
tws_handle_startup_file(
249 boolean_t
*new_info
);
251 extern kern_return_t
tws_send_startup_info(
254 #endif /* KERNEL_PRIVATE */
256 #endif /* _VM_TASK_WORKING_SET_H_ */