diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-02-12 01:36:13 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-02-12 01:36:13 +0000 |
commit | 4d41f0d5e871b86b8f56339a99cfb1b80fbb420c (patch) | |
tree | 0446c51aeb07d8ef817fe3cdc53955dc7fe3a09d /src/libs6ps | |
parent | 2f8984613e17f9e5971ec338240b0bb6d2dd1cc6 (diff) | |
download | s6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.tar.gz s6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.tar.xz s6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.zip |
Deglobalize s6-ps
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libs6ps')
-rw-r--r-- | src/libs6ps/deps-lib/s6ps | 8 | ||||
-rw-r--r-- | src/libs6ps/s6ps-internal.h | 17 | ||||
-rw-r--r-- | src/libs6ps/s6ps_cache.c | 34 | ||||
-rw-r--r-- | src/libs6ps/s6ps_grcache.c | 47 | ||||
-rw-r--r-- | src/libs6ps/s6ps_otree.c | 100 | ||||
-rw-r--r-- | src/libs6ps/s6ps_pfield.c | 575 | ||||
-rw-r--r-- | src/libs6ps/s6ps_pwcache.c | 47 | ||||
-rw-r--r-- | src/libs6ps/s6ps_statparse.c | 186 | ||||
-rw-r--r-- | src/libs6ps/s6ps_ttycache.c | 118 | ||||
-rw-r--r-- | src/libs6ps/s6ps_wchan.c | 93 |
10 files changed, 1225 insertions, 0 deletions
diff --git a/src/libs6ps/deps-lib/s6ps b/src/libs6ps/deps-lib/s6ps new file mode 100644 index 0000000..f3bc04a --- /dev/null +++ b/src/libs6ps/deps-lib/s6ps @@ -0,0 +1,8 @@ +s6ps_cache.o +s6ps_grcache.o +s6ps_otree.o +s6ps_pfield.o +s6ps_pwcache.o +s6ps_statparse.o +s6ps_ttycache.o +s6ps_wchan.o diff --git a/src/libs6ps/s6ps-internal.h b/src/libs6ps/s6ps-internal.h new file mode 100644 index 0000000..5f205da --- /dev/null +++ b/src/libs6ps/s6ps-internal.h @@ -0,0 +1,17 @@ +/* ISC license. */ + +#ifndef S6PS_INTERNAL_H +#define S6PS_INTERNAL_H + +#include <stdint.h> +#include <stddef.h> + +typedef struct dius_s dius_t, *dius_t_ref ; +struct dius_s +{ + uint32_t left ; + size_t right ; +} ; +#define DIUS_ZERO { .left = 0, .right = 0 } + +#endif diff --git a/src/libs6ps/s6ps_cache.c b/src/libs6ps/s6ps_cache.c new file mode 100644 index 0000000..0ef438a --- /dev/null +++ b/src/libs6ps/s6ps_cache.c @@ -0,0 +1,34 @@ +/* ISC license. */ + +#include <stdint.h> + +#include <skalibs/genalloc.h> +#include <skalibs/avltree.h> + +#include "s6ps.h" +#include "s6ps-internal.h" + +static void *left_dtok (unsigned int d, void *x) +{ + return (void *)&genalloc_s(dius_t, (genalloc *)x)[d].left ; +} + +int s6ps_uint32_cmp (void const *a, void const *b, void *x) +{ + uint32_t aa = *(uint32_t *)a ; + uint32_t bb = *(uint32_t *)b ; + (void)x ; + return (aa < bb) ? -1 : (aa > bb) ; +} + +int s6ps_cache_init (s6ps_cache_t *cache) +{ + avltree_init(&cache->tree, 5, 3, 8, &left_dtok, &s6ps_uint32_cmp, &cache->index) ; + return 1 ; +} + +void s6ps_cache_finish (s6ps_cache_t *cache) +{ + avltree_free(&cache->tree) ; + genalloc_free(dius_t, &cache->index) ; +} diff --git a/src/libs6ps/s6ps_grcache.c b/src/libs6ps/s6ps_grcache.c new file mode 100644 index 0000000..49d84e5 --- /dev/null +++ b/src/libs6ps/s6ps_grcache.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <stdint.h> +#include <grp.h> +#include <errno.h> + +#include <skalibs/types.h> +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/skamisc.h> +#include <skalibs/avltree.h> + +#include "s6ps.h" +#include "s6ps-internal.h" + +int s6ps_grcache_lookup (s6ps_cache_t *cache, stralloc *sa, gid_t gid) +{ + dius_t d = { .left = (uint32_t)gid, .right = satmp.len } ; + uint32_t i ; + if (!avltree_search(&cache->tree, &d.left, &i)) + { + struct group *gr ; + size_t n = genalloc_len(dius_t, &cache->index) ; + errno = 0 ; + gr = getgrgid(gid) ; + if (!gr) + { + if (errno) return 0 ; + if (!stralloc_readyplus(&satmp, UINT_FMT + 2)) return 0 ; + stralloc_catb(&satmp, "(", 1) ; + satmp.len += uint_fmt(satmp.s + satmp.len, gid) ; + stralloc_catb(&satmp, ")", 2) ; + } + else if (!stralloc_cats(&satmp, gr->gr_name) || !stralloc_0(&satmp)) return 0 ; + if (!genalloc_append(dius_t, &cache->index, &d)) goto err ; + if (!avltree_insert(&cache->tree, n)) + { + genalloc_setlen(dius_t, &cache->index, n) ; + goto err ; + } + i = n ; + } + return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &cache->index)[i].right) ; + err: + satmp.len = d.right ; + return 0 ; +} diff --git a/src/libs6ps/s6ps_otree.c b/src/libs6ps/s6ps_otree.c new file mode 100644 index 0000000..c3bd202 --- /dev/null +++ b/src/libs6ps/s6ps_otree.c @@ -0,0 +1,100 @@ +/* ISC license. */ + +#include <errno.h> + +#include <skalibs/avltreen.h> + +#include "s6ps.h" + +typedef struct ptreeiter_s ptreeiter_t, *ptreeiter_t_ref ; +struct ptreeiter_s +{ + unsigned int *childlist ; + unsigned int const *childindex ; + unsigned int const *ppindex ; + unsigned int *cpos ; +} ; + +typedef struct pstuff_s pstuff_t, *pstuff_t_ref ; +struct pstuff_s +{ + unsigned int *orderedlist ; + pscan_t *p ; + unsigned int const *childlist ; + unsigned int const *childindex ; + unsigned int const *nchild ; +} ; + +static int fillchildlist (unsigned int i, unsigned int h, void *x) +{ + ptreeiter_t *pt = x ; + unsigned int j = pt->ppindex[i] ; + pt->childlist[pt->childindex[j] + pt->cpos[j]++] = i ; + (void)h ; + return 1 ; +} + +static void fillo_tree_rec (pstuff_t *blah, unsigned int root, signed int h) +{ + static unsigned int j = 0 ; + unsigned int i = !blah->p[root].pid ; + if (blah->p[root].pid == 1) h = -1 ; + blah->p[root].height = (h > 0) ? h : 0 ; + blah->orderedlist[j++] = root ; + for (; i < blah->nchild[root] ; i++) + fillo_tree_rec(blah, blah->childlist[blah->childindex[root] + i], h+1) ; +} + + /* + Fills up orderedlist with the right indices to print a process tree. + O(n log n) time, O(n) space, all in the stack. + */ + +void s6ps_otree (pscan_t *p, unsigned int n, avltreen *pidtree, unsigned int *orderedlist) +{ + unsigned int childlist[n] ; + unsigned int childindex[n] ; + unsigned int nchild[n] ; + unsigned int i = 0 ; + for (; i < n ; i++) nchild[i] = 0 ; + + /* Compute the ppid tree */ + for (i = 0 ; i < n ; i++) + { + uint32_t k ; + if (!avltreen_search(pidtree, &p[i].ppid, &k)) k = n-1 ; + orderedlist[i] = k ; /* using orderedlist as ppindex */ + nchild[k]++ ; + } + { + unsigned int j = 0 ; + for (i = 0 ; i < n ; i++) + { + childindex[i] = j ; + j += nchild[i] ; + } + } + + /* Fill the childlist by increasing pids so it is sorted */ + { + unsigned int cpos[n] ; + ptreeiter_t blah = { .childlist = childlist, .childindex = childindex, .ppindex = orderedlist, .cpos = cpos } ; + for (i = 0 ; i < n ; i++) cpos[i] = 0 ; + avltreen_iter_nocancel(pidtree, avltreen_totalsize(pidtree), &fillchildlist, &blah) ; + } + + /* If we have init, make it the last in the orphan list */ + if (n > 1 && p[childlist[childindex[n-1]+1]].pid == 1) + { + unsigned int pos1 = childlist[childindex[n-1] + 1] ; + for (i = 2 ; i < nchild[n-1] ; i++) + childlist[childindex[n-1]+i-1] = childlist[childindex[n-1]+i] ; + childlist[childindex[n-1]+nchild[n-1]-1] = pos1 ; + } + + /* Finally, fill orderedlist by walking the childindex tree. */ + { + pstuff_t blah = { .orderedlist = orderedlist, .p = p, .childlist = childlist, .childindex = childindex, .nchild = nchild } ; + fillo_tree_rec(&blah, n-1, -1) ; + } +} diff --git a/src/libs6ps/s6ps_pfield.c b/src/libs6ps/s6ps_pfield.c new file mode 100644 index 0000000..6d84ea0 --- /dev/null +++ b/src/libs6ps/s6ps_pfield.c @@ -0,0 +1,575 @@ +/* ISC license. */ + +#include <string.h> +#include <stdint.h> +#include <unistd.h> +#include <time.h> +#include <sys/sysinfo.h> + +#include <skalibs/uint64.h> +#include <skalibs/types.h> +#include <skalibs/strerr.h> +#include <skalibs/tai.h> +#include <skalibs/djbtime.h> +#include <skalibs/stralloc.h> + +#include "s6ps.h" + +static char const *const fieldheaders[PFIELD_PHAIL] = +{ + "PID", + "COMM", + "STAT", + "PPID", + "PGRP", + "SESSION", + "TTY", + "TPGID", + "UTIME", + "STIME", + "CUTIME", + "CSTIME", + "PRIO", + "NICE", + "THREADS", + "START", + "VSZ", + "RSS", + "RSSLIM", + "CPU", + "RTPRIO", + "RTPOLICY", + "USER", + "GROUP", + "%MEM", + "WCHAN", + "COMMAND", + "ENVIRONMENT", + "%CPU", + "TTIME", + "CTTIME", + "TSTART", + "C%CPU" +} ; + +char const *const *s6ps_fieldheaders = fieldheaders ; + +static char const *const opttable[PFIELD_PHAIL] = +{ + "pid", + "comm", + "s", + "ppid", + "pgrp", + "sess", + "tty", + "tpgid", + "utime", + "stime", + "cutime", + "cstime", + "prio", + "nice", + "thcount", + "start", + "vsize", + "rss", + "rsslimit", + "psr", + "rtprio", + "policy", + "user", + "group", + "pmem", + "wchan", + "args", + "env", + "pcpu", + "ttime", + "cttime", + "tstart", + "cpcpu" +} ; + +char const *const *s6ps_opttable = opttable ; + +static int fmt_64 (pscan_t *p, size_t *pos, size_t *len, uint64_t u) +{ + if (!stralloc_readyplus(&p->data, UINT64_FMT)) return 0 ; + *pos = p->data.len ; + *len = uint64_fmt(p->data.s + *pos, u) ; + p->data.len += *len ; + return 1 ; +} + +static int fmt_i (pscan_t *p, size_t *pos, size_t *len, int d) +{ + if (!stralloc_readyplus(&p->data, UINT32_FMT+1)) return 0 ; + *pos = p->data.len ; + *len = int_fmt(p->data.s + *pos, d) ; + p->data.len += *len ; + return 1 ; +} + +static int fmt_pid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->pid) ; +} + +static int fmt_comm (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + *pos = p->statlen ; + *len = p->commlen ; + return 1 ; +} + +static int fmt_s (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + if (!stralloc_readyplus(&p->data, 4)) return 0 ; + *pos = p->data.len ; + p->data.s[p->data.len++] = p->data.s[p->state] ; + if (p->pid == p->session) p->data.s[p->data.len++] = 's' ; + if (p->threads > 1) p->data.s[p->data.len++] = 'l' ; + if ((p->tpgid > 0) && ((unsigned int)p->tpgid == p->pgrp)) + p->data.s[p->data.len++] = '+' ; + if (p->nice) p->data.s[p->data.len++] = (p->nice < 0) ? '<' : 'N' ; + + *len = p->data.len - *pos ; + return 1 ; +} + +static int fmt_ppid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->ppid) ; +} + +static int fmt_pgrp (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->pgrp) ; +} + +static int fmt_session (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->session) ; +} + +static int fmt_ttynr(s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + if (p->ttynr) + { + size_t tmppos = p->data.len ; + if (!s6ps_ttycache_lookup(&aux->caches[2], &p->data, p->ttynr)) return 0 ; + *pos = tmppos ; + *len = p->data.len - tmppos ; + } + else + { + if (!stralloc_catb(&p->data, "-", 1)) return 0 ; + *pos = p->data.len - 1 ; + *len = 1 ; + } + return 1 ; +} + +static int fmt_tpgid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return p->tpgid < 0 ? fmt_i(p, pos, len, -1) : fmt_64(p, pos, len, p->tpgid) ; +} + +static unsigned int gethz (s6ps_auxinfo_t *aux) +{ + if (!aux->hz) + { + long jiffies = sysconf(_SC_CLK_TCK) ; + if (jiffies < 1) + { + char fmt[ULONG_FMT + 1] ; + fmt[long_fmt(fmt, jiffies)] = 0 ; + strerr_warnw3x("invalid _SC_CLK_TCK value (", fmt, "), using 100") ; + aux->hz = 100 ; + } + else aux->hz = (unsigned int)jiffies ; + } + return aux->hz ; +} + +int s6ps_compute_boottime (s6ps_auxinfo_t *aux, pscan_t *p, unsigned int mypos) +{ + if (!mypos--) + { + strerr_warnwu1x("compute boot time - using epoch") ; + return 0 ; + } + else + { + unsigned int hz = gethz(aux) ; + tain offset = { .sec = { .x = p[mypos].start / hz }, .nano = (p[mypos].start % hz) * (1000000000 / hz) } ; + tain_sub(&aux->boottime, &STAMP, &offset) ; + return 1 ; + } +} + +static int fmt_jiffies (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len, uint64_t j) +{ + unsigned int hz = gethz(aux) ; + uint32_t hrs, mins, secs, hfrac ; + if (!stralloc_readyplus(&p->data, UINT64_FMT + 13)) return 0 ; + hfrac = (j % hz) * 100 / hz ; + *pos = p->data.len ; + j /= hz ; + secs = j % 60 ; j /= 60 ; + mins = j % 60 ; j /= 60 ; + hrs = j % 24 ; j /= 24 ; + if (j) + { + p->data.len += uint64_fmt(p->data.s + p->data.len, j) ; + p->data.s[p->data.len++] = 'd' ; + } + if (j || hrs) + { + uint320_fmt(p->data.s + p->data.len, hrs, 2) ; + p->data.len += 2 ; + p->data.s[p->data.len++] = 'h' ; + } + if (j || hrs || mins) + { + uint320_fmt(p->data.s + p->data.len, mins, 2) ; + p->data.len += 2 ; + p->data.s[p->data.len++] = 'm' ; + } + uint320_fmt(p->data.s + p->data.len, secs, 2) ; + p->data.len += 2 ; + if (!j && !hrs && !mins) + { + p->data.s[p->data.len++] = '.' ; + uint320_fmt(p->data.s + p->data.len, hfrac, 2) ; + p->data.len += 2 ; + } + p->data.s[p->data.len++] = 's' ; + *len = p->data.len - *pos ; + return 1 ; +} + +static int fmt_utime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_jiffies(aux, p, pos, len, p->utime) ; +} + +static int fmt_stime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_jiffies(aux, p, pos, len, p->stime) ; +} + +static int fmt_cutime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_jiffies(aux, p, pos, len, p->utime + p->cutime) ; +} + +static int fmt_cstime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_jiffies(aux, p, pos, len, p->stime + p->cstime) ; +} + +static int fmt_prio (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_i(p, pos, len, p->prio) ; +} + +static int fmt_nice (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_i(p, pos, len, p->nice) ; +} + +static int fmt_threads (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->threads) ; +} + +static int fmt_timedate (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len, struct tm const *tm) +{ + static struct tm nowtm = { .tm_year = 0 } ; + size_t tmplen ; + char *tmpstrf = "%F" ; + if (!nowtm.tm_year && !localtm_from_tai(&nowtm, tain_secp(&STAMP), 1)) return 0 ; + if (!stralloc_readyplus(&p->data, 20)) return 0 ; + if (tm->tm_year == nowtm.tm_year && tm->tm_yday == nowtm.tm_yday) + tmpstrf = "%T" ; + else if (tm->tm_year == nowtm.tm_year || (tm->tm_year+1 == nowtm.tm_year && (nowtm.tm_mon + 12 - tm->tm_mon) % 12 < 9)) + tmpstrf = "%b%d %R" ; + tmplen = strftime(p->data.s + p->data.len, 20, tmpstrf, tm) ; + if (!tmplen) return 0 ; + *len = tmplen ; + *pos = p->data.len ; + p->data.len += tmplen ; + return 1 ; +} + +static int fmt_start (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + struct tm starttm ; + unsigned int hz = gethz(aux) ; + tain blah = { .sec = { .x = p->start / hz }, .nano = (p->start % hz) * (1000000000 / hz) } ; + tain_add(&blah, &aux->boottime, &blah) ; + if (!localtm_from_tai(&starttm, tain_secp(&blah), 1)) return 0 ; + return fmt_timedate(aux, p, pos, len, &starttm) ; +} + +static unsigned int getpgsz (s6ps_auxinfo_t *aux) +{ + if (!aux->pgsz) + { + long sz = sysconf(_SC_PAGESIZE) ; + if (sz < 1) + { + char fmt[ULONG_FMT + 1] ; + fmt[long_fmt(fmt, sz)] = 0 ; + strerr_warnw3x("invalid _SC_PAGESIZE value (", fmt, "), using 4096") ; + aux->pgsz = 4096 ; + } + else aux->pgsz = sz ; + } + return aux->pgsz ; +} + +static int fmt_vsize (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->vsize / 1024) ; +} + +static int fmt_rss (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_64(p, pos, len, p->rss * (getpgsz(aux) / 1024)) ; +} + +static int fmt_rsslim (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->rsslim / 1024) ; +} + +static int fmt_cpuno (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->cpuno) ; +} + +static int fmt_rtprio (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + (void)aux ; + return fmt_64(p, pos, len, p->rtprio) ; +} + +static int fmt_policy (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + static char const *const policies[8] = { "NORMAL", "FIFO", "RR", "BATCH", "ISO", "IDLE", "UNKNOWN", "UNKNOWN" } ; + size_t tmppos = p->data.len ; + if (!stralloc_cats(&p->data, policies[p->policy & 7])) return 0 ; + *pos = tmppos ; + *len = p->data.len - tmppos ; + (void)aux ; + return 1 ; +} + +static int fmt_user (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + size_t tmppos = p->data.len ; + if (!s6ps_pwcache_lookup(&aux->caches[0], &p->data, p->uid)) return 0 ; + *pos = tmppos ; + *len = p->data.len - tmppos ; + return 1 ; +} + +static int fmt_group (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + size_t tmppos = p->data.len ; + if (!s6ps_grcache_lookup(&aux->caches[1], &p->data, p->gid)) return 0 ; + *pos = tmppos ; + *len = p->data.len - tmppos ; + return 1 ; +} + +static uint64_t gettotalmem (s6ps_auxinfo_t *aux) +{ + if (!aux->totalmem) + { + struct sysinfo si = { .totalram = 0, .loads = { 0, 0, 0 } } ; + if (sysinfo(&si) < 0) return 0 ; + aux->totalmem = si.totalram ; + aux->totalmem *= si.mem_unit ; + } + return aux->totalmem ; +} + +static int percent (stralloc *sa, unsigned int n, size_t *pos, size_t *len) +{ + if (!stralloc_readyplus(sa, UINT64_FMT+1)) return 0 ; + *pos = sa->len ; + sa->len += uint_fmt(sa->s + sa->len, n / 100) ; + sa->s[sa->len++] = '.' ; + uint0_fmt(sa->s + sa->len, n % 100, 2) ; + sa->len += 2 ; + *len = sa->len - *pos ; + return 1 ; +} + +static int fmt_pmem (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + uint64_t l = gettotalmem(aux) ; + return l ? percent(&p->data, p->rss * getpgsz(aux) * 10000 / l, pos, len) : 0 ; +} + +static int fmt_wchan (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + size_t tmppos = p->data.len ; + if (!s6ps_wchan_lookup(&aux->wchan, &p->data, p->wchan)) return 0 ; + *len = p->data.len - tmppos ; + *pos = tmppos ; + return 1 ; +} + +static int fmt_args (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + if (!stralloc_readyplus(&p->data, (p->height << 2) + (p->cmdlen ? p->cmdlen : p->commlen + (p->data.s[p->state] == 'Z' ? 11 : 3)))) + return 0 ; + *pos = p->data.len ; + if (p->height) + { + unsigned int i = 0 ; + for (; i < 4 * (unsigned int)p->height - 3 ; i++) + p->data.s[p->data.len + i] = ' ' ; + memcpy(p->data.s + p->data.len + 4 * p->height - 3, "\\_ ", 3) ; + p->data.len += p->height << 2 ; + } + if (p->cmdlen) + { + char const *r = p->data.s + p->statlen + p->commlen ; + char *w = p->data.s + p->data.len ; + size_t i = p->cmdlen ; + while (i--) + { + char c = *r++ ; + *w++ = c ? c : ' ' ; + } + p->data.len += p->cmdlen ; + } + else if (p->data.s[p->state] == 'Z') + { + stralloc_catb(&p->data, p->data.s + uint32_fmt(0, p->pid) + 2, p->commlen) ; + stralloc_catb(&p->data, " <defunct>", 10) ; + } + else + stralloc_catb(&p->data, p->data.s + uint32_fmt(0, p->pid) + 1, p->commlen+2) ; + *len = p->data.len - *pos ; + (void)aux ; + return 1 ; +} + +static int fmt_env (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + size_t i = 0 ; + if (!p->envlen) + { + if (!stralloc_catb(&p->data, "*", 1)) return 0 ; + *pos = p->data.len - 1 ; + *len = 1 ; + return 1 ; + } + *pos = p->statlen + p->commlen + p->cmdlen ; + *len = p->envlen ; + for (; i < *len ; i++) + if (!p->data.s[*pos + i]) p->data.s[*pos + i] = ' ' ; + (void)aux ; + return 1 ; +} + +static uint64_t gettotalj (s6ps_auxinfo_t *aux, uint64_t j) +{ + tain totaltime ; + unsigned int hz = gethz(aux) ; + tain_sub(&totaltime, &STAMP, &aux->boottime) ; + j = totaltime.sec.x * hz + totaltime.nano / (1000000000 / hz) - j ; + if (!j) j = 1 ; + return j ; +} + +static int fmt_pcpu (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return percent(&p->data, 10000 * (p->utime + p->stime) / gettotalj(aux, p->start), pos, len) ; +} + +static int fmt_ttime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_jiffies(aux, p, pos, len, p->utime + p->stime) ; +} + +static int fmt_cttime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return fmt_jiffies(aux, p, pos, len, p->utime + p->stime + p->cutime + p->cstime) ; +} + +static int fmt_tstart (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + unsigned int hz = gethz(aux) ; + tain blah = { .sec = { .x = p->start / hz }, .nano = (p->start % hz) * (1000000000 / hz) } ; + if (!stralloc_readyplus(&p->data, TIMESTAMP)) return 0 ; + tain_add(&blah, &aux->boottime, &blah) ; + *pos = p->data.len ; + *len = timestamp_fmt(p->data.s + p->data.len, &blah) ; + p->data.len += *len ; + return 1 ; +} + +static int fmt_cpcpu (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len) +{ + return percent(&p->data, 10000 * (p->utime + p->stime + p->cutime + p->cstime) / gettotalj(aux, p->start), pos, len) ; +} + +static pfieldfmt_func_ref const pfieldfmt_table[PFIELD_PHAIL] = +{ + &fmt_pid, + &fmt_comm, + &fmt_s, + &fmt_ppid, + &fmt_pgrp, + &fmt_session, + &fmt_ttynr, + &fmt_tpgid, + &fmt_utime, + &fmt_stime, + &fmt_cutime, + &fmt_cstime, + &fmt_prio, + &fmt_nice, + &fmt_threads, + &fmt_start, + &fmt_vsize, + &fmt_rss, + &fmt_rsslim, + &fmt_cpuno, + &fmt_rtprio, + &fmt_policy, + &fmt_user, + &fmt_group, + &fmt_pmem, + &fmt_wchan, + &fmt_args, + &fmt_env, + &fmt_pcpu, + &fmt_ttime, + &fmt_cttime, + &fmt_tstart, + &fmt_cpcpu +} ; + +pfieldfmt_func_ref const *const s6ps_pfield_fmt = pfieldfmt_table ; diff --git a/src/libs6ps/s6ps_pwcache.c b/src/libs6ps/s6ps_pwcache.c new file mode 100644 index 0000000..2c29977 --- /dev/null +++ b/src/libs6ps/s6ps_pwcache.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <stdint.h> +#include <pwd.h> +#include <errno.h> + +#include <skalibs/types.h> +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/skamisc.h> +#include <skalibs/avltree.h> + +#include "s6ps.h" +#include "s6ps-internal.h" + +int s6ps_pwcache_lookup (s6ps_cache_t *cache, stralloc *sa, uid_t uid) +{ + dius_t d = { .left = (uint32_t)uid, .right = satmp.len } ; + uint32_t i ; + if (!avltree_search(&cache->tree, &d.left, &i)) + { + struct passwd *pw ; + size_t n = genalloc_len(dius_t, &cache->index) ; + errno = 0 ; + pw = getpwuid(uid) ; + if (!pw) + { + if (errno) return 0 ; + if (!stralloc_readyplus(&satmp, UINT_FMT + 2)) return 0 ; + stralloc_catb(&satmp, "(", 1) ; + satmp.len += uint_fmt(satmp.s + satmp.len, uid) ; + stralloc_catb(&satmp, ")", 2) ; + } + else if (!stralloc_cats(&satmp, pw->pw_name) || !stralloc_0(&satmp)) return 0 ; + if (!genalloc_append(dius_t, &cache->index, &d)) goto err ; + if (!avltree_insert(&cache->tree, n)) + { + genalloc_setlen(dius_t, &cache->index, n) ; + goto err ; + } + i = n ; + } + return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &cache->index)[i].right) ; + err: + satmp.len = d.right ; + return 0 ; +} diff --git a/src/libs6ps/s6ps_statparse.c b/src/libs6ps/s6ps_statparse.c new file mode 100644 index 0000000..8cc5a93 --- /dev/null +++ b/src/libs6ps/s6ps_statparse.c @@ -0,0 +1,186 @@ +/* ISC license. */ + +#include <stdint.h> +#include <sys/types.h> +#include <errno.h> + +#include <skalibs/uint64.h> +#include <skalibs/types.h> +#include <skalibs/stralloc.h> +#include <skalibs/tai.h> + +#include "s6ps.h" + + + /* + going to great lengths to avoid scanf(), but all this code + is still smaller than scanf (no floating point parsing etc.) + */ + +#define STATVARS 49 + +typedef size_t pscan_func (char const *, void *) ; +typedef pscan_func *pscan_func_ref ; + +static size_t f64 (char const *s, void *u64) +{ + uint64_t *u = u64 ; + return uint64_scan(s, u) ; +} + +#define DEFUNU(name, type) \ +static size_t name (char const *s, void *p) \ +{ \ + uint64_t u ; \ + size_t len = uint64_scan(s, &u) ; \ + *(type *)p = u ; \ + return len ; \ +} \ + +#define DEFUNS(name, type) \ +static size_t name (char const *s, void *p) \ +{ \ + int64_t d ; \ + size_t len = int64_scan(s, &d) ; \ + *(type *)p = d ; \ + return len ; \ +} \ + +DEFUNS(fint, int) +DEFUNS(fpid, pid_t) +DEFUNU(fdev, dev_t) + +static pscan_func_ref scanfuncs[STATVARS] = +{ + &fpid, /* ppid */ + &fpid, /* pgrp */ + &fpid, /* session */ + &fdev, /* tty_nr */ + &fpid, /* tpgid */ + &f64, /* flags */ + &f64, /* minflt */ + &f64, /* cminflt */ + &f64, /* majflt */ + &f64, /* cmajflt */ + &f64, /* utime */ + &f64, /* stime */ + &f64, /* cutime */ + &f64, /* cstime */ + &fint, /* priority */ + &fint, /* nice */ + &f64, /* num_threads */ + &f64, /* itrealvalue */ + &f64, /* starttime */ + &f64, /* vsize */ + &f64, /* rss */ + &f64, /* rsslim */ + &f64, /* startcode */ + &f64, /* endcode */ + &f64, /* startstack */ + &f64, /* kstkesp */ + &f64, /* kstkeip */ + &f64, /* signal */ + &f64, /* blocked */ + &f64, /* sigignore */ + &f64, /* sigcatch */ + &f64, /* wchan */ + &f64, /* nswap */ + &f64, /* cnswap */ + &fint, /* exit_signal */ + &f64, /* processor */ + &f64, /* rt_priority */ + &f64, /* policy */ + &f64, /* delayacct_blkio_ticks */ + &f64, /* guest_time */ + &f64, /* cguest_time */ + &f64, /* start_data */ + &f64, /* end_data */ + &f64, /* start_brk */ + &f64, /* arg_start */ + &f64, /* arg_end */ + &f64, /* env_start */ + &f64, /* env_end */ + &fint /* exit_code */ +} ; + +int s6ps_statparse (pscan_t *p) +{ + uint64_t dummy64 ; + int dummyint ; + size_t pos = 0 ; + void *scanresults[STATVARS] = + { + &p->ppid, + &p->pgrp, + &p->session, + &p->ttynr, + &p->tpgid, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &p->utime, + &p->stime, + &p->cutime, + &p->cstime, + &p->prio, + &p->nice, + &p->threads, + &dummy64, + &p->start, + &p->vsize, + &p->rss, + &p->rsslim, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &p->wchan, + &dummy64, + &dummy64, + &dummy64, + &p->cpuno, + &p->rtprio, + &p->policy, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummy64, + &dummyint + } ; + unsigned int i = 0 ; + + if (!p->statlen) return 0 ; + pos = uint64_scan(p->data.s, &dummy64) ; + if (!pos) return 0 ; + if (dummy64 != p->pid) return 0 ; + if (pos + 5 + p->commlen > p->statlen) return 0 ; + if (p->data.s[pos++] != ' ') return 0 ; + if (p->data.s[pos++] != '(') return 0 ; + pos += p->commlen ; + if (p->data.s[pos++] != ')') return 0 ; + if (p->data.s[pos++] != ' ') return 0 ; + p->state = pos++ ; + for (; i < STATVARS ; i++) + { + size_t w ; + if (pos + 1 > p->statlen) return 0 ; + if (p->data.s[pos++] != ' ') return 0 ; + w = (*scanfuncs[i])(p->data.s + pos, scanresults[i]) ; + if (!w) return 0 ; + pos += w ; + } + return 1 ; +} diff --git a/src/libs6ps/s6ps_ttycache.c b/src/libs6ps/s6ps_ttycache.c new file mode 100644 index 0000000..583ab03 --- /dev/null +++ b/src/libs6ps/s6ps_ttycache.c @@ -0,0 +1,118 @@ +/* ISC license. */ + +#include <string.h> +#include <stdint.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> +#include <errno.h> + +#include <skalibs/types.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/buffer.h> +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/djbunix.h> +#include <skalibs/skamisc.h> +#include <skalibs/avltree.h> + +#include "s6ps.h" +#include "s6ps-internal.h" + +static int check (char const *s, dev_t ttynr) +{ + struct stat st ; + if (stat(s, &st) < 0) return 0 ; + return S_ISCHR(st.st_mode) && (st.st_rdev == ttynr) ; +} + + + /* No blind scanning of all /dev or /sys/devices, kthx */ + +static int ttyguess (stralloc *sa, dev_t ttynr) +{ + unsigned int maj = major(ttynr), min = minor(ttynr) ; + + /* Try /dev/tty? and /dev/pts/? */ + if (maj == 4 && min < 64) + { + char tmp[11] = "/dev/tty" ; + tmp[uint_fmt(tmp+8, min)] = 0 ; + if (check(tmp, ttynr)) return stralloc_cats(sa, tmp+5) && stralloc_0(sa) ; + } + else if (maj >= 136 && maj < 144) + { + unsigned int n = ((maj - 136) << 20) | min ; + char tmp[9 + UINT_FMT] = "/dev/pts/" ; + tmp[9 + uint_fmt(tmp+9, n)] = 0 ; + if (check(tmp, ttynr)) return stralloc_cats(sa, tmp+5) && stralloc_0(sa) ; + } + + /* Use /sys/dev/char/maj:min if it exists */ + { + int fd ; + size_t pos = 14 ; + char path[23 + 2 * UINT_FMT] = "/sys/dev/char/" ; + pos += uint_fmt(path + pos, maj) ; + path[pos++] = ':' ; + pos += uint_fmt(path + pos, min) ; + memcpy(path + pos, "/uevent", 8) ; + fd = open_read(path) ; + if (fd >= 0) + { + char buf[4097] ; + buffer b = BUFFER_INIT(&fd_readv, fd, buf, 4097) ; + size_t start = satmp.len ; + int r ; + for (;;) + { + satmp.len = start ; + r = skagetln(&b, &satmp, '\n') ; + if (r <= 0) break ; + if ((satmp.len - start) > 8 && !memcmp(satmp.s + start, "DEVNAME=", 8)) break ; + } + fd_close(fd) ; + if (r > 0) + { + satmp.s[satmp.len - 1] = 0 ; + satmp.len = start ; + memcpy(satmp.s + start + 3, "/dev/", 5) ; + if (check(satmp.s + start + 3, ttynr)) + return stralloc_cats(sa, satmp.s + start + 8) && stralloc_0(sa) ; + } + } + } + + /* Fallback: print explicit maj:min */ + { + size_t pos = 1 ; + char tmp[3 + 2 * UINT_FMT] = "(" ; + pos += uint_fmt(tmp + pos, maj) ; + tmp[pos++] = ':' ; + pos += uint_fmt(tmp + pos, min) ; + tmp[pos++] = ')' ; + tmp[pos++] = 0 ; + return stralloc_catb(sa, tmp, pos) ; + } +} + +int s6ps_ttycache_lookup (s6ps_cache_t *cache, stralloc *sa, dev_t ttynr) +{ + dius_t d = { .left = (uint32_t)ttynr, .right = satmp.len } ; + uint32_t i ; + if (!avltree_search(&cache->tree, &d.left, &i)) + { + size_t n = genalloc_len(dius_t, &cache->index) ; + if (!ttyguess(&satmp, ttynr)) return 0 ; + if (!genalloc_append(dius_t, &cache->index, &d)) goto err ; + if (!avltree_insert(&cache->tree, n)) + { + genalloc_setlen(dius_t, &cache->index, n) ; + goto err ; + } + i = n ; + } + return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &cache->index)[i].right) ; + err: + satmp.len = d.right ; + return 0 ; +} diff --git a/src/libs6ps/s6ps_wchan.c b/src/libs6ps/s6ps_wchan.c new file mode 100644 index 0000000..c7c4348 --- /dev/null +++ b/src/libs6ps/s6ps_wchan.c @@ -0,0 +1,93 @@ +/* ISC license. */ + +#include <string.h> +#include <sys/utsname.h> + +#include <skalibs/uint64.h> +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/djbunix.h> + +#include "s6ps.h" + +int s6ps_wchan_init (s6ps_wchan_t *w, char const *file) +{ + if (file) + { + if (!openslurpclose(&w->sysmap, file)) return 0 ; + } + else + { + char *files[3] = { "/proc/kallsyms", 0, "/boot/System.map" } ; + struct utsname uts ; + size_t n ; + if (uname(&uts) < 0) return 0 ; + n = strlen(uts.release) ; + { + char buf[18 + n] ; + unsigned int i = 0 ; + memcpy(buf, "/boot/System.map", 16) ; + buf[16] = '-' ; + memcpy(buf + 17, uts.release, n + 1) ; + files[1] = buf ; + for (; i < 3 ; i++) + if (openslurpclose(&w->sysmap, files[i])) break ; + if (i >= 3) return 0 ; + } + } + { + size_t i = 0 ; + if (!genalloc_append(size_t, &w->ind, &i)) goto err2 ; + for (i = 1 ; i <= w->sysmap.len ; i++) + if (w->sysmap.s[i-1] == '\n') + if (!genalloc_append(size_t, &w->ind, &i)) goto err ; + } + return 1 ; + err: + genalloc_free(size_t, &w->ind) ; + err2: + stralloc_free(&w->sysmap) ; + return 0 ; +} + +void s6ps_wchan_finish (s6ps_wchan_t *w) +{ + genalloc_free(size_t, &w->ind) ; + stralloc_free(&w->sysmap) ; +} + +static inline size_t lookup (s6ps_wchan_t const *w, uint64_t addr, size_t *i) +{ + size_t low = 0, mid, high = genalloc_len(size_t, &w->ind), len ; + for (;;) + { + uint64_t cur ; + mid = (low + high) >> 1 ; + len = uint64_xscan(w->sysmap.s + genalloc_s(size_t, &w->ind)[mid], &cur) ; + if (!len) return 0 ; + if (cur == addr) break ; + if (mid == low) return 0 ; + if (addr < cur) high = mid ; else low = mid ; + } + *i = mid ; + return len ; +} + +int s6ps_wchan_lookup (s6ps_wchan_t const *w, stralloc *sa, uint64_t addr) +{ + if (addr == (sizeof(void *) == 8 ? 0xffffffffffffffffULL : 0xffffffffUL)) + return stralloc_catb(sa, "*", 1) ; + if (!addr) return stralloc_catb(sa, "-", 1) ; + if (w->sysmap.len) + { + size_t i, pos, len = lookup(w, addr, &i) ; + if (!len) return stralloc_catb(sa, "?", 1) ; + pos = genalloc_s(size_t, &w->ind)[i] + len + 3 ; + return stralloc_catb(sa, w->sysmap.s + pos, genalloc_s(size_t, &w->ind)[i+1] - 1 - pos) ; + } + if (!stralloc_readyplus(sa, UINT64_FMT + 3)) return 0 ; + stralloc_catb(sa, "(0x", 3) ; + sa->len += uint64_fmt(sa->s + sa->len, addr) ; + stralloc_catb(sa, ")", 1) ; + return 1 ; +} |