perf tools: Introduce filename__read_int helper
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 17 Oct 2013 19:33:43 +0000 (16:33 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 17 Oct 2013 19:33:43 +0000 (16:33 -0300)
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 <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-669q04c5tou5pnt8jtiz6y2r@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/util.c
tools/perf/util/util.h

index 8dc8cf39f4edc0f988c63a48daa47395a1c5ef87..c25e57b3acb2994d7d53b8089f1897030b71efb7 100644 (file)
@@ -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;
+}
index 42dfba70fbfcc09238a719da003798dad461618a..c8f362daba8755f5b054d70c90f52211f6d8ccc8 100644 (file)
@@ -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 */