From patchwork Fri Aug 9 08:52:25 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sunshaojie X-Patchwork-Id: 18909 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qe0-f72.google.com (mail-qe0-f72.google.com [209.85.128.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 0F99F2391D for ; Fri, 9 Aug 2013 08:52:42 +0000 (UTC) Received: by mail-qe0-f72.google.com with SMTP id a11sf4835379qen.11 for ; Fri, 09 Aug 2013 01:52:41 -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=MWBUJVLltNTZ7XWUhfNpPp1xmQEcDKkL/5Ug15z1j4scyT8tr7QpevlZYqCTM+GtMi DtWyQdXdsgcHeZRdinHKSnl3djQb/8dWQ6jkbL1qkpfuArZzlNgawIlxHbiJn5Uh371U LYK+H+dk7FJMeFRUWpqlOIKvcuJSbtBBOJ/hAqe3sH6lZG6jbOa11k0lfxDOA6U/3vyd /5Q2a9HCAhSakXG2hspMv7pTBeM1PvF4jffWUzYVrZrLksriXrmIgamGLdZiwmVTkjSp e1MzcRdbJWs6lXC5ezxmCDja0aPn1qve/lCbJ0bT6Of+lN0+EdTnA2jvPA72jzqNS7H9 Pqrg== X-Received: by 10.236.79.67 with SMTP id h43mr4967352yhe.46.1376038361494; Fri, 09 Aug 2013 01:52:41 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.97.197 with SMTP id ec5ls1390367qeb.26.gmail; Fri, 09 Aug 2013 01:52:41 -0700 (PDT) X-Received: by 10.52.163.242 with SMTP id yl18mr4414437vdb.90.1376038361365; Fri, 09 Aug 2013 01:52:41 -0700 (PDT) Received: from mail-vc0-f170.google.com (mail-vc0-f170.google.com [209.85.220.170]) by mx.google.com with ESMTPS id bk8si4368827vec.18.2013.08.09.01.52.41 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 01:52:41 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.170 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.170; Received: by mail-vc0-f170.google.com with SMTP id kw10so851948vcb.15 for ; Fri, 09 Aug 2013 01:52:41 -0700 (PDT) X-Gm-Message-State: ALoCoQkdChGn7l58JDa1MXvWpW7FPWi0iGo5smM3+oevootn8tmbWV6uAza+YlD+XbecrDKFw5Wb X-Received: by 10.52.243.201 with SMTP id xa9mr4241899vdc.106.1376038361227; Fri, 09 Aug 2013 01:52:41 -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 u4csp61926vcz; Fri, 9 Aug 2013 01:52:40 -0700 (PDT) X-Received: by 10.68.196.134 with SMTP id im6mr10370317pbc.110.1376038360251; Fri, 09 Aug 2013 01:52:40 -0700 (PDT) Received: from mail-pa0-f53.google.com (mail-pa0-f53.google.com [209.85.220.53]) by mx.google.com with ESMTPS id ie10si11482497pbc.251.2013.08.09.01.52.39 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 01:52:40 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.53 is neither permitted nor denied by best guess record for domain of shaojie.sun@linaro.org) client-ip=209.85.220.53; Received: by mail-pa0-f53.google.com with SMTP id lb1so4668971pab.40 for ; Fri, 09 Aug 2013 01:52:39 -0700 (PDT) X-Received: by 10.68.143.73 with SMTP id sc9mr10570117pbb.2.1376038359715; Fri, 09 Aug 2013 01:52:39 -0700 (PDT) Received: from localhost.localdomain ([58.251.159.202]) by mx.google.com with ESMTPSA id ts6sm18943650pbc.12.2013.08.09.01.52.36 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Aug 2013 01:52:38 -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 IDLESTAT 1/2] Adjust codes to different files. Date: Fri, 9 Aug 2013 16:52:25 +0800 Message-Id: <1376038345-15628-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.170 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