libprocessgroup: use libcgrouprc to read cgroup.rc am: 53e0deb7b2
am: c2cb4aa768
Change-Id: Icb28519d3ed68751de8bad36611ab090b276a4bb
This commit is contained in:
commit
30559a8a00
9 changed files with 104 additions and 144 deletions
|
@ -235,6 +235,7 @@ test_libraries = [
|
|||
"libbase",
|
||||
"libjsoncpp",
|
||||
"libprocessgroup",
|
||||
"libcgrouprc",
|
||||
]
|
||||
|
||||
cc_test {
|
||||
|
@ -249,7 +250,10 @@ cc_test {
|
|||
name: "libcutils_test_static",
|
||||
test_suites: ["device-tests"],
|
||||
defaults: ["libcutils_test_default"],
|
||||
static_libs: ["libc"] + test_libraries,
|
||||
static_libs: [
|
||||
"libc",
|
||||
"libcgrouprc_format",
|
||||
] + test_libraries,
|
||||
stl: "libc++_static",
|
||||
|
||||
target: {
|
||||
|
|
|
@ -31,6 +31,7 @@ cc_library {
|
|||
},
|
||||
shared_libs: [
|
||||
"libbase",
|
||||
"libcgrouprc",
|
||||
"libjsoncpp",
|
||||
],
|
||||
// for cutils/android_filesystem_config.h
|
||||
|
|
|
@ -48,28 +48,38 @@ static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
|
|||
static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
|
||||
static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.tasks";
|
||||
|
||||
CgroupController::CgroupController(uint32_t version, const std::string& name,
|
||||
const std::string& path) {
|
||||
version_ = version;
|
||||
strncpy(name_, name.c_str(), sizeof(name_) - 1);
|
||||
name_[sizeof(name_) - 1] = '\0';
|
||||
strncpy(path_, path.c_str(), sizeof(path_) - 1);
|
||||
path_[sizeof(path_) - 1] = '\0';
|
||||
uint32_t CgroupController::version() const {
|
||||
CHECK(HasValue());
|
||||
return ACgroupController_getVersion(controller_);
|
||||
}
|
||||
|
||||
std::string CgroupController::GetTasksFilePath(const std::string& path) const {
|
||||
std::string tasks_path = path_;
|
||||
const char* CgroupController::name() const {
|
||||
CHECK(HasValue());
|
||||
return ACgroupController_getName(controller_);
|
||||
}
|
||||
|
||||
if (!path.empty()) {
|
||||
tasks_path += "/" + path;
|
||||
const char* CgroupController::path() const {
|
||||
CHECK(HasValue());
|
||||
return ACgroupController_getPath(controller_);
|
||||
}
|
||||
|
||||
bool CgroupController::HasValue() const {
|
||||
return controller_ != nullptr;
|
||||
}
|
||||
|
||||
std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const {
|
||||
std::string tasks_path = path();
|
||||
|
||||
if (!rel_path.empty()) {
|
||||
tasks_path += "/" + rel_path;
|
||||
}
|
||||
return (version_ == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
|
||||
return (version() == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
|
||||
}
|
||||
|
||||
std::string CgroupController::GetProcsFilePath(const std::string& path, uid_t uid,
|
||||
std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid,
|
||||
pid_t pid) const {
|
||||
std::string proc_path(path_);
|
||||
proc_path.append("/").append(path);
|
||||
std::string proc_path(path());
|
||||
proc_path.append("/").append(rel_path);
|
||||
proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid));
|
||||
proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid));
|
||||
|
||||
|
@ -90,7 +100,7 @@ bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
|
|||
return true;
|
||||
}
|
||||
|
||||
std::string cg_tag = StringPrintf(":%s:", name_);
|
||||
std::string cg_tag = StringPrintf(":%s:", name());
|
||||
size_t start_pos = content.find(cg_tag);
|
||||
if (start_pos == std::string::npos) {
|
||||
return false;
|
||||
|
@ -107,20 +117,12 @@ bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
|
|||
return true;
|
||||
}
|
||||
|
||||
CgroupMap::CgroupMap() : cg_file_data_(nullptr), cg_file_size_(0) {
|
||||
CgroupMap::CgroupMap() {
|
||||
if (!LoadRcFile()) {
|
||||
LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed";
|
||||
}
|
||||
}
|
||||
|
||||
CgroupMap::~CgroupMap() {
|
||||
if (cg_file_data_) {
|
||||
munmap(cg_file_data_, cg_file_size_);
|
||||
cg_file_data_ = nullptr;
|
||||
cg_file_size_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
CgroupMap& CgroupMap::GetInstance() {
|
||||
// Deliberately leak this object to avoid a race between destruction on
|
||||
// process exit and concurrent access from another thread.
|
||||
|
@ -129,85 +131,46 @@ CgroupMap& CgroupMap::GetInstance() {
|
|||
}
|
||||
|
||||
bool CgroupMap::LoadRcFile() {
|
||||
struct stat sb;
|
||||
|
||||
if (cg_file_data_) {
|
||||
// Data already initialized
|
||||
return true;
|
||||
if (!loaded_) {
|
||||
loaded_ = (ACgroupFile_getVersion() != 0);
|
||||
}
|
||||
|
||||
unique_fd fd(TEMP_FAILURE_RETRY(open(CGROUPS_RC_PATH, O_RDONLY | O_CLOEXEC)));
|
||||
if (fd < 0) {
|
||||
PLOG(ERROR) << "open() failed for " << CGROUPS_RC_PATH;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fstat(fd, &sb) < 0) {
|
||||
PLOG(ERROR) << "fstat() failed for " << CGROUPS_RC_PATH;
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t file_size = sb.st_size;
|
||||
if (file_size < sizeof(CgroupFile)) {
|
||||
LOG(ERROR) << "Invalid file format " << CGROUPS_RC_PATH;
|
||||
return false;
|
||||
}
|
||||
|
||||
CgroupFile* file_data = (CgroupFile*)mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (file_data == MAP_FAILED) {
|
||||
PLOG(ERROR) << "Failed to mmap " << CGROUPS_RC_PATH;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (file_data->version_ != CgroupFile::FILE_CURR_VERSION) {
|
||||
LOG(ERROR) << CGROUPS_RC_PATH << " file version mismatch";
|
||||
munmap(file_data, file_size);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (file_size != sizeof(CgroupFile) + file_data->controller_count_ * sizeof(CgroupController)) {
|
||||
LOG(ERROR) << CGROUPS_RC_PATH << " file has invalid size";
|
||||
munmap(file_data, file_size);
|
||||
return false;
|
||||
}
|
||||
|
||||
cg_file_data_ = file_data;
|
||||
cg_file_size_ = file_size;
|
||||
|
||||
return true;
|
||||
return loaded_;
|
||||
}
|
||||
|
||||
void CgroupMap::Print() const {
|
||||
if (!cg_file_data_) {
|
||||
if (!loaded_) {
|
||||
LOG(ERROR) << "CgroupMap::Print called for [" << getpid()
|
||||
<< "] failed, RC file was not initialized properly";
|
||||
return;
|
||||
}
|
||||
LOG(INFO) << "File version = " << cg_file_data_->version_;
|
||||
LOG(INFO) << "File controller count = " << cg_file_data_->controller_count_;
|
||||
LOG(INFO) << "File version = " << ACgroupFile_getVersion();
|
||||
LOG(INFO) << "File controller count = " << ACgroupFile_getControllerCount();
|
||||
|
||||
LOG(INFO) << "Mounted cgroups:";
|
||||
CgroupController* controller = (CgroupController*)(cg_file_data_ + 1);
|
||||
for (int i = 0; i < cg_file_data_->controller_count_; i++, controller++) {
|
||||
LOG(INFO) << "\t" << controller->name() << " ver " << controller->version() << " path "
|
||||
<< controller->path();
|
||||
|
||||
auto controller_count = ACgroupFile_getControllerCount();
|
||||
for (uint32_t i = 0; i < controller_count; ++i) {
|
||||
const ACgroupController* controller = ACgroupFile_getController(i);
|
||||
LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
|
||||
<< ACgroupController_getVersion(controller) << " path "
|
||||
<< ACgroupController_getPath(controller);
|
||||
}
|
||||
}
|
||||
|
||||
const CgroupController* CgroupMap::FindController(const std::string& name) const {
|
||||
if (!cg_file_data_) {
|
||||
CgroupController CgroupMap::FindController(const std::string& name) const {
|
||||
if (!loaded_) {
|
||||
LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
|
||||
<< "] failed, RC file was not initialized properly";
|
||||
return nullptr;
|
||||
return CgroupController(nullptr);
|
||||
}
|
||||
|
||||
// skip the file header to get to the first controller
|
||||
CgroupController* controller = (CgroupController*)(cg_file_data_ + 1);
|
||||
for (int i = 0; i < cg_file_data_->controller_count_; i++, controller++) {
|
||||
if (name == controller->name()) {
|
||||
return controller;
|
||||
auto controller_count = ACgroupFile_getControllerCount();
|
||||
for (uint32_t i = 0; i < controller_count; ++i) {
|
||||
const ACgroupController* controller = ACgroupFile_getController(i);
|
||||
if (name == ACgroupController_getName(controller)) {
|
||||
return CgroupController(controller);
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return CgroupController(nullptr);
|
||||
}
|
||||
|
|
|
@ -20,39 +20,30 @@
|
|||
#include <sys/types.h>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Minimal controller description to be mmapped into process address space
|
||||
#include <android/cgrouprc.h>
|
||||
|
||||
// Convenient wrapper of an ACgroupController pointer.
|
||||
class CgroupController {
|
||||
public:
|
||||
CgroupController() {}
|
||||
CgroupController(uint32_t version, const std::string& name, const std::string& path);
|
||||
// Does not own controller
|
||||
explicit CgroupController(const ACgroupController* controller) : controller_(controller) {}
|
||||
|
||||
uint32_t version() const { return version_; }
|
||||
const char* name() const { return name_; }
|
||||
const char* path() const { return path_; }
|
||||
uint32_t version() const;
|
||||
const char* name() const;
|
||||
const char* path() const;
|
||||
|
||||
bool HasValue() const;
|
||||
|
||||
std::string GetTasksFilePath(const std::string& path) const;
|
||||
std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const;
|
||||
bool GetTaskGroup(int tid, std::string* group) const;
|
||||
|
||||
private:
|
||||
static constexpr size_t CGROUP_NAME_BUF_SZ = 16;
|
||||
static constexpr size_t CGROUP_PATH_BUF_SZ = 32;
|
||||
|
||||
uint32_t version_;
|
||||
char name_[CGROUP_NAME_BUF_SZ];
|
||||
char path_[CGROUP_PATH_BUF_SZ];
|
||||
};
|
||||
|
||||
struct CgroupFile {
|
||||
static constexpr uint32_t FILE_VERSION_1 = 1;
|
||||
static constexpr uint32_t FILE_CURR_VERSION = FILE_VERSION_1;
|
||||
|
||||
uint32_t version_;
|
||||
uint32_t controller_count_;
|
||||
CgroupController controllers_[];
|
||||
const ACgroupController* controller_ = nullptr;
|
||||
};
|
||||
|
||||
class CgroupMap {
|
||||
|
@ -61,16 +52,11 @@ class CgroupMap {
|
|||
static bool SetupCgroups();
|
||||
|
||||
static CgroupMap& GetInstance();
|
||||
|
||||
const CgroupController* FindController(const std::string& name) const;
|
||||
CgroupController FindController(const std::string& name) const;
|
||||
|
||||
private:
|
||||
struct CgroupFile* cg_file_data_;
|
||||
size_t cg_file_size_;
|
||||
|
||||
bool loaded_ = false;
|
||||
CgroupMap();
|
||||
~CgroupMap();
|
||||
|
||||
bool LoadRcFile();
|
||||
void Print() const;
|
||||
};
|
||||
|
|
|
@ -12,10 +12,15 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
cc_library_shared {
|
||||
cc_library {
|
||||
name: "libcgrouprc",
|
||||
host_supported: true,
|
||||
recovery_available: true,
|
||||
// Do not ever mark this as vendor_available; otherwise, vendor modules
|
||||
// that links to the static library will behave unexpectedly. All on-device
|
||||
// modules should use libprocessgroup which links to the LL-NDK library
|
||||
// defined below. The static library is built for tests.
|
||||
vendor_available: false,
|
||||
srcs: [
|
||||
"cgroup_controller.cpp",
|
||||
"cgroup_file.cpp",
|
||||
|
|
|
@ -56,14 +56,14 @@ using namespace std::chrono_literals;
|
|||
#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
|
||||
|
||||
bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
|
||||
const CgroupController* controller = CgroupMap::GetInstance().FindController(cgroup_name);
|
||||
auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
|
||||
|
||||
if (controller == nullptr) {
|
||||
if (!controller.HasValue()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (path) {
|
||||
*path = controller->path();
|
||||
*path = controller.path();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -107,7 +107,7 @@ bool UsePerAppMemcg() {
|
|||
|
||||
static bool isMemoryCgroupSupported() {
|
||||
std::string cgroup_name;
|
||||
static bool memcg_supported = (CgroupMap::GetInstance().FindController("memory") != nullptr);
|
||||
static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").HasValue();
|
||||
|
||||
return memcg_supported;
|
||||
}
|
||||
|
|
|
@ -152,21 +152,21 @@ int set_sched_policy(int tid, SchedPolicy policy) {
|
|||
}
|
||||
|
||||
bool cpusets_enabled() {
|
||||
static bool enabled = (CgroupMap::GetInstance().FindController("cpuset") != nullptr);
|
||||
static bool enabled = (CgroupMap::GetInstance().FindController("cpuset").HasValue());
|
||||
return enabled;
|
||||
}
|
||||
|
||||
bool schedboost_enabled() {
|
||||
static bool enabled = (CgroupMap::GetInstance().FindController("schedtune") != nullptr);
|
||||
static bool enabled = (CgroupMap::GetInstance().FindController("schedtune").HasValue());
|
||||
return enabled;
|
||||
}
|
||||
|
||||
static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
|
||||
const CgroupController* controller = CgroupMap::GetInstance().FindController(subsys);
|
||||
auto controller = CgroupMap::GetInstance().FindController(subsys);
|
||||
|
||||
if (!controller) return -1;
|
||||
if (!controller.HasValue()) return -1;
|
||||
|
||||
if (!controller->GetTaskGroup(tid, &subgroup)) {
|
||||
if (!controller.GetTaskGroup(tid, &subgroup)) {
|
||||
LOG(ERROR) << "Failed to find cgroup for tid " << tid;
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ using android::base::WriteStringToFile;
|
|||
|
||||
bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const {
|
||||
std::string subgroup;
|
||||
if (!controller_->GetTaskGroup(tid, &subgroup)) {
|
||||
if (!controller()->GetTaskGroup(tid, &subgroup)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -55,9 +55,10 @@ bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const {
|
|||
}
|
||||
|
||||
if (subgroup.empty()) {
|
||||
*path = StringPrintf("%s/%s", controller_->path(), file_name_.c_str());
|
||||
*path = StringPrintf("%s/%s", controller()->path(), file_name_.c_str());
|
||||
} else {
|
||||
*path = StringPrintf("%s/%s/%s", controller_->path(), subgroup.c_str(), file_name_.c_str());
|
||||
*path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(),
|
||||
file_name_.c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -135,7 +136,7 @@ bool SetCgroupAction::IsAppDependentPath(const std::string& path) {
|
|||
return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
|
||||
}
|
||||
|
||||
SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p)
|
||||
SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
|
||||
: controller_(c), path_(p) {
|
||||
#ifdef CACHE_FILE_DESCRIPTORS
|
||||
// cache file descriptor only if path is app independent
|
||||
|
@ -145,7 +146,7 @@ SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p
|
|||
return;
|
||||
}
|
||||
|
||||
std::string tasks_path = c->GetTasksFilePath(p.c_str());
|
||||
std::string tasks_path = c.GetTasksFilePath(p);
|
||||
|
||||
if (access(tasks_path.c_str(), W_OK) != 0) {
|
||||
// file is not accessible
|
||||
|
@ -199,7 +200,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
|
|||
}
|
||||
|
||||
// this is app-dependent path, file descriptor is not cached
|
||||
std::string procs_path = controller_->GetProcsFilePath(path_, uid, pid);
|
||||
std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
|
||||
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
|
||||
if (tmp_fd < 0) {
|
||||
PLOG(WARNING) << "Failed to open " << procs_path;
|
||||
|
@ -212,7 +213,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
|
|||
|
||||
return true;
|
||||
#else
|
||||
std::string procs_path = controller_->GetProcsFilePath(path_, uid, pid);
|
||||
std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
|
||||
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
|
||||
if (tmp_fd < 0) {
|
||||
// no permissions to access the file, ignore
|
||||
|
@ -247,7 +248,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
|
|||
LOG(ERROR) << "Application profile can't be applied to a thread";
|
||||
return false;
|
||||
#else
|
||||
std::string tasks_path = controller_->GetTasksFilePath(path_);
|
||||
std::string tasks_path = controller()->GetTasksFilePath(path_);
|
||||
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
|
||||
if (tmp_fd < 0) {
|
||||
// no permissions to access the file, ignore
|
||||
|
@ -326,8 +327,8 @@ bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) {
|
|||
std::string file_attr = attr[i]["File"].asString();
|
||||
|
||||
if (attributes_.find(name) == attributes_.end()) {
|
||||
const CgroupController* controller = cg_map.FindController(controller_name);
|
||||
if (controller) {
|
||||
auto controller = cg_map.FindController(controller_name);
|
||||
if (controller.HasValue()) {
|
||||
attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr);
|
||||
} else {
|
||||
LOG(WARNING) << "Controller " << controller_name << " is not found";
|
||||
|
@ -355,8 +356,8 @@ bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) {
|
|||
std::string controller_name = params_val["Controller"].asString();
|
||||
std::string path = params_val["Path"].asString();
|
||||
|
||||
const CgroupController* controller = cg_map.FindController(controller_name);
|
||||
if (controller) {
|
||||
auto controller = cg_map.FindController(controller_name);
|
||||
if (controller.HasValue()) {
|
||||
profile->Add(std::make_unique<SetCgroupAction>(controller, path));
|
||||
} else {
|
||||
LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found";
|
||||
|
|
|
@ -27,16 +27,16 @@
|
|||
|
||||
class ProfileAttribute {
|
||||
public:
|
||||
ProfileAttribute(const CgroupController* controller, const std::string& file_name)
|
||||
ProfileAttribute(const CgroupController& controller, const std::string& file_name)
|
||||
: controller_(controller), file_name_(file_name) {}
|
||||
|
||||
const CgroupController* controller() const { return controller_; }
|
||||
const CgroupController* controller() const { return &controller_; }
|
||||
const std::string& file_name() const { return file_name_; }
|
||||
|
||||
bool GetPathForTask(int tid, std::string* path) const;
|
||||
|
||||
private:
|
||||
const CgroupController* controller_;
|
||||
CgroupController controller_;
|
||||
std::string file_name_;
|
||||
};
|
||||
|
||||
|
@ -106,16 +106,16 @@ class SetAttributeAction : public ProfileAction {
|
|||
// Set cgroup profile element
|
||||
class SetCgroupAction : public ProfileAction {
|
||||
public:
|
||||
SetCgroupAction(const CgroupController* c, const std::string& p);
|
||||
SetCgroupAction(const CgroupController& c, const std::string& p);
|
||||
|
||||
virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
|
||||
virtual bool ExecuteForTask(int tid) const;
|
||||
|
||||
const CgroupController* controller() const { return controller_; }
|
||||
const CgroupController* controller() const { return &controller_; }
|
||||
std::string path() const { return path_; }
|
||||
|
||||
private:
|
||||
const CgroupController* controller_;
|
||||
CgroupController controller_;
|
||||
std::string path_;
|
||||
#ifdef CACHE_FILE_DESCRIPTORS
|
||||
android::base::unique_fd fd_;
|
||||
|
|
Loading…
Reference in a new issue