Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4356388ybv; Tue, 25 Feb 2020 18:31:00 -0800 (PST) X-Google-Smtp-Source: APXvYqyw5Y/p6JwVE7eesoqVTuVBeWRS93CYLG1knYcqLyeFODYyXGV3M1NzIwv8RZboNLYP/cTQ X-Received: by 2002:a9d:24c8:: with SMTP id z66mr1331922ota.52.1582684260160; Tue, 25 Feb 2020 18:31:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582684260; cv=none; d=google.com; s=arc-20160816; b=oqcAvkf0eOC7S28rNtZrs6u2HAsHoAVJg+Dyz2EEoILeXbQsHb8kKC9ymsZmvW87jo BVdv1//F4ZOhG0jE38qMooesTYY5/5WAjmHCEv3A0Z5+LoLimsGUYvzsSayex9YGIo8Q szIZpo4QSihLrWooEZchYFVh8Ieeum/d9PjHjm71hm5FG6fkPokO4YDkY1Ff0gNNAT1f fAnO0kx+q7KNqeOLnq5z2IlRZkd+tDVX9RzFzxsdXcUWPEVnTeGMYqIg8cVz28wqvA+t /ovCRl4NSY0KdpwKskD7IYCuTZzGGAbe+FaW8qLrgDcA6grbeB60vtuuYqtN1oV5gnVt zskQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=XL1znedA4fMtEGxLGk7NjP9zj307A8lQsc4L+MICY30=; b=nUz08XsNUfbLdVZqYXA3Bu5lTBLIXqYaF6HsYzu37J8jZEiEoEKZojtoMjd4C6drga lhyGQuWZQ4VGHSl9sB9g0jZ1rh7WSSXt/HF6hTJCv/EJB+4FiQLbSyX01mnve8F6YwR/ mt7l5yjbUNlcYSK2f0reYVUyaGdC2smwkl5YhEEKMWUUlED1GnUvC6xH534Tb4xR305y a2FG+uyhLkSwx1pRs7OBNoVBpwyN1T532pyHUF35jUPuCVlBkRltQZcTEpqemDkH5ZFg lKGB2d+CvyWURWquLPsgobOt+Y31FSf+UEwbrfLvn8OvWsF+pAgyaWGOQDvvw8lh4TJi hAvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=oVBcjrQC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r10si403902otk.83.2020.02.25.18.30.47; Tue, 25 Feb 2020 18:31:00 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=oVBcjrQC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729795AbgBZCag (ORCPT + 99 others); Tue, 25 Feb 2020 21:30:36 -0500 Received: from mail-pg1-f201.google.com ([209.85.215.201]:54553 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727880AbgBZCag (ORCPT ); Tue, 25 Feb 2020 21:30:36 -0500 Received: by mail-pg1-f201.google.com with SMTP id l17so752153pgh.21 for ; Tue, 25 Feb 2020 18:30:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=XL1znedA4fMtEGxLGk7NjP9zj307A8lQsc4L+MICY30=; b=oVBcjrQCI6WtKZf/BcZxDjvhXasLSMhheKBBVX16ir2pnUsfWSJGpJsyILGnSpvH40 O79uROStFMycaKQNEF8ovTfet5AeILP2OBb+nDPYveoT/IYd7QQA5sxkgyT/tumiYfF7 n3JUPHDP/mROnjQbiCEqc0pZ7tbei8Cp+hHQuydEQMpPWUkcj2xQ9rh/yJHVUSSg73Rn IDkPRItHhmCA0ld87NoM447njTSXubyqLNfE5VWFwrcgo1jXu/hTgu4q4Lv6pDj9Qae1 lJNeXOBO5NhFLjQsMZywpoZZ9cFPlT9Qb7Fd0FtGhXIkKKvldour9QH+fWi4oaYpqUWt qV6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=XL1znedA4fMtEGxLGk7NjP9zj307A8lQsc4L+MICY30=; b=uc5R+UJ0Cn88ZTy37Fm+WB+e0CSrr6HikdUsZ3E+18wPXM3aWc73nKd+kCYLN/eAzk tAuJ0sYIbrw15HEFmNMAXsgM2ngK8NVOyLHSp94qe9oeEVDzFKqR+5BJHOrFAcWsOLa8 RfkEmK4CK4IWpz++av62a/lOiFQaAVWxtMXN5A8FWis36v7vxKBtm9BcxNQdSeEfyqf8 oeb14VgG8MRFMgWJe8jI66/0eYecOoW5ztJewU7962AzNxpkCgwl/AklwezWbuPfn5OU 7xsX9yTGwYW7BFULi2Tvu6+gpt9et9QlOksC46QvDH9oGkoBrgBLgGvFyali4eGAQNgd iGKg== X-Gm-Message-State: APjAAAXnVvktKVXUuIOKT4SPSuVaZmSERpQULmTcGj9HgHHrPAGGZKLt E1tsG61rsPUycauwytub3tb1oRa2LQQbHKeQDG2LX79LAXmZW/xoFinUq6gy4tzHMZLGhimHXfP 7zSIXb9zMCmOgSLCDzgSaUjhYAeJ37XOMV++ltRasF9EMkNAZ3Dk8sRoy2SfRos5az2bHJA== X-Received: by 2002:a63:aa07:: with SMTP id e7mr1499287pgf.90.1582684233286; Tue, 25 Feb 2020 18:30:33 -0800 (PST) Date: Tue, 25 Feb 2020 18:30:25 -0800 Message-Id: <20200226023027.218365-1-lrizzo@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH 0/2] quickstats, kernel sample collector From: Luigi Rizzo To: linux-kernel@vger.kernel.org, mhiramat@kernel.org, akpm@linux-foundation.org, gregkh@linuxfoundation.org, naveen.n.rao@linux.ibm.com, changbin.du@intel.com, ardb@kernel.org, rizzo@iet.unipi.it, pabeni@redhat.com, toke@redhat.com, hawk@kernel.org Cc: Luigi Rizzo Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patchset introduces a small library to collect per-cpu samples and accumulate distributions to be exported through debugfs. I have used this to trace the execution times of small sections of code (network stack functions, drivers and xdp processing, ...) or entire functions, or latencies (e.g. IPI dispatch, etc.) and the fine granularity helps identifying outliers, multimodal distributions, caching effects and generally help in performance analysis. Samples are 64-bit values that are aggregated into per-cpu logarithmic buckets with configurable density (typical sample collectors use one buckets for each power of 2, powers of 2, but that is very coarse and corresponds to 1 significant bit per value; in quickstats one can specify the number of significant bits, up to 5, which is useful for finer grain measurements). There are two ways to trace a block of code: manual annotations has the best performance at runtime and is done as follows: // create metric and entry in debugfs struct kstats *key = kstats_new("foo", frac_bits); ... // instrument the code u64 t0 = ktime_get_ns(); // about 20ns
t0 = ktime_get_ns() - t0; // about 20ns kstats_record(key, t0); // 5ns hot cache, 300ns cold This method has an accuracy of about 20-30ns (inherited from the clock) and an overhead with hot/cold cache as show above. Values can be read from debugfs in an easy to parse format # cat /sys/kernel/debug/kstats/foo ... slot 55 CPU 0 count 589 avg 480 p 0.027613 slot 55 CPU 1 count 18 avg 480 p 0.002572 slot 55 CPU 2 count 25 avg 480 p 0.003325 ... slot 55 CPUS 28 count 814 avg 480 p 0.002474 ... slot 97 CPU 13 count 1150 avg 20130 p 0.447442 slot 97 CPUS 28 count 152585 avg 19809 p 0.651747 ... And write to the file STOP, START, RESET executes the corresponding action echo RESET > /sys/kernel/debug/kstats/foo The second instrumentation mechanism uses kretrprobes or tracepoints and lets tracing be enabled or removed at runtime from the command line for any globally visible function echo trace some_function bits 3 > /sys/kernel/debug/kstats/_control Data are exported or controlled as abube. Accuracy is worse due to the presence of kretprobe trampolines: 90ns with hot cache, 500ns with cold cache. The overhead on the traced function is 250ns hot, 1500ns cold. Hope you find this useful Luigi Rizzo (2): quickstats, kernel sample collector quickstats: user commands to trace execution time of code include/linux/kstats.h | 61 ++++ lib/Kconfig.debug | 7 + lib/Makefile | 1 + lib/kstats.c | 659 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 728 insertions(+) create mode 100644 include/linux/kstats.h create mode 100644 lib/kstats.c -- 2.25.0.265.gbab2e86ba0-goog