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/util/rdtsc.h>
21 #include <l4/sys/l4int.h>
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[]);
27 static volatile l4_uint64_t use_result_dummy;
28 void use_dummy(int result) { use_result_dummy += result; }
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.
39 size_t* words_initialize(size_t max, int scale)
42 size_t* words = (size_t*)calloc(max, sizeof(size_t));
44 if (!words) return NULL;
46 //bzero(words, max * sizeof(size_t));
47 for (i = max>>1, nbits = 0; i != 0; i >>= 1, nbits++)
49 for (i = 0; i < max; ++i) {
50 /* now reverse the bits */
51 for (j = 0; j < nbits; j++) {
53 words[i] |= (1<<(nbits-j-1));
61 l4_uint64_t parse_param( char param, int argc, char* argv[])
64 for (i = 1; i < argc; i++){
65 if ('-' == argv[i][0] && param == argv[i][1] ){
66 return atoll(argv[i+1]);
72 #define DEF_SIZE (1000000)
73 #define DEF_STRIDE (10)
76 int main(int argc, char* argv[])
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);
83 //stride = parse_param('t', argc, argv);
86 size_t * arry = words_initialize(size, 5);
88 printf("Init failed.");
92 printf("Start benchmark: array size is %llu, stride is %llu\n", size, stride);
94 time_start = l4_tsc_to_us(l4_rdtsc());
95 for (i = 0; i < size; i += stride) {
98 time_end = l4_tsc_to_us(l4_rdtsc());
100 time_diff = (time_end-time_start);
101 printf("time: %lld us, size/time %lld", time_diff, size/time_diff );