fix(Memory): fix reading procfs files and simplify data parsing

Files in procfs are always changed by kernel.

It means if we read the data with more than one read syscall,
we could get an incorrect data which was broken by kernel.

A typical case about this problem was reported by psutil in
https://github.com/giampaolo/psutil/issues/2050

Using an big buffer let read reads the whole file can fix this.
psutil uses a 32K buffer while procps-ng uses a 8K buffer, i think a 8K buffer is enough.
This commit is contained in:
apocelipes
2023-12-04 16:47:00 +09:00
committed by Carter Li
parent 182c90b22c
commit 6eae425679
2 changed files with 27 additions and 30 deletions
+4
View File
@@ -11,6 +11,10 @@
#include <unistd.h>
#include <dirent.h>
typedef int FFNativeFD;
// procfs's file can be changed between read calls such as /proc/meminfo and /proc/uptime.
// one safe way to read correct data is reading the whole file in a single read syscall
// 8192 comes from procps-ng: https://gitlab.com/procps-ng/procps/-/blob/master/library/meminfo.c?ref_type=heads#L39
#define PROC_FILE_BUFFSIZ 8192
#endif
static inline FFNativeFD FFUnixFD2NativeFD(int unixfd)