From patchwork Tue Sep 3 06:06:53 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sunshaojie X-Patchwork-Id: 19696 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f198.google.com (mail-qc0-f198.google.com [209.85.216.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id EDA5B24869 for ; Tue, 3 Sep 2013 06:07:21 +0000 (UTC) Received: by mail-qc0-f198.google.com with SMTP id e16sf3702071qcx.9 for ; Mon, 02 Sep 2013 23:07:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; 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=KETtIeMbBzmXMytyYl+0JV0AFA0/7bCFLHj00z0CLPL0TVGaKQVC2o8Q19lgRzqjs5 VVVtMBR+BSV1sT768MVxzAEkkbIej05v01CSRNhE1tnx4QRmu8HWrOEZE3mHuWAbiVOo sXPreAQMTYPg10uUHn8dwTVUbnT+pj69rZkIAta+/kdN5960pJSuKuCyLMaml3d44PRD AhPZ2JLTDKIT6OixY7KoT3IRBk/eMQ+K9oG9emE9S8zbn0YDPLrU6e9C9hZmHQutDoGO DgF6xgfbgdfTi883ebLC96FNnO6r6a+9U/cBUDCtqmHW4Z6la1pj9AXG1krB6/oE8IA9 vLhg== X-Received: by 10.236.31.2 with SMTP id l2mr9668775yha.36.1378188440807; Mon, 02 Sep 2013 23:07:20 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.116.135 with SMTP id jw7ls1293499qeb.58.gmail; Mon, 02 Sep 2013 23:07:20 -0700 (PDT) X-Received: by 10.220.11.7 with SMTP id r7mr26602164vcr.12.1378188440618; Mon, 02 Sep 2013 23:07:20 -0700 (PDT) Received: from mail-vc0-f169.google.com (mail-vc0-f169.google.com [209.85.220.169]) by mx.google.com with ESMTPS id o5si3095181vdw.50.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Sep 2013 23:07:20 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.220.169 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.169; Received: by mail-vc0-f169.google.com with SMTP id ib11so3741379vcb.28 for ; Mon, 02 Sep 2013 23:07:20 -0700 (PDT) X-Gm-Message-State: ALoCoQn4FToWYqWI9hFHZLTmW5ylZs8cnBxE9jryjaR4Yok57fiPJRy0MzauJDOp+sSRSSmXt6B7 X-Received: by 10.220.105.199 with SMTP id u7mr26220617vco.1.1378188440310; Mon, 02 Sep 2013 23:07:20 -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 u4csp151962vcz; Mon, 2 Sep 2013 23:07:19 -0700 (PDT) X-Received: by 10.69.17.1 with SMTP id ga1mr29096739pbd.38.1378188438911; Mon, 02 Sep 2013 23:07:18 -0700 (PDT) Received: from mail-pd0-f175.google.com (mail-pd0-f175.google.com [209.85.192.175]) by mx.google.com with ESMTPS id ut10si13651650pbc.0.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Sep 2013 23:07:18 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.192.175 is neither permitted nor denied by best guess record for domain of shaojie.sun@linaro.org) client-ip=209.85.192.175; Received: by mail-pd0-f175.google.com with SMTP id q10so5500112pdj.6 for ; Mon, 02 Sep 2013 23:07:18 -0700 (PDT) X-Received: by 10.67.3.103 with SMTP id bv7mr29808245pad.15.1378188438231; Mon, 02 Sep 2013 23:07:18 -0700 (PDT) Received: from localhost.localdomain ([58.251.159.202]) by mx.google.com with ESMTPSA id nv6sm20117421pbc.6.1969.12.31.16.00.00 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Sep 2013 23:07:17 -0700 (PDT) From: Shaojie Sun To: daniel.lezcano@linaro.org Cc: sunshaojie@huawei.com, patches@linaro.org, Shaojie Sun Subject: [PATCH IDLESTAT 1/2] Adjust codes to different files. Date: Tue, 3 Sep 2013 14:06:53 +0800 Message-Id: <1378188413-4982-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.169 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