Aurimas Liutikas | 88c7ff1 | 2023-08-10 12:42:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
| 19 | import android.perftests.utils.BenchmarkState; |
| 20 | import android.perftests.utils.PerfStatusReporter; |
| 21 | |
| 22 | import androidx.test.filters.LargeTest; |
| 23 | import androidx.test.runner.AndroidJUnit4; |
| 24 | |
| 25 | import org.junit.Rule; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | |
| 29 | import java.nio.file.Files; |
| 30 | import java.nio.file.Paths; |
| 31 | |
| 32 | /** |
| 33 | * Performance tests collecting CPU data different mechanisms. |
| 34 | */ |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | @LargeTest |
| 37 | public class CpuUsageTrackingPerfTest { |
| 38 | @Rule |
| 39 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 40 | |
| 41 | @Test |
| 42 | public void timeSystemThread() { |
| 43 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 44 | Binder b = new Binder(); |
| 45 | while (state.keepRunning()) { |
| 46 | SystemClock.currentThreadTimeMicro(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void timeReadStatFileDirectly() throws Exception { |
| 52 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 53 | // CPU usage by frequency for this pid. Data is in text format. |
| 54 | final String procFile = "/proc/self/stat"; |
| 55 | while (state.keepRunning()) { |
| 56 | byte[] data = Files.readAllBytes(Paths.get(procFile)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void timeReadPidProcDirectly() throws Exception { |
| 62 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 63 | // CPU usage by frequency for this pid. Data is in text format. |
| 64 | final String procFile = "/proc/self/time_in_state"; |
| 65 | while (state.keepRunning()) { |
| 66 | byte[] data = Files.readAllBytes(Paths.get(procFile)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void timeReadThreadProcDirectly() throws Exception { |
| 72 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 73 | // CPU usage by frequency for this UID. Data is in text format. |
| 74 | final String procFile = "/proc/self/task/" + android.os.Process.myTid() |
| 75 | + "/time_in_state"; |
| 76 | while (state.keepRunning()) { |
| 77 | byte[] data = Files.readAllBytes(Paths.get(procFile)); |
| 78 | } |
| 79 | } |
| 80 | } |