2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * File: vm/task_working_set.h
30 * Author: Chris Youngworth
33 * Working set detection and maintainence module
37 #ifndef _VM_TASK_WORKING_SET_H_
38 #define _VM_TASK_WORKING_SET_H_
40 #include <kern/queue.h>
41 #include <vm/vm_object.h>
43 /* task working set */
45 #define tws_lock(tws) mutex_lock(&(tws)->lock)
46 #define tws_lock_try(tws) mutex_try(&(tws)->lock)
47 #define tws_unlock(tws) mutex_unlock(&(tws)->lock)
50 #define TWS_ARRAY_SIZE 8
51 #define TWS_HASH_LINE_COUNT 32
52 /* start out size to allow monitoring of working set without excessive use */
53 /* of wired memory resource. */
54 #define TWS_SMALL_HASH_LINE_COUNT 4
57 * do not think of changing this hash unless you understand the implications
58 * for the hash element page_cache field
60 #define do_tws_hash(object,offset, rows, lines) \
61 ((((((natural_t)(object)) + \
62 (((natural_t)(object)) >> 6) + \
63 (((natural_t)(object)) >> 12) + \
64 (((natural_t)(object)) >> 18) + \
65 (((natural_t)(object)) >> 24)) << 5) + \
66 ((natural_t)(((vm_object_offset_t)(offset)) >> 17))) & \
70 #define alt_tws_hash(addr, rows, lines) \
71 ((((natural_t)(addr)) >> 17) & \
75 /* Long term startup data structures for initial cache filling */
77 #define TWS_STARTUP_MAX_HASH_RETRY 3
79 /* 87 is the wrap skew, its based on RETRY times the RETRY offset of 29 */
81 #define do_startup_hash(addr, hash_size) \
82 ((((((natural_t)(addr)) >> 17) & \
83 ((2 * (hash_size)) -1)) + \
84 (87 * (((addr) & TWS_ADDR_OFF_MASK)/(2 * (hash_size))))) & \
85 ((2 * (hash_size)) -1))
87 #define do_startup_hash(addr, hash_size) \
88 (((((natural_t)(addr)) >> 17) * 3) & \
93 struct tws_startup_ele
{
94 unsigned int page_cache
;
95 vm_offset_t page_addr
;
98 typedef struct tws_startup_ele
*tws_startup_ele_t
;
101 struct tws_startup_ptr
{
102 tws_startup_ele_t element
;
103 struct tws_startup_ptr
*next
;
106 typedef struct tws_startup_ptr
*tws_startup_ptr_t
;
109 unsigned int tws_hash_size
; /* total size of struct in bytes */
110 unsigned int ele_count
;
111 unsigned int array_size
; /* lines * rows * expansion_count */
112 unsigned int hash_count
;
114 tws_startup_ptr_t
*table
; /* hash table */
115 struct tws_startup_ptr
*ele
; /* hash elements */
116 struct tws_startup_ele
*array
;
119 typedef struct tws_startup
*tws_startup_t
;
122 /* Dynamic cache data structures for working set */
124 struct tws_hash_ele
{
126 vm_object_offset_t offset
;
127 unsigned int page_cache
;
128 vm_offset_t page_addr
;
132 typedef struct tws_hash_ele
*tws_hash_ele_t
;
134 #define TWS_HASH_OFF_MASK ((vm_object_offset_t)0xFFFFFFFFFFFE0000)
135 #define TWS_ADDR_OFF_MASK ((vm_offset_t)0xFFFE0000)
136 #define TWS_INDEX_MASK ((vm_object_offset_t)0x000000000001F000)
138 struct tws_hash_ptr
{
139 tws_hash_ele_t element
;
140 struct tws_hash_ptr
*next
;
142 typedef struct tws_hash_ptr
*tws_hash_ptr_t
;
144 struct tws_hash_line
{
146 struct tws_hash_ele list
[TWS_ARRAY_SIZE
];
148 typedef struct tws_hash_line
*tws_hash_line_t
;
150 #define TWS_HASH_STYLE_DEFAULT 0x0
151 #define TWS_HASH_STYLE_BASIC 0x1
152 #define TWS_HASH_STYLE_SIGNAL 0x2
155 #define TWS_ADDR_HASH 1
156 #define TWS_HASH_EXPANSION_MAX 5
157 #define TWS_MAX_REHASH 3
161 decl_mutex_data(,lock
) /* tws_hash's lock */
165 unsigned int pageout_count
;
169 int number_of_elements
;
171 unsigned int time_of_creation
;
176 tws_startup_t startup_cache
;
178 int startup_name_length
;
183 unsigned int obj_free_count
[TWS_HASH_EXPANSION_MAX
];
184 unsigned int addr_free_count
[TWS_HASH_EXPANSION_MAX
];
185 tws_hash_ptr_t free_hash_ele
[TWS_HASH_EXPANSION_MAX
];
186 tws_hash_ptr_t
*table
[TWS_HASH_EXPANSION_MAX
];
187 tws_hash_ptr_t table_ele
[TWS_HASH_EXPANSION_MAX
];
188 tws_hash_ptr_t alt_ele
[TWS_HASH_EXPANSION_MAX
];
189 struct tws_hash_line
*cache
[TWS_HASH_EXPANSION_MAX
];
192 typedef struct tws_hash
*tws_hash_t
;
195 extern tws_hash_t
tws_hash_create();
197 extern void tws_hash_line_clear(
199 tws_hash_line_t hash_line
,
202 extern kern_return_t
tws_lookup(
204 vm_object_offset_t offset
,
206 tws_hash_line_t
*line
);
208 extern kern_return_t
tws_insert(
210 vm_object_offset_t offset
,
212 vm_offset_t page_addr
,
215 extern void tws_build_cluster(
218 vm_object_offset_t
*start
,
219 vm_object_offset_t
*end
,
220 vm_size_t max_length
);
222 extern tws_line_signal(
225 tws_hash_line_t hash_line
,
226 vm_offset_t target_page
);
228 extern void tws_hash_destroy(
231 extern void tws_hash_clear(
234 kern_return_t
task_working_set_create(
240 kern_return_t
tws_expand_working_set(
243 boolean_t dump_data
);
245 kern_return_t
tws_handle_startup_file(
250 boolean_t
*new_info
);
252 kern_return_t
tws_write_startup_file(
257 unsigned int string_length
);
259 kern_return_t
tws_read_startup_file(
261 tws_startup_t startup
,
262 vm_offset_t cache_size
);
270 #endif /* _VM_TASK_WORKING_SET_H_ */