File: cpu_set.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (290 lines) | stat: -rw-r--r-- 8,143 bytes parent folder | download | duplicates (4)
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
#include "cpu_set.h"

#include <log/log.h>

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

#include <android-base/file.h>

#include "directory_reader.h"
#include "stdio_filebuf.h"
#include "task.h"
#include "unique_file.h"

using android::pdx::ErrorStatus;
using android::pdx::Status;

namespace {

constexpr int kDirectoryFlags = O_RDONLY | O_DIRECTORY | O_CLOEXEC;
constexpr pid_t kKernelThreadDaemonPid = 2;

}  // anonymous namespace

namespace android {
namespace dvr {

bool CpuSet::prefix_enabled_ = false;

void CpuSetManager::Load(const std::string& cpuset_root) {
  if (!root_set_)
    root_set_ = Create(cpuset_root);
}

std::unique_ptr<CpuSet> CpuSetManager::Create(const std::string& path) {
  base::unique_fd root_cpuset_fd(open(path.c_str(), kDirectoryFlags));
  if (root_cpuset_fd.get() < 0) {
    ALOGE("CpuSet::Create: Failed to open \"%s\": %s", path.c_str(),
          strerror(errno));
    return nullptr;
  }

  return Create(std::move(root_cpuset_fd), "/", nullptr);
}

std::unique_ptr<CpuSet> CpuSetManager::Create(base::unique_fd base_fd,
                                              const std::string& name,
                                              CpuSet* parent) {
  DirectoryReader directory(base::unique_fd(dup(base_fd)));
  if (!directory) {
    ALOGE("CpuSet::Create: Failed to opendir %s cpuset: %s", name.c_str(),
          strerror(directory.GetError()));
    return nullptr;
  }

  std::unique_ptr<CpuSet> group(
      new CpuSet(parent, name, base::unique_fd(dup(base_fd))));
  path_map_.insert(std::make_pair(group->path(), group.get()));

  while (dirent* entry = directory.Next()) {
    if (entry->d_type == DT_DIR) {
      std::string directory_name(entry->d_name);

      if (directory_name == "." || directory_name == "..")
        continue;

      base::unique_fd entry_fd(
          openat(base_fd.get(), directory_name.c_str(), kDirectoryFlags));
      if (entry_fd.get() >= 0) {
        auto child =
            Create(std::move(entry_fd), directory_name.c_str(), group.get());

        if (child)
          group->AddChild(std::move(child));
        else
          return nullptr;
      } else {
        ALOGE("CpuSet::Create: Failed to openat \"%s\": %s", entry->d_name,
              strerror(errno));
        return nullptr;
      }
    }
  }

  return group;
}

CpuSet* CpuSetManager::Lookup(const std::string& path) {
  auto search = path_map_.find(path);
  if (search != path_map_.end())
    return search->second;
  else
    return nullptr;
}

std::vector<CpuSet*> CpuSetManager::GetCpuSets() {
  std::vector<CpuSet*> sets(path_map_.size());

  for (const auto& pair : path_map_) {
    sets.push_back(pair.second);
  }

  return sets;
}

void CpuSetManager::DumpState(std::ostringstream& stream) const {
  size_t max_path = 0;
  std::vector<CpuSet*> sets;

  for (const auto& pair : path_map_) {
    max_path = std::max(max_path, pair.second->path().length());
    sets.push_back(pair.second);
  }

  std::sort(sets.begin(), sets.end(), [](const CpuSet* a, const CpuSet* b) {
    return a->path() < b->path();
  });

  stream << std::left;
  stream << std::setw(max_path) << "Path";
  stream << " ";
  stream << std::setw(6) << "CPUs";
  stream << " ";
  stream << std::setw(6) << "Tasks";
  stream << std::endl;

  stream << std::string(max_path, '_');
  stream << " ";
  stream << std::string(6, '_');
  stream << " ";
  stream << std::string(6, '_');
  stream << std::endl;

  for (const auto set : sets) {
    stream << std::left;
    stream << std::setw(max_path) << set->path();
    stream << " ";
    stream << std::right;
    stream << std::setw(6) << set->GetCpuList();
    stream << " ";
    stream << std::setw(6) << set->GetTasks().size();
    stream << std::endl;
  }
}

void CpuSetManager::MoveUnboundTasks(const std::string& target_set) {
  auto root = Lookup("/");
  if (!root) {
    ALOGE("CpuSetManager::MoveUnboundTasks: Failed to find root cpuset!");
    return;
  }

  auto target = Lookup(target_set);
  if (!target) {
    ALOGE(
        "CpuSetManager::MoveUnboundTasks: Failed to find target cpuset \"%s\"!",
        target_set.c_str());
    return;
  }

  auto cpu_list = root->GetCpuList();

  for (auto task_id : root->GetTasks()) {
    Task task(task_id);

    // Move only unbound kernel threads to the target cpuset.
    if (task.cpus_allowed_list() == cpu_list &&
        task.parent_process_id() == kKernelThreadDaemonPid) {
      ALOGD_IF(TRACE,
               "CpuSetManager::MoveUnboundTasks: Moving task_id=%d name=%s to "
               "target_set=%s tgid=%d ppid=%d.",
               task_id, task.name().c_str(), target_set.c_str(),
               task.thread_group_id(), task.parent_process_id());

      auto status = target->AttachTask(task_id);
      ALOGW_IF(!status && status.error() != EINVAL,
               "CpuSetManager::MoveUnboundTasks: Failed to attach task_id=%d "
               "to cpuset=%s: %s",
               task_id, target_set.c_str(), status.GetErrorMessage().c_str());
    } else {
      ALOGD_IF(TRACE,
               "CpuSet::MoveUnboundTasks: Skipping task_id=%d name=%s cpus=%s.",
               task_id, task.name().c_str(), task.cpus_allowed_list().c_str());
    }
  }
}

CpuSet::CpuSet(CpuSet* parent, const std::string& name,
               base::unique_fd&& cpuset_fd)
    : parent_(parent), name_(name), cpuset_fd_(std::move(cpuset_fd)) {
  if (parent_ == nullptr)
    path_ = name_;
  else if (parent_->IsRoot())
    path_ = parent_->name() + name_;
  else
    path_ = parent_->path() + "/" + name_;

  ALOGI("CpuSet::CpuSet: path=%s", path().c_str());
}

base::unique_fd CpuSet::OpenPropertyFile(const std::string& name) const {
  return OpenFile(prefix_enabled_ ? "cpuset." + name : name);
}

UniqueFile CpuSet::OpenPropertyFilePointer(const std::string& name) const {
  return OpenFilePointer(prefix_enabled_ ? "cpuset." + name : name);
}

base::unique_fd CpuSet::OpenFile(const std::string& name, int flags) const {
  const std::string relative_path = "./" + name;
  return base::unique_fd(
      openat(cpuset_fd_.get(), relative_path.c_str(), flags));
}

UniqueFile CpuSet::OpenFilePointer(const std::string& name, int flags) const {
  const std::string relative_path = "./" + name;
  base::unique_fd fd(openat(cpuset_fd_.get(), relative_path.c_str(), flags));
  if (fd.get() < 0) {
    ALOGE("CpuSet::OpenPropertyFilePointer: Failed to open %s/%s: %s",
          path_.c_str(), name.c_str(), strerror(errno));
    return nullptr;
  }

  UniqueFile fp(fdopen(fd.release(), "r"));
  if (!fp)
    ALOGE("CpuSet::OpenPropertyFilePointer: Failed to fdopen %s/%s: %s",
          path_.c_str(), name.c_str(), strerror(errno));

  return fp;
}

Status<void> CpuSet::AttachTask(pid_t task_id) const {
  auto file = OpenFile("tasks", O_RDWR);
  if (file.get() >= 0) {
    std::ostringstream stream;
    stream << task_id;
    std::string value = stream.str();

    const bool ret = base::WriteStringToFd(value, file.get());
    if (!ret)
      return ErrorStatus(errno);
    else
      return {};
  } else {
    const int error = errno;
    ALOGE("CpuSet::AttachTask: Failed to open %s/tasks: %s", path_.c_str(),
          strerror(error));
    return ErrorStatus(error);
  }
}

std::vector<pid_t> CpuSet::GetTasks() const {
  std::vector<pid_t> tasks;

  if (auto file = OpenFilePointer("tasks")) {
    stdio_filebuf<char> filebuf(file.get());
    std::istream file_stream(&filebuf);

    for (std::string line; std::getline(file_stream, line);) {
      pid_t task_id = std::strtol(line.c_str(), nullptr, 10);
      tasks.push_back(task_id);
    }
  }

  return tasks;
}

std::string CpuSet::GetCpuList() const {
  if (auto file = OpenPropertyFilePointer("cpus")) {
    stdio_filebuf<char> filebuf(file.get());
    std::istream file_stream(&filebuf);

    std::string line;
    if (std::getline(file_stream, line))
      return line;
  }

  ALOGE("CpuSet::GetCpuList: Failed to read cpu list!!!");
  return "";
}

void CpuSet::AddChild(std::unique_ptr<CpuSet> child) {
  children_.push_back(std::move(child));
}

}  // namespace dvr
}  // namespace android