All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mauro.chehab@linux.intel.com>
To: igt-dev@lists.freedesktop.org
Subject: [igt-dev] [PATCH v3 09/10] scripts:code_cov_gather_on_test: use a faster script
Date: Wed, 16 Mar 2022 16:00:02 +0100	[thread overview]
Message-ID: <20220316150003.1583681-10-mauro.chehab@linux.intel.com> (raw)
In-Reply-To: <20220316150003.1583681-1-mauro.chehab@linux.intel.com>

From: Tomi Sarvela <tomi.p.sarvela@intel.com>

The original shell script takes too long to complete (~7-10 seconds),
while the python version requires only ~500 ms.

As this has a relevant impact when doing the tests, use the faster
version.

Signed-off-by: Tomi Sarvela <tomi.p.sarvela@intel.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
---
 scripts/code_cov_gather_on_test.py | 91 ++++++++++++++++++++++++++++++
 scripts/code_cov_gather_on_test.sh | 20 -------
 2 files changed, 91 insertions(+), 20 deletions(-)
 create mode 100755 scripts/code_cov_gather_on_test.py
 delete mode 100755 scripts/code_cov_gather_on_test.sh

diff --git a/scripts/code_cov_gather_on_test.py b/scripts/code_cov_gather_on_test.py
new file mode 100755
index 000000000000..b4356aa1397b
--- /dev/null
+++ b/scripts/code_cov_gather_on_test.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0 OR MIT
+#
+# Copyright (C) 2022 Intel Corporation
+#
+# gather_on_test.py
+#
+# by Tomi Sarvela <tomi.p.sarvela@intel.com>
+#
+# Faster implementation for linux kernel GCOV data collection
+# Command line compatible with original gather_on_test.sh
+#
+# Refs:
+# https://www.kernel.org/doc/html/latest/dev-tools/gcov.html
+#
+import argparse
+import errno
+import io
+import os
+import sys
+import tarfile
+
+def parse_args() -> argparse.Namespace:
+    '''Command line arguments'''
+    ap = argparse.ArgumentParser(description="Gather Linux kernel GCOV data")
+    ap.add_argument('output',
+                    help="Output file name (.tar.gz will be added)")
+    ap.add_argument('--gcov', default='/sys/kernel/debug/gcov',
+                    help="GCOV directory, default: /sys/kernel/debug/gcov")
+    return ap.parse_args()
+
+def tar_add_link(tar:tarfile.TarFile, filename:str):
+    '''Add filename as symlink to tarfile'''
+    info = tarfile.TarInfo(filename)
+    if info.name[0] == '/': info.name = info.name[1:]
+    info.type = tarfile.SYMTYPE
+    info.linkname = os.readlink(filename)
+    tar.addfile(info)
+
+def tar_add_file(tar:tarfile.TarFile, filename:str):
+    '''Add filename to tarfile, file size expected to be invalid'''
+    try:
+        with open(filename, "rb") as fp:
+            data = fp.read() # big gulp
+    except OSError as e:
+        print(f"ERROR: {filename}: {e}", file=sys.stderr)
+        return
+    info = tarfile.TarInfo(filename)
+    if info.name[0] == '/': info.name = info.name[1:]
+    info.size = len(data)
+    tar.addfile(info, io.BytesIO(data))
+
+def tar_add_tree(tar:tarfile.TarFile, tree:str):
+    '''Add gcov files in directory tree to tar'''
+    # FIXME: should dirs be added to tar for compatibility?
+    for root, _, files in os.walk(tree, followlinks=False):
+        for file in files:
+            filepath = os.path.join(root, file)
+            if file.endswith('.gcda'): tar_add_file(tar, filepath)
+            if file.endswith('.gcno'): tar_add_link(tar, filepath)
+
+def main() -> int:
+    '''MAIN'''
+    if not os.path.isdir(args.gcov):
+        print(f"ERROR: [Errno {errno.ENOTDIR}] {os.strerror(errno.ENOTDIR)}: '{args.gcov}'",
+              file=sys.stderr)
+        return errno.ENOTDIR
+    if args.output == '-':
+        # reopen stdout as bytes for tarfile
+        fp = os.fdopen(sys.stdout.fileno(), "wb", closefd=False)
+    else:
+        if not args.output.endswith('.tgz') and \
+           not args.output.endswith('.tar.gz'):
+            args.output+='.tar.gz'
+        try:
+            fp = open(args.output, 'wb')
+        except OSError as e:
+            print(f"ERROR: {e}", file=sys.stderr)
+            return e.errno
+    with tarfile.open(fileobj=fp, mode='w:gz') as tar:
+        tar_add_tree(tar, args.gcov)
+    fp.close()
+    return 0
+
+if __name__ == '__main__':
+    try:
+        args = parse_args()
+        sys.exit(main())
+    except KeyboardInterrupt:
+        print("Interrupted", file=sys.stderr)
+        sys.exit(errno.EINTR)
diff --git a/scripts/code_cov_gather_on_test.sh b/scripts/code_cov_gather_on_test.sh
deleted file mode 100755
index 8834aa0d78af..000000000000
--- a/scripts/code_cov_gather_on_test.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash -e
-
-DEST=$1
-GCDA=/sys/kernel/debug/gcov
-
-if [ -z "$DEST" ] ; then
-  echo "Usage: $0 <output.tar.gz>" >&2
-  exit 1
-fi
-
-TEMPDIR=$(mktemp -d)
-echo Collecting data..
-find $GCDA -type d -exec mkdir -p $TEMPDIR/\{\} \;
-find $GCDA -name '*.gcda' -exec sh -c 'cat < $0 > '$TEMPDIR'/$0' {} \;
-find $GCDA -name '*.gcno' -exec sh -c 'cp -d $0 '$TEMPDIR'/$0' {} \;
-tar czf $DEST -C $TEMPDIR sys
-rm -rf $TEMPDIR
-
-echo "$DEST successfully created, copy to build system and unpack with:"
-echo "  tar xfz $DEST"
-- 
2.35.1

  parent reply	other threads:[~2022-03-16 15:03 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16 14:59 [igt-dev] [PATCH v3 00/10] Add support to collect code coverage data Mauro Carvalho Chehab
2022-03-16 14:59 ` [igt-dev] [PATCH v3 01/10] runner: check if it has root permissions Mauro Carvalho Chehab
2022-03-16 14:59 ` [igt-dev] [PATCH v3 02/10] runner: Add support for code coverage Mauro Carvalho Chehab
2022-03-16 14:59 ` [igt-dev] [PATCH v3 03/10] runner: cleanup code_cov directory, if any Mauro Carvalho Chehab
2022-03-16 14:59 ` [igt-dev] [PATCH v3 04/10] scripts/code_cov_gather*/sh: add help scripts for code coverage Mauro Carvalho Chehab
2022-03-16 14:59 ` [igt-dev] [PATCH v3 05/10] scripts/code_cov_gather_on_build.sh: Improve the script Mauro Carvalho Chehab
2022-03-16 14:59 ` [igt-dev] [PATCH v3 06/10] scripts/code_cov_capture.sh: add a script to use lcov on build+test machine Mauro Carvalho Chehab
2022-03-16 15:00 ` [igt-dev] [PATCH v3 07/10] scripts/code_cov_gen_report.sh: add a script to generate code coverage reports Mauro Carvalho Chehab
2022-03-16 15:00 ` [igt-dev] [PATCH v3 08/10] scripts/run-tests.sh: add code coverage support Mauro Carvalho Chehab
2022-03-16 15:00 ` Mauro Carvalho Chehab [this message]
2022-03-16 15:00 ` [igt-dev] [PATCH v3 10/10] docs: add documentation for code coverage Mauro Carvalho Chehab
2022-03-16 15:44 ` [igt-dev] ✓ Fi.CI.BAT: success for Add support to collect code coverage data Patchwork
2022-03-16 16:53 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220316150003.1583681-10-mauro.chehab@linux.intel.com \
    --to=mauro.chehab@linux.intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.