From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id +IqKEE6Ao19jAgAA0tVLHw (envelope-from ) for ; Thu, 05 Nov 2020 04:32:14 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id MDJVDE6Ao1+WfQAAB5/wlQ (envelope-from ) for ; Thu, 05 Nov 2020 04:32:14 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 939DB9401D0 for ; Thu, 5 Nov 2020 04:32:13 +0000 (UTC) Received: from localhost ([::1]:37508 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kaWwU-0003gL-To for larch@yhetil.org; Wed, 04 Nov 2020 23:32:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40896) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kaWwM-0003er-R3 for guix-patches@gnu.org; Wed, 04 Nov 2020 23:32:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:39769) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kaWwM-0002Kd-H3 for guix-patches@gnu.org; Wed, 04 Nov 2020 23:32:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kaWwM-0001mf-Ds for guix-patches@gnu.org; Wed, 04 Nov 2020 23:32:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#44460] [PATCH] processes: Optionally normalize recutils output. Resent-From: John Soo Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 05 Nov 2020 04:32:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 44460 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 44460@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16045506766795 (code B ref -1); Thu, 05 Nov 2020 04:32:02 +0000 Received: (at submit) by debbugs.gnu.org; 5 Nov 2020 04:31:16 +0000 Received: from localhost ([127.0.0.1]:51315 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kaWvb-0001lW-B4 for submit@debbugs.gnu.org; Wed, 04 Nov 2020 23:31:16 -0500 Received: from lists.gnu.org ([209.51.188.17]:53536) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kaWvY-0001lO-QP for submit@debbugs.gnu.org; Wed, 04 Nov 2020 23:31:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40760) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kaWvY-0003Gb-Eq for guix-patches@gnu.org; Wed, 04 Nov 2020 23:31:12 -0500 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]:40254) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kaWvT-0001yL-K0 for guix-patches@gnu.org; Wed, 04 Nov 2020 23:31:12 -0500 Received: by mail-pg1-x52f.google.com with SMTP id x13so410776pgp.7 for ; Wed, 04 Nov 2020 20:31:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=asu-edu.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version; bh=T+WyXqXVWzERGBPEyTgwODzuz6p8KyXmVE8A5B588+I=; b=webrDd4j1rd3Zws7QoKYXB5QQ0j/ThyJMI6RVmnIGgSyuBWTNNhPCRMHKCfj60Tk+O fVUnzadnfN3xCX6ANjQK6cNYwK4Sapc35nbaLXm6G7gFBNP4ozU0tU1ttDlUXNIBHexc PE7Uc9CSkm2sqygG4XcRrnXeJIAvchwezUiXHsGBNhtDIN9XGZSQnxBayxMAhwf2viXU vPvx7dPjBGFhL/uBZ3Bi1L+u6DbdYkScqj3y2KU7kXEiNFvyzxtNtPWM088fBSYSUGKG Dy+IdzbRiaz6dfvOliV2gNtNJgJAP52IVdRKX+LDrDtbYZPPB5wfsbs++LVZE8ATwrSF /4tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=T+WyXqXVWzERGBPEyTgwODzuz6p8KyXmVE8A5B588+I=; b=Y3miVb7hMkl3jXczfou1wPeBQ3SHiq/+Jg0smKAXhKMcNWNlHI2SkMts/Qhmm+3m0r 5J9eqYh9UXY1AUHuyZFNYh+uRVpiJgrkns7ak0iXpLIe9pLeze9b09mixLSYRAxQ5zgp C4nkIZJeUKAuj+ADnmD4uwkHrcaPArdUtE3ynh6berphd3K6Lwg02ahyfq7Rbq9u8+4u ChXMMtX//RiGMSAhXkGj2fNFbDXKu1BFawvu5u3xmuCTKIx1vQ94OiUCOLD5YnYKZDHK +NVFgJyeqp3AE679D1VBmf1wfGOap+1EN/eyswYeimX61HF3mJjRrQIPJMYGzM60dcXj SK1A== X-Gm-Message-State: AOAM532L0SuA2WilvaCuGa9uE72jBN5Xk9CPXxB1l7k3HjlJ4ZdFlRZV 4bUyH6h5e3WRe2erEnZlNxJd/lIezMx9ng== X-Google-Smtp-Source: ABdhPJzHmbvTr6YuQKwWh1RPejdEyq8XLh2VzP8CI1AlWxCz3uv9400aXbmDDBS2CLo6DhMsmeJ3nQ== X-Received: by 2002:a62:834a:0:b029:18a:e0b1:f0d9 with SMTP id h71-20020a62834a0000b029018ae0b1f0d9mr606811pfe.73.1604550663848; Wed, 04 Nov 2020 20:31:03 -0800 (PST) Received: from ecenter ([2600:1700:83b0:8bd0::7a8]) by smtp.gmail.com with ESMTPSA id c127sm471694pfc.115.2020.11.04.20.31.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 04 Nov 2020 20:31:03 -0800 (PST) From: John Soo Date: Wed, 04 Nov 2020 20:31:01 -0800 Message-ID: <87o8kcv30a.fsf@asu.edu> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::52f; envelope-from=jsoo1@asu.edu; helo=mail-pg1-x52f.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -2.3 (--) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Scanner: ns3122888.ip-94-23-21.eu Authentication-Results: aspmx1.migadu.com; dkim=fail (body hash did not verify) header.d=asu-edu.20150623.gappssmtp.com header.s=20150623 header.b=webrDd4j; dmarc=fail reason="SPF not aligned (relaxed)" header.from=asu.edu (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: 0.09 X-TUID: /WwJXKr28agi --=-=-= Content-Type: text/plain Hi Guix, Please let me know if there is already a way to do what I want without this patch :). I really don't know a whole lot of recutils beyond what I learned this morning. I was trying to extract PIDs of the child processes of sessions from the output of guix processes. I ran into the following problem: the PID and the command are on the same line. ---- Begin ---- $ guix processes SessionPID: 4278 ClientPID: 4268 ClientCommand: /gnu/store/... ChildProcess: 4435: /gnu/store/... ChildProcess: 4554: /gnu/store/... ChildProcess: 4646: guile --no-auto-compile ... ---- End ---- I ended up having to use sed to remove the trailing command for the child process. After reading the documentation for recutils I found out that records can also be expressed in separate record sets that can be joined together. This patch adds a --normalize flag that specifies the recutils output be put in separate record sets for Locks, Sessions, and ChildProcesses. For example: ---- Begin ---- PAGER=cat ./pre-inst-env guix processes --normalize %rec: Session %type: PID int %type: ClientPID int %key: PID PID: 4278 ClientPID: 4268 ClientCommand: /gnu/store/... %rec: Lock %type: Session rec Session %rec: ChildProcess %type: Session rec Session %type: PID int %key: PID Session: 4278 PID: 4435 Command: /gnu/store/... Session: 4278 PID: 4554 Command: /gnu/store/... Session: 4278 PID: 4646 Command: guile --no-auto-compile ... ---- End ---- What do you think? John --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-processes-Optionally-normalize-recutils-output.patch >From 9c04e2d184977c95090ec237a6bc0334ee14a8e7 Mon Sep 17 00:00:00 2001 From: John Soo Date: Wed, 4 Nov 2020 07:51:52 -0800 Subject: [PATCH] processes: Optionally normalize recutils output. * guix/scripts/processes.scm: Add "normalize" flag --- doc/guix.texi | 26 ++++++++ guix/scripts/processes.scm | 121 +++++++++++++++++++++++++++++++++---- 2 files changed, 135 insertions(+), 12 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 5e3e0435b4..ed54c26072 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -12691,6 +12691,32 @@ ClientPID: 19419 ClientCommand: cuirass --cache-directory /var/cache/cuirass @dots{} @end example +Additional options are listed below. + +@table @code +@item --normalize +Normalize the output records into record sets (@pxref{Record Sets,,, +recutils, GNU recutils manual}). Normalizing into record sets allows +joins across record types. + +@example +$ guix processes --normalize | \ + recsel \ + -j Session \ + -t ChildProcess \ + -p Session.PID,PID \ + -e 'Session.ClientCommand ~ build' +Session_PID: 4278 +PID: 4435 + +Session_PID: 4278 +PID: 4554 + +Session_PID: 4278 +PID: 4646 +@end example +@end table + @node System Configuration @chapter System Configuration diff --git a/guix/scripts/processes.scm b/guix/scripts/processes.scm index b4ca7b1687..11cfd561b9 100644 --- a/guix/scripts/processes.scm +++ b/guix/scripts/processes.scm @@ -176,6 +176,9 @@ active sessions, and the master 'guix-daemon' process." (values (filter-map child-process->session children) master))) +(define (lock->record lock port) + (format port "LockHeld: ~a~%" lock)) + (define (daemon-session->recutils session port) "Display SESSION information in recutils format on PORT." (format port "SessionPID: ~a~%" @@ -184,8 +187,7 @@ active sessions, and the master 'guix-daemon' process." (process-id (daemon-session-client session))) (format port "ClientCommand:~{ ~a~}~%" (process-command (daemon-session-client session))) - (for-each (lambda (lock) - (format port "LockHeld: ~a~%" lock)) + (for-each (lambda (lock) (lock->record lock port)) (daemon-session-locks-held session)) (for-each (lambda (process) (format port "ChildProcess: ~a:~{ ~a~}~%" @@ -193,6 +195,98 @@ active sessions, and the master 'guix-daemon' process." (process-command process))) (daemon-session-children session))) +(define (format-single-record port) + "Display denormalized session information to PORT." + (for-each (lambda (session) + (daemon-session->recutils session port) + (newline port)) + (daemon-sessions))) + +(define session-rec-type + ;; Also includes ClientCommand but it doesn't seem to be possible to express + ;; a plain string field (the default) without further restrictions + "%rec: Session +%type: PID int +%type: ClientPID int +%key: PID") + +(define lock-rec-type + ;; Also includes LockHeld but it doesn't seem to be possible to express + ;; a plain string field (the default) without further restrictions + "%rec: Lock +%type: Session rec Session") + +(define child-process-rec-type + ;; Also includes Command but it doesn't seem to be possible to + ;; express a plain string field (the default) without further restrictions + "%rec: ChildProcess +%type: Session rec Session +%type: PID int +%key: PID") + +(define (session-pid-key->field session port) + "Display SESSION PID as field on PORT." + (format + port "Session: ~a" + (process-id (daemon-session-process session)))) + +(define (session-scalars->normalized-record session port) + "Display SESSION scalar fields to PORT in normalized form." + (format port "PID: ~a~%" + (process-id (daemon-session-process session))) + (format port "ClientPID: ~a~%" + (process-id (daemon-session-client session))) + (format port "ClientCommand:~{ ~a~}~%" + (process-command (daemon-session-client session)))) + +(define (child-process->normalized-record process port) + "Display PROCESS record on PORT in normalized form" + (format port "PID: ~a" (process-id process)) + (newline port) + (format port "Command:~{ ~a~}" (process-command process))) + +(define (format-normalized port) + (define sessions (daemon-sessions)) + + (format port session-rec-type) + (newline port) + (newline port) + (for-each + (lambda (session) + (session-scalars->normalized-record session port)) + sessions) + (newline port) + + (format port lock-rec-type) + (newline port) + (newline port) + (for-each + (lambda (session) + (for-each + (lambda (lock) + (session-pid-key->field session port) + (newline port) + (lock->record lock port) + (newline port) + (newline port)) + (daemon-session-locks-held session))) + sessions) + + (format port child-process-rec-type) + (newline port) + (newline port) + (for-each + (lambda (session) + (for-each + (lambda (process) + (session-pid-key->field session port) + (newline port) + (child-process->normalized-record process port) + (newline port) + (newline port)) + (daemon-session-children session))) + sessions)) + ;;; ;;; Options. @@ -205,7 +299,10 @@ active sessions, and the master 'guix-daemon' process." (exit 0))) (option '(#\V "version") #f #f (lambda args - (show-version-and-exit "guix processes"))))) + (show-version-and-exit "guix processes"))) + (option '("normalize") #f #f + (lambda (opt name arg result) + (alist-cons 'normalize #t result))))) (define (show-help) (display (G_ "Usage: guix processes @@ -216,8 +313,13 @@ List the current Guix sessions and their processes.")) (display (G_ " -V, --version display version information and exit")) (newline) + (display (G_ " + --normalize display results as normalized record sets")) + (newline) (show-bug-report-information)) +(define %default-options '()) + ;;; ;;; Entry point. @@ -227,17 +329,12 @@ List the current Guix sessions and their processes.")) (category plumbing) (synopsis "list currently running sessions") (define options - (args-fold* args %options - (lambda (opt name arg result) - (leave (G_ "~A: unrecognized option~%") name)) - cons - '())) + (parse-command-line args %options (list %default-options))) (with-paginated-output-port port - (for-each (lambda (session) - (daemon-session->recutils session port) - (newline port)) - (daemon-sessions)) + (match (assoc-ref options 'normalize) + (#t (format-normalized port)) + (_ (format-single-record port))) ;; Pass 'R' (instead of 'r') so 'less' correctly estimates line length. #:less-options "FRX")) -- 2.29.1 --=-=-=--