Beispiel #1
0
static void
display(void)
{
	struct passwd *pwd;
	struct xfile *xf;
	struct sock *s;
	int hash, n, pos;

	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s",
	    "USER", "COMMAND", "PID", "FD", "PROTO",
	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
	if (opt_s)
		printf(" %-12s", "STATE");
	printf("\n");
	setpassent(1);
	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
		if (xf->xf_data == NULL)
			continue;
		if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
			continue;
		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
		for (s = sockhash[hash]; s != NULL; s = s->next) {
			if ((void *)s->socket != xf->xf_data)
				continue;
			if (!check_ports(s))
				continue;
			s->shown = 1;
			pos = 0;
			if ((pwd = getpwuid(xf->xf_uid)) == NULL)
				pos += xprintf("%lu ", (u_long)xf->xf_uid);
			else
				pos += xprintf("%s ", pwd->pw_name);
			while (pos < 9)
				pos += xprintf(" ");
			pos += xprintf("%.10s", getprocname(xf->xf_pid));
			while (pos < 20)
				pos += xprintf(" ");
			pos += xprintf("%lu ", (u_long)xf->xf_pid);
			while (pos < 26)
				pos += xprintf(" ");
			pos += xprintf("%d ", xf->xf_fd);
			displaysock(s, pos);
		}
	}
	if (opt_j >= 0)
		return;
	for (hash = 0; hash < HASHSIZE; hash++) {
		for (s = sockhash[hash]; s != NULL; s = s->next) {
			if (s->shown)
				continue;
			if (!check_ports(s))
				continue;
			pos = 0;
			pos += xprintf("%-8s %-10s %-5s %-2s ",
			    "?", "?", "?", "?");
			displaysock(s, pos);
		}
	}
}
Beispiel #2
0
char *procname (pid_t pid) 

{
	static char buffer [PROCNAME_MAX];
	if (pid != (pid_t)(-1)) 
	{
		getprocname (buffer, sizeof (buffer), pid);
	}
	return (buffer);
}
struct process_info *get_process(pid_t pid) {
    struct process_info *process;

    process = calloc(1, sizeof(*process));
    if (!process) {
        fprintf(stderr, "Couldn't allocate space for process struct: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    process->pid = pid;
    getprocname(pid, process->cmdline, sizeof(process->cmdline));

    return process;
}
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[]) {
    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
proc_err(pb_t *ppb, int pc, paramv_t *pv)
{
	register func_t	*f;
	register int		i;
	register ctx_t		*ctx;

#ifdef xCTR2
	if (tTf(6, 8))
		lprintf("proc_err: new = %d\n", Ctx.ctx_new);
#endif
	pb_prime(ppb, PB_ERR);

	/*
	**  Scan back on the list of context dependencies.
	**	If we come to someone who can process this message,
	**	we go ahead and do it.  We also take this
	**	opportunity to unwind the context list & call the
	**	cleanup functions.
	*/

	for (ctx = &Ctx; ctx != NULL; ctx = ctx->ctx_link) {
		setprocname(ctx->ctx_name);
		f = ctx->ctx_fn;
#ifdef xCTR2
		if (tTf(6, 9))
			lprintf("proc_err: unwinding %s: errfn=%x, ppb=%x, link=%x, resp=%d, fn=%x\n",
			    getprocname(), ctx->ctx_errfn, ctx->ctx_ppb,
			    ctx->ctx_link, ctx->ctx_resp, f);
#endif

		/*  Do the actual error processing. */
		ppb->pb_proc = ctx->ctx_resp;
		if (ctx->ctx_errfn != NULL)
			i = (*ctx->ctx_errfn)(pc, pv);
		else
			i = -1;

#ifdef xCTR2
		if (tTf(6, 11))
			lprintf("proc_err: errcode %d\n", i);
#endif
		if (i == 0)
			break;
		else if (i > 0) {
			/* turn into nonfatal error */
			ppb->pb_stat |= PB_INFO;
			ppb->pb_proc = PB_FRONT;
		} else {
			/* call the cleanup function */
			if (f != NULL && f->fn_active > 0) {
				(*f->fn_cleanup)(1);
			}
		}

		/* arrange to leave if parent not in this process */
		if (ppb->pb_proc != Cm.cm_myproc) {
			send_off(ppb, pc, pv);
			pb_flush(ppb);

			/* throw away dead contexts and exit */
			break;
		}
	}
	if (ctx == NULL) {
		syserr("proc_err: no parent");
	}

#ifdef xCTR3
	MonPpb = getmonppb();
	if (tTf(6, 12)) {
		lprintf("proc_err: cleanup: ctx=%x, ->_link=%x, MonPpb = ", ctx, ctx->ctx_link);
		pb_dump(MonPpb, TRUE);
	}
#endif
	/* pop contexts down to ctx and exit */
	ctx = ctx->ctx_link;
	while (Ctx.ctx_link != ctx) {
		if (Ctx.ctx_link == NULL)
			syserr("proc_err: underflow");
		Ctx.ctx_new = TRUE;
		resetp();
	}

	/*
	**  Flush input pipe.
	**	THIS CODE IS ONLY NEEDED TO MAKE READMON WORK, AND
	**	SHOULD BE REMOVED WHEN READMON GOES AWAY!!
	*/

	if (ctx == NULL) {
		Cm.cm_input = Cm.cm_rinput;
		MonPpb = getmonppb();
		while (!BITISSET(PB_EOF, MonPpb->pb_stat)) {
			pb_read(MonPpb);
		}
		MonPpb->pb_st = PB_UNKNOWN;
	}

	longjmp(Ctx.ctx_jbuf, 1);
}
Beispiel #7
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;
}
Beispiel #8
0
static void
display(void)
{
	struct passwd *pwd;
	struct xfile *xf;
	struct sock *s;
	void *p;
	int hash, n, pos;

	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
	    "USER", "COMMAND", "PID", "FD", "PROTO",
	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
	setpassent(1);
	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
		if (xf->xf_data == NULL)
			continue;
		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
		for (s = sockhash[hash]; s != NULL; s = s->next)
			if ((void *)s->socket == xf->xf_data)
				break;
		if (s == NULL)
			continue;
		if (!check_ports(s))
			continue;
		pos = 0;
		if ((pwd = getpwuid(xf->xf_uid)) == NULL)
			pos += xprintf("%lu ", (u_long)xf->xf_uid);
		else
			pos += xprintf("%s ", pwd->pw_name);
		while (pos < 9)
			pos += xprintf(" ");
		pos += xprintf("%.10s", getprocname(xf->xf_pid));
		while (pos < 20)
			pos += xprintf(" ");
		pos += xprintf("%lu ", (u_long)xf->xf_pid);
		while (pos < 26)
			pos += xprintf(" ");
		pos += xprintf("%d ", xf->xf_fd);
		while (pos < 29)
			pos += xprintf(" ");
		pos += xprintf("%s", s->protoname);
		if (s->vflag & INP_IPV4)
			pos += xprintf("4 ");
		if (s->vflag & INP_IPV6)
			pos += xprintf("6 ");
		while (pos < 36)
			pos += xprintf(" ");
		switch (s->family) {
		case AF_INET:
		case AF_INET6:
			pos += printaddr(s->family, &s->laddr);
			if (s->family == AF_INET6 && pos >= 58)
				pos += xprintf(" ");
			while (pos < 58)
				pos += xprintf(" ");
			pos += printaddr(s->family, &s->faddr);
			break;
		case AF_UNIX:
			/* server */
			if (s->laddr.ss_len > 0) {
				pos += printaddr(s->family, &s->laddr);
				break;
			}
			/* client */
			p = *(void **)&s->faddr;
			if (p == NULL) {
				pos += xprintf("(not connected)");
				break;
			}
			pos += xprintf("-> ");
			for (hash = 0; hash < HASHSIZE; ++hash) {
				for (s = sockhash[hash]; s != NULL; s = s->next)
					if (s->pcb == p)
						break;
				if (s != NULL)
					break;
			}
			if (s == NULL || s->laddr.ss_len == 0)
				pos += xprintf("??");
			else
				pos += printaddr(s->family, &s->laddr);
			break;
		default:
			abort();
		}
		xprintf("\n");
	}
}
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;
}
int main(int argc, char *argv[])
{
    int ret;
    pm_kernel_t *ker;
    size_t num_procs;
    pid_t *pids;
    struct memtrack_proc *p;
    size_t i;

    (void)argc;
    (void)argv;

    ret = memtrack_init();
    if (ret < 0) {
        fprintf(stderr, "failed to initialize HAL: %s (%d)\n", strerror(-ret), ret);
        exit(EXIT_FAILURE);
    }

    ret = pm_kernel_create(&ker);
    if (ret) {
        fprintf(stderr, "Error creating kernel interface -- "
                        "does this kernel have pagemap?\n");
        exit(EXIT_FAILURE);
    }

    ret = pm_kernel_pids(ker, &pids, &num_procs);
    if (ret) {
        fprintf(stderr, "Error listing processes.\n");
        exit(EXIT_FAILURE);
    }

    p = memtrack_proc_new();
    if (ret) {
        fprintf(stderr, "failed to create memtrack process handle\n");
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < num_procs; i++) {
        pid_t pid = pids[i];
        char cmdline[256];
        size_t v1;
        size_t v2;
        size_t v3;
        size_t v4;
        size_t v5;
        size_t v6;

        getprocname(pid, cmdline, (int)sizeof(cmdline));

        ret = memtrack_proc_get(p, pid);
        if (ret) {
            fprintf(stderr, "failed to get memory info for pid %d: %s (%d)\n",
                    pid, strerror(-ret), ret);
            continue;
        }

        v1 = DIV_ROUND_UP(memtrack_proc_graphics_total(p), 1024);
        v2 = DIV_ROUND_UP(memtrack_proc_graphics_pss(p), 1024);
        v3 = DIV_ROUND_UP(memtrack_proc_gl_total(p), 1024);
        v4 = DIV_ROUND_UP(memtrack_proc_gl_pss(p), 1024);
        v5 = DIV_ROUND_UP(memtrack_proc_other_total(p), 1024);
        v6 = DIV_ROUND_UP(memtrack_proc_other_pss(p), 1024);

        if (v1 | v2 | v3 | v4 | v5 | v6) {
            printf("%5d %6zu %6zu %6zu %6zu %6zu %6zu %s\n", pid,
                   v1, v2, v3, v4, v5, v6, cmdline);
        }
    }

    memtrack_proc_destroy(p);

    return 0;
}