Skip to content

Commit

Permalink
Replace psi python module with psutil
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreedhar Hardikar committed Mar 18, 2016
1 parent 00ce2c1 commit 271373b
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ python:

install:
- pip install --user --upgrade pip
- pip install --user --pre psi
- pip install --user --pre psutil
- pip install --user lockfile
- pip install --user paramiko
- pip install --user setuptools
Expand Down
2 changes: 1 addition & 1 deletion README.amazon_linux
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ sudo make install
sudo pip install --upgrade pip
as root
pip install epydoc
pip install psi
pip install psutil
pip install --upgrade lockfile

# init GPDB
Expand Down
4 changes: 2 additions & 2 deletions README.debian
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pip install lockfile
pip install paramiko
pip install setuptools
pip install epydoc
pip install psi
Note: debian8 required pip install --pre psi
pip install psutil
Note: debian8 required pip install --pre psutil

sysctl.conf
kernel.shmmax = 500000000
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ to the segments, and collects the results.
You will need to add the following Python modules (2.7 & 2.6 are
supported) into your installation

* psi
* psutil
* lockfile (>= 0.9.1)
* paramiko
* setuptools
Expand Down
Empty file added fil
Empty file.
35 changes: 14 additions & 21 deletions gpAux/gpperfmon/src/gpperfmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import shutil
import ConfigParser
import re
import psi.process
import getpass
import psutil
from gppylib.db import dbconn

GPPERFMONHOME=os.getenv('GPPERFMONHOME')
Expand Down Expand Up @@ -244,12 +244,10 @@ def lighty_stop(instance):
status = lighty_status(instance)
if status == RUNNING or status == STRANDED_LIGHT or status == ONLY_LIGHT:
try:
ptable = psi.process.ProcessTable()
for pid in ptable:
process = ptable[pid]
for process in psutil.process_iter():

try:
process_args = process.args
process_args = process.cmdline()
except:
continue
# we do not have privalege to view/kill this process, so skip it
Expand All @@ -261,8 +259,8 @@ def lighty_stop(instance):
if not re.search(instance_conf, process_args[2]):
continue

print "killing pid %d" % pid
os.kill(pid, signal.SIGTERM)
print "killing pid %d" % process.pid
os.kill(process.pid, signal.SIGTERM)

# lighty shuts down quickly, but give it a little bit of time.
time.sleep(1)
Expand All @@ -289,22 +287,19 @@ def lighty_stop(instance):
def kill_orphaned_cgi_scripts():
"Search for any gpmonws.py that are stranded and do not belong to lighttpd"

ptable = psi.process.ProcessTable()
for pid in ptable:
process = ptable[pid]

for process in psutil.process_iter():
try:
process_args = process.args
process_args = process.cmdline()
except:
continue
# we do not have privalege to view/kill this process, so skip it

if len(process_args) >= 2:
if process_args[0] == 'python':
if re.search("gpmonws.py", process_args[1]):
if process.ppid == 1:
print "Killing stranded gpmonws.py process with pid %d" % pid
os.kill(pid, signal.SIGTERM)
if process.ppid() == 1:
print "Killing stranded gpmonws.py process with pid %d" % process.pid
os.kill(process.pid, signal.SIGTERM)

#################
def lighty_restart(instance):
Expand Down Expand Up @@ -346,12 +341,10 @@ def lighty_status(instance):
pass

try:
ptable = psi.process.ProcessTable()
for pid in ptable:
process = ptable[pid]
for process in psutil.process_iter():

try:
process_args = process.args
process_args = process.cmdline()
except:
continue
# we do not have privalege to view/kill this process, so skip it
Expand All @@ -361,7 +354,7 @@ def lighty_status(instance):

# lighttpd process
if re.search(light_bin, process_args[0]):
if lightpid != 0 and pid == lightpid:
if lightpid != 0 and process.pid == lightpid:
foundLighthttp = True
elif len(process_args) >= 3:
if re.search(instance_conf, process_args[2]):
Expand All @@ -372,7 +365,7 @@ def lighty_status(instance):
if len(process_args) < 2:
continue
if re.search(instance_mon, process_args[1]):
if lightpid != 0 and process.ppid == lightpid:
if lightpid != 0 and process.ppid() == lightpid:
foundGpmonws = True
else:
foundStrandedPython = True
Expand Down
20 changes: 10 additions & 10 deletions gpMgmt/bin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PYLIB_DIR=$(SRC)/ext
core: pygresql subprocess32

ifneq "$(wildcard $(CURDIR)/pythonSrc/ext/*.tar.gz)" ""
all: core lockfile paramiko pycrypto stream pychecker psi unittest2
all: core lockfile paramiko pycrypto stream pychecker psutil unittest2
else
all: core stream
endif
Expand Down Expand Up @@ -119,16 +119,16 @@ pycrypto:
cp -r $(PYLIB_SRC_EXT)/$(PYCRYPTO_DIR)/build/lib.*/Crypto $(PYLIB_DIR)

#
# PSI
# PSUTIL
#
PSI_VERSION=0.3b2_gp
PSI_DIR=PSI-$(PSI_VERSION)

psi:
@echo "--- psi"
cd $(PYLIB_SRC_EXT)/ && $(TAR) xzf $(PSI_DIR).tar.gz
cd $(PYLIB_SRC_EXT)/$(PSI_DIR)/ && CC="$(CC)" python setup.py build
cp -r $(PYLIB_SRC_EXT)/$(PSI_DIR)/build/lib.*/psi $(PYLIB_DIR)
PSUTIL_VERSION=4.0.0
PSUTIL_DIR=psutil-$(PSUTIL_VERSION)

psutil:
@echo "--- psutil"
cd $(PYLIB_SRC_EXT)/ && $(TAR) xzf $(PSUTIL_DIR).tar.gz
cd $(PYLIB_SRC_EXT)/$(PSUTIL_DIR)/ && CC="$(CC)" python setup.py build
cp -r $(PYLIB_SRC_EXT)/$(PSUTIL_DIR)/build/lib.*/psutil $(PYLIB_DIR)


#
Expand Down
14 changes: 7 additions & 7 deletions gpMgmt/bin/gpdebug
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"""
import os, signal, time

from psi import AttrInsufficientPrivsError
from psi.process import Process, ProcessTable, NoSuchProcessError
import psutil

from gppylib import gplog, pgconf
from gppylib.db import dbconn
Expand Down Expand Up @@ -339,10 +338,11 @@ class GpDebugMain(YamlMain):
""
self.logger.info("Era: %s" % self.options.era)
self.orphans = []
for p in ProcessTable().values():
for p in psutil.process_iter():
try:
d = dict(p.env)
except AttrInsufficientPrivsError:
d = p.environ()
command = ' '.join(p.cmdline())
except psutil.Error:
continue
e = d.get('GPERA')
s = d.get('GPSESSID')
Expand All @@ -352,7 +352,7 @@ class GpDebugMain(YamlMain):
if e is None:
continue
if e != self.options.era:
self.orphans.append((e, s, str(p.pid), p.command.strip()))
self.orphans.append((e, s, str(p.pid), command.strip()))
continue

# look for process in same era with a session id not in
Expand All @@ -369,7 +369,7 @@ class GpDebugMain(YamlMain):
if s == '0' or s in (self.options.sessionids or []):
continue

self.orphans.append((e, s, str(p.pid), p.command.strip()))
self.orphans.append((e, s, str(p.pid), command.strip()))

def return_orphan_details(self):
""
Expand Down
20 changes: 7 additions & 13 deletions gpMgmt/bin/gppylib/commands/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"""
import os
import psutil
import platform
import socket
import sys
import signal
import psi.process

from gppylib.gplog import *
from gppylib.commands.base import *
Expand Down Expand Up @@ -961,21 +961,15 @@ def file_uid(self):
#--------------get list of desecendant processes -------------------

def getDescendentProcesses(pid):
''' return all process which are descendant from the given processid '''
''' return all process pids which are descendant from the given processid '''

children = list()
grandchildren = list()
children_pids = []

for p in psi.process.ProcessTable().values():
for p in psutil.Process(pid).children(recursive=True):
if p.is_running():
children_pids.append(p.pid)

if int(p.ppid) == int(pid):
children.append(int(p.pid))

# recursion
for child in children:
grandchildren.extend( getDescendentProcesses(child) )

return children + grandchildren
return children_pids


#--------------global variable initialization ----------------------
Expand Down
9 changes: 5 additions & 4 deletions gpMgmt/bin/gppylib/programs/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from gppylib.mainUtils import addStandardLoggingAndHelpOptions, ProgramArgumentValidationException
from gppylib.commands.unix import kill_sequence, check_pid
from gppylib.operations.package import dereference_symlink
from psi.process import Process, NoSuchProcessError
from psutil import Process, NoSuchProcess

logger = gplog.get_default_logger()

Expand Down Expand Up @@ -94,12 +94,13 @@ def examine_process(self, pid):

try:
proc = Process(pid=pid)
except NoSuchProcessError, e:
except NoSuchProcess, e:
raise KillError('Process with pid(%s) does not exist' % pid)

logger.info('process %s is %s' % (pid, proc.command.strip()))
command = ' '.join(proc.cmdline())
logger.info('process %s is %s' % (pid, command.strip()))

return proc.command.strip()
return command.strip()

def terminate_process(self, pid):

Expand Down
13 changes: 7 additions & 6 deletions gpMgmt/bin/gppylib/test/regress/test_regress_gpssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import os, signal, time, re
import unittest2 as unittest
import psi.process, subprocess
import psutil
from subprocess import PIPE

class GpsshTestCase(unittest.TestCase):

Expand All @@ -12,12 +13,12 @@ def searchForProcessOrChildren(self):
euid = os.getuid()
count = 0

for p in psi.process.ProcessTable().values():
if p.euid != euid:
for p in psutil.process_iter():
if p.uids().effective != euid:
continue
if not re.search('ssh', p.command):
if not re.search('ssh', ' '.join(p.cmdline())):
continue
if p.ppid != 1:
if p.ppid() != 1:
continue

count += 1
Expand All @@ -32,7 +33,7 @@ def test00_gpssh_sighup(self):

before_count = self.searchForProcessOrChildren()

p = subprocess.Popen("gpssh -h localhost", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = psutil.Popen("gpssh -h localhost", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
pid = p.pid

time.sleep(3)
Expand Down
2 changes: 1 addition & 1 deletion gpMgmt/bin/pythonSrc/ext
Submodule ext updated from f09729 to 71cc64
2 changes: 1 addition & 1 deletion vagrant/centos/vagrant-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sudo yum -y install perl-Env
sudo yum -y install ccache
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo pip install psi lockfile paramiko setuptools epydoc
sudo pip install psutil lockfile paramiko setuptools epydoc
rm get-pip.py

# Misc
Expand Down
2 changes: 1 addition & 1 deletion vagrant/debian/vagrant-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pip install lockfile
pip install paramiko
pip install setuptools
pip install epydoc
pip install --pre psi
pip install --pre psutil

sudo service docker start
sudo useradd postgres
Expand Down

0 comments on commit 271373b

Please sign in to comment.