Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34210e81 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I8d755f8b,Ia297427b

* changes:
  Add explicit GCs to SystemMemoryTest.
  SystemMemoryTest: Introduce Device class.
parents 066a59da cdc122cc
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -16,24 +16,27 @@

package com.android.tests.sysmem.host;

import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;

/**
 * Critical user journeys with which to exercise the system, driven from the
 * host.
 */
public class Cujs {
    private ITestDevice mDevice;
    private Device mDevice;

    public Cujs(ITestDevice device) {
    public Cujs(Device device) {
        this.mDevice = device;
    }

    /**
     * Runs the critical user journeys.
     */
    public void run() throws DeviceNotAvailableException {
    public void run() throws TestException {
        // Do an explicit GC in the system server process as part of the test
        // case to reduce GC-related sources of noise.
        // SIGUSR1 = 10 is the magic signal to trigger the GC.
        int pid = mDevice.getPidForProcess("system_server");
        mDevice.executeShellCommand("kill -10 " + pid);

        // Invoke the Device Cujs instrumentation to run the cujs.
        // TODO: Consider exercising the system in other interesting ways as
        // well.
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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 com.android.tests.sysmem.host;

import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Wrapper around ITestDevice exposing useful device functions.
 */
class Device {

    private ITestDevice mDevice;

    Device(ITestDevice device) {
        mDevice = device;
    }

    /**
     * Execute a shell command and return the output as a string.
     */
    public String executeShellCommand(String command) throws TestException {
        try {
            return mDevice.executeShellCommand(command);
        } catch (DeviceNotAvailableException e) {
            throw new TestException(e);
        }
    }

    /**
     * Enable adb root
     */
    public void enableAdbRoot() throws TestException {
        try {
            mDevice.enableAdbRoot();
        } catch (DeviceNotAvailableException e) {
            throw new TestException(e);
        }
    }

    /**
     * Returns the pid for the process with the given name.
     */
    public int getPidForProcess(String name) throws TestException {
        String psout = executeShellCommand("ps -A -o PID,CMD");
        Scanner sc = new Scanner(psout);
        try {
            // ps output is of the form:
            //  PID CMD
            //    1 init
            //    2 kthreadd
            //    ...
            // 9693 ps
            sc.nextLine();
            while (sc.hasNextLine()) {
                int pid = sc.nextInt();
                String cmd = sc.next();

                if (name.equals(cmd)) {
                    return pid;
                }
            }
        } catch (InputMismatchException e) {
            throw new TestException("unexpected ps output format: " + psout, e);
        }

        throw new TestException("failed to get pid for process " + name);
    }
}
+6 −9
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.tests.sysmem.host;

import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
@@ -27,8 +26,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

/**
 * Runs a system memory test.
 */
@@ -44,8 +41,9 @@ public class MemoryTest implements IDeviceTest {
    private Cujs mCujs;

    @Override
    public void setDevice(ITestDevice device) {
        mTestDevice = device;
    public void setDevice(ITestDevice testDevice) {
        mTestDevice = testDevice;
        Device device = new Device(testDevice);
        mMetrics = new Metrics(device, testMetrics, testLogs);
        mCujs = new Cujs(device);
    }
@@ -56,14 +54,13 @@ public class MemoryTest implements IDeviceTest {
    }

    // Invoke a single iteration of running the cujs.
    private void runCujs() throws DeviceNotAvailableException {
    private void runCujs() throws TestException {
        mCujs.run();
        mIterations++;
    }

    // Sample desired memory.
    private void sample()
            throws DeviceNotAvailableException, IOException, Metrics.MetricsException {
    private void sample() throws TestException {
        mMetrics.sample(String.format("%03d", mIterations));
    }

@@ -71,7 +68,7 @@ public class MemoryTest implements IDeviceTest {
     * Runs the memory tests.
     */
    @Test
    public void run() throws Exception {
    public void run() throws TestException {
        sample();   // Sample before running cujs
        runCujs();
        sample();   // Sample after first iteration of cujs
+13 −54
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.tests.sysmem.host;

import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.result.FileInputStreamSource;
import com.android.tradefed.result.LogDataType;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
@@ -34,23 +32,10 @@ import java.util.Scanner;
 */
class Metrics {

    private ITestDevice mDevice;
    private Device mDevice;
    private TestMetrics mMetrics;
    private TestLogData mLogs;

    /**
     * Exception thrown in case of error sampling metrics.
     */
    public static class MetricsException extends Exception {
        MetricsException(String msg) {
            super(msg);
        }

        MetricsException(String msg, Exception cause) {
            super(msg, cause);
        }
    }

    /**
     * Constructs a metrics instance that will output high level metrics and
     * more detailed breakdowns using the given <code>metrics</code> and
@@ -60,7 +45,7 @@ class Metrics {
     * @param metrics where to log the high level metrics when taking a sample
     * @param logs where to log detailed breakdowns when taking a sample
     */
    Metrics(ITestDevice device, TestMetrics metrics, TestLogData logs) {
    Metrics(Device device, TestMetrics metrics, TestLogData logs) {
        this.mDevice = device;
        this.mMetrics = metrics;
        this.mLogs = logs;
@@ -69,43 +54,17 @@ class Metrics {
    /**
     * Writes the given <code>text</code> to a log with the given label.
     */
    private void logText(String label, String text) throws IOException {
    private void logText(String label, String text) throws TestException {
        try {
            File file = File.createTempFile(label, "txt");
            PrintStream ps = new PrintStream(file);
            ps.print(text);
            try (FileInputStreamSource dataStream = new FileInputStreamSource(file)) {
                mLogs.addTestLog(label, LogDataType.TEXT, dataStream);
            }
        } catch (IOException e) {
            throw new TestException(e);
        }

    /**
     * Returns the pid for the process with the given name.
     */
    private int getPidForProcess(String name)
            throws DeviceNotAvailableException, IOException, MetricsException {
        String psout = mDevice.executeShellCommand("ps -A -o PID,CMD");
        Scanner sc = new Scanner(psout);
        try {
            // ps output is of the form:
            //  PID CMD
            //    1 init
            //    2 kthreadd
            //    ...
            // 9693 ps
            sc.nextLine();
            while (sc.hasNextLine()) {
                int pid = sc.nextInt();
                String cmd = sc.next();

                if (name.equals(cmd)) {
                    return pid;
                }
            }
        } catch (InputMismatchException e) {
            throw new MetricsException("unexpected ps output format: " + psout, e);
        }

        throw new MetricsException("failed to get pid for process " + name);
    }

    /**
@@ -116,11 +75,11 @@ class Metrics {
     *
     * @param label prefix to use for metrics and logs output for this sample.
     */
    void sample(String label) throws DeviceNotAvailableException, IOException, MetricsException {
    void sample(String label) throws TestException {
        // adb root access is required to get showmap
        mDevice.enableAdbRoot();

        int pid = getPidForProcess("system_server");
        int pid = mDevice.getPidForProcess("system_server");

        // Read showmap for system server and add it as a test log
        String showmap = mDevice.executeShellCommand("showmap " + pid);
@@ -146,7 +105,7 @@ class Metrics {
            mMetrics.addTestMetric(label + ".system_server.rss", Long.toString(rss));
            mMetrics.addTestMetric(label + ".system_server.pss", Long.toString(pss));
        } catch (InputMismatchException e) {
            throw new MetricsException("unexpected showmap format", e);
            throw new TestException("unexpected showmap format", e);
        }

        // Run debuggerd -j to get GC stats for system server and add it as a
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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 com.android.tests.sysmem.host;

/**
 * Exception thrown in case of unexpected error encountered when executing the
 * test.
 */
class TestException extends Exception {
    TestException(Exception cause) {
        super(cause);
    }

    TestException(String msg) {
        super(msg);
    }

    TestException(String msg, Exception cause) {
        super(msg, cause);
    }
}