⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 top.c

📁 Android 一些工具
💻 C
📖 第 1 页 / 共 2 页
字号:
        sprintf(filename, "/proc/%d/task", pid);        task_dir = opendir(filename);        if (!task_dir) continue;        while ((tid_dir = readdir(task_dir))) {            if (!isdigit(tid_dir->d_name[0]))                continue;            if (threads) {                tid = atoi(tid_dir->d_name);                proc = alloc_proc();                proc->pid = pid; proc->tid = tid;                sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);                read_stat(filename, proc);                sprintf(filename, "/proc/%d/task/%d/cmdline", pid, tid);                read_cmdline(filename, proc);                sprintf(filename, "/proc/%d/task/%d/status", pid, tid);                read_status(filename, proc);                add_proc(proc_num++, proc);            } else {                proc->num_threads++;            }        }        closedir(task_dir);                if (!threads)            add_proc(proc_num++, proc);    }    for (i = proc_num; i < num_new_procs; i++)        new_procs[i] = NULL;    closedir(proc_dir);}static int read_stat(char *filename, struct proc_info *proc) {    FILE *file;    char buf[MAX_LINE], *open_paren, *close_paren;    int res, idx;    file = fopen(filename, "r");    if (!file) return 1;    fgets(buf, MAX_LINE, file);    fclose(file);    /* Split at first '(' and last ')' to get process name. */    open_paren = strchr(buf, '(');    close_paren = strrchr(buf, ')');    if (!open_paren || !close_paren) return 1;    *open_paren = *close_paren = '\0';    strcpy(proc->name, open_paren + 1);    /* Scan rest of string. */    sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "                 "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld",                 &proc->state, &proc->utime, &proc->stime, &proc->vss, &proc->rss);    return 0;}static void add_proc(int proc_num, struct proc_info *proc) {    int i;    if (proc_num >= num_new_procs) {        new_procs = realloc(new_procs, 2 * num_new_procs * sizeof(struct proc_info *));        if (!new_procs) die("Could not expand procs array.\n");        for (i = num_new_procs; i < 2 * num_new_procs; i++)            new_procs[i] = NULL;        num_new_procs = 2 * num_new_procs;    }    new_procs[proc_num] = proc;}static int read_cmdline(char *filename, struct proc_info *proc) {    FILE *file;    char line[MAX_LINE];    line[0] = '\0';    file = fopen(filename, "r");    if (!file) return 1;    fgets(line, MAX_LINE, file);    fclose(file);    if (strlen(line) > 0)        strcpy(proc->name, line);    return 0;}static int read_status(char *filename, struct proc_info *proc) {    FILE *file;    char line[MAX_LINE];    unsigned int uid, gid;    file = fopen(filename, "r");    if (!file) return 1;    while (fgets(line, MAX_LINE, file)) {        sscanf(line, "Uid: %u", &uid);        sscanf(line, "Gid: %u", &gid);    }    fclose(file);    proc->uid = uid; proc->gid = gid;    return 0;}static void print_procs(void) {    int i;    struct proc_info *old_proc, *proc;    long unsigned total_delta_time;    struct passwd *user;    struct group *group;    char *user_str, user_buf[20];    char *group_str, group_buf[20];    for (i = 0; i < num_new_procs; i++) {        if (new_procs[i]) {            old_proc = find_old_proc(new_procs[i]->pid, new_procs[i]->tid);            if (old_proc) {                new_procs[i]->delta_utime = new_procs[i]->utime - old_proc->utime;                new_procs[i]->delta_stime = new_procs[i]->stime - old_proc->stime;            } else {                new_procs[i]->delta_utime = 0;                new_procs[i]->delta_stime = 0;            }            new_procs[i]->delta_time = new_procs[i]->delta_utime + new_procs[i]->delta_stime;        }    }    total_delta_time = (new_cpu.utime + new_cpu.ntime + new_cpu.stime + new_cpu.itime)                     - (old_cpu.utime + old_cpu.ntime + old_cpu.stime + old_cpu.itime);    qsort(new_procs, num_new_procs, sizeof(struct proc_info *), proc_cmp);    printf("\n\n\n");    if (!threads)         printf("%5s %4s %1s %5s %7s %7s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "UID", "Name");    else        printf("%5s %5s %4s %1s %7s %7s %-8s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "UID", "Name");    for (i = 0; i < num_new_procs; i++) {        proc = new_procs[i];        if (!proc || (max_procs && (i >= max_procs)))            break;        user  = getpwuid(proc->uid);        group = getgrgid(proc->gid);        if (user && user->pw_name) {            user_str = user->pw_name;        } else {            snprintf(user_buf, 20, "%d", proc->uid);            user_str = user_buf;        }        if (group && group->gr_name) {            group_str = group->gr_name;        } else {            snprintf(group_buf, 20, "%d", proc->gid);            group_str = group_buf;        }        if (!threads)             printf("%5d %3ld%% %c %5d %6ldK %6ldK %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,                proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name);        else            printf("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state,                proc->vss / 1024, proc->rss * getpagesize() / 1024, user_str, proc->name);    }}static struct proc_info *find_old_proc(pid_t pid, pid_t tid) {    int i;    for (i = 0; i < num_old_procs; i++)        if (old_procs[i] && (old_procs[i]->pid == pid) && (old_procs[i]->tid == tid))            return old_procs[i];    return NULL;}static void free_old_procs(void) {    int i;    for (i = 0; i < num_old_procs; i++)        if (old_procs[i])            free_proc(old_procs[i]);    free(old_procs);}static int proc_cpu_cmp(const void *a, const void *b) {    struct proc_info *pa, *pb;    pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);    if (!pa && !pb) return 0;    if (!pa) return 1;    if (!pb) return -1;    return -numcmp(pa->delta_time, pb->delta_time);}static int proc_vss_cmp(const void *a, const void *b) {    struct proc_info *pa, *pb;    pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);    if (!pa && !pb) return 0;    if (!pa) return 1;    if (!pb) return -1;    return -numcmp(pa->vss, pb->vss);}static int proc_rss_cmp(const void *a, const void *b) {    struct proc_info *pa, *pb;    pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);    if (!pa && !pb) return 0;    if (!pa) return 1;    if (!pb) return -1;    return -numcmp(pa->rss, pb->rss);}static int proc_thr_cmp(const void *a, const void *b) {    struct proc_info *pa, *pb;    pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);    if (!pa && !pb) return 0;    if (!pa) return 1;    if (!pb) return -1;    return -numcmp(pa->num_threads, pb->num_threads);}static int numcmp(long long a, long long b) {    if (a < b) return -1;    if (a > b) return 1;    return 0;}static void usage(char *cmd) {    fprintf(stderr, "Usage: %s [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]\n"                    "    -m num  Maximum number of processes to display.\n"                    "    -n num  Updates to show before exiting.\n"                    "    -d num  Seconds to wait between updates.\n"                    "    -s col  Column to sort by (cpu,vss,rss,thr).\n"                    "    -t      Show threads instead of processes.\n"                    "    -h      Display this help screen.\n",        cmd);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -