2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * File: vm/task_working_set.h
33 * Author: Chris Youngworth
36 * Working set detection and maintainence module
40 #ifndef _VM_TASK_WORKING_SET_H_
41 #define _VM_TASK_WORKING_SET_H_
43 #include <mach/mach_types.h>
47 #ifdef MACH_KERNEL_PRIVATE
49 #include <kern/queue.h>
50 #include <vm/vm_object.h>
52 /* task working set */
54 #define tws_lock(tws) mutex_lock(&(tws)->lock)
55 #define tws_lock_try(tws) mutex_try(&(tws)->lock)
56 #define tws_unlock(tws) mutex_unlock(&(tws)->lock)
59 #define TWS_ARRAY_SIZE 8
60 #define TWS_HASH_LINE_COUNT 32
61 /* start out size to allow monitoring of working set without excessive use */
62 /* of wired memory resource. */
63 #define TWS_SMALL_HASH_LINE_COUNT 4
66 * do not think of changing this hash unless you understand the implications
67 * for the hash element page_cache field
69 #define do_tws_hash(object,offset, rows, lines) \
70 ((((((natural_t)(object)) + \
71 (((natural_t)(object)) >> 6) + \
72 (((natural_t)(object)) >> 12) + \
73 (((natural_t)(object)) >> 18) + \
74 (((natural_t)(object)) >> 24)) << 5) + \
75 ((natural_t)(((vm_object_offset_t)(offset)) >> 17))) & \
79 #define alt_tws_hash(addr, rows, lines) \
80 ((((natural_t)(addr)) >> 17) & \
84 /* Long term startup data structures for initial cache filling */
86 #define TWS_STARTUP_MAX_HASH_RETRY 3
88 /* 87 is the wrap skew, its based on RETRY times the RETRY offset of 29 */
90 #define do_startup_hash(addr, hash_size) \
91 ((((((natural_t)(addr)) >> 17) & \
92 ((2 * (hash_size)) -1)) + \
93 (87 * (((addr) & TWS_ADDR_OFF_MASK)/(2 * (hash_size))))) & \
94 ((2 * (hash_size)) -1))
96 #define do_startup_hash(addr, hash_size) \
97 (((((natural_t)(addr)) >> 17) * 3) & \
102 struct tws_startup_ele
{
103 unsigned int page_cache
;
104 vm_offset_t page_addr
;
107 typedef struct tws_startup_ele
*tws_startup_ele_t
;
110 struct tws_startup_ptr
{
111 tws_startup_ele_t element
;
112 struct tws_startup_ptr
*next
;
115 typedef struct tws_startup_ptr
*tws_startup_ptr_t
;
118 unsigned int tws_hash_size
; /* total size of struct in bytes */
119 unsigned int ele_count
;
120 unsigned int array_size
; /* lines * rows * expansion_count */
121 unsigned int hash_count
;
123 tws_startup_ptr_t
*table
; /* hash table */
124 struct tws_startup_ptr
*ele
; /* hash elements */
125 struct tws_startup_ele
*array
;
128 typedef struct tws_startup
*tws_startup_t
;
131 /* Dynamic cache data structures for working set */
133 struct tws_hash_ele
{
135 vm_object_offset_t offset
;
136 unsigned int page_cache
;
137 vm_offset_t page_addr
;
141 typedef struct tws_hash_ele
*tws_hash_ele_t
;
143 #define TWS_HASH_OFF_MASK ((vm_object_offset_t)0xFFFFFFFFFFFE0000ULL)
144 #define TWS_ADDR_OFF_MASK ((vm_offset_t)0xFFFE0000)
145 #define TWS_INDEX_MASK ((vm_object_offset_t)0x000000000001F000ULL)
147 struct tws_hash_ptr
{
148 tws_hash_ele_t element
;
149 struct tws_hash_ptr
*next
;
151 typedef struct tws_hash_ptr
*tws_hash_ptr_t
;
153 struct tws_hash_line
{
154 unsigned int ele_count
;
155 struct tws_hash_ele list
[TWS_ARRAY_SIZE
];
157 typedef struct tws_hash_line
*tws_hash_line_t
;
159 #define TWS_HASH_STYLE_DEFAULT 0x0
160 #define TWS_HASH_STYLE_BASIC 0x1
161 #define TWS_HASH_STYLE_SIGNAL 0x2
164 #define TWS_ADDR_HASH 1
165 #define TWS_HASH_EXPANSION_MAX 10
166 #define TWS_MAX_REHASH 3
170 decl_mutex_data(,lock
) /* tws_hash's lock */
173 unsigned int current_line
;
174 unsigned int pageout_count
;
175 unsigned int line_count
;
177 unsigned int number_of_lines
;
178 unsigned int number_of_elements
;
179 unsigned int expansion_count
;
180 unsigned int time_of_creation
;
182 unsigned int lookup_count
;
183 unsigned int insert_count
;
185 tws_startup_t startup_cache
;
187 int startup_name_length
;
192 unsigned int obj_free_count
[TWS_HASH_EXPANSION_MAX
];
193 unsigned int addr_free_count
[TWS_HASH_EXPANSION_MAX
];
194 tws_hash_ptr_t free_hash_ele
[TWS_HASH_EXPANSION_MAX
];
195 tws_hash_ptr_t
*table
[TWS_HASH_EXPANSION_MAX
];
196 tws_hash_ptr_t table_ele
[TWS_HASH_EXPANSION_MAX
];
197 tws_hash_ptr_t alt_ele
[TWS_HASH_EXPANSION_MAX
];
198 struct tws_hash_line
*cache
[TWS_HASH_EXPANSION_MAX
];
201 typedef struct tws_hash
*tws_hash_t
;
204 extern kern_return_t
tws_lookup(
206 vm_object_offset_t offset
,
208 tws_hash_line_t
*line
);
210 extern kern_return_t
tws_insert(
212 vm_object_offset_t offset
,
214 vm_offset_t page_addr
,
217 extern void tws_build_cluster(
220 vm_object_offset_t
*start
,
221 vm_object_offset_t
*end
,
222 vm_size_t max_length
);
224 extern void tws_line_signal(
227 tws_hash_line_t hash_line
,
228 vm_offset_t target_page
);
230 extern void tws_hash_destroy(
233 extern void tws_hash_ws_flush(
236 extern kern_return_t
tws_expand_working_set(
238 unsigned int line_count
,
239 boolean_t dump_data
);
241 extern kern_return_t
task_working_set_create(
247 #endif /* MACH_KERNEL_PRIVATE */
249 extern kern_return_t
tws_handle_startup_file(
254 boolean_t
*new_info
);
256 extern kern_return_t
tws_send_startup_info(
259 #endif /* KERNEL_PRIVATE */
261 #endif /* _VM_TASK_WORKING_SET_H_ */