aboutsummaryrefslogtreecommitdiffstats
path: root/include/cache_common.h
blob: 8239233a7a610d576bacaaa44d22b603f8cb4fb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef __CACHE_COMMON_H__
#define __CACHE_COMMON_H__

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <assert.h>

#include <signal.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/io.h>
#include <sys/utsname.h>

#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>

#include "litmus.h"
#include "asm/cycles.h"

#if defined(__i386__) || defined(__x86_64__)
#include "asm/irq.h"
#endif


#define UNCACHE_DEV "/dev/litmus/uncache"

static void die(char *error)
{
    fprintf(stderr, "Error: %s (errno: %m)\n",
        error);
    exit(1);
}

static int lock_memory(void)
{
    return mlockall(MCL_CURRENT | MCL_FUTURE);
}

/* define CACHELINE_SIZE if not provided by compiler args */
#ifndef CACHELINE_SIZE
#if defined(__i386__) || defined(__x86_64__)
/* recent intel cpus */
#define CACHELINE_SIZE 64
#elif defined(__arm__)
/* at least with Cortex-A9 cpus ("8 words") */
#define CACHELINE_SIZE 32
#else
#error "Could not determine cacheline size!"
#endif
#endif

#define INTS_IN_CACHELINE (CACHELINE_SIZE/sizeof(int))
typedef struct cacheline
{
        int line[INTS_IN_CACHELINE];
} __attribute__((aligned(CACHELINE_SIZE))) cacheline_t;

#define CACHELINES_IN_1KB (1024 / sizeof(cacheline_t))


static cacheline_t* alloc_arena(size_t size, int use_huge_pages, int use_uncache_pages)
{
    int flags = MAP_PRIVATE | MAP_POPULATE;
    cacheline_t* arena = NULL;
    int fd;

    if(use_huge_pages)
        flags |= MAP_HUGETLB;

	if(use_uncache_pages) {
			fd = open(UNCACHE_DEV, O_RDWR);
			if (fd == -1)
					die("Failed to open uncache device. Are you running the LITMUS^RT kernel?");
	}
	else {
			fd = -1;
			flags |= MAP_ANONYMOUS;
	}

    arena = mmap(0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
	
    if(use_uncache_pages)
		close(fd);

    assert(arena);

        return arena;
}

static void dealloc_arena(cacheline_t* arena, size_t size)
{
		int ret = munmap((void*)arena, size);
        if(ret != 0)
                die("munmap() error");
}

static int randrange(int min, int max)
{
        /* generate a random number on the range [min, max) w/o skew */
        int limit = max - min;
        int devisor = RAND_MAX/limit;
        int retval;

        do {
                retval = rand() / devisor;
        } while(retval == limit);
        retval += min;

        return retval;
}

static void init_arena(cacheline_t* arena, size_t size)
{
    int i;
        size_t num_arena_elem = size / sizeof(cacheline_t);

        /* Generate a cycle among the cache lines using Sattolo's algorithm.
           Every int in the cache line points to the same cache line.
           Note: Sequential walk doesn't care about these values. */
        for (i = 0; i < num_arena_elem; i++) {
                int j;
                for(j = 0; j < INTS_IN_CACHELINE; ++j)
                        arena[i].line[j] = i;
        }
        while(1 < i--) {
                int j = randrange(0, i);
                cacheline_t temp = arena[j];
                arena[j] = arena[i];
                arena[i] = temp;
        }
}

#endif