From patchwork Fri Aug 9 08:45:38 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sunshaojie X-Patchwork-Id: 18908 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-gh0-f198.google.com (mail-gh0-f198.google.com [209.85.160.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B17BB2391D for ; Fri, 9 Aug 2013 08:45:57 +0000 (UTC) Received: by mail-gh0-f198.google.com with SMTP id r13sf4572958ghr.1 for ; Fri, 09 Aug 2013 01:45:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-gm-message-state:delivered-to:from:to:cc:subject :date:message-id:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=yeucnveXxGWZB6MenDgfmDpOYvmm1xmmY9DCshJNC3o=; b=iFjKe8nFtUeWp+qg9PVbvplxZyoG37MeikDbgdqo+ZbLzcrKZqwm2kIuzyz2+MFRWx i4vOBhkgMCTsJk7arn/3+EdjV6sKxnQXhjTPRaT+Ey/PG3m+HsmEYG2TjEsKskS6kTAP Qc04Ftn0IA7cvDTAWhHeS2Tt3isR1xCMJfKlVax1uT44OOgAsNm8DBZtEysNBsvkzXL8 D5UroIHliRW7FRyWuh544rs1pkis3+3F+J2xtQwI9p8WeR+3xo70UmZXFS89eMOvJs64 DMma3lQm74X9rkPMXGhVyEX+loXwy0BaTwLygvpg7yT8Vv5dADb6jgsjjzmAon/+F2gI AffA== X-Received: by 10.236.111.40 with SMTP id v28mr4961415yhg.27.1376037956925; Fri, 09 Aug 2013 01:45:56 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.18.199 with SMTP id y7ls1377235qed.78.gmail; Fri, 09 Aug 2013 01:45:56 -0700 (PDT) X-Received: by 10.58.34.178 with SMTP id a18mr5175301vej.86.1376037956756; Fri, 09 Aug 2013 01:45:56 -0700 (PDT) Received: from mail-vc0-f173.google.com (mail-vc0-f173.google.com [209.85.220.173]) by mx.google.com with ESMTPS id bq2si4355172vcb.115.2013.08.09.01.45.56 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 01:45:56 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.173 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.173; Received: by mail-vc0-f173.google.com with SMTP id id13so416192vcb.18 for ; Fri, 09 Aug 2013 01:45:56 -0700 (PDT) X-Gm-Message-State: ALoCoQkscaDjqW6iERC6zFHHi3/cb1ltZetSkCksF9cymWZujvuA20+Vv2tkOZOwh4T2EOrggb8d X-Received: by 10.52.243.201 with SMTP id xa9mr4234091vdc.106.1376037956442; Fri, 09 Aug 2013 01:45:56 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp61672vcz; Fri, 9 Aug 2013 01:45:55 -0700 (PDT) X-Received: by 10.68.254.73 with SMTP id ag9mr10342898pbd.54.1376037955184; Fri, 09 Aug 2013 01:45:55 -0700 (PDT) Received: from mail-pb0-f45.google.com (mail-pb0-f45.google.com [209.85.160.45]) by mx.google.com with ESMTPS id c10si12620672pav.171.2013.08.09.01.45.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 01:45:55 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.160.45 is neither permitted nor denied by best guess record for domain of shaojie.sun@linaro.org) client-ip=209.85.160.45; Received: by mail-pb0-f45.google.com with SMTP id mc17so4416424pbc.32 for ; Fri, 09 Aug 2013 01:45:54 -0700 (PDT) X-Received: by 10.66.50.69 with SMTP id a5mr10387863pao.150.1376037954327; Fri, 09 Aug 2013 01:45:54 -0700 (PDT) Received: from localhost.localdomain ([58.251.159.202]) by mx.google.com with ESMTPSA id qh10sm18877089pbb.33.2013.08.09.01.45.50 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 01:45:53 -0700 (PDT) From: Shaojie Sun To: daniel.lezcano@linaro.org Cc: private-pmwg@linaro.org, patches@linaro.org, sunshaojie@huawei.com, Shaojie Sun Subject: [PATCH 1/2] Adjust codes to different files. Date: Fri, 9 Aug 2013 16:45:38 +0800 Message-Id: <1376037938-15418-1-git-send-email-shaojie.sun@linaro.com> X-Mailer: git-send-email 1.7.9.5 X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: shaojie.sun@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.173 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Signed-off-by: Shaojie Sun --- Makefile | 4 +-- idlestat.c | 106 +++--------------------------------------------------------- trace.c | 51 +++++++++++++++++++++++++++++ trace.h | 21 ++++++++++++ utils.c | 55 +++++++++++++++++++++++++++++++ utils.h | 9 ++++++ 6 files changed, 143 insertions(+), 103 deletions(-) create mode 100644 trace.c create mode 100644 trace.h create mode 100644 utils.c create mode 100644 utils.h diff --git a/Makefile b/Makefile index 16f0267..05c3f4f 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ CFLAGS?=-g -Wall CC?=gcc -OBJS = idlestat.o +OBJS = idlestat.o trace.o utils.o default: idlestat -idledebug: $(OBJS) +idlestat: $(OBJS) $(CC) ${CFLAGS} $(OBJS) -lncurses -o $@ clean: diff --git a/idlestat.c b/idlestat.c index da1c761..069a1f3 100644 --- a/idlestat.c +++ b/idlestat.c @@ -12,6 +12,9 @@ #include #include +#include "utils.h" +#include "trace.h" + #define BUFSIZE 256 #define MAXCSTATE 8 #define MAX(A,B) (A > B ? A : B) @@ -458,63 +461,8 @@ int getoptions(int argc, char *argv[], struct idledebug_options *options) return 0; } -#define TRACE_PATH "/sys/kernel/debug/tracing" -#define TRACE_ON_PATH TRACE_PATH "/tracing_on" -#define TRACE_BUFFER_SIZE_PATH TRACE_PATH "/buffer_size_kb" -#define TRACE_BUFFER_TOTAL_PATH TRACE_PATH "/buffer_total_size_kb" -#define TRACE_CPUIDLE_EVENT_PATH TRACE_PATH "/events/power/cpu_idle/enable" -#define TRACE_EVENT_PATH TRACE_PATH "/events/enable" -#define TRACE_FREE TRACE_PATH "/free_buffer" -#define TRACE_FILE TRACE_PATH "/trace" -#define TRACE_IDLE_NRHITS_PER_SEC 10000 -#define TRACE_IDLE_LENGTH 196 - -static int write_int(const char *path, int val) -{ - FILE *f; - - f = fopen(path, "w"); - if (!f) { - fprintf(stderr, "failed to open '%s': %m\n", path); - return -1; - } - - fprintf(f, "%d", val); - - fclose(f); - - return 0; -} - -static int read_int(const char *path, int *val) -{ - FILE *f; - - f = fopen(path, "r"); - if (!f) { - fprintf(stderr, "failed to open '%s': %m\n", path); - return -1; - } - - fscanf(f, "%d", val); - - fclose(f); - - return 0; -} - -static int idlestat_trace_enable(bool enable) -{ - return write_int(TRACE_ON_PATH, enable); -} - -static int idlestat_flush_trace(void) -{ - return write_int(TRACE_FILE, 0); -} - static int idlestat_file_for_each_line(const char *path, void *data, - int (*handler)(const char *, void *)) + int (*handler)(const char *, void *)) { FILE *f; int ret; @@ -523,13 +471,13 @@ static int idlestat_file_for_each_line(const char *path, void *data, return -1; f = fopen(path, "r"); + if (!f) { fprintf(f, "failed to open '%s': %m\n", path); return -1; } while (fgets(buffer, BUFSIZE, f)) { - ret = handler(buffer, data); if (ret) break; @@ -540,19 +488,6 @@ static int idlestat_file_for_each_line(const char *path, void *data, return ret; } -static int store_line(const char *line, void *data) -{ - FILE *f = data; - - /* ignore comment line */ - if (line[0] == '#') - return 0; - - fprintf(f, "%s", line); - - return 0; -} - static int idlestat_store(const char *path) { FILE *f; @@ -580,37 +515,6 @@ static int idlestat_store(const char *path) return ret; } -static int idlestat_init_trace(unsigned int duration) -{ - int bufsize; - - /* Assuming the worst case where we can have - * TRACE_IDLE_NRHITS_PER_SEC. Each state enter/exit line are - * 196 chars wide, so we have 2 x 196 x TRACE_IDLE_NRHITS_PER_SEC bytes. - * divided by 2^10 to have Kb. We add 1Kb to be sure to round up. - */ - bufsize = 2 * TRACE_IDLE_LENGTH * TRACE_IDLE_NRHITS_PER_SEC * duration; - bufsize = (bufsize / (1 << 10)) + 1; - - if (write_int(TRACE_BUFFER_SIZE_PATH, bufsize)) - return -1; - - if (read_int(TRACE_BUFFER_TOTAL_PATH, &bufsize)) - return -1; - - printf("Total trace buffer: %d kB\n", bufsize); - - /* Disable all the traces */ - if (write_int(TRACE_EVENT_PATH, 0)) - return -1; - - /* Enable only cpu_idle traces */ - if (write_int(TRACE_CPUIDLE_EVENT_PATH, 1)) - return -1; - - return 0; -} - static int idlestat_wake_all(void) { int rcpu, i, ret; diff --git a/trace.c b/trace.c new file mode 100644 index 0000000..56afe73 --- /dev/null +++ b/trace.c @@ -0,0 +1,51 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include "trace.h" +#include "utils.h" + +int idlestat_trace_enable(bool enable) +{ + return write_int(TRACE_ON_PATH, enable); +} + +int idlestat_flush_trace(void) +{ + return write_int(TRACE_FILE, 0); +} + +int idlestat_init_trace(unsigned int duration) +{ + int bufsize; + + /* Assuming the worst case where we can have + * TRACE_IDLE_NRHITS_PER_SEC. Each state enter/exit line are + * 196 chars wide, so we have 2 x 196 x TRACE_IDLE_NRHITS_PER_SEC bytes. + * divided by 2^10 to have Kb. We add 1Kb to be sure to round up. + */ + + bufsize = 2 * TRACE_IDLE_LENGTH * TRACE_IDLE_NRHITS_PER_SEC * duration; + bufsize = (bufsize / (1 << 10)) + 1; + + if (write_int(TRACE_BUFFER_SIZE_PATH, bufsize)) + return -1; + + if (read_int(TRACE_BUFFER_TOTAL_PATH, &bufsize)) + return -1; + + printf("Total trace buffer: %d kB\n", bufsize); + + /* Disable all the traces */ + if (write_int(TRACE_EVENT_PATH, 0)) + return -1; + + /* Enable only cpu_idle traces */ + if (write_int(TRACE_CPUIDLE_EVENT_PATH, 1)) + return -1; + + return 0; +} diff --git a/trace.h b/trace.h new file mode 100644 index 0000000..589fd2a --- /dev/null +++ b/trace.h @@ -0,0 +1,21 @@ + +#ifndef __TRACE_H +#define __TRACE_H + +#define TRACE_PATH "/sys/kernel/debug/tracing" +#define TRACE_ON_PATH TRACE_PATH "/tracing_on" +#define TRACE_BUFFER_SIZE_PATH TRACE_PATH "/buffer_size_kb" +#define TRACE_BUFFER_TOTAL_PATH TRACE_PATH "/buffer_total_size_kb" +#define TRACE_CPUIDLE_EVENT_PATH TRACE_PATH "/events/power/cpu_idle/enable" +#define TRACE_EVENT_PATH TRACE_PATH "/events/enable" +#define TRACE_FREE TRACE_PATH "/free_buffer" +#define TRACE_FILE TRACE_PATH "/trace" +#define TRACE_IDLE_NRHITS_PER_SEC 10000 +#define TRACE_IDLE_LENGTH 196 + +extern int idlestat_trace_enable(bool enable); +extern int idlestat_flush_trace(void); +extern int idlestat_init_trace(unsigned int duration); + +#endif + diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..2b2cac1 --- /dev/null +++ b/utils.c @@ -0,0 +1,55 @@ + +#define _GNU_SOURCE +#include +#undef _GNU_SOURCE +#include + +#include "utils.h" + +int write_int(const char *path, int val) +{ + FILE *f; + + f = fopen(path, "w"); + if (!f) { + fprintf(stderr, "failed to open '%s': %m\n", path); + return -1; + } + + fprintf(f, "%d", val); + + fclose(f); + + return 0; +} + +int read_int(const char *path, int *val) +{ + FILE *f; + + f = fopen(path, "r"); + + if (!f) { + fprintf(stderr, "failed to open '%s': %m\n", path); + return -1; + } + + fscanf(f, "%d", val); + + fclose(f); + + return 0; +} + +int store_line(const char *line, void *data) +{ + FILE *f = data; + + /* ignore comment line */ + if (line[0] == '#') + return 0; + + fprintf(f, "%s", line); + + return 0; +} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..950cf26 --- /dev/null +++ b/utils.h @@ -0,0 +1,9 @@ + +#ifndef __UTILS_H +#define __UTILS_H + +extern int write_int(const char *path, int val); +extern int read_int(const char *path, int *val); +extern int store_line(const char *line, void *data); + +#endif