Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 16 | |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 17 | package com.android.tradefed.command; |
| 18 | |
Omari Stephens | 0f92122 | 2011-10-20 19:40:07 -0700 | [diff] [blame] | 19 | import com.android.ddmlib.DdmPreferences; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 20 | import com.android.ddmlib.Log; |
Brett Chabot | 73029de | 2010-08-12 14:06:07 -0700 | [diff] [blame] | 21 | import com.android.ddmlib.Log.LogLevel; |
Brett Chabot | 9c27c90 | 2013-09-27 11:00:56 -0700 | [diff] [blame^] | 22 | import com.android.tradefed.command.remote.RemoteClient; |
| 23 | import com.android.tradefed.command.remote.RemoteManager; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 24 | import com.android.tradefed.config.ConfigurationException; |
| 25 | import com.android.tradefed.config.ConfigurationFactory; |
Steve Moyer | 98632ab | 2013-09-10 16:52:37 -0700 | [diff] [blame] | 26 | import com.android.tradefed.config.GlobalConfiguration; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 27 | import com.android.tradefed.config.IConfiguration; |
| 28 | import com.android.tradefed.config.IConfigurationFactory; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 29 | import com.android.tradefed.device.DeviceNotAvailableException; |
| 30 | import com.android.tradefed.device.DeviceUnresponsiveException; |
| 31 | import com.android.tradefed.device.IDeviceManager; |
Brett Chabot | d850c48 | 2011-02-25 14:17:26 -0800 | [diff] [blame] | 32 | import com.android.tradefed.device.IDeviceManager.FreeDeviceState; |
Brett Chabot | 0eabebe | 2011-09-12 16:15:42 -0700 | [diff] [blame] | 33 | import com.android.tradefed.device.ITestDevice; |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 34 | import com.android.tradefed.invoker.IRescheduler; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 35 | import com.android.tradefed.invoker.ITestInvocation; |
| 36 | import com.android.tradefed.invoker.TestInvocation; |
Omari Stephens | 0f92122 | 2011-10-20 19:40:07 -0700 | [diff] [blame] | 37 | import com.android.tradefed.log.LogRegistry; |
Omari Stephens | 463dda6 | 2011-11-21 17:21:42 -0800 | [diff] [blame] | 38 | import com.android.tradefed.log.LogUtil.CLog; |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 39 | import com.android.tradefed.util.ArrayUtil; |
Brett Chabot | c5ea26e | 2010-08-09 19:45:40 -0700 | [diff] [blame] | 40 | import com.android.tradefed.util.ConditionPriorityBlockingQueue; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 41 | import com.android.tradefed.util.TableFormatter; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 42 | |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 43 | import java.io.IOException; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 44 | import java.io.PrintWriter; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 46 | import java.util.Arrays; |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 47 | import java.util.Collections; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 48 | import java.util.Comparator; |
| 49 | import java.util.HashSet; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 50 | import java.util.LinkedHashSet; |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 51 | import java.util.LinkedList; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 52 | import java.util.List; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 53 | import java.util.ListIterator; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 54 | import java.util.Set; |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 55 | import java.util.concurrent.CountDownLatch; |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 56 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 57 | import java.util.concurrent.TimeUnit; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
Brett Chabot | 4e6f367 | 2011-03-02 17:43:21 -0800 | [diff] [blame] | 60 | * A scheduler for running TradeFederation commands across all available devices. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 61 | * <p/> |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 62 | * Will attempt to prioritize commands to run based on a total running count of their execution |
| 63 | * time. e.g. infrequent or fast running commands will get prioritized over long running commands. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 64 | * <p/> |
| 65 | * Runs forever in background until shutdown. |
| 66 | */ |
Brett Chabot | 0283cb4 | 2010-08-07 16:12:17 -0700 | [diff] [blame] | 67 | public class CommandScheduler extends Thread implements ICommandScheduler { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 68 | |
Brett Chabot | 4e6f367 | 2011-03-02 17:43:21 -0800 | [diff] [blame] | 69 | /** the queue of commands ready to be executed. */ |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 70 | private ConditionPriorityBlockingQueue<ExecutableCommand> mCommandQueue; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 71 | |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 72 | /** |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 73 | * The thread-safe list of all active executable commands. |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 74 | */ |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 75 | private List<ExecutableCommand> mAllCommands; |
| 76 | |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 77 | /** list of active invocation threads */ |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 78 | private Set<InvocationThread> mInvocationThreads; |
| 79 | |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 80 | /** timer for scheduling commands to be re-queued for execution */ |
| 81 | private ScheduledThreadPoolExecutor mCommandTimer; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 82 | |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 83 | private RemoteClient mRemoteClient = null; |
| 84 | private RemoteManager mRemoteManager = null; |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 85 | |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 86 | /** latch used to notify other threads that this thread is running */ |
| 87 | private final CountDownLatch mRunLatch; |
| 88 | |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Delay time in ms for adding a command back to the queue if it failed to allocate a device. |
| 91 | */ |
| 92 | private static final int NO_DEVICE_DELAY_TIME = 20; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 93 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 94 | /** used to assign unique ids to each CommandTracker created */ |
| 95 | private int mCurrentCommandId = 0; |
| 96 | |
Brett Chabot | 29058a9 | 2013-01-10 09:40:48 -0800 | [diff] [blame] | 97 | /** flag for instructing scheduler to exit when no commands are present */ |
| 98 | private boolean mShutdownOnEmpty = false; |
| 99 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 100 | private enum CommandState { |
| 101 | WAITING_FOR_DEVICE("Wait_for_device"), |
| 102 | EXECUTING("Executing"), |
| 103 | SLEEPING("Sleeping"); |
| 104 | |
| 105 | private String mDisplayName; |
| 106 | |
| 107 | CommandState(String displayName) { |
| 108 | mDisplayName = displayName; |
| 109 | } |
| 110 | |
| 111 | public String getDisplayName() { |
| 112 | return mDisplayName; |
| 113 | } |
| 114 | } |
| 115 | |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 116 | /** |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 117 | * Represents one active command added to the scheduler. Will track total execution time of all |
| 118 | * instances of this command |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 119 | */ |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 120 | private static class CommandTracker { |
| 121 | private final int mId; |
Brett Chabot | 2d84551 | 2010-08-10 13:06:06 -0700 | [diff] [blame] | 122 | private final String[] mArgs; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 123 | |
Brett Chabot | 4e6f367 | 2011-03-02 17:43:21 -0800 | [diff] [blame] | 124 | /** the total amount of time this command was executing. Used to prioritize */ |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 125 | private long mTotalExecTime = 0; |
| 126 | |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 127 | CommandTracker(int id, String[] args) { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 128 | mId = id; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 129 | mArgs = args; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | synchronized void incrementExecTime(long execTime) { |
| 133 | mTotalExecTime += execTime; |
| 134 | } |
Brett Chabot | 2d84551 | 2010-08-10 13:06:06 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 137 | * @return the total amount of execution time for this command. |
Brett Chabot | 2d84551 | 2010-08-10 13:06:06 -0700 | [diff] [blame] | 138 | */ |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 139 | synchronized long getTotalExecTime() { |
| 140 | return mTotalExecTime; |
Brett Chabot | 2d84551 | 2010-08-10 13:06:06 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the full list of config arguments associated with this command. |
| 145 | */ |
| 146 | String[] getArgs() { |
| 147 | return mArgs; |
| 148 | } |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 149 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 150 | int getId() { |
| 151 | return mId; |
| 152 | } |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 156 | * Represents one instance of a command to be executed. |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 157 | */ |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 158 | private class ExecutableCommand { |
| 159 | private final CommandTracker mCmdTracker; |
| 160 | private final IConfiguration mConfig; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 161 | private final boolean mRescheduled; |
| 162 | private final long mCreationTime; |
| 163 | private CommandState mState; |
| 164 | private Long mSleepTime; |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 165 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 166 | private ExecutableCommand(CommandTracker tracker, IConfiguration config, |
| 167 | boolean rescheduled) { |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 168 | mConfig = config; |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 169 | mCmdTracker = tracker; |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 170 | mRescheduled = rescheduled; |
| 171 | mCreationTime = System.currentTimeMillis(); |
| 172 | mState = CommandState.WAITING_FOR_DEVICE; |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Gets the {@link IConfiguration} for this command instance |
| 177 | */ |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 178 | public IConfiguration getConfiguration() { |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 179 | return mConfig; |
| 180 | } |
| 181 | |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Gets the associated {@link CommandTracker}. |
| 184 | */ |
| 185 | CommandTracker getCommandTracker() { |
| 186 | return mCmdTracker; |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 187 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Callback to inform listener that command has started execution. |
| 191 | */ |
| 192 | void commandStarted() { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 193 | mState = CommandState.EXECUTING; |
| 194 | mSleepTime = null; |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 195 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 196 | |
| 197 | public void commandFinished(long elapsedTime) { |
| 198 | getCommandTracker().incrementExecTime(elapsedTime); |
| 199 | mAllCommands.remove(this); |
| 200 | } |
| 201 | |
| 202 | public boolean isRescheduled() { |
| 203 | return mRescheduled; |
| 204 | } |
| 205 | |
| 206 | public long getCreationTime() { |
| 207 | return mCreationTime; |
| 208 | } |
| 209 | |
| 210 | public boolean isLoopMode() { |
| 211 | return mConfig.getCommandOptions().isLoopMode(); |
| 212 | } |
| 213 | |
| 214 | public CommandState getState() { |
| 215 | return mState; |
| 216 | } |
| 217 | |
| 218 | public void setSleepState(Long delayTime) { |
| 219 | mSleepTime = delayTime; |
| 220 | mState = CommandState.SLEEPING; |
| 221 | } |
| 222 | |
| 223 | public void setWaitState() { |
| 224 | mState = CommandState.WAITING_FOR_DEVICE; |
| 225 | mSleepTime = null; |
| 226 | } |
| 227 | |
| 228 | public Long getSleepTime() { |
| 229 | return mSleepTime; |
| 230 | } |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Brett Chabot | 4e6f367 | 2011-03-02 17:43:21 -0800 | [diff] [blame] | 234 | * A {@link IRescheduler} that will add a command back to the queue. |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 235 | */ |
| 236 | private class Rescheduler implements IRescheduler { |
| 237 | |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 238 | private CommandTracker mCmdTracker; |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 239 | |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 240 | Rescheduler(CommandTracker cmdTracker) { |
| 241 | mCmdTracker = cmdTracker; |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * {@inheritDoc} |
| 246 | */ |
| 247 | @Override |
| 248 | public boolean scheduleConfig(IConfiguration config) { |
Brett Chabot | 014fac8 | 2012-06-01 16:45:21 -0700 | [diff] [blame] | 249 | // force loop mode to off, otherwise each rescheduled config will be treated as |
| 250 | // a new command and added back to queue |
| 251 | config.getCommandOptions().setLoopMode(false); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 252 | ExecutableCommand rescheduledCmd = createExecutableCommand(mCmdTracker, config, true); |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 253 | return addExecCommandToQueue(rescheduledCmd, 0); |
Brett Chabot | 1c82918 | 2011-01-12 19:49:15 -0800 | [diff] [blame] | 254 | } |
Eric Rowe | a3b8585 | 2012-07-16 15:07:49 -0700 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * {@inheritDoc} |
| 258 | */ |
| 259 | @Override |
| 260 | public boolean rescheduleCommand() { |
| 261 | try { |
| 262 | IConfiguration config = getConfigFactory().createConfigurationFromArgs( |
| 263 | mCmdTracker.getArgs()); |
| 264 | ExecutableCommand execCmd = createExecutableCommand(mCmdTracker, config, true); |
Eric Rowe | dba4d37 | 2013-03-08 16:06:38 -0800 | [diff] [blame] | 265 | return addExecCommandToQueue(execCmd, config.getCommandOptions().getMinLoopTime()); |
Eric Rowe | a3b8585 | 2012-07-16 15:07:49 -0700 | [diff] [blame] | 266 | } catch (ConfigurationException e) { |
| 267 | // FIXME: do this with jline somehow for ANSI support |
| 268 | // note: make sure not to log (aka record) this line, as (args) may contain |
| 269 | // passwords. |
| 270 | System.out.println(String.format("Error while processing args: %s", |
| 271 | Arrays.toString(mCmdTracker.getArgs()))); |
| 272 | System.out.println(e.getMessage()); |
| 273 | System.out.println(); |
| 274 | return false; |
| 275 | } |
| 276 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 280 | * Comparator for {@link ExecutableCommand}. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 281 | * <p/> |
Eric Rowe | a3b8585 | 2012-07-16 15:07:49 -0700 | [diff] [blame] | 282 | * Delegates to {@link CommandTrackerTimeComparator}. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 283 | */ |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 284 | private static class ExecutableCommandComparator implements Comparator<ExecutableCommand> { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 285 | CommandTrackerTimeComparator mTrackerComparator = new CommandTrackerTimeComparator(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 286 | |
| 287 | /** |
| 288 | * {@inheritDoc} |
| 289 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 290 | @Override |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 291 | public int compare(ExecutableCommand c1, ExecutableCommand c2) { |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 292 | return mTrackerComparator.compare(c1.getCommandTracker(), c2.getCommandTracker()); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Comparator for {@link CommandTracker}. |
| 298 | * <p/> |
| 299 | * Compares by mTotalExecTime, prioritizing configs with lower execution time |
| 300 | */ |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 301 | private static class CommandTrackerTimeComparator implements Comparator<CommandTracker> { |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 302 | |
| 303 | @Override |
| 304 | public int compare(CommandTracker c1, CommandTracker c2) { |
| 305 | if (c1.getTotalExecTime() == c2.getTotalExecTime()) { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 306 | return 0; |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 307 | } else if (c1.getTotalExecTime() < c2.getTotalExecTime()) { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 308 | return -1; |
| 309 | } else { |
| 310 | return 1; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 315 | /** |
| 316 | * Comparator for {@link CommandTracker}. |
| 317 | * <p/> |
| 318 | * Compares by id. |
| 319 | */ |
| 320 | private static class CommandTrackerIdComparator implements Comparator<CommandTracker> { |
| 321 | |
| 322 | @Override |
| 323 | public int compare(CommandTracker c1, CommandTracker c2) { |
| 324 | if (c1.getId() == c2.getId()) { |
| 325 | return 0; |
| 326 | } else if (c1.getId() < c2.getId()) { |
| 327 | return -1; |
| 328 | } else { |
| 329 | return 1; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 334 | private class InvocationThread extends Thread { |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 335 | private final IDeviceManager mManager; |
| 336 | private final ITestDevice mDevice; |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 337 | private final ExecutableCommand mCmd; |
Brett Chabot | c0554a6 | 2013-01-31 18:38:19 -0800 | [diff] [blame] | 338 | private final ITestInvocation mInvocation; |
Omari Stephens | 1ee376d | 2011-10-20 18:48:55 -0700 | [diff] [blame] | 339 | private long mStartTime = -1; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 340 | |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 341 | public InvocationThread(String name, IDeviceManager manager, ITestDevice device, |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 342 | ExecutableCommand command) { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 343 | // create a thread group so LoggerRegistry can identify this as an invocationThread |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 344 | super(new ThreadGroup(name), name); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 345 | mManager = manager; |
| 346 | mDevice = device; |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 347 | mCmd = command; |
Omari Stephens | a9a4317 | 2011-03-05 18:02:00 -0800 | [diff] [blame] | 348 | mInvocation = createRunInstance(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Omari Stephens | 1ee376d | 2011-10-20 18:48:55 -0700 | [diff] [blame] | 351 | public long getStartTime() { |
| 352 | return mStartTime; |
| 353 | } |
| 354 | |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 355 | @Override |
| 356 | public void run() { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 357 | FreeDeviceState deviceState = FreeDeviceState.AVAILABLE; |
Omari Stephens | 1ee376d | 2011-10-20 18:48:55 -0700 | [diff] [blame] | 358 | mStartTime = System.currentTimeMillis(); |
Brett Chabot | c0554a6 | 2013-01-31 18:38:19 -0800 | [diff] [blame] | 359 | ITestInvocation instance = getInvocation(); |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 360 | IConfiguration config = mCmd.getConfiguration(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 361 | try { |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 362 | mCmd.commandStarted(); |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 363 | instance.invoke(mDevice, config, new Rescheduler(mCmd.getCommandTracker())); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 364 | } catch (DeviceUnresponsiveException e) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 365 | CLog.w("Device %s is unresponsive. Reason: %s", mDevice.getSerialNumber(), |
| 366 | e.getMessage()); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 367 | deviceState = FreeDeviceState.UNRESPONSIVE; |
| 368 | } catch (DeviceNotAvailableException e) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 369 | CLog.w("Device %s is not available. Reason: %s", mDevice.getSerialNumber(), |
| 370 | e.getMessage()); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 371 | deviceState = FreeDeviceState.UNAVAILABLE; |
Brett Chabot | a17af5d | 2010-08-23 13:51:17 -0700 | [diff] [blame] | 372 | } catch (FatalHostError e) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 373 | CLog.logAndDisplay(LogLevel.ERROR, "Fatal error occurred: %s, shutting down", |
| 374 | e.getMessage()); |
Brett Chabot | a17af5d | 2010-08-23 13:51:17 -0700 | [diff] [blame] | 375 | if (e.getCause() != null) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 376 | CLog.e(e.getCause()); |
Brett Chabot | a17af5d | 2010-08-23 13:51:17 -0700 | [diff] [blame] | 377 | } |
| 378 | shutdown(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 379 | } catch (Throwable e) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 380 | CLog.e(e); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 381 | } finally { |
Omari Stephens | 1ee376d | 2011-10-20 18:48:55 -0700 | [diff] [blame] | 382 | long elapsedTime = System.currentTimeMillis() - mStartTime; |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 383 | CLog.i("Updating command '%s' with elapsed time %d ms", |
| 384 | getArgString(mCmd.getCommandTracker().getArgs()), elapsedTime); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 385 | mCmd.commandFinished(elapsedTime); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 386 | mManager.freeDevice(mDevice, deviceState); |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 387 | remoteFreeDevice(mDevice); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 388 | removeInvocationThread(this); |
| 389 | } |
| 390 | } |
| 391 | |
Brett Chabot | c0554a6 | 2013-01-31 18:38:19 -0800 | [diff] [blame] | 392 | ITestInvocation getInvocation() { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 393 | return mInvocation; |
| 394 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 395 | |
| 396 | ITestDevice getDevice() { |
| 397 | return mDevice; |
| 398 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Brett Chabot | ccf00ea | 2010-08-26 16:04:33 -0700 | [diff] [blame] | 402 | * Creates a {@link CommandScheduler}. |
Omari Stephens | 3c1f4dd | 2011-10-24 15:12:18 -0700 | [diff] [blame] | 403 | * <p /> |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 404 | * Note: logging is initialized here. We assume that {@link CommandScheduler#start} will be |
Omari Stephens | 3c1f4dd | 2011-10-24 15:12:18 -0700 | [diff] [blame] | 405 | * called, so that we can clean logs up at the end of the {@link CommandScheduler#run} method. |
| 406 | * In particular, this means that a leak will result if a {@link CommandScheduler} instance is |
| 407 | * instantiated but not started. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 408 | */ |
Omari Stephens | 5f75084 | 2011-04-01 12:28:13 -0700 | [diff] [blame] | 409 | public CommandScheduler() { |
Omari Stephens | 3c1f4dd | 2011-10-24 15:12:18 -0700 | [diff] [blame] | 410 | initLogging(); |
| 411 | |
Omari Stephens | f9060ec | 2012-11-27 19:52:39 -0800 | [diff] [blame] | 412 | initDeviceManager(); |
Omari Stephens | 3c1f4dd | 2011-10-24 15:12:18 -0700 | [diff] [blame] | 413 | |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 414 | mCommandQueue = new ConditionPriorityBlockingQueue<ExecutableCommand>( |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 415 | new ExecutableCommandComparator()); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 416 | mAllCommands = Collections.synchronizedList(new LinkedList<ExecutableCommand>()); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 417 | mInvocationThreads = new HashSet<InvocationThread>(); |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 418 | // use a ScheduledThreadPoolExecutorTimer as a single-threaded timer. This class |
| 419 | // is used instead of a java.util.Timer because it offers advanced shutdown options |
| 420 | mCommandTimer = new ScheduledThreadPoolExecutor(1); |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 421 | mRunLatch = new CountDownLatch(1); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
Brett Chabot | b50ceb8 | 2012-05-11 12:45:15 -0700 | [diff] [blame] | 425 | * Initialize the device manager, optionally using a global device filter if specified. |
| 426 | */ |
Omari Stephens | f9060ec | 2012-11-27 19:52:39 -0800 | [diff] [blame] | 427 | void initDeviceManager() { |
| 428 | getDeviceManager().init(); |
Brett Chabot | b50ceb8 | 2012-05-11 12:45:15 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 432 | * Factory method for creating a {@link TestInvocation}. |
| 433 | * |
| 434 | * @return the {@link ITestInvocation} to use |
| 435 | */ |
| 436 | ITestInvocation createRunInstance() { |
| 437 | return new TestInvocation(); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Factory method for getting a reference to the {@link IDeviceManager} |
| 442 | * |
| 443 | * @return the {@link IDeviceManager} to use |
| 444 | */ |
| 445 | IDeviceManager getDeviceManager() { |
Steve Moyer | 98632ab | 2013-09-10 16:52:37 -0700 | [diff] [blame] | 446 | return GlobalConfiguration.getDeviceManagerInstance(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Factory method for getting a reference to the {@link IConfigurationFactory} |
| 451 | * |
| 452 | * @return the {@link IConfigurationFactory} to use |
| 453 | */ |
| 454 | IConfigurationFactory getConfigFactory() { |
| 455 | return ConfigurationFactory.getInstance(); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * The main execution block of this thread. |
| 460 | */ |
| 461 | @Override |
| 462 | public void run() { |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 463 | try { |
| 464 | // Notify other threads that we're running. |
| 465 | mRunLatch.countDown(); |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 466 | |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 467 | IDeviceManager manager = getDeviceManager(); |
| 468 | while (!isShutdown()) { |
| 469 | ExecutableCommand cmd = dequeueConfigCommand(); |
| 470 | if (cmd != null) { |
| 471 | ITestDevice device = manager.allocateDevice(0, cmd.getConfiguration() |
| 472 | .getDeviceRequirements()); |
| 473 | if (device != null) { |
| 474 | // Spawn off a thread to perform the invocation |
| 475 | InvocationThread invThread = startInvocation(manager, device, cmd); |
| 476 | addInvocationThread(invThread); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 477 | if (cmd.isLoopMode()) { |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 478 | addNewExecCommandToQueue(cmd.getCommandTracker()); |
| 479 | } |
| 480 | } else { |
| 481 | // no device available for command, put back in queue |
| 482 | // increment exec time to ensure fair scheduling among commands when devices |
| 483 | // are scarce |
| 484 | cmd.getCommandTracker().incrementExecTime(1); |
| 485 | addExecCommandToQueue(cmd, NO_DEVICE_DELAY_TIME); |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 486 | } |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 487 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 488 | } |
Brett Chabot | 29058a9 | 2013-01-10 09:40:48 -0800 | [diff] [blame] | 489 | mCommandTimer.shutdown(); |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 490 | CLog.i("Waiting for invocation threads to complete"); |
| 491 | List<InvocationThread> threadListCopy; |
| 492 | synchronized (this) { |
| 493 | threadListCopy = new ArrayList<InvocationThread>(mInvocationThreads.size()); |
| 494 | threadListCopy.addAll(mInvocationThreads); |
| 495 | } |
| 496 | for (Thread thread : threadListCopy) { |
| 497 | waitForThread(thread); |
| 498 | } |
| 499 | closeRemoteClient(); |
| 500 | if (mRemoteManager != null) { |
| 501 | mRemoteManager.cancel(); |
| 502 | } |
| 503 | exit(manager); |
| 504 | cleanUp(); |
| 505 | CLog.logAndDisplay(LogLevel.INFO, "All done"); |
| 506 | } finally { |
| 507 | // Make sure that we don't quit with messages still in the buffers |
| 508 | System.err.flush(); |
| 509 | System.out.flush(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 510 | } |
Omari Stephens | 4c21783 | 2012-04-19 18:58:30 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * {@inheritDoc} |
| 515 | */ |
| 516 | @Override |
| 517 | public void await() throws InterruptedException { |
| 518 | while (mRunLatch.getCount() > 0) { |
| 519 | mRunLatch.await(); |
Brett Chabot | ccf00ea | 2010-08-26 16:04:33 -0700 | [diff] [blame] | 520 | } |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | private void closeRemoteClient() { |
| 524 | if (mRemoteClient != null) { |
| 525 | try { |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 526 | mRemoteClient.sendClose(); |
| 527 | mRemoteClient.close(); |
| 528 | } catch (IOException e) { |
| 529 | // ignore |
| 530 | } |
| 531 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Brett Chabot | ccf00ea | 2010-08-26 16:04:33 -0700 | [diff] [blame] | 534 | private void waitForThread(Thread thread) { |
| 535 | try { |
| 536 | thread.join(); |
| 537 | } catch (InterruptedException e) { |
| 538 | // ignore |
| 539 | waitForThread(thread); |
| 540 | } |
| 541 | } |
| 542 | |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 543 | private void exit(IDeviceManager manager) { |
| 544 | if (manager != null) { |
| 545 | manager.terminate(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /** |
Brett Chabot | 0283cb4 | 2010-08-07 16:12:17 -0700 | [diff] [blame] | 550 | * {@inheritDoc} |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 551 | */ |
Brett Chabot | e7e335e | 2011-02-16 14:51:03 -0800 | [diff] [blame] | 552 | @Override |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 553 | public boolean addCommand(String[] args) { |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 554 | return addCommand(args, 0); |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 555 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 556 | |
| 557 | /** |
| 558 | * {@inheritDoc} |
| 559 | */ |
| 560 | @Override |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 561 | public boolean addCommand(String[] args, long totalExecTime) { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 562 | try { |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 563 | IConfiguration config = getConfigFactory().createConfigurationFromArgs(args); |
| 564 | if (config.getCommandOptions().isHelpMode()) { |
Brett Chabot | 77de04a | 2011-04-25 12:59:26 -0700 | [diff] [blame] | 565 | getConfigFactory().printHelpForConfig(args, true, System.out); |
| 566 | } else if (config.getCommandOptions().isFullHelpMode()) { |
| 567 | getConfigFactory().printHelpForConfig(args, false, System.out); |
Omari Stephens | 463dda6 | 2011-11-21 17:21:42 -0800 | [diff] [blame] | 568 | } else if (config.getCommandOptions().isDryRunMode()) { |
Omari Stephens | e2109e9 | 2013-02-21 18:49:33 -0800 | [diff] [blame] | 569 | if (config.getCommandOptions().isNoisyDryRunMode()) { |
| 570 | CLog.logAndDisplay(LogLevel.DEBUG, "DRY RUN: %s", Arrays.toString(args)); |
| 571 | } else { |
| 572 | CLog.d("Dry run mode; skipping adding command: %s", Arrays.toString(args)); |
| 573 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 574 | } else { |
Brett Chabot | 0dc54f4 | 2012-08-20 12:15:20 -0700 | [diff] [blame] | 575 | config.validateOptions(); |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 576 | |
Brett Chabot | 249ef61 | 2013-02-05 16:24:26 -0800 | [diff] [blame] | 577 | if (config.getCommandOptions().runOnAllDevices()) { |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 578 | addCommandForAllDevices(totalExecTime, args); |
Brett Chabot | 249ef61 | 2013-02-05 16:24:26 -0800 | [diff] [blame] | 579 | } else { |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 580 | CommandTracker cmdTracker = createCommandTracker(args); |
| 581 | cmdTracker.incrementExecTime(totalExecTime); |
Brett Chabot | 249ef61 | 2013-02-05 16:24:26 -0800 | [diff] [blame] | 582 | ExecutableCommand cmdInstance = createExecutableCommand(cmdTracker, config, false); |
| 583 | addExecCommandToQueue(cmdInstance, 0); |
| 584 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 585 | return true; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 586 | } |
| 587 | } catch (ConfigurationException e) { |
Omari Stephens | 3e7f86a | 2011-11-14 19:58:18 -0800 | [diff] [blame] | 588 | // FIXME: do this with jline somehow for ANSI support |
Omari Stephens | 8bda56e | 2011-11-21 17:21:42 -0800 | [diff] [blame] | 589 | // note: make sure not to log (aka record) this line, as (args) may contain passwords. |
| 590 | System.out.println(String.format("Error while processing args: %s", |
| 591 | Arrays.toString(args))); |
Omari Stephens | 3e7f86a | 2011-11-14 19:58:18 -0800 | [diff] [blame] | 592 | System.out.println(e.getMessage()); |
| 593 | System.out.println(); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 594 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 595 | return false; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /** |
Brett Chabot | 249ef61 | 2013-02-05 16:24:26 -0800 | [diff] [blame] | 599 | * Creates a new command for each connected device, and adds each to the queue. |
| 600 | * <p/> |
| 601 | * Note this won't have the desired effect if user has specified other |
| 602 | * conflicting {@link IConfiguration#getDeviceRequirements()}in the command. |
| 603 | */ |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 604 | private void addCommandForAllDevices(long totalExecTime, String[] args) |
Brett Chabot | 249ef61 | 2013-02-05 16:24:26 -0800 | [diff] [blame] | 605 | throws ConfigurationException { |
| 606 | Set<String> devices = new HashSet<String>(); |
| 607 | devices.addAll(getDeviceManager().getAvailableDevices()); |
| 608 | devices.addAll(getDeviceManager().getAllocatedDevices()); |
| 609 | // schedule for for unavailable devices, just in case they come back online |
| 610 | devices.addAll(getDeviceManager().getUnavailableDevices()); |
| 611 | |
| 612 | for (String device : devices) { |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 613 | String[] argsWithDevice = Arrays.copyOf(args, args.length + 2); |
| 614 | argsWithDevice[argsWithDevice.length - 2] = "-s"; |
| 615 | argsWithDevice[argsWithDevice.length - 1] = device; |
| 616 | CommandTracker cmdTracker = createCommandTracker(argsWithDevice); |
| 617 | cmdTracker.incrementExecTime(totalExecTime); |
Brett Chabot | 249ef61 | 2013-02-05 16:24:26 -0800 | [diff] [blame] | 618 | IConfiguration config = getConfigFactory().createConfigurationFromArgs( |
| 619 | cmdTracker.getArgs()); |
| 620 | CLog.logAndDisplay(LogLevel.INFO, "Scheduling '%s' on '%s'", cmdTracker.getArgs()[0], |
| 621 | device); |
| 622 | config.getDeviceRequirements().setSerial(device); |
| 623 | ExecutableCommand execCmd = createExecutableCommand(cmdTracker, config, false); |
| 624 | addExecCommandToQueue(execCmd, 0); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /** |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 629 | * Creates a new {@link CommandTracker} with a unique id. |
| 630 | */ |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 631 | private synchronized CommandTracker createCommandTracker(String[] args) { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 632 | mCurrentCommandId++; |
Brett Chabot | e9d7c4f | 2013-02-14 10:24:18 -0800 | [diff] [blame] | 633 | return new CommandTracker(mCurrentCommandId, args); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Creates a new {@link ExecutableCommand}, and adds it to the all commands tracking list. |
| 638 | */ |
| 639 | private ExecutableCommand createExecutableCommand(CommandTracker cmdTracker, |
| 640 | IConfiguration config, boolean rescheduled) { |
| 641 | ExecutableCommand cmd = new ExecutableCommand(cmdTracker, config, rescheduled); |
Brett Chabot | 0e8e0b5 | 2013-08-13 18:04:49 -0700 | [diff] [blame] | 642 | CLog.d("adding command"); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 643 | mAllCommands.add(cmd); |
| 644 | return cmd; |
| 645 | } |
| 646 | |
| 647 | /** |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 648 | * Dequeue the highest priority command from the queue. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 649 | * |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 650 | * @return the {@link ExecutableCommand} or <code>null</code> |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 651 | */ |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 652 | private ExecutableCommand dequeueConfigCommand() { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 653 | try { |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 654 | // poll for a command, rather than block indefinitely, to handle shutdown case |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 655 | return mCommandQueue.poll(getCommandPollTimeMs(), TimeUnit.MILLISECONDS); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 656 | } catch (InterruptedException e) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 657 | CLog.i("Waiting for command interrupted"); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 658 | } |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 659 | return null; |
| 660 | } |
| 661 | |
| 662 | /** |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 663 | * Get the poll time to wait to retrieve a command to execute. |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 664 | * <p/> |
| 665 | * Exposed so unit tests can mock. |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 666 | */ |
| 667 | long getCommandPollTimeMs() { |
| 668 | return 1000; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 672 | * Creates a new {@link ExecutableCommand}, and adds it to queue |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 673 | * |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 674 | * @param commandTracker |
| 675 | */ |
| 676 | private void addNewExecCommandToQueue(CommandTracker commandTracker) { |
| 677 | try { |
| 678 | IConfiguration config = getConfigFactory().createConfigurationFromArgs( |
| 679 | commandTracker.getArgs()); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 680 | ExecutableCommand execCmd = createExecutableCommand(commandTracker, config, false); |
| 681 | addExecCommandToQueue(execCmd, config.getCommandOptions().getMinLoopTime()); |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 682 | } catch (ConfigurationException e) { |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 683 | CLog.e(e); |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | /** |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 688 | * Adds executable command instance to queue, with optional delay. |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 689 | * |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 690 | * @param cmd the {@link ExecutableCommand} to return to queue |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 691 | * @param delayTime the time in ms to delay before adding command to queue |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 692 | * @return <code>true</code> if command will be added to queue, <code>false</code> otherwise |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 693 | */ |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 694 | private synchronized boolean addExecCommandToQueue(final ExecutableCommand cmd, |
| 695 | long delayTime) { |
Brett Chabot | 919d2c5 | 2011-03-03 19:20:53 -0800 | [diff] [blame] | 696 | if (isShutdown()) { |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 697 | return false; |
Brett Chabot | 919d2c5 | 2011-03-03 19:20:53 -0800 | [diff] [blame] | 698 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 699 | if (delayTime > 0) { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 700 | cmd.setSleepState(delayTime); |
| 701 | // delay before making command active |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 702 | Runnable delayCommand = new Runnable() { |
| 703 | @Override |
| 704 | public void run() { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 705 | synchronized (CommandScheduler.this) { |
| 706 | cmd.setWaitState(); |
| 707 | mCommandQueue.add(cmd); |
| 708 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 709 | } |
| 710 | }; |
| 711 | mCommandTimer.schedule(delayCommand, delayTime, TimeUnit.MILLISECONDS); |
| 712 | } else { |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 713 | mCommandQueue.add(cmd); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 714 | } |
Brett Chabot | 552f8fd | 2011-06-06 19:26:05 -0700 | [diff] [blame] | 715 | return true; |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Helper method to return an array of {@link String} elements as a readable {@link String} |
| 720 | * |
| 721 | * @param args the {@link String}[] to use |
| 722 | * @return a display friendly {@link String} of args contents |
| 723 | */ |
| 724 | private String getArgString(String[] args) { |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 725 | return ArrayUtil.join(" ", (Object[])args); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /** |
Brett Chabot | 4e6f367 | 2011-03-02 17:43:21 -0800 | [diff] [blame] | 729 | * Spawns off thread to run invocation for given device |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 730 | * |
| 731 | * @param manager the {@link IDeviceManager} to return device to when complete |
| 732 | * @param device the {@link ITestDevice} |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 733 | * @param cmd the {@link ExecutableCommand} to execute |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 734 | * @return the invocation's thread |
| 735 | */ |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 736 | private InvocationThread startInvocation(IDeviceManager manager, ITestDevice device, |
Brett Chabot | cd84205 | 2011-04-14 10:47:58 -0700 | [diff] [blame] | 737 | ExecutableCommand cmd) { |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 738 | final String invocationName = String.format("Invocation-%s", device.getSerialNumber()); |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 739 | InvocationThread invocationThread = new InvocationThread(invocationName, manager, device, |
| 740 | cmd); |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 741 | invocationThread.start(); |
| 742 | return invocationThread; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Removes a {@link InvocationThread} from the active list. |
| 747 | */ |
| 748 | private synchronized void removeInvocationThread(InvocationThread invThread) { |
| 749 | mInvocationThreads.remove(invThread); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Adds a {@link InvocationThread} to the active list. |
| 754 | */ |
| 755 | private synchronized void addInvocationThread(InvocationThread invThread) { |
| 756 | mInvocationThreads.add(invThread); |
| 757 | } |
| 758 | |
Brett Chabot | ccf00ea | 2010-08-26 16:04:33 -0700 | [diff] [blame] | 759 | private synchronized boolean isShutdown() { |
Brett Chabot | 29058a9 | 2013-01-10 09:40:48 -0800 | [diff] [blame] | 760 | return mCommandTimer.isShutdown() || (mShutdownOnEmpty && mAllCommands.isEmpty()); |
Brett Chabot | ccf00ea | 2010-08-26 16:04:33 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Brett Chabot | 0e8e0b5 | 2013-08-13 18:04:49 -0700 | [diff] [blame] | 763 | private synchronized boolean isShuttingDown() { |
| 764 | return mCommandTimer.isShutdown() || mShutdownOnEmpty; |
| 765 | } |
| 766 | |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 767 | /** |
Brett Chabot | 0283cb4 | 2010-08-07 16:12:17 -0700 | [diff] [blame] | 768 | * {@inheritDoc} |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 769 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 770 | @Override |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 771 | public synchronized void shutdown() { |
Brett Chabot | 0e8e0b5 | 2013-08-13 18:04:49 -0700 | [diff] [blame] | 772 | if (!isShuttingDown()) { |
| 773 | CLog.d("initiating shutdown"); |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 774 | clearWaitingCommands(); |
Brett Chabot | 4e6f367 | 2011-03-02 17:43:21 -0800 | [diff] [blame] | 775 | if (mCommandTimer != null) { |
Brett Chabot | c6026f0 | 2011-03-02 20:23:33 -0800 | [diff] [blame] | 776 | mCommandTimer.shutdownNow(); |
Brett Chabot | a17af5d | 2010-08-23 13:51:17 -0700 | [diff] [blame] | 777 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 778 | } |
| 779 | } |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 780 | |
Brett Chabot | d850c48 | 2011-02-25 14:17:26 -0800 | [diff] [blame] | 781 | /** |
| 782 | * {@inheritDoc} |
| 783 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 784 | @Override |
Brett Chabot | 29058a9 | 2013-01-10 09:40:48 -0800 | [diff] [blame] | 785 | public synchronized void shutdownOnEmpty() { |
Brett Chabot | 0e8e0b5 | 2013-08-13 18:04:49 -0700 | [diff] [blame] | 786 | if (!isShuttingDown()) { |
| 787 | CLog.d("initiating shutdown on empty"); |
Brett Chabot | 29058a9 | 2013-01-10 09:40:48 -0800 | [diff] [blame] | 788 | mShutdownOnEmpty = true; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * {@inheritDoc} |
| 794 | */ |
| 795 | @Override |
Brett Chabot | 33ab111 | 2012-01-24 14:32:39 -0800 | [diff] [blame] | 796 | public synchronized void removeAllCommands() { |
Brett Chabot | 33ab111 | 2012-01-24 14:32:39 -0800 | [diff] [blame] | 797 | if (mCommandTimer != null) { |
| 798 | for (Runnable task : mCommandTimer.getQueue()) { |
| 799 | mCommandTimer.remove(task); |
| 800 | } |
| 801 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 802 | clearWaitingCommands(); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Clears all {@link ExecutableCommand} not currently executing. |
| 807 | */ |
Brett Chabot | 260d241 | 2012-06-13 10:25:22 -0700 | [diff] [blame] | 808 | private void clearWaitingCommands() { |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 809 | mCommandQueue.clear(); |
Brett Chabot | 260d241 | 2012-06-13 10:25:22 -0700 | [diff] [blame] | 810 | synchronized (mAllCommands) { |
| 811 | ListIterator<ExecutableCommand> cmdIter = mAllCommands.listIterator(); |
| 812 | while (cmdIter.hasNext()) { |
| 813 | ExecutableCommand cmd = cmdIter.next(); |
| 814 | if (!cmd.getState().equals(CommandState.EXECUTING)) { |
| 815 | cmdIter.remove(); |
| 816 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
Brett Chabot | 33ab111 | 2012-01-24 14:32:39 -0800 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /** |
| 822 | * {@inheritDoc} |
| 823 | */ |
| 824 | @Override |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 825 | public synchronized boolean handoverShutdown(int handoverPort) { |
| 826 | if (mRemoteClient != null) { |
| 827 | CLog.e("A handover has already been initiated"); |
| 828 | return false; |
| 829 | } |
| 830 | try { |
| 831 | mRemoteClient = RemoteClient.connect(handoverPort); |
| 832 | CLog.d("connected to remote manager at %d", handoverPort); |
| 833 | // inform remote manager of the devices we are still using |
| 834 | for (String deviceInUse : getDeviceManager().getAllocatedDevices()) { |
Brett Chabot | 9c27c90 | 2013-09-27 11:00:56 -0700 | [diff] [blame^] | 835 | if (!mRemoteClient.sendAllocateDevice(deviceInUse)) { |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 836 | CLog.e("Failed to send command to remote manager"); |
| 837 | return false; |
| 838 | } |
| 839 | CLog.d("Sent filter device %s command", deviceInUse); |
| 840 | } |
| 841 | // now send command info |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 842 | List<CommandTracker> cmdCopy = getCommandTrackers(); |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 843 | // sort so high priority commands are sent first |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 844 | Collections.sort(cmdCopy, new CommandTrackerTimeComparator()); |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 845 | for (CommandTracker cmd : cmdCopy) { |
| 846 | mRemoteClient.sendAddCommand(cmd.getTotalExecTime(), cmd.mArgs); |
| 847 | } |
| 848 | shutdown(); |
| 849 | return true; |
| 850 | |
| 851 | } catch (IOException e) { |
| 852 | CLog.e("Failed to connect to remote manager at port %d", handoverPort); |
| 853 | } |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | /** |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 858 | * @return the list of active {@link CommandTracker}. 'Active' here means all commands added |
| 859 | * to the scheduler that are either executing, waiting for a device to execute on, or looping. |
| 860 | */ |
| 861 | private List<CommandTracker> getCommandTrackers() { |
| 862 | List<ExecutableCommand> cmdCopy = new ArrayList<ExecutableCommand>(mAllCommands); |
| 863 | Set<CommandTracker> cmdTrackers = new LinkedHashSet<CommandTracker>(); |
| 864 | for (ExecutableCommand cmd : cmdCopy) { |
| 865 | cmdTrackers.add(cmd.getCommandTracker()); |
| 866 | } |
| 867 | return new ArrayList<CommandTracker>(cmdTrackers); |
| 868 | } |
| 869 | |
| 870 | /** |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 871 | * Inform the remote listener of the freed device. Has no effect if there is no remote listener. |
| 872 | * |
| 873 | * @param device the freed {@link ITestDevice} |
| 874 | */ |
| 875 | private void remoteFreeDevice(ITestDevice device) { |
| 876 | // TODO: send freed device state too |
| 877 | if (mRemoteClient != null) { |
| 878 | try { |
Brett Chabot | 9c27c90 | 2013-09-27 11:00:56 -0700 | [diff] [blame^] | 879 | mRemoteClient.sendFreeDevice(device.getSerialNumber()); |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 880 | } catch (IOException e) { |
| 881 | CLog.e("Failed to send unfilter device %s to remote manager", |
| 882 | device.getSerialNumber()); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * {@inheritDoc} |
| 889 | */ |
| 890 | @Override |
Brett Chabot | d850c48 | 2011-02-25 14:17:26 -0800 | [diff] [blame] | 891 | public synchronized void shutdownHard() { |
| 892 | shutdown(); |
Brett Chabot | cb2dcdd | 2011-11-23 09:18:45 -0800 | [diff] [blame] | 893 | CLog.logAndDisplay(LogLevel.WARN, "Force killing adb connection"); |
Brett Chabot | d850c48 | 2011-02-25 14:17:26 -0800 | [diff] [blame] | 894 | getDeviceManager().terminateHard(); |
| 895 | } |
| 896 | |
Omari Stephens | 0f92122 | 2011-10-20 19:40:07 -0700 | [diff] [blame] | 897 | /** |
| 898 | * Initializes the ddmlib log. |
| 899 | * <p /> |
| 900 | * Exposed so unit tests can mock. |
| 901 | */ |
| 902 | void initLogging() { |
| 903 | DdmPreferences.setLogLevel(LogLevel.VERBOSE.getStringValue()); |
| 904 | Log.setLogOutput(LogRegistry.getLogRegistry()); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Closes the logs and does any other necessary cleanup before we quit. |
| 909 | * <p /> |
| 910 | * Exposed so unit tests can mock. |
| 911 | */ |
| 912 | void cleanUp() { |
| 913 | LogRegistry.getLogRegistry().closeAndRemoveAllLogs(); |
| 914 | } |
| 915 | |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 916 | /** |
| 917 | * {@inheritDoc} |
| 918 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 919 | @Override |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 920 | public void displayInvocationsInfo(PrintWriter printWriter) { |
| 921 | if (mInvocationThreads == null || mInvocationThreads.size() == 0) { |
| 922 | return; |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 923 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 924 | List<InvocationThread> copy = new ArrayList<InvocationThread>(mInvocationThreads); |
| 925 | ArrayList<List<String>> displayRows = new ArrayList<List<String>>(); |
| 926 | displayRows.add(Arrays.asList("Command Id", "Exec Time", "Device", "State")); |
Omari Stephens | 1ee376d | 2011-10-20 18:48:55 -0700 | [diff] [blame] | 927 | long curTime = System.currentTimeMillis(); |
| 928 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 929 | for (InvocationThread invThread : copy) { |
| 930 | displayRows.add(Arrays.asList( |
| 931 | Integer.toString(invThread.mCmd.getCommandTracker().getId()), |
| 932 | getTimeString(curTime - invThread.getStartTime()), |
| 933 | invThread.getDevice().getSerialNumber(), |
| 934 | invThread.getInvocation().toString())); |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 935 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 936 | new TableFormatter().displayTable(displayRows, printWriter); |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 939 | private String getTimeString(long elapsedTime) { |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 940 | long duration = elapsedTime / 1000; |
| 941 | long secs = duration % 60; |
| 942 | long mins = (duration / 60) % 60; |
| 943 | long hrs = duration / (60 * 60); |
| 944 | String time = "unknown"; |
| 945 | if (hrs > 0) { |
| 946 | time = String.format("%dh:%02d:%02d", hrs, mins, secs); |
| 947 | } else { |
| 948 | time = String.format("%dm:%02d", mins, secs); |
| 949 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 950 | return time; |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 953 | /** |
| 954 | * {@inheritDoc} |
| 955 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 956 | @Override |
Omari Stephens | ae6ffbf | 2010-08-09 21:44:41 -0700 | [diff] [blame] | 957 | public boolean stopInvocation(ITestInvocation invocation) throws UnsupportedOperationException { |
| 958 | throw new UnsupportedOperationException(); |
| 959 | } |
Omari Stephens | 68a6e78 | 2010-08-18 18:19:51 -0700 | [diff] [blame] | 960 | |
| 961 | /** |
| 962 | * {@inheritDoc} |
| 963 | */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 964 | @Override |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 965 | public void displayCommandsInfo(PrintWriter printWriter) { |
| 966 | List<CommandTracker> cmds = getCommandTrackers(); |
| 967 | Collections.sort(cmds, new CommandTrackerIdComparator()); |
| 968 | for (CommandTracker cmd : cmds) { |
| 969 | String cmdDesc = String.format("Command %d: [%s] %s", cmd.getId(), |
| 970 | getTimeString(cmd.getTotalExecTime()), getArgString(cmd.getArgs())); |
| 971 | printWriter.println(cmdDesc); |
Omari Stephens | 68a6e78 | 2010-08-18 18:19:51 -0700 | [diff] [blame] | 972 | } |
Brett Chabot | 9f2c1a8 | 2012-06-01 16:43:38 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /** |
| 976 | * {@inheritDoc} |
| 977 | */ |
| 978 | @Override |
| 979 | public void displayCommandQueue(PrintWriter printWriter) { |
| 980 | if (mAllCommands.isEmpty()) { |
| 981 | return; |
| 982 | } |
| 983 | ArrayList<List<String>> displayRows = new ArrayList<List<String>>(); |
| 984 | displayRows.add(Arrays.asList("Id", "Config", "Created", "State", "Sleep time", |
| 985 | "Rescheduled", "Loop")); |
| 986 | long curTime = System.currentTimeMillis(); |
| 987 | List<ExecutableCommand> cmdCopy = new ArrayList<ExecutableCommand>(mAllCommands); |
| 988 | for (ExecutableCommand cmd : cmdCopy) { |
| 989 | dumpCommand(curTime, cmd, displayRows); |
| 990 | } |
| 991 | new TableFormatter().displayTable(displayRows, printWriter); |
| 992 | } |
| 993 | |
| 994 | private void dumpCommand(long curTime, ExecutableCommand cmd, |
| 995 | ArrayList<List<String>> displayRows) { |
| 996 | String sleepTime = cmd.getSleepTime() == null ? "N/A" : getTimeString(cmd.getSleepTime()); |
| 997 | displayRows.add(Arrays.asList( |
| 998 | Integer.toString(cmd.getCommandTracker().getId()), |
| 999 | cmd.getCommandTracker().getArgs()[0], |
| 1000 | getTimeString(curTime - cmd.getCreationTime()), |
| 1001 | cmd.getState().getDisplayName(), |
| 1002 | sleepTime, |
| 1003 | Boolean.toString(cmd.isRescheduled()), |
| 1004 | Boolean.toString(cmd.isLoopMode()))); |
Omari Stephens | 68a6e78 | 2010-08-18 18:19:51 -0700 | [diff] [blame] | 1005 | } |
Brett Chabot | 33513a5 | 2011-11-09 18:20:04 -0800 | [diff] [blame] | 1006 | |
| 1007 | /** |
| 1008 | * {@inheritDoc} |
| 1009 | */ |
| 1010 | @Override |
| 1011 | public int startRemoteManager() { |
| 1012 | if (mRemoteManager != null && !mRemoteManager.isCanceled()) { |
| 1013 | CLog.logAndDisplay(LogLevel.INFO, "A remote manager is already running at port %d", |
| 1014 | mRemoteManager.getPort()); |
| 1015 | return -1; |
| 1016 | } |
| 1017 | mRemoteManager = new RemoteManager(getDeviceManager(), this); |
| 1018 | mRemoteManager.start(); |
| 1019 | int port = mRemoteManager.getPort(); |
| 1020 | if (port == -1) { |
| 1021 | // set mRemoteManager to null so it can be started again if necessary |
| 1022 | mRemoteManager = null; |
| 1023 | } |
| 1024 | return port; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * {@inheritDoc} |
| 1029 | */ |
| 1030 | @Override |
| 1031 | public void stopRemoteManager() { |
| 1032 | if (mRemoteManager == null) { |
| 1033 | CLog.logAndDisplay(LogLevel.INFO, "A remote manager is not running"); |
| 1034 | return; |
| 1035 | } |
| 1036 | mRemoteManager.cancel(); |
| 1037 | mRemoteManager = null; |
| 1038 | } |
Brett Chabot | f7ad156 | 2010-07-27 11:58:58 -0700 | [diff] [blame] | 1039 | } |