pg_activity icon indicating copy to clipboard operation
pg_activity copied to clipboard

MacOS: can't get CPU/mem stats

Open rutchkiwi opened this issue 2 years ago • 9 comments

Hi. This looks like a really cool tool but I can not manage to get the CPU% column to show under MacOS (10.15.7).

I launch like this pg_activity --dbname=web

I assume I do not need to change the user as homebrew launches processes using my user:

❯ ps aux | grep postgres
viktor             573   0.0  0.0  4488148    400   ??  Ss   30Jun22   0:00.53 postgres: logical replication launcher
viktor             572   0.0  0.0  4341228    700   ??  Ss   30Jun22   1:09.45 postgres: stats collector
viktor             571   0.0  0.0  4488148    956   ??  Ss   30Jun22   0:12.22 postgres: autovacuum launcher
viktor             570   0.0  0.0  4487956     84   ??  Ss   30Jun22   0:20.46 postgres: walwriter
viktor             569   0.0  0.0  4487956    184   ??  Ss   30Jun22   0:05.68 postgres: background writer
viktor             568   0.0  0.0  4488488    220   ??  Ss   30Jun22   0:21.74 postgres: checkpointer
viktor             468   0.0  0.0  4488240    440   ??  S    30Jun22   0:46.32 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
viktor           13104   0.0  0.0  4268180    480 s004  R+   12:39pm   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn postgres
viktor           12430   0.0  0.0  4488508   2272   ??  Ss   12:26pm   0:00.01 postgres: viktor web [local] idle

On another issue there was a comment to try this, which works:

import psutil
print(psutil.virtual_memory())
p = psutil.Process(468)
print(p.cpu_times())
exit()
EOF
svmem(total=8589934592, available=1713590272, percent=80.1, used=5525413888, free=16392192, active=1698951168, inactive=1661759488, wired=3826462720)
pcputimes(user=8.53199872, system=37.791858688, children_user=0.0, children_system=0.0)

Screenshot 2022-07-20 at 12 40 29

Apologies if I'm missing something obvious and appreciate any help!

rutchkiwi avatar Jul 20 '22 11:07 rutchkiwi

Hi, does your user have access to PGDATA ? (we read the pidfile in PGDATA)

blogh avatar Jul 20 '22 14:07 blogh

@blogh Yes I can read it just fine using my own user:

❯ cat /usr/local/var/postgres/postmaster.pid
468
/usr/local/var/postgres
1656576135
5432
/tmp
localhost
 83572118     65536
ready

❯ ps aux | grep ' 468 '
viktor             468   0.0  0.0  4488240    412   ??  S    30Jun22   0:47.60 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres

rutchkiwi avatar Jul 20 '22 16:07 rutchkiwi

ok, Thanks for testing. I will look further into it ASAP, hopefully this week. Unfortunately, It's a busy working week and holidays are looming.

blogh avatar Jul 21 '22 08:07 blogh

Thanks, I appreciate any help! And of course enjoy your holidays, I only appriciate this tool being availible :)

/Viktor On 21 Jul 2022, 09:08 +0100, Benoit @.***>, wrote:

ok, Thanks for testing. I will look further into it ASAP, hopefully this week. Unfortunately, It's a busy working week and holidays are looming. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

rutchkiwi avatar Jul 21 '22 09:07 rutchkiwi

is the "viktor" database user a "superuser" ? (see \du+)

blogh avatar Aug 01 '22 14:08 blogh

@blogh Indeed it is:

                                          List of roles
 Role name │                         Attributes                         │ Member of │ Description
───────────┼────────────────────────────────────────────────────────────┼───────────┼─────────────
 postgres  │ Superuser, Create role, Create DB                          │ {}        │
 root      │ Superuser                                                  │ {}        │
 test      │ Replication                                                │ {}        │
 viktor    │ Superuser, Create role, Create DB, Replication, Bypass RLS │ {}        │

rutchkiwi avatar Aug 03 '22 09:08 rutchkiwi

Can you try this code please ? (python ) Please use environment variables to set the connection (PGHOST, PGPORT, PGUSER, PGDATABASE)

import psycopg2
import psycopg2.extras
import psutil

conn = psycopg2.connect(cursor_factory=psycopg2.extras.DictCursor)
query = "SELECT setting||'/postmaster.pid' AS pid_file FROM pg_settings WHERE name = 'data_directory';"

with conn.cursor() as cur:
    # This query doesn't crash when the user doesn't have the
    # requiered privilege to acces the data_directory guc
    # it will just return an empty string
    cur.execute(query)
    ret = cur.fetchone()
pid_file = ret["pid_file"]    # fails here : not enought privilege in postgres
with open(pid_file, "r") as fd:
    pid = fd.readlines()[0].strip()  # fails here : no access to the file (maybe the line before ?)
    proc = psutil.Process(int(pid))  # fails here or after : problems with psutils
    proc.io_counters()
    proc.cpu_times()
print("success")

blogh avatar Aug 05 '22 09:08 blogh

it's from data.py => pg_is_local_access()

blogh avatar Aug 05 '22 09:08 blogh

Apologies for the delay @blogh. Here are the results of running it:

❯ PGDATABASE=web python3 test.py
Traceback (most recent call last):
  File "/Users/viktor/dev/web/test.py", line 18, in <module>
    proc.io_counters()
AttributeError: 'Process' object has no attribute 'io_counters'

I don't have a sufficient understanding to interpret that.

rutchkiwi avatar Aug 11 '22 12:08 rutchkiwi

Apologies for the delay @blogh. Here are the results of running it

no problem, I went back on holliday also anyway.

Ok, so the counters are not displayed because they are not available on mac via psutil.

blogh avatar Aug 16 '22 12:08 blogh

I will document this and see if we can issue a warning message when that kind of stuff appends in a future release.

blogh avatar Aug 18 '22 08:08 blogh

Hi,

Master now displays a hint when you exit in cases where feature were disabled at runtime. The man page also adds some info about such cases.

blogh avatar Sep 05 '22 12:09 blogh