From patchwork Fri May 5 10:38:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 98617 Delivered-To: patch@linaro.org Received: by 10.140.96.100 with SMTP id j91csp52860qge; Fri, 5 May 2017 03:48:11 -0700 (PDT) X-Received: by 10.200.36.131 with SMTP id s3mr45670235qts.289.1493981291782; Fri, 05 May 2017 03:48:11 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id c186si4201174qkd.44.2017.05.05.03.48.11 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 05 May 2017 03:48:11 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:46214 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6amP-00021g-Cd for patch@linaro.org; Fri, 05 May 2017 06:48:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52636) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6acT-0001of-Ml for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6acS-0006ol-KJ for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:53 -0400 Received: from mail-wm0-x233.google.com ([2a00:1450:400c:c09::233]:35415) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d6acS-0006nD-BJ for qemu-devel@nongnu.org; Fri, 05 May 2017 06:37:52 -0400 Received: by mail-wm0-x233.google.com with SMTP id w64so20094989wma.0 for ; Fri, 05 May 2017 03:37:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ktf4cSj6hYYBwLb7A+y78uFZPtRBL2oqzEnVaZhJNgM=; b=eBonx0OpItGlfYWfbdBhThcRqYcQtu9CuJzs1oQtB6nagcd0/JGBYSMNtqIEchwzxJ dDs+JaF9MHNbKVfse6XLVz7zhOe192ImZV7CPvmP9Pr70Z8+Nad573NDuWtwmliwDEan 2D7cJf8Iv92sM4jPaJHAhfV67cfL6Lfa2iLOA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ktf4cSj6hYYBwLb7A+y78uFZPtRBL2oqzEnVaZhJNgM=; b=J3wLhLydo41Ep1xn/+KPtpUMNFcMd8jwqTl67KSSRlps3pSqY/Jowr6vK4KIQY8KrA zedx6S7umQbRvd3ODw+IgvWFCI2JvCVRWkHl5GZde6jVDOvhGerGVeUaUFVw9IheINcQ 99g9QXUP4HQqoyJuE2cDS1wnJLTt4X53ZzINDxOZ6+mytT0trsV5937B15rbg6nOH0bX esdsMQOSkqbSkqo4Jg4gePOpeZn7eH+wAM/wXhJP4GQBhk4dklJlLmhbPoucPAiUfFXl tmnMsvCA6rFFCN0MnnCmRH3sv62gl3OJNQDKHv6ejRsKrlu3kYwr8vvMLx3IzfNwtK5T rKww== X-Gm-Message-State: AODbwcAok3gnDb/jXvb76d25aOZlvEkzujEOyJ8+gr7KaMGK2NMxpgEJ 2/633xJ5/0eLpJXV X-Received: by 10.28.161.68 with SMTP id k65mr2588441wme.134.1493980671209; Fri, 05 May 2017 03:37:51 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id 72sm2062449wmx.23.2017.05.05.03.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 May 2017 03:37:48 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 5D7353E024F; Fri, 5 May 2017 11:38:23 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: pbonzini@redhat.com, boost.lists@gmail.com, pavel.dovgaluk@ispras.ru Date: Fri, 5 May 2017 11:38:22 +0100 Message-Id: <20170505103822.20641-10-alex.bennee@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170505103822.20641-1-alex.bennee@linaro.org> References: <20170505103822.20641-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::233 Subject: [Qemu-devel] [RFC PATCH v1 9/9] scripts/analyse-locks-simpletrace.py: script to analyse lock times X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: cota@braap.org, =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This script allows analysis of mutex acquisition and hold times based on a trace file. Given a trace control file of: qemu_mutex_lock qemu_mutex_locked qemu_mutex_unlock And running with: $QEMU $QEMU_ARGS -trace events=./lock-trace You can analyse the results with: ./scripts/analyse-locks-simpletrace.py trace-events-all ./trace-21812 Signed-off-by: Alex Bennée --- scripts/analyse-locks-simpletrace.py | 99 ++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100755 scripts/analyse-locks-simpletrace.py -- 2.11.0 diff --git a/scripts/analyse-locks-simpletrace.py b/scripts/analyse-locks-simpletrace.py new file mode 100755 index 0000000000..b72c951cd9 --- /dev/null +++ b/scripts/analyse-locks-simpletrace.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Analyse lock events and +# +# Author: Alex Bennée +# + +import os +import simpletrace +import argparse +import numpy as np + +class MutexAnalyser(simpletrace.Analyzer): + "A simpletrace Analyser for checking locks." + + def __init__(self): + self.locks = 0 + self.locked = 0 + self.unlocks = 0 + self.mutex_records = {} + + def _get_mutex(self, mutex): + if not mutex in self.mutex_records: + self.mutex_records[mutex] = {"locks": 0, + "lock_time": 0, + "acquire_times": [], + "locked": 0, + "locked_time": 0, + "held_times": [], + "unlocked": 0} + + return self.mutex_records[mutex] + + def qemu_mutex_lock(self, timestamp, mutex, filename, line): + self.locks += 1 + rec = self._get_mutex(mutex) + rec["locks"] += 1 + rec["lock_time"] = timestamp[0] + rec["lock_loc"] = (filename, line) + + def qemu_mutex_locked(self, timestamp, mutex, filename, line): + self.locked += 1 + rec = self._get_mutex(mutex) + rec["locked"] += 1 + rec["locked_time"] = timestamp[0] + acquire_time = rec["locked_time"] - rec["lock_time"] + rec["locked_loc"] = (filename, line) + rec["acquire_times"].append(acquire_time) + + def qemu_mutex_unlock(self, timestamp, mutex, filename, line): + self.unlocks += 1 + rec = self._get_mutex(mutex) + rec["unlocked"] += 1 + held_time = timestamp[0] - rec["locked_time"] + rec["held_times"].append(held_time) + rec["unlock_loc"] = (filename, line) + + +def get_args(): + "Grab options" + parser = argparse.ArgumentParser() + parser.add_argument("--output", "-o", type=str, help="Render plot to file") + parser.add_argument("events", type=str, help='trace file read from') + parser.add_argument("tracefile", type=str, help='trace file read from') + return parser.parse_args() + +if __name__ == '__main__': + args = get_args() + + # Gather data from the trace + analyser = MutexAnalyser() + simpletrace.process(args.events, args.tracefile, analyser) + + print ("Total locks: %d, locked: %d, unlocked: %d" % + (analyser.locks, analyser.locked, analyser.unlocks)) + + # Now dump the individual lock stats + for key, val in sorted(analyser.mutex_records.iteritems(), + key=lambda (k,v): v["locks"]): + print ("Lock: %#x locks: %d, locked: %d, unlocked: %d" % + (key, val["locks"], val["locked"], val["unlocked"])) + + acquire_times = np.array(val["acquire_times"]) + if len(acquire_times) > 0: + print (" Acquire Time: min:%d median:%d avg:%.2f max:%d" % + (acquire_times.min(), np.median(acquire_times), + acquire_times.mean(), acquire_times.max())) + + held_times = np.array(val["held_times"]) + if len(held_times) > 0: + print (" Held Time: min:%d median:%d avg:%.2f max:%d" % + (held_times.min(), np.median(held_times), + held_times.mean(), held_times.max())) + + # Check if any locks still held + if val["locks"] > val["locked"]: + print (" LOCK HELD (%s:%s)" % (val["locked_loc"])) + print (" BLOCKED (%s:%s)" % (val["lock_loc"]))