aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSeongJae Park <sj38.park@gmail.com>2024-04-06 09:38:03 -0700
committerSeongJae Park <sj38.park@gmail.com>2024-04-06 09:38:03 -0700
commit8265ce5386587877e765a2b4eab0eae877cbee55 (patch)
tree9eefbf63949a64d99cb16729ed19048ba6c08abf
parent3f357ec5a75354d2ae9d590745dfcba2120f855c (diff)
downloaddamo-8265ce5386587877e765a2b4eab0eae877cbee55.tar.gz
damo_record: Move memory footprint recording to _damo_records
Signed-off-by: SeongJae Park <sj38.park@gmail.com>
-rw-r--r--_damo_records.py77
-rw-r--r--damo_record.py78
2 files changed, 80 insertions, 75 deletions
diff --git a/_damo_records.py b/_damo_records.py
index 8c49be69..a60fa89e 100644
--- a/_damo_records.py
+++ b/_damo_records.py
@@ -498,6 +498,83 @@ def update_records_file(file_path, file_format, file_permission=None,
return rewrite_record_file(file_path, file_path, file_format,
file_permission, monitoring_intervals)
+# memory footprint recording
+
+
+def save_mem_footprint(snapshots, filepath, file_permission):
+ with open(filepath, 'w') as f:
+ json.dump([s.to_kvpairs() for s in snapshots], f, indent=4)
+ os.chmod(filepath, file_permission)
+
+# Meaning of the fileds of MemFootprint are as below.
+#
+# ======== =============================== ==============================
+# Field Content
+# ======== =============================== ==============================
+# size total program size (pages) (same as VmSize in status)
+# resident size of memory portions (pages) (same as VmRSS in status)
+# shared number of pages that are shared (i.e. backed by a file, same
+# as RssFile+RssShmem in status)
+# trs number of pages that are 'code' (not including libs; broken,
+# includes data segment)
+# lrs number of pages of library (always 0 on 2.6)
+# drs number of pages of data/stack (including libs; broken,
+# includes library text)
+# dt number of dirty pages (always 0 on 2.6)
+# ======== =============================== ==============================
+#
+# The above table is tolen from Documentation/filesystems/proc.rst file of
+# Linux
+class MemFootprint:
+ size = None
+ resident = None
+ shared = None
+ trs = None
+ lrs = None
+ drs = None
+ dt = None
+
+ def __init__(self, pid):
+ with open('/proc/%s/statm' % pid, 'r') as f:
+ fields = [int(x) for x in f.read().split()]
+ self.size = fields[0]
+ self.resident = fields[1]
+ self.shared = fields[2]
+ self.trs = fields[3]
+ self.lrs = fields[4]
+ self.drs = fields[5]
+ self.dt = fields[6]
+
+ def to_kvpairs(self):
+ return self.__dict__
+
+class MemFootprintsSnapshot:
+ time = None
+ footprints = None
+
+ def __init__(self, pids):
+ self.time = time.time()
+ self.footprints = {}
+ for pid in pids:
+ self.footprints[pid] = MemFootprint(pid)
+
+ def to_kvpairs(self):
+ footprints = []
+ for pid, fp in self.footprints.items():
+ footprints.append({'pid': pid, 'footprint': fp.to_kvpairs()})
+ return {'time': self.time, 'footprints': footprints}
+
+def record_mem_footprint(kdamonds, snapshots):
+ pids = []
+ for kdamond in kdamonds:
+ for ctx in kdamond.contexts:
+ for target in ctx.targets:
+ if target.pid is None:
+ continue
+ pids.append(target.pid)
+ snapshots.append(MemFootprintsSnapshot(pids))
+
+
# for recording
class RecordingHandle:
diff --git a/damo_record.py b/damo_record.py
index ff1ccac6..c7aa6511 100644
--- a/damo_record.py
+++ b/damo_record.py
@@ -24,11 +24,6 @@ data_for_cleanup = DataForCleanup()
cleaning = False
-def save_mem_footprint(snapshots, filepath, file_permission):
- with open(filepath, 'w') as f:
- json.dump([s.to_kvpairs() for s in snapshots], f, indent=4)
- os.chmod(filepath, file_permission)
-
def cleanup_exit(exit_code):
global cleaning
if cleaning == True:
@@ -45,7 +40,7 @@ def cleanup_exit(exit_code):
_damo_records.finish_recording(data_for_cleanup.record_handle)
if data_for_cleanup.footprint_snapshots is not None:
- save_mem_footprint(
+ _damo_records.save_mem_footprint(
data_for_cleanup.footprint_snapshots, '%s.mem_footprint' %
data_for_cleanup.record_handle.file_path,
data_for_cleanup.record_handle.file_permission)
@@ -139,74 +134,6 @@ def poll_target_pids(kdamonds, add_childs):
cleanup_exit(1)
return True
-# Meaning of the fileds of MemFootprint are as below.
-#
-# ======== =============================== ==============================
-# Field Content
-# ======== =============================== ==============================
-# size total program size (pages) (same as VmSize in status)
-# resident size of memory portions (pages) (same as VmRSS in status)
-# shared number of pages that are shared (i.e. backed by a file, same
-# as RssFile+RssShmem in status)
-# trs number of pages that are 'code' (not including libs; broken,
-# includes data segment)
-# lrs number of pages of library (always 0 on 2.6)
-# drs number of pages of data/stack (including libs; broken,
-# includes library text)
-# dt number of dirty pages (always 0 on 2.6)
-# ======== =============================== ==============================
-#
-# The above table is tolen from Documentation/filesystems/proc.rst file of
-# Linux
-class MemFootprint:
- size = None
- resident = None
- shared = None
- trs = None
- lrs = None
- drs = None
- dt = None
-
- def __init__(self, pid):
- with open('/proc/%s/statm' % pid, 'r') as f:
- fields = [int(x) for x in f.read().split()]
- self.size = fields[0]
- self.resident = fields[1]
- self.shared = fields[2]
- self.trs = fields[3]
- self.lrs = fields[4]
- self.drs = fields[5]
- self.dt = fields[6]
-
- def to_kvpairs(self):
- return self.__dict__
-
-class MemFootprintsSnapshot:
- time = None
- footprints = None
-
- def __init__(self, pids):
- self.time = time.time()
- self.footprints = {}
- for pid in pids:
- self.footprints[pid] = MemFootprint(pid)
-
- def to_kvpairs(self):
- footprints = []
- for pid, fp in self.footprints.items():
- footprints.append({'pid': pid, 'footprint': fp.to_kvpairs()})
- return {'time': self.time, 'footprints': footprints}
-
-def record_mem_footprint(kdamonds, snapshots):
- pids = []
- for kdamond in kdamonds:
- for ctx in kdamond.contexts:
- for target in ctx.targets:
- if target.pid is None:
- continue
- pids.append(target.pid)
- snapshots.append(MemFootprintsSnapshot(pids))
-
def main(args):
global data_for_cleanup
@@ -258,7 +185,8 @@ def main(args):
if _damon_args.self_started_target(args):
while poll_target_pids(kdamonds, args.include_child_tasks):
if args.footprint:
- record_mem_footprint(kdamonds, footprint_snapshots)
+ _damo_records.record_mem_footprint(
+ kdamonds, footprint_snapshots)
time.sleep(1)
_damon.wait_kdamonds_turned_off()