Skip to contentSkip to conversation

perf tools: Introduce filename__read_int helper

tip:perf/core tip-bot for Arnaldo Carvalho de Melo 1 participant 1 message Oct 23, 2013 perf events

lore
Share & tools
lore
#0 lore raw
Commit-ID:  97a07f10c38064dea492794c99445f6260afcdc1
Gitweb:     http://git.kernel.org/tip/97a07f10c38064dea492794c99445f6260afcdc1
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Thu, 17 Oct 2013 16:33:43 -0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 17 Oct 2013 16:33:43 -0300

perf tools: Introduce filename__read_int helper

Just opens a file and calls atoi() in at most its first 64 bytes.

To read things like /proc/sys/kernel/perf_event_paranoid.

Cc Adrian Hunter <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/util.c | 17 +++++++++++++++++
 tools/perf/util/util.h |  2 ++
 2 files changed, 19 insertions(+)
tools/perf/util/util.c +17 -0
index 8dc8cf3..c25e57b 100644
@@ -394,3 +394,20 @@ unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
 
 	return (unsigned long) -1;
 }
+
+int filename__read_int(const char *filename, int *value)
+{
+	char line[64];
+	int fd = open(filename, O_RDONLY), err = -1;
+
+	if (fd < 0)
+		return -1;
+
+	if (read(fd, line, sizeof(line)) > 0) {
+		*value = atoi(line);
+		err = 0;
+	}
+
+	close(fd);
+	return err;
+}
tools/perf/util/util.h +2 -0
index 42dfba7..c8f362d 100644
@@ -305,4 +305,6 @@ struct dso;
 
 char *get_srcline(struct dso *dso, unsigned long addr);
 void free_srcline(char *srcline);
+
+int filename__read_int(const char *filename, int *value);
 #endif /* GIT_COMPAT_UTIL_H */