2018-12-21 20:41:50 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
2022-08-02 22:18:12 +02:00
|
|
|
#include <functional>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <map>
|
2019-05-31 13:05:22 +02:00
|
|
|
#include <mutex>
|
2022-08-02 22:18:12 +02:00
|
|
|
#include <span>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <string>
|
2022-08-02 22:06:26 +02:00
|
|
|
#include <string_view>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
#include <cgroup_map.h>
|
|
|
|
|
2022-02-03 20:50:16 +01:00
|
|
|
class IProfileAttribute {
|
|
|
|
public:
|
|
|
|
virtual ~IProfileAttribute() = 0;
|
2023-07-25 23:50:18 +02:00
|
|
|
virtual void Reset(const CgroupController& controller, const std::string& file_name,
|
|
|
|
const std::string& file_v2_name) = 0;
|
2022-02-03 20:50:16 +01:00
|
|
|
virtual const CgroupController* controller() const = 0;
|
|
|
|
virtual const std::string& file_name() const = 0;
|
2023-07-26 00:45:45 +02:00
|
|
|
virtual bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const = 0;
|
2024-01-25 17:29:54 +01:00
|
|
|
virtual bool GetPathForTask(pid_t tid, std::string* path) const = 0;
|
2022-08-22 23:25:09 +02:00
|
|
|
virtual bool GetPathForUID(uid_t uid, std::string* path) const = 0;
|
2022-02-03 20:50:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProfileAttribute : public IProfileAttribute {
|
2018-12-21 20:41:50 +01:00
|
|
|
public:
|
2022-02-17 02:26:44 +01:00
|
|
|
// Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
|
|
|
|
// not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
|
|
|
|
// the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
|
|
|
|
ProfileAttribute(const CgroupController& controller, const std::string& file_name,
|
|
|
|
const std::string& file_v2_name)
|
|
|
|
: controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
|
2022-02-03 20:50:16 +01:00
|
|
|
~ProfileAttribute() = default;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2022-02-03 20:50:16 +01:00
|
|
|
const CgroupController* controller() const override { return &controller_; }
|
2023-07-25 23:50:18 +02:00
|
|
|
const std::string& file_name() const override;
|
|
|
|
void Reset(const CgroupController& controller, const std::string& file_name,
|
|
|
|
const std::string& file_v2_name) override;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2023-07-26 00:45:45 +02:00
|
|
|
bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool GetPathForTask(pid_t tid, std::string* path) const override;
|
2022-08-22 23:25:09 +02:00
|
|
|
bool GetPathForUID(uid_t uid, std::string* path) const override;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
2019-03-23 01:01:08 +01:00
|
|
|
CgroupController controller_;
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string file_name_;
|
2022-02-17 02:26:44 +01:00
|
|
|
std::string file_v2_name_;
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Abstract profile element
|
|
|
|
class ProfileAction {
|
|
|
|
public:
|
2022-01-21 00:41:28 +01:00
|
|
|
enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
virtual ~ProfileAction() {}
|
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
virtual const char* Name() const = 0;
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
// Default implementations will fail
|
2023-04-12 03:24:23 +02:00
|
|
|
virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }
|
|
|
|
virtual bool ExecuteForTask(int) const { return false; }
|
|
|
|
virtual bool ExecuteForUID(uid_t) const { return false; }
|
2019-02-14 23:40:41 +01:00
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
virtual void EnableResourceCaching(ResourceCacheType) {}
|
|
|
|
virtual void DropResourceCaching(ResourceCacheType) {}
|
2023-04-12 03:24:23 +02:00
|
|
|
virtual bool IsValidForProcess(uid_t uid, pid_t pid) const { return false; }
|
2024-01-25 17:29:54 +01:00
|
|
|
virtual bool IsValidForTask(pid_t tid) const { return false; }
|
2022-01-21 00:41:28 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
enum CacheUseResult { SUCCESS, FAIL, UNUSED };
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Profile actions
|
|
|
|
class SetClampsAction : public ProfileAction {
|
|
|
|
public:
|
|
|
|
SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
|
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const char* Name() const override { return "SetClamps"; }
|
2022-01-24 21:52:51 +01:00
|
|
|
bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const override;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int boost_;
|
|
|
|
int clamp_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SetTimerSlackAction : public ProfileAction {
|
|
|
|
public:
|
|
|
|
SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
|
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const char* Name() const override { return "SetTimerSlack"; }
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const override;
|
2023-04-12 03:24:23 +02:00
|
|
|
bool IsValidForProcess(uid_t uid, pid_t pid) const override { return true; }
|
2024-01-25 17:29:54 +01:00
|
|
|
bool IsValidForTask(pid_t tid) const override { return true; }
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned long slack_;
|
|
|
|
|
2024-01-25 17:29:54 +01:00
|
|
|
static bool IsTimerSlackSupported(pid_t tid);
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set attribute profile element
|
|
|
|
class SetAttributeAction : public ProfileAction {
|
|
|
|
public:
|
2022-01-24 22:08:57 +01:00
|
|
|
SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
|
|
|
|
: attribute_(attribute), value_(value), optional_(optional) {}
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const char* Name() const override { return "SetAttribute"; }
|
2022-01-24 21:52:51 +01:00
|
|
|
bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const override;
|
2022-08-22 23:25:09 +02:00
|
|
|
bool ExecuteForUID(uid_t uid) const override;
|
2023-04-12 03:24:23 +02:00
|
|
|
bool IsValidForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool IsValidForTask(pid_t tid) const override;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
2022-02-03 20:50:16 +01:00
|
|
|
const IProfileAttribute* attribute_;
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string value_;
|
2022-01-24 22:08:57 +01:00
|
|
|
bool optional_;
|
2023-07-26 00:45:45 +02:00
|
|
|
|
|
|
|
bool WriteValueToFile(const std::string& path) const;
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
2021-11-21 08:57:36 +01:00
|
|
|
// Set cgroup profile element
|
2022-01-20 19:58:43 +01:00
|
|
|
class SetCgroupAction : public ProfileAction {
|
2021-11-21 08:57:36 +01:00
|
|
|
public:
|
|
|
|
SetCgroupAction(const CgroupController& c, const std::string& p);
|
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const char* Name() const override { return "SetCgroup"; }
|
2022-01-24 21:52:51 +01:00
|
|
|
bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const override;
|
2022-01-24 21:52:51 +01:00
|
|
|
void EnableResourceCaching(ResourceCacheType cache_type) override;
|
|
|
|
void DropResourceCaching(ResourceCacheType cache_type) override;
|
2023-04-12 03:24:23 +02:00
|
|
|
bool IsValidForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool IsValidForTask(pid_t tid) const override;
|
2021-11-21 08:57:36 +01:00
|
|
|
|
|
|
|
const CgroupController* controller() const { return &controller_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CgroupController controller_;
|
|
|
|
std::string path_;
|
2022-01-21 00:41:28 +01:00
|
|
|
android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
|
2022-01-20 19:58:43 +01:00
|
|
|
mutable std::mutex fd_mutex_;
|
2021-11-21 08:57:36 +01:00
|
|
|
|
2024-01-25 17:29:54 +01:00
|
|
|
bool AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const;
|
2022-01-21 00:41:28 +01:00
|
|
|
CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
2020-10-26 13:32:52 +01:00
|
|
|
// Write to file action
|
2022-01-20 19:58:43 +01:00
|
|
|
class WriteFileAction : public ProfileAction {
|
2020-10-26 13:32:52 +01:00
|
|
|
public:
|
2022-02-10 09:44:43 +01:00
|
|
|
WriteFileAction(const std::string& task_path, const std::string& proc_path,
|
|
|
|
const std::string& value, bool logfailures);
|
2020-10-26 13:32:52 +01:00
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const char* Name() const override { return "WriteFile"; }
|
2022-01-24 21:52:51 +01:00
|
|
|
bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const override;
|
2022-01-24 21:52:51 +01:00
|
|
|
void EnableResourceCaching(ResourceCacheType cache_type) override;
|
|
|
|
void DropResourceCaching(ResourceCacheType cache_type) override;
|
2023-04-12 03:24:23 +02:00
|
|
|
bool IsValidForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool IsValidForTask(pid_t tid) const override;
|
2021-11-21 08:57:36 +01:00
|
|
|
|
2020-10-26 13:32:52 +01:00
|
|
|
private:
|
2022-02-10 09:44:43 +01:00
|
|
|
std::string task_path_, proc_path_, value_;
|
2021-01-25 05:44:45 +01:00
|
|
|
bool logfailures_;
|
2022-02-10 09:44:43 +01:00
|
|
|
android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
|
2022-01-20 19:58:43 +01:00
|
|
|
mutable std::mutex fd_mutex_;
|
2021-11-21 08:57:36 +01:00
|
|
|
|
2024-01-24 23:45:26 +01:00
|
|
|
bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, uid_t uid,
|
2024-01-25 00:42:39 +01:00
|
|
|
pid_t pid, bool logfailures) const;
|
2022-01-21 00:41:28 +01:00
|
|
|
CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
|
2020-10-26 13:32:52 +01:00
|
|
|
};
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
class TaskProfile {
|
|
|
|
public:
|
2022-01-24 20:59:13 +01:00
|
|
|
TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const std::string& Name() const { return name_; }
|
2018-12-21 20:41:50 +01:00
|
|
|
void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
|
2020-01-25 01:36:10 +01:00
|
|
|
void MoveTo(TaskProfile* profile);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
bool ExecuteForProcess(uid_t uid, pid_t pid) const;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const;
|
2022-08-22 23:25:09 +02:00
|
|
|
bool ExecuteForUID(uid_t uid) const;
|
2022-01-21 00:41:28 +01:00
|
|
|
void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
|
|
|
|
void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
|
2023-04-12 03:24:23 +02:00
|
|
|
bool IsValidForProcess(uid_t uid, pid_t pid) const;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool IsValidForTask(pid_t tid) const;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
2022-01-24 20:59:13 +01:00
|
|
|
const std::string name_;
|
2019-02-14 23:40:41 +01:00
|
|
|
bool res_cached_;
|
2018-12-21 20:41:50 +01:00
|
|
|
std::vector<std::unique_ptr<ProfileAction>> elements_;
|
|
|
|
};
|
|
|
|
|
2019-09-16 13:07:17 +02:00
|
|
|
// Set aggregate profile element
|
|
|
|
class ApplyProfileAction : public ProfileAction {
|
|
|
|
public:
|
|
|
|
ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
|
|
|
|
: profiles_(profiles) {}
|
|
|
|
|
2022-01-24 20:59:13 +01:00
|
|
|
const char* Name() const override { return "ApplyProfileAction"; }
|
2022-01-24 21:52:51 +01:00
|
|
|
bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool ExecuteForTask(pid_t tid) const override;
|
2022-01-24 21:52:51 +01:00
|
|
|
void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
|
|
|
|
void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
|
2023-04-12 03:24:23 +02:00
|
|
|
bool IsValidForProcess(uid_t uid, pid_t pid) const override;
|
2024-01-25 17:29:54 +01:00
|
|
|
bool IsValidForTask(pid_t tid) const override;
|
2019-09-16 13:07:17 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::shared_ptr<TaskProfile>> profiles_;
|
|
|
|
};
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
class TaskProfiles {
|
|
|
|
public:
|
|
|
|
// Should be used by all users
|
|
|
|
static TaskProfiles& GetInstance();
|
|
|
|
|
2022-08-02 22:06:26 +02:00
|
|
|
TaskProfile* GetProfile(std::string_view name) const;
|
|
|
|
const IProfileAttribute* GetAttribute(std::string_view name) const;
|
2022-01-21 00:41:28 +01:00
|
|
|
void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
|
2022-08-02 22:18:12 +02:00
|
|
|
template <typename T>
|
|
|
|
bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
|
|
|
|
template <typename T>
|
2024-01-25 17:29:54 +01:00
|
|
|
bool SetTaskProfiles(pid_t tid, std::span<const T> profiles, bool use_fd_cache);
|
2022-08-22 23:25:09 +02:00
|
|
|
template <typename T>
|
|
|
|
bool SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
TaskProfiles();
|
|
|
|
|
2019-02-20 00:01:28 +01:00
|
|
|
bool Load(const CgroupMap& cg_map, const std::string& file_name);
|
2022-08-02 22:06:26 +02:00
|
|
|
|
|
|
|
std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
|
|
|
|
std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|