Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1333513
| From | Jiri Olsa <jolsa@kernel.org> |
|---|---|
| Newsgroups | linux.kernel |
| Subject | [PATCH 2/5] tools lib api fs: Move filename__read_str into api/fs/fs.c |
| Date | 2016-02-14 17:10 +0100 |
| Message-ID | <r27ph-4Un-25@gated-at.bofh.it> (permalink) |
| References | <r27pg-4Un-3@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
We already moved similar functions in here,
also it'll be useful for sysfs__read_str
addition in following patch.
Link: http://lkml.kernel.org/n/tip-azxqdrdsw6fpfpk9ycmyp5eb@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
tools/lib/api/fs/fs.c | 51 +++++++++++++++++++++++++++++++++++++++++++
tools/lib/api/fs/fs.h | 2 ++
tools/perf/util/trace-event.c | 1 +
tools/perf/util/util.c | 48 ----------------------------------------
tools/perf/util/util.h | 1 -
5 files changed, 54 insertions(+), 49 deletions(-)
diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
index 459599d1b6c4..2cbf6773ca5d 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -13,6 +13,7 @@
#include <sys/mount.h>
#include "fs.h"
+#include "debug-internal.h"
#define _STR(x) #x
#define STR(x) _STR(x)
@@ -300,6 +301,56 @@ int filename__read_ull(const char *filename, unsigned long long *value)
return err;
}
+#define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
+
+int filename__read_str(const char *filename, char **buf, size_t *sizep)
+{
+ size_t size = 0, alloc_size = 0;
+ void *bf = NULL, *nbf;
+ int fd, n, err = 0;
+ char sbuf[STRERR_BUFSIZE];
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ do {
+ if (size == alloc_size) {
+ alloc_size += BUFSIZ;
+ nbf = realloc(bf, alloc_size);
+ if (!nbf) {
+ err = -ENOMEM;
+ break;
+ }
+
+ bf = nbf;
+ }
+
+ n = read(fd, bf + size, alloc_size - size);
+ if (n < 0) {
+ if (size) {
+ pr_warning("read failed %d: %s\n", errno,
+ strerror_r(errno, sbuf, sizeof(sbuf)));
+ err = 0;
+ } else
+ err = -errno;
+
+ break;
+ }
+
+ size += n;
+ } while (n > 0);
+
+ if (!err) {
+ *sizep = size;
+ *buf = bf;
+ } else
+ free(bf);
+
+ close(fd);
+ return err;
+}
+
int sysfs__read_ull(const char *entry, unsigned long long *value)
{
char path[PATH_MAX];
diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h
index d024a7f682f6..858922b61141 100644
--- a/tools/lib/api/fs/fs.h
+++ b/tools/lib/api/fs/fs.h
@@ -2,6 +2,7 @@
#define __API_FS__
#include <stdbool.h>
+#include <unistd.h>
/*
* On most systems <limits.h> would have given us this, but not on some systems
@@ -26,6 +27,7 @@ FS(tracefs)
int filename__read_int(const char *filename, int *value);
int filename__read_ull(const char *filename, unsigned long long *value);
+int filename__read_str(const char *filename, char **buf, size_t *sizep);
int sysctl__read_int(const char *sysctl, int *value);
int sysfs__read_int(const char *entry, int *value);
diff --git a/tools/perf/util/trace-event.c b/tools/perf/util/trace-event.c
index 802bb868d446..8ae051e0ec79 100644
--- a/tools/perf/util/trace-event.c
+++ b/tools/perf/util/trace-event.c
@@ -10,6 +10,7 @@
#include <linux/err.h>
#include <traceevent/event-parse.h>
#include <api/fs/tracing_path.h>
+#include <api/fs/fs.h>
#include "trace-event.h"
#include "machine.h"
#include "util.h"
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index b9e2843cfbe7..35b20dd454de 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -507,54 +507,6 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
return ret;
}
-int filename__read_str(const char *filename, char **buf, size_t *sizep)
-{
- size_t size = 0, alloc_size = 0;
- void *bf = NULL, *nbf;
- int fd, n, err = 0;
- char sbuf[STRERR_BUFSIZE];
-
- fd = open(filename, O_RDONLY);
- if (fd < 0)
- return -errno;
-
- do {
- if (size == alloc_size) {
- alloc_size += BUFSIZ;
- nbf = realloc(bf, alloc_size);
- if (!nbf) {
- err = -ENOMEM;
- break;
- }
-
- bf = nbf;
- }
-
- n = read(fd, bf + size, alloc_size - size);
- if (n < 0) {
- if (size) {
- pr_warning("read failed %d: %s\n", errno,
- strerror_r(errno, sbuf, sizeof(sbuf)));
- err = 0;
- } else
- err = -errno;
-
- break;
- }
-
- size += n;
- } while (n > 0);
-
- if (!err) {
- *sizep = size;
- *buf = bf;
- } else
- free(bf);
-
- close(fd);
- return err;
-}
-
const char *get_filename_for_perf_kvm(void)
{
const char *filename;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index a8615816a00d..3dd04089e8be 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -303,7 +303,6 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
bool show_sym, bool unwind_inlines);
void free_srcline(char *srcline);
-int filename__read_str(const char *filename, char **buf, size_t *sizep);
int perf_event_paranoid(void);
void mem_bswap_64(void *src, int byte_size);
--
2.4.3
Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[PATCH 0/5] perf tools: Store CPU cache details under perf data Jiri Olsa <jolsa@kernel.org> - 2016-02-14 17:10 +0100
[PATCH 3/5] tools lib api fs: Add sysfs__read_str function Jiri Olsa <jolsa@kernel.org> - 2016-02-14 17:10 +0100
[tip:perf/core] tools lib api fs: Add sysfs__read_str function tip-bot for Jiri Olsa <tipbot@zytor.com> - 2016-02-17 13:10 +0100
[PATCH 5/5] perf tools: Add perf data cache feature Jiri Olsa <jolsa@kernel.org> - 2016-02-14 17:10 +0100
[PATCHv2 5/5] perf tools: Add perf data cache feature Jiri Olsa <jolsa@redhat.com> - 2016-02-16 13:20 +0100
Re: [PATCHv2 5/5] perf tools: Add perf data cache feature Namhyung Kim <namhyung@kernel.org> - 2016-02-16 15:30 +0100
[PATCHv3 5/5] perf tools: Add perf data cache feature Jiri Olsa <jolsa@redhat.com> - 2016-02-16 16:10 +0100
Re: [PATCHv3 5/5] perf tools: Add perf data cache feature Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-02-16 16:30 +0100
Re: [PATCHv3 5/5] perf tools: Add perf data cache feature Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com> - 2016-02-16 16:50 +0100
[tip:perf/core] perf tools: Add perf data cache feature tip-bot for Jiri Olsa <tipbot@zytor.com> - 2016-02-17 13:10 +0100
[PATCH 4/5] perf tools: Initialize libapi debug output Jiri Olsa <jolsa@kernel.org> - 2016-02-14 17:10 +0100
[tip:perf/core] perf tools: Initialize libapi debug output tip-bot for Jiri Olsa <tipbot@zytor.com> - 2016-02-17 13:10 +0100
[PATCH 1/5] tools lib api: Add debug output support Jiri Olsa <jolsa@kernel.org> - 2016-02-14 17:10 +0100
[tip:perf/core] tools lib api: Add debug output support tip-bot for Jiri Olsa <tipbot@zytor.com> - 2016-02-17 13:10 +0100
[PATCH 2/5] tools lib api fs: Move filename__read_str into api/fs/fs.c Jiri Olsa <jolsa@kernel.org> - 2016-02-14 17:10 +0100
[tip:perf/core] tools lib api fs: Adopt filename__read_str from perf tip-bot for Jiri Olsa <tipbot@zytor.com> - 2016-02-17 13:10 +0100
Re: [PATCH 0/5] perf tools: Store CPU cache details under perf data Ingo Molnar <mingo@kernel.org> - 2016-02-16 09:20 +0100
Re: [PATCH 0/5] perf tools: Store CPU cache details under perf data Jiri Olsa <jolsa@redhat.com> - 2016-02-16 12:30 +0100
csiph-web