From owner-freebsd-hackers@freebsd.org Fri Nov 23 18:42:05 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D779E114EADD for ; Fri, 23 Nov 2018 18:42:04 +0000 (UTC) (envelope-from bch@online.de) Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "mout.kundenserver.de", Issuer "TeleSec ServerPass DE-2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id A703D7017B for ; Fri, 23 Nov 2018 18:42:03 +0000 (UTC) (envelope-from bch@online.de) Received: from online.de ([195.201.29.72]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MxV8b-1fTDYo1NEX-00xu8I for ; Fri, 23 Nov 2018 19:41:55 +0100 Received: by online.de (Postfix, from userid 1000) id 7F859587CA; Fri, 23 Nov 2018 19:41:54 +0100 (CET) From: Christian Barthel To: freebsd-hackers@freebsd.org Subject: top(1): adding grep like selection and system processes Date: Fri, 23 Nov 2018 19:41:54 +0100 Message-ID: <878t1j1vrh.fsf@x230.onfire.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (berkeley-unix) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Provags-ID: V03:K1:19Vb0lvzPUuwVP2zVjVU7rf6KdWKONg+VGkGKdMekZM9LP4KJjc V3v3ELuhtOrs5Qo92B/o7KKOYtkJCxpSpE0CjzVNZ7cDRys0037XfS5VV/feBcahPA9+X9S SsY/1I6FzyUZJ48vAjkY0tGxd3F8jd2OtZJW3+gU2oNxi8PWB3FBcNCpyzXS19cRLhwO1dS er6+h0+2hc316Af1k+6Gw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:LX0COE2/tA8=:Mkmv2IJ+eGBkuGpdMyspcb 0aW9vxwnbIwJNxQctJj4GeTmjS6H3aWT8Eolh43Jz6fMB1GGPzrQjiO0Pi4KMOg6pqSV+4uEK k4DXUXX0rXXIb8Dy4aIGb5w6Bh7ZI9DVPDwFYPAQm0SLJRZV3sWXT/XVvgi1J/WQxCC7YtbXH m4oWErCnYPYP8G48nyWokTF+q1JHDrw65GBGDxuMWx0MPClWYBojAfqurD4PdKcEn8cq4Bf64 NAvgZLtpvB4lfIxVOi3YrV+sXTAhsbQAIuMA8ECW18AatgMm68XcSOhHUyNCVcexaZDS/3oLP 6jeqpzULgdgA9SWKUFDVDWMqta6/Q62m3n30Cazpr8Hqbs0hEZgQERDH37z10+dOJuGzMvQFI qds7BJxcFO03GbI9RD33Nd+I8aPLueoJxhXui0yP2Afsqth2u2SdTAdoezTp+7QzGFh7T8H4w aKd8bVUcawrrzEXsSmn2st3pUwIknK8NYqHCNRICGPHqMZN1Db/VPcF3pD0ZzneuInFBgW35S bdTusvQJ8RPTn4qdB/PMIT7DtF8hfi93AwOpt7N7OatVDHd/8Mp1m60gD8A0OMSZExYBbihbx iZbD0RvdaETkug6bKRRtCHy7141paX8YJc9TIT9iifUOJyzhRMs4A76ol9hVMGdBUYqvUYZWG QzKZ+t/rkePXoJSiNGQm27lIufmIQ0HMOS+WMoXqLTVxwxti/EsODiTspzxPLkbN3hiZhm9rC fp2edkLuq+po0HwUIdh/hoYa6bNjgtATyvSHjmmAvulho8C2p2gtgFgBTyKz9yhPZH91P+45c 8CHuzpA X-Rspamd-Queue-Id: A703D7017B X-Spamd-Result: default: False [1.51 / 15.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-0.66)[-0.655,0]; HAS_ATTACHMENT(0.00)[]; MIME_GOOD(-0.10)[multipart/mixed,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; AUTH_NA(1.00)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_NONE(0.00)[]; NEURAL_SPAM_SHORT(0.87)[0.866,0]; MX_GOOD(-0.01)[mx00.emig.kundenserver.de,mx01.emig.kundenserver.de]; RCVD_IN_DNSWL_NONE(0.00)[130.126.227.212.list.dnswl.org : 127.0.5.0]; NEURAL_SPAM_MEDIUM(0.34)[0.341,0]; R_SPF_NA(0.00)[]; DMARC_NA(0.00)[online.de]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; RCVD_TLS_LAST(0.00)[]; ASN(0.00)[asn:8560, ipnet:212.227.0.0/16, country:DE]; IP_SCORE(-0.03)[ipnet: 212.227.0.0/16(-0.30), asn: 8560(0.15), country: DE(-0.01)]; MIME_UNKNOWN(0.10)[text/x-diff] X-Rspamd-Server: mx1.freebsd.org X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Nov 2018 18:42:05 -0000 --=-=-= Content-Type: text/plain Hello, I've added two features that I (personally) found useful in top(1): A grep-like feature to filter command names in top(1) (inspired by the OpenBSD top version). One example may be to filter out all current virtual machines by using "top -g bhyve" or interactively with "g bhyve". (there was already a selection field ps.command; so, perhaps someone is already working on a similar feature?). It is not possible to filter with a regular expression. The second flag is selection of system processes with -K or interactively toggle between user and system processes. I want to ask if this is of general interest for FreeBSD users or if its useful for someone else here? I've tested it on FreeBSD 13.0-CURRENT. Christian --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=top-grep-sysproc.patch Content-Description: top(1) patch to add grep-like feature and system process selection. diff --git a/usr.bin/top/commands.c b/usr.bin/top/commands.c index 88f4b0867d4..082f5fcc9af 100644 --- a/usr.bin/top/commands.c +++ b/usr.bin/top/commands.c @@ -59,10 +59,12 @@ const struct command all_commands[] = {'H', "toggle the displaying of threads", false, CMD_thrtog}, {'h', "show this help text", true, CMD_help}, {'?', NULL, true, CMD_help}, + {'g', "grep command name", false, CMD_grep}, {'i', "toggle the displaying of idle processes", false, CMD_idletog}, {'I', NULL, false, CMD_idletog}, {'j', "toggle the displaying of jail ID", false, CMD_jidtog}, {'J', "display processes for only one jail (+ selects all jails)", false, CMD_jail}, + {'K', "toggle between system and user processes", false, CMD_kern}, {'k', "kill processes; send a signal to a list of processes", false, CMD_kill}, {'q', "quit" , true, CMD_quit}, {'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog}, diff --git a/usr.bin/top/commands.h b/usr.bin/top/commands.h index 0071fbe62fc..75fe9756a56 100644 --- a/usr.bin/top/commands.h +++ b/usr.bin/top/commands.h @@ -46,6 +46,8 @@ enum cmd_id { CMD_order, CMD_pid , CMD_toggletid, + CMD_grep, + CMD_kern, }; struct command { diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 374c9da0edf..2fb5b5221b7 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -803,6 +803,13 @@ get_process_info(struct system_info *si, struct process_select *sel, /* skip self */ continue; + if (sel->command && strncmp(pp->ki_comm, sel->command, COMMLEN)) + /* skip not matching commands */ + continue; + + if (!sel->user && !(pp->ki_flag & P_SYSTEM) && sel->pid == -1) + continue; + if (!sel->system && (pp->ki_flag & P_SYSTEM) && sel->pid == -1) /* skip system process */ continue; diff --git a/usr.bin/top/machine.h b/usr.bin/top/machine.h index c3c7777d910..40a36e947b9 100644 --- a/usr.bin/top/machine.h +++ b/usr.bin/top/machine.h @@ -63,6 +63,7 @@ struct process_select bool idle; /* show idle processes */ bool self; /* show self */ bool system; /* show system processes */ + bool user; /* show user processes */ bool thread; /* show threads */ bool thread_id; /* show thread ids */ #define TOP_MAX_UIDS 8 @@ -73,7 +74,7 @@ struct process_select bool swap; /* show swap usage */ bool kidle; /* show per-CPU idle threads */ int pid; /* only this pid (unless pid == -1) */ - const char *command; /* only this command (unless == NULL) */ + char *command; /* only this command (unless == NULL) */ }; /* routines defined by the machine dependent module */ diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c index 2279479409b..3a30bf40d9a 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -271,6 +271,7 @@ main(int argc, char *argv[]) ps.idle = true; ps.self = true; ps.system = false; + ps.user = true; reset_uids(); ps.thread = false; ps.wcpu = 1; @@ -305,14 +306,20 @@ main(int argc, char *argv[]) optind = 1; } - while ((i = getopt_long(ac, av, "CSIHPabijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF) + while ((i = getopt_long(ac, av, "CSIHKPabg:ijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF) { switch(i) { case 'v': /* show version number */ errx(0, "version FreeBSD"); break; - + case 'g': + ps.command = strdup(optarg); + break; + case 'K': + ps.user = false; + ps.system = true; + break; case 'u': /* toggle uid/username display */ do_unames = !do_unames; break; @@ -870,7 +877,15 @@ main(int argc, char *argv[]) topn = newval; } break; - + case CMD_kern: + if (ps.user == false) { + ps.user = true; + ps.system = false; + } else { + ps.user = false; + ps.system = true; + } + break; case CMD_delay: /* new seconds delay */ new_message(MT_standout, "Seconds to delay: "); if ((i = readline(tempbuf1, 8, true)) > -1) @@ -1027,7 +1042,19 @@ main(int argc, char *argv[]) reset_display(); putchar('\r'); break; + case CMD_grep: + new_message(MT_standout, + "command name: "); + if (readline(tempbuf2, sizeof(tempbuf2), false) > 0) { + if (ps.command != NULL) + free(ps.command); + if (tempbuf2[0] == '+' && tempbuf2[1] == '\0') + ps.command = NULL; + else + ps.command = strdup(tempbuf2); + } + break; case CMD_jail: new_message(MT_standout, "Jail to show (+ for all): "); --=-=-=--