-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmaps.c
More file actions
193 lines (186 loc) · 4.05 KB
/
Copy pathsmaps.c
File metadata and controls
193 lines (186 loc) · 4.05 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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include "psmem.h"
int read_proc(struct program* prog, const char *pid_dir) {
char line[BUFSIZ];
/* pid_dir = /proc/3152
* pid_max 32 bit is 32768
* sp is smaps_rollup */
FILE *sp;
double private = 0;
double private_huge = 0;
/*double shared_huge = 0;*/
double pss = 0;
double shared = 0;
int have_pss = 0;
if (chdir(pid_dir) < 0) {
return -1;
}
/* need to deal with the case that we have a really long
* absolute directory, perhaps change to just cd'ing into
* /proc and doing all our magic there */
sp = fopen("smaps_rollup", "r");
if (!sp) {
return -1;
}
while (fgets(line, sizeof(line), sp)) {
char key[32];
double value;
/* read key until : ignoring :, then get our number*/
int ret = sscanf(line, "%31[^:]: %lf", key, &value);
if (ret == 2) {
if (strcmp(key, "Private_Clean") == 0) {
private += value;
}
if (strcmp(key, "Private_Hugetlb") == 0) {
private_huge += value;
}
if (strcmp(key, "Private_Dirty") == 0) {
private += value;
}
if (strcmp(key, "Shared_Clean") == 0) {
shared += value;
}
if (strcmp(key, "Shared_Dirty") == 0) {
shared += value;
}
if (strcmp(key, "Shared_Hugetlb") == 0) {
/* shared_huge += value;*/
}
if (strcmp(key, "Pss") == 0) {
have_pss = 1;
pss += value + 0.5;
}
}
}
if (have_pss) {
shared = pss - private;
}
/* missing shared_huge usage*/
private += private_huge;
prog->shared_mem = shared;
prog->private_mem = private;
prog->count = 0;
if(fclose(sp) == EOF) {
return -1;
}
if (getCmdName(prog->name, pid_dir) != 0) {
return -1;
}
return 0;
}
static int getParentName(char* cmdName, const char* pid_dir) {
FILE *cp;
char cmdline[BUFSIZ];
char path[PATH_SIZE];
memset(&path, 0, sizeof(path));
if (chdir(pid_dir) < 0) {
return -1;
}
cp = fopen("cmdline", "r");
if (!cp) {
return -1;
}
if (!fgets(cmdline, sizeof(cmdline), cp)) {
fclose(cp);
return -1;
}
/*missing while cmdline[-1] thing unsure of what it does */
if (fclose(cp) == EOF) {
return -1;
}
if (readlink("exe", path, sizeof(path)) == -1) {
return -1;
}
/*missing check for (deleted) and (updated) */
if (snprintf(cmdName, NAME_SIZE, "%s", basename(path)) < 0) {
return -1;
}
return 0;
}
int getCmdName(char* cmdName, const char* pid_dir) {
char cmdline[BUFSIZ];
FILE *cp,*pp;
char path[PATH_SIZE];
char exe[PATH_SIZE];
char proc_status[BUFSIZ];
char cmd[BUFSIZ];
char line[BUFSIZ];
char p_exe[PATH_SIZE];
int ppid = 0;
if (chdir(pid_dir) < 0) {
return -1;
}
memset(&p_exe, 0, sizeof(p_exe));
memset(&path, 0, sizeof(path));
cp = fopen("cmdline", "r");
if (!cp) {
return -1;
}
if(!fgets(cmdline, sizeof(cmdline), cp)) {
return -1;
}
/*missing while cmdline[-1] thing unsure of what it does */
if (fclose(cp) == EOF) {
return -1;
}
if (readlink("exe", path, sizeof(path)) == -1) {
return -1;
}
/*missing check for (deleted) and (updated) */
if (snprintf(exe,sizeof(exe), "%s", basename(path)) < 0) {
return -1;
}
pp = fopen("status", "r");
if (!pp) {
return -1;
}
if (!fgets(proc_status, sizeof(proc_status), pp)) {
fclose(pp);
return -1;
}
if (sscanf(proc_status, "%*31[^:]: %s",cmd) == EOF) {
fclose(pp);
return -1;
}
if (strncmp(cmd,exe,strlen(cmd)) == 0) {
if (snprintf(cmd, sizeof(cmd), "%s", exe) < 0) {
fclose(pp);
return -1;
}
} else {
while(fgets(line, sizeof(line), pp)) {
char key[32];
int value;
int ret = sscanf(line, "%31[^:]: %d", key, &value);
if (ret == 2 && strcmp(key, "PPid") == 0) {
ppid = value;
break;
}
}
if (ppid) {
int ret;
char newPath[PATH_SIZE];
if (snprintf(newPath,sizeof(newPath), "/proc/%d", ppid) < 0) {
fclose(pp);
return -1;
}
ret = getParentName(p_exe, newPath);
if (ret == 0 && strcmp(exe,p_exe) == 0) {
if (snprintf(cmd, sizeof(cmd), "%s", exe) < 0) {
fclose(pp);
return -1;
}
}
}
}
if (fclose(pp) == EOF) {
return -1;
}
if (snprintf(cmdName, NAME_SIZE, "%s", cmd) < 0) {
return -1;
}
return 0;
}