void getPss(pid_t pid, size_t* pss_out) { pm_kernel_t* kernel; ASSERT_EQ(0, pm_kernel_create(&kernel)); pm_process_t* process; ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); pm_map_t** maps; size_t num_maps; ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); size_t total_pss = 0; for (size_t i = 0; i < num_maps; i++) { pm_memusage_t usage; ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); total_pss += usage.pss; } *pss_out = total_pss; free(maps); pm_process_destroy(process); pm_kernel_destroy(kernel); }
int main(int argc, char *argv[]) { pm_kernel_t *ker; pm_process_t *proc; pid_t *pids; size_t num_procs; size_t i; pm_map_t **maps; size_t num_maps; char cmdline[256]; // this must be within the range of int int error; int rc = EXIT_SUCCESS; uint8_t pr_flags = 0; struct ksm_pages kp; memset(&kp, 0, sizeof(kp)); opterr = 0; do { int c = getopt(argc, argv, "hvsa"); if (c == -1) break; switch (c) { case 'a': pr_flags |= PR_ALL; break; case 's': pr_flags |= PR_SORTED; break; case 'v': pr_flags |= PR_VERBOSE; break; case 'h': usage(argv[0]); exit(EXIT_SUCCESS); case '?': fprintf(stderr, "unknown option: %c\n", optopt); usage(argv[0]); exit(EXIT_FAILURE); } } while (1); error = pm_kernel_create(&ker); if (error) { fprintf(stderr, "Error creating kernel interface -- " "does this kernel have pagemap?\n"); exit(EXIT_FAILURE); } if (pr_flags & PR_ALL) { error = pm_kernel_pids(ker, &pids, &num_procs); if (error) { fprintf(stderr, "Error listing processes.\n"); exit(EXIT_FAILURE); } } else { if (optind != argc - 1) { usage(argv[0]); exit(EXIT_FAILURE); } pids = malloc(sizeof(*pids)); if (pids == NULL) { fprintf(stderr, "Error allocating pid memory\n"); exit(EXIT_FAILURE); } *pids = strtoul(argv[optind], NULL, 10); if (*pids == 0) { fprintf(stderr, "Invalid PID\n"); rc = EXIT_FAILURE; goto exit; } num_procs = 1; if (getprocname(*pids, cmdline, sizeof(cmdline)) < 0) { cmdline[0] = '\0'; } printf("%s (%u):\n", cmdline, *pids); } printf("Warning: this tool only compares the KSM CRCs of pages, there is a chance of " "collisions\n"); for (i = 0; i < num_procs; i++) { error = pm_process_create(ker, pids[i], &proc); if (error) { fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); rc = EXIT_FAILURE; goto exit; } error = pm_process_maps(proc, &maps, &num_maps); if (error) { pm_process_destroy(proc); fprintf(stderr, "warning: could not read process map for %d\n", pids[i]); rc = EXIT_FAILURE; goto exit; } if (read_pages(&kp, maps, num_maps, pr_flags) < 0) { free(maps); pm_process_destroy(proc); rc = EXIT_FAILURE; goto exit; } free(maps); pm_process_destroy(proc); } if (pr_flags & PR_SORTED) { qsort(kp.pages, kp.len, sizeof(*kp.pages), cmp_pages); } print_pages(&kp, pr_flags); exit: free_pages(&kp, pr_flags); free(pids); return rc; }
int main(int argc, char *argv[]) { char cmdline[256]; char *prefix; size_t prefix_len; int (*compfn)(const void *a, const void *b); pm_kernel_t *ker; pm_process_t *proc; pid_t *pids; size_t num_procs; pm_map_t **maps; size_t num_maps; pm_memusage_t map_usage; struct library_info *li, **lis; struct mapping_info *mi, **mis; struct process_info *pi; size_t i, j; int error; int perm; bool all; uint64_t required_flags; uint64_t flags_mask; bool has_swap = false; signal(SIGPIPE, SIG_IGN); compfn = &sort_by_pss; order = -1; prefix = NULL; prefix_len = 0; opterr = 0; perm = 0; all = false; required_flags = 0; flags_mask = 0; while (1) { int c; const struct option longopts[] = { {"all", 0, 0, 'a'}, {"cached", 0, 0, 'c'}, {"nocached", 0, 0, 'C'}, {"ksm", 0, 0, 'k'}, {"help", 0, 0, 'h'}, {"pss", 0, 0, 'p'}, {"uss", 0, 0, 'u'}, {"vss", 0, 0, 'v'}, {"rss", 0, 0, 'r'}, {"swap", 0, 0, 's'}, {"reverse", 0, 0, 'R'}, {"path", required_argument, 0, 'P'}, {"perm", required_argument, 0, 'm'}, {0, 0, 0, 0} }; c = getopt_long(argc, argv, "acChkm:pP:uvrsR", longopts, NULL); if (c < 0) { break; } /* Alphabetical cases */ switch (c) { case 'a': all = true; break; case 'c': required_flags = 0; flags_mask = PM_PAGE_SWAPBACKED; break; case 'C': required_flags = PM_PAGE_SWAPBACKED; flags_mask = PM_PAGE_SWAPBACKED; break; case 'k': required_flags = PM_PAGE_KSM; flags_mask = PM_PAGE_KSM; break; case 'h': usage(argv[0]); exit(EXIT_SUCCESS); case 'm': perm = parse_perm(optarg); break; case 'p': compfn = &sort_by_pss; break; case 'P': prefix = optarg; prefix_len = strlen(prefix); break; case 'u': compfn = &sort_by_uss; break; case 'v': compfn = &sort_by_vss; break; case 'r': compfn = &sort_by_rss; break; case 's': compfn = &sort_by_swap; break; case 'R': order *= -1; break; case '?': fprintf(stderr, "Invalid argument \"%s\".\n", argv[optind - 1]); usage(argv[0]); exit(EXIT_FAILURE); default: abort(); } } argc -= optind; argv += optind; libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *)); libraries_count = 0; libraries_size = INIT_LIBRARIES; error = pm_kernel_create(&ker); if (error) { fprintf(stderr, "Error initializing kernel interface -- " "does this kernel have pagemap?\n"); exit(EXIT_FAILURE); } error = pm_kernel_pids(ker, &pids, &num_procs); if (error) { fprintf(stderr, "Error listing processes.\n"); exit(EXIT_FAILURE); } for (i = 0; i < num_procs; i++) { error = pm_process_create(ker, pids[i], &proc); if (error) { fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); continue; } pi = get_process(pids[i]); error = pm_process_maps(proc, &maps, &num_maps); if (error) { fprintf(stderr, "Error listing maps for process %d.\n", proc->pid); exit(EXIT_FAILURE); } for (j = 0; j < num_maps; j++) { if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len))) continue; if (perm && (pm_map_flags(maps[j]) & PM_MAP_PERMISSIONS) != perm) continue; li = get_library(pm_map_name(maps[j]), all); if (!li) continue; mi = get_mapping(li, pi); error = pm_map_usage_flags(maps[j], &map_usage, flags_mask, required_flags); if (error) { fprintf(stderr, "Error getting map memory usage of " "map %s in process %d.\n", pm_map_name(maps[j]), proc->pid); exit(EXIT_FAILURE); } if (map_usage.swap) { has_swap = true; } pm_memusage_add(&mi->usage, &map_usage); pm_memusage_add(&li->total_usage, &map_usage); } } printf(" %6s %6s %6s %6s %6s ", "RSStot", "VSS", "RSS", "PSS", "USS"); if (has_swap) { printf(" %6s ", "Swap"); } printf("Name/PID\n"); fflush(stdout); qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp); for (i = 0; i < libraries_count; i++) { li = libraries[i]; printf("%6zdK %6s %6s %6s %6s ", li->total_usage.pss / 1024, "", "", "", ""); if (has_swap) { printf(" %6s ", ""); } printf("%s\n", li->name); fflush(stdout); qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn); for (j = 0; j < li->mappings_count; j++) { mi = li->mappings[j]; pi = mi->proc; printf( " %6s %6zdK %6zdK %6zdK %6zdK ", "", mi->usage.vss / 1024, mi->usage.rss / 1024, mi->usage.pss / 1024, mi->usage.uss / 1024); if (has_swap) { printf("%6zdK ", mi->usage.swap / 1024); } printf(" %s [%d]\n", pi->cmdline, pi->pid); } printf("\n"); fflush(stdout); } return 0; }
int main(int argc, char *argv[]) { pid_t pid; /* libpagemap context */ pm_kernel_t *ker; int pagesize; /* cached for speed */ pm_process_t *proc; /* maps and such */ pm_map_t **maps; int num_maps; struct map_info **mis; struct map_info *mi; /* pagemap information */ uint64_t *pagemap; int num_pages; unsigned long address; uint64_t mapentry; uint64_t count, flags; /* totals */ unsigned long total_shared_clean, total_shared_dirty, total_private_clean, total_private_dirty; pm_memusage_t total_usage; /* command-line options */ int ws; #define WS_OFF (0) #define WS_ONLY (1) #define WS_RESET (2) int (*compfn)(const void *a, const void *b); int hide_zeros; /* temporary variables */ int i, j; char *endptr; int error; if (argc < 2) { usage(argv[0]); exit(EXIT_FAILURE); } ws = WS_OFF; compfn = NULL; hide_zeros = 0; for (i = 1; i < argc - 1; i++) { if (!strcmp(argv[i], "-w")) { ws = WS_ONLY; continue; } if (!strcmp(argv[i], "-W")) { ws = WS_RESET; continue; } if (!strcmp(argv[i], "-m")) { compfn = NULL; continue; } if (!strcmp(argv[i], "-p")) { compfn = &comp_pss; continue; } if (!strcmp(argv[i], "-h")) { hide_zeros = 1; continue; } fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]); usage(argv[0]); exit(EXIT_FAILURE); } pid = (pid_t)strtol(argv[argc - 1], &endptr, 10); if (*endptr != '\0') { fprintf(stderr, "Invalid PID \"%s\".\n", argv[argc - 1]); exit(EXIT_FAILURE); } error = pm_kernel_create(&ker); if (error) { fprintf(stderr, "error creating kernel interface -- " "does this kernel have pagemap?\n"); exit(EXIT_FAILURE); } pagesize = pm_kernel_pagesize(ker); error = pm_process_create(ker, pid, &proc); if (error) { fprintf(stderr, "error creating process interface -- " "does process %d really exist?\n", pid); exit(EXIT_FAILURE); } if (ws == WS_RESET) { error = pm_process_workingset(proc, NULL, 1); if (error) { fprintf(stderr, "error resetting working set for process.\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } /* get maps, and allocate our map_info array */ error = pm_process_maps(proc, &maps, &num_maps); if (error) { fprintf(stderr, "error listing maps.\n"); exit(EXIT_FAILURE); } mis = (struct map_info **)calloc(num_maps, sizeof(struct map_info *)); if (!mis) { fprintf(stderr, "error allocating map_info array: %s\n", strerror(errno)); exit(EXIT_FAILURE); } /* print header */ if (ws == WS_ONLY) { printf("%7s %7s %7s %7s %7s %7s %7s %s\n", "WRss", "WPss", "WUss", "WShCl", "WShDi", "WPrCl", "WPrDi", "Name"); printf("%7s %7s %7s %7s %7s %7s %7s %s\n", "-------", "-------", "-------", "-------", "-------", "-------", "-------", ""); } else { printf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n", "Vss", "Rss", "Pss", "Uss", "ShCl", "ShDi", "PrCl", "PrDi", "Name"); printf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n", "-------", "-------", "-------", "-------", "-------", "-------", "-------", "-------", ""); } /* zero things */ pm_memusage_zero(&total_usage); total_shared_clean = total_shared_dirty = total_private_clean = total_private_dirty = 0; for (i = 0; i < num_maps; i++) { mi = (struct map_info *)calloc(1, sizeof(struct map_info)); if (!mi) { fprintf(stderr, "error allocating map_info: %s\n", strerror(errno)); exit(EXIT_FAILURE); } mi->map = maps[i]; /* get, and sum, memory usage */ if (ws == WS_ONLY) error = pm_map_workingset(mi->map, &mi->usage); else error = pm_map_usage(mi->map, &mi->usage); if (error) { fflush(stdout); fprintf(stderr, "error getting usage for map.\n"); continue; } pm_memusage_add(&total_usage, &mi->usage); /* get, and sum, individual page counts */ error = pm_map_pagemap(mi->map, &pagemap, &num_pages); if (error) { fflush(stdout); fprintf(stderr, "error getting pagemap for map.\n"); continue; } mi->shared_clean = mi->shared_dirty = mi->private_clean = mi->private_dirty = 0; for (j = 0; j < num_pages; j++) { address = pm_map_start(mi->map) + j * ker->pagesize; mapentry = pagemap[j]; if (PM_PAGEMAP_PRESENT(mapentry) && !PM_PAGEMAP_SWAPPED(mapentry)) { error = pm_kernel_count(ker, PM_PAGEMAP_PFN(mapentry), &count); if (error) { fflush(stdout); fprintf(stderr, "error getting count for frame.\n"); } error = pm_kernel_flags(ker, PM_PAGEMAP_PFN(mapentry), &flags); if (error) { fflush(stdout); fprintf(stderr, "error getting flags for frame.\n"); } if ((ws != WS_ONLY) || (flags & PM_PAGE_REFERENCED)) { if (count > 1) { if (flags & PM_PAGE_DIRTY) mi->shared_dirty++; else mi->shared_clean++; } else { if (flags & PM_PAGE_DIRTY) mi->private_dirty++; else mi->private_clean++; } } } } total_shared_clean += mi->shared_clean; total_shared_dirty += mi->shared_dirty; total_private_clean += mi->private_clean; total_private_dirty += mi->private_dirty; /* add to array */ mis[i] = mi; } /* sort the array, if requested (compfn == NULL for original order) */ if (compfn) qsort(mis, num_maps, sizeof(mis[0]), compfn); for (i = 0; i < num_maps; i++) { mi = mis[i]; if ((!mi) || (hide_zeros && !mi->usage.rss)) continue; if (ws == WS_ONLY) { printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n", (long)mi->usage.rss / 1024, (long)mi->usage.pss / 1024, (long)mi->usage.uss / 1024, mi->shared_clean * pagesize / 1024, mi->shared_dirty * pagesize / 1024, mi->private_clean * pagesize / 1024, mi->private_dirty * pagesize / 1024, pm_map_name(mi->map) ); } else { printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n", (long)mi->usage.vss / 1024, (long)mi->usage.rss / 1024, (long)mi->usage.pss / 1024, (long)mi->usage.uss / 1024, mi->shared_clean * pagesize / 1024, mi->shared_dirty * pagesize / 1024, mi->private_clean * pagesize / 1024, mi->private_dirty * pagesize / 1024, pm_map_name(mi->map) ); } } /* print totals */ if (ws == WS_ONLY) { printf("%7s %7s %7s %7s %7s %7s %7s %s\n", "-------", "-------", "-------", "-------", "-------", "-------", "-------", ""); printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n", (long)total_usage.rss / 1024, (long)total_usage.pss / 1024, (long)total_usage.uss / 1024, total_shared_clean * pagesize / 1024, total_shared_dirty * pagesize / 1024, total_private_clean * pagesize / 1024, total_private_dirty * pagesize / 1024, "TOTAL" ); } else { printf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n", "-------", "-------", "-------", "-------", "-------", "-------", "-------", "-------", ""); printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n", (long)total_usage.vss / 1024, (long)total_usage.rss / 1024, (long)total_usage.pss / 1024, (long)total_usage.uss / 1024, total_shared_clean * pagesize / 1024, total_shared_dirty * pagesize / 1024, total_private_clean * pagesize / 1024, total_private_dirty * pagesize / 1024, "TOTAL" ); } return 0; }
int main(int argc, char *argv[]) { char cmdline[256]; char *prefix; size_t prefix_len; int (*compfn)(const void *a, const void *b); /// M: get swap usage @{ int show_swap; /// @} pm_kernel_t *ker; pm_process_t *proc; pid_t *pids; size_t num_procs; pm_map_t **maps; size_t num_maps; pm_memusage_t map_usage; struct library_info *li, **lis; struct mapping_info *mi, **mis; struct process_info *pi; int i, j, error; signal(SIGPIPE, SIG_IGN); compfn = &sort_by_pss; order = -1; prefix = NULL; prefix_len = 0; /// M: get swap usage @{ show_swap = 0; /// @} for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-P")) { if (i + 1 >= argc) { fprintf(stderr, "Option -P requires an argument.\n"); usage(argv[0]); exit(EXIT_FAILURE); } prefix = argv[++i]; prefix_len = strlen(prefix); continue; } if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; } if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; } if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; } if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; } if (!strcmp(argv[i], "-R")) { order *= -1; continue; } /// M: get swap usage @{ if (!strcmp(argv[i], "-s")) { show_swap = 1; continue; } /// @} if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); } fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]); usage(argv[0]); exit(EXIT_FAILURE); } libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *)); libraries_count = 0; libraries_size = INIT_LIBRARIES; error = pm_kernel_create(&ker); if (error) { fprintf(stderr, "Error initializing kernel interface -- " "does this kernel have pagemap?\n"); exit(EXIT_FAILURE); } error = pm_kernel_pids(ker, &pids, &num_procs); if (error) { fprintf(stderr, "Error listing processes.\n"); exit(EXIT_FAILURE); } for (i = 0; i < num_procs; i++) { error = pm_process_create(ker, pids[i], &proc); if (error) { fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); continue; } pi = get_process(pids[i]); error = pm_process_maps(proc, &maps, &num_maps); if (error) { fprintf(stderr, "Error listing maps for process %d.\n", proc->pid); exit(EXIT_FAILURE); } for (j = 0; j < num_maps; j++) { if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len))) continue; li = get_library(pm_map_name(maps[j])); if (!li) continue; mi = get_mapping(li, pi); error = pm_map_usage(maps[j], &map_usage); if (error) { fprintf(stderr, "Error getting map memory usage of " "map %s in process %d.\n", pm_map_name(maps[j]), proc->pid); exit(EXIT_FAILURE); } pm_memusage_add(&mi->usage, &map_usage); pm_memusage_add(&li->total_usage, &map_usage); } } /// M: get swap usage @{ if (show_swap) { printf( " %6s %6s %6s %6s %6s %6s %6s %s\n", "RSStot", "VSS", "RSS", "PSS", "USS", "SWAP", "PSWAP", "Name/PID"); } else { printf( " %6s %6s %6s %6s %6s %s\n", "RSStot", "VSS", "RSS", "PSS", "USS", "Name/PID"); } /// @} fflush(stdout); qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp); for (i = 0; i < libraries_count; i++) { li = libraries[i]; printf("%6dK %6s %6s %6s %6s %s\n", li->total_usage.pss / 1024, "", "", "", "", li->name); fflush(stdout); qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn); for (j = 0; j < li->mappings_count; j++) { mi = li->mappings[j]; pi = mi->proc; /// M: get swap usage @{ if (show_swap) { printf( " %6s %6dK %6dK %6dK %6dK %6dK %6dK %s [%d]\n", "", mi->usage.vss / 1024, mi->usage.rss / 1024, mi->usage.pss / 1024, mi->usage.uss / 1024, mi->usage.swap / 1024, mi->usage.pswap / 1024, pi->cmdline, pi->pid); } else { printf( " %6s %6dK %6dK %6dK %6dK %s [%d]\n", "", mi->usage.vss / 1024, mi->usage.rss / 1024, mi->usage.pss / 1024, mi->usage.uss / 1024, pi->cmdline, pi->pid); } /// @} } printf("\n"); fflush(stdout); } return 0; }