1 //taken from [..see below..] and modifed a little.
3 * lib_mem.c - library of routines used to analyze the memory hierarchy
5 * @(#)lib_mem.c 1.15 staelin@hpliclu2.hpli.hpl.hp.com
7 * Copyright (c) 2000 Carl Staelin.
8 * Copyright (c) 1994 Larry McVoy.
9 * Distributed under the FSF GPL with
10 * additional restriction that results may published only if
11 * (1) the benchmark is unmodified, and
12 * (2) the version in the sccsid below is included in the report.
13 * Support for this development by Sun Microsystems is gratefully acknowledged.
20 // #include <l4/re/c/mem_alloc.h>
21 // #include <l4/re/c/rm.h>
22 // #include <l4/re/c/util/cap_alloc.h>
23 // #include <l4/sys/err.h>
25 #include <l4/util/rdtsc.h>
26 #include <l4/sys/l4int.h>
27 #include <l4/sys/kip.h>
28 #include <l4/re/env.h>
30 #define DEF_SIZE (4*1024)
31 #define DEF_STRIDE (1)
32 #define TEST_TYPE l4_uint8_t
35 void use_dummy(int result);
36 TEST_TYPE* words_initialize(l4_uint64_t max, int scale);
37 l4_uint64_t parse_param( char param, int argc, char* argv[]);
38 //-------------------------------------------------------------
39 //---------L4 memory allocation--------------------------------
40 /*------from examples/libs/l4re/c/ma+rm.c example-------------*/
41 // static int allocate_mem(unsigned long size_in_bytes, unsigned long flags,
47 // /* Allocate a free capability index for our data space */
48 // ds = l4re_util_cap_alloc();
49 // if (l4_is_invalid_cap(ds))
52 // size_in_bytes = l4_trunc_page(size_in_bytes);
54 // /* Allocate memory via a dataspace */
55 // if ((r = l4re_ma_alloc(size_in_bytes, ds, flags)))
58 // /* Make the dataspace visible in our address space */
60 // if ((r = l4re_rm_attach(virt_addr, size_in_bytes,
61 // L4RE_RM_SEARCH_ADDR, ds, 0,
62 // flags & L4RE_MA_SUPER_PAGES
63 // ? L4_SUPERPAGESHIFT : L4_PAGESHIFT)))
66 // /* Done, virtual address is in virt_addr */
71 // * \brief Free previously allocated memory.
73 // * \param virt_addr Virtual address return by allocate_mem
75 // * \return 0 on success, error code otherwise
77 // static int free_mem(void *virt_addr)
82 // /* Detach memory from our address space */
83 // if ((r = l4re_rm_detach_ds(virt_addr, &ds)))
86 // /* Free memory at our memory allocator */
87 // if ((r = l4re_ma_free(ds)))
90 // l4re_util_cap_free(ds);
96 //-------------------------------------------------------------
97 static volatile l4_uint64_t use_result_dummy;
98 void use_dummy(int result) { use_result_dummy += result; }
103 * This is supposed to create the order in which the words in a
104 * "cache line" are used. Since we rarely know the cache line
105 * size with any real reliability, we need to jump around so
106 * as to maximize the number of potential cache misses, and to
107 * minimize the possibility of re-using a cache line.
110 TEST_TYPE* words_initialize(l4_uint64_t max, int scale)
112 l4_uint64_t i, j, nbits;
113 TEST_TYPE* words = (TEST_TYPE*) malloc(max);
114 printf("%llu bytes allocated.\n", max*sizeof(TEST_TYPE));
115 memset(words, 0, max);
117 if (!words) return NULL;
119 printf("Start init.");
120 //bzero(words, max * sizeof(size_t));
122 for (i = max>>1, nbits = 0; i != 0; i >>= 1, nbits++);
123 for (i = 0; i < max; ++i) {
124 /* now reverse the bits */
125 for (j = 0; j < nbits; j++) {
127 words[i] |= (1<<(nbits-j-1));
136 l4_uint64_t parse_param( char param, int argc, char* argv[])
139 for (i = 1; i < argc; i++){
140 if ('-' == argv[i][0] && param == argv[i][1] ){
141 return atoll(argv[i+1]);
148 int main(int argc, char* argv[])
150 printf("Benchmark started.\n");
151 l4_uint64_t time_start, time_end, time_diff, j=0, i, stride = DEF_STRIDE;
152 l4_uint64_t size = DEF_SIZE;
153 // //size = parse_param('s', argc, argv);
155 // //stride = parse_param('t', argc, argv);
158 TEST_TYPE * arry = words_initialize(size, 5);
159 printf("Done init.\n");
161 printf("Init failed.");
165 printf("Start benchmark: array size is %llu, stride is %llu.\n", size, stride);
166 l4_calibrate_tsc(l4re_kip());
170 time_start = l4_tsc_to_us(l4_rdtsc());
171 for (i = 0; i < size; i += stride) {
174 time_end = l4_tsc_to_us(l4_rdtsc());
177 time_diff = (time_end-time_start);
178 double baud = ((double) size*(sizeof(TEST_TYPE)))/ ((double) time_diff);
180 printf("time: %llu us, baud: %lf bytes/us\n", time_diff, baud);