struct mapping_info *get_mapping(struct library_info *library, struct process_info *proc) { struct mapping_info *mapping; size_t i; for (i = 0; i < library->mappings_count; i++) { if (library->mappings[i]->proc == proc) return library->mappings[i]; } if (library->mappings_count >= library->mappings_size) { library->mappings = realloc(library->mappings, 2 * library->mappings_size * sizeof(struct mapping*)); if (!library->mappings) { fprintf(stderr, "Couldn't resize mappings array: %s\n", strerror(errno)); exit(EXIT_FAILURE); } library->mappings_size = 2 * library->mappings_size; } mapping = calloc(1, sizeof(*mapping)); if (!mapping) { fprintf(stderr, "Couldn't allocate space for mapping struct: %s\n", strerror(errno)); exit(EXIT_FAILURE); } mapping->proc = proc; pm_memusage_zero(&mapping->usage); library->mappings[library->mappings_count++] = mapping; return mapping; }
struct library_info *get_library(const char *name, bool all) { size_t i; struct library_info *library; if (!all) { for (i = 0; library_name_blacklist[i]; i++) if (!strcmp(name, library_name_blacklist[i])) return NULL; } else { if (name[0] == 0) { name = "[anon]"; } } for (i = 0; i < libraries_count; i++) { if (!strcmp(libraries[i]->name, name)) return libraries[i]; } if (libraries_count >= libraries_size) { libraries = realloc(libraries, 2 * libraries_size * sizeof(struct library_info *)); if (!libraries) { fprintf(stderr, "Couldn't resize libraries array: %s\n", strerror(errno)); exit(EXIT_FAILURE); } libraries_size = 2 * libraries_size; } library = calloc(1, sizeof(*library)); if (!library) { fprintf(stderr, "Couldn't allocate space for library struct: %s\n", strerror(errno)); exit(EXIT_FAILURE); } library->name = malloc(strlen(name) + 1); if (!library->name) { fprintf(stderr, "Couldn't allocate space for library name: %s\n", strerror(errno)); exit(EXIT_FAILURE); } strcpy(library->name, name); library->mappings = malloc(INIT_MAPPINGS * sizeof(struct mapping_info *)); if (!library->mappings) { fprintf(stderr, "Couldn't allocate space for library mappings array: %s\n", strerror(errno)); exit(EXIT_FAILURE); } library->mappings_count = 0; library->mappings_size = INIT_MAPPINGS; pm_memusage_zero(&library->total_usage); libraries[libraries_count++] = library; return library; }
int pm_map_workingset(pm_map_t *map, pm_memusage_t *ws_out) { uint64_t *pagemap; size_t len, i; uint64_t count, flags; pm_memusage_t ws; int error; if (!map || !ws_out) return -1; error = pm_map_pagemap(map, &pagemap, &len); if (error) return error; pm_memusage_zero(&ws); for (i = 0; i < len; i++) { error = pm_kernel_flags(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]), &flags); if (error) goto out; if (!(flags & PM_PAGE_REFERENCED)) continue; error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]), &count); if (error) goto out; ws.vss += map->proc->ker->pagesize; if( PM_PAGEMAP_SWAPPED(pagemap[i]) ) continue; ws.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0); ws.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0); ws.uss += (count == 1) ? (map->proc->ker->pagesize) : (0); } memcpy(ws_out, &ws, sizeof(ws)); error = 0; out: free(pagemap); return 0; }
int pm_map_usage(pm_map_t *map, pm_memusage_t *usage_out) { uint64_t *pagemap; size_t len, i; uint64_t count; pm_memusage_t usage; int error; if (!map || !usage_out) return -1; error = pm_map_pagemap(map, &pagemap, &len); if (error) return error; pm_memusage_zero(&usage); for (i = 0; i < len; i++) { if (!PM_PAGEMAP_PRESENT(pagemap[i]) || PM_PAGEMAP_SWAPPED(pagemap[i])) continue; error = pm_kernel_count(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]), &count); if (error) goto out; usage.vss += map->proc->ker->pagesize; usage.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0); usage.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0); usage.uss += (count == 1) ? (map->proc->ker->pagesize) : (0); } memcpy(usage_out, &usage, sizeof(usage)); error = 0; out: free(pagemap); return error; }
int main(int argc, char *argv[]) { pm_kernel_t *ker; pm_process_t *proc; pid_t *pids; struct proc_info **procs; size_t num_procs; unsigned long total_pss; unsigned long total_uss; char cmdline[256]; // this must be within the range of int int error; /// M: get swap usage @{ int show_swap; /// @} #define WS_OFF 0 #define WS_ONLY 1 #define WS_RESET 2 int ws; int arg; size_t i, j; signal(SIGPIPE, SIG_IGN); compfn = &sort_by_pss; order = -1; ws = WS_OFF; /// M: get swap usage @{ show_swap = 0; /// @} for (arg = 1; arg < argc; arg++) { if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; } if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; } if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; } if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; } if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; } if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; } if (!strcmp(argv[arg], "-R")) { order *= -1; continue; } /// M: get swap usage @{ if (!strcmp(argv[arg], "-s")) { show_swap = 1; continue; } /// @} if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); } fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]); usage(argv[0]); 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); } error = pm_kernel_pids(ker, &pids, &num_procs); if (error) { fprintf(stderr, "Error listing processes.\n"); exit(EXIT_FAILURE); } procs = calloc(num_procs, sizeof(struct proc_info*)); if (procs == NULL) { fprintf(stderr, "calloc: %s", strerror(errno)); exit(EXIT_FAILURE); } for (i = 0; i < num_procs; i++) { procs[i] = malloc(sizeof(struct proc_info)); if (procs[i] == NULL) { fprintf(stderr, "malloc: %s\n", strerror(errno)); exit(EXIT_FAILURE); } procs[i]->pid = pids[i]; pm_memusage_zero(&procs[i]->usage); error = pm_process_create(ker, pids[i], &proc); if (error) { fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); continue; } switch (ws) { case WS_OFF: error = pm_process_usage(proc, &procs[i]->usage); break; case WS_ONLY: error = pm_process_workingset(proc, &procs[i]->usage, 0); break; case WS_RESET: error = pm_process_workingset(proc, NULL, 1); break; } if (error) { fprintf(stderr, "warning: could not read usage for %d\n", pids[i]); } pm_process_destroy(proc); } free(pids); if (ws == WS_RESET) exit(0); j = 0; for (i = 0; i < num_procs; i++) { if (procs[i]->usage.vss) { procs[j++] = procs[i]; } else { free(procs[i]); } } num_procs = j; qsort(procs, num_procs, sizeof(procs[0]), compfn); if (ws) printf("%5s %7s %7s %7s %s\n", "PID", "WRss", "WPss", "WUss", "cmdline"); else { /// M: get swap usage @{ if (show_swap) { printf("%5s %7s %7s %7s %7s %7s %7s %s\n", "PID", "Vss", "Rss", "Pss", "Uss", "Swap", "PSwap", "cmdline"); } else { printf("%5s %7s %7s %7s %7s %s\n", "PID", "Vss", "Rss", "Pss", "Uss", "cmdline"); } /// @} } total_pss = 0; total_uss = 0; for (i = 0; i < num_procs; i++) { if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) { /* * Something is probably seriously wrong if writing to the stack * failed. */ free(procs[i]); continue; } total_pss += procs[i]->usage.pss; total_uss += procs[i]->usage.uss; if (ws) printf("%5d %6dK %6dK %6dK %s\n", procs[i]->pid, procs[i]->usage.rss / 1024, procs[i]->usage.pss / 1024, procs[i]->usage.uss / 1024, cmdline ); else { /// M: get swap usage @{ if (show_swap) { printf("%5d %6dK %6dK %6dK %6dK %6dK %6dK %s\n", procs[i]->pid, procs[i]->usage.vss / 1024, procs[i]->usage.rss / 1024, procs[i]->usage.pss / 1024, procs[i]->usage.uss / 1024, procs[i]->usage.swap / 1024, procs[i]->usage.pswap / 1024, cmdline ); } else { printf("%5d %6dK %6dK %6dK %6dK %s\n", procs[i]->pid, procs[i]->usage.vss / 1024, procs[i]->usage.rss / 1024, procs[i]->usage.pss / 1024, procs[i]->usage.uss / 1024, cmdline ); } /// @} } free(procs[i]); } free(procs); if (ws) { printf("%5s %7s %7s %7s %s\n", "", "", "------", "------", "------"); printf("%5s %7s %6ldK %6ldK %s\n", "", "", total_pss / 1024, total_uss / 1024, "TOTAL"); } else { printf("%5s %7s %7s %7s %7s %s\n", "", "", "", "------", "------", "------"); printf("%5s %7s %7s %6ldK %6ldK %s\n", "", "", "", total_pss / 1024, total_uss / 1024, "TOTAL"); } printf("\n"); print_mem_info(); return 0; }
int main(int argc, char *argv[]) { pm_kernel_t *ker; pm_process_t *proc; pid_t *pids; size_t num_procs; uint64_t total_pss; uint64_t total_uss; uint64_t total_swap; uint64_t total_pswap; uint64_t total_uswap; uint64_t total_zswap; int error; bool has_swap = false, has_zram = false; uint64_t required_flags = 0; uint64_t flags_mask = 0; int arg; size_t i; enum { WS_OFF, WS_ONLY, WS_RESET, } ws; uint64_t mem[MEMINFO_COUNT] = { }; pm_proportional_swap_t *p_swap; float zram_cr = 0.0; signal(SIGPIPE, SIG_IGN); compfn = &sort_by_pss; order = -1; ws = WS_OFF; bool oomadj = false; for (arg = 1; arg < argc; arg++) { if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; } if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; } if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; } if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; } if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; } if (!strcmp(argv[arg], "-o")) { compfn = &sort_by_oomadj; oomadj = true; continue; } if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = (1 << KPF_SWAPBACKED); continue; } if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = (1 << KPF_SWAPBACKED); continue; } if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = (1 << KPF_KSM); continue; } if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; } if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; } if (!strcmp(argv[arg], "-R")) { order *= -1; continue; } if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); } fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]); usage(argv[0]); exit(EXIT_FAILURE); } get_mem_info(mem); p_swap = pm_memusage_pswap_create(mem[MEMINFO_SWAP_TOTAL] * 1024); error = pm_kernel_create(&ker); if (error) { fprintf(stderr, "Error creating 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); } std::vector<proc_info> procs(num_procs); for (i = 0; i < num_procs; i++) { procs[i].pid = pids[i]; procs[i].oomadj = getoomadj(pids[i]); pm_memusage_zero(&procs[i].usage); pm_memusage_pswap_init_handle(&procs[i].usage, p_swap); error = pm_process_create(ker, pids[i], &proc); if (error) { fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); continue; } switch (ws) { case WS_OFF: error = pm_process_usage_flags(proc, &procs[i].usage, flags_mask, required_flags); break; case WS_ONLY: error = pm_process_workingset(proc, &procs[i].usage, 0); break; case WS_RESET: error = pm_process_workingset(proc, NULL, 1); break; } if (error) { fprintf(stderr, "warning: could not read usage for %d\n", pids[i]); } if (ws != WS_RESET && procs[i].usage.swap) { has_swap = true; } pm_process_destroy(proc); } free(pids); if (ws == WS_RESET) exit(0); procs.erase(std::remove_if(procs.begin(), procs.end(), [](auto proc){ return proc.usage.vss == 0; }), procs.end()); qsort(procs.data(), procs.size(), sizeof(procs[0]), compfn); if (has_swap) { uint64_t zram_mem_used = get_zram_mem_used(); if (zram_mem_used) { mem[MEMINFO_ZRAM_TOTAL] = zram_mem_used/1024; zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] / (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]); has_zram = true; } } printf("%5s ", "PID"); if (oomadj) { printf("%5s ", "oom"); } if (ws) { printf("%7s %7s %7s ", "WRss", "WPss", "WUss"); if (has_swap) { printf("%7s %7s %7s ", "WSwap", "WPSwap", "WUSwap"); if (has_zram) { printf("%7s ", "WZSwap"); } } } else { printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss"); if (has_swap) { printf("%7s %7s %7s ", "Swap", "PSwap", "USwap"); if (has_zram) { printf("%7s ", "ZSwap"); } } } printf("%s\n", "cmdline"); total_pss = 0; total_uss = 0; total_swap = 0; total_pswap = 0; total_uswap = 0; total_zswap = 0; std::vector<uint64_t> lmk_minfree; std::vector<int> lmk_adj; if (oomadj) { getminfree(&lmk_minfree, &lmk_adj); } auto lmk_minfree_it = lmk_minfree.cbegin(); auto lmk_adj_it = lmk_adj.cbegin(); auto print_oomadj_totals = [&](int adj){ for (; lmk_adj_it != lmk_adj.cend() && lmk_minfree_it != lmk_minfree.cend() && adj > *lmk_adj_it; lmk_adj_it++, lmk_minfree_it++) { // Print the cumulative total line printf("%5s ", ""); // pid printf("%5s ", ""); // oomadj if (ws) { printf("%7s %6" PRIu64 "K %6" PRIu64 "K ", "", total_pss / 1024, total_uss / 1024); } else { printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ", "", "", total_pss / 1024, total_uss / 1024); } if (has_swap) { printf("%6" PRIu64 "K ", total_swap / 1024); printf("%6" PRIu64 "K ", total_pswap / 1024); printf("%6" PRIu64 "K ", total_uswap / 1024); if (has_zram) { printf("%6" PRIu64 "K ", total_zswap / 1024); } } printf("TOTAL for oomadj < %d (%6" PRIu64 "K)\n", *lmk_adj_it, *lmk_minfree_it / 1024); } }; for (auto& proc: procs) { if (oomadj) { print_oomadj_totals(proc.oomadj); } std::string cmdline = getprocname(proc.pid); total_pss += proc.usage.pss; total_uss += proc.usage.uss; total_swap += proc.usage.swap; printf("%5d ", proc.pid); if (oomadj) { printf("%5d ", proc.oomadj); } if (ws) { printf("%6zuK %6zuK %6zuK ", proc.usage.rss / 1024, proc.usage.pss / 1024, proc.usage.uss / 1024 ); } else { printf("%7zuK %6zuK %6zuK %6zuK ", proc.usage.vss / 1024, proc.usage.rss / 1024, proc.usage.pss / 1024, proc.usage.uss / 1024 ); } if (has_swap) { pm_swapusage_t su; pm_memusage_pswap_get_usage(&proc.usage, &su); printf("%6zuK ", proc.usage.swap / 1024); printf("%6zuK ", su.proportional / 1024); printf("%6zuK ", su.unique / 1024); total_pswap += su.proportional; total_uswap += su.unique; pm_memusage_pswap_free(&proc.usage); if (has_zram) { size_t zpswap = su.proportional * zram_cr; printf("%6zuK ", zpswap / 1024); total_zswap += zpswap; } } printf("%s\n", cmdline.c_str()); } pm_memusage_pswap_destroy(p_swap); if (oomadj) { print_oomadj_totals(INT_MAX); } // Print the separator line printf("%5s ", ""); if (oomadj) { printf("%5s ", ""); } if (ws) { printf("%7s %7s %7s ", "", "------", "------"); } else { printf("%8s %7s %7s %7s ", "", "", "------", "------"); } if (has_swap) { printf("%7s %7s %7s ", "------", "------", "------"); if (has_zram) { printf("%7s ", "------"); } } printf("%s\n", "------"); // Print the total line printf("%5s ", ""); if (oomadj) { printf("%5s ", ""); } if (ws) { printf("%7s %6" PRIu64 "K %6" PRIu64 "K ", "", total_pss / 1024, total_uss / 1024); } else { printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ", "", "", total_pss / 1024, total_uss / 1024); } if (has_swap) { printf("%6" PRIu64 "K ", total_swap / 1024); printf("%6" PRIu64 "K ", total_pswap / 1024); printf("%6" PRIu64 "K ", total_uswap / 1024); if (has_zram) { printf("%6" PRIu64 "K ", total_zswap / 1024); } } printf("TOTAL\n"); printf("\n"); if (has_swap) { printf("ZRAM: %" PRIu64 "K physical used for %" PRIu64 "K in swap " "(%" PRIu64 "K total swap)\n", mem[MEMINFO_ZRAM_TOTAL], (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]), mem[MEMINFO_SWAP_TOTAL]); } printf(" RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, " "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n", mem[MEMINFO_TOTAL], mem[MEMINFO_FREE], mem[MEMINFO_BUFFERS], mem[MEMINFO_CACHED], mem[MEMINFO_SHMEM], mem[MEMINFO_SLAB]); 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[]) { pm_kernel_t *ker; pm_process_t *proc; pid_t *pids; struct proc_info **procs; size_t num_procs; uint64_t total_pss; uint64_t total_uss; uint64_t total_swap; char cmdline[256]; // this must be within the range of int int error; bool has_swap = false; uint64_t required_flags = 0; uint64_t flags_mask = 0; #define WS_OFF 0 #define WS_ONLY 1 #define WS_RESET 2 int ws; int arg; size_t i, j; signal(SIGPIPE, SIG_IGN); compfn = &sort_by_pss; order = -1; ws = WS_OFF; for (arg = 1; arg < argc; arg++) { if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; } if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; } if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; } if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; } if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; } if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = PM_PAGE_SWAPBACKED; continue; } if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = PM_PAGE_SWAPBACKED; continue; } if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = PM_PAGE_KSM; continue; } if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; } if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; } if (!strcmp(argv[arg], "-R")) { order *= -1; continue; } if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); } fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]); usage(argv[0]); 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); } error = pm_kernel_pids(ker, &pids, &num_procs); if (error) { fprintf(stderr, "Error listing processes.\n"); exit(EXIT_FAILURE); } procs = calloc(num_procs, sizeof(struct proc_info*)); if (procs == NULL) { fprintf(stderr, "calloc: %s", strerror(errno)); exit(EXIT_FAILURE); } for (i = 0; i < num_procs; i++) { procs[i] = malloc(sizeof(struct proc_info)); if (procs[i] == NULL) { fprintf(stderr, "malloc: %s\n", strerror(errno)); exit(EXIT_FAILURE); } procs[i]->pid = pids[i]; pm_memusage_zero(&procs[i]->usage); error = pm_process_create(ker, pids[i], &proc); if (error) { fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); continue; } switch (ws) { case WS_OFF: error = pm_process_usage_flags(proc, &procs[i]->usage, flags_mask, required_flags); break; case WS_ONLY: error = pm_process_workingset(proc, &procs[i]->usage, 0); break; case WS_RESET: error = pm_process_workingset(proc, NULL, 1); break; } if (error) { fprintf(stderr, "warning: could not read usage for %d\n", pids[i]); } if (ws != WS_RESET && procs[i]->usage.swap) { has_swap = true; } pm_process_destroy(proc); } free(pids); if (ws == WS_RESET) exit(0); j = 0; for (i = 0; i < num_procs; i++) { if (procs[i]->usage.vss) { procs[j++] = procs[i]; } else { free(procs[i]); } } num_procs = j; qsort(procs, num_procs, sizeof(procs[0]), compfn); printf("%5s ", "PID"); if (ws) { printf("%s %7s %7s ", "WRss", "WPss", "WUss"); if (has_swap) { printf("%7s ", "WSwap"); } } else { printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss"); if (has_swap) { printf("%7s ", "Swap"); } } printf("%s\n", "cmdline"); total_pss = 0; total_uss = 0; total_swap = 0; for (i = 0; i < num_procs; i++) { if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) { /* * Something is probably seriously wrong if writing to the stack * failed. */ free(procs[i]); continue; } total_pss += procs[i]->usage.pss; total_uss += procs[i]->usage.uss; total_swap += procs[i]->usage.swap; printf("%5d ", procs[i]->pid); if (ws) { printf("%6zuK %6zuK %6zuK ", procs[i]->usage.rss / 1024, procs[i]->usage.pss / 1024, procs[i]->usage.uss / 1024 ); } else { printf("%7zuK %6zuK %6zuK %6zuK ", procs[i]->usage.vss / 1024, procs[i]->usage.rss / 1024, procs[i]->usage.pss / 1024, procs[i]->usage.uss / 1024 ); } if (has_swap) { printf("%6zuK ", procs[i]->usage.swap / 1024); } printf("%s\n", cmdline); free(procs[i]); } free(procs); /* Print the separator line */ printf("%5s ", ""); if (ws) { printf("%7s %7s %7s ", "", "------", "------"); } else { printf("%8s %7s %7s %7s ", "", "", "------", "------"); } if (has_swap) { printf("%7s ", "------"); } printf("%s\n", "------"); /* Print the total line */ printf("%5s ", ""); if (ws) { printf("%7s %6" PRIu64 "K %" PRIu64 "K ", "", total_pss / 1024, total_uss / 1024); } else { printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ", "", "", total_pss / 1024, total_uss / 1024); } if (has_swap) { printf("%6" PRIu64 "K ", total_swap / 1024); } printf("TOTAL\n"); printf("\n"); print_mem_info(); return 0; }