| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.telecom.Logging; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Looper; |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 22 | import android.os.Process; |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 23 | import android.provider.Settings; |
| 24 | import android.util.Base64; |
| 25 | |
| 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | |
| 28 | import java.nio.ByteBuffer; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Arrays; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.List; |
| 33 | import java.util.concurrent.ConcurrentHashMap; |
| 34 | |
| 35 | /** |
| 36 | * TODO: Create better Sessions Documentation |
| 37 | * @hide |
| 38 | */ |
| 39 | |
| 40 | public class SessionManager { |
| 41 | |
| 42 | // Currently using 3 letters, So don't exceed 64^3 |
| 43 | private static final long SESSION_ID_ROLLOVER_THRESHOLD = 262144; |
| 44 | |
| 45 | // This parameter can be overridden in Telecom's Timeouts class. |
| 46 | public static final long DEFAULT_SESSION_TIMEOUT_MS = 30000L; // 30 seconds |
| 47 | |
| 48 | private static String LOGGING_TAG = "Logging"; |
| 49 | |
| 50 | private static final String TIMEOUTS_PREFIX = "telecom."; |
| 51 | |
| 52 | // Synchronized in all method calls |
| 53 | private int sCodeEntryCounter = 0; |
| 54 | private Context mContext; |
| 55 | |
| 56 | @VisibleForTesting |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 57 | public ConcurrentHashMap<Integer, Session> mSessionMapper = new ConcurrentHashMap<>(100); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 58 | @VisibleForTesting |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 59 | public Handler mSessionCleanupHandler = new Handler(Looper.getMainLooper()); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 60 | @VisibleForTesting |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 61 | public java.lang.Runnable mCleanStaleSessions = () -> |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 62 | cleanupStaleSessions(getSessionCleanupTimeoutMs()); |
| 63 | |
| 64 | // Overridden in LogTest to skip query to ContentProvider |
| 65 | private interface ISessionCleanupTimeoutMs { |
| 66 | long get(); |
| 67 | } |
| 68 | |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 69 | // Overridden in tests to provide test Thread IDs |
| 70 | public interface ICurrentThreadId { |
| 71 | int get(); |
| 72 | } |
| 73 | |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 74 | @VisibleForTesting |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 75 | public ICurrentThreadId mCurrentThreadId = Process::myTid; |
| 76 | |
| 77 | @VisibleForTesting |
| 78 | public ISessionCleanupTimeoutMs mSessionCleanupTimeoutMs = () -> { |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 79 | // mContext may be null in some cases, such as testing. For these cases, use the |
| 80 | // default value. |
| 81 | if (mContext == null) { |
| 82 | return DEFAULT_SESSION_TIMEOUT_MS; |
| 83 | } |
| 84 | return getCleanupTimeout(mContext); |
| 85 | }; |
| 86 | |
| 87 | // Usage is synchronized on this class. |
| 88 | private List<ISessionListener> mSessionListeners = new ArrayList<>(); |
| 89 | |
| 90 | public interface ISessionListener { |
| 91 | /** |
| 92 | * This method is run when a full Session has completed. |
| 93 | * @param sessionName The name of the Session that has completed. |
| 94 | * @param timeMs The time it took to complete in ms. |
| 95 | */ |
| 96 | void sessionComplete(String sessionName, long timeMs); |
| 97 | } |
| 98 | |
| 99 | public interface ISessionIdQueryHandler { |
| 100 | String getSessionId(); |
| 101 | } |
| 102 | |
| 103 | public void setContext(Context context) { |
| 104 | mContext = context; |
| 105 | } |
| 106 | |
| 107 | public SessionManager() { |
| 108 | } |
| 109 | |
| 110 | private long getSessionCleanupTimeoutMs() { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 111 | return mSessionCleanupTimeoutMs.get(); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private synchronized void resetStaleSessionTimer() { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 115 | mSessionCleanupHandler.removeCallbacksAndMessages(null); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 116 | // Will be null in Log Testing |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 117 | if (mCleanStaleSessions != null) { |
| 118 | mSessionCleanupHandler.postDelayed(mCleanStaleSessions, getSessionCleanupTimeoutMs()); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Call at an entry point to the Telecom code to track the session. This code must be |
| 124 | * accompanied by a Log.endSession(). |
| 125 | */ |
| 126 | public synchronized void startSession(String shortMethodName, |
| 127 | String callerIdentification) { |
| 128 | resetStaleSessionTimer(); |
| 129 | int threadId = getCallingThreadId(); |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 130 | Session activeSession = mSessionMapper.get(threadId); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 131 | // We have called startSession within an active session that has not ended... Register this |
| 132 | // session as a subsession. |
| 133 | if (activeSession != null) { |
| 134 | Session childSession = createSubsession(true); |
| 135 | continueSession(childSession, shortMethodName); |
| 136 | return; |
| 137 | } |
| 138 | Session newSession = new Session(getNextSessionID(), shortMethodName, |
| 139 | System.currentTimeMillis(), threadId, false, callerIdentification); |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 140 | mSessionMapper.put(threadId, newSession); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 141 | |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 142 | android.util.Slog.v(LOGGING_TAG, Session.START_SESSION); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Notifies the logging system that a subsession will be run at a later point and |
| 148 | * allocates the resources. Returns a session object that must be used in |
| 149 | * Log.continueSession(...) to start the subsession. |
| 150 | */ |
| 151 | public Session createSubsession() { |
| 152 | return createSubsession(false); |
| 153 | } |
| 154 | |
| 155 | private synchronized Session createSubsession(boolean isStartedFromActiveSession) { |
| 156 | int threadId = getCallingThreadId(); |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 157 | Session threadSession = mSessionMapper.get(threadId); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 158 | if (threadSession == null) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 159 | android.util.Slog.d(LOGGING_TAG, "Log.createSubsession was called with no session " + |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 160 | "active."); |
| 161 | return null; |
| 162 | } |
| 163 | // Start execution time of the session will be overwritten in continueSession(...). |
| 164 | Session newSubsession = new Session(threadSession.getNextChildId(), |
| 165 | threadSession.getShortMethodName(), System.currentTimeMillis(), threadId, |
| 166 | isStartedFromActiveSession, null); |
| 167 | threadSession.addChild(newSubsession); |
| 168 | newSubsession.setParentSession(threadSession); |
| 169 | |
| 170 | if (!isStartedFromActiveSession) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 171 | android.util.Slog.v(LOGGING_TAG, Session.CREATE_SUBSESSION + " " + |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 172 | newSubsession.toString()); |
| 173 | } else { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 174 | android.util.Slog.v(LOGGING_TAG, Session.CREATE_SUBSESSION + |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 175 | " (Invisible subsession)"); |
| 176 | } |
| 177 | return newSubsession; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Cancels a subsession that had Log.createSubsession() called on it, but will never have |
| 182 | * Log.continueSession(...) called on it due to an error. Allows the subsession to be cleaned |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 183 | * gracefully instead of being removed by the mSessionCleanupHandler forcefully later. |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 184 | */ |
| 185 | public synchronized void cancelSubsession(Session subsession) { |
| 186 | if (subsession == null) { |
| 187 | return; |
| 188 | } |
| 189 | |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 190 | subsession.markSessionCompleted(Session.UNDEFINED); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 191 | endParentSessions(subsession); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Starts the subsession that was created in Log.CreateSubsession. The Log.endSession() method |
| 196 | * must be called at the end of this method. The full session will complete when all |
| 197 | * subsessions are completed. |
| 198 | */ |
| 199 | public synchronized void continueSession(Session subsession, String shortMethodName) { |
| 200 | if (subsession == null) { |
| 201 | return; |
| 202 | } |
| 203 | resetStaleSessionTimer(); |
| 204 | String callingMethodName = subsession.getShortMethodName(); |
| 205 | subsession.setShortMethodName(callingMethodName + "->" + shortMethodName); |
| 206 | subsession.setExecutionStartTimeMs(System.currentTimeMillis()); |
| 207 | Session parentSession = subsession.getParentSession(); |
| 208 | if (parentSession == null) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 209 | android.util.Slog.d(LOGGING_TAG, "Log.continueSession was called with no session " + |
| 210 | "active for method " + shortMethodName); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 214 | mSessionMapper.put(getCallingThreadId(), subsession); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 215 | if (!subsession.isStartedFromActiveSession()) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 216 | android.util.Slog.v(LOGGING_TAG, Session.CONTINUE_SUBSESSION); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 217 | } else { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 218 | android.util.Slog.v(LOGGING_TAG, Session.CONTINUE_SUBSESSION + |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 219 | " (Invisible Subsession) with Method " + shortMethodName); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Ends the current session/subsession. Must be called after a Log.startSession(...) and |
| 225 | * Log.continueSession(...) call. |
| 226 | */ |
| 227 | public synchronized void endSession() { |
| 228 | int threadId = getCallingThreadId(); |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 229 | Session completedSession = mSessionMapper.get(threadId); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 230 | if (completedSession == null) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 231 | android.util.Slog.w(LOGGING_TAG, "Log.endSession was called with no session active."); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | completedSession.markSessionCompleted(System.currentTimeMillis()); |
| 236 | if (!completedSession.isStartedFromActiveSession()) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 237 | android.util.Slog.v(LOGGING_TAG, Session.END_SUBSESSION + " (dur: " + |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 238 | completedSession.getLocalExecutionTime() + " mS)"); |
| 239 | } else { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 240 | android.util.Slog.v(LOGGING_TAG, Session.END_SUBSESSION + |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 241 | " (Invisible Subsession) (dur: " + completedSession.getLocalExecutionTime() + |
| 242 | " ms)"); |
| 243 | } |
| 244 | // Remove after completed so that reference still exists for logging the end events |
| 245 | Session parentSession = completedSession.getParentSession(); |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 246 | mSessionMapper.remove(threadId); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 247 | endParentSessions(completedSession); |
| 248 | // If this subsession was started from a parent session using Log.startSession, return the |
| 249 | // ThreadID back to the parent after completion. |
| 250 | if (parentSession != null && !parentSession.isSessionCompleted() && |
| 251 | completedSession.isStartedFromActiveSession()) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 252 | mSessionMapper.put(threadId, parentSession); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | // Recursively deletes all complete parent sessions of the current subsession if it is a leaf. |
| 257 | private void endParentSessions(Session subsession) { |
| 258 | // Session is not completed or not currently a leaf, so we can not remove because a child is |
| 259 | // still running |
| 260 | if (!subsession.isSessionCompleted() || subsession.getChildSessions().size() != 0) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | Session parentSession = subsession.getParentSession(); |
| 265 | if (parentSession != null) { |
| 266 | subsession.setParentSession(null); |
| 267 | parentSession.removeChild(subsession); |
| 268 | endParentSessions(parentSession); |
| 269 | } else { |
| 270 | // All of the subsessions have been completed and it is time to report on the full |
| 271 | // running time of the session. |
| 272 | long fullSessionTimeMs = |
| 273 | System.currentTimeMillis() - subsession.getExecutionStartTimeMilliseconds(); |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 274 | android.util.Slog.d(LOGGING_TAG, Session.END_SESSION + " (dur: " + fullSessionTimeMs |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 275 | + " ms): " + subsession.toString()); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 276 | for (ISessionListener l : mSessionListeners) { |
| 277 | l.sessionComplete(subsession.getShortMethodName(), fullSessionTimeMs); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public String getSessionId() { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 283 | Session currentSession = mSessionMapper.get(getCallingThreadId()); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 284 | return currentSession != null ? currentSession.toString() : ""; |
| 285 | } |
| 286 | |
| 287 | public synchronized void registerSessionListener(ISessionListener l) { |
| 288 | if (l != null) { |
| 289 | mSessionListeners.add(l); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | private synchronized String getNextSessionID() { |
| 294 | Integer nextId = sCodeEntryCounter++; |
| 295 | if (nextId >= SESSION_ID_ROLLOVER_THRESHOLD) { |
| 296 | restartSessionCounter(); |
| 297 | nextId = sCodeEntryCounter++; |
| 298 | } |
| 299 | return getBase64Encoding(nextId); |
| 300 | } |
| 301 | |
| 302 | @VisibleForTesting |
| 303 | public synchronized void restartSessionCounter() { |
| 304 | sCodeEntryCounter = 0; |
| 305 | } |
| 306 | |
| 307 | @VisibleForTesting |
| 308 | public String getBase64Encoding(int number) { |
| 309 | byte[] idByteArray = ByteBuffer.allocate(4).putInt(number).array(); |
| 310 | idByteArray = Arrays.copyOfRange(idByteArray, 2, 4); |
| 311 | return Base64.encodeToString(idByteArray, Base64.NO_WRAP | Base64.NO_PADDING); |
| 312 | } |
| 313 | |
| 314 | public int getCallingThreadId() { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 315 | return mCurrentThreadId.get(); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @VisibleForTesting |
| 319 | private synchronized void cleanupStaleSessions(long timeoutMs) { |
| 320 | String logMessage = "Stale Sessions Cleaned:\n"; |
| 321 | boolean isSessionsStale = false; |
| 322 | long currentTimeMs = System.currentTimeMillis(); |
| 323 | // Remove references that are in the Session Mapper (causing GC to occur) on |
| 324 | // sessions that are lasting longer than LOGGING_SESSION_TIMEOUT_MS. |
| 325 | // If this occurs, then there is most likely a Session active that never had |
| 326 | // Log.endSession called on it. |
| 327 | for (Iterator<ConcurrentHashMap.Entry<Integer, Session>> it = |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 328 | mSessionMapper.entrySet().iterator(); it.hasNext(); ) { |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 329 | ConcurrentHashMap.Entry<Integer, Session> entry = it.next(); |
| 330 | Session session = entry.getValue(); |
| 331 | if (currentTimeMs - session.getExecutionStartTimeMilliseconds() > timeoutMs) { |
| 332 | it.remove(); |
| 333 | logMessage += session.printFullSessionTree() + "\n"; |
| 334 | isSessionsStale = true; |
| 335 | } |
| 336 | } |
| 337 | if (isSessionsStale) { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 338 | android.util.Slog.w(LOGGING_TAG, logMessage); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 339 | } else { |
| Brad Ebinger | 096d282 | 2016-10-13 15:26:58 -0700 | [diff] [blame] | 340 | android.util.Slog.v(LOGGING_TAG, "No stale logging sessions needed to be cleaned..."); |
| Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Returns the amount of time after a Logging session has been started that Telecom is set to |
| 346 | * perform a sweep to check and make sure that the session is still not incomplete (stale). |
| 347 | */ |
| 348 | private long getCleanupTimeout(Context context) { |
| 349 | return Settings.Secure.getLong(context.getContentResolver(), TIMEOUTS_PREFIX + |
| 350 | "stale_session_cleanup_timeout_millis", DEFAULT_SESSION_TIMEOUT_MS); |
| 351 | } |
| 352 | } |