2 * Copyright (c) 2000 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 <kern/queue.h>
38 #include <vm/vm_object.h>
40 /* task working set */
42 #define tws_lock(tws) mutex_lock(&(tws)->lock)
43 #define tws_lock_try(tws) mutex_try(&(tws)->lock)
44 #define tws_unlock(tws) mutex_unlock(&(tws)->lock)
47 #define TWS_ARRAY_SIZE 8
48 #define TWS_HASH_LINE_COUNT 32
49 /* start out size to allow monitoring of working set without excessive use */
50 /* of wired memory resource. */
51 #define TWS_SMALL_HASH_LINE_COUNT 4
54 * do not think of changing this hash unless you understand the implications
55 * for the hash element page_cache field
57 #define do_tws_hash(object,offset, rows, lines) \
58 ((((((natural_t)(object)) + \
59 (((natural_t)(object)) >> 6) + \
60 (((natural_t)(object)) >> 12) + \
61 (((natural_t)(object)) >> 18) + \
62 (((natural_t)(object)) >> 24)) << 5) + \
63 ((natural_t)(((vm_object_offset_t)(offset)) >> 17))) & \
67 #define alt_tws_hash(addr, rows, lines) \
68 ((((natural_t)(addr)) >> 17) & \
72 /* Long term startup data structures for initial cache filling */
74 #define TWS_STARTUP_MAX_HASH_RETRY 3
76 /* 87 is the wrap skew, its based on RETRY times the RETRY offset of 29 */
78 #define do_startup_hash(addr, hash_size) \
79 ((((((natural_t)(addr)) >> 17) & \
80 ((2 * (hash_size)) -1)) + \
81 (87 * (((addr) & TWS_ADDR_OFF_MASK)/(2 * (hash_size))))) & \
82 ((2 * (hash_size)) -1))
84 #define do_startup_hash(addr, hash_size) \
85 (((((natural_t)(addr)) >> 17) * 3) & \
90 struct tws_startup_ele
{
91 unsigned int page_cache
;
92 vm_offset_t page_addr
;
95 typedef struct tws_startup_ele
*tws_startup_ele_t
;
98 struct tws_startup_ptr
{
99 tws_startup_ele_t element
;
100 struct tws_startup_ptr
*next
;
103 typedef struct tws_startup_ptr
*tws_startup_ptr_t
;
106 unsigned int tws_hash_size
; /* total size of struct in bytes */
107 unsigned int ele_count
;
108 unsigned int array_size
; /* lines * rows * expansion_count */
109 unsigned int hash_count
;
111 tws_startup_ptr_t
*table
; /* hash table */
112 struct tws_startup_ptr
*ele
; /* hash elements */
113 struct tws_startup_ele
*array
;
116 typedef struct tws_startup
*tws_startup_t
;
119 /* Dynamic cache data structures for working set */
121 struct tws_hash_ele
{
123 vm_object_offset_t offset
;
124 unsigned int page_cache
;
125 vm_offset_t page_addr
;
129 typedef struct tws_hash_ele
*tws_hash_ele_t
;
131 #define TWS_HASH_OFF_MASK ((vm_object_offset_t)0xFFFFFFFFFFFE0000)
132 #define TWS_ADDR_OFF_MASK ((vm_offset_t)0xFFFE0000)
133 #define TWS_INDEX_MASK ((vm_object_offset_t)0x000000000001F000)
135 struct tws_hash_ptr
{
136 tws_hash_ele_t element
;
137 struct tws_hash_ptr
*next
;
139 typedef struct tws_hash_ptr
*tws_hash_ptr_t
;
141 struct tws_hash_line
{
143 struct tws_hash_ele list
[TWS_ARRAY_SIZE
];
145 typedef struct tws_hash_line
*tws_hash_line_t
;
147 #define TWS_HASH_STYLE_DEFAULT 0x0
148 #define TWS_HASH_STYLE_BASIC 0x1
149 #define TWS_HASH_STYLE_SIGNAL 0x2
152 #define TWS_ADDR_HASH 1
153 #define TWS_HASH_EXPANSION_MAX 5
154 #define TWS_MAX_REHASH 3
158 decl_mutex_data(,lock
) /* tws_hash's lock */
162 unsigned int pageout_count
;
166 int number_of_elements
;
168 unsigned int time_of_creation
;
173 tws_startup_t startup_cache
;
175 int startup_name_length
;
180 unsigned int obj_free_count
[TWS_HASH_EXPANSION_MAX
];
181 unsigned int addr_free_count
[TWS_HASH_EXPANSION_MAX
];
182 tws_hash_ptr_t free_hash_ele
[TWS_HASH_EXPANSION_MAX
];
183 tws_hash_ptr_t
*table
[TWS_HASH_EXPANSION_MAX
];
184 tws_hash_ptr_t table_ele
[TWS_HASH_EXPANSION_MAX
];
185 tws_hash_ptr_t alt_ele
[TWS_HASH_EXPANSION_MAX
];
186 struct tws_hash_line
*cache
[TWS_HASH_EXPANSION_MAX
];
189 typedef struct tws_hash
*tws_hash_t
;
192 extern tws_hash_t
tws_hash_create();
194 extern void tws_hash_line_clear(
196 tws_hash_line_t hash_line
,
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 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_clear(
231 kern_return_t
task_working_set_create(
237 kern_return_t
tws_expand_working_set(
240 boolean_t dump_data
);
242 kern_return_t
tws_handle_startup_file(
247 boolean_t
*new_info
);
249 kern_return_t
tws_write_startup_file(
254 unsigned int string_length
);
256 kern_return_t
tws_read_startup_file(
258 tws_startup_t startup
,
259 vm_offset_t cache_size
);
263 #endif /* _VM_TASK_WORKING_SET_H_ */