/* gcc -W -Wall -o pageslack pageslack.c */ #include #include #include #include #include #include #include #define PAGE_SIZE (1UL << PAGE_SHIFT) #define PAGE_MASK (~(PAGE_SIZE - 1)) unsigned long long total; unsigned long long slack[5]; void do_dir(const char *name) { DIR *dir; struct dirent *ent; dir = opendir(name); if (!dir) { perror("opendir"); exit(EXIT_FAILURE); } while ((ent = readdir(dir))) { struct stat buf; char path[PATH_MAX]; if (!strcmp(ent->d_name, ".")) continue; if (!strcmp(ent->d_name, "..")) continue; sprintf(path, "%s/%s", name, ent->d_name); if (stat(path, &buf)) { perror("stat"); exit(EXIT_FAILURE); } if (S_ISDIR(buf.st_mode)) { do_dir(path); continue; } if (S_ISREG(buf.st_mode)) { int i; for (i = 0; i < 5; i++) { unsigned long PAGE_SHIFT = 12 + i; slack[i] += (PAGE_SIZE - (buf.st_size % PAGE_SIZE)) % PAGE_SIZE; } total += buf.st_size; } } if (closedir(dir)) { perror("closedir"); exit(EXIT_FAILURE); } } int main(void) { do_dir("."); printf("total\t: %llu\n", total); printf(" 4k\t: %llu (%llu%%)\n", slack[0], (100 * slack[0]) / total); printf(" 8k\t: %llu (%llu%%)\n", slack[1], (100 * slack[1]) / total); printf("16k\t: %llu (%llu%%)\n", slack[2], (100 * slack[2]) / total); printf("32k\t: %llu (%llu%%)\n", slack[3], (100 * slack[3]) / total); printf("64k\t: %llu (%llu%%)\n", slack[4], (100 * slack[4]) / total); return EXIT_SUCCESS; }