From 0ee090ae16a3a98ba64a1bb8108597328a4c05b0 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Sat, 15 Aug 2015 17:10:38 +1000 Subject: [PATCH] ps: display control group name The cgroup field while shown as a vector is a concatenated string, so alot of the complexity of sorting and displaying has gone. This change simplifies the cgroup sorting and adds display and sorting for the name attribute of the cgroup, if found. Signed-off-by: Craig Small --- NEWS | 1 + ps/output.c | 47 ++++++++++++++++++++++++++++++++++++++++------- ps/ps.1 | 6 +++++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 50c7b7ae..b30aedf3 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ procps-ng-NEXT ---------------- * ps: sort by cgroup Debian #692279 + * ps: display control group name with -o cgname procps-ng-3.3.11 ---------------- diff --git a/ps/output.c b/ps/output.c index 35286c0b..4722fee5 100644 --- a/ps/output.c +++ b/ps/output.c @@ -98,13 +98,29 @@ static int sr_nop(const proc_t* a, const proc_t* b){ static int sr_cgroup(const proc_t* a, const proc_t* b) { - int i; - int cmpval; - for (i=0; a->cgroup[i] != NULL && b->cgroup[i] != NULL; i++) { - if ((cmpval = strcmp(a->cgroup[i], b->cgroup[i])) != 0) - return cmpval; - } - return 0; + /* This is a "vector" of one */ + if (*a->cgroup == NULL || *b->cgroup == NULL) + return 0; + return strcmp(*a->cgroup, *b->cgroup); +} + +static int sr_cgname(const proc_t* a, const proc_t* b) +{ + char *aname, *bname; + /* This is a "vector" of one */ + if (*a->cgroup == NULL || *b->cgroup == NULL) + return 0; + aname = strstr(*a->cgroup, ":name="); + bname = strstr(*b->cgroup, ":name="); + /* check for missing names, they win */ + if (aname == NULL || aname[6] == '\0') { + if (bname == NULL || bname[6] == '\0') + return 0; + return -1; + } else if (bname == NULL || bname[6] == '\0') + return 1; + return strcmp(aname+6,bname+6); + } @@ -455,6 +471,22 @@ static int pr_cgroup(char *restrict const outbuf,const proc_t *restrict const pp return max_rightward-rightward; } +static int pr_cgname(char *restrict const outbuf,const proc_t *restrict const pp) { + int rightward = max_rightward; + int i; + char *name; + + if ((name = strstr(*pp->cgroup, ":name=")) != NULL) { + name += 6; + if (name != '\0') { + escape_str(outbuf, name, OUTBUF_SIZE, &rightward); + return max_rightward - rightward; + } + } + /* fallback: use full cgroup for name */ + return pr_cgroup(outbuf, pp); +} + /* Non-standard, from SunOS 5 */ static int pr_fname(char *restrict const outbuf, const proc_t *restrict const pp){ char *endp = outbuf; @@ -1451,6 +1483,7 @@ static const format_struct format_array[] = { {"bsdtime", "TIME", pr_bsdtime, sr_nop, 6, 0, LNX, ET|RIGHT}, {"c", "C", pr_c, sr_pcpu, 2, 0, SUN, ET|RIGHT}, {"caught", "CAUGHT", pr_sigcatch, sr_nop, 9, 0, BSD, TO|SIGNAL}, /*sigcatch*/ +{"cgname", "CGNAME", pr_cgname, sr_cgname, 27,CGRP, LNX, PO|UNLIMITED}, {"cgroup", "CGROUP", pr_cgroup, sr_cgroup, 27,CGRP, LNX, PO|UNLIMITED}, {"class", "CLS", pr_class, sr_sched, 3, 0, XXX, TO|LEFT}, {"cls", "CLS", pr_class, sr_sched, 3, 0, HPU, TO|RIGHT}, /*says HPUX or RT*/ diff --git a/ps/ps.1 b/ps/ps.1 index bce55bd9..ee9c38f0 100644 --- a/ps/ps.1 +++ b/ps/ps.1 @@ -4,7 +4,7 @@ .\" Quick hack conversion by Albert Cahalan, 1998. .\" Licensed under version 2 of the Gnu General Public License. .\" -.TH PS 1 "June 2015" "procps-ng" "User Commands" +.TH PS 1 "August 2015" "procps-ng" "User Commands" .\" .\" To render this page: .\" groff -t -b -man -X -P-resolution -P100 -Tps ps.1 & @@ -1080,6 +1080,10 @@ format is displayed. (alias .BR sig_catch , \ sigcatch ). T} +cgname CGNAME T{ +display name of control groups to which the process belongs. +T} + cgroup CGROUP T{ display control groups to which the process belongs. T} -- 2.40.0