From patchwork Mon Nov 3 15:04:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yogesh Tillu X-Patchwork-Id: 5217351 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B5F62C11AC for ; Mon, 3 Nov 2014 15:11:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DE7E32015E for ; Mon, 3 Nov 2014 15:11:14 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 83FCF2012B for ; Mon, 3 Nov 2014 15:11:13 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XlJFU-0004gh-No; Mon, 03 Nov 2014 15:08:52 +0000 Received: from mail-pa0-f50.google.com ([209.85.220.50]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XlJFP-0004cA-Te for linux-arm-kernel@lists.infradead.org; Mon, 03 Nov 2014 15:08:48 +0000 Received: by mail-pa0-f50.google.com with SMTP id eu11so12412900pac.9 for ; Mon, 03 Nov 2014 07:08:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4aUxEuqaYYEworOOIAfUxWSiKURY8DLKIljFteXL68A=; b=MgvIc4/yAnI3/C8oymUzLFpiC1QeRWX3bU/Dwx9tn4RXdcjnBvS5XgGNUmUuYefbuj yhnFQcYRmzeIjOspbU/lzQ4v66bPtddPnnbwD9P6gdI1x/an7GkWg6+t1gULjnwA7fTr 4UqqyOTWxhdO5Eu4kzDZYWngQWEnwSXLFH/Y180/VXdUJCXhaft/11sXtUuSlBG3B0hD rAW+m3042LMx2lvNN6rzowK6ScddgTDM+jm1d+PDeDGfbVZWvUANjVe2loaGrAYSk2E4 gNZGea3+Z7cvexisqd3AhH/yE97ppsI/vKL93AAZ8sSiByYXTM8GrsgaGBm4m/na0WgL ktuQ== X-Gm-Message-State: ALoCoQn+bvw/lcuV2K/SV8Kyp0jYzDTNxy5UdUj9YXZvP4PjQhbSoULM7B02U/GOo+/RdCeG96IZ X-Received: by 10.70.37.8 with SMTP id u8mr24886409pdj.9.1415027306946; Mon, 03 Nov 2014 07:08:26 -0800 (PST) Received: from yogesh-Dell-System-Vostro-3360.Airtel4Grouter.cpe ([14.140.2.178]) by mx.google.com with ESMTPSA id mp5sm17413005pbc.33.2014.11.03.07.08.21 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 03 Nov 2014 07:08:25 -0800 (PST) From: Yogesh Tillu To: linux-arm-kernel@lists.infradead.org Subject: [RFC PATCH 1/5] Application: reads perf cycle counter using perf_event_open syscall Date: Mon, 3 Nov 2014 20:34:01 +0530 Message-Id: <1415027045-6573-2-git-send-email-yogesh.tillu@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1415027045-6573-1-git-send-email-yogesh.tillu@linaro.org> References: <1415027045-6573-1-git-send-email-yogesh.tillu@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141103_070847_987686_6C165DEE X-CRM114-Status: GOOD ( 13.30 ) X-Spam-Score: -0.7 (/) Cc: magnus.karlsson@avagotech.com, tillu.yogesh@gmail.com, Prasun.Kapoor@caviumnetworks.com, linux-perf-users@vger.kernel.org, Andrew.Pinski@caviumnetworks.com, mike.holmes@linaro.org, ola.liljedahl@linaro.org, Yogesh Tillu , linaro-networking@linaro.org, jean.pihet@linaro.org, arnd@linaro.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patchset is for application reading perf cycle counter using syscall perf_event_open. Signed-off-by: Yogesh Tillu --- app_readcounter.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 app_readcounter.c diff --git a/app_readcounter.c b/app_readcounter.c new file mode 100644 index 0000000..5363dd4 --- /dev/null +++ b/app_readcounter.c @@ -0,0 +1,83 @@ +/* +Application to Read perf cycle counter using perf_event_open syscall + +To Run: pass randon number to create busy loop +e.g.: $./app_readcounter 64 + +*/ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +static int fddev = -1; +__attribute__((constructor)) static void +init(void) +{ + static struct perf_event_attr attr; + attr.type = PERF_TYPE_HARDWARE; + attr.config = PERF_COUNT_HW_CPU_CYCLES; + fddev = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0); +} + +__attribute__((destructor)) static void +fini(void) +{ + close(fddev); +} + +static inline long long +cpucycles(void) +{ + long long result = 0; + if (read(fddev, &result, sizeof(result)) < sizeof(result)) return 0; + return result; +} + +/* Simple loop body to keep things interested. Make sure it gets inlined. */ +static inline int +loop(int* __restrict__ a, int* __restrict__ b, int n) +{ + unsigned sum = 0; + int i=0; + for ( i = 0; i < n; ++i) + if(a[i] > b[i]) + sum += a[i] + 5; + return sum; +} + +int +main(int ac, char **av) +{ + long long time_start = 0; + long long time_end = 0; + + int *a = NULL; + int *b = NULL; + int len = 0; + int i,sum = 0; + if (ac != 2) return -1; + len = atoi(av[1]); + printf("%s: len = %d\n", av[0], len); + + a = malloc(len*sizeof(*a)); + b = malloc(len*sizeof(*b)); + + for (i = 0; i < len; ++i) { + a[i] = i+128; + b[i] = i+64; + } + + printf("%s: beginning loop\n", av[0]); + time_start = cpucycles(); + sum = loop(a, b, len); + time_end = cpucycles(); + printf("%s: done. sum = %d; time delta = %llu\n", av[0], sum, time_end - time_start); + + free(a); free(b); + return 0; +}