-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestMem.hpp
More file actions
executable file
·199 lines (140 loc) · 3.36 KB
/
testMem.hpp
File metadata and controls
executable file
·199 lines (140 loc) · 3.36 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
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#define VMRSS_LINE 17
#define VMSIZE_LINE 13
#define PROCESS_ITEM 14
typedef struct {
unsigned long user;
unsigned long nice;
unsigned long system;
unsigned long idle;
}Total_Cpu_Occupy_t;
typedef struct {
unsigned int pid;
unsigned long utime; //user time
unsigned long stime; //kernel time
unsigned long cutime; //all user time
unsigned long cstime; //all dead time
}Proc_Cpu_Occupy_t;
const char* get_items(const char*buffer ,unsigned int item){
const char *p =buffer;
int len = strlen(buffer);
int count = 0;
for (int i=0; i<len;i++){
if (' ' == *p){
count ++;
if(count == item -1){
p++;
break;
}
}
p++;
}
return p;
}
unsigned long get_cpu_total_occupy(){
FILE *fd;
char buff[1024]={0};
Total_Cpu_Occupy_t t;
fd =fopen("/proc/stat","r");
if (nullptr == fd){
return 0;
}
fgets(buff,sizeof(buff),fd);
char name[64]={0};
sscanf(buff,"%s %ld %ld %ld %ld",name,&t.user,&t.nice,&t.system,&t.idle);
fclose(fd);
return (t.user + t.nice + t.system + t.idle);
}
unsigned long get_cpu_proc_occupy(unsigned int pid){
char file_name[64]={0};
Proc_Cpu_Occupy_t t;
FILE *fd;
char line_buff[1024]={0};
sprintf(file_name,"/proc/%d/stat",pid);
fd = fopen(file_name,"r");
if(nullptr == fd){
return 0;
}
fgets(line_buff,sizeof(line_buff),fd);
sscanf(line_buff,"%u",&t.pid);
const char *q =get_items(line_buff,PROCESS_ITEM);
sscanf(q,"%ld %ld %ld %ld",&t.utime,&t.stime,&t.cutime,&t.cstime);
fclose(fd);
return (t.utime + t.stime + t.cutime + t.cstime);
}
float get_proc_cpu(unsigned int pid){
unsigned long totalcputime1,totalcputime2;
unsigned long procputime1,procputime2;
totalcputime1=get_cpu_total_occupy();
procputime1=get_cpu_proc_occupy(pid);
usleep(200000);
totalcputime2=get_cpu_total_occupy();
procputime2=get_cpu_proc_occupy(pid);
float pcpu = 0.0;
if(0 != totalcputime2-totalcputime1){
pcpu=100.0 * (procputime2-procputime1)/(totalcputime2-totalcputime1);
}
return pcpu;
}
unsigned int get_proc_mem(unsigned int pid){
char file_name[64]={0};
FILE *fd;
char line_buff[512]={0};
sprintf(file_name,"/proc/%d/status",pid);
fd =fopen(file_name,"r");
if(nullptr == fd){
return 0;
}
char name[64];
int vmrss;
for (int i=0; i<VMRSS_LINE-1;i++){
fgets(line_buff,sizeof(line_buff),fd);
}
fgets(line_buff,sizeof(line_buff),fd);
sscanf(line_buff,"%s %d",name,&vmrss);
fclose(fd);
return vmrss;
}
unsigned int get_proc_virtualmem(unsigned int pid){
char file_name[64]={0};
FILE *fd;
char line_buff[512]={0};
sprintf(file_name,"/proc/%d/status",pid);
fd =fopen(file_name,"r");
if(nullptr == fd){
return 0;
}
char name[64];
int vmsize;
for (int i=0; i<VMSIZE_LINE-1;i++){
fgets(line_buff,sizeof(line_buff),fd);
}
fgets(line_buff,sizeof(line_buff),fd);
sscanf(line_buff,"%s %d",name,&vmsize);
fclose(fd);
return vmsize;
}
int get_pid(const char* process_name, const char* user = nullptr)
{
if(user == nullptr){
user = getlogin();
}
char cmd[512];
if (user){
sprintf(cmd, "pgrep %s -u %s", process_name, user);
}
FILE *pstr = popen(cmd,"r");
if(pstr == nullptr){
return 0;
}
char buff[512];
::memset(buff, 0, sizeof(buff));
if(NULL == fgets(buff, 512, pstr)){
return 0;
}
return atoi(buff);
}