Whamcloud - gitweb
7eb6ea4434c87aed261c88129ab578993b408d2d
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-mem.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40
41
42 cfs_mem_cache_t *cfs_page_t_slab = NULL;
43 cfs_mem_cache_t *cfs_page_p_slab = NULL;
44
45 cfs_page_t * virt_to_page(void * addr)
46 {
47     cfs_page_t *pg;
48     pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
49     
50     if (NULL == pg) {
51         cfs_enter_debugger();
52         return NULL;
53     }
54
55     memset(pg, 0, sizeof(cfs_page_t));
56     pg->addr = (void *)((__u64)addr & (~((__u64)PAGE_SIZE-1)));
57     pg->mapping = addr;
58     cfs_atomic_set(&pg->count, 1);
59     cfs_set_bit(PG_virt, &(pg->flags));
60     cfs_enter_debugger();
61     return pg;
62 }
63
64 /*
65  * cfs_alloc_page
66  *   To allocate the cfs_page_t and also 1 page of memory
67  *
68  * Arguments:
69  *   flags:  the allocation options
70  *
71  * Return Value:
72  *   pointer to the cfs_page_t strcture in success or
73  *   NULL in failure case
74  *
75  * Notes: 
76  *   N/A
77  */
78
79 cfs_atomic_t libcfs_total_pages;
80
81 cfs_page_t * cfs_alloc_page(int flags)
82 {
83     cfs_page_t *pg;
84     pg = cfs_mem_cache_alloc(cfs_page_t_slab, 0);
85     
86     if (NULL == pg) {
87         cfs_enter_debugger();
88         return NULL;
89     }
90
91     memset(pg, 0, sizeof(cfs_page_t));
92     pg->addr = cfs_mem_cache_alloc(cfs_page_p_slab, 0);
93     cfs_atomic_set(&pg->count, 1);
94
95     if (pg->addr) {
96         if (cfs_is_flag_set(flags, CFS_ALLOC_ZERO)) {
97             memset(pg->addr, 0, CFS_PAGE_SIZE);
98         }
99         cfs_atomic_inc(&libcfs_total_pages);
100     } else {
101         cfs_enter_debugger();
102         cfs_mem_cache_free(cfs_page_t_slab, pg);
103         pg = NULL;
104     }
105
106     return pg;
107 }
108
109 /*
110  * cfs_free_page
111  *   To free the cfs_page_t including the page
112  *
113  * Arguments:
114  *   pg:  pointer to the cfs_page_t strcture
115  *
116  * Return Value:
117  *   N/A
118  *
119  * Notes: 
120  *   N/A
121  */
122 void cfs_free_page(cfs_page_t *pg)
123 {
124     ASSERT(pg != NULL);
125     ASSERT(pg->addr  != NULL);
126     ASSERT(cfs_atomic_read(&pg->count) <= 1);
127
128     if (!cfs_test_bit(PG_virt, &pg->flags)) {
129         cfs_mem_cache_free(cfs_page_p_slab, pg->addr);
130         cfs_atomic_dec(&libcfs_total_pages);
131     } else {
132         cfs_enter_debugger();
133     }
134     cfs_mem_cache_free(cfs_page_t_slab, pg);
135 }
136
137 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem)
138 {
139     KdPrint(("cfs_mem_is_in_cache: not implemented. (should maintain a"
140               "chain to keep all allocations traced.)\n"));
141     return 1;
142 }
143
144 /*
145  * cfs_alloc
146  *   To allocate memory from system pool
147  *
148  * Arguments:
149  *   nr_bytes:  length in bytes of the requested buffer
150  *   flags:     flags indiction
151  *
152  * Return Value:
153  *   NULL: if there's no enough memory space in system
154  *   the address of the allocated memory in success.
155  *
156  * Notes: 
157  *   This operation can be treated as atomic.
158  */
159
160 void *
161 cfs_alloc(size_t nr_bytes, u_int32_t flags)
162 {
163     void *ptr;
164
165     /* Ignore the flags: always allcoate from NonPagedPool */
166     ptr = ExAllocatePoolWithTag(NonPagedPool, nr_bytes, 'Lufs');
167     if (ptr != NULL && (flags & CFS_ALLOC_ZERO)) {
168         memset(ptr, 0, nr_bytes);
169     }
170
171     if (!ptr) {
172         cfs_enter_debugger();
173     }
174
175     return ptr;
176 }
177
178 /*
179  * cfs_free
180  *   To free the sepcified memory to system pool
181  *
182  * Arguments:
183  *   addr:   pointer to the buffer to be freed
184  *
185  * Return Value:
186  *   N/A
187  *
188  * Notes: 
189  *    This operation can be treated as atomic.
190  */
191
192 void
193 cfs_free(void *addr)
194 {
195     ExFreePool(addr);
196 }
197
198 /*
199  * cfs_alloc_large
200  *   To allocate large block of memory from system pool
201  *
202  * Arguments:
203  *   nr_bytes:  length in bytes of the requested buffer
204  *
205  * Return Value:
206  *   NULL: if there's no enough memory space in system
207  *   the address of the allocated memory in success.
208  *
209  * Notes: 
210  *   N/A
211  */
212
213 void *
214 cfs_alloc_large(size_t nr_bytes)
215 {
216     return cfs_alloc(nr_bytes, 0);
217 }
218
219 /*
220  * cfs_free_large
221  *   To free the sepcified memory to system pool
222  *
223  * Arguments:
224  *   addr:   pointer to the buffer to be freed
225  *
226  * Return Value:
227  *   N/A
228  *
229  * Notes: 
230  *   N/A
231  */
232
233 void
234 cfs_free_large(void *addr)
235 {
236     cfs_free(addr);
237 }
238
239
240 /*
241  * cfs_mem_cache_create
242  *   To create a SLAB cache
243  *
244  * Arguments:
245  *   name:   name string of the SLAB cache to be created
246  *   size:   size in bytes of SLAB entry buffer
247  *   offset: offset in the page
248  *   flags:  SLAB creation flags
249 *
250  * Return Value:
251  *   The poitner of cfs_memory_cache structure in success.
252  *   NULL pointer in failure case.
253  *
254  * Notes: 
255  *   1, offset won't be used here.
256  *   2, it could be better to induce a lock to protect the access of the
257  *       SLAB structure on SMP if there's not outside lock protection.
258  *   3, parameters C/D are removed.
259  */
260
261 cfs_mem_cache_t *
262 cfs_mem_cache_create(
263     const char * name,
264     size_t size,
265     size_t offset,
266     unsigned long flags
267     )
268 {
269     cfs_mem_cache_t * kmc = NULL;
270
271     /*  The name of the SLAB could not exceed 20 chars */
272
273     if (name && strlen(name) >= 20) {
274         goto errorout;
275     }
276
277     /* Allocate and initialize the SLAB strcture */
278
279     kmc = cfs_alloc (sizeof(cfs_mem_cache_t), 0);
280
281     if (NULL == kmc) {
282         goto errorout;
283     }
284
285     memset(kmc, 0, sizeof(cfs_mem_cache_t));
286     kmc->flags = flags;
287
288     if (name) {
289         strcpy(&kmc->name[0], name);
290     }
291
292     /* Initialize the corresponding LookAside list */
293
294     ExInitializeNPagedLookasideList(
295             &(kmc->npll),
296             NULL,
297             NULL,
298             0,
299             size,
300             'pnmk',
301             0);
302  
303 errorout:
304
305     return kmc;
306 }
307
308 /*
309  * cfs_mem_cache_destroy
310  *   To destroy the unused SLAB cache
311  *
312  * Arguments:
313  *   kmc: the SLAB cache to be destroied.
314  *
315  * Return Value:
316  *   0: in success case.
317  *   1: in failure case.
318  *
319  * Notes: 
320  *   N/A
321  */
322
323 int cfs_mem_cache_destroy (cfs_mem_cache_t * kmc)
324 {
325     ASSERT(kmc != NULL);
326
327     ExDeleteNPagedLookasideList(&(kmc->npll));
328
329     cfs_free(kmc);
330
331     return 0;
332 }
333
334 /*
335  * cfs_mem_cache_alloc
336  *   To allocate an object (LookAside entry) from the SLAB
337  *
338  * Arguments:
339  *   kmc:   the SLAB cache to be allocated from.
340  *   flags: flags for allocation options
341  *
342  * Return Value:
343  *   object buffer address: in success case.
344  *   NULL: in failure case.
345  *
346  * Notes: 
347  *   N/A
348  */
349
350 void *cfs_mem_cache_alloc(cfs_mem_cache_t * kmc, int flags)
351 {
352     void *buf = NULL;
353
354     buf = ExAllocateFromNPagedLookasideList(&(kmc->npll));
355
356     return buf;
357 }
358
359 /*
360  * cfs_mem_cache_free
361  *   To free an object (LookAside entry) to the SLAB cache
362  *
363  * Arguments:
364  *   kmc: the SLAB cache to be freed to.
365  *   buf: the pointer to the object to be freed.
366  *
367  * Return Value:
368  *   N/A
369  *
370  * Notes: 
371  *   N/A
372  */
373
374 void cfs_mem_cache_free(cfs_mem_cache_t * kmc, void * buf)
375 {
376     ExFreeToNPagedLookasideList(&(kmc->npll), buf);
377 }
378
379 cfs_spinlock_t  shrinker_guard = {0};
380 CFS_LIST_HEAD(shrinker_hdr);
381 cfs_timer_t shrinker_timer = {0};
382
383 struct cfs_shrinker * cfs_set_shrinker(int seeks, shrink_callback cb)
384 {
385     struct cfs_shrinker * s = (struct cfs_shrinker *)
386         cfs_alloc(sizeof(struct cfs_shrinker), CFS_ALLOC_ZERO);
387     if (s) {
388         s->cb = cb;
389         s->seeks = seeks;
390         s->nr = 2;
391         cfs_spin_lock(&shrinker_guard);
392         cfs_list_add(&s->list, &shrinker_hdr); 
393         cfs_spin_unlock(&shrinker_guard);
394     }
395
396     return s;
397 }
398
399 void cfs_remove_shrinker(struct cfs_shrinker *s)
400 {
401     struct cfs_shrinker *tmp;
402     cfs_spin_lock(&shrinker_guard);
403 #if TRUE
404     cfs_list_for_each_entry_typed(tmp, &shrinker_hdr,
405                                   struct cfs_shrinker, list) {
406         if (tmp == s) {
407             cfs_list_del(&tmp->list);
408             break;
409         } 
410     }
411 #else
412     cfs_list_del(&s->list);
413 #endif
414     cfs_spin_unlock(&shrinker_guard);
415     cfs_free(s);
416 }
417
418 /* time ut test proc */
419 void shrinker_timer_proc(ulong_ptr_t arg)
420 {
421     struct cfs_shrinker *s;
422     cfs_spin_lock(&shrinker_guard);
423
424     cfs_list_for_each_entry_typed(s, &shrinker_hdr,
425                                   struct cfs_shrinker, list) {
426             s->cb(s->nr, __GFP_FS);
427     }
428     cfs_spin_unlock(&shrinker_guard);
429     cfs_timer_arm(&shrinker_timer, 300);
430 }
431
432 int start_shrinker_timer()
433 {
434     /* initialize shriner timer */
435     cfs_timer_init(&shrinker_timer, shrinker_timer_proc, NULL);
436
437     /* start the timer to trigger in 5 minutes */
438     cfs_timer_arm(&shrinker_timer, 300);
439
440     return 0;
441 }
442
443 void stop_shrinker_timer()
444 {
445     /* cancel the timer */
446     cfs_timer_disarm(&shrinker_timer);
447     cfs_timer_done(&shrinker_timer);
448 }