Search code examples
androidandroid-binder

How android system manages list of connected clients to a bounded remote service


I am doing a project which does real time analysis of the system on android phone.I want to know how many clients are bounded to a any remote services at a given point in time.

For example:No of clients bounded to LocationManager service.

This will help me identify application which are stressing the system services.Also it will help me identify whether its the system service which is badly written or the clients of the system service are behaving badly.

Eg:I found out that when you continously query/update/delete contacts content provider,the system shows com.android.acore is consuming more cpu but in reality its the third party app which is behaving bad.

I am working on custom ROM and have access to code for making any modifications.

I know binders are reference counted and each BinderProxy which references a Binder constitutes a reference to that Binder.But I am not sure how it is done in code.

Please share your knowledge if anyone has already done some research on it.


Solution

  • Fount out that there is ServiceRecord class which maintains all the info related to a service.refer-http://androidxref.com/4.4.4_r1/xref/frameworks/base/services/java/com/android/server/am/ServiceRecord.java

    Some of the fields are below:

                         // all information about the service.
    final ApplicationInfo appInfo;
                            // information about service's app.
    final int userId;       // user that this service is running as
    final String packageName; // the package implementing intent's component
    final String processName; // process where this component wants to run
    final String permission;// permission needed to access service
    final String baseDir;   // where activity source (resources etc) located
    final String resDir;   // where public activity source (public resources etc) located
    final String dataDir;   // where activity data should go
    final boolean exported; // from ServiceInfo.exported
    final Runnable restarter; // used to schedule retries of starting the service
    final long createTime;  // when this service was created
    final ArrayMap<Intent.FilterComparison, IntentBindRecord> bindings
            = new ArrayMap<Intent.FilterComparison, IntentBindRecord>();
                            // All active bindings to the service.
    final ArrayMap<IBinder, ArrayList<ConnectionRecord>> connections
            = new ArrayMap<IBinder, ArrayList<ConnectionRecord>>();
                            // IBinder -> ConnectionRecord of all bound clients
    
    ProcessRecord app;      // where this service is running or null.
    ProcessRecord isolatedProc; // keep track of isolated process, if requested
    ProcessStats.ServiceState tracker; // tracking service execution, may be null
    ProcessStats.ServiceState restartTracker; // tracking service restart
    boolean delayed;        // are we waiting to start this service in the background?
    boolean isForeground;   // is service currently in foreground mode?
    int foregroundId;       // Notification ID of last foreground req.
    Notification foregroundNoti; // Notification record of foreground state.
    long lastActivity;      // last time there was some activity on the service.
    long startingBgTimeout;  // time at which we scheduled this for a delayed start.
    boolean startRequested; // someone explicitly called start?
    boolean delayedStop;    // service has been stopped but is in a delayed start?
    boolean stopIfKilled;   // last onStart() said to stop if service killed?
    boolean callStart;      // last onStart() has asked to alway be called on restart.
    int executeNesting;     // number of outstanding operations keeping foreground.
    boolean executeFg;      // should we be executing in the foreground?
    long executingStart;    // start time of last execute request.
    boolean createdFromFg;  // was this service last created due to a foreground process call?
    int crashCount;         // number of times proc has crashed with service running
    int totalRestartCount;  // number of times we have had to restart.
    int restartCount;       // number of restarts performed in a row.
    long restartDelay;      // delay until next restart attempt.
    long restartTime;       // time of last restart.
    long nextRestartTime;   // time when restartDelay will expire.
    
    String stringName;      // caching of toString
    
    private int lastStartId;    // identifier of most recent start request.