blob: c058d48053ffc338b46f68605e2bae23f4acbe76 [file] [log] [blame]
Chris Craik995602d2017-03-03 16:45:05 -08001/*
2 * Copyright (C) 2017 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 */
16package com.android.tradefed.suite.checker;
17
18import com.android.tradefed.device.DeviceNotAvailableException;
Julien Desprez52388172019-07-31 16:32:49 -070019import com.android.tradefed.device.DeviceProperties;
Chris Craik995602d2017-03-03 16:45:05 -080020import com.android.tradefed.device.ITestDevice;
21import com.android.tradefed.log.LogUtil.CLog;
Julien Desprez3e4d47d2018-05-23 15:53:40 -070022import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus;
Chris Craik995602d2017-03-03 16:45:05 -080023
24/** Checks if system server appears to be running out of FDs. */
25public class SystemServerFileDescriptorChecker implements ISystemStatusChecker {
Guang Zhua61d5862017-03-21 18:53:13 -070026
Chris Craik995602d2017-03-03 16:45:05 -080027 /** Process will fail to allocate beyond 1024, so heuristic considers 900 a bad state */
28 private static final int MAX_EXPECTED_FDS = 900;
Guang Zhua61d5862017-03-21 18:53:13 -070029 private static final String USER_BUILD = "user";
30
31 private String mBuildType = null;
32
33 @Override
Julien Desprez3e4d47d2018-05-23 15:53:40 -070034 public StatusCheckerResult preExecutionCheck(ITestDevice device)
35 throws DeviceNotAvailableException {
Guang Zhua61d5862017-03-21 18:53:13 -070036 if (mBuildType == null) {
37 // build type not initialized yet, check on device
Julien Desprez52388172019-07-31 16:32:49 -070038 mBuildType = device.getProperty(DeviceProperties.BUILD_TYPE);
Guang Zhua61d5862017-03-21 18:53:13 -070039 }
Julien Desprez3e4d47d2018-05-23 15:53:40 -070040 return new StatusCheckerResult(CheckStatus.SUCCESS);
Guang Zhua61d5862017-03-21 18:53:13 -070041 }
Chris Craik995602d2017-03-03 16:45:05 -080042
43 /** {@inheritDoc} */
44 @Override
Julien Desprez3e4d47d2018-05-23 15:53:40 -070045 public StatusCheckerResult postExecutionCheck(ITestDevice device)
46 throws DeviceNotAvailableException {
Guang Zhua61d5862017-03-21 18:53:13 -070047 if (USER_BUILD.equals(mBuildType)) {
48 CLog.d("Skipping system_server fd check on user builds.");
Julien Desprez3e4d47d2018-05-23 15:53:40 -070049 return new StatusCheckerResult(CheckStatus.SUCCESS);
Guang Zhua61d5862017-03-21 18:53:13 -070050 }
Chris Craik995602d2017-03-03 16:45:05 -080051 Integer pid = getIntegerFromCommand(device, "pidof system_server");
52 if (pid == null) {
53 CLog.d("Unable to find system_server pid.");
Julien Desprez3e4d47d2018-05-23 15:53:40 -070054 return new StatusCheckerResult(CheckStatus.SUCCESS);
Chris Craik995602d2017-03-03 16:45:05 -080055 }
56
Chris Craik397c1a82017-03-14 14:49:23 -070057 Integer fds = getIntegerFromCommand(device, "su root ls /proc/" + pid + "/fd | wc -w");
Chris Craik995602d2017-03-03 16:45:05 -080058 if (fds == null) {
59 CLog.d("Unable to query system_server fd count.");
Julien Desprez3e4d47d2018-05-23 15:53:40 -070060 return new StatusCheckerResult(CheckStatus.SUCCESS);
Chris Craik995602d2017-03-03 16:45:05 -080061 }
62
63 if (fds > MAX_EXPECTED_FDS) {
Julien Desprez3e4d47d2018-05-23 15:53:40 -070064 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.FAILED);
65 String message = String.format("FDs currently allocated in system server %s", fds);
66 CLog.w(message);
67 result.setErrorMessage(message);
68 return result;
Chris Craik995602d2017-03-03 16:45:05 -080069 }
Julien Desprez3e4d47d2018-05-23 15:53:40 -070070 return new StatusCheckerResult(CheckStatus.SUCCESS);
Chris Craik995602d2017-03-03 16:45:05 -080071 }
72
73 private static Integer getIntegerFromCommand(ITestDevice device, String command)
74 throws DeviceNotAvailableException {
75 String output = device.executeShellCommand(command);
76 if (output == null) {
77 CLog.w("no shell output for command: " + command);
78 return null;
79 }
80 output = output.trim();
81 try {
82 return Integer.parseInt(output);
83 } catch (NumberFormatException e) {
84 CLog.w("unable to parse result of '" + command + "' : " + output);
85 return null;
86 }
87 }
88}