来自https://plus.google.com/u/0/105051985738280261832/posts/XAZ4CeVP6DC,据说设置为背景优先级的线程将被放入特定的 cgroup 中,最多会消耗 10% 的 CPU。
我想在我的应用程序中限制一个任务的 CPU 使用率,所以我将优先级设置为背景,但它似乎不起作用。
所以我写了一个简单的应用程序来测试它,发现它会消耗与默认优先级相同的 CPU。我检查了两个线程的cgroup,一个在apps cgroup中,另一个在apps/bg_non_interactive cgroup中。
public class TestThreadPriority {
private class RunnerThread implements Runnable{
String id = null;
long n = 0;
int mSavedPriority = -100;
String mSavedCGroup = null;
RunnerThread(String identity)
{
id = identity;
}
public void run()
{
if (id == "1")
{
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
}
else
{
Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
}
for ( int jj = 0; jj < 10000; jj++)
{
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 10000; j++)
{
int m = 3;
n += m * 12;
}
}
mSavedPriority = Process.getThreadPriority(Process.myTid());
mSavedCGroup = getSchedulerGroup();
Log.e("CGROUPS", "runner " + id + ": " + jj + " Pri:" + mSavedPriority + " cgroup: " + mSavedCGroup + " n : " + Long.toString(n));
}
}
}
public void beginTest()
{
RunnerThread rThread1 = new RunnerThread("1");
Thread runner1 = new Thread(rThread1);
RunnerThread rThread2 = new RunnerThread("2");
Thread runner2 = new Thread(rThread2);
runner2.start();
runner1.start();
}
public static String TAG = "CGROUPS";
public static String getSchedulerGroup() {
String fn = "/proc/" + Process.myPid() + "/task/" + Process.myTid() + "/cgroup";
try {
List<String> cgroups = FileUtils.readLines(new File(fn));
for (String line : cgroups) {
String fields[] = line.trim().split(":");
if (fields.length == 3 && fields[1].equals("cpu")) return fields[2];
}
} catch (IOException e) {
Log.e(TAG, "Can't read: " + fn, e);
}
return null;
}
}
结果是:
12-17 11:02:15.321: E/CGROUPS(27604): runner 2: 0 Pri:0 cgroup: /apps n : 3600000000
12-17 11:02:15.361: E/CGROUPS(27604): runner 1: 0 Pri:10 cgroup: /apps/bg_non_interactive n : 3600000000
12-17 11:02:17.091: E/CGROUPS(27604): runner 2: 1 Pri:0 cgroup: /apps n : 7200000000
12-17 11:02:17.171: E/CGROUPS(27604): runner 1: 1 Pri:10 cgroup: /apps/bg_non_interactive n : 7200000000
12-17 11:02:18.891: E/CGROUPS(27604): runner 2: 2 Pri:0 cgroup: /apps n : 10800000000
12-17 11:02:18.966: E/CGROUPS(27604): runner 1: 2 Pri:10 cgroup: /apps/bg_non_interactive n : 10800000000
12-17 11:02:20.676: E/CGROUPS(27604): runner 2: 3 Pri:0 cgroup: /apps n : 14400000000
12-17 11:02:20.746: E/CGROUPS(27604): runner 1: 3 Pri:10 cgroup: /apps/bg_non_interactive n : 14400000000
12-17 11:02:22.466: E/CGROUPS(27604): runner 2: 4 Pri:0 cgroup: /apps n : 18000000000
12-17 11:02:22.566: E/CGROUPS(27604): runner 1: 4 Pri:10 cgroup: /apps/bg_non_interactive n : 18000000000
12-17 11:02:24.246: E/CGROUPS(27604): runner 2: 5 Pri:0 cgroup: /apps n : 21600000000
12-17 11:02:24.336: E/CGROUPS(27604): runner 1: 5 Pri:10 cgroup: /apps/bg_non_interactive n : 21600000000
12-17 11:02:26.046: E/CGROUPS(27604): runner 2: 6 Pri:0 cgroup: /apps n : 25200000000
12-17 11:02:26.121: E/CGROUPS(27604): runner 1: 6 Pri:10 cgroup: /apps/bg_non_interactive n : 25200000000
12-17 11:02:27.826: E/CGROUPS(27604): runner 2: 7 Pri:0 cgroup: /apps n : 28800000000
12-17 11:02:28.056: E/CGROUPS(27604): runner 1: 7 Pri:10 cgroup: /apps/bg_non_interactive n : 28800000000
12-17 11:02:29.596: E/CGROUPS(27604): runner 2: 8 Pri:0 cgroup: /apps n : 32400000000
12-17 11:02:29.811: E/CGROUPS(27604): runner 1: 8 Pri:10 cgroup: /apps/bg_non_interactive n : 32400000000
12-17 11:02:31.366: E/CGROUPS(27604): runner 2: 9 Pri:0 cgroup: /apps n : 36000000000
12-17 11:02:31.571: E/CGROUPS(27604): runner 1: 9 Pri:10 cgroup: /apps/bg_non_interactive n : 36000000000
12-17 11:02:33.141: E/CGROUPS(27604): runner 2: 10 Pri:0 cgroup: /apps n : 39600000000
12-17 11:02:33.376: E/CGROUPS(27604): runner 1: 10 Pri:10 cgroup: /apps/bg_non_interactive n : 39600000000
12-17 11:02:34.916: E/CGROUPS(27604): runner 2: 11 Pri:0 cgroup: /apps n : 43200000000
12-17 11:02:35.151: E/CGROUPS(27604): runner 1: 11 Pri:10 cgroup: /apps/bg_non_interactive n : 43200000000
12-17 11:02:36.681: E/CGROUPS(27604): runner 2: 12 Pri:0 cgroup: /apps n : 46800000000
12-17 11:02:36.941: E/CGROUPS(27604): runner 1: 12 Pri:10 cgroup: /apps/bg_non_interactive n : 46800000000
12-17 11:02:38.466: E/CGROUPS(27604): runner 2: 13 Pri:0 cgroup: /apps n : 50400000000
12-17 11:02:38.716: E/CGROUPS(27604): runner 1: 13 Pri:10 cgroup: /apps/bg_non_interactive n : 50400000000
12-17 11:02:40.236: E/CGROUPS(27604): runner 2: 14 Pri:0 cgroup: /apps n : 54000000000
12-17 11:02:40.481: E/CGROUPS(27604): runner 1: 14 Pri:10 cgroup: /apps/bg_non_interactive n : 54000000000
12-17 11:02:42.001: E/CGROUPS(27604): runner 2: 15 Pri:0 cgroup: /apps n : 57600000000
12-17 11:02:42.261: E/CGROUPS(27604): runner 1: 15 Pri:10 cgroup: /apps/bg_non_interactive n : 57600000000
12-17 11:02:43.781: E/CGROUPS(27604): runner 2: 16 Pri:0 cgroup: /apps n : 61200000000
12-17 11:02:44.056: E/CGROUPS(27604): runner 1: 16 Pri:10 cgroup: /apps/bg_non_interactive n : 61200000000
12-17 11:02:45.556: E/CGROUPS(27604): runner 2: 17 Pri:0 cgroup: /apps n : 64800000000
12-17 11:02:45.826: E/CGROUPS(27604): runner 1: 17 Pri:10 cgroup: /apps/bg_non_interactive n : 64800000000
12-17 11:02:47.331: E/CGROUPS(27604): runner 2: 18 Pri:0 cgroup: /apps n : 68400000000
12-17 11:02:47.596: E/CGROUPS(27604): runner 1: 18 Pri:10 cgroup: /apps/bg_non_interactive n : 68400000000