-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemObj.cpp
122 lines (106 loc) · 3.3 KB
/
MemObj.cpp
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
#include <stdio.h>
#include <string.h>
#include <set>
#include "MemObj.h"
#include "Cache.h"
#include "DRAM.h"
std::map<std::string, MemObj*> MemObj::memObjs;
MemObj *MemObj::create(const char *name)
{
GError *error = NULL;
gchar* deviceType = NULL;
gchar* writePolicy= NULL;
assert(config && config->keyfile);
assert(name);
// If memory object already created, just return that one
std::map<std::string, MemObj*>::iterator it = memObjs.find(name);
if(it != memObjs.end()) {
MemObj *obj = it->second;
assert(obj);
return obj;
}
// Create memory object according to device type and write policy
MemObj *obj = NULL;
deviceType = g_key_file_get_string(config->keyfile, name, "deviceType", &error);
if(error != NULL) g_error (error->message);
if(!strcmp(deviceType, "dram")) {
obj = new DRAM(name);
} else if(!strcmp(deviceType, "cache")) {
writePolicy = g_key_file_get_string(config->keyfile, name, "writePolicy", &error);
if(error != NULL) g_error (error->message);
if(!strcmp(writePolicy, "WB")) {
obj = new WBCache(name);
} else if(!strcmp(writePolicy, "WT")) {
obj = new WTCache(name);
} else {
assert(0);
}
} else {
assert(0);
}
assert(obj);
// Register memory object to map
memObjs[name] = obj;
g_free(deviceType);
g_free(writePolicy);
return obj;
}
MemObj::MemObj(const char *s)
:name(s)
{
GError *error = NULL;
// If there is a lower level memory object, recursively create that one too!
lowerLevel = g_key_file_get_string(config->keyfile, s, "lowerLevel", &error);
if(error != NULL) g_error (error->message);
if(strcmp(lowerLevel, "null")) {
// If not "null" create that object
lowerLevelMemObj = MemObj::create(lowerLevel);
assert(lowerLevelMemObj);
}
}
MemObj::~MemObj()
{
g_free(lowerLevel);
}
void MemObj::freeAll()
{
std::map<std::string, MemObj*>::iterator it;
for(it = memObjs.begin(); it != memObjs.end(); it++) {
MemObj *obj = it->second;
delete obj;
}
memObjs.clear();
}
void MemObj::printAll()
{
printf("======================================================================\n\n");
printf("Printing all memory objects ... \n\n");
std::map<std::string, MemObj*>::iterator it;
for(it = memObjs.begin(); it != memObjs.end(); it++) {
MemObj *obj = it->second;
printf("%s\n", obj->toString().c_str());
}
printf("======================================================================\n");
}
void MemObj::printAllStats()
{
printf("======================================================================\n\n");
printf("Printing all memory stats ... \n\n");
std::map<std::string, MemObj*>::iterator it;
for(it = memObjs.begin(); it != memObjs.end(); it++) {
MemObj *obj = it->second;
printf("%s\n", obj->getStatString().c_str());
}
printf("\n======================================================================\n");
}
void MemObj::printAllContents()
{
printf("======================================================================\n");
printf("Printing all cache contents ...\n");
std::map<std::string, MemObj*>::iterator it;
for(it = memObjs.begin(); it != memObjs.end(); it++) {
MemObj *obj = it->second;
printf("%s", obj->getContentString().c_str());
}
printf("======================================================================\n");
}