aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xenpaging/policy_default.c
blob: 6cce0ed31726e38d1aedd1403c41a156e47d6111 (plain)
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
/******************************************************************************
 *
 * Xen domain paging default policy.
 *
 * Copyright (c) 2009 Citrix Systems, Inc. (Patrick Colp)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "xc_bitops.h"
#include "policy.h"


#define DEFAULT_MRU_SIZE (1024 * 16)


static unsigned long *mru;
static unsigned int i_mru;
static unsigned int mru_size;
static unsigned long *bitmap;
static unsigned long *unconsumed;
static unsigned long current_gfn;
static unsigned long max_pages;


int policy_init(xenpaging_t *paging)
{
    int i;
    int rc = -ENOMEM;

    /* Allocate bitmap for pages not to page out */
    bitmap = bitmap_alloc(paging->domain_info->max_pages);
    if ( !bitmap )
        goto out;
    /* Allocate bitmap to track unusable pages */
    unconsumed = bitmap_alloc(paging->domain_info->max_pages);
    if ( !unconsumed )
        goto out;

    max_pages = paging->domain_info->max_pages;

    /* Initialise MRU list of paged in pages */
    if ( paging->policy_mru_size > 0 )
        mru_size = paging->policy_mru_size;
    else
        mru_size = DEFAULT_MRU_SIZE;

    mru = malloc(sizeof(*mru) * mru_size);
    if ( mru == NULL )
        goto out;

    for ( i = 0; i < mru_size; i++ )
        mru[i] = INVALID_MFN;

    /* Don't page out page 0 */
    set_bit(0, bitmap);

    /* Start in the middle to avoid paging during BIOS startup */
    current_gfn = max_pages / 2;
    current_gfn -= paging->num_pages / 2;

    rc = 0;
 out:
    return rc;
}

int policy_choose_victim(xenpaging_t *paging, xenpaging_victim_t *victim)
{
    xc_interface *xch = paging->xc_handle;
    unsigned long wrap = current_gfn;

    do
    {
        current_gfn++;
        if ( current_gfn >= max_pages )
            current_gfn = 0;
        if ( wrap == current_gfn )
        {
            victim->gfn = INVALID_MFN;
            return -ENOSPC;
        }
    }
    while ( test_bit(current_gfn, bitmap) || test_bit(current_gfn, unconsumed) );

    set_bit(current_gfn, unconsumed);
    victim->gfn = current_gfn;

    return 0;
}

void policy_notify_paged_out(unsigned long gfn)
{
    set_bit(gfn, bitmap);
    clear_bit(gfn, unconsumed);
}

void policy_notify_paged_in(unsigned long gfn)
{
    unsigned long old_gfn = mru[i_mru & (mru_size - 1)];

    if ( old_gfn != INVALID_MFN )
        clear_bit(old_gfn, bitmap);
    
    mru[i_mru & (mru_size - 1)] = gfn;
    i_mru++;
}


/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */