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

Commit 470fd72a authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Log netd traffic nicely.

Create a LocalLog class for logging within a service for dumping in dumps.
Use it in the NativeDaemonConnector so we can get some insight into what
is happening in these lockups.

bug:5864209
Change-Id: I68ddc58847f3c8de613be9528570f8c3157d8274
parent fa78fe0d
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.util;

import android.text.format.Time;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @hide
 */
public final class LocalLog {

    private LinkedList<String> mLog;
    private int mMaxLines;
    private Time mNow;

    public LocalLog(int maxLines) {
        mLog = new LinkedList<String>();
        mMaxLines = maxLines;
        mNow = new Time();
    }

    public synchronized void log(String msg) {
        if (mMaxLines > 0) {
            mNow.setToNow();
            mLog.add(mNow.format("%H:%M:%S") + " - " + msg);
            while (mLog.size() > mMaxLines) mLog.remove();
        }
    }

    public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        Iterator<String> itr = mLog.listIterator(0);
        while (itr.hasNext()) {
            pw.println(itr.next());
        }
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -1169,7 +1169,7 @@ class MountService extends IMountService.Stub
         * amount of containers we'd ever expect to have. This keeps an
         * "asec list" from blocking a thread repeatedly.
         */
        mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG);
        mConnector = new NativeDaemonConnector(this, "vold", MAX_CONTAINERS * 2, VOLD_TAG, 25);
        mReady = false;
        Thread thread = new Thread(mConnector, VOLD_TAG);
        thread.start();
@@ -2429,6 +2429,10 @@ class MountService extends IMountService.Stub
                pw.println(v.toString());
            }
        }

        pw.println();
        pw.println("  mConnection:");
        mConnector.dump(fd, pw, args);
    }

    /** {@inheritDoc} */
+18 −4
Original line number Diff line number Diff line
@@ -22,14 +22,17 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.os.SystemClock;
import android.util.LocalLog;
import android.util.Slog;

import com.google.android.collect.Lists;

import java.nio.charset.Charsets;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charsets;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -45,6 +48,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo

    private String mSocket;
    private OutputStream mOutputStream;
    private LocalLog mLocalLog;

    private final BlockingQueue<NativeDaemonEvent> mResponseQueue;

@@ -57,11 +61,12 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
    private final int BUFFER_SIZE = 4096;

    NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket,
            int responseQueueSize, String logTag) {
            int responseQueueSize, String logTag, int maxLogSize) {
        mCallbacks = callbacks;
        mSocket = socket;
        mResponseQueue = new LinkedBlockingQueue<NativeDaemonEvent>(responseQueueSize);
        TAG = logTag != null ? logTag : "NativeDaemonConnector";
        mLocalLog = new LocalLog(maxLogSize);
    }

    @Override
@@ -125,7 +130,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
                    if (buffer[i] == 0) {
                        final String rawEvent = new String(
                                buffer, start, i - start, Charsets.UTF_8);
                        if (LOGD) Slog.d(TAG, "RCV <- " + rawEvent);
                        log("RCV <- {" + rawEvent + "}");

                        try {
                            final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(
@@ -208,7 +213,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
        }

        final String unterminated = builder.toString();
        if (LOGD) Slog.d(TAG, "SND -> " + unterminated);
        log("SND -> {" + unterminated + "}");

        builder.append('\0');

@@ -432,4 +437,13 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
    public void monitor() {
        synchronized (mDaemonLock) { }
    }

    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        mLocalLog.dump(fd, pw, args);
    }

    private void log(String logstring) {
        if (LOGD) Slog.d(TAG, logstring);
        mLocalLog.log(logstring);
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -163,7 +163,7 @@ public class NetworkManagementService extends INetworkManagementService.Stub
        }

        mConnector = new NativeDaemonConnector(
                new NetdCallbackReceiver(), "netd", 10, NETD_TAG);
                new NetdCallbackReceiver(), "netd", 10, NETD_TAG, 50);
        mThread = new Thread(mConnector, NETD_TAG);

        // Add ourself to the Watchdog monitors.
@@ -1265,6 +1265,10 @@ public class NetworkManagementService extends INetworkManagementService.Stub
    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        mContext.enforceCallingOrSelfPermission(DUMP, TAG);

        pw.println("NetworkManagementService NativeDaemonConnector Log:");
        mConnector.dump(fd, pw, args);
        pw.println();

        pw.print("Bandwidth control enabled: "); pw.println(mBandwidthControlEnabled);

        synchronized (mQuotaLock) {