blob: a202965aff82f443bb7ec594f7ddb0d259ad03f2 [file] [log] [blame]
Vince Lehmanbf600a42016-06-01 17:14:43 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Saurab Dulal576a4192020-08-25 00:55:22 -05003# Copyright (C) 2015-2020, The University of Memphis,
Vince Lehmanbf600a42016-06-01 17:14:43 -05004# 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
24import time
25
Vince Lehmanbf600a42016-06-01 17:14:43 -050026from threading import Timer
27
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050028class ProcessMonitor(object):
Vince Lehmanbf600a42016-06-01 17:14:43 -050029 def __init__(self, processId, processName, outputDir, interval=1):
30 self._processId = processId.strip()
31 self._processName = processName
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050032 self._statFile = '/proc/{}/stat'.format(self._processId)
33 self._logFile = '{}/{}-{}-stat.txt'.format(outputDir, self._processName, self._processId)
Vince Lehmanbf600a42016-06-01 17:14:43 -050034 self._interval = interval
35
36 def _recordStats(self):
37 try:
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050038 with open(self._statFile, 'r') as stat:
Vince Lehmanbf600a42016-06-01 17:14:43 -050039 currentTime = int(time.time())
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050040 with open(self._logFile, 'a') as log:
Vince Lehmanbf600a42016-06-01 17:14:43 -050041 for line in stat:
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050042 log.write('{} {}'.format(currentTime, line))
Vince Lehmanbf600a42016-06-01 17:14:43 -050043 except IOError as e:
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050044 print('I/O error({0}): {1}'.format(e.errno, e.strerror))
45 print('No process with PID={}'.format(self._processId))
Vince Lehmanbf600a42016-06-01 17:14:43 -050046 return
47
48 self.start() # Reschedule event
49
50 def start(self):
51 self._timer = Timer(self._interval, self._recordStats)
52 self._timer.start()