Subversion Repositories HelenOS

Rev

Rev 2050 | Rev 2071 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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