2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * File: vm/task_working_set.h
27 * Author: Chris Youngworth
30 * Working set detection and maintainence module
34 #ifndef _VM_TASK_WORKING_SET_H_
35 #define _VM_TASK_WORKING_SET_H_
37 #include <mach/mach_types.h>
41 #ifdef MACH_KERNEL_PRIVATE
43 #include <kern/queue.h>
44 #include <vm/vm_object.h>
46 /* task working set */
48 #define tws_lock(tws) mutex_lock(&(tws)->lock)
49 #define tws_lock_try(tws) mutex_try(&(tws)->lock)
50 #define tws_unlock(tws) mutex_unlock(&(tws)->lock)
53 #define TWS_ARRAY_SIZE 8
54 #define TWS_HASH_LINE_COUNT 32
55 /* start out size to allow monitoring of working set without excessive use */
56 /* of wired memory resource. */
57 #define TWS_SMALL_HASH_LINE_COUNT 4
60 * do not think of changing this hash unless you understand the implications
61 * for the hash element page_cache field
63 #define do_tws_hash(object,offset, rows, lines) \
64 ((((((natural_t)(object)) + \
65 (((natural_t)(object)) >> 6) + \
66 (((natural_t)(object)) >> 12) + \
67 (((natural_t)(object)) >> 18) + \
68 (((natural_t)(object)) >> 24)) << 5) + \
69 ((natural_t)(((vm_object_offset_t)(offset)) >> 17))) & \
73 #define alt_tws_hash(addr, rows, lines) \
74 ((((natural_t)(addr)) >> 17) & \
78 /* Long term startup data structures for initial cache filling */
80 #define TWS_STARTUP_MAX_HASH_RETRY 3
82 /* 87 is the wrap skew, its based on RETRY times the RETRY offset of 29 */
84 #define do_startup_hash(addr, hash_size) \
85 ((((((natural_t)(addr)) >> 17) & \
86 ((2 * (hash_size)) -1)) + \
87 (87 * (((addr) & TWS_ADDR_OFF_MASK)/(2 * (hash_size))))) & \
88 ((2 * (hash_size)) -1))
90 #define do_startup_hash(addr, hash_size) \
91 (((((natural_t)(addr)) >> 17) * 3) & \
96 struct tws_startup_ele
{
97 unsigned int page_cache
;
98 vm_offset_t page_addr
;
101 typedef struct tws_startup_ele
*tws_startup_ele_t
;
104 struct tws_startup_ptr
{
105 tws_startup_ele_t element
;
106 struct tws_startup_ptr
*next
;
109 typedef struct tws_startup_ptr
*tws_startup_ptr_t
;
112 unsigned int tws_hash_size
; /* total size of struct in bytes */
113 unsigned int ele_count
;
114 unsigned int array_size
; /* lines * rows * expansion_count */
115 unsigned int hash_count
;
117 tws_startup_ptr_t
*table
; /* hash table */
118 struct tws_startup_ptr
*ele
; /* hash elements */
119 struct tws_startup_ele
*array
;
122 typedef struct tws_startup
*tws_startup_t
;
125 /* Dynamic cache data structures for working set */
127 struct tws_hash_ele
{
129 vm_object_offset_t offset
;
130 unsigned int page_cache
;
131 vm_offset_t page_addr
;
135 typedef struct tws_hash_ele
*tws_hash_ele_t
;
137 #define TWS_HASH_OFF_MASK ((vm_object_offset_t)0xFFFFFFFFFFFE0000ULL)
138 #define TWS_ADDR_OFF_MASK ((vm_offset_t)0xFFFE0000)
139 #define TWS_INDEX_MASK ((vm_object_offset_t)0x000000000001F000ULL)
141 struct tws_hash_ptr
{
142 tws_hash_ele_t element
;
143 struct tws_hash_ptr
*next
;
145 typedef struct tws_hash_ptr
*tws_hash_ptr_t
;
147 struct tws_hash_line
{
148 unsigned int ele_count
;
149 struct tws_hash_ele list
[TWS_ARRAY_SIZE
];
151 typedef struct tws_hash_line
*tws_hash_line_t
;
153 #define TWS_HASH_STYLE_DEFAULT 0x0
154 #define TWS_HASH_STYLE_BASIC 0x1
155 #define TWS_HASH_STYLE_SIGNAL 0x2
158 #define TWS_ADDR_HASH 1
159 #define TWS_HASH_EXPANSION_MAX 10
160 #define TWS_MAX_REHASH 3
164 decl_mutex_data(,lock
) /* tws_hash's lock */
167 unsigned int current_line
;
168 unsigned int pageout_count
;
169 unsigned int line_count
;
171 unsigned int number_of_lines
;
172 unsigned int number_of_elements
;
173 unsigned int expansion_count
;
174 unsigned int time_of_creation
;
176 unsigned int lookup_count
;
177 unsigned int insert_count
;
179 tws_startup_t startup_cache
;
181 int startup_name_length
;
186 unsigned int obj_free_count
[TWS_HASH_EXPANSION_MAX
];
187 unsigned int addr_free_count
[TWS_HASH_EXPANSION_MAX
];
188 tws_hash_ptr_t free_hash_ele
[TWS_HASH_EXPANSION_MAX
];
189 tws_hash_ptr_t
*table
[TWS_HASH_EXPANSION_MAX
];
190 tws_hash_ptr_t table_ele
[TWS_HASH_EXPANSION_MAX
];
191 tws_hash_ptr_t alt_ele
[TWS_HASH_EXPANSION_MAX
];
192 struct tws_hash_line
*cache
[TWS_HASH_EXPANSION_MAX
];
195 typedef struct tws_hash
*tws_hash_t
;
198 extern kern_return_t
tws_lookup(
200 vm_object_offset_t offset
,
202 tws_hash_line_t
*line
);
204 extern kern_return_t
tws_insert(
206 vm_object_offset_t offset
,
208 vm_offset_t page_addr
,
211 extern void tws_build_cluster(
214 vm_object_offset_t
*start
,
215 vm_object_offset_t
*end
,
216 vm_size_t max_length
);
218 extern void tws_line_signal(
221 tws_hash_line_t hash_line
,
222 vm_offset_t target_page
);
224 extern void tws_hash_destroy(
227 extern void tws_hash_ws_flush(
230 extern kern_return_t
tws_expand_working_set(
232 unsigned int line_count
,
233 boolean_t dump_data
);
235 extern kern_return_t
task_working_set_create(
241 #endif /* MACH_KERNEL_PRIVATE */
243 extern kern_return_t
tws_handle_startup_file(
248 boolean_t
*new_info
);
250 extern kern_return_t
tws_send_startup_info(
253 #endif /* KERNEL_PRIVATE */
255 #endif /* _VM_TASK_WORKING_SET_H_ */