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

Subversion Repositories c0or1k

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

powered by: WebSVN 2.1.0

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