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
214
215
216
217
|
#pragma once
#include "xmrstak/misc/console.hpp"
#include "xmrstak/misc/configEditor.hpp"
#include "xmrstak/params.hpp"
#include "xmrstak/backend/cryptonight.hpp"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32
#include <string>
#include <hwloc.h>
#include <stdio.h>
namespace xmrstak
{
namespace cpu
{
class autoAdjust
{
public:
autoAdjust()
{
hashMemSize = cn_select_memory(::jconf::inst()->GetMiningAlgo());
halfHashMemSize = hashMemSize / 2u;
}
bool printConfig()
{
hwloc_topology_t topology;
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
std::string conf;
configEditor configTpl{};
// load the template of the backend config into a char variable
const char *tpl =
#include "./config.tpl"
;
configTpl.set( std::string(tpl) );
try
{
std::vector<hwloc_obj_t> tlcs;
tlcs.reserve(16);
results.reserve(16);
findChildrenCaches(hwloc_get_root_obj(topology),
[&tlcs](hwloc_obj_t found) { tlcs.emplace_back(found); } );
if(tlcs.size() == 0)
throw(std::runtime_error("The CPU doesn't seem to have a cache."));
for(hwloc_obj_t obj : tlcs)
proccessTopLevelCache(obj);
for(uint32_t id : results)
{
conf += std::string(" { \"low_power_mode\" : ");
conf += std::string((id & 0x8000000) != 0 ? "true" : "false");
conf += std::string(", \"be_mode\" : true, \"affine_to_cpu\" : ");
conf += std::to_string(id & 0x7FFFFFF);
conf += std::string(" },\n");
}
}
catch(const std::runtime_error& err)
{
// \todo add fallback to default auto adjust
conf += std::string(" { \"low_power_mode\" : false, \"be_mode\" : true, \"affine_to_cpu\" : false },\n");
printer::inst()->print_msg(L0, "Autoconf FAILED: %s. Create config for a single thread.", err.what());
}
configTpl.replace("CPUCONFIG",conf);
configTpl.write(params::inst().configFileCPU);
printer::inst()->print_msg(L0, "CPU configuration stored in file '%s'", params::inst().configFileCPU.c_str());
/* Destroy topology object. */
hwloc_topology_destroy(topology);
return true;
}
private:
size_t hashMemSize;
size_t halfHashMemSize;
std::vector<uint32_t> results;
template<typename func>
inline void findChildrenByType(hwloc_obj_t obj, hwloc_obj_type_t type, func lambda)
{
for(size_t i=0; i < obj->arity; i++)
{
if(obj->children[i]->type == type)
lambda(obj->children[i]);
else
findChildrenByType(obj->children[i], type, lambda);
}
}
inline bool isCacheObject(hwloc_obj_t obj)
{
#if HWLOC_API_VERSION >= 0x20000
return hwloc_obj_type_is_cache(obj->type);
#else
return obj->type == HWLOC_OBJ_CACHE;
#endif // HWLOC_API_VERSION
}
template<typename func>
inline void findChildrenCaches(hwloc_obj_t obj, func lambda)
{
for(size_t i=0; i < obj->arity; i++)
{
if(isCacheObject(obj->children[i]))
lambda(obj->children[i]);
else
findChildrenCaches(obj->children[i], lambda);
}
}
inline bool isCacheExclusive(hwloc_obj_t obj)
{
const char* value = hwloc_obj_get_info_by_name(obj, "Inclusive");
return value == nullptr || value[0] != '1';
}
// Top level cache isn't shared with other cores on the same package
// This will usually be 1 x L3, but can be 2 x L2 per package
void proccessTopLevelCache(hwloc_obj_t obj)
{
if(obj->attr == nullptr)
throw(std::runtime_error("Cache object hasn't got attributes."));
size_t PUs = 0;
findChildrenByType(obj, HWLOC_OBJ_PU, [&PUs](hwloc_obj_t found) { PUs++; } );
//Strange case, but we will handle it silently, surely there must be one PU somewhere?
if(PUs == 0)
return;
if(obj->attr->cache.size == 0)
{
//We will always have one child if PUs > 0
if(!isCacheObject(obj->children[0]))
throw(std::runtime_error("The CPU doesn't seem to have a cache."));
//Try our luck with lower level caches
for(size_t i=0; i < obj->arity; i++)
proccessTopLevelCache(obj->children[i]);
return;
}
size_t cacheSize = obj->attr->cache.size;
if(isCacheExclusive(obj))
{
for(size_t i=0; i < obj->arity; i++)
{
hwloc_obj_t l2obj = obj->children[i];
//If L2 is exclusive and greater or equal to 2MB add room for one more hash
if(isCacheObject(l2obj) && l2obj->attr != nullptr && l2obj->attr->cache.size >= hashMemSize)
cacheSize += hashMemSize;
}
}
std::vector<hwloc_obj_t> cores;
cores.reserve(16);
findChildrenByType(obj, HWLOC_OBJ_CORE, [&cores](hwloc_obj_t found) { cores.emplace_back(found); } );
size_t cacheHashes = (cacheSize + halfHashMemSize) / hashMemSize;
//Firstly allocate PU 0 of every CORE, then PU 1 etc.
size_t pu_id = 0;
while(cacheHashes > 0 && PUs > 0)
{
bool allocated_pu = false;
for(hwloc_obj_t core : cores)
{
if(core->arity <= pu_id || core->children[pu_id]->type != HWLOC_OBJ_PU)
continue;
size_t os_id = core->children[pu_id]->os_index;
if(cacheHashes > PUs)
{
cacheHashes -= 2;
os_id |= 0x8000000; //double hash marker bit
}
else
cacheHashes--;
PUs--;
allocated_pu = true;
results.emplace_back(os_id);
if(cacheHashes == 0)
break;
}
if(!allocated_pu)
throw(std::runtime_error("Failed to allocate a PU."));
pu_id++;
}
}
};
} // namespace cpu
} // namespace xmrstak
|