16 // Initial size of the array holding struct file_info
17 #define INITIAL_NUM_FILES 512
19 // Max number of file descriptors to use for ntfw
25 size_t num_cached_pages;
28 // Size of pages on this system
29 static int g_page_size;
31 // Total number of cached pages found so far
32 static size_t g_total_cached = 0;
34 // Total number of files scanned so far
35 static size_t g_num_files = 0;
37 // Scanned files and their associated cached page counts
38 static struct file_info **g_files;
40 // Current size of files array
43 static struct file_info *get_file_info(const char* fpath, size_t file_size) {
44 struct file_info *info;
45 if (g_num_files >= g_files_size) {
46 g_files = realloc(g_files, 2 * g_files_size * sizeof(struct file_info*));
48 fprintf(stderr, "Couldn't allocate space for files array: %s\n", strerror(errno));
51 g_files_size = 2 * g_files_size;
54 info = calloc(1, sizeof(*info));
56 fprintf(stderr, "Couldn't allocate space for file struct: %s\n", strerror(errno));
60 info->name = malloc(strlen(fpath) + 1);
62 fprintf(stderr, "Couldn't allocate space for file struct: %s\n", strerror(errno));
65 strcpy(info->name, fpath);
67 info->num_cached_pages = 0;
68 info->file_size = file_size;
70 g_files[g_num_files++] = info;
75 static int store_num_cached(const char* fpath, const struct stat *sb) {
77 fd = open (fpath, O_RDONLY);
80 fprintf(stderr, "Could not open file: %s\n", fpath);
84 void* mapped_addr = mmap(NULL, sb->st_size, PROT_NONE, MAP_SHARED, fd, 0);
86 if (mapped_addr != MAP_FAILED) {
87 // Calculate bit-vector size
88 size_t num_file_pages = (sb->st_size + g_page_size - 1) / g_page_size;
89 unsigned char* mincore_data = calloc(1, num_file_pages);
90 ret = mincore(mapped_addr, sb->st_size, mincore_data);
93 unsigned int page = 0;
94 for (page = 0; page < num_file_pages; page++) {
95 if (mincore_data[page]) num_cached++;
98 struct file_info *info = get_file_info(fpath, sb->st_size);
99 info->num_cached_pages += num_cached;
100 g_total_cached += num_cached;
103 munmap(mapped_addr, sb->st_size);
110 static int scan_entry(const char *fpath, const struct stat *sb, int typeflag,
111 struct FTW * __attribute__((unused))ftwbuf) {
112 if (typeflag == FTW_F) {
113 store_num_cached(fpath, sb);
118 static int cmpsize(size_t a, size_t b) {
119 if (a < b) return -1;
124 static int cmpfiles(const void *a, const void *b) {
125 return cmpsize((*((struct file_info**)a))->num_cached_pages,
126 (*((struct file_info**)b))->num_cached_pages);
132 g_page_size = getpagesize();
134 g_files = malloc(INITIAL_NUM_FILES * sizeof(struct file_info*));
135 g_files_size = INITIAL_NUM_FILES;
137 // Walk filesystem trees through procfs except rootfs/devfs/sysfs/procfs
138 FILE* fp = setmntent("/proc/mounts", "r");
140 fprintf(stderr, "Error opening /proc/mounts\n");
143 struct mntent* mentry;
144 while ((mentry = getmntent(fp)) != NULL) {
145 if (strcmp(mentry->mnt_type, "rootfs") != 0 &&
146 strncmp("/dev", mentry->mnt_dir, strlen("/dev")) != 0 &&
147 strncmp("/sys", mentry->mnt_dir, strlen("/sys")) != 0 &&
148 strncmp("/proc", mentry->mnt_dir, strlen("/proc")) != 0) {
149 nftw(mentry->mnt_dir, &scan_entry, MAX_NUM_FD, FTW_MOUNT | FTW_PHYS | FTW_DEPTH);
155 qsort(g_files, g_num_files, sizeof(g_files[0]), &cmpfiles);
158 for (i = 0; i < g_num_files; i++) {
159 struct file_info *info = g_files[i];
160 fprintf(stdout, "%s: %zu cached pages (%.2f MB, %zu%% of total file size.)\n", info->name,
161 info->num_cached_pages,
162 (float) (info->num_cached_pages * g_page_size) / 1024 / 1024,
163 (100 * info->num_cached_pages * g_page_size) / info->file_size);
166 fprintf(stdout, "TOTAL CACHED: %zu pages (%f MB)\n", g_total_cached,
167 (float) (g_total_cached * 4096) / 1024 / 1024);