Vince Lehman | bf600a4 | 2016-06-01 17:14:43 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015-2016, The University of Memphis, |
| 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | import time |
| 25 | |
| 26 | from subprocess import call |
| 27 | from threading import Timer |
| 28 | |
| 29 | class ProcessMonitor: |
| 30 | def __init__(self, processId, processName, outputDir, interval=1): |
| 31 | self._processId = processId.strip() |
| 32 | self._processName = processName |
| 33 | self._statFile = "/proc/{}/stat".format(self._processId) |
| 34 | self._logFile = "{}/{}-{}-stat.txt".format(outputDir, self._processName, self._processId) |
| 35 | self._interval = interval |
| 36 | |
| 37 | def _recordStats(self): |
| 38 | try: |
| 39 | with open(self._statFile, "r") as stat: |
| 40 | currentTime = int(time.time()) |
| 41 | with open(self._logFile, "a") as log: |
| 42 | for line in stat: |
| 43 | log.write("{} {}".format(currentTime, line)) |
| 44 | except IOError as e: |
| 45 | print "I/O error({0}): {1}".format(e.errno, e.strerror) |
| 46 | print "No process with PID={}".format(self._processId) |
| 47 | return |
| 48 | |
| 49 | self.start() # Reschedule event |
| 50 | |
| 51 | def start(self): |
| 52 | self._timer = Timer(self._interval, self._recordStats) |
| 53 | self._timer.start() |