From patchwork Thu Jan 6 13:21:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12705414 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A66DEC433EF for ; Thu, 6 Jan 2022 13:22:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239441AbiAFNWM (ORCPT ); Thu, 6 Jan 2022 08:22:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230012AbiAFNWL (ORCPT ); Thu, 6 Jan 2022 08:22:11 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F23D6C061245 for ; Thu, 6 Jan 2022 05:22:10 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id i22so4689694wrb.13 for ; Thu, 06 Jan 2022 05:22:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=KfOBcjvG/FdMyYfeavYMJxanZ671mMYUw12xivxKaJg=; b=N9663iAjVQpsgXpETXhUd1KDSvwj1ksVKHiolxuugtmxEPjZffynyGReq0jx2M9nDR fB+AqF4liGtyh06lWFrmmcYJfeY4yDFq0OJ8tJ8Kf6+G0oTkWpNRzT5PbNE7IRYoV7HU 6SI3OZYTHBCxywGaZ66kimwIVBFMW3oqw+/ktwBBS+B1T9A2ar5WYKk7vbWSD05axvLn Mfa1A3BncI42WDui1MHdbtGTq7fMr+/VBDwup6dUTprjLIyFP6pv60alPqaEGlCeo3xw klxW6YmcQEmeTaa0mclrylfymK6BumKT+XWXv4eXm845ufWokzM3LQYItVfpeUSqEPpr URMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=KfOBcjvG/FdMyYfeavYMJxanZ671mMYUw12xivxKaJg=; b=FDfATqlNQQcsZgsvOxoSU94aSQoBgCQ/gbV4WrgqkPzpOKRs+2ifAtUHtCv3mUv9lP hggoW6VUsWZbuE/9f60k8vwxGoihaKbxN+72uhECV5jSXhGbk2HiXYgXcPMmNR9AopUG GTZMy5zLB9hyQW0VtkTEi+rnI8usHfE1hUC4Q6h2RfnSM8I5U7VbBjj5xiucyZ8cmuV8 KrHpK+xpsMofdrvqe7DYPEH7YUyEkIU4J4F4/sWIjq0p0Zds2yDSxPcuxiQF7d8NOL8n zxgkpSHh+AwSfIYgMgyHGy9Mu9in6wtaGEQd9wU1gEqdK/DhZ53QcZtK/diKJO1XyXn9 +Lmw== X-Gm-Message-State: AOAM531UeP999X/LhzCBkr05nvTurtlfa/oEZ4Zsx7rxguWj0LETeebl 1WbHXOodBVhXrG/BG4pyYYRu72Yw/gI= X-Google-Smtp-Source: ABdhPJz11ysuQWcJpnL2rKvZQR4c2R/YzLshoemkiTIRF9xJn+TNyrtINRlcmbCPfX2Z8k+i3XDTCQ== X-Received: by 2002:adf:fc50:: with SMTP id e16mr6456483wrs.554.1641475328952; Thu, 06 Jan 2022 05:22:08 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id o3sm2329283wry.98.2022.01.06.05.22.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 05:22:08 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 1/3] trace-cruncher: Add khist class to ft_utiles Date: Thu, 6 Jan 2022 15:21:54 +0200 Message-Id: <20220106132156.168538-1-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Define a Python class to be used for easy manipulation of kernel histograms. Signed-off-by: Yordan Karadzhov (VMware) --- tracecruncher/ft_utils.py | 124 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/tracecruncher/ft_utils.py b/tracecruncher/ft_utils.py index cae54b0..ed4bdcd 100644 --- a/tracecruncher/ft_utils.py +++ b/tracecruncher/ft_utils.py @@ -210,3 +210,127 @@ class kretval_probe(kprobe_base): """ self.kp = ft.register_kprobe(event=self.name, function=self.func); self.evt_id = find_event_id(system=ft.tc_event_system(), event=self.name) + + +class khist: + def __init__(self, name, event, axes, weights=[], + sort_keys=[], sort_dir={}, find=False): + """ Constructor. + """ + self.name = name + self.inst = None + + inst_name = name+'_inst' + if find: + self.inst = ft.find_instance(name=inst_name) + self.attached = False + else: + self.inst = ft.create_instance(name=inst_name) + self.attached = True + + self.hist = ft.hist(name=name, + system=event.system, + event=event.name, + axes=axes) + + for v in weights: + self.hist.add_value(value=v) + + self.hist.sort_keys(keys=sort_keys) + + for key, val in sort_dir.items(): + self.hist.sort_key_direction(sort_key=key, + direction=val) + + self.trigger = '{0}/events/{1}/{2}/trigger'.format(self.inst.dir(), + event.system, + event.name) + + if not find: + # Put the kernel histogram on 'standby' + self.hist.stop(self.inst) + + def __del__(self): + """ Destructor. + """ + if self.inst and self.attached: + self.clear() + + def start(self): + """ Start accumulating data. + """ + self.hist.resume(self.inst) + + def stop(self): + """ Stop accumulating data. + """ + self.hist.stop(self.inst) + + def resume(self): + """ Continue accumulating data. + """ + self.hist.resume(self.inst) + + def data(self): + """ Read the accumulated data. + """ + return self.hist.read(self.inst) + + def clear(self): + """ Clear the accumulated data. + """ + self.hist.clear(self.inst) + + def detach(self): + """ Detach the object from the Python module. + """ + ft.detach(self.inst) + self.attached = False + + def attach(self): + """ Attach the object to the Python module. + """ + ft.attach(self.inst) + self.attached = True + + def is_attached(self): + """ Check if the object is attached to the Python module. + """ + return self.attached + + def __repr__(self): + """ Read the descriptor of the histogram. + """ + with open(self.trigger) as f: + return f.read().rstrip() + + def __str__(self): + return self.data() + + +def create_khist(name, event, axes, weights=[], + sort_keys=[], sort_dir={}): + """ Create new kernel histogram. + """ + try: + hist = khist(name=name, event=event, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir, find=False) + except Exception as err: + msg = 'Failed to create histogram \'{0}\''.format(name) + raise RuntimeError(msg) from err + + return hist + + +def find_khist(name, event, axes, instance=None, + weights=[], sort_keys=[], sort_dir={}): + """ Find existing kernel histogram. + """ + try: + hist = khist(name=name, event=event, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir, find=True) + except Exception as err: + msg = 'Failed to find histogram \'{0}\''.format(name) + raise RuntimeError(msg) from err + + return hist From patchwork Thu Jan 6 13:21:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12705415 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7342C433FE for ; Thu, 6 Jan 2022 13:22:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230012AbiAFNWM (ORCPT ); Thu, 6 Jan 2022 08:22:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239292AbiAFNWL (ORCPT ); Thu, 6 Jan 2022 08:22:11 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5948BC061201 for ; Thu, 6 Jan 2022 05:22:11 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id w20so4720145wra.9 for ; Thu, 06 Jan 2022 05:22:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+CktRCeP2Ji58eq8z/vedUgZVGQkN6VSq8zNBfkzYQA=; b=qH2lbDsptWEg/oe9GKUZDQB/4OtVcl/veC8AmSazWdeZuOqzT4/FWYNBVh3fhZtWyK fb0bsuOn0GwIXDYAGXzSrOfOfbzfWLUR+StBtqLtwI5lSMQgTm8RCV39YUyNv5pChu2s cj+KxH3nfqHCwYiZCC5tvNdkqVBLh9qH68hV6dzK8lw4OJmMWRM1skZa/rUmlBeTd+/j Hb1rmNEESGuKK6uRYn6301FiL8a+my2PhWD6s3tA4sCcehIz5RPBsYnhn4Vf0YI3JOo7 czXsAytMTIa11JZtIfEZcRZlvSnCVO6Ik3xHUbbpNOhgk6+Nj/NdWj4R7fdzfEZR0kxK Xitw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+CktRCeP2Ji58eq8z/vedUgZVGQkN6VSq8zNBfkzYQA=; b=WEIpFtNsPV33JRT/j17oFCDwv6/4tAj2MdmuYwF5nTN47F5CafgOuwSDqpsl0iD0q0 mj2pWrWLZTgJaFez0ds35XBVhb2sZEe/Gb/j6ilazRQikgoZiRnkhAGQfBRTWiuh3QqE 4mCn3VT28LeZvxToF80zbG7qvwoDkoBQoEEorws1Yi8nNDO9jb7+n30mn2dTd90e+Pf+ WOlSzUDk9HiPcxnYHZunqU6Xo58ZLsAcVLEd3o6PVD5YnE5129Xqz0XJtWCtJMmGpZA4 wuChH5a45LuvOfHw21PpmVUzkjOYTG6Ius6/1rWOKtTgjqOUvMPiTfcr02bXhXfecp7c soEw== X-Gm-Message-State: AOAM5326Wz0XeCEBCc02hvTpXmwznaS8kXN2ONPt5T3txCdIDOgrUQrH EL0ppduJl42yidZS7IFvr2VjQwv7Ekg= X-Google-Smtp-Source: ABdhPJyEMVJ3+Y7qkbvl8hyhmBSHVoFLAL+2uOFH9jM82AmBMlVDS9VSN3DvHo8xdixI77OgXQYpKA== X-Received: by 2002:a5d:588f:: with SMTP id n15mr51737370wrf.279.1641475329800; Thu, 06 Jan 2022 05:22:09 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id o3sm2329283wry.98.2022.01.06.05.22.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 05:22:09 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 2/3] trace-cruncher: Add example for the high-level histogram APIs Date: Thu, 6 Jan 2022 15:21:55 +0200 Message-Id: <20220106132156.168538-2-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220106132156.168538-1-y.karadz@gmail.com> References: <20220106132156.168538-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org This is a very basic example, demonstrating the usage of the new high-level (OOP) APIs for kernel histograms. Signed-off-by: Yordan Karadzhov (VMware) --- examples/hist_oop.py | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 examples/hist_oop.py diff --git a/examples/hist_oop.py b/examples/hist_oop.py new file mode 100644 index 0000000..1a3aab6 --- /dev/null +++ b/examples/hist_oop.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +""" +SPDX-License-Identifier: CC-BY-4.0 + +Copyright 2021 VMware Inc, Yordan Karadzhov (VMware) +""" + +import sys +import time + +import tracecruncher.ft_utils as tc + +name = 'khist_example_oop' + +cmds = ['start', 'stop', 'show', 'continue', 'clear', 'close'] + +evt = tc.event('kmem', 'kmalloc') + +axes={'call_site': 'sym', + 'bytes_req': 'n'} + +weights=['bytes_alloc'] + +sort_keys=['bytes_req', 'bytes_alloc'] + +sort_dir={'bytes_req': 'desc'} + +if __name__ == "__main__": + if len(sys.argv) != 2: + sys.exit(1) + + if not sys.argv[1].isdigit() and not sys.argv[1] in cmds: + sys.exit(1) + + arg1 = sys.argv[1] + if arg1.isdigit() or arg1 == 'start': + # Create the kernel tracing histogram. + hist = tc.create_khist(name=name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + # Start taking data. + hist.start() + + if arg1.isdigit(): + # Take data for a while, then stop, print the result and exit. The + # trace-cruncher module will take care for clearing and destroying + # the histogram in the kernel. + time.sleep(int(arg1)) + hist.stop() + print(hist.data()) + + else: + # Detach the 'hist' object from the trace-cruncher module. This will + # prevent the kernel histogram from being destroyed when the module + # is closed (at exit). + hist.detach() + + else: + # Try to find an existing histogram with the same definition. + # The returned histogram is detached from the trace-cruncher module. + hist = tc.find_khist(name=name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + + if arg1 == 'stop': + # Stop taking data. + hist.stop() + elif arg1 == 'show': + # Print the collected data. + print(hist.data()) + elif arg1 == 'continue': + # Continue taking data. + hist.resume() + elif arg1 == 'clear': + # Reset the histogram. + hist.clear() + + if arg1 == 'close': + # Attach the 'hist' object to the trace-cruncher module. This will + # ensure that the kernel histogram will be destroyed when the + # module is closed (at exit). + hist.attach() From patchwork Thu Jan 6 13:21:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 12705416 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B1D9C433F5 for ; Thu, 6 Jan 2022 13:22:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239292AbiAFNWM (ORCPT ); Thu, 6 Jan 2022 08:22:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239473AbiAFNWM (ORCPT ); Thu, 6 Jan 2022 08:22:12 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14C18C061245 for ; Thu, 6 Jan 2022 05:22:12 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id l4so1744493wmq.3 for ; Thu, 06 Jan 2022 05:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qSnaugyGMhNHqebSmy+Y2bS9w/sj3/iXhI+kuKCIRYk=; b=dMyMB1AzBl6HjuM8psY8rxc5Q+TOoIcjghPrL8jQ1jQJE5fSxgbCUGFzdof/LOm8C3 5wNf3AEaW7lYxsRJpyRUq1FuamqA/chE3mVm1TqD3gTuP4BwQVM2NIvfeKwSYelM9WAO 2yHRgh0UvmKkg1CRw0/4i/DV0lQKL/31tWkhoykDeGIjEfNfbSF/KtCzOVCnG0jU60Ku uN4GYyEB+nqECdRi9WDO2ZomoDPiCqTBtvA0dJwPDvdpXFJZEG7FLyyc/N+BYoTBBpa2 eqNxs58834HBwQ/M+2OsUG6KOXAp0Uh6h4wx10kqGIk7XHCyWKlQnwqp/0qXw8WFfxig s1QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qSnaugyGMhNHqebSmy+Y2bS9w/sj3/iXhI+kuKCIRYk=; b=DI72Z7aM7tLSnDpYTEuIFf7KVoADj73dPdvNUXLhlVzUMRf8FzzawO0AMwmDwD70ZF nDWsKYsjHMnbkOakWNBECGofwTokUGSyM1FcFwxv2dGdXYjlC0m1b/1D64tIoMdJqebW UWD94wx6t+nSpS9jtSlA5IfLQ3ebE17tICmse7+rDDWPH7qy7hbaY2DVjNEitEs6glzA oZ135RWxW61BMZ1Hul1HBPB1PLrcNADj1L/h2K9xztBI47rN7yT+vdvqlRxVJ9FPUNVd eUCZ8U6zxrHYmqEBpivrk+NGyBZYXvnyXXIUm5wN5/CGGVjDS+fhSBdTedshMaHBkllx 4YQA== X-Gm-Message-State: AOAM532Bi+ORudPl8kAOlfncQd+CPUS79bW7rXn6PjDukLKtqR9J3xKe g1e1VAG8aDE8SAb6crlgBeVLs5fp4EM= X-Google-Smtp-Source: ABdhPJwMYV68Roui5D6NONU1LHSSVKztGZqsA7iS06F8lOxs/MjqWOaDk3xf0COUuuXwV4vNEKUP7g== X-Received: by 2002:a1c:a517:: with SMTP id o23mr6903079wme.189.1641475330531; Thu, 06 Jan 2022 05:22:10 -0800 (PST) Received: from crow.. ([95.87.219.163]) by smtp.gmail.com with ESMTPSA id o3sm2329283wry.98.2022.01.06.05.22.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 05:22:10 -0800 (PST) From: "Yordan Karadzhov (VMware)" To: linux-trace-devel@vger.kernel.org Cc: "Yordan Karadzhov (VMware)" Subject: [PATCH 3/3] trace-cruncher: Add tests for the khist class Date: Thu, 6 Jan 2022 15:21:56 +0200 Message-Id: <20220106132156.168538-3-y.karadz@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220106132156.168538-1-y.karadz@gmail.com> References: <20220106132156.168538-1-y.karadz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Testing the high-level Python APIs for kernel histograms. Signed-off-by: Yordan Karadzhov (VMware) --- .../tests/1_unit/test_01_ftracepy_unit.py | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py b/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py index 7e5b20e..87b2be8 100644 --- a/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py +++ b/tracecruncher/tests/1_unit/test_01_ftracepy_unit.py @@ -10,6 +10,7 @@ import os import sys import unittest import tracecruncher.ftracepy as ft +import tracecruncher.ft_utils as tc instance_name = 'test_instance1' another_instance_name = 'test_instance2' @@ -600,5 +601,95 @@ class HistTestCase(unittest.TestCase): hist.close(inst) +hist_name = 'h2d' +evt_sys = 'kmem' +evt_name = 'kmalloc' + +axes={'call_site': 'sym', + 'bytes_req': 'n'} +weights=['bytes_alloc'] +sort_keys=['bytes_req', 'bytes_alloc'] +sort_dir={'bytes_req': 'desc'} + +tgr_file = '{0}/instances/{1}_inst/events/{2}/{3}/trigger'.format(ft.dir(), hist_name, evt_sys, evt_name) + +class HistOopTestCase(unittest.TestCase): + def test_hist_create(self): + evt = tc.event(evt_sys, evt_name) + hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + f = open(tgr_file) + h_buff = f.read() + self.assertTrue('hist:name=h2d' in h_buff) + self.assertTrue(':keys=call_site.sym,bytes_req' in h_buff) + self.assertTrue(':vals=hitcount,bytes_alloc' in h_buff) + self.assertTrue(':sort=bytes_req.descending,bytes_alloc' in str(h_buff)) + + f.close() + + def test_hist_ctrl(self): + evt = tc.event(evt_sys, evt_name) + hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + f = open(tgr_file) + h_buff = f.read() + self.assertTrue('[paused]' in h_buff) + + hist.start() + f.seek(0) + h_buff = f.read() + self.assertTrue('[active]' in h_buff) + + hist.stop() + f.seek(0) + h_buff = f.read() + self.assertTrue('[paused]' in h_buff) + + hist.resume() + f.seek(0) + h_buff = f.read() + self.assertTrue('[active]' in h_buff) + + h_buff = hist.data() + self.assertTrue('Totals:' in h_buff) + + hist.stop() + hist.clear() + f.seek(0) + h_buff = f.read() + self.assertTrue('[paused]' in h_buff) + h_buff = hist.data() + self.assertTrue('Hits: 0' in h_buff) + self.assertTrue('Entries: 0' in h_buff) + self.assertTrue('Dropped: 0' in h_buff) + + f.close() + + def test_1_detach(self): + evt = tc.event(evt_sys, evt_name) + hist = tc.create_khist(name=hist_name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + self.assertTrue(hist.is_attached()) + hist.detach() + self.assertFalse(hist.is_attached()) + + def test_2_attach(self): + evt = tc.event(evt_sys, evt_name) + hist = tc.find_khist(name=hist_name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + self.assertFalse(hist.is_attached()) + hist.attach() + self.assertTrue(hist.is_attached()) + + def test_hist_err(self): + evt = tc.event(evt_sys, evt_name) + + err = 'Failed to find histogram' + with self.assertRaises(Exception) as context: + hist = tc.find_khist(name=hist_name, event=evt, axes=axes, weights=weights, + sort_keys=sort_keys, sort_dir=sort_dir) + self.assertTrue(err in str(context.exception)) + + if __name__ == '__main__': unittest.main()