11#include "llvm/ADT/StringExtras.h"
12#include "llvm/Support/Error.h"
13#include "llvm/Support/MemoryBuffer.h"
14#include "llvm/Support/Threading.h"
19using namespace process_linux;
23 static ErrorOr<std::unique_ptr<MemoryBuffer>> cpu_info_or_err =
26 if (!*cpu_info_or_err)
27 cpu_info_or_err.getError();
29 MemoryBuffer &buffer = **cpu_info_or_err;
30 return arrayRefFromStringRef(buffer.getBuffer());
33Expected<std::vector<cpu_id_t>>
35 SmallVector<StringRef, 8> lines;
36 cpuinfo.split(lines,
"\n", -1,
false);
37 std::vector<cpu_id_t> logical_cores;
39 for (StringRef line : lines) {
40 std::pair<StringRef, StringRef> key_value = line.split(
':');
41 auto key = key_value.first.trim();
42 auto val = key_value.second.trim();
43 if (key ==
"processor") {
45 if (val.getAsInteger(10, processor))
46 return createStringError(
47 inconvertibleErrorCode(),
48 "Failed parsing the /proc/cpuinfo line entry: %s", line.data());
49 logical_cores.push_back(processor);
55llvm::Expected<llvm::ArrayRef<cpu_id_t>>
57 static std::optional<std::vector<cpu_id_t>> logical_cores_ids;
58 if (!logical_cores_ids) {
62 return cpuinfo.takeError();
65 StringRef(
reinterpret_cast<const char *
>(cpuinfo->data())));
67 return cpu_ids.takeError();
69 logical_cores_ids.emplace(std::move(*cpu_ids));
71 return *logical_cores_ids;
75 ErrorOr<std::unique_ptr<MemoryBuffer>> ptrace_scope_file =
77 if (!*ptrace_scope_file)
78 return errorCodeToError(ptrace_scope_file.getError());
80 StringRef buffer = (*ptrace_scope_file)->getBuffer().trim();
81 int ptrace_scope_value;
82 if (buffer.getAsInteger(10, ptrace_scope_value)) {
83 return createStringError(inconvertibleErrorCode(),
84 "Invalid ptrace_scope value: '%s'", buffer.data());
86 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)