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

Commit 1b0b114a authored by Bookatz's avatar Bookatz
Browse files

StatsCompanionService sends messages to statsd

StatsCompanionService can now inform statsd that an alarm (for anomaly
alerting and for polling) has fired, so that statsd can act accordingly.

Test: manual created an alarm from statsd.main and checked logcat that
statsd received the broadcast that it fired
Change-Id: I1d33dfbee0d3e213c91dd6973d2622ecacc890c8
parent 52c0e916
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -161,6 +161,38 @@ StatsService::doLoadConfig(FILE* in)
    }
}

Status
StatsService::informAnomalyAlarmFired()
{
    ALOGD("StatsService::informAnomalyAlarmFired was called");

    if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
        return Status::fromExceptionCode(Status::EX_SECURITY,
                "Only system uid can call informAnomalyAlarmFired");
    }

    ALOGD("StatsService::informAnomalyAlarmFired succeeded");
    // TODO: check through all counters/timers and see if an anomaly has indeed occurred.

    return Status::ok();
}

Status
StatsService::informPollAlarmFired()
{
    ALOGD("StatsService::informPollAlarmFired was called");

    if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
        return Status::fromExceptionCode(Status::EX_SECURITY,
                "Only system uid can call informPollAlarmFired");
    }

    ALOGD("StatsService::informPollAlarmFired succeeded");
    // TODO: determine what services to poll and poll (or ask StatsCompanionService to poll) them.

    return Status::ok();
}

Status
StatsService::systemRunning()
{
+4 −0
Original line number Diff line number Diff line
@@ -49,6 +49,10 @@ public:

    virtual Status systemRunning();

    virtual Status informAnomalyAlarmFired();

    virtual Status informPollAlarmFired();

    virtual status_t setProcessor(const sp<StatsLogProcessor>& main_processor);

private:
+3 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ package android.os;
  * Binder interface to communicate with the Java-based statistics service helper.
  * {@hide}
  */
interface IStatsCompanionService {
oneway interface IStatsCompanionService {
    /**
    * Register an alarm for anomaly detection to fire at the given timestamp (ms since epoch).
    * If anomaly alarm had already been registered, it will be replaced with the new timestamp.
@@ -28,6 +28,7 @@ interface IStatsCompanionService {
    * alarm is inexact.
    */
    void setAnomalyAlarm(long timestampMs);

    /** Cancel any anomaly detection alarm. */
    void cancelAnomalyAlarm();

@@ -39,6 +40,7 @@ interface IStatsCompanionService {
      * and alarm is inexact.
      */
    void setPollingAlarms(long timestampMs, long intervalMs);

    /** Cancel any repeating polling alarm. */
    void cancelPollingAlarms();
}
+13 −2
Original line number Diff line number Diff line
@@ -17,12 +17,23 @@
package android.os;

/**
  * Binder interface to communicate with the statistics collection service.
  * Binder interface to communicate with the statistics management service.
  * {@hide}
  */
oneway interface IStatsManager {
    /**
     * Tell the incident daemon that the android system server is up and running.
     * Tell the stats daemon that the android system server is up and running.
     */
    void systemRunning();

    /**
     * Tells statsd that an anomaly may have occurred, so statsd can check whether this is so and
     * act accordingly.
     */
    void informAnomalyAlarmFired();

    /** Tells statsd that it is time to poll some stats. Statsd will be responsible for determing
     * what stats to poll and initiating the polling.
     */
    void informPollAlarmFired();
}
+24 −6
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.os.Binder;
import android.os.IStatsCompanionService;
import android.os.IStatsManager;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Slog;

@@ -39,7 +40,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {

    private final Context mContext;
    private final AlarmManager mAlarmManager;
    private final IStatsManager mStatsd;
    private static IStatsManager sStatsd;

    private final PendingIntent mAnomalyAlarmIntent;
    private final PendingIntent mPollingAlarmIntent;
@@ -48,7 +49,14 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        @Override
        public void onReceive(Context context, Intent intent) {
            Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred.");
            // TODO: mStatsd.informAlarm(); // should be twoway so device won't sleep before acting?
            try {
                // TODO: should be twoway so device won't sleep before acting?
                getStatsdService().informAnomalyAlarmFired();
            } catch (RemoteException e) {
                Slog.e(TAG, "failed to inform statsd of anomaly alarm firing", e);
            } catch (NullPointerException e) {
                Slog.e(TAG, "could not access statsd to inform it of anomaly alarm firing", e);
            }
            // AlarmManager releases its own wakelock here.
        }
    };
@@ -57,7 +65,15 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) Slog.d(TAG, "Time to poll something.");
            // TODO: mStatsd.poll(); // should be twoway so device won't sleep before acting?
            if (DEBUG) Slog.d(TAG, "Time to poll something.");
            try {
                // TODO: should be twoway so device won't sleep before acting?
                getStatsdService().informPollAlarmFired();
            } catch (RemoteException e) {
                Slog.e(TAG, "failed to inform statsd of polling alarm firing",e);
            } catch (NullPointerException e) {
                Slog.e(TAG, "could not access statsd to inform it of polling alarm firing", e);
            }
            // AlarmManager releases its own wakelock here.
        }
    };
@@ -71,13 +87,15 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                new Intent(mContext, AnomalyAlarmReceiver.class), 0);
        mPollingAlarmIntent = PendingIntent.getBroadcast(mContext, 0,
                new Intent(mContext, PollingAlarmReceiver.class), 0);

        mStatsd = getStatsdService();
    }

    /** Returns the statsd IBinder service */
    public static IStatsManager getStatsdService() {
        return IStatsManager.Stub.asInterface(ServiceManager.getService("statsd"));
        if (sStatsd != null) {
            return sStatsd;
        }
        sStatsd = IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
        return sStatsd;
    }

    public static final class Lifecycle extends SystemService {