|
2 | 2 | # @Author: oesteban
|
3 | 3 | # @Date: 2017-09-21 15:50:37
|
4 | 4 | # @Last Modified by: oesteban
|
5 |
| -# @Last Modified time: 2017-09-28 13:11:03 |
| 5 | +# @Last Modified time: 2017-09-29 16:42:27 |
6 | 6 | """
|
7 | 7 | Utilities to keep track of performance
|
8 | 8 | """
|
|
33 | 33 |
|
34 | 34 | class ResourceMonitor(threading.Thread):
|
35 | 35 | def __init__(self, pid, freq=5, fname=None):
|
36 |
| - if freq <= 0: |
37 |
| - raise RuntimeError('Frequency (%0.2fs) cannot be lower than zero' % freq) |
| 36 | + if freq < 0.2: |
| 37 | + raise RuntimeError('Frequency (%0.2fs) cannot be lower than 0.2s' % freq) |
38 | 38 |
|
39 | 39 | if fname is None:
|
40 |
| - fname = '.nipype.prof' |
| 40 | + fname = '.proc-%d_time-%s_freq-%0.2f' % (pid, time(), freq) |
41 | 41 |
|
42 | 42 | self._pid = pid
|
43 | 43 | self._fname = fname
|
44 | 44 | self._freq = freq
|
45 | 45 |
|
46 |
| - self._log = open(self._fname, 'w') |
47 |
| - print('%s,0.0,0' % time(), file=self._log) |
48 |
| - self._log.flush() |
| 46 | + self._logfile = open(self._fname, 'w') |
| 47 | + self._sample() |
| 48 | + |
49 | 49 | threading.Thread.__init__(self)
|
50 | 50 | self._event = threading.Event()
|
51 | 51 |
|
| 52 | + @property |
| 53 | + def fname(self): |
| 54 | + return self._fname |
| 55 | + |
52 | 56 | def stop(self):
|
53 | 57 | if not self._event.is_set():
|
54 | 58 | self._event.set()
|
55 | 59 | self.join()
|
56 |
| - ram = _get_ram_mb(self._pid) or 0 |
57 |
| - cpus = _get_num_threads(self._pid) or 0 |
58 |
| - print('%s,%f,%d' % (time(), ram, cpus), |
59 |
| - file=self._log) |
60 |
| - self._log.flush() |
61 |
| - self._log.close() |
| 60 | + self._sample() |
| 61 | + self._logfile.flush() |
| 62 | + self._logfile.close() |
| 63 | + |
| 64 | + def _sample(self): |
| 65 | + ram = _get_ram_mb(self._pid) or 0 |
| 66 | + cpus = _get_num_threads(self._pid) or 0 |
| 67 | + print('%s,%f,%d' % (time(), ram, cpus), |
| 68 | + file=self._logfile) |
| 69 | + self._logfile.flush() |
62 | 70 |
|
63 | 71 | def run(self):
|
64 | 72 | while not self._event.is_set():
|
65 |
| - ram = _get_ram_mb(self._pid) |
66 |
| - cpus = _get_num_threads(self._pid) |
67 |
| - if ram is not None and cpus is not None: |
68 |
| - print('%s,%f,%d' % (time(), ram, cpus), |
69 |
| - file=self._log) |
70 |
| - self._log.flush() |
| 73 | + self._sample() |
71 | 74 | self._event.wait(self._freq)
|
72 | 75 |
|
73 | 76 |
|
|
0 commit comments