diff options
Diffstat (limited to 'libps/spec.c')
-rw-r--r-- | libps/spec.c | 225 |
1 files changed, 145 insertions, 80 deletions
diff --git a/libps/spec.c b/libps/spec.c index 62b6dfc2..b34a2343 100644 --- a/libps/spec.c +++ b/libps/spec.c @@ -1,8 +1,8 @@ /* Access, formatting, & comparison routines for printing process info. - Copyright (C) 1995, 1996 Free Software Foundation, Inc. + Copyright (C) 1995,96,97,99,2001,02 Free Software Foundation, Inc. - Written by Miles Bader <miles@gnu.ai.mit.edu> + Written by Miles Bader <miles@gnu.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -41,7 +41,7 @@ static char *get_rpc_name (mach_msg_id_t it) { return 0; } typedef void (*vf)(); -static int +static int ps_get_pid (struct proc_stat *ps) { return proc_stat_pid (ps); @@ -49,7 +49,7 @@ ps_get_pid (struct proc_stat *ps) const struct ps_getter ps_pid_getter = {"pid", PSTAT_PID, (vf) ps_get_pid}; -static int +static int ps_get_thread_index (struct proc_stat *ps) { return proc_stat_thread_index (ps); @@ -73,7 +73,7 @@ ps_get_owner_uid (struct proc_stat *ps) const struct ps_getter ps_owner_uid_getter = {"uid", PSTAT_OWNER_UID, (vf) ps_get_owner_uid}; -static int +static int ps_get_ppid (struct proc_stat *ps) { return proc_stat_proc_info (ps)->ppid; @@ -81,7 +81,7 @@ ps_get_ppid (struct proc_stat *ps) const struct ps_getter ps_ppid_getter = {"ppid", PSTAT_PROC_INFO, (vf) ps_get_ppid}; -static int +static int ps_get_pgrp (struct proc_stat *ps) { return proc_stat_proc_info (ps)->pgrp; @@ -89,7 +89,7 @@ ps_get_pgrp (struct proc_stat *ps) const struct ps_getter ps_pgrp_getter = {"pgrp", PSTAT_PROC_INFO, (vf) ps_get_pgrp}; -static int +static int ps_get_session (struct proc_stat *ps) { return proc_stat_proc_info (ps)->session; @@ -97,7 +97,7 @@ ps_get_session (struct proc_stat *ps) const struct ps_getter ps_session_getter = {"session", PSTAT_PROC_INFO, (vf) ps_get_session}; -static int +static int ps_get_login_col (struct proc_stat *ps) { return proc_stat_proc_info (ps)->logincollection; @@ -105,7 +105,7 @@ ps_get_login_col (struct proc_stat *ps) const struct ps_getter ps_login_col_getter = {"login_col", PSTAT_PROC_INFO, (vf) ps_get_login_col}; -static int +static int ps_get_num_threads (struct proc_stat *ps) { return proc_stat_num_threads (ps); @@ -113,7 +113,7 @@ ps_get_num_threads (struct proc_stat *ps) const struct ps_getter ps_num_threads_getter = {"num_threads", PSTAT_NUM_THREADS, (vf)ps_get_num_threads}; -static void +static void ps_get_args (struct proc_stat *ps, char **args_p, int *args_len_p) { *args_p = proc_stat_args (ps); @@ -122,7 +122,16 @@ ps_get_args (struct proc_stat *ps, char **args_p, int *args_len_p) const struct ps_getter ps_args_getter = {"args", PSTAT_ARGS, ps_get_args}; -static int +static void +ps_get_env (struct proc_stat *ps, char **env_p, int *env_len_p) +{ + *env_p = proc_stat_env (ps); + *env_len_p = proc_stat_env_len (ps); +} +const struct ps_getter ps_env_getter = +{"env", PSTAT_ENV, ps_get_env}; + +static int ps_get_state (struct proc_stat *ps) { return proc_stat_state (ps); @@ -139,7 +148,7 @@ ps_get_wait (struct proc_stat *ps, char **wait, int *rpc) const struct ps_getter ps_wait_getter = {"wait", PSTAT_THREAD_WAIT, ps_get_wait}; -static int +static size_t ps_get_vsize (struct proc_stat *ps) { return proc_stat_task_basic_info (ps)->virtual_size; @@ -147,7 +156,7 @@ ps_get_vsize (struct proc_stat *ps) const struct ps_getter ps_vsize_getter = {"vsize", PSTAT_TASK_BASIC, (vf) ps_get_vsize}; -static int +static size_t ps_get_rsize (struct proc_stat *ps) { return proc_stat_task_basic_info (ps)->resident_size; @@ -155,7 +164,7 @@ ps_get_rsize (struct proc_stat *ps) const struct ps_getter ps_rsize_getter = {"rsize", PSTAT_TASK_BASIC, (vf) ps_get_rsize}; -static int +static int ps_get_cur_priority (struct proc_stat *ps) { return proc_stat_thread_basic_info (ps)->cur_priority; @@ -163,7 +172,7 @@ ps_get_cur_priority (struct proc_stat *ps) const struct ps_getter ps_cur_priority_getter = {"cur_priority", PSTAT_THREAD_BASIC, (vf) ps_get_cur_priority}; -static int +static int ps_get_base_priority (struct proc_stat *ps) { return proc_stat_thread_basic_info (ps)->base_priority; @@ -171,7 +180,7 @@ ps_get_base_priority (struct proc_stat *ps) const struct ps_getter ps_base_priority_getter = {"base_priority", PSTAT_THREAD_BASIC, (vf) ps_get_base_priority}; -static int +static int ps_get_max_priority (struct proc_stat *ps) { return proc_stat_thread_sched_info (ps)->max_priority; @@ -179,7 +188,7 @@ ps_get_max_priority (struct proc_stat *ps) const struct ps_getter ps_max_priority_getter = {"max_priority", PSTAT_THREAD_SCHED, (vf) ps_get_max_priority}; -static void +static void ps_get_usr_time (struct proc_stat *ps, struct timeval *tv) { time_value_t tvt = proc_stat_thread_basic_info (ps)->user_time; @@ -189,7 +198,7 @@ ps_get_usr_time (struct proc_stat *ps, struct timeval *tv) const struct ps_getter ps_usr_time_getter = {"usr_time", PSTAT_THREAD_BASIC, ps_get_usr_time}; -static void +static void ps_get_sys_time (struct proc_stat *ps, struct timeval *tv) { time_value_t tvt = proc_stat_thread_basic_info (ps)->system_time; @@ -199,7 +208,7 @@ ps_get_sys_time (struct proc_stat *ps, struct timeval *tv) const struct ps_getter ps_sys_time_getter = {"sys_time", PSTAT_THREAD_BASIC, ps_get_sys_time}; -static void +static void ps_get_tot_time (struct proc_stat *ps, struct timeval *tv) { time_value_t tvt = proc_stat_thread_basic_info (ps)->user_time; @@ -210,10 +219,20 @@ ps_get_tot_time (struct proc_stat *ps, struct timeval *tv) const struct ps_getter ps_tot_time_getter = {"tot_time", PSTAT_THREAD_BASIC, ps_get_tot_time}; -static float +static void +ps_get_start_time (struct proc_stat *ps, struct timeval *tv) +{ + time_value_t *const tvt = &proc_stat_task_basic_info (ps)->creation_time; + tv->tv_sec = tvt->seconds; + tv->tv_usec = tvt->microseconds; +} +const struct ps_getter ps_start_time_getter = +{"start_time", PSTAT_TASK_BASIC, ps_get_start_time}; + +static float ps_get_rmem_frac (struct proc_stat *ps) { - static int mem_size = 0; + static size_t mem_size = 0; if (mem_size == 0) { @@ -222,7 +241,7 @@ ps_get_rmem_frac (struct proc_stat *ps) if (err == 0) mem_size = info->memory_size; } - + if (mem_size > 0) return (float)proc_stat_task_basic_info (ps)->resident_size @@ -233,7 +252,7 @@ ps_get_rmem_frac (struct proc_stat *ps) const struct ps_getter ps_rmem_frac_getter = {"rmem_frac", PSTAT_TASK_BASIC, (vf) ps_get_rmem_frac}; -static float +static float ps_get_cpu_frac (struct proc_stat *ps) { return (float) proc_stat_thread_basic_info (ps)->cpu_usage @@ -242,7 +261,7 @@ ps_get_cpu_frac (struct proc_stat *ps) const struct ps_getter ps_cpu_frac_getter = {"cpu_frac", PSTAT_THREAD_BASIC, (vf) ps_get_cpu_frac}; -static int +static int ps_get_sleep (struct proc_stat *ps) { return proc_stat_thread_basic_info (ps)->sleep_time; @@ -250,7 +269,7 @@ ps_get_sleep (struct proc_stat *ps) const struct ps_getter ps_sleep_getter = {"sleep", PSTAT_THREAD_BASIC, (vf) ps_get_sleep}; -static int +static int ps_get_susp_count (struct proc_stat *ps) { return proc_stat_suspend_count (ps); @@ -258,7 +277,7 @@ ps_get_susp_count (struct proc_stat *ps) const struct ps_getter ps_susp_count_getter = {"susp_count", PSTAT_SUSPEND_COUNT, (vf) ps_get_susp_count}; -static int +static int ps_get_proc_susp_count (struct proc_stat *ps) { return proc_stat_task_basic_info (ps)->suspend_count; @@ -266,7 +285,7 @@ ps_get_proc_susp_count (struct proc_stat *ps) const struct ps_getter ps_proc_susp_count_getter = {"proc_susp_count", PSTAT_TASK_BASIC, (vf) ps_get_proc_susp_count}; -static int +static int ps_get_thread_susp_count (struct proc_stat *ps) { return proc_stat_thread_basic_info (ps)->suspend_count; @@ -282,7 +301,7 @@ ps_get_tty (struct proc_stat *ps) const struct ps_getter ps_tty_getter = {"tty", PSTAT_TTY, (vf)ps_get_tty}; -static int +static int ps_get_page_faults (struct proc_stat *ps) { return proc_stat_task_events_info (ps)->faults; @@ -290,7 +309,7 @@ ps_get_page_faults (struct proc_stat *ps) const struct ps_getter ps_page_faults_getter = {"page_faults", PSTAT_TASK_EVENTS, (vf) ps_get_page_faults}; -static int +static int ps_get_cow_faults (struct proc_stat *ps) { return proc_stat_task_events_info (ps)->cow_faults; @@ -298,7 +317,7 @@ ps_get_cow_faults (struct proc_stat *ps) const struct ps_getter ps_cow_faults_getter = {"cow_faults", PSTAT_TASK_EVENTS, (vf) ps_get_cow_faults}; -static int +static int ps_get_pageins (struct proc_stat *ps) { return proc_stat_task_events_info (ps)->pageins; @@ -306,7 +325,7 @@ ps_get_pageins (struct proc_stat *ps) const struct ps_getter ps_pageins_getter = {"pageins", PSTAT_TASK_EVENTS, (vf) ps_get_pageins}; -static int +static int ps_get_msgs_sent (struct proc_stat *ps) { return proc_stat_task_events_info (ps)->messages_sent; @@ -314,7 +333,7 @@ ps_get_msgs_sent (struct proc_stat *ps) const struct ps_getter ps_msgs_sent_getter = {"msgs_sent", PSTAT_TASK_EVENTS, (vf) ps_get_msgs_sent}; -static int +static int ps_get_msgs_rcvd (struct proc_stat *ps) { return proc_stat_task_events_info (ps)->messages_received; @@ -322,7 +341,7 @@ ps_get_msgs_rcvd (struct proc_stat *ps) const struct ps_getter ps_msgs_rcvd_getter = {"msgs_rcvd", PSTAT_TASK_EVENTS, (vf) ps_get_msgs_rcvd}; -static int +static int ps_get_zero_fills (struct proc_stat *ps) { return proc_stat_task_events_info (ps)->zero_fills; @@ -330,6 +349,14 @@ ps_get_zero_fills (struct proc_stat *ps) const struct ps_getter ps_zero_fills_getter = {"zero_fills", PSTAT_TASK_EVENTS, (vf) ps_get_zero_fills}; +static int +ps_get_num_ports (struct proc_stat *ps) +{ + return proc_stat_num_ports (ps); +} +const struct ps_getter ps_num_ports_getter = +{"num_ports", PSTAT_NUM_PORTS, (vf) ps_get_num_ports}; + /* ---------------------------------------------------------------- */ /* some printing functions */ @@ -378,16 +405,18 @@ ps_emit_num_blocks (struct proc_stat *ps, struct ps_fmt_field *field, return ps_stream_write_field (stream, buf, field->width); } -int +size_t sprint_frac_value (char *buf, - int value, int min_value_len, - int frac, int frac_scale, + size_t value, int min_value_len, + size_t frac, int frac_scale, int width) { - int value_len; - int frac_len; + int value_len = 0; + int frac_len = 0; - if (value >= 100) /* the integer part */ + if (value >= 1000) /* the integer part */ + value_len = 4; /* values 1000-1023 */ + if (value >= 100) value_len = 3; else if (value >= 10) value_len = 2; @@ -403,9 +432,9 @@ sprint_frac_value (char *buf, frac /= 10; if (frac_len > 0) - sprintf (buf, "%d.%0*d", value, frac_len, frac); + sprintf (buf, "%zd.%0*zd", value, frac_len, frac); else - sprintf (buf, "%d", value); + sprintf (buf, "%zd", value); return strlen (buf); } @@ -430,13 +459,13 @@ ps_emit_percent (struct proc_stat *ps, struct ps_fmt_field *field, /* prints its value nicely */ error_t -ps_emit_nice_int (struct proc_stat *ps, struct ps_fmt_field *field, - struct ps_stream *stream) +ps_emit_nice_size_t (struct proc_stat *ps, struct ps_fmt_field *field, + struct ps_stream *stream) { char buf[20]; - int value = FG (field, int)(ps); + size_t value = FG (field, size_t)(ps); char *sfx = " KMG"; - int frac = 0; + size_t frac = 0; while (value >= 1024) { @@ -462,8 +491,11 @@ ps_emit_seconds (struct proc_stat *ps, struct ps_fmt_field *field, FG (field, void)(ps, &tv); - fmt_seconds (&tv, !(field->flags & PS_FMT_FIELD_AT_MOD), prec, ABS (width), - buf, sizeof (buf)); + if ((field->flags & PS_FMT_FIELD_COLON_MOD) && tv.tv_sec == 0) + strcpy (buf, "-"); + else + fmt_seconds (&tv, !(field->flags & PS_FMT_FIELD_AT_MOD), prec, ABS (width), + buf, sizeof (buf)); return ps_stream_write_field (stream, buf, width); } @@ -476,10 +508,13 @@ ps_emit_minutes (struct proc_stat *ps, struct ps_fmt_field *field, struct timeval tv; int width = field->width; - FG (field, int)(ps, &tv); + FG (field, void)(ps, &tv); - fmt_minutes (&tv, !(field->flags & PS_FMT_FIELD_AT_MOD), ABS (width), - buf, sizeof (buf)); + if ((field->flags & PS_FMT_FIELD_COLON_MOD) && tv.tv_sec < 60) + strcpy (buf, "-"); + else + fmt_minutes (&tv, !(field->flags & PS_FMT_FIELD_AT_MOD), ABS (width), + buf, sizeof (buf)); return ps_stream_write_field (stream, buf, width); } @@ -493,7 +528,7 @@ ps_emit_past_time (struct proc_stat *ps, struct ps_fmt_field *field, struct timeval tv; int width = field->width; - FG (field, int)(ps, &tv); + FG (field, void)(ps, &tv); if (now.tv_sec == 0 && gettimeofday (&now, 0) < 0) return errno; @@ -544,7 +579,7 @@ ps_emit_user_name (struct proc_stat *ps, struct ps_fmt_field *field, if (pw == NULL) { char buf[20]; - sprintf (buf, "(UID %d)", pw->pw_uid); + sprintf (buf, "(UID %d)", u->uid); return ps_stream_write_field (stream, buf, width); } else @@ -568,8 +603,8 @@ ps_emit_args (struct proc_stat *ps, struct ps_fmt_field *field, FG (field, void)(ps, &s0, &s0len); - if (s0 == NULL) - *buf = '\0'; + if (!s0 || s0len == 0 ) + strcpy (buf, "-"); else { if (s0len > sizeof static_buf) @@ -610,8 +645,8 @@ ps_emit_string (struct proc_stat *ps, struct ps_fmt_field *field, FG (field, void)(ps, &str, &len); - if (str == NULL) - str = ""; + if (!str || len == 0) + str = "-"; return ps_stream_write_trunc_field (stream, str, field->width); } @@ -706,7 +741,6 @@ ps_emit_wait (struct proc_stat *ps, struct ps_fmt_field *field, else if (strcmp (wait, "kernel") == 0) /* A syscall. RPC is actually the syscall number. */ { - extern char *get_syscall_name (int num); char *name = get_syscall_name (rpc); if (! name) { @@ -719,29 +753,35 @@ ps_emit_wait (struct proc_stat *ps, struct ps_fmt_field *field, /* An rpc (with msg id RPC); WAIT describes the dest port. */ { char port_name_buf[20]; - extern char *get_rpc_name (mach_msg_id_t num); char *name = get_rpc_name (rpc); /* See if we should give a more useful name for the port. */ if (strcmp (wait, "init#0") == 0) - wait = "cwdir"; /* Current directory */ + wait = "cwd"; /* Current directory */ else if (strcmp (wait, "init#1") == 0) - wait = "crdir"; /* Root directory */ + wait = "root"; /* Root directory */ else if (strcmp (wait, "init#2") == 0) wait = "auth"; /* Auth port */ else if (strcmp (wait, "init#3") == 0) wait = "proc"; /* Proc port */ else if (strcmp (wait, "init#4") == 0) - wait = "bootstrap"; /* Bootstrap port */ + wait = "cttyid"; /* Ctty id port */ + else if (strcmp (wait, "init#5") == 0) + wait = "boot"; /* Bootstrap port */ else - /* See if we can shorten the name to fit better. We happen know that - all currently returned keys are unique in the first character. */ + /* See if we can shorten the name to fit better. */ { - char *sep = index (wait, '#'); - if (sep && sep > wait) + char *abbrev = 0, *num = 0; + if (strncmp (wait, "fd#", 3) == 0) + abbrev = "fd", num = wait + 3; + else if (strncmp (wait, "bgfd#", 5) == 0) + abbrev = "bg", num = wait + 5; + else if (strncmp (wait, "port#", 5) == 0) + abbrev = "", num = wait + 5; + if (abbrev) { snprintf (port_name_buf, sizeof port_name_buf, - "%c%s", wait[0], sep); + "%s%s", abbrev, num); wait = port_name_buf; } } @@ -764,25 +804,34 @@ ps_emit_wait (struct proc_stat *ps, struct ps_fmt_field *field, #define GUARDED_CMP(s1, s2, call) \ ((s1) == NULL ? (((s2) == NULL) ? 0 : -1) : ((s2) == NULL ? 1 : (call))) -int +int ps_cmp_ints (struct proc_stat *ps1, struct proc_stat *ps2, const struct ps_getter *getter) { int (*gf)() = G (getter, int); int v1 = gf(ps1), v2 = gf (ps2); return v1 == v2 ? 0 : v1 < v2 ? -1 : 1; -} +} -int +int ps_cmp_floats (struct proc_stat *ps1, struct proc_stat *ps2, const struct ps_getter *getter) { float (*gf)() = G (getter, float); float v1 = gf(ps1), v2 = gf (ps2); return v1 == v2 ? 0 : v1 < v2 ? -1 : 1; -} +} -int +int +ps_cmp_size_ts (struct proc_stat *ps1, struct proc_stat *ps2, + const struct ps_getter *getter) +{ + size_t (*gf)() = G (getter, size_t); + size_t v1 = gf(ps1), v2 = gf (ps2); + return v1 == v2 ? 0 : v1 < v2 ? -1 : 1; +} + +int ps_cmp_uids (struct proc_stat *ps1, struct proc_stat *ps2, const struct ps_getter *getter) { @@ -791,7 +840,7 @@ ps_cmp_uids (struct proc_stat *ps1, struct proc_stat *ps2, return (u1 ? ps_user_uid (u1) : -1) - (u2 ? ps_user_uid (u2) : -1); } -int +int ps_cmp_unames (struct proc_stat *ps1, struct proc_stat *ps2, const struct ps_getter *getter) { @@ -802,7 +851,7 @@ ps_cmp_unames (struct proc_stat *ps1, struct proc_stat *ps2, return GUARDED_CMP (pw1, pw2, strcmp (pw1->pw_name, pw2->pw_name)); } -int +int ps_cmp_strings (struct proc_stat *ps1, struct proc_stat *ps2, const struct ps_getter *getter) { @@ -845,6 +894,16 @@ ps_nominal_zint (struct proc_stat *ps, const struct ps_getter *getter) return G (getter, int)(ps) == 0; } +/* Neither is an empty string. */ +int +ps_nominal_string (struct proc_stat *ps, const struct ps_getter *getter) +{ + char *str; + size_t len; + G (getter, char *)(ps, &str, &len); + return !str || len == 0 || (len == 1 && *str == '-'); +} + /* Priorities are similar, but have to be converted to the unix nice scale first. */ int @@ -864,7 +923,7 @@ ps_nominal_nth (struct proc_stat *ps, const struct ps_getter *getter) static int own_uid = -2; /* -1 means no uid at all. */ /* A user is nominal if it's the current user. */ -int +int ps_nominal_user (struct proc_stat *ps, const struct ps_getter *getter) { struct ps_user *u = G (getter, struct ps_user *)(ps); @@ -874,7 +933,7 @@ ps_nominal_user (struct proc_stat *ps, const struct ps_getter *getter) } /* A uid is nominal if it's that of the current user. */ -int +int ps_nominal_uid (struct proc_stat *ps, const struct ps_getter *getter) { uid_t uid = G (getter, uid_t)(ps); @@ -916,7 +975,7 @@ specv_find (const struct ps_fmt_spec *specs, const char *name, return 0; } -/* Number of specs allocated in each block of expansions. */ +/* Number of specs allocated in each block of expansions. */ #define EXP_BLOCK_SIZE 20 /* A node in a linked list of spec vectors. */ @@ -1051,9 +1110,13 @@ static const struct ps_fmt_spec specs[] = {"LColl", 0, -5, -1, 0, &ps_login_col_getter, ps_emit_int, ps_cmp_ints, 0}, {"Args", 0, 0, -1, 0, - &ps_args_getter, ps_emit_args, ps_cmp_strings,0}, + &ps_args_getter, ps_emit_args, ps_cmp_strings,ps_nominal_string}, {"Arg0", 0, 0, -1, 0, - &ps_args_getter, ps_emit_string, ps_cmp_strings,0}, + &ps_args_getter, ps_emit_string, ps_cmp_strings,ps_nominal_string}, + {"Env", 0, 0, -1, 0, + &ps_env_getter, ps_emit_args, ps_cmp_strings,ps_nominal_string}, + {"Start", 0, -7, 1, 0, + &ps_start_time_getter, ps_emit_past_time, ps_cmp_times,0}, {"Time", 0, -8, 2, 0, &ps_tot_time_getter, ps_emit_seconds, ps_cmp_times, 0}, {"UTime", 0, -8, 2, 0, @@ -1061,9 +1124,9 @@ static const struct ps_fmt_spec specs[] = {"STime", 0, -8, 2, 0, &ps_sys_time_getter, ps_emit_seconds, ps_cmp_times, 0}, {"VSize", 0, -5, -1, 0, - &ps_vsize_getter, ps_emit_nice_int,ps_cmp_ints, 0}, + &ps_vsize_getter, ps_emit_nice_size_t,ps_cmp_size_ts, 0}, {"RSize", 0, -5, -1, 0, - &ps_rsize_getter, ps_emit_nice_int,ps_cmp_ints, 0}, + &ps_rsize_getter, ps_emit_nice_size_t,ps_cmp_size_ts, 0}, {"Pri", 0, -3, -1, 0, &ps_cur_priority_getter,ps_emit_priority,ps_cmp_ints, ps_nominal_pri}, {"BPri", 0, -3, -1, 0, @@ -1100,6 +1163,8 @@ static const struct ps_fmt_spec specs[] = &ps_msgs_sent_getter, ps_emit_int, ps_cmp_ints, ps_nominal_zint}, {"ZFills", 0, -5, -1, 0, &ps_zero_fills_getter, ps_emit_int, ps_cmp_ints, ps_nominal_zint}, + {"Ports", 0, -5, -1, 0, + &ps_num_ports_getter, ps_emit_int, ps_cmp_ints, 0}, {0} }; |