Skip to content

Commit fbc3d45

Browse files
committed
procrank: prefer new zram ABI
Starting with Linux 4.1, reading from /dev/block/zram*/mem_used_total logs this warning: "Attribute mem_used_total (and others) will be removed. See zram documentation." mem_used_total and several related counters are now consolidated under a new node mm_stat. Prefer that when it's available. Bug: 25951511 Change-Id: Ida732ee56a1e39e01c390f61bb40b3f52f643004 Signed-off-by: Greg Hackmann <[email protected]>
1 parent 763bdf6 commit fbc3d45

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

procrank/procrank.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,35 @@ void get_mem_info(uint64_t mem[]) {
144144
}
145145
}
146146

147+
static uint64_t get_zram_mem_used() {
148+
#define ZRAM_SYSFS "/sys/block/zram0/"
149+
FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
150+
if (f) {
151+
uint64_t mem_used_total = 0;
152+
153+
int matched = fscanf(f, "%*d %*d %" SCNu64 " %*d %*d %*d %*d", &mem_used_total);
154+
if (matched != 1)
155+
fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mm_stat\n");
156+
157+
fclose(f);
158+
return mem_used_total;
159+
}
160+
161+
f = fopen(ZRAM_SYSFS "mem_used_total", "r");
162+
if (f) {
163+
uint64_t mem_used_total = 0;
164+
165+
int matched = fscanf(f, "%" SCNu64, &mem_used_total);
166+
if (matched != 1)
167+
fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mem_used_total\n");
168+
169+
fclose(f);
170+
return mem_used_total;
171+
}
172+
173+
return 0;
174+
}
175+
147176
int main(int argc, char *argv[]) {
148177
pm_kernel_t *ker;
149178
pm_process_t *proc;
@@ -172,8 +201,6 @@ int main(int argc, char *argv[]) {
172201

173202
uint64_t mem[MEMINFO_COUNT] = { };
174203
pm_proportional_swap_t *p_swap;
175-
int fd, len;
176-
char buffer[1024];
177204
float zram_cr = 0.0;
178205

179206
signal(SIGPIPE, SIG_IGN);
@@ -277,17 +304,12 @@ int main(int argc, char *argv[]) {
277304
qsort(procs, num_procs, sizeof(procs[0]), compfn);
278305

279306
if (has_swap) {
280-
fd = open("/sys/block/zram0/mem_used_total", O_RDONLY);
281-
if (fd >= 0) {
282-
len = read(fd, buffer, sizeof(buffer)-1);
283-
close(fd);
284-
if (len > 0) {
285-
buffer[len] = 0;
286-
mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
287-
zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] /
288-
(mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]);
289-
has_zram = true;
290-
}
307+
uint64_t zram_mem_used = get_zram_mem_used();
308+
if (zram_mem_used) {
309+
mem[MEMINFO_ZRAM_TOTAL] = zram_mem_used/1024;
310+
zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] /
311+
(mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]);
312+
has_zram = true;
291313
}
292314
}
293315

0 commit comments

Comments
 (0)