diff mbox

[IDLESTAT,1/2] Adjust codes to different files.

Message ID 1376038345-15628-1-git-send-email-shaojie.sun@linaro.com
State New
Headers show

Commit Message

sunshaojie Aug. 9, 2013, 8:52 a.m. UTC
Signed-off-by: Shaojie Sun <shaojie.sun@linaro.com>
---
 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 mbox

Patch

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 <sys/signal.h>
 #include <sys/resource.h>
 
+#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 <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <string.h>
+
+#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 <stdio.h>
+#undef _GNU_SOURCE
+#include <stdlib.h>
+
+#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