11#include "llvm/Support/Error.h"
12#include "llvm/Support/MemoryBuffer.h"
13#include "llvm/Support/Threading.h"
18using namespace process_linux;
22 static ErrorOr<std::unique_ptr<MemoryBuffer>> cpu_info_or_err =
25 if (!*cpu_info_or_err)
26 cpu_info_or_err.getError();
28 MemoryBuffer &buffer = **cpu_info_or_err;
29 return arrayRefFromStringRef(buffer.getBuffer());
32Expected<std::vector<cpu_id_t>>
34 SmallVector<StringRef, 8> lines;
35 cpuinfo.split(lines,
"\n", -1,
false);
36 std::vector<cpu_id_t> logical_cores;
38 for (StringRef line : lines) {
39 std::pair<StringRef, StringRef> key_value = line.split(
':');
40 auto key = key_value.first.trim();
41 auto val = key_value.second.trim();
42 if (key ==
"processor") {
44 if (val.getAsInteger(10, processor))
45 return createStringError(
46 inconvertibleErrorCode(),
47 "Failed parsing the /proc/cpuinfo line entry: %s", line.data());
48 logical_cores.push_back(processor);
54llvm::Expected<llvm::ArrayRef<cpu_id_t>>
56 static std::optional<std::vector<cpu_id_t>> logical_cores_ids;
57 if (!logical_cores_ids) {
61 return cpuinfo.takeError();
64 StringRef(
reinterpret_cast<const char *
>(cpuinfo->data())));
66 return cpu_ids.takeError();
68 logical_cores_ids.emplace(std::move(*cpu_ids));
70 return *logical_cores_ids;
74 ErrorOr<std::unique_ptr<MemoryBuffer>> ptrace_scope_file =
76 if (!*ptrace_scope_file)
77 return errorCodeToError(ptrace_scope_file.getError());
79 StringRef buffer = (*ptrace_scope_file)->getBuffer().trim();
80 int ptrace_scope_value;
81 if (buffer.getAsInteger(10, ptrace_scope_value)) {
82 return createStringError(inconvertibleErrorCode(),
83 "Invalid ptrace_scope value: '%s'", buffer.data());
85 return ptrace_scope_value;
llvm::Expected< int > GetPtraceScope()
llvm::Expected< llvm::ArrayRef< lldb::cpu_id_t > > GetAvailableLogicalCoreIDs()
llvm::Expected< llvm::ArrayRef< uint8_t > > GetProcfsCpuInfo()
A class that represents a running process on the host machine.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file)