Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.tradefed.suite.checker; |
| 17 | |
| 18 | import com.android.tradefed.device.DeviceNotAvailableException; |
Julien Desprez | 5238817 | 2019-07-31 16:32:49 -0700 | [diff] [blame] | 19 | import com.android.tradefed.device.DeviceProperties; |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 20 | import com.android.tradefed.device.ITestDevice; |
| 21 | import com.android.tradefed.log.LogUtil.CLog; |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 22 | import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus; |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 23 | |
| 24 | /** Checks if system server appears to be running out of FDs. */ |
| 25 | public class SystemServerFileDescriptorChecker implements ISystemStatusChecker { |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 26 | |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 27 | /** Process will fail to allocate beyond 1024, so heuristic considers 900 a bad state */ |
| 28 | private static final int MAX_EXPECTED_FDS = 900; |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 29 | private static final String USER_BUILD = "user"; |
| 30 | |
| 31 | private String mBuildType = null; |
| 32 | |
| 33 | @Override |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 34 | public StatusCheckerResult preExecutionCheck(ITestDevice device) |
| 35 | throws DeviceNotAvailableException { |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 36 | if (mBuildType == null) { |
| 37 | // build type not initialized yet, check on device |
Julien Desprez | 5238817 | 2019-07-31 16:32:49 -0700 | [diff] [blame] | 38 | mBuildType = device.getProperty(DeviceProperties.BUILD_TYPE); |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 39 | } |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 40 | return new StatusCheckerResult(CheckStatus.SUCCESS); |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 41 | } |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 42 | |
| 43 | /** {@inheritDoc} */ |
| 44 | @Override |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 45 | public StatusCheckerResult postExecutionCheck(ITestDevice device) |
| 46 | throws DeviceNotAvailableException { |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 47 | if (USER_BUILD.equals(mBuildType)) { |
| 48 | CLog.d("Skipping system_server fd check on user builds."); |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 49 | return new StatusCheckerResult(CheckStatus.SUCCESS); |
Guang Zhu | a61d586 | 2017-03-21 18:53:13 -0700 | [diff] [blame] | 50 | } |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 51 | Integer pid = getIntegerFromCommand(device, "pidof system_server"); |
| 52 | if (pid == null) { |
| 53 | CLog.d("Unable to find system_server pid."); |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 54 | return new StatusCheckerResult(CheckStatus.SUCCESS); |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Chris Craik | 397c1a8 | 2017-03-14 14:49:23 -0700 | [diff] [blame] | 57 | Integer fds = getIntegerFromCommand(device, "su root ls /proc/" + pid + "/fd | wc -w"); |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 58 | if (fds == null) { |
| 59 | CLog.d("Unable to query system_server fd count."); |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 60 | return new StatusCheckerResult(CheckStatus.SUCCESS); |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (fds > MAX_EXPECTED_FDS) { |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 64 | 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 Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 69 | } |
Julien Desprez | 3e4d47d | 2018-05-23 15:53:40 -0700 | [diff] [blame] | 70 | return new StatusCheckerResult(CheckStatus.SUCCESS); |
Chris Craik | 995602d | 2017-03-03 16:45:05 -0800 | [diff] [blame] | 71 | } |
| 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 | } |