Thread: backend now show status in 'ps'

backend now show status in 'ps'

From
Bruce Momjian
Date:
I have completed a patch that shows the backend status on the 'ps' line
for each process:

24081  ./bin/postmaster -i -B 401 -d -o -F -S 1024
24089  /usr/local/postgres/./bin/postgres postgres test idle "" "" "" (postmaster)
24106  /usr/local/postgres/./bin/postgres postgres test SELECT "" "" "" (postmaster)

As you can see, the backend shows the user, database, and status, which
is either 'idle' or 'SELECT', 'UPDATE', 'VACUUM', etc.  Those "" are
there because I erased the other args.  "(postmaster)" is there because
that was the initial argv[0] value (we don't fork() anymore).

This will be useful, even if we go with further status features.  I am
interested in any other information I should be showing.  I believe
there is almost zero performance overhead in assigning/showing these
values, except that the strings should be valid during the entire time
it is assigned to argv.  We could almost display the row number as we
scan through a table.  Nifty feature.

This worked under BSDI, because if you say argv[1] = "test", and argc is
at least 2, it shows "test" in ps.  If argc is only one (they didn't use
any args), it will not show it, but I have added a nifty hack to the
postmaster to re-exec it so it is sure to have a least three args.  I
strip them off before processing.  You can see the patch in the patches
list.

BSDI uses the kvm interface for ps, which allows 'ps' to grab the args
right out of the process's address space.  This is a nifty trick,
considering that 'ps' is run inside the address space of another
process.  I did not use the sendmail wack-the-environment method of
changing 'ps'-displayed args, because it is ugly code, and will probably
cause more problems than it is worth.  Hopefully most platforms will
allow this kind of assignment to be shown in 'ps'.

I have also removed some unused args to pg_exec_query().  Again, it is
in the patch posted.

--
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)

Re: [HACKERS] backend now show status in 'ps'

From
Massimo Dal Zotto
Date:
>
> I have completed a patch that shows the backend status on the 'ps' line
> for each process:
>
> 24081  ./bin/postmaster -i -B 401 -d -o -F -S 1024
> 24089  /usr/local/postgres/./bin/postgres postgres test idle "" "" "" (postmaster)
> 24106  /usr/local/postgres/./bin/postgres postgres test SELECT "" "" "" (postmaster)
>
> As you can see, the backend shows the user, database, and status, which
> is either 'idle' or 'SELECT', 'UPDATE', 'VACUUM', etc.  Those "" are
> there because I erased the other args.  "(postmaster)" is there because
> that was the initial argv[0] value (we don't fork() anymore).
>
> This will be useful, even if we go with further status features.  I am
> interested in any other information I should be showing.  I believe
> there is almost zero performance overhead in assigning/showing these
> values, except that the strings should be valid during the entire time
> it is assigned to argv.  We could almost display the row number as we
> scan through a table.  Nifty feature.

I agree.

> This worked under BSDI, because if you say argv[1] = "test", and argc is
> at least 2, it shows "test" in ps.  If argc is only one (they didn't use
> any args), it will not show it, but I have added a nifty hack to the
> postmaster to re-exec it so it is sure to have a least three args.  I
> strip them off before processing.  You can see the patch in the patches
> list.

I believe, this won't work under Linux. I'm not 100% sure about it but from
what I can remember Linux pass a copy of the original argv to the program
and changing it doesn't change the argv strings shown by ps. You must zap
the strings itself inside the page allocated for argv.
I would suggest the following code which works fine also under linux, even
with zero args.

#ifdef linux
    progname = argv[0];
    /* Fill the argv buffer vith 0's, once during the initialization */
    for (i=0; i<argc; argc++) {
        memset(argv[i], 0, strlen(argv[i]));
    }
#endif

    /* Build status info */
    sprintf(status, "%s ...", ...);
#ifdef bsdi
    argv[1] = status;
#endif
#ifdef linux
    /* Print the original argv[0] + status info in the argv buffer */
    sprintf(argv[0], "%s %s", progname, status);
#endif

I would also suggest using only lowercase messages if possible. They don't
hurt the eyes too much.

> BSDI uses the kvm interface for ps, which allows 'ps' to grab the args
> right out of the process's address space.  This is a nifty trick,
> considering that 'ps' is run inside the address space of another
> process.  I did not use the sendmail wack-the-environment method of
> changing 'ps'-displayed args, because it is ugly code, and will probably
> cause more problems than it is worth.  Hopefully most platforms will
> allow this kind of assignment to be shown in 'ps'.
>
> I have also removed some unused args to pg_exec_query().  Again, it is
> in the patch posted.
>
> --
> Bruce Momjian                          |  830 Blythe Avenue
> maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
>   +  If your life is a hard drive,     |  (610) 353-9879(w)
>   +  Christ can be your backup.        |  (610) 853-3000(h)

--
Massimo Dal Zotto

+----------------------------------------------------------------------+
|  Massimo Dal Zotto                e-mail:  dz@cs.unitn.it            |
|  Via Marconi, 141                 phone:  ++39-461-534251            |
|  38057 Pergine Valsugana (TN)     www:  http://www.cs.unitn.it/~dz/  |
|  Italy                            pgp:  finger dz@tango.cs.unitn.it  |
+----------------------------------------------------------------------+

Re: [HACKERS] backend now show status in 'ps'

From
Oleg Broytmann
Date:
Hello!

On Mon, 8 Jun 1998, Massimo Dal Zotto wrote:
> > This worked under BSDI, because if you say argv[1] = "test", and argc is
> > at least 2, it shows "test" in ps.  If argc is only one (they didn't use
> > any args), it will not show it, but I have added a nifty hack to the
> > postmaster to re-exec it so it is sure to have a least three args.  I
> > strip them off before processing.  You can see the patch in the patches
> > list.
>
> I believe, this won't work under Linux. I'm not 100% sure about it but from
> what I can remember Linux pass a copy of the original argv to the program
> and changing it doesn't change the argv strings shown by ps. You must zap
> the strings itself inside the page allocated for argv.
> I would suggest the following code which works fine also under linux, even
> with zero args.

   AFAIK the only "portable" way is to steal copy from well-known sendmail
hacks...

Oleg.
----
  Oleg Broytmann     http://members.tripod.com/~phd2/     phd2@earthling.net
           Programmers don't die, they just GOSUB without RETURN.


Re: [HACKERS] backend now show status in 'ps'

From
Bruce Momjian
Date:
> I believe, this won't work under Linux. I'm not 100% sure about it but from
> what I can remember Linux pass a copy of the original argv to the program
> and changing it doesn't change the argv strings shown by ps. You must zap
> the strings itself inside the page allocated for argv.
> I would suggest the following code which works fine also under linux, even
> with zero args.
>
> #ifdef linux
>     progname = argv[0];
>     /* Fill the argv buffer vith 0's, once during the initialization */
>     for (i=0; i<argc; argc++) {
>         memset(argv[i], 0, strlen(argv[i]));
>     }
> #endif

This is OK.  It will work.

>
>     /* Build status info */
>     sprintf(status, "%s ...", ...);
> #ifdef bsdi
>     argv[1] = status;
> #endif
> #ifdef linux
>     /* Print the original argv[0] + status info in the argv buffer */
>     sprintf(argv[0], "%s %s", progname, status);
> #endif

This may not work.  The problem is that there is no guarantee that there
enough string space in argv[0] to hold the new string value.  That is
why sendmail actually re-allocates/moves the argv[] strings to make
room, but such code is very ugly.

We can perform some tricks to make argv[0] larger by re-exec'ing the
postmaster, which we already do to make sure we have enough args, but
let's see what Linux people report.

> I would also suggest using only lowercase messages if possible. They don't
> hurt the eyes too much.

Yes, that would be nice, but I want to assign fixed string constants, so
they don't change, and currently I use the same strings that are
displayed as part of psql:

    test=> update test set x=2;
    UPDATE 2
        ^^^^^^

Didn't seem worth making another string for every command type, and
because it is a string constant, I can't lowercase it.

--
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)