OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [libmem/] [tests/] [main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
#include <stdio.h>
2
#include <malloc.h>
3
#include <string.h>
4
#include <stdlib.h>
5
 
6
#include <l4/macros.h>
7
#include <l4/config.h>
8
#include <kmalloc/kmalloc.h>
9
#include <mm/alloc_page.h>
10
 
11
#include INC_SUBARCH(mm.h)
12
#include INC_ARCH(linker.h)
13
#include INC_PLAT(print-early.h)
14
#include INC_PLAT(offsets.h)
15
#include INC_GLUE(memlayout.h)
16
 
17
#include "tests.h"
18
#include "test_kmalloc.h"
19
#include "test_allocpage.h"
20
#include "test_memcache.h"
21
#include "clz.h"
22
#include "memory.h"
23
#include "libl4.h"
24
#include "debug.h"
25
 
26
unsigned int TEST_PHYSMEM_TOTAL_PAGES = 250;
27
unsigned int TEST_PHYSMEM_TOTAL_SIZE;
28
unsigned int PHYS_MEM_START;
29
unsigned int PHYS_MEM_END;
30
 
31
void *malloced_test_memory;
32
 
33
void memory_initialise(void)
34
{
35
        init_page_allocator(PHYS_MEM_START, PHYS_MEM_END);
36
        kmalloc_init();
37
}
38
 
39
/* Allocating memory from the host C library, and
40
 * it is used as if it is the physical memory available
41
 * on the system.
42
 */
43
void alloc_test_memory()
44
{
45
        TEST_PHYSMEM_TOTAL_SIZE = (PAGE_SIZE * TEST_PHYSMEM_TOTAL_PAGES);
46
 
47
        if (!(malloced_test_memory = malloc(TEST_PHYSMEM_TOTAL_SIZE)))
48
                printf("Host system out of memory.\n");
49
        PHYS_MEM_START = (unsigned int)malloced_test_memory;
50
        PHYS_MEM_END = PHYS_MEM_START + TEST_PHYSMEM_TOTAL_SIZE;
51
        PHYS_MEM_START = page_align_up(PHYS_MEM_START);
52
        PHYS_MEM_END = page_align(PHYS_MEM_END);
53
        /* Normally _end is to know where the loaded kernel image
54
         * ends in physical memory, so the system can start allocating
55
         * physical memory from there. Because in our mock-up there's no
56
         * used space in the malloc()'ed memory, _end is the same as the
57
         * beginning of malloc()'ed memory.
58
         */
59
        _end = PHYS_MEM_START;
60
 
61
        dprintf("Initialising physical memory\n");
62
        dprintf("Initialising allocators:\n");
63
        memory_initialise();
64
 
65
}
66
 
67
struct cmdline_opts {
68
        char run_allocator;
69
        int allocations;
70
        int alloc_size_max;
71
        int physmem_pages;
72
        int page_size;
73
        int no_of_pages;
74
        char *finit_path;
75
        char *fexit_path;
76
} options;
77
 
78
int check_options_validity(struct cmdline_opts *opts)
79
{
80
        if (opts->allocations <= 0) {
81
                printf("Invalid number of allocations: %d\n", opts->allocations);
82
                return -1;
83
        }
84
        if (opts->no_of_pages <= 0) {
85
                printf("Invalid number of pages: %d\n", opts->no_of_pages);
86
                return -1;
87
        }
88
        if (opts->alloc_size_max <= 0) {
89
                printf("Invalid alloc_size_max: %d\n", opts->alloc_size_max);
90
                return -1;
91
        }
92
        if (opts->page_size <= 0) {
93
                printf("Invalid page_size: %d\n", opts->page_size);
94
                return -1;
95
        }
96
        return 0;
97
}
98
 
99
void print_options(struct cmdline_opts *opts)
100
{
101
        dprintf("Running: %s\n",
102
               ((opts->run_allocator == 'p') ? "page allocator" :
103
                ((opts->run_allocator == 'k') ? "kmalloc/kfree" :
104
                 "memcache allocator")));
105
        dprintf("Total allocations: %d\n", opts->allocations);
106
        dprintf("Maximum allocation size: %d, 0x%x(hex)\n\n",
107
               opts->alloc_size_max, opts->alloc_size_max);
108
        dprintf("Initial state file: %s\n", opts->finit_path);
109
        dprintf("Exit state file: %s\n", opts->fexit_path);
110
 
111
}
112
 
113
void display_help(void)
114
{
115
        printf("Main:\n");
116
        printf("\tUsage:\n");
117
        printf("\tmain\t-a=<p>|<k>|<m> [-n=<number of allocations>] [-s=<maximum size for any allocation>]\n"
118
               "\t\t[-fi=<file to dump init state>] [-fx=<file to dump exit state>]\n"
119
               "\t\t[-ps=<page size>] [-pn=<total number of pages>]\n");
120
        printf("\n");
121
}
122
 
123
int get_cmdline_opts(int argc, char *argv[], struct cmdline_opts *opts)
124
{
125
        int parsed = 0;
126
 
127
        memset(opts, 0, sizeof (struct cmdline_opts));
128
        if (argc <= 1)
129
                return -1;
130
        for (int i = 1; i < argc; i++) {
131
                if (argv[i][0] == '-' && argv[i][2] == '=') {
132
                        if (argv[i][1] == 'a') {
133
                                if (argv[i][3] == 'k' ||
134
                                    argv[i][3] == 'm' ||
135
                                    argv[i][3] == 'p') {
136
                                        opts->run_allocator = argv[i][3];
137
                                        parsed = 1;
138
                                }
139
                        }
140
                        if (argv[i][1] == 'n') {
141
                                opts->allocations = atoi(&argv[i][3]);
142
                                parsed = 1;
143
                        }
144
                        if (argv[i][1] == 's') {
145
                                opts->alloc_size_max = atoi(&argv[i][3]);
146
                                parsed = 1;
147
                        }
148
                }
149
                if (argv[i][0] == '-' && argv[i][1] == 'f'
150
                    && argv[i][3] == '=') {
151
                        if (argv[i][2] == 'i') {
152
                                opts->finit_path = &argv[i][4];
153
                                parsed = 1;
154
                        }
155
                        if (argv[i][2] == 'x') {
156
                                opts->fexit_path = &argv[i][4];
157
                                parsed = 1;
158
                        }
159
                }
160
                if (argv[i][0] == '-' && argv[i][1] == 'p'
161
                    && argv[i][3] == '=') {
162
                        if (argv[i][2] == 's') {
163
                                opts->page_size = atoi(&argv[i][4]);
164
                                parsed = 1;
165
                        }
166
                        if (argv[i][2] == 'n') {
167
                                opts->no_of_pages = atoi(&argv[i][4]);
168
                                parsed = 1;
169
                        }
170
                }
171
        }
172
 
173
        if (!parsed)
174
                return -1;
175
        return 0;
176
}
177
 
178
void get_output_files(FILE **out1, FILE **out2,
179
                      char *alloc_func_name, char *rootpath)
180
{
181
        char pathbuf[150];
182
        char *root = "/tmp/";
183
        char *initstate_prefix = "test_initstate_";
184
        char *endstate_prefix = "test_endstate_";
185
        char *extension = ".out";
186
 
187
        if (!rootpath)
188
                rootpath = root;
189
        /* File path manipulations */
190
        sprintf(pathbuf, "%s%s%s%s", rootpath, initstate_prefix, alloc_func_name, extension);
191
        *out1 = fopen(pathbuf,"w+");
192
        sprintf(pathbuf, "%s%s%s%s", rootpath, endstate_prefix, alloc_func_name, extension);
193
        *out2 = fopen(pathbuf, "w+");
194
        return;
195
}
196
 
197
int main(int argc, char *argv[])
198
{
199
        FILE *finit, *fexit;
200
        int output_files = 0;
201
        if (get_cmdline_opts(argc, argv, &options) < 0) {
202
                display_help();
203
                return 1;
204
        }
205
        print_options(&options);
206
        if (check_options_validity(&options) < 0)
207
                exit(1);
208
 
209
        if (options.finit_path && options.fexit_path) {
210
                finit = fopen(options.finit_path, "w+");
211
                fexit = fopen(options.fexit_path, "w+");
212
                output_files = 1;
213
        }
214
        if (options.page_size) {
215
                PAGE_SIZE = options.page_size;
216
                PAGE_MASK = PAGE_SIZE - 1;
217
                PAGE_BITS = 32 - __clz(PAGE_MASK);
218
                dprintf("Using: Page Size: %d\n", PAGE_SIZE);
219
                dprintf("Using: Page Mask: 0x%x\n", PAGE_MASK);
220
                dprintf("Using: Page Bits: %d\n", PAGE_BITS);
221
        }
222
        if (options.no_of_pages) {
223
                dprintf("Using: Total pages: %d\n", options.no_of_pages);
224
                TEST_PHYSMEM_TOTAL_PAGES = options.no_of_pages;
225
        }
226
        alloc_test_memory();
227
        if (options.run_allocator == 'p') {
228
                if (!output_files)
229
                        get_output_files(&finit, &fexit, "alloc_page", 0);
230
                test_allocpage(options.allocations, options.alloc_size_max,
231
                               finit, fexit);
232
        } else if (options.run_allocator == 'k') {
233
                if (!output_files)
234
                        get_output_files(&finit, &fexit, "kmalloc", 0);
235
                test_kmalloc(options.allocations, options.alloc_size_max,
236
                             finit, fexit);
237
        } else if (options.run_allocator == 'm') {
238
                if (!output_files)
239
                        get_output_files(&finit, &fexit, "memcache", 0);
240
                test_memcache(options.allocations, options.alloc_size_max,
241
                              finit, fexit, 1);
242
        } else {
243
                printf("Invalid allocator option.\n");
244
        }
245
        free((void *)malloced_test_memory);
246
        fclose(finit);
247
        fclose(fexit);
248
        return 0;
249
}
250
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.