-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathactivity.cc
377 lines (325 loc) · 12.4 KB
/
activity.cc
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "activity.h"
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <random>
#include <vector>
#include "absl/log/log.h"
#include "benchmark/benchmark.h"
#include "boost/preprocessor/repetition/repeat.hpp"
#include "distbench_utils.h"
namespace distbench {
class ConsumeCpu : public Activity {
public:
void DoActivity() override;
ActivityLog GetActivityLog() override;
void Initialize(ParsedActivityConfig* config, SimpleClock* clock) override;
static absl::Status ValidateConfig(const ActivityConfig& ac);
private:
std::vector<int> rand_array;
int iteration_count_ = 0;
int64_t optimization_preventing_num_ = 0;
};
class PolluteDataCache : public Activity {
public:
static absl::Status ValidateConfig(const ActivityConfig& ac);
void Initialize(ParsedActivityConfig* config, SimpleClock* clock) override;
void DoActivity() override;
ActivityLog GetActivityLog() override;
private:
int iteration_count_ = 0;
int array_reads_per_iteration_ = 0;
std::vector<int> data_array_;
int64_t optimization_preventing_num_ = 0;
std::uniform_int_distribution<> random_index_;
absl::BitGen bit_gen_;
};
class PolluteInstructionCache : public Activity {
public:
static absl::Status ValidateConfig(const ActivityConfig& ac);
void Initialize(ParsedActivityConfig* config, SimpleClock* clock) override;
void DoActivity() override;
ActivityLog GetActivityLog() override;
private:
int iteration_count_ = 0;
int function_invocations_per_iteration_ = 0;
absl::BitGen bit_gen_;
std::vector<int (*)(bool)> func_ptr_array_;
std::uniform_int_distribution<> random_index_;
};
class SleepFor : public Activity {
public:
static absl::Status ValidateConfig(const ActivityConfig& ac);
void Initialize(ParsedActivityConfig* config, SimpleClock* clock) override;
void DoActivity() override;
ActivityLog GetActivityLog() override;
private:
SimpleClock* clock_ = nullptr;
absl::Duration duration_;
};
class SpinFor : public Activity {
public:
static absl::Status ValidateConfig(const ActivityConfig& ac);
void Initialize(ParsedActivityConfig* config, SimpleClock* clock) override;
void DoActivity() override;
ActivityLog GetActivityLog() override;
private:
SimpleClock* clock_ = nullptr;
absl::Duration duration_;
};
absl::StatusOr<ParsedActivityConfig> ParseActivityConfig(
const ActivityConfig& ac) {
ParsedActivityConfig s;
s.activity_func =
GetNamedSettingString(ac.activity_settings(), "activity_func", "");
s.activity_config_name = ac.name();
if (s.activity_func == "ConsumeCpu") {
auto status = ConsumeCpu::ValidateConfig(ac);
if (!status.ok()) return status;
s.array_size =
GetNamedSettingInt64(ac.activity_settings(), "array_size", 1000);
} else if (s.activity_func == "PolluteDataCache") {
auto status = PolluteDataCache::ValidateConfig(ac);
if (!status.ok()) return status;
s.array_size =
GetNamedSettingInt64(ac.activity_settings(), "array_size", 2'000'000);
s.array_reads_per_iteration = GetNamedSettingInt64(
ac.activity_settings(), "array_reads_per_iteration", 1000);
} else if (s.activity_func == "PolluteInstructionCache") {
auto status = PolluteInstructionCache::ValidateConfig(ac);
if (!status.ok()) return status;
s.function_invocations_per_iteration = GetNamedSettingInt64(
ac.activity_settings(), "function_invocations_per_iteration", 1000);
} else if (s.activity_func == "SleepFor") {
auto status = SleepFor::ValidateConfig(ac);
if (!status.ok()) return status;
s.sleepfor_duration = absl::Microseconds(
GetNamedSettingInt64(ac.activity_settings(), "duration_us", 0));
} else if (s.activity_func == "SpinFor") {
auto status = SpinFor::ValidateConfig(ac);
if (!status.ok()) return status;
s.sleepfor_duration = absl::Microseconds(
GetNamedSettingInt64(ac.activity_settings(), "duration_us", 0));
} else {
return absl::FailedPreconditionError(absl::StrCat(
"Activity config '", s.activity_config_name,
"' has an unknown activity_func '", s.activity_func, "'."));
}
return s;
}
std::unique_ptr<Activity> AllocateActivity(ParsedActivityConfig* config,
SimpleClock* clock) {
std::unique_ptr<Activity> activity;
auto activity_func = config->activity_func;
if (activity_func == "ConsumeCpu") {
activity = std::make_unique<ConsumeCpu>();
} else if (activity_func == "PolluteDataCache") {
activity = std::make_unique<PolluteDataCache>();
} else if (activity_func == "PolluteInstructionCache") {
activity = std::make_unique<PolluteInstructionCache>();
} else if (activity_func == "SleepFor") {
activity = std::make_unique<SleepFor>();
} else if (activity_func == "SpinFor") {
activity = std::make_unique<SpinFor>();
}
activity->Initialize(config, clock);
return activity;
}
void SleepFor::DoActivity() { clock_->SleepFor(duration_); }
ActivityLog SleepFor::GetActivityLog() { return {}; }
void SleepFor::Initialize(ParsedActivityConfig* config, SimpleClock* clock) {
clock_ = clock;
duration_ = config->sleepfor_duration;
}
absl::Status SleepFor::ValidateConfig(const ActivityConfig& ac) {
auto duration_us =
GetNamedSettingInt64(ac.activity_settings(), "duration_us", -1);
if (duration_us < 1) {
return absl::InvalidArgumentError(absl::StrCat(
"duration_us ", duration_us, ") must be a positive integer."));
}
return absl::OkStatus();
}
void SpinFor::DoActivity() { clock_->SpinFor(duration_); }
ActivityLog SpinFor::GetActivityLog() { return {}; }
void SpinFor::Initialize(ParsedActivityConfig* config, SimpleClock* clock) {
clock_ = clock;
duration_ = config->sleepfor_duration;
}
absl::Status SpinFor::ValidateConfig(const ActivityConfig& ac) {
auto duration_us =
GetNamedSettingInt64(ac.activity_settings(), "duration_us", -1);
if (duration_us < 1) {
return absl::InvalidArgumentError(absl::StrCat(
"duration_us ", duration_us, ") must be a positive integer."));
}
return absl::OkStatus();
}
void ConsumeCpu::DoActivity() {
iteration_count_++;
unsigned int sum = 0;
std::srand(time(0));
std::generate(rand_array.begin(), rand_array.end(), std::rand);
std::sort(rand_array.begin(), rand_array.end());
for (auto num : rand_array) sum += num;
optimization_preventing_num_ = sum;
}
ActivityLog ConsumeCpu::GetActivityLog() {
ActivityLog alog;
if (iteration_count_) {
auto* am = alog.add_activity_metrics();
am->set_name("iteration_count");
am->set_value_int(iteration_count_);
}
return alog;
}
void ConsumeCpu::Initialize(ParsedActivityConfig* config, SimpleClock* clock) {
rand_array.resize(config->array_size);
iteration_count_ = 0;
}
absl::Status ConsumeCpu::ValidateConfig(const ActivityConfig& ac) {
auto array_size =
GetNamedSettingInt64(ac.activity_settings(), "array_size", 1000);
if (array_size < 1) {
return absl::InvalidArgumentError(absl::StrCat(
"array_size (", array_size, ") must be a positive integer."));
}
return absl::OkStatus();
}
absl::Status PolluteDataCache::ValidateConfig(const ActivityConfig& ac) {
auto array_size =
GetNamedSettingInt64(ac.activity_settings(), "array_size", 1000);
if (array_size < 1) {
return absl::InvalidArgumentError(absl::StrCat(
"array_size (", array_size, ") must be a positive integer."));
}
auto array_reads_per_iteration = GetNamedSettingInt64(
ac.activity_settings(), "array_reads_per_iteration", 1000);
if (array_reads_per_iteration < 1) {
return absl::InvalidArgumentError(
absl::StrCat("array_reads_per_iteration (", array_reads_per_iteration,
") must be a positive integer."));
}
return absl::OkStatus();
}
void PolluteDataCache::Initialize(ParsedActivityConfig* config,
SimpleClock* clock) {
std::srand(time(0));
auto array_size = config->array_size;
data_array_.resize(array_size);
for (int i = 0; i < array_size; i++) {
data_array_[i] = i;
}
random_index_ = std::uniform_int_distribution<>(0, array_size - 1);
array_reads_per_iteration_ = config->array_reads_per_iteration;
iteration_count_ = 0;
}
void PolluteDataCache::DoActivity() {
iteration_count_++;
int64_t sum = 0;
for (int i = 0; i < array_reads_per_iteration_; i++) {
int index = random_index_(bit_gen_);
// This is read and write operation.
sum += data_array_[index]++;
}
optimization_preventing_num_ = sum;
}
ActivityLog PolluteDataCache::GetActivityLog() {
ActivityLog alog;
if (iteration_count_) {
auto* am = alog.add_activity_metrics();
am->set_name("iteration_count");
am->set_value_int(iteration_count_);
}
return alog;
}
// The boost library (https://github.com/nelhage/rules_boost) is used to
// create a 2D array of functions with names -
//
// DummyFunc_0_0, DummyFunc_0_1, ..., DummyFunc_0_{POLLUTE_ICACHE_LOOP_SIZE-1},
// ...
// ...
// ...
// DummyFunc_{POLLUTE_ICACHE_LOOP_SIZE-1}_0, ...,
// DummyFunc_{POLLUTE_ICACHE_LOOP_SIZE-1}_{POLLUTE_ICACHE_LOOP_SIZE-1}. Each of
// these functions returns an int.
//
// We create this 2D array because BOOST_PP_REPEAT() macro can
// generate at most 255 repetitions.
// The purpose of 'do_work' part of the function is only to increase
// the size of the function.
#define POLLUTE_ICACHE_LOOP_SIZE 100
#define GENERATE_FUNC_INNER_LOOP(z, n, prefix) \
int DummyFunc_##prefix##_##n(bool do_work) { \
if (do_work) { \
int result = 0; \
for (int i = 0; i < n; i++) { \
result += i * i; \
result /= i + 1; \
result -= i / 2; \
result /= i * i; \
result += result * i; \
result /= result + 1; \
result -= result / 2; \
result *= result; \
} \
return result; \
} else { \
return n; \
} \
}
#define GENERATE_FUNC_OUTER_LOOP(z, n, text) \
BOOST_PP_REPEAT(POLLUTE_ICACHE_LOOP_SIZE, GENERATE_FUNC_INNER_LOOP, n)
// Get the pointers to the functions created above and store it in
// func_ptr_array.
#define GET_FUNC_PTR_INNER_LOOP(z, n, prefix) \
func_ptr_array_[(prefix * POLLUTE_ICACHE_LOOP_SIZE + n)] = \
&DummyFunc_##prefix##_##n;
#define GET_FUNC_PTR_OUTER_LOOP(z, n, text) \
BOOST_PP_REPEAT(POLLUTE_ICACHE_LOOP_SIZE, GET_FUNC_PTR_INNER_LOOP, n)
// Generate the functions for instruction cache miss.
BOOST_PP_REPEAT(POLLUTE_ICACHE_LOOP_SIZE, GENERATE_FUNC_OUTER_LOOP, );
absl::Status PolluteInstructionCache::ValidateConfig(const ActivityConfig& ac) {
return absl::OkStatus();
}
void PolluteInstructionCache::Initialize(ParsedActivityConfig* config,
SimpleClock* clock) {
auto func_array_size = POLLUTE_ICACHE_LOOP_SIZE * POLLUTE_ICACHE_LOOP_SIZE;
func_ptr_array_.resize(func_array_size);
BOOST_PP_REPEAT(POLLUTE_ICACHE_LOOP_SIZE, GET_FUNC_PTR_OUTER_LOOP, );
function_invocations_per_iteration_ =
config->function_invocations_per_iteration;
random_index_ = std::uniform_int_distribution<>(0, func_array_size - 1);
iteration_count_ = 0;
}
void PolluteInstructionCache::DoActivity() {
iteration_count_++;
int64_t sum = 0;
for (int i = 0; i < function_invocations_per_iteration_; i++) {
int index = random_index_(bit_gen_);
benchmark::DoNotOptimize(sum += func_ptr_array_[index](false));
}
}
ActivityLog PolluteInstructionCache::GetActivityLog() {
ActivityLog alog;
if (iteration_count_) {
auto* am = alog.add_activity_metrics();
am->set_name("iteration_count");
am->set_value_int(iteration_count_);
}
return alog;
}
} // namespace distbench