Subversion Repositories HelenOS

Rev

Rev 3425 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3425 Rev 4377
Line 34... Line 34...
34
#include <mm/frame.h>
34
#include <mm/frame.h>
35
#include <memstr.h>
35
#include <memstr.h>
36
#include <synch/condvar.h>
36
#include <synch/condvar.h>
37
#include <synch/mutex.h>
37
#include <synch/mutex.h>
38
 
38
 
39
#define ITEM_SIZE 256
39
#define ITEM_SIZE  256
40
 
40
 
41
/** Fill memory with 2 caches, when allocation fails,
41
/** Fill memory with 2 caches, when allocation fails,
42
 *  free one of the caches. We should have everything in magazines,
42
 *  free one of the caches. We should have everything in magazines,
43
 *  now allocation should clean magazines and allow for full allocation.
43
 *  now allocation should clean magazines and allow for full allocation.
44
 */
44
 */
45
static void totalmemtest(bool quiet)
45
static void totalmemtest(void)
46
{
46
{
47
    slab_cache_t *cache1;
47
    slab_cache_t *cache1;
48
    slab_cache_t *cache2;
48
    slab_cache_t *cache2;
49
    int i;
49
    int i;
50
 
50
   
51
    void *data1, *data2;
51
    void *data1, *data2;
52
    void *olddata1 = NULL, *olddata2 = NULL;
52
    void *olddata1 = NULL, *olddata2 = NULL;
53
   
53
   
54
    cache1 = slab_cache_create("cache1_tst", ITEM_SIZE, 0, NULL, NULL, 0);
54
    cache1 = slab_cache_create("cache1_tst", ITEM_SIZE, 0, NULL, NULL, 0);
55
    cache2 = slab_cache_create("cache2_tst", ITEM_SIZE, 0, NULL, NULL, 0);
55
    cache2 = slab_cache_create("cache2_tst", ITEM_SIZE, 0, NULL, NULL, 0);
56
   
56
   
57
    if (!quiet)
-
 
58
        printf("Allocating...");
57
    TPRINTF("Allocating...");
59
   
58
   
60
    /* Use atomic alloc, so that we find end of memory */
59
    /* Use atomic alloc, so that we find end of memory */
61
    do {
60
    do {
62
        data1 = slab_alloc(cache1, FRAME_ATOMIC);
61
        data1 = slab_alloc(cache1, FRAME_ATOMIC);
63
        data2 = slab_alloc(cache2, FRAME_ATOMIC);
62
        data2 = slab_alloc(cache2, FRAME_ATOMIC);
Line 72... Line 71...
72
        memsetb(data2, ITEM_SIZE, 0);
71
        memsetb(data2, ITEM_SIZE, 0);
73
        *((void **) data1) = olddata1;
72
        *((void **) data1) = olddata1;
74
        *((void **) data2) = olddata2;
73
        *((void **) data2) = olddata2;
75
        olddata1 = data1;
74
        olddata1 = data1;
76
        olddata2 = data2;
75
        olddata2 = data2;
77
    } while (1);
76
    } while (true);
78
   
77
   
79
    if (!quiet) {
-
 
80
        printf("done.\n");
78
    TPRINTF("done.\n");
-
 
79
   
81
        printf("Deallocating cache2...");
80
    TPRINTF("Deallocating cache2...");
82
    }
-
 
83
   
81
   
84
    /* We do not have memory - now deallocate cache2 */
82
    /* We do not have memory - now deallocate cache2 */
85
    while (olddata2) {
83
    while (olddata2) {
86
        data2 = *((void **) olddata2);
84
        data2 = *((void **) olddata2);
87
        slab_free(cache2, olddata2);
85
        slab_free(cache2, olddata2);
88
        olddata2 = data2;
86
        olddata2 = data2;
89
    }
87
    }
90
   
88
   
91
    if (!quiet) {
-
 
92
        printf("done.\n");
89
    TPRINTF("done.\n");
-
 
90
   
93
        printf("Allocating to cache1...\n");
91
    TPRINTF("Allocating to cache1...\n");
94
    }
-
 
95
   
92
   
96
    for (i = 0; i < 30; i++) {
93
    for (i = 0; i < 30; i++) {
97
        data1 = slab_alloc(cache1, FRAME_ATOMIC);
94
        data1 = slab_alloc(cache1, FRAME_ATOMIC);
98
        if (!data1) {
95
        if (!data1) {
99
            if (!quiet)
-
 
100
                printf("Incorrect memory size - use another test.");
96
            TPRINTF("Incorrect memory size - use another test.");
101
            return;
97
            return;
102
        }
98
        }
103
        memsetb(data1, ITEM_SIZE, 0);
99
        memsetb(data1, ITEM_SIZE, 0);
104
        *((void **) data1) = olddata1;
100
        *((void **) data1) = olddata1;
105
        olddata1 = data1;
101
        olddata1 = data1;
106
    }
102
    }
107
    while (1) {
103
    while (true) {
108
        data1 = slab_alloc(cache1, FRAME_ATOMIC);
104
        data1 = slab_alloc(cache1, FRAME_ATOMIC);
109
        if (!data1)
105
        if (!data1)
110
            break;
106
            break;
111
        memsetb(data1, ITEM_SIZE, 0);
107
        memsetb(data1, ITEM_SIZE, 0);
112
        *((void **) data1) = olddata1;
108
        *((void **) data1) = olddata1;
113
        olddata1 = data1;
109
        olddata1 = data1;
114
    }
110
    }
115
   
111
   
116
    if (!quiet)
-
 
117
        printf("Deallocating cache1...");
112
    TPRINTF("Deallocating cache1...");
118
   
113
   
119
    while (olddata1) {
114
    while (olddata1) {
120
        data1 = *((void **) olddata1);
115
        data1 = *((void **) olddata1);
121
        slab_free(cache1, olddata1);
116
        slab_free(cache1, olddata1);
122
        olddata1 = data1;
117
        olddata1 = data1;
123
    }
118
    }
124
   
119
   
125
    if (!quiet) {
120
    TPRINTF("done.\n");
-
 
121
   
126
        printf("done.\n");
122
    if (!test_quiet)
127
        slab_print_list();
123
        slab_print_list();
128
    }
-
 
129
   
124
   
130
    slab_cache_destroy(cache1);
125
    slab_cache_destroy(cache1);
131
    slab_cache_destroy(cache2);
126
    slab_cache_destroy(cache2);
132
}
127
}
133
 
128
 
134
static slab_cache_t *thr_cache;
129
static slab_cache_t *thr_cache;
135
static semaphore_t thr_sem;
130
static semaphore_t thr_sem;
136
static condvar_t thread_starter;
131
static condvar_t thread_starter;
137
static mutex_t starter_mutex;
132
static mutex_t starter_mutex;
138
static bool sh_quiet;
-
 
139
 
133
 
140
#define THREADS 8
134
#define THREADS  8
141
 
135
 
142
static void slabtest(void *priv)
136
static void slabtest(void *priv)
143
{
137
{
144
    void *data = NULL, *new;
138
    void *data = NULL, *new;
145
   
139
   
Line 147... Line 141...
147
   
141
   
148
    mutex_lock(&starter_mutex);
142
    mutex_lock(&starter_mutex);
149
    condvar_wait(&thread_starter,&starter_mutex);
143
    condvar_wait(&thread_starter,&starter_mutex);
150
    mutex_unlock(&starter_mutex);
144
    mutex_unlock(&starter_mutex);
151
   
145
   
152
    if (!sh_quiet)
-
 
153
        printf("Starting thread #%" PRIu64 "...\n", THREAD->tid);
146
    TPRINTF("Starting thread #%" PRIu64 "...\n", THREAD->tid);
154
 
147
 
155
    /* Alloc all */
148
    /* Alloc all */
156
    if (!sh_quiet)
-
 
157
        printf("Thread #%" PRIu64 " allocating...\n", THREAD->tid);
149
    TPRINTF("Thread #%" PRIu64 " allocating...\n", THREAD->tid);
158
   
150
   
159
    while (1) {
151
    while (true) {
160
        /* Call with atomic to detect end of memory */
152
        /* Call with atomic to detect end of memory */
161
        new = slab_alloc(thr_cache, FRAME_ATOMIC);
153
        new = slab_alloc(thr_cache, FRAME_ATOMIC);
162
        if (!new)
154
        if (!new)
163
            break;
155
            break;
164
        *((void **) new) = data;
156
        *((void **) new) = data;
165
        data = new;
157
        data = new;
166
    }
158
    }
167
   
159
   
168
    if (!sh_quiet)
-
 
169
        printf("Thread #%" PRIu64 " releasing...\n", THREAD->tid);
160
    TPRINTF("Thread #%" PRIu64 " releasing...\n", THREAD->tid);
170
   
161
   
171
    while (data) {
162
    while (data) {
172
        new = *((void **)data);
163
        new = *((void **)data);
173
        *((void **) data) = NULL;
164
        *((void **) data) = NULL;
174
        slab_free(thr_cache, data);
165
        slab_free(thr_cache, data);
175
        data = new;
166
        data = new;
176
    }
167
    }
177
   
168
   
178
    if (!sh_quiet)
-
 
179
        printf("Thread #%" PRIu64 " allocating...\n", THREAD->tid);
169
    TPRINTF("Thread #%" PRIu64 " allocating...\n", THREAD->tid);
180
   
170
   
181
    while (1) {
171
    while (true) {
182
        /* Call with atomic to detect end of memory */
172
        /* Call with atomic to detect end of memory */
183
        new = slab_alloc(thr_cache, FRAME_ATOMIC);
173
        new = slab_alloc(thr_cache, FRAME_ATOMIC);
184
        if (!new)
174
        if (!new)
185
            break;
175
            break;
186
        *((void **) new) = data;
176
        *((void **) new) = data;
187
        data = new;
177
        data = new;
188
    }
178
    }
189
   
179
   
190
    if (!sh_quiet)
-
 
191
        printf("Thread #%" PRIu64 " releasing...\n", THREAD->tid);
180
    TPRINTF("Thread #%" PRIu64 " releasing...\n", THREAD->tid);
192
   
181
   
193
    while (data) {
182
    while (data) {
194
        new = *((void **)data);
183
        new = *((void **)data);
195
        *((void **) data) = NULL;
184
        *((void **) data) = NULL;
196
        slab_free(thr_cache, data);
185
        slab_free(thr_cache, data);
197
        data = new;
186
        data = new;
198
    }
187
    }
199
   
188
   
-
 
189
    TPRINTF("Thread #%" PRIu64 " finished\n", THREAD->tid);
-
 
190
   
200
    if (!sh_quiet)
191
    if (!test_quiet)
201
        printf("Thread #%" PRIu64 " finished\n", THREAD->tid);
192
        slab_print_list();
202
   
193
   
203
    slab_print_list();
-
 
204
    semaphore_up(&thr_sem);
194
    semaphore_up(&thr_sem);
205
}
195
}
206
 
196
 
207
static void multitest(int size, bool quiet)
197
static void multitest(int size)
208
{
198
{
209
    /* Start 8 threads that just allocate as much as possible,
199
    /* Start 8 threads that just allocate as much as possible,
210
     * then release everything, then again allocate, then release
200
     * then release everything, then again allocate, then release
211
     */
201
     */
212
    thread_t *t;
202
    thread_t *t;
213
    int i;
203
    int i;
214
   
204
   
215
    if (!quiet)
-
 
216
        printf("Running stress test with size %d\n", size);
205
    TPRINTF("Running stress test with size %d\n", size);
217
   
206
   
218
    condvar_initialize(&thread_starter);
207
    condvar_initialize(&thread_starter);
219
    mutex_initialize(&starter_mutex, MUTEX_PASSIVE);
208
    mutex_initialize(&starter_mutex, MUTEX_PASSIVE);
220
 
209
   
221
    thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0);
210
    thr_cache = slab_cache_create("thread_cache", size, 0, NULL, NULL, 0);
222
    semaphore_initialize(&thr_sem,0);
211
    semaphore_initialize(&thr_sem,0);
223
    for (i = 0; i < THREADS; i++) {  
212
    for (i = 0; i < THREADS; i++) {  
224
        if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest", false))) {
213
        if (!(t = thread_create(slabtest, NULL, TASK, 0, "slabtest", false))) {
225
            if (!quiet)
-
 
226
                printf("Could not create thread %d\n", i);
214
            TPRINTF("Could not create thread %d\n", i);
227
        } else
215
        } else
228
            thread_ready(t);
216
            thread_ready(t);
229
    }
217
    }
230
    thread_sleep(1);
218
    thread_sleep(1);
231
    condvar_broadcast(&thread_starter);
219
    condvar_broadcast(&thread_starter);
232
 
220
   
233
    for (i = 0; i < THREADS; i++)
221
    for (i = 0; i < THREADS; i++)
234
        semaphore_down(&thr_sem);
222
        semaphore_down(&thr_sem);
235
   
223
   
236
    slab_cache_destroy(thr_cache);
224
    slab_cache_destroy(thr_cache);
237
    if (!quiet)
-
 
238
        printf("Stress test complete.\n");
225
    TPRINTF("Stress test complete.\n");
239
}
226
}
240
 
227
 
241
char * test_slab2(bool quiet)
228
char *test_slab2(void)
242
{
229
{
-
 
230
    TPRINTF("Running reclaim single-thread test .. pass 1\n");
-
 
231
    totalmemtest();
-
 
232
   
-
 
233
    TPRINTF("Running reclaim single-thread test .. pass 2\n");
243
    sh_quiet = quiet;
234
    totalmemtest();
-
 
235
   
-
 
236
    TPRINTF("Reclaim test OK.\n");
244
   
237
   
245
    if (!quiet)
-
 
246
        printf("Running reclaim single-thread test .. pass 1\n");
-
 
247
    totalmemtest(quiet);
-
 
248
    if (!quiet)
-
 
249
        printf("Running reclaim single-thread test .. pass 2\n");
-
 
250
    totalmemtest(quiet);
-
 
251
    if (!quiet)
-
 
252
        printf("Reclaim test OK.\n");
-
 
253
   
-
 
254
    multitest(128, quiet);
238
    multitest(128);
255
    multitest(2048, quiet);
239
    multitest(2048);
256
    multitest(8192, quiet);
240
    multitest(8192);
257
   
241
   
258
    return NULL;
242
    return NULL;
259
}
243
}