forked from alibaba-edu/Alita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdt_manager.h
213 lines (188 loc) · 7.21 KB
/
rdt_manager.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef RDT_MANAGER_H
#define RDT_MANAGER_H
#include "intel_rdt.h"
typedef struct VMRdtInfo
{
set<int> cpuset;
int vote_cpu;
uint32_t rmid;
uint32_t cosid;
uint32_t cat_way;
uint32_t mba_rate;
} VMRdtInfo;
class RdtManager
{
public:
uint32_t nr_rmid;
vector<int> rmid_bitmap = vector<int>(nr_sockets, 1);
vector<int> cosid_bitmap = vector<int>(nr_sockets, 1);
uint32_t cat_nr_way;
uint32_t nr_cosid;
uint32_t mba_rate_max;
map<int, map<string, VMRdtInfo>> vm_rdtinfo;
RdtManager()
{
for (int socket = 0; socket < nr_sockets; socket++)
{
rdtcat.disable_cat_cdp(socket_vote_cpu[socket]);
}
this->nr_rmid = rdtmon.nr_rmid;
// cat & mba share the same cosid
this->nr_cosid = min(rdtcat.cat_nr_cosid, rdtmba.mba_nr_cosid);
// key parameters
this->cat_nr_way = rdtcat.cat_nr_way;
this->mba_rate_max = rdtmba.mba_rate_max;
//this->vm_rdtinfo = [dict() for _ in range(nr_sockets)]
// this->logger = get_logger("memory_contention")
}
// INFO MANAGE VM_RDTINFO
void init_vm_rdtinfo(int socket, string vmid, set<int> cpuset)
{
VMRdtInfo vm_rdtinfo;
vm_rdtinfo.cpuset = cpuset;
vm_rdtinfo.vote_cpu = *cpuset.begin(); //
vm_rdtinfo.rmid = RMID0;
vm_rdtinfo.cosid = COSID0;
vm_rdtinfo.cat_way = this->cat_nr_way;
vm_rdtinfo.mba_rate = this->mba_rate_max;
this->vm_rdtinfo[socket][vmid] = vm_rdtinfo;
}
void reset_vm_rdtinfo(int socket, string vmid, bool reset_rmid = false)
{
this->vm_rdtinfo[socket][vmid].cosid = COSID0;
this->vm_rdtinfo[socket][vmid].cat_way = this->cat_nr_way;
this->vm_rdtinfo[socket][vmid].mba_rate = this->mba_rate_max;
if (reset_rmid)
this->vm_rdtinfo[socket][vmid].rmid = RMID0;
}
// INFO PERFORMANCE COUNTERS
double vm_llc_occupancy(int socket, string vmid)
{
int vote_cpu = this->vm_rdtinfo[socket][vmid].vote_cpu;
return rdtmon.get_llc_occupancy(vote_cpu) * 1.0 / pow(1024.0, 2);
}
uint32_t rmid_alloc(int socket)
{
uint32_t rmid = RMID0;
for (uint32_t id = 1; id < nr_rmid; id++)
{
if ((this->rmid_bitmap[socket] & (1 << id)) == 0)
{
rmid = id;
this->rmid_bitmap[socket] |= (1 << id);
break;
}
}
return rmid;
}
uint32_t cosid_alloc(int socket)
{
uint32_t cosid = COSID0;
for (uint32_t id = 1; id < nr_cosid; id++)
{
if ((cosid_bitmap[socket] & (1 << id)) == 0)
{
cosid = id;
this->cosid_bitmap[socket] |= (1 << id);
break;
}
}
return cosid;
}
bool rmid_free(int socket, string vmid)
{
uint32_t rmid = this->vm_rdtinfo[socket][vmid].rmid;
if (rmid == RMID0)
return false;
for (auto iter = vm_rdtinfo[socket][vmid].cpuset.begin(); iter != vm_rdtinfo[socket][vmid].cpuset.end(); ++iter)
rdtmon.reset_rmid(*iter);
this->rmid_bitmap[socket] &= ~(1 << rmid);
//this->rdtdebug("rmid free", socket, vmid, rmid, header = "rdtmon")
return true;
}
bool cosid_free(int socket, string vmid)
{
uint32_t cosid = this->vm_rdtinfo[socket][vmid].cosid;
if (cosid == COSID0)
return false;
rdtcat.reset_cat_cbm(socket_vote_cpu[socket], cosid);
rdtmba.reset_mba_available(socket_vote_cpu[socket], cosid);
for (auto iter = vm_rdtinfo[socket][vmid].cpuset.begin(); iter != vm_rdtinfo[socket][vmid].cpuset.end(); ++iter)
rdtmba.reset_cosid(*iter);
this->cosid_bitmap[socket] &= ~(1 << cosid);
//this->rdtdebug("cosid free", socket, vmid, cosid, header="rdtalloc")
return true;
}
// INFO VM MONITORING & ISOLATION
bool monitor_vm(int socket, string vmid)
{
if (this->vm_rdtinfo[socket][vmid].rmid != RMID0)
return true;
uint32_t rmid = this->rmid_alloc(socket);
DEBUG_LOG("rmid %d", rmid);
if (rmid == RMID0)
//this->rdtdebug("run out of rmid", header="error")
return false;
for (auto iter = vm_rdtinfo[socket][vmid].cpuset.begin(); iter != vm_rdtinfo[socket][vmid].cpuset.end(); ++iter)
{
rdtmon.set_rmid(*iter, rmid);
DEBUG_LOG("CPU %d, rmid %d", *iter, rmid);
}
this->vm_rdtinfo[socket][vmid].rmid = rmid;
//this->rdtdebug(socket, vmid, rmid, this->vm_rdtinfo[socket][vmid]["cpuset"], header="rdtmon")
return true;
}
bool isolate_vm_llc(int socket, string vmid, uint32_t cat_way, int cat_cbm = -1)
{
int vote_cpu = this->vm_rdtinfo[socket][vmid].vote_cpu;
uint32_t cosid = this->vm_rdtinfo[socket][vmid].cosid;
if (cosid == COSID0)
{
cosid = this->cosid_alloc(socket);
DEBUG_LOG("cosid %d", cosid);
if (cosid == COSID0)
//this->rdtdebug("run out of cosid", header="error")
return false;
for (auto iter = vm_rdtinfo[socket][vmid].cpuset.begin(); iter != vm_rdtinfo[socket][vmid].cpuset.end(); ++iter)
rdtcat.set_cosid(*iter, cosid);
this->vm_rdtinfo[socket][vmid].cosid = cosid;
//this->rdtdebug(socket, vmid, cosid, this->vm_rdtinfo[socket][vmid]["cpuset"], header="rdtalloc")
}
if (cat_cbm == -1 || !rdtcat.is_cbm_contiguous(cat_cbm))
cat_cbm = (1 << cat_way) - 1;
rdtcat.set_cat_cbm(vote_cpu, cosid, cat_cbm);
this->vm_rdtinfo[socket][vmid].cat_way = cat_way;
//this->rdtdebug(socket, vmid, cosid, cat_way, header="rdtcat")
return true;
}
bool isolate_vm_mem(int socket, string vmid, int mba_rate)
{
int vote_cpu = this->vm_rdtinfo[socket][vmid].vote_cpu;
uint32_t cosid = this->vm_rdtinfo[socket][vmid].cosid;
if (cosid == COSID0)
{
cosid = this->cosid_alloc(socket);
if (cosid == COSID0)
//this->rdtdebug("run out of cosid", header="error")
return false;
for (auto iter = vm_rdtinfo[socket][vmid].cpuset.begin(); iter != vm_rdtinfo[socket][vmid].cpuset.end(); ++iter)
rdtcat.set_cosid(*iter, cosid);
this->vm_rdtinfo[socket][vmid].cosid = cosid;
//this->rdtdebug(socket, vmid, cosid, this->vm_rdtinfo[socket][vmid]["cpuset"], header="rdtalloc")
}
rdtmba.set_mba_available(vote_cpu, cosid, mba_rate);
// we are NOT absolutely sure about effective mba rate
this->vm_rdtinfo[socket][vmid].mba_rate = rdtmba.get_mba_available(vote_cpu, cosid);
//this->rdtdebug(socket, vmid, cosid, this->vm_rdtinfo[socket][vmid]["mba_rate"], header="rdtmba")
return true;
}
void disable_mba()
{
rdtmba.clean_up();
for (int socket = 0; socket < nr_sockets; socket++)
for (auto iter = vm_rdtinfo[socket].begin(); iter != vm_rdtinfo[socket].end(); ++iter)
this->vm_rdtinfo[socket][iter->first].mba_rate = this->mba_rate_max;
}
};
RdtManager rdtmgr;
#endif