Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /tools /multi_process_rss.py
blob: 68a3f3078808d5da5557a43010b4b1affde15574 [file] [log] [blame]
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]1#!/usr/bin/env python
Avi Drissmandfd880852022-09-15 20:11:09[diff] [blame]2# Copyright 2013 The Chromium Authors
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Counts a resident set size (RSS) of multiple processes without double-counts.
7# If they share the same page frame, the page frame is counted only once.
8#
9# Usage:
10# ./multi-process-rss.py <pid>|<pid>r [...]
11#
12# If <pid> has 'r' at the end, all descendants of the process are accounted.
13#
14# Example:
15# ./multi-process-rss.py 12345 23456r
16#
17# The command line above counts the RSS of 1) process 12345, 2) process 23456
18# and 3) all descendant processes of process 23456.
19
Raul Tambre26d7db42019-09-25 11:06:35[diff] [blame]20from __future__import print_function
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]21
22import collections
23import logging
24import os
25import psutil
26import sys
27
28
29if sys.platform.startswith('linux'):
30 _TOOLS_PATH= os.path.dirname(os.path.abspath(__file__))
31 _TOOLS_LINUX_PATH= os.path.join(_TOOLS_PATH,'linux')
32 sys.path.append(_TOOLS_LINUX_PATH)
33import procfs# pylint: disable=F0401
34
35
36class_NullHandler(logging.Handler):
37def emit(self, record):
38pass
39
40
41_LOGGER= logging.getLogger('multi-process-rss')
42_LOGGER.addHandler(_NullHandler())
43
44
45def _recursive_get_children(pid):
dmikurube@chromium.org71151132013-12-11 16:25:20[diff] [blame]46try:
47 children= psutil.Process(pid).get_children()
48except psutil.error.NoSuchProcess:
49return[]
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]50 descendant=[]
51for childin children:
52 descendant.append(child.pid)
53 descendant.extend(_recursive_get_children(child.pid))
54return descendant
55
56
57def list_pids(argv):
58 pids=[]
59for argin argv[1:]:
60try:
61if arg.endswith('r'):
62 recursive=True
63 pid= int(arg[:-1])
64else:
65 recursive=False
66 pid= int(arg)
67exceptValueError:
68raiseSyntaxError("%s is not an integer."% arg)
69else:
70 pids.append(pid)
71if recursive:
72 children= _recursive_get_children(pid)
73 pids.extend(children)
74
75 pids= sorted(set(pids), key=pids.index)# uniq: maybe slow, but simple.
76
77return pids
78
79
80def count_pageframes(pids):
81 pageframes= collections.defaultdict(int)
82 pagemap_dct={}
83for pidin pids:
84 maps= procfs.ProcMaps.load(pid)
dmikurube@chromium.org71151132013-12-11 16:25:20[diff] [blame]85ifnot maps:
86 _LOGGER.warning('/proc/%d/maps not found.'% pid)
87continue
88 pagemap= procfs.ProcPagemap.load(pid, maps)
89ifnot pagemap:
90 _LOGGER.warning('/proc/%d/pagemap not found.'% pid)
91continue
92 pagemap_dct[pid]= pagemap
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]93
94for pid, pagemapin pagemap_dct.iteritems():
95for vmain pagemap.vma_internals.itervalues():
96for pageframe, numberin vma.pageframes.iteritems():
97 pageframes[pageframe]+= number
98
99return pageframes
100
101
dmikurube@chromium.org71151132013-12-11 16:25:20[diff] [blame]102def count_statm(pids):
103 resident=0
104 shared=0
105 private=0
106
107for pidin pids:
108 statm= procfs.ProcStatm.load(pid)
109ifnot statm:
110 _LOGGER.warning('/proc/%d/statm not found.'% pid)
111continue
112 resident+= statm.resident
113 shared+= statm.share
114 private+=(statm.resident- statm.share)
115
116return(resident, shared, private)
117
118
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]119def main(argv):
120 logging_handler= logging.StreamHandler()
121 logging_handler.setLevel(logging.WARNING)
122 logging_handler.setFormatter(logging.Formatter(
123'%(asctime)s:%(name)s:%(levelname)s:%(message)s'))
124
125 _LOGGER.setLevel(logging.WARNING)
126 _LOGGER.addHandler(logging_handler)
127
128if sys.platform.startswith('linux'):
129 logging.getLogger('procfs').setLevel(logging.WARNING)
130 logging.getLogger('procfs').addHandler(logging_handler)
131 pids= list_pids(argv)
132 pageframes= count_pageframes(pids)
133else:
134 _LOGGER.error('%s is not supported.'% sys.platform)
135return1
136
137# TODO(dmikurube): Classify this total RSS.
Raul Tambre26d7db42019-09-25 11:06:35[diff] [blame]138print(len(pageframes)*4096)
dmikurube@chromium.orgb47705b72013-12-03 03:46:39[diff] [blame]139
140return0
141
142
143if __name__=='__main__':
144 sys.exit(main(sys.argv))

[8]ページ先頭

©2009-2025 Movatter.jp