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>
|
|
|
|
#include <map>
|
2019-05-31 13:05:22 +02:00
|
|
|
#include <mutex>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/unique_fd.h>
|
|
|
|
#include <cgroup_map.h>
|
|
|
|
|
|
|
|
class ProfileAttribute {
|
|
|
|
public:
|
2019-03-23 01:01:08 +01:00
|
|
|
ProfileAttribute(const CgroupController& controller, const std::string& file_name)
|
2018-12-21 20:41:50 +01:00
|
|
|
: controller_(controller), file_name_(file_name) {}
|
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
const CgroupController* controller() const { return &controller_; }
|
2018-12-21 20:41:50 +01:00
|
|
|
const std::string& file_name() const { return file_name_; }
|
|
|
|
|
|
|
|
bool GetPathForTask(int tid, std::string* path) const;
|
|
|
|
|
|
|
|
private:
|
2019-03-23 01:01:08 +01:00
|
|
|
CgroupController controller_;
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string file_name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Abstract profile element
|
|
|
|
class ProfileAction {
|
|
|
|
public:
|
|
|
|
virtual ~ProfileAction() {}
|
|
|
|
|
|
|
|
// Default implementations will fail
|
2019-02-04 15:33:26 +01:00
|
|
|
virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
|
|
|
|
virtual bool ExecuteForTask(int) const { return false; };
|
2019-02-14 23:40:41 +01:00
|
|
|
|
|
|
|
virtual void EnableResourceCaching() {}
|
2019-06-18 23:53:53 +02:00
|
|
|
virtual void DropResourceCaching() {}
|
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) {}
|
|
|
|
|
|
|
|
virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
|
|
|
|
virtual bool ExecuteForTask(int tid) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int boost_;
|
|
|
|
int clamp_;
|
|
|
|
};
|
|
|
|
|
2019-02-02 23:19:41 +01:00
|
|
|
// To avoid issues in sdk_mac build
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
class SetTimerSlackAction : public ProfileAction {
|
|
|
|
public:
|
|
|
|
SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
|
|
|
|
|
|
|
|
virtual bool ExecuteForTask(int tid) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned long slack_;
|
|
|
|
|
|
|
|
static bool IsTimerSlackSupported(int tid);
|
|
|
|
};
|
|
|
|
|
2019-02-02 23:19:41 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
class SetTimerSlackAction : public ProfileAction {
|
|
|
|
public:
|
|
|
|
SetTimerSlackAction(unsigned long) noexcept {}
|
|
|
|
|
|
|
|
virtual bool ExecuteForTask(int) const { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
// Set attribute profile element
|
|
|
|
class SetAttributeAction : public ProfileAction {
|
|
|
|
public:
|
|
|
|
SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
|
|
|
|
: attribute_(attribute), value_(value) {}
|
|
|
|
|
|
|
|
virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
|
|
|
|
virtual bool ExecuteForTask(int tid) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ProfileAttribute* attribute_;
|
|
|
|
std::string value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set cgroup profile element
|
|
|
|
class SetCgroupAction : public ProfileAction {
|
|
|
|
public:
|
2019-03-23 01:01:08 +01:00
|
|
|
SetCgroupAction(const CgroupController& c, const std::string& p);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
|
|
|
|
virtual bool ExecuteForTask(int tid) const;
|
2019-02-14 23:40:41 +01:00
|
|
|
virtual void EnableResourceCaching();
|
2019-06-18 23:53:53 +02:00
|
|
|
virtual void DropResourceCaching();
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
const CgroupController* controller() const { return &controller_; }
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string path() const { return path_; }
|
|
|
|
|
|
|
|
private:
|
2019-02-14 23:40:41 +01:00
|
|
|
enum FdState {
|
|
|
|
FDS_INACCESSIBLE = -1,
|
|
|
|
FDS_APP_DEPENDENT = -2,
|
|
|
|
FDS_NOT_CACHED = -3,
|
|
|
|
};
|
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
CgroupController controller_;
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string path_;
|
|
|
|
android::base::unique_fd fd_;
|
2019-05-31 13:05:22 +02:00
|
|
|
mutable std::mutex fd_mutex_;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
static bool IsAppDependentPath(const std::string& path);
|
|
|
|
static bool AddTidToCgroup(int tid, int fd);
|
2019-02-14 23:40:41 +01:00
|
|
|
|
|
|
|
bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class TaskProfile {
|
|
|
|
public:
|
2019-02-14 23:40:41 +01:00
|
|
|
TaskProfile() : res_cached_(false) {}
|
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;
|
|
|
|
bool ExecuteForTask(int tid) const;
|
2019-02-14 23:40:41 +01:00
|
|
|
void EnableResourceCaching();
|
2019-06-18 23:53:53 +02:00
|
|
|
void DropResourceCaching();
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
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) {}
|
|
|
|
|
|
|
|
virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
|
|
|
|
virtual bool ExecuteForTask(int tid) const;
|
2020-02-14 02:28:00 +01:00
|
|
|
virtual void EnableResourceCaching();
|
|
|
|
virtual void DropResourceCaching();
|
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();
|
|
|
|
|
2019-02-14 23:40:41 +01:00
|
|
|
TaskProfile* GetProfile(const std::string& name) const;
|
2018-12-21 20:41:50 +01:00
|
|
|
const ProfileAttribute* GetAttribute(const std::string& name) const;
|
2019-06-18 23:53:53 +02:00
|
|
|
void DropResourceCaching() const;
|
2020-02-14 02:28:00 +01:00
|
|
|
bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);
|
2019-09-16 13:07:17 +02:00
|
|
|
bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
2019-09-16 13:07:17 +02:00
|
|
|
std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
|
2018-12-21 20:41:50 +01:00
|
|
|
std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
|
|
|
|
|
|
|
|
TaskProfiles();
|
|
|
|
|
2019-02-20 00:01:28 +01:00
|
|
|
bool Load(const CgroupMap& cg_map, const std::string& file_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|