]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/benchmark/server/src/main.c
4181d76ed602192e757729330f78d1b8ac4bb60f
[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 <stdlib.h>
18 #include <unistd.h>
19
20 #include <l4/util/rdtsc.h>
21 #include <l4/sys/l4int.h>
22
23 void use_dummy(int result);
24 size_t* words_initialize(size_t max, int scale);
25 l4_uint64_t parse_param( char param, int argc, char* argv[]);
26
27 static volatile l4_uint64_t use_result_dummy;
28 void use_dummy(int result) { use_result_dummy += result; }
29
30 /*
31  * words_initialize
32  *
33  * This is supposed to create the order in which the words in a 
34  * "cache line" are used.  Since we rarely know the cache line
35  * size with any real reliability, we need to jump around so
36  * as to maximize the number of potential cache misses, and to
37  * minimize the possibility of re-using a cache line.
38  */
39 size_t* words_initialize(size_t max, int scale)
40 {
41         size_t  i, j, nbits;
42         size_t* words = (size_t*)calloc(max, sizeof(size_t));
43
44         if (!words) return NULL;
45
46         //bzero(words, max * sizeof(size_t));
47         for (i = max>>1, nbits = 0; i != 0; i >>= 1, nbits++)
48                 ;
49         for (i = 0; i < max; ++i) {
50                 /* now reverse the bits */
51                 for (j = 0; j < nbits; j++) {
52                         if (i & (1<<j)) {
53                                 words[i] |= (1<<(nbits-j-1));
54                         }
55                 }
56                 words[i] *= scale;
57         }
58         return words;
59 }
60
61 l4_uint64_t parse_param( char param, int argc, char* argv[])
62 {
63         int i;
64         for (i = 1; i < argc; i++){
65                 if ('-' == argv[i][0] && param == argv[i][1] ){
66                         return atoll(argv[i+1]);                
67                 }
68         }
69         return 0;
70 }
71
72 #define DEF_SIZE (1000000)
73 #define DEF_STRIDE (10)
74
75
76 int main(int argc, char* argv[])
77 {
78                         
79         l4_uint64_t time_start, time_end, time_diff, j=0, i, stride = DEF_STRIDE;;
80         l4_uint64_t size = DEF_SIZE;;
81         //size = parse_param('s', argc, argv);
82         //if ( !size ) 
83         //stride = parse_param('t', argc, argv); 
84         //if ( !stride ) 
85         
86         size_t * arry = words_initialize(size, 5);
87         if (!arry) {
88                 printf("Init failed.");
89                 return -1;
90         }       
91         
92         printf("Start benchmark: array size is %llu, stride is %llu\n", size, stride);
93         while(1){
94                 time_start = l4_tsc_to_us(l4_rdtsc());              
95                 for (i = 0; i < size; i += stride) {
96                         j += arry[i];
97                 }
98                 time_end = l4_tsc_to_us(l4_rdtsc());
99                 use_dummy(j);
100                 time_diff = (time_end-time_start);
101                 printf("time: %lld us, size/time %lld", time_diff,  size/time_diff );
102         }
103 }