Skip to content

Commit

Permalink
tools: allows heapwatch to enable mutex and blocking profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Sep 10, 2024
1 parent d060921 commit 2f4250a
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions test/heapwatch/heapWatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,32 @@ def get_pprof_snapshot(self, name, snapshot_name=None, outdir=None, timeout=None
logger.debug('%s -> %s', self.nick, outpath)
return outpath

def get_debug_settings_pprof(self):
timeout = self.timeout
url = 'http://' + self.net + '/debug/settings/pprof'
headers = self.headers.copy()
headers['X-Algo-API-Token'] = self.admin_token
try:
response = urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=timeout)
except Exception as e:
logger.error('could not fetch %s from %s via %r (%s)', '/debug/settings/pprof', self.path, url, e)
return
blob = response.read()
return json.loads(blob)

def set_debug_settings_pprof(self, settings):
timeout = self.timeout
url = 'http://' + self.net + '/debug/settings/pprof'
headers = self.headers.copy()
headers['X-Algo-API-Token'] = self.admin_token
data = json.dumps(settings).encode()
try:
response = urllib.request.urlopen(urllib.request.Request(url, data=data, headers=headers, method='PUT'), timeout=timeout)
except Exception as e:
logger.error('could not put %s to %s via %r (%s)', settings, self.path, url, e)
return
response.close()

def get_heap_snapshot(self, snapshot_name=None, outdir=None):
return self.get_pprof_snapshot('heap', snapshot_name, outdir)

Expand Down Expand Up @@ -355,6 +381,24 @@ def do_snap(self, now, get_cpu=False, fraction=False):
rss, vsz = rssvsz
with open(os.path.join(self.args.out, nick + '.heap.csv'), 'at') as fout:
fout.write('{},{},{},{}\n'.format(snapshot_name,snapshot_isotime,rss, vsz))
if self.args.mutex or self.args.block:
# get mutex/blocking profiles state and enable as needed
for ad in self.they:
settings = ad.get_debug_settings_pprof()
updated = False
if self.args.mutex:
mrate = settings.get('mutex-rate', 0)
if mrate == 0:
settings['mutex-rate'] = 5 # 1/5 of events recorded
updated = True
if self.args.block:
brate = settings.get('block-rate', 0)
if brate == 0:
settings['block-rate'] = 100 # one blocking event per 100 nanoseconds spent blocked.
updated = True
if updated:
logger.debug('enabling mutex/blocking profiles on %s', ad.path)
ad.set_debug_settings_pprof(settings)
if self.args.goroutine:
for ad in self.they:
ad.get_goroutine_snapshot(snapshot_name, outdir=self.args.out)
Expand Down Expand Up @@ -466,12 +510,6 @@ def main():
else:
logging.basicConfig(level=logging.INFO)

if args.block:
print('Ensure algod is compiled with `runtime.SetBlockProfileRate()` set')

if args.mutex:
print('Ensure algod is compiled with `runtime.SetMutexProfileFraction()` set')

for nre in args.tf_name_re:
try:
# do re.compile just to check
Expand Down

0 comments on commit 2f4250a

Please sign in to comment.