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