forked from alipay/SoloPi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_usage.c
More file actions
301 lines (253 loc) · 8.67 KB
/
cpu_usage.c
File metadata and controls
301 lines (253 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright (C) 2015-present, Ant Financial Services Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by Cathor on 17/8/29.
//
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <string.h>
#include <stdlib.h>
#include <android/log.h>
#include <stdio.h>
#define TAG "CPU_JNI" // 这个是自定义的LOG的标识
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定义LOGD类型
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) // 定义LOGI类型
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) // 定义LOGI类型
typedef struct PidData{
struct PidData* next;
int pid;
long long previousAppUsage;
} PidData;
static PidData root = {
.next = NULL,
.pid = -1,
.previousAppUsage = 0
};
static long long previousTotalUsage = 0;
static long long previousIdle = 0;
/*
* 获取总CPU占用率
* Class: com_alipay_hulu_shared_display_items_CPUTools
* Method: getTotalUsage
* Signature: ()F
*/
jfloat Java_com_alipay_hulu_shared_display_items_CPUTools_getTotalUsage(JNIEnv* env, jclass clz) {
FILE * pFile;
char* filename = "/proc/stat";
pFile = fopen(filename, "r");
if (pFile == NULL) {
return 0;
}
char* line = (char*) malloc(1024);
char* tmp;
fgets(line, 1024, pFile);
char* pch = strtok_r(line," \n", &tmp);
int count = 0;
long long currentTotalUsage = 0;
long long currentIdle = 0;
while (pch != NULL) {
if (count > 7) {
break;
}
if (count == 4) {
currentIdle = atoll(pch);
}
if (count > 0) {
currentTotalUsage += atoll(pch);
}
pch = strtok_r(NULL, " \n", &tmp);
count++;
}
free(line);
fclose(pFile);
if (previousTotalUsage == 0L) {
previousTotalUsage = currentTotalUsage;
previousIdle = currentIdle;
return 0;
}
// 计算时间段内cpu运行时间与idle时间
long cpuTotalTime = currentTotalUsage - previousTotalUsage;
long cpuIdleTime = currentIdle - previousIdle;
// 转化为double计算结果
double result = 100 * (cpuTotalTime - cpuIdleTime) / (double) cpuTotalTime;
// 同步到上一次结果
previousTotalUsage = currentTotalUsage;
previousIdle = currentIdle;
return (float)result;
}
static int count = 0;
/*
* 获取应用CPU占用率与CPU总占用率
* Class: com_alipay_hulu_shared_display_items_CPUTools
* Method: getAppTotalUsage
* Signature: (I)[F
*/
jfloatArray
Java_com_alipay_hulu_shared_display_items_CPUTools_getAppTotalUsage(JNIEnv *env, jclass clz, jintArray pids) {
jboolean b;
jint *realPids = (*env)->GetIntArrayElements(env, pids, &b);
int length = (*env)->GetArrayLength(env, pids);
FILE *cpuFile;
FILE **appFiles = (FILE**) malloc(sizeof(FILE*) * length);
float *data = (float *) malloc(sizeof(float) * (1 + length));
// 尽量保证同时打开文件,同时获取文件数据
cpuFile = fopen("/proc/stat", "r");
if (cpuFile == NULL) {
free(appFiles);
LOGI("Open CPU file failed");
jfloatArray result = (*env)->NewFloatArray(env, 0);
free(data);
(*env)->ReleaseIntArrayElements(env, pids, realPids, 0);
return result;
}
// 逐项格式化文件名
for (int i = 0; i < length; ++i) {
char filename[20];
sprintf(filename, "/proc/%d/stat", realPids[i]);
appFiles[i] = fopen(filename, "r");
}
// // 存在文件不能打开的情况,Android 7.0以上无法直接打开/proc/pid/stat
// if (appFile == NULL) {
// fclose(cpuFile);
// jfloat cpuUsage = Java_com_alipay_hulu_display_CPUTools_getTotalUsage(env, clz);
// LOGI("Open app file failed");
// jfloatArray result = (*env)->NewFloatArray(env, cpuUsage);
// free(data);
// return result;
// }
// 获取第一行数据
char *cpuLine = (char *) malloc(256);
fgets(cpuLine, 256, cpuFile);
char **appLines = (char **) malloc(sizeof(char *) * length);
for (int i = 0; i < length; ++i) {
if (appFiles[i] != NULL) {
char *appLine = (char *) malloc(256);
fgets(appLine, 256, appFiles[i]);
appLines[i] = appLine;
} else {
appLines[i] = NULL;
}
}
// 文件读取完毕,关闭文件
fclose(cpuFile);
for (int i = 0; i < length; ++i) {
if (appFiles[i] != NULL) {
fclose(appFiles[i]);
}
}
free(appFiles);
// 解析CPU数据
char* tmp;
char *pch = strtok_r(cpuLine, " \n", &tmp);
int count = 0;
long long currentTotalUsage = 0;
long long currentIdle = 0;
// 第2-8位为CPU各项运行时间,第5位为CPU空闲时间
while (pch != NULL) {
if (!pch || *pch == '\0') {
continue;
}
if (count > 7) {
break;
}
if (count == 4) {
currentIdle = atoll(pch);
}
if (count > 0) {
currentTotalUsage += atoll(pch);
}
pch = strtok_r(NULL, " \n", &tmp);
count++;
}
// 清理CPU行内存
free(cpuLine);
if (previousTotalUsage == 0L) {
previousTotalUsage = currentTotalUsage;
previousIdle = currentIdle;
jfloatArray result = (*env)->NewFloatArray(env, 0);
free(data);
(*env)->ReleaseIntArrayElements(env, pids, realPids, 0);
return result;
}
// 计算时间段内cpu运行时间与idle时间
long cpuTotalTime = currentTotalUsage - previousTotalUsage;
long cpuIdleTime = currentIdle - previousIdle;
double cpuUsage = 100 * (cpuTotalTime - cpuIdleTime) / (double) cpuTotalTime;
// 替换前一次数据
previousTotalUsage = currentTotalUsage;
previousIdle = currentIdle;
// 存入结果
data[length] = (float) cpuUsage;
for (int i = 0; i < length; ++i) {
// 对于空数据,直接跳过
if (appLines[i] == NULL) {
data[i] = 0;
continue;
}
char* save_tmp;
pch = strtok_r(appLines[i], " \n", &save_tmp);
count = 0;
long long currentAppUsage = 0;
// 计算应用占用总时间,从13到16位分别为用户态运行,核心态运行时间,已死线程用户态运行时间,已死线程核心态运行时间
while (pch != NULL) {
// 空字符,忽略
if (!pch || *pch == '\0') {
continue;
}
if (count > 16) {
break;
}
if (count > 12) {
currentAppUsage += atoll(pch);
}
pch = strtok_r(NULL, " \n", &save_tmp);
count++;
}
// 清理应用行内存
free(appLines[i]);
PidData *currentPidData = &root;
while (currentPidData->pid != realPids[i] && currentPidData->next != NULL) {
currentPidData = currentPidData->next;
}
LOGD("Pid: %d with data: %lld", realPids[i], currentAppUsage);
// 当pid不为目标pid,说明未找到,在末尾添加新的PidData
if (currentPidData->pid != realPids[i]) {
PidData *newItem = (PidData *) malloc(sizeof(PidData));
newItem->next = NULL;
newItem->previousAppUsage = 0;
newItem->pid = realPids[i];
currentPidData->next = newItem;
currentPidData = newItem;
}
// 未初始化,清理
if (currentPidData->previousAppUsage == 0) {
currentPidData->previousAppUsage = currentAppUsage;
data[i] = 0;
continue;
}
long appTime = currentAppUsage - currentPidData->previousAppUsage;
double appUsage = 100 * appTime / (double) cpuTotalTime;
data[i] = (float) appUsage;
// 替换前一次数据
currentPidData->previousAppUsage = currentAppUsage;
}
// 拷贝结果数据
jfloatArray result = (*env)->NewFloatArray(env, length + 1);
(*env)->SetFloatArrayRegion(env, result, 0, length + 1, data);
free(data);
(*env)->ReleaseIntArrayElements(env, pids, realPids, 0);
return result;
}