Skip to contentSkip to conversation

tools lib api: Adopt fdarray class from perf' s evlist

tip:perf/core tip-bot for Arnaldo Carvalho de Melo 1 participant 1 message Sep 26, 2014 perf events

lore
Share & tools
lore
#0 lore raw
Commit-ID:  1b85337d0685d1dc5a6f9061434ba4316d69f3b8
Gitweb:     http://git.kernel.org/tip/1b85337d0685d1dc5a6f9061434ba4316d69f3b8
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Wed, 3 Sep 2014 18:02:59 -0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 25 Sep 2014 16:46:55 -0300

tools lib api: Adopt fdarray class from perf's evlist

The extensible file description array that grew in the perf_evlist class can be useful for other tools, as it is not something that only evlists need, so move it to tools/lib/api/fd to ease sharing it.

v2: Don't use {} like in:

libapi_dirs:

	$(QUIET_MKDIR)mkdir -p $(OUTPUT){fs,fd}/

in Makefiles, as it will not work in some systems, as in ubuntu13.10.

v3: Add fd/*.[ch] to LIBAPIKFS_SOURCES (Fix from Jiri Olsa)

v4: Leave the fcntl(fd, O_NONBLOCK) in the evlist layer, remains to
be checked if it is really needed there, but has no place in the fdarray class (Fix from Jiri Olsa)

v5: Remove evlist details from fdarray grow/filter tests. Improve it a
bit doing more tests about expected internal state.

Cc Adrian Hunter <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jean Pihet <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/lib/api/Makefile          |   7 +-
 tools/lib/api/fd/array.c        | 107 ++++++++++++++++++++
 tools/lib/api/fd/array.h        |  32 ++++++
 tools/perf/Makefile.perf        |   4 +-
 tools/perf/builtin-kvm.c        |   4 +-
 tools/perf/tests/builtin-test.c |   8 +-
 tools/perf/tests/evlist.c       | 214 ----------------------------------------
 tools/perf/tests/fdarray.c      | 174 ++++++++++++++++++++++++++++++++
 tools/perf/tests/tests.h        |   4 +-
 tools/perf/util/evlist.c        |  57 ++---------
 tools/perf/util/evlist.h        |   5 +-
 tools/perf/util/python.c        |   4 +-
 12 files changed, 342 insertions(+), 278 deletions(-)
tools/lib/api/Makefile +6 -1
index ce00f7e..36c08b1 100644
@@ -10,9 +10,14 @@ LIB_OBJS=
 
 LIB_H += fs/debugfs.h
 LIB_H += fs/fs.h
+# See comment below about piggybacking...
+LIB_H += fd/array.h
 
 LIB_OBJS += $(OUTPUT)fs/debugfs.o
 LIB_OBJS += $(OUTPUT)fs/fs.o
+# XXX piggybacking here, need to introduce libapikfd, or rename this
+# to plain libapik.a and make it have it all api goodies
+LIB_OBJS += $(OUTPUT)fd/array.o
 
 LIBFILE = libapikfs.a
 
@@ -29,7 +34,7 @@ $(LIBFILE): $(LIB_OBJS)
 $(LIB_OBJS): $(LIB_H)
 
 libapi_dirs:
-	$(QUIET_MKDIR)mkdir -p $(OUTPUT)fs/
+	$(QUIET_MKDIR)mkdir -p $(OUTPUT)fd $(OUTPUT)fs
 
 $(OUTPUT)%.o: %.c libapi_dirs
 	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<
tools/lib/api/fd/array.c new file +107 -0

107 lines not on this page · raw message

tools/lib/api/fd/array.h new file +32 -0
new file mode 100644
index 0000000..de38361
@@ -0,0 +1,32 @@
+#ifndef __API_FD_ARRAY__
+#define __API_FD_ARRAY__
+
+#include <stdio.h>
+
+struct pollfd;
+
+struct fdarray {
+	int	       nr;
+	int	       nr_alloc;
+	int	       nr_autogrow;
+	struct pollfd *entries;
+};
+
+void fdarray__init(struct fdarray *fda, int nr_autogrow);
+void fdarray__exit(struct fdarray *fda);
+
+struct fdarray *fdarray__new(int nr_alloc, int nr_autogrow);
+void fdarray__delete(struct fdarray *fda);
+
+int fdarray__add(struct fdarray *fda, int fd, short revents);
+int fdarray__poll(struct fdarray *fda, int timeout);
+int fdarray__filter(struct fdarray *fda, short revents);
+int fdarray__grow(struct fdarray *fda, int extra);
+int fdarray__fprintf(struct fdarray *fda, FILE *fp);
+
+static inline int fdarray__available_entries(struct fdarray *fda)
+{
+	return fda->nr_alloc - fda->nr;
+}
+
+#endif /* __API_FD_ARRAY__ */
tools/perf/Makefile.perf +2 -2
index f287c25..262916f 100644
@@ -400,9 +400,9 @@ LIB_OBJS += $(OUTPUT)tests/open-syscall-tp-fields.o
 LIB_OBJS += $(OUTPUT)tests/mmap-basic.o
 LIB_OBJS += $(OUTPUT)tests/perf-record.o
 LIB_OBJS += $(OUTPUT)tests/rdpmc.o
-LIB_OBJS += $(OUTPUT)tests/evlist.o
 LIB_OBJS += $(OUTPUT)tests/evsel-roundtrip-name.o
 LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o
+LIB_OBJS += $(OUTPUT)tests/fdarray.o
 LIB_OBJS += $(OUTPUT)tests/pmu.o
 LIB_OBJS += $(OUTPUT)tests/hists_common.o
 LIB_OBJS += $(OUTPUT)tests/hists_link.o
@@ -770,7 +770,7 @@ $(LIBTRACEEVENT)-clean:
 install-traceevent-plugins: $(LIBTRACEEVENT)
 	$(QUIET_SUBDIR0)$(TRACE_EVENT_DIR) $(LIBTRACEEVENT_FLAGS) install_plugins
 
-LIBAPIKFS_SOURCES = $(wildcard $(LIB_PATH)fs/*.[ch])
+LIBAPIKFS_SOURCES = $(wildcard $(LIB_PATH)fs/*.[ch] $(LIB_PATH)fd/*.[ch])
 
 # if subdir is set, we've been called from above so target has been built
 # already
tools/perf/builtin-kvm.c +2 -2
index a440219..1e639d6 100644
@@ -920,7 +920,7 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)
 	signal(SIGTERM, sig_handler);
 
 	/* use pollfds -- need to add timerfd and stdin */
-	nr_fds = kvm->evlist->nr_fds;
+	nr_fds = kvm->evlist->pollfd.nr;
 
 	/* add timer fd */
 	if (perf_kvm__timerfd_create(kvm) < 0) {
@@ -941,7 +941,7 @@ static int kvm_events_live_report(struct perf_kvm_stat *kvm)
 	if (fd_set_nonblock(fileno(stdin)) != 0)
 		goto out;
 
-	pollfds	 = kvm->evlist->pollfd;
+	pollfds	 = kvm->evlist->pollfd.entries;
 
 	/* everything is good - enable the events and process */
 	perf_evlist__enable(kvm->evlist);
tools/perf/tests/builtin-test.c +4 -4
index 174c3ff..ac655b0 100644
@@ -158,12 +158,12 @@ static struct test {
 		.func = test__switch_tracking,
 	},
 	{
-		.desc = "Filter fds with revents mask in a pollfd array",
-		.func = test__perf_evlist__filter_pollfd,
+		.desc = "Filter fds with revents mask in a fdarray",
+		.func = test__fdarray__filter,
 	},
 	{
-		.desc = "Add fd to pollfd array, making it autogrow",
-		.func = test__perf_evlist__add_pollfd,
+		.desc = "Add fd to a fdarray, making it autogrow",
+		.func = test__fdarray__add,
 	},
 	{
 		.func = NULL,
tools/perf/tests/evlist.c deleted +0 -214

214 lines not on this page · raw message

tools/perf/tests/fdarray.c new file +174 -0

174 lines not on this page · raw message

tools/perf/tests/tests.h +2 -2
index 699d4bb..00e776a 100644
@@ -49,8 +49,8 @@ int test__thread_mg_share(void);
 int test__hists_output(void);
 int test__hists_cumulate(void);
 int test__switch_tracking(void);
-int test__perf_evlist__filter_pollfd(void);
-int test__perf_evlist__add_pollfd(void);
+int test__fdarray__filter(void);
+int test__fdarray__add(void);
 
 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
tools/perf/util/evlist.c +9 -48

102 lines not on this page · raw message

tools/perf/util/evlist.h +2 -3
index d7e99b6..fc01370 100644
@@ -2,6 +2,7 @@
 #define __PERF_EVLIST_H 1
 
 #include <linux/list.h>
+#include <api/fd/array.h>
 #include <stdio.h>
 #include "../perf.h"
 #include "event.h"
@@ -29,8 +30,6 @@ struct perf_evlist {
 	struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
 	int		 nr_entries;
 	int		 nr_groups;
-	int		 nr_fds;
-	int		 nr_fds_alloc;
 	int		 nr_mmaps;
 	size_t		 mmap_len;
 	int		 id_pos;
@@ -41,8 +40,8 @@ struct perf_evlist {
 		pid_t	pid;
 	} workload;
 	bool		 overwrite;
+	struct fdarray	 pollfd;
 	struct perf_mmap *mmap;
-	struct pollfd	 *pollfd;
 	struct thread_map *threads;
 	struct cpu_map	  *cpus;
 	struct perf_evsel *selected;
tools/perf/util/python.c +2 -2
index 4472f8b..3dda85c 100644
@@ -753,9 +753,9 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
         PyObject *list = PyList_New(0);
 	int i;
 
-	for (i = 0; i < evlist->nr_fds; ++i) {
+	for (i = 0; i < evlist->pollfd.nr; ++i) {
 		PyObject *file;
-		FILE *fp = fdopen(evlist->pollfd[i].fd, "r");
+		FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
 
 		if (fp == NULL)
 			goto free_list;