Untitled
unknown
plain_text
6 months ago
1.7 kB
3
Indexable
Never
// Function to get the physical memory sizes for a process void get_physical_memory_sizes(int pid, unsigned long* exclusiveSize, unsigned long* totalSize) { char filename[128]; snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); FILE* file = fopen(filename, "r"); if (file == NULL) { perror("Error opening file"); return; } unsigned long exclusivePages = 0; unsigned long totalPages = 0; char line[256]; while (fgets(line, sizeof(line), file)) { unsigned long start, end; if (sscanf(line, "%lx-%lx", &start, &end) != 2) { perror("Error parsing line"); fclose(file); return; } unsigned long startPage = start / PAGE_SIZE; unsigned long endPage = end / PAGE_SIZE; for (unsigned long page = startPage; page <= endPage; ++page) { char pgMap[128]; snprintf(pgMap, sizeof(pgMap), "/proc/%d/pagemap", pid); unsigned long pfn = read_file(pgMap, page); unsigned long temp = pfn & (1UL << 63); pfn = (pfn << 9) >> 9; if (temp == 0) { printf("irrr \n"); continue; } unsigned long mappingCount = read_file("/proc/kpagecount", pfn); if (mappingCount == 0) { continue; } if (mappingCount == 1){ exclusivePages++; } totalPages++; } } fclose(file); *exclusiveSize = exclusivePages * PAGE_SIZE; *totalSize = totalPages * PAGE_SIZE; }