diff options
author | Wei Wang <wvw@google.com> | 2021-11-30 23:22:18 -0800 |
---|---|---|
committer | Wei Wang <wvw@google.com> | 2021-12-03 17:18:02 +0000 |
commit | c733a915cc76326af40f1d468b35100bcbbd99e5 (patch) | |
tree | 352140dbadbcf281405abe8f3307c9063609e58c | |
parent | 4a9b548867db1dd158ff78d47d448ca738289da6 (diff) | |
download | extras-c733a915cc76326af40f1d468b35100bcbbd99e5.tar.gz |
Add a tool for setting uclamp for given PID
Test: run
Bug: 205764497
Change-Id: I5afb92909a93728909dd43b6aa92d36f0d965630
-rw-r--r-- | setuclamp/Android.bp | 27 | ||||
-rw-r--r-- | setuclamp/setuclamp.cpp | 75 |
2 files changed, 102 insertions, 0 deletions
diff --git a/setuclamp/Android.bp b/setuclamp/Android.bp new file mode 100644 index 00000000..4980aea2 --- /dev/null +++ b/setuclamp/Android.bp @@ -0,0 +1,27 @@ +// Copyright (C) 2021 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. + +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +cc_binary { + name: "setuclamp", + cflags: [ + "-Wall", + "-Werror", + ], + + srcs: ["setuclamp.cpp"], +} diff --git a/setuclamp/setuclamp.cpp b/setuclamp/setuclamp.cpp new file mode 100644 index 00000000..e40fb0fd --- /dev/null +++ b/setuclamp/setuclamp.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 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. + */ + +#include <stdlib.h> +#include <sys/syscall.h> +#include <unistd.h> + +#include <iostream> + +[[noreturn]] static void usage(int exit_status) { + std::cerr << "Usage: " << getprogname() << " <tid> <uclamp_min> <uclamp_max>" << std::endl + << " tid Thread ID to apply the uclamp setting." << std::endl + << " uclamp_min uclamp.min value range from [0, 1024]." << std::endl + << " uclamp_max uclamp.max value range from [0, 1024]." << std::endl; + exit(exit_status); +} + +struct sched_attr { + __u32 size; + __u32 sched_policy; + __u64 sched_flags; + __s32 sched_nice; + __u32 sched_priority; + __u64 sched_runtime; + __u64 sched_deadline; + __u64 sched_period; + __u32 sched_util_min; + __u32 sched_util_max; +}; + +static int sched_setattr(int pid, struct sched_attr* attr, unsigned int flags) { + return syscall(__NR_sched_setattr, pid, attr, flags); +} + +static int set_uclamp(int32_t min, int32_t max, int tid) { + sched_attr attr = {}; + attr.size = sizeof(attr); + + attr.sched_flags = (SCHED_FLAG_KEEP_ALL | SCHED_FLAG_UTIL_CLAMP); + attr.sched_util_min = min; + attr.sched_util_max = max; + + int ret = sched_setattr(tid, &attr, 0); + if (ret) { + int err = errno; + std::cerr << "sched_setattr failed for thread " << tid << " err=" << err << std::endl; + } + + return ret; +} + +int main(int argc, char* argv[]) { + if (argc != 4) { + usage(EXIT_FAILURE); + } + + int tid = atoi(argv[1]); + int uclamp_min = atoi(argv[2]); + int uclamp_max = atoi(argv[3]); + + return set_uclamp(uclamp_min, uclamp_max, tid); +} |