From 30a3f23cdd48f0b4310a1eb62c24971251fe0436 Mon Sep 17 00:00:00 2001 From: Denis Monnerat Date: Thu, 3 Sep 2026 11:36:12 +0200 Subject: [PATCH] typo --- tp/tp1/src/scripts/parse_smaps.py | 123 ----------------------- tp/tp1/src/scripts/vmap.py | 159 ------------------------------ 2 files changed, 282 deletions(-) delete mode 100755 tp/tp1/src/scripts/parse_smaps.py delete mode 100755 tp/tp1/src/scripts/vmap.py diff --git a/tp/tp1/src/scripts/parse_smaps.py b/tp/tp1/src/scripts/parse_smaps.py deleted file mode 100755 index 4dac000..0000000 --- a/tp/tp1/src/scripts/parse_smaps.py +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/bin/env python -# -# Author: Craig Chi -# - -import sys -import os -import getopt -from collections import defaultdict, OrderedDict -from subprocess import check_output - -def usage(): - print(""" -usage: parse_smaps.py [-p process_name] [-t memory_type] [-h] [smaps_filename] - -example: parse_smaps.py /proc/12424/smaps - parse_smaps.py -p smbd - parse_smaps.py -p smbd -t Pss -""") - - -def print_header(mem_idx): - print('=' * 70) - for title in zip(*map(lambda x: x.split('_'), mem_idx.keys()), - ('', '= Total : library')): - print('{:>8} + {:>8} + {:>8} + {:>8} {}'.format(*title,)) - print('=' * 70) - - -def main(): - try: - opts, args = getopt.getopt(sys.argv[1:], 'p:t:ah', - ['process-name=', 'memory-type=', - 'all', 'help']) - except getopt.GetoptError as err: - print(err) - sys.exit(2) - - ps_name = '' - mem_type = '' - mem_idx = OrderedDict([ - ('Private_Clean', 0), - ('Private_Dirty', 1), - ('Shared_Clean', 2), - ('Shared_Dirty', 3) - ]) - for o, a in opts: - if o in ('-p', '--process-name'): - ps_name = a - elif o in ('-t', '--memory-type'): - mem_type = a - mem_idx = {a: 0} - else: - usage() - sys.exit(2) - - if (len(args) == 0 and ps_name == '') or len(args) > 1: - usage() - sys.exit(2) - - smaps_file = '' - if ps_name == '': - smaps_file = os.path.abspath(args[0]) - else: - try: - pids = check_output(['pidof', ps_name]).decode().strip().split() - if len(pids) > 1: - print('There are multiple pids:') - for i, p in enumerate(pids): - cmdline_file = '/proc/' + p + '/cmdline' - with open(cmdline_file, 'r') as cmdline: - line = next(cmdline) - print('[{}] {:>8}: {}'.format(i, p, line)) - num = input('Choose which one process you want (default=0): ') - num = int(num) if num != '' else 0 - pid = pids[num] - else: - pid = pids[0] - except Exception as err: - print(err) - sys.exit(1) - - smaps_file = '/proc/' + pid + '/smaps' - - mapinfo = defaultdict(lambda: [0] * len(mem_idx)) - total = [0] * len(mem_idx) - - with open(smaps_file, 'r') as smap: - for line in smap: - line_arr = line.split() - if '-' in line_arr[0]: - if len(line_arr) < 6: - filename = '[anonymous]' - else: - filename = os.path.basename(line_arr[-1]) - else: - line_arr[0] = line_arr[0].strip(':') - - if line_arr[0] in mem_idx: - mapinfo[filename][mem_idx[line_arr[0]]] += int(line_arr[1]) - total[mem_idx[line_arr[0]]] += int(line_arr[1]) - - if mem_type == '': - print_header(mem_idx) - - for filename, mem in sorted(mapinfo.items(), key=lambda x: -sum(x[1])): - print('{:>5} kB + {:>5} kB + {:>5} kB + {:>5} kB' - ' = {:>5} kB : {:<}'.format(*mem, sum(mem), filename)) - - print('=' * 70) - print('{:>5} kB + {:>5} kB + {:>5} kB + {:>5} kB' - ' = {:>5} kB : Total'.format(*total, sum(total))) - - else: - for filename, mem in sorted(mapinfo.items(), key=lambda x: -sum(x[1])): - print('{:>11} kB {:<}'.format(mem[0], filename)) - - print('=' * 30) - print('Total: {} kB'.format(total[0])) - - -if __name__ == '__main__': - main() diff --git a/tp/tp1/src/scripts/vmap.py b/tp/tp1/src/scripts/vmap.py deleted file mode 100755 index ae3a643..0000000 --- a/tp/tp1/src/scripts/vmap.py +++ /dev/null @@ -1,159 +0,0 @@ -#!/usr/bin/python -# coding=utf-8 - -"""Tool to analyze and display the contents of /proc//maps""" - -import re -import itertools -import argparse -from dataclasses import dataclass - -MAPS_LINE_RE = re.compile(r""" - (?P[0-9a-f]+)-(?P[0-9a-f]+)\s+ # Address - (?P\S+)\s+ # Permissions - (?P[0-9a-f]+)\s+ # Map offset - (?P\S+)\s+ # Device node - (?P\d+)\s+ # Inode - (?P.*)\s+ # Pathname -""", re.VERBOSE) - - -def human_bytes(size): - modifier = 1 - while size > 1024: - modifier *= 1024 - size /= 1024 - return "%.1f%s" % (size, { - 1024**0: 'b', - 1024**1: 'k', - 1024**2: 'M', - 1024**3: 'G', - 1024**4: 'T', - }.get(modifier, " x%d" % modifier)) - - -@dataclass -class Record: - addr_start: int - addr_end: int - perms: str - offset: int - dev: str - inode: int - pathname: str - - @property - def size(self): - return self.addr_end - self.addr_start - - @property - def human_size(self): - return human_bytes(self.size) - - @property - def readable(self): - return self.perms[0] == "r" - - @property - def writable(self): - return self.perms[1] == "w" - - @property - def executable(self): - return self.perms[2] == "x" - - @property - def shared(self): - return self.perms[3] == "s" - - @property - def private(self): - return self.perms[3] == "p" - - @classmethod - def parse(self, pid): - records = [] - with open("/proc/%d/maps" % pid) as fd: - for line in fd: - m = MAPS_LINE_RE.match(line) - if not m: - print("Skipping: %s" % line) - continue - addr_start, addr_end, perms, offset, dev, inode, pathname = m.groups() - addr_start = int(addr_start, 16) - addr_end = int(addr_end, 16) - offset = int(offset, 16) - records.append(Record( - addr_start=addr_start, - addr_end=addr_end, - perms=perms, - offset=offset, - dev=dev, - inode=inode, - pathname=pathname, - )) - return records - - @classmethod - def aggregate(self, records, only_used=False, only_private=False): - - named_records = {} - anonymous_records = [] - for record in records: - if only_private and not record.private: - continue - if only_used and not record.readable and not record.writable and not record.shared and not record.pathname: - continue - if record.pathname: - if record.pathname in named_records: - other = named_records[record.pathname] - named_records[record.pathname] = Record( - min(record.addr_start, other.addr_start), - max(record.addr_end, other.addr_end), - perms=''.join("?" if c1 != c2 else c1 for c1, c2 in zip(record.perms, other.perms)), - offset=0, - dev='', - inode='', - pathname=record.pathname, - ) - else: - named_records[record.pathname] = record - else: - anonymous_records.append(record) - - return list(sorted( - itertools.chain(anonymous_records, named_records.values()), - key=lambda r: r.size, - reverse=True, - )) - - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument("pid", type=int, help="Process identifier (pid)") - parser.add_argument("--only-used", "-u", action="store_true", help="Only show used pages (non readable, writable, executable and private pages)") - parser.add_argument("--only-private", "-p", action="store_true", help="Only show private pages") - args = parser.parse_args() - - records = Record.parse(args.pid) - #records = Record.aggregate(records, only_used=args.only_used, only_private=args.only_private) - - print("\t".join([ - "% 16s" % "Start of range", - "% 16s" % "End of range", - "% 12s" % "Size", - "% 4s" % "Perms", - "Path", - ])) - for record in records: - print("\t".join([ - "%016x" % record.addr_start, - "%016x" % record.addr_end, - "% 12s" % record.human_size, - "% 4s" % record.perms, - record.pathname, - ])) - - print("") - print("Total: %s" % human_bytes(sum(r.size for r in records)))