Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Friedland committed Feb 21, 2019
1 parent c712dcd commit 3d5686f
Show file tree
Hide file tree
Showing 4 changed files with 1,248 additions and 1,372 deletions.
16 changes: 7 additions & 9 deletions python_bindings/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
if os.path.exists(library_file):
# if we have a prebuilt nmslib library file, use that.
extra_objects.append(library_file)
print("Found: " + os.path.abspath(library_file))
else:
raise RuntimeError("can't find prebuild lib: " + os.path.abspath(library_file))
# # Otherwise build all the files here directly (excluding extras which need eigen/boost)
# exclude_files = set("""bbtree.cc lsh.cc lsh_multiprobe.cc lsh_space.cc falconn.cc nndes.cc space_sqfd.cc
# dummy_app.cc main.cc""".split())
#
# for root, subdirs, files in os.walk(os.path.join(libdir, "src")):
# source_files.extend(os.path.join(root, f) for f in files
# if f.endswith(".cc") and f not in exclude_files)
# Otherwise build all the files here directly (excluding extras which need eigen/boost)
exclude_files = set("""bbtree.cc lsh.cc lsh_multiprobe.cc lsh_space.cc falconn.cc nndes.cc space_sqfd.cc
dummy_app.cc main.cc""".split())

for root, subdirs, files in os.walk(os.path.join(libdir, "src")):
source_files.extend(os.path.join(root, f) for f in files
if f.endswith(".cc") and f not in exclude_files)


if sys.platform.startswith('linux'):
Expand Down
119 changes: 0 additions & 119 deletions python_bindings/tests/bindings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,6 @@
import numpy.testing as npt

import nmslib
import psutil
import logging
import multiprocessing
import time
import os
import threading

MB = 1024 * 1024


class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""

def __init__(self, *args, **kwargs):
super().__init__()
self._stop_event = threading.Event()

def stop(self):
self._stop_event.set()

def stopped(self):
return self._stop_event.is_set()


class Timer:
""" Context manager for timing named blocks of code """
def __init__(self, name, logger=None):
self.name = name
self.logger = logger if logger else logging.getLogger()

def __enter__(self):
self.start = time.time()
self.logger.debug("Starting {}".format(self.name))

def __exit__(self, type, value, trace):
self.logger.info("{}: {:0.2f}s".format(self.name, time.time() - self.start))


class PeakMemoryUsage:
class Worker(StoppableThread):
def __init__(self, interval, *args, **kwargs):
super().__init__(*args, **kwargs)
self.interval = interval
self.max_rss = self.max_vms = 0

def run(self):
process = psutil.Process()
while not self.stopped():
mem = process.memory_info()
self.max_rss = max(self.max_rss, mem.rss)
self.max_vms = max(self.max_vms, mem.vms)
time.sleep(self.interval)

""" Context manager to calculate peak memory usage in a statement block """
def __init__(self, name, logger=None, interval=1):
self.name = name
self.logger = logger if logger else logging.getLogger()
self.interval = interval
self.start = time.time()
self.worker = None

def __enter__(self):
if self.interval > 0:
pid = os.getpid()
mem = psutil.Process(pid).memory_info()
self.start_rss, self.start_vms = mem.rss, mem.vms

self.worker = PeakMemoryUsage.Worker(self.interval)
self.worker.start()
return self

def __exit__(self, _, value, trace):
if self.worker:
self.worker.stop()
self.worker.join()
self.logger.warning("Peak memory usage for '{}' in MBs: orig=(rss={:0.1f} vms={:0.1f}) "
"peak=(rss={:0.1f} vms={:0.1f}) in {:0.2f}s"
.format(self.name, self.start_rss / MB, self.start_vms / MB,
self.worker.max_rss / MB,
self.worker.max_vms / MB, time.time() - self.start))


class PsUtil(object):
def __init__(self, attr=('virtual_memory',), proc_attr=None,
logger=None, interval=60):
""" attr can be multiple methods of psutil (e.g. attr=['virtual_memory', 'cpu_times_percent']) """
self.ps_mon = None
self.attr = attr
self.proc_attr = proc_attr
self.logger = logger if logger else logging.getLogger()
self.interval = interval

def psutil_worker(self, pid):
root_proc = psutil.Process(pid)
while True:
for attr in self.attr:
self.logger.warning("PSUTIL {}".format(getattr(psutil, attr)()))
if self.proc_attr:
procs = set(root_proc.children(recursive=True))
procs.add(root_proc)
procs = sorted(procs, key=lambda p: p.pid)

for proc in procs:
self.logger.warning("PSUTIL process={}: {}"
.format(proc.pid, proc.as_dict(self.proc_attr)))

time.sleep(self.interval)

def __enter__(self):
if self.interval > 0:
self.ps_mon = multiprocessing.Process(target=self.psutil_worker, args=(os.getpid(),))
self.ps_mon.start()
time.sleep(1) # sleep so the first iteration doesn't include statements in the PsUtil context
return self

def __exit__(self, type, value, trace):
if self.ps_mon is not None:
self.ps_mon.terminate()


def get_exact_cosine(row, data, N=10):
Expand Down
Loading

0 comments on commit 3d5686f

Please sign in to comment.