Search in sources :

Example 1 with CpuStatus

use of edu.berkeley.cs.amplab.carat.thrift.CpuStatus in project carat by amplab.

the class SamplingLibrary method getSample.

public static Sample getSample(Context context, Intent intent, String lastBatteryState) {
    final String TAG = "SamplingLibrary.getSample";
    Log.d(TAG, "getSample() was invoked.");
    String action = intent.getAction();
    Log.d(TAG, "action = " + action);
    // Construct sample and return it in the end
    Sample mySample = new Sample();
    SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    String uuId = p.getString(CaratApplication.getRegisteredUuid(), null);
    mySample.setUuId(uuId);
    mySample.setTriggeredBy(action);
    // required always
    long now = System.currentTimeMillis();
    mySample.setTimestamp(now / 1000.0);
    // Record first data point for CPU usage
    long[] idleAndCpu1 = readUsagePoint();
    // If the sampler is running because of the SCREEN_ON or SCREEN_OFF
    // event/action,
    // we want to get the info of all installed apps/packages, not only
    // those running.
    // This is because we need the traffic info of all apps, some might not
    // be running when
    // those events (screen on / screen off) occur
    // TODO: let's comment out these lines for debugging purpose
    // if (action.equals(Intent.ACTION_SCREEN_ON) ||
    // action.equals(Intent.ACTION_SCREEN_OFF)) {
    // Log.d(TAG,
    // "the action has been Intent.ACTION_SCREEN_ON or SCREEN_OFF. Taking sample of ALL INSTALLED packages (rather than running processes)");
    // Map<String, ProcessInfo> installedPackages =
    // getInstalledPackages(context, false);
    // List<ProcessInfo> processes = new ArrayList<ProcessInfo>();
    // processes.addAll(installedPackages.values());
    // } else {
    // Log.d(TAG,
    // "the action has NOT been Intent.ACTION_SCREEN_ON or SCREEN_OFF. Taking sample of running processes.");
    List<ProcessInfo> processes = getRunningProcessInfoForSample(context);
    mySample.setPiList(processes);
    // }
    int screenBrightness = SamplingLibrary.getScreenBrightness(context);
    mySample.setScreenBrightness(screenBrightness);
    boolean autoScreenBrightness = SamplingLibrary.isAutoBrightness(context);
    if (autoScreenBrightness)
        // Auto
        mySample.setScreenBrightness(-1);
    // boolean gpsEnabled = SamplingLibrary.getGpsEnabled(context);
    // Location providers
    List<String> enabledLocationProviders = SamplingLibrary.getEnabledLocationProviders(context);
    mySample.setLocationProviders(enabledLocationProviders);
    // TODO: not in Sample yet
    // int maxNumSatellite = SamplingLibrary.getMaxNumSatellite(context);
    String network = SamplingLibrary.getNetworkStatus(context);
    String networkType = SamplingLibrary.getNetworkType(context);
    String mobileNetworkType = SamplingLibrary.getMobileNetworkType(context);
    // Required in new Carat protocol
    if (network.equals(NETWORKSTATUS_CONNECTED)) {
        if (networkType.equals("WIFI"))
            mySample.setNetworkStatus(networkType);
        else
            mySample.setNetworkStatus(mobileNetworkType);
    } else
        mySample.setNetworkStatus(network);
    // String ns = mySample.getNetworkStatus();
    // Log.d(STAG, "Set networkStatus="+ns);
    // Network details
    NetworkDetails nd = new NetworkDetails();
    // Network type
    nd.setNetworkType(networkType);
    nd.setMobileNetworkType(mobileNetworkType);
    boolean roamStatus = SamplingLibrary.getRoamingStatus(context);
    nd.setRoamingEnabled(roamStatus);
    String dataState = SamplingLibrary.getDataState(context);
    nd.setMobileDataStatus(dataState);
    String dataActivity = SamplingLibrary.getDataActivity(context);
    nd.setMobileDataActivity(dataActivity);
    // Wifi stuff
    String wifiState = SamplingLibrary.getWifiState(context);
    nd.setWifiStatus(wifiState);
    int wifiSignalStrength = SamplingLibrary.getWifiSignalStrength(context);
    nd.setWifiSignalStrength(wifiSignalStrength);
    int wifiLinkSpeed = SamplingLibrary.getWifiLinkSpeed(context);
    nd.setWifiLinkSpeed(wifiLinkSpeed);
    // Add NetworkDetails substruct to Sample
    mySample.setNetworkDetails(nd);
    /* Calling Information */
    // List<String> callInfo;
    // callInfo=SamplingLibrary.getCallInfo(context);
    /* Total call time */
    // long totalCallTime=0;
    // totalCallTime=SamplingLibrary.getTotalCallDur(context);
    /*
		 * long[] incomingOutgoingIdle = getCalltimesSinceBoot(context);
		 * Log.d(STAG, "Call time since boot: Incoming=" +
		 * incomingOutgoingIdle[0] + " Outgoing=" + incomingOutgoingIdle[1] +
		 * " idle=" + incomingOutgoingIdle[2]);
		 * 
		 * // Summary Call info CallInfo ci = new CallInfo(); String callState =
		 * SamplingLibrary.getCallState(context); ci.setCallStatus(callState);
		 * ci.setIncomingCallTime(incomingOutgoingIdle[0]);
		 * ci.setOutgoingCallTime(incomingOutgoingIdle[1]);
		 * ci.setNonCallTime(incomingOutgoingIdle[2]);
		 * 
		 * mySample.setCallInfo(ci);
		 */
    // Bundle b = intent.getExtras();
    int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
    // This is really an int.
    // FIXED: Not used yet, Sample needs more fields
    int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
    String batteryTechnology = intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
    // FIXED: Not used yet, Sample needs more fields
    String batteryHealth = "Unknown";
    String batteryStatus = "Unknown";
    switch(health) {
        case BatteryManager.BATTERY_HEALTH_DEAD:
            batteryHealth = "Dead";
            break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
            batteryHealth = "Good";
            break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
            batteryHealth = "Over voltage";
            break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
            batteryHealth = "Overheat";
            break;
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
            batteryHealth = "Unknown";
            break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
            batteryHealth = "Unspecified failure";
            break;
    }
    switch(status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            batteryStatus = "Charging";
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            batteryStatus = "Discharging";
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            batteryStatus = "Full";
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            batteryStatus = "Not charging";
            break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            batteryStatus = "Unknown";
            break;
        default:
            batteryStatus = lastBatteryState != null ? lastBatteryState : "Unknown";
    }
    // FIXED: Not used yet, Sample needs more fields
    String batteryCharger = "unplugged";
    switch(plugged) {
        case BatteryManager.BATTERY_PLUGGED_AC:
            batteryCharger = "ac";
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            batteryCharger = "usb";
            break;
    }
    BatteryDetails bd = new BatteryDetails();
    // otherInfo.setCPUIdleTime(totalIdleTime);
    // IMPORTANT: All of the battery details fields were never set (=always
    // zero), like the last battery level.
    // Now all must have been fixed.
    // current battery temperature in degrees Centigrade (the unit of the
    // temperature value
    // (returned by BatteryManager) is not Centigrade, it should be divided
    // by 10)
    int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0) / 10;
    bd.setBatteryTemperature(temperature);
    // otherInfo.setBatteryTemperature(temperature);
    // current battery voltage in VOLTS (the unit of the returned value by
    // BatteryManager is millivolts)
    double voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0) / 1000;
    bd.setBatteryVoltage(voltage);
    // otherInfo.setBatteryVoltage(voltage);
    bd.setBatteryTechnology(batteryTechnology);
    bd.setBatteryCharger(batteryCharger);
    bd.setBatteryHealth(batteryHealth);
    mySample.setBatteryDetails(bd);
    mySample.setBatteryLevel(currentBatteryLevel);
    mySample.setBatteryState(batteryStatus);
    int[] usedFreeActiveInactive = SamplingLibrary.readMeminfo();
    if (usedFreeActiveInactive != null && usedFreeActiveInactive.length == 4) {
        mySample.setMemoryUser(usedFreeActiveInactive[0]);
        mySample.setMemoryFree(usedFreeActiveInactive[1]);
        mySample.setMemoryActive(usedFreeActiveInactive[2]);
        mySample.setMemoryInactive(usedFreeActiveInactive[3]);
    }
    // TODO: Memory Wired should have memory that is "unevictable", that
    // will always be used even when all apps are killed
    // Log.d(STAG, "serial=" + getBuildSerial());
    // Record second data point for cpu/idle time
    now = System.currentTimeMillis();
    long[] idleAndCpu2 = readUsagePoint();
    CpuStatus cs = new CpuStatus();
    cs.setCpuUsage(getUsage(idleAndCpu1, idleAndCpu2));
    cs.setUptime(getUptime());
    mySample.setCpuStatus(cs);
    mySample.setDeveloperMode(isDeveloperModeOn(context));
    mySample.setUnknownSources(allowUnknownSources(context));
    mySample.setScreenOn(isScreenOn(context));
    mySample.setTimeZone(getTimeZone(context));
    // printAverageFeaturePower(context);
    // If there are extra fields, include them into the sample.
    List<Feature> extras = getExtras(context);
    if (extras != null && extras.size() > 0)
        mySample.setExtra(extras);
    return mySample;
}
Also used : CpuStatus(edu.berkeley.cs.amplab.carat.thrift.CpuStatus) SharedPreferences(android.content.SharedPreferences) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) NetworkDetails(edu.berkeley.cs.amplab.carat.thrift.NetworkDetails) RunningAppProcessInfo(android.app.ActivityManager.RunningAppProcessInfo) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature) BatteryDetails(edu.berkeley.cs.amplab.carat.thrift.BatteryDetails)

Example 2 with CpuStatus

use of edu.berkeley.cs.amplab.carat.thrift.CpuStatus in project carat by amplab.

the class SampleReader method readSample.

/**
     * Read a Sample from a HashMap stored in the Carat Sample db.
     * @param data
     * @return
     */
public static final Sample readSample(Object data) {
    Sample s = null;
    if (data != null && data instanceof HashMap<?, ?>) {
        HashMap<String, String> m = (HashMap<String, String>) data;
        s = new Sample();
        NetworkDetails n = new NetworkDetails();
        BatteryDetails bd = new BatteryDetails();
        // CellInfo ci = new CellInfo();
        // CallInfo calli = new CallInfo();
        // CallMonth cm = new CallMonth();
        CpuStatus cs = new CpuStatus();
        // Set single fields automatically:
        for (String k : m.keySet()) {
            _Fields sf = Sample._Fields.findByName(k);
            if (sf != null) {
                // Top level Sample field.
                FieldMetaData md = Sample.metaDataMap.get(sf);
                String cleaned = origStr(m.get(k));
                switch(md.valueMetaData.type) {
                    case org.apache.thrift.protocol.TType.STRING:
                        s.setFieldValue(sf, cleaned);
                        break;
                    case org.apache.thrift.protocol.TType.I32:
                        try {
                            s.setFieldValue(sf, Integer.parseInt(cleaned));
                        } catch (NumberFormatException e) {
                            Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                        }
                        break;
                    case org.apache.thrift.protocol.TType.DOUBLE:
                        try {
                            s.setFieldValue(sf, Double.parseDouble(cleaned));
                        } catch (NumberFormatException e) {
                            Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                        }
                        break;
                    case org.apache.thrift.protocol.TType.STRUCT:
                        if (md.fieldName.equals(Sample._Fields.NETWORK_DETAILS.getFieldName())) {
                            fillNetworkDetails(m.get(k), n);
                            s.setNetworkDetails(n);
                        } else if (md.fieldName.equals(Sample._Fields.BATTERY_DETAILS.getFieldName())) {
                            fillBatteryDetails(m.get(k), bd);
                            s.setBatteryDetails(bd);
                        } else if (md.fieldName.equals(Sample._Fields.CPU_STATUS.getFieldName())) {
                            fillCpuStatusDetails(m.get(k), cs);
                            s.setCpuStatus(cs);
                        }
                        /*
                          * else if (md.fieldName.equals("CallInfo")){ }
                          */
                        break;
                    case org.apache.thrift.protocol.TType.LIST:
                        if (md.fieldName.equals(Sample._Fields.EXTRA.getFieldName())) {
                            List<Feature> list = new LinkedList<Feature>();
                            String[] extras = m.get(k).split("\n");
                            for (String e : extras) {
                                Feature f = new Feature();
                                String[] feat = e.split(";");
                                if (feat.length > 1) {
                                    f.setKey(origStr(feat[0]));
                                    f.setValue(origStr(feat[1]));
                                }
                                list.add(f);
                            }
                            s.setExtra(list);
                        } else if (md.fieldName.equals(Sample._Fields.LOCATION_PROVIDERS.getFieldName())) {
                            List<String> list = new LinkedList<String>();
                            String[] arr = m.get(k).split("\n");
                            for (String lp : arr) list.add(lp);
                            s.setLocationProviders(list);
                        } else if (md.fieldName.equals(Sample._Fields.PI_LIST.getFieldName())) {
                            // Set piList fields automatically:
                            LinkedList<ProcessInfo> piList = new LinkedList<ProcessInfo>();
                            String[] processes = m.get(md.fieldName).split("\n");
                            for (String process : processes) {
                                String[] items = process.split(";");
                                ProcessInfo pi = new ProcessInfo();
                                /*
                                 * Items are in the same order as they appear in ProcessInfo
                                 * protocol class, so I can use Thrift ID for setting the fields
                                 * automatically.
                                 */
                                for (int i = 1; i <= items.length; i++) {
                                    if (items[i - 1] == null)
                                        continue;
                                    ProcessInfo._Fields pif = ProcessInfo._Fields.findByThriftId(i);
                                    FieldMetaData pmd = ProcessInfo.metaDataMap.get(pif);
                                    cleaned = origStr(items[i - 1]);
                                    switch(pmd.valueMetaData.type) {
                                        case org.apache.thrift.protocol.TType.STRING:
                                            pi.setFieldValue(pif, cleaned);
                                            break;
                                        case org.apache.thrift.protocol.TType.I32:
                                            try {
                                                pi.setFieldValue(pif, Integer.parseInt(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.DOUBLE:
                                            try {
                                                pi.setFieldValue(pif, Double.parseDouble(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.BOOL:
                                            try {
                                                pi.setFieldValue(pif, Boolean.parseBoolean(cleaned));
                                            } catch (NumberFormatException e) {
                                                Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a bool");
                                            }
                                            break;
                                        case org.apache.thrift.protocol.TType.LIST:
                                            List<String> list = new LinkedList<String>();
                                            String[] arr = cleaned.split("#");
                                            for (String sig : arr) list.add(sig);
                                            pi.setFieldValue(pif, list);
                                            break;
                                        default:
                                    }
                                }
                                piList.add(pi);
                            }
                            s.setPiList(piList);
                        }
                        break;
                    default:
                }
            }
        }
    }
    return s;
}
Also used : CpuStatus(edu.berkeley.cs.amplab.carat.thrift.CpuStatus) Sample._Fields(edu.berkeley.cs.amplab.carat.thrift.Sample._Fields) HashMap(java.util.HashMap) Sample(edu.berkeley.cs.amplab.carat.thrift.Sample) NetworkDetails(edu.berkeley.cs.amplab.carat.thrift.NetworkDetails) ProcessInfo(edu.berkeley.cs.amplab.carat.thrift.ProcessInfo) Feature(edu.berkeley.cs.amplab.carat.thrift.Feature) LinkedList(java.util.LinkedList) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) BatteryDetails(edu.berkeley.cs.amplab.carat.thrift.BatteryDetails) List(java.util.List) LinkedList(java.util.LinkedList)

Example 3 with CpuStatus

use of edu.berkeley.cs.amplab.carat.thrift.CpuStatus in project carat by amplab.

the class SampleReader method fillCpuStatusDetails.

private static void fillCpuStatusDetails(String string, CpuStatus cs) {
    String[] items = string.split("\n");
    for (int i = 1; i <= items.length; i++) {
        CpuStatus._Fields pif = CpuStatus._Fields.findByThriftId(i);
        FieldMetaData md = CpuStatus.metaDataMap.get(pif);
        String cleaned = origStr(items[i - 1]);
        switch(md.valueMetaData.type) {
            case org.apache.thrift.protocol.TType.STRING:
                cs.setFieldValue(pif, cleaned);
                break;
            case org.apache.thrift.protocol.TType.I32:
                try {
                    cs.setFieldValue(pif, Integer.parseInt(cleaned));
                } catch (NumberFormatException e) {
                    Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as an int");
                }
                break;
            case org.apache.thrift.protocol.TType.DOUBLE:
                try {
                    cs.setFieldValue(pif, Double.parseDouble(cleaned));
                } catch (NumberFormatException e) {
                    Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a double");
                }
                break;
            case org.apache.thrift.protocol.TType.BOOL:
                try {
                    cs.setFieldValue(pif, Boolean.parseBoolean(cleaned));
                } catch (NumberFormatException e) {
                    Log.e(TAG, "Could not read " + md.fieldName + ": \"" + cleaned + "\" as a bool");
                }
                break;
            case org.apache.thrift.protocol.TType.LIST:
                List<String> list = new LinkedList<String>();
                String[] arr = items[i - 1].split(";");
                for (String sig : arr) list.add(sig);
                cs.setFieldValue(pif, list);
                break;
            default:
        }
    }
}
Also used : CpuStatus(edu.berkeley.cs.amplab.carat.thrift.CpuStatus) FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) LinkedList(java.util.LinkedList)

Aggregations

CpuStatus (edu.berkeley.cs.amplab.carat.thrift.CpuStatus)3 BatteryDetails (edu.berkeley.cs.amplab.carat.thrift.BatteryDetails)2 Feature (edu.berkeley.cs.amplab.carat.thrift.Feature)2 NetworkDetails (edu.berkeley.cs.amplab.carat.thrift.NetworkDetails)2 ProcessInfo (edu.berkeley.cs.amplab.carat.thrift.ProcessInfo)2 Sample (edu.berkeley.cs.amplab.carat.thrift.Sample)2 LinkedList (java.util.LinkedList)2 FieldMetaData (org.apache.thrift.meta_data.FieldMetaData)2 RunningAppProcessInfo (android.app.ActivityManager.RunningAppProcessInfo)1 SharedPreferences (android.content.SharedPreferences)1 Sample._Fields (edu.berkeley.cs.amplab.carat.thrift.Sample._Fields)1 HashMap (java.util.HashMap)1 List (java.util.List)1