Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / extension / api / device_capabilities / DeviceCapabilitiesCPU.java
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.xwalk.core.internal.extension.api.device_capabilities;
6
7 import android.util.Log;
8
9 import java.io.RandomAccessFile;
10 import java.io.IOException;
11
12 import org.json.JSONException;
13 import org.json.JSONObject;
14
15 class DeviceCapabilitiesCPU {
16     private static final String SYSTEM_INFO_STAT_FILE = "/proc/stat";
17     private static final String TAG = "DeviceCapabilitiesCPU";
18
19     private DeviceCapabilities mDeviceCapabilities;
20
21     private int mCoreNum = 0;
22     private String mCPUArch = "Unknown";
23     private double mCPULoad = 0.0;
24
25     public DeviceCapabilitiesCPU(DeviceCapabilities instance) {
26         mDeviceCapabilities = instance;
27
28         // Get arch and core number at constructor since they won't change time to time.
29         mCoreNum = Runtime.getRuntime().availableProcessors();
30         mCPUArch = System.getProperty("os.arch");
31     }
32
33     public JSONObject getInfo() {
34         getCPULoad();
35
36         JSONObject out = new JSONObject();
37         try {
38             out.put("numOfProcessors", mCoreNum);
39             out.put("archName", mCPUArch);
40             out.put("load", mCPULoad);
41         } catch (JSONException e) {
42             return mDeviceCapabilities.setErrorMessage(e.toString());
43         }
44
45         return out;
46     }
47
48     /**
49      * The algorithm here can be found at:
50      * http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c
51      */
52     private boolean getCPULoad() {
53         try {
54             RandomAccessFile file = new RandomAccessFile(SYSTEM_INFO_STAT_FILE, "r");
55             String line = file.readLine();
56
57             String[] arrs = line.split("\\s+");
58             long total1 = 0;
59             for (int i = 1; i < arrs.length; ++i) {
60                 total1 += Long.parseLong(arrs[i]);
61             }
62             // arrs[4] is the time spent in idle tasks.
63             long used1 = total1 - Long.parseLong(arrs[4]);
64             try {
65                 Thread.sleep(1000);
66             } catch (Exception e) {
67                 mCPULoad = 0.0;
68                 return false;
69             }
70
71             file.seek(0);
72             line = file.readLine();
73             file.close();
74
75             arrs = line.split("\\s+");
76             long total2 = 0;
77             for (int i = 1; i < arrs.length; ++i) {
78                 total2 += Long.parseLong(arrs[i]);
79             }
80             // arrs[4] is the time spent in idle tasks.
81             long used2 = total2 - Long.parseLong(arrs[4]);
82             if (total2 == total1) {
83                 mCPULoad = 0.0;
84             } else {
85                 mCPULoad = (double) (used2 - used1) / (total2 - total1);
86             }
87         } catch (IOException e) {
88             mCPULoad = 0.0;
89             return false;
90         }
91         return true;
92     }
93 }