]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/benchmark/server/src/main.c
l4: benchmark: Make benchmark work.
[l4.git] / l4 / pkg / benchmark / server / src / main.c
1 //taken from [..see below..] and modifed a little.
2 /*
3  * lib_mem.c - library of routines used to analyze the memory hierarchy
4  *
5  * @(#)lib_mem.c 1.15 staelin@hpliclu2.hpli.hpl.hp.com
6  *
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.
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19
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>
24
25 #include <l4/util/rdtsc.h>
26 #include <l4/sys/l4int.h>
27 #include <l4/sys/kip.h>
28 #include <l4/re/env.h>
29
30 #define DEF_SIZE (4*1024)
31 #define DEF_STRIDE (1)
32 #define TEST_TYPE l4_uint8_t
33 #define POLLITE_CACHE
34
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,
42 //                         void **virt_addr)
43 // {
44 //   int r;
45 //   l4re_ds_t ds;
46
47 //   /* Allocate a free capability index for our data space */
48 //   ds = l4re_util_cap_alloc();
49 //   if (l4_is_invalid_cap(ds))
50 //     return -L4_ENOMEM;
51
52 //   size_in_bytes = l4_trunc_page(size_in_bytes);
53
54 //   /* Allocate memory via a dataspace */
55 //   if ((r = l4re_ma_alloc(size_in_bytes, ds, flags)))
56 //     return r;
57
58 //   /* Make the dataspace visible in our address space */
59 //   *virt_addr = 0;
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)))
64 //     return r;
65
66 //   /* Done, virtual address is in virt_addr */
67 //   return 0;
68 // }
69
70 // /**
71 //  * \brief Free previously allocated memory.
72 //  *
73 //  * \param virt_addr    Virtual address return by allocate_mem
74 //  *
75 //  * \return 0 on success, error code otherwise
76 //  */
77 // static int free_mem(void *virt_addr)
78 // {
79 //   int r;
80 //   l4re_ds_t ds;
81
82 //   /* Detach memory from our address space */
83 //   if ((r = l4re_rm_detach_ds(virt_addr, &ds)))
84 //     return r;
85
86 //   /* Free memory at our memory allocator */
87 //   if ((r = l4re_ma_free(ds)))
88 //     return r;
89
90 //   l4re_util_cap_free(ds);
91
92 //   /* All went ok */
93 //   return 0;
94 // }
95
96 //-------------------------------------------------------------
97 static volatile l4_uint64_t use_result_dummy;
98 void use_dummy(int result) { use_result_dummy += result; }
99
100 /*
101  * words_initialize
102  *
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.
108  */
109
110 TEST_TYPE* words_initialize(l4_uint64_t max, int scale)
111 {
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);
116
117         if (!words) return NULL;
118
119         printf("Start init.");
120         //bzero(words, max * sizeof(size_t));
121 #ifdef POLLITE_CACHE
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++) {
126                         if (i & (1<<j)) {
127                                 words[i] |= (1<<(nbits-j-1));
128                         }
129                 }
130                 words[i] *= scale;
131          }
132  #endif
133         return words;
134 }
135
136 l4_uint64_t parse_param( char param, int argc, char* argv[])
137 {
138         int i;
139         for (i = 1; i < argc; i++){
140                 if ('-' == argv[i][0] && param == argv[i][1] ){
141                         return atoll(argv[i+1]);                
142                 }
143         }
144         return 0;
145 }
146
147
148 int main(int argc, char* argv[])
149 {
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);
154         // //if ( !size ) 
155         // //stride = parse_param('t', argc, argv); 
156         // //if ( !stride ) 
157         
158         TEST_TYPE * arry = words_initialize(size, 5);
159         printf("Done init.\n");
160          if (!arry) {
161                 printf("Init failed.");
162                 return -1;
163          }      
164         
165         printf("Start benchmark: array size is %llu, stride is %llu.\n", size, stride);
166         l4_calibrate_tsc(l4re_kip());
167
168         while(1){
169                 
170                 time_start = l4_tsc_to_us(l4_rdtsc());              
171                 for (i = 0; i < size; i += stride) {
172                         j += arry[i];
173                 }
174                 time_end = l4_tsc_to_us(l4_rdtsc());
175                 use_dummy(j);
176
177                 time_diff = (time_end-time_start);
178             double baud = ((double) size*(sizeof(TEST_TYPE)))/ ((double) time_diff);
179
180                 printf("time: %llu us, baud: %lf bytes/us\n", time_diff, baud);
181                 sleep(1);
182         }
183 }