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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// 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
//
// http://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.
#ifndef INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_RESULT_MAP_HPP
#define INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_RESULT_MAP_HPP
#include <algorithm>
#include <compare>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iterator>
#include <memory>
#include <mutex>
#include <numeric>
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility> // std::move
#include <vector>
#include "gsl/gsl"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/analysed_target/analysed_target.hpp"
#include "src/buildtool/build_engine/analysed_target/target_graph_information.hpp"
#include "src/buildtool/build_engine/base_maps/entity_name_data.hpp"
#include "src/buildtool/build_engine/expression/configuration.hpp"
#include "src/buildtool/build_engine/target_map/configured_target.hpp"
#include "src/buildtool/common/action_description.hpp"
#include "src/buildtool/common/identifier.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/common/tree.hpp"
#include "src/buildtool/common/tree_overlay.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/multithreading/task_system.hpp"
#include "src/buildtool/progress_reporting/progress.hpp"
#include "src/buildtool/storage/target_cache_key.hpp"
namespace BuildMaps::Target {
// Class collecting analysed targets for their canonical configuration.
class ResultTargetMap {
public:
struct ActionWithOrigin {
ActionDescription::Ptr desc;
nlohmann::json origin;
};
template <bool kIncludeOrigins = false>
struct ResultType {
std::vector<ActionDescription::Ptr> actions;
std::vector<std::string> blobs;
std::vector<Tree::Ptr> trees;
std::vector<TreeOverlay::Ptr> tree_overlays;
};
explicit ResultTargetMap(std::size_t jobs) : width_{ComputeWidth(jobs)} {}
ResultTargetMap() = default;
// \brief Add the analysed target for the given target and
// configuration, if no entry is present for the given
// target-configuration pair. \returns the analysed target that is
// element of the map after insertion.
[[nodiscard]] auto Add(
BuildMaps::Base::EntityName name,
Configuration conf,
gsl::not_null<AnalysedTargetPtr> const& result,
std::optional<TargetCacheKey> target_cache_key = std::nullopt,
bool is_export_target = false) -> AnalysedTargetPtr {
auto part = std::hash<BuildMaps::Base::EntityName>{}(name) % width_;
std::unique_lock lock{m_[part]};
auto [entry, inserted] =
targets_[part].emplace(ConfiguredTarget{.target = std::move(name),
.config = std::move(conf)},
result);
if (target_cache_key) {
cache_targets_[part].emplace(*target_cache_key, entry->second);
}
if (is_export_target) {
export_targets_[part].emplace(entry->first);
}
if (inserted) {
num_actions_[part] += entry->second->Actions().size();
num_blobs_[part] += entry->second->Blobs().size();
num_trees_[part] += entry->second->Trees().size();
num_tree_overlays_[part] += entry->second->TreeOverlays().size();
}
return entry->second;
}
[[nodiscard]] auto ConfiguredTargets() const noexcept
-> std::vector<ConfiguredTarget> {
std::vector<ConfiguredTarget> targets{};
std::size_t s = 0;
for (const auto& target : targets_) {
s += target.size();
}
targets.reserve(s);
for (const auto& i : targets_) {
std::transform(i.begin(),
i.end(),
std::back_inserter(targets),
[](auto const& target) { return target.first; });
}
std::sort(targets.begin(),
targets.end(),
[](auto const& lhs, auto const& rhs) {
return lhs.ToString() < rhs.ToString();
});
return targets;
}
[[nodiscard]] auto ExportTargets() const noexcept
-> std::vector<ConfiguredTarget> {
std::vector<ConfiguredTarget> all_exports{};
for (auto const& exports : export_targets_) {
all_exports.insert(
all_exports.end(), exports.begin(), exports.end());
}
std::sort(all_exports.begin(),
all_exports.end(),
[](auto const& lhs, auto const& rhs) {
return lhs.ToString() < rhs.ToString();
});
return all_exports;
}
[[nodiscard]] auto ConfiguredTargetsGraph() const -> nlohmann::json {
auto result = nlohmann::json::object();
for (auto const& i : targets_) {
for (auto const& it : i) {
auto const& info = it.second->GraphInformation();
auto node = info.NodeString();
if (node) {
result[*node] = info.DepsToJson();
}
}
}
return result;
}
[[nodiscard]] auto CacheTargets() const noexcept
-> std::unordered_map<TargetCacheKey, AnalysedTargetPtr> {
return std::accumulate(
cache_targets_.begin(),
cache_targets_.end(),
std::unordered_map<TargetCacheKey, AnalysedTargetPtr>{},
[](auto&& l, auto const& r) {
l.insert(r.begin(), r.end());
return std::forward<decltype(l)>(l);
});
}
[[nodiscard]] auto GetAction(const ActionIdentifier& identifier)
-> std::optional<ActionDescription::Ptr> {
for (const auto& target : targets_) {
for (const auto& el : target) {
for (const auto& action : el.second->Actions()) {
if (action->Id() == identifier) {
return action;
}
}
}
}
return std::nullopt;
}
template <bool kIncludeOrigins = false>
[[nodiscard]] auto ToResult(gsl::not_null<Statistics const*> const& stats,
gsl::not_null<Progress*> const& progress,
Logger const* logger = nullptr) const
-> ResultType<kIncludeOrigins> {
ResultType<kIncludeOrigins> result{};
std::size_t na = 0;
std::size_t nb = 0;
std::size_t nt = 0;
std::size_t nto = 0;
for (std::size_t i = 0; i < width_; i++) {
na += num_actions_[i];
nb += num_blobs_[i];
nt += num_trees_[i];
nto += num_tree_overlays_[i];
}
result.actions.reserve(na);
result.blobs.reserve(nb);
result.trees.reserve(nt);
result.tree_overlays.reserve(nto);
auto& origin_map = progress->OriginMap();
origin_map.clear();
origin_map.reserve(na + nto);
for (const auto& target : targets_) {
std::for_each(target.begin(), target.end(), [&](auto const& el) {
auto const& actions = el.second->Actions();
std::size_t pos{};
std::for_each(
actions.begin(),
actions.end(),
[&origin_map, &pos, &el](auto const& action) {
std::pair<ConfiguredTarget, std::size_t> origin{
el.first, pos++};
auto id = action->Id();
if (origin_map.contains(id)) {
origin_map[id].push_back(origin);
}
else {
origin_map[id] = std::vector<
std::pair<ConfiguredTarget, std::size_t>>{
origin};
}
});
auto const& tree_overlays = el.second->TreeOverlays();
std::for_each(
tree_overlays.begin(),
tree_overlays.end(),
[&origin_map, &pos, &el](auto const& overlay) {
std::pair<ConfiguredTarget, std::size_t> origin{
el.first, pos++};
auto id = overlay->Id();
if (origin_map.contains(id)) {
origin_map[id].push_back(origin);
}
else {
origin_map[id] = std::vector<
std::pair<ConfiguredTarget, std::size_t>>{
origin};
}
});
});
// Sort origins to get a reproducible order. We don't expect many
// origins for a single action, so the cost of comparison is not
// too important. Moreover, we expect most actions to have a single
// origin, so any precomputation would be more expensive.
for (auto const& i : origin_map) {
std::sort(origin_map[i.first].begin(),
origin_map[i.first].end(),
[](auto const& left, auto const& right) {
auto const& left_target = left.first;
auto const& right_target = right.first;
return (left_target < right_target) or
(left_target == right_target and
left.second < right.second);
});
}
}
for (const auto& target : targets_) {
std::for_each(target.begin(), target.end(), [&](auto const& el) {
auto const& actions = el.second->Actions();
if constexpr (kIncludeOrigins) {
std::for_each(
actions.begin(),
actions.end(),
[&result, &origin_map](auto const& action) {
auto origins = nlohmann::json::array();
for (auto const& [ct, count] :
origin_map[action->Id()]) {
origins.push_back(nlohmann::json{
{"target", ct.target.ToJson()},
{"subtask", count},
{"config", ct.config.ToJson()}});
}
result.actions.emplace_back(ActionWithOrigin{
.desc = action, .origin = origins});
});
}
else {
std::for_each(actions.begin(),
actions.end(),
[&result](auto const& action) {
result.actions.emplace_back(action);
});
}
auto const& blobs = el.second->Blobs();
auto const& trees = el.second->Trees();
auto const& tree_overlays = el.second->TreeOverlays();
result.blobs.insert(
result.blobs.end(), blobs.begin(), blobs.end());
result.trees.insert(
result.trees.end(), trees.begin(), trees.end());
result.tree_overlays.insert(result.tree_overlays.end(),
tree_overlays.begin(),
tree_overlays.end());
});
}
std::sort(result.blobs.begin(), result.blobs.end());
auto lastblob = std::unique(result.blobs.begin(), result.blobs.end());
result.blobs.erase(lastblob, result.blobs.end());
std::sort(
result.trees.begin(),
result.trees.end(),
[](auto left, auto right) { return left->Id() < right->Id(); });
auto lasttree = std::unique(
result.trees.begin(),
result.trees.end(),
[](auto left, auto right) { return left->Id() == right->Id(); });
result.trees.erase(lasttree, result.trees.end());
std::sort(
result.tree_overlays.begin(),
result.tree_overlays.end(),
[](auto left, auto right) { return left->Id() < right->Id(); });
auto lasttree_overlay = std::unique(
result.tree_overlays.begin(),
result.tree_overlays.end(),
[](auto left, auto right) { return left->Id() == right->Id(); });
result.tree_overlays.erase(lasttree_overlay,
result.tree_overlays.end());
std::sort(result.actions.begin(),
result.actions.end(),
[](auto left, auto right) {
if constexpr (kIncludeOrigins) {
return left.desc->Id() < right.desc->Id();
}
else {
return left->Id() < right->Id();
}
});
auto lastaction =
std::unique(result.actions.begin(),
result.actions.end(),
[](auto left, auto right) {
if constexpr (kIncludeOrigins) {
return left.desc->Id() == right.desc->Id();
}
else {
return left->Id() == right->Id();
}
});
result.actions.erase(lastaction, result.actions.end());
auto& output_map = progress->OutputMap();
output_map.clear();
output_map.reserve(na);
if constexpr (kIncludeOrigins) {
for (auto const& action : result.actions) {
if (not action.desc->OutputFiles().empty()) {
output_map[action.desc->Id()] =
action.desc->OutputFiles()[0];
}
else if (not action.desc->OutputDirs().empty()) {
output_map[action.desc->Id()] =
action.desc->OutputDirs()[0];
}
}
}
else {
for (auto const& action : result.actions) {
if (not action->OutputFiles().empty()) {
output_map[action->Id()] = action->OutputFiles()[0];
}
else if (not action->OutputDirs().empty()) {
output_map[action->Id()] = action->OutputDirs()[0];
}
}
}
int trees_traversed = stats->TreesAnalysedCounter();
if (trees_traversed > 0) {
Logger::Log(logger,
LogLevel::Performance,
"Analysed {} non-known source trees",
trees_traversed);
}
Logger::Log(
logger,
LogLevel::Info,
"Discovered {} actions, {} tree overlays, {} trees, {} blobs",
result.actions.size(),
result.tree_overlays.size(),
result.trees.size(),
result.blobs.size());
return result;
}
template <bool kIncludeOrigins = false>
[[nodiscard]] auto ToJson(gsl::not_null<Statistics const*> const& stats,
gsl::not_null<Progress*> const& progress,
Logger const* logger = nullptr) const
-> nlohmann::json {
auto const result = ToResult<kIncludeOrigins>(stats, progress, logger);
auto actions = nlohmann::json::object();
auto trees = nlohmann::json::object();
auto tree_overlays = nlohmann::json::object();
std::for_each(result.actions.begin(),
result.actions.end(),
[&actions](auto const& action) {
if constexpr (kIncludeOrigins) {
auto const& id = action.desc->GraphAction().Id();
actions[id] = action.desc->ToJson();
actions[id]["origins"] = action.origin;
}
else {
auto const& id = action->GraphAction().Id();
actions[id] = action->ToJson();
}
});
std::for_each(
result.trees.begin(),
result.trees.end(),
[&trees](auto const& tree) { trees[tree->Id()] = tree->ToJson(); });
std::for_each(result.tree_overlays.begin(),
result.tree_overlays.end(),
[&tree_overlays](auto const& tree_overlay) {
tree_overlays[tree_overlay->Id()] =
tree_overlay->ToJson();
});
auto json = nlohmann::json{
{"actions", actions}, {"blobs", result.blobs}, {"trees", trees}};
if (not tree_overlays.empty()) {
json["tree_overlays"] = tree_overlays;
}
return json;
}
template <bool kIncludeOrigins = true>
auto ToFile(std::vector<std::filesystem::path> const& destinations,
gsl::not_null<Statistics const*> const& stats,
gsl::not_null<Progress*> const& progress,
int indent = 2) const -> void {
auto logger = Logger("result-dumping");
logger.SetLogLimit(LogLevel::Error);
if (not destinations.empty()) {
// As serialization is expensive, compute the string only once
auto data =
ToJson<kIncludeOrigins>(stats, progress, &logger).dump(indent);
for (auto const& graph_file : destinations) {
Logger::Log(LogLevel::Info,
"Dumping action graph to file {}.",
graph_file.string());
std::ofstream os(graph_file);
os << data << std::endl;
}
}
}
void Clear(gsl::not_null<TaskSystem*> const& ts) {
for (std::size_t i = 0; i < width_; ++i) {
ts->QueueTask([i, this]() {
targets_[i].clear();
cache_targets_[i].clear();
});
}
}
private:
constexpr static std::size_t kScalingFactor = 2;
std::size_t width_{ComputeWidth(0)};
std::vector<std::mutex> m_{width_};
std::vector<
std::unordered_map<ConfiguredTarget, gsl::not_null<AnalysedTargetPtr>>>
targets_{width_};
std::vector<
std::unordered_map<TargetCacheKey, gsl::not_null<AnalysedTargetPtr>>>
cache_targets_{width_};
std::vector<std::unordered_set<ConfiguredTarget>> export_targets_{width_};
std::vector<std::size_t> num_actions_{std::vector<std::size_t>(width_)};
std::vector<std::size_t> num_blobs_{std::vector<std::size_t>(width_)};
std::vector<std::size_t> num_trees_{std::vector<std::size_t>(width_)};
std::vector<std::size_t> num_tree_overlays_{
std::vector<std::size_t>(width_)};
constexpr static auto ComputeWidth(std::size_t jobs) -> std::size_t {
if (jobs <= 0) {
// Non-positive indicates to use the default value
return ComputeWidth(
std::max(1U, std::thread::hardware_concurrency()));
}
return jobs * kScalingFactor + 1;
}
}; // namespace BuildMaps::Target
template <>
struct ResultTargetMap::ResultType</*kIncludeOrigins=*/true> {
std::vector<ActionWithOrigin> actions;
std::vector<std::string> blobs;
std::vector<Tree::Ptr> trees;
std::vector<TreeOverlay::Ptr> tree_overlays;
};
} // namespace BuildMaps::Target
#endif // INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_TARGET_MAP_RESULT_MAP_HPP
|