Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1224337

[PATCH 18/27] tools lib api fs: Introduce sysfs__read_{int,ull}()

From Arnaldo Carvalho de Melo <acme@kernel.org>
Newsgroups linux.kernel
Subject [PATCH 18/27] tools lib api fs: Introduce sysfs__read_{int,ull}()
Date 2015-09-14 18:50 +0200
Message-ID <q8F76-3qM-71@gated-at.bofh.it> (permalink)
References <q8EXn-3f1-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Arnaldo Carvalho de Melo <acme@redhat.com>

To read either an int or an unsigned long long value from the given
file.

E.g.:

  $ cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
  3200000
  $ ./sysfs__read_ull
  devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
  /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq=3200000
  $

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-4a12m4d5k8m4qgc1vguocvei@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/lib/api/fs/fs.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/api/fs/fs.h |  4 ++++
 2 files changed, 49 insertions(+)

diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
index 791509346178..732dbef588b0 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -1,5 +1,6 @@
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -281,6 +282,50 @@ int filename__read_int(const char *filename, int *value)
 	return err;
 }
 
+int filename__read_ull(const char *filename, unsigned long long *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 = strtoull(line, NULL, 10);
+		if (*value != ULLONG_MAX)
+			err = 0;
+	}
+
+	close(fd);
+	return err;
+}
+
+int sysfs__read_ull(const char *entry, unsigned long long *value)
+{
+	char path[PATH_MAX];
+	const char *sysfs = sysfs__mountpoint();
+
+	if (!sysfs)
+		return -1;
+
+	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
+
+	return filename__read_ull(path, value);
+}
+
+int sysfs__read_int(const char *entry, int *value)
+{
+	char path[PATH_MAX];
+	const char *sysfs = sysfs__mountpoint();
+
+	if (!sysfs)
+		return -1;
+
+	snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
+
+	return filename__read_int(path, value);
+}
+
 int sysctl__read_int(const char *sysctl, int *value)
 {
 	char path[PATH_MAX];
diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h
index f654bcb99d1e..d024a7f682f6 100644
--- a/tools/lib/api/fs/fs.h
+++ b/tools/lib/api/fs/fs.h
@@ -25,5 +25,9 @@ FS(tracefs)
 
 
 int filename__read_int(const char *filename, int *value);
+int filename__read_ull(const char *filename, unsigned long long *value);
+
 int sysctl__read_int(const char *sysctl, int *value);
+int sysfs__read_int(const char *entry, int *value);
+int sysfs__read_ull(const char *entry, unsigned long long *value);
 #endif /* __API_FS__ */
-- 
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[GIT PULL 00/27] perf/core2 improvements and fixes Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:40 +0200
  [PATCH 01/27] perf tests: Take into account address of each objdump line Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:40 +0200
  [PATCH 03/27] perf tests: Stop reading if objdump output crossed sections Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:40 +0200
  [PATCH 12/27] perf env: Rename some leftovers from rename to perf_env Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 16/27] perf tools: Add tools/include into tags directories Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 14/27] perf hists browser: Fixup the "cpu" column width calculation Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 21/27] perf env: Introduce read_cpu_topology_map() method Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 19/27] tools lib api cpu: Introduce cpu.[ch] to obtain cpu related information Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 22/27] perf machine: Add pointer to sample's environment Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 11/27] perf env: Move perf_env out of header.h and session.c into separate object Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 27/27] perf test: Add entry for hists socket filter Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 15/27] perf evsel: Remove forward declaration of 'struct perf_evlist' Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 10/27] perf tests: Introduce iterator function for tests Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 20/27] perf cpu_map: Use sysfs__read_int in get_{core,socket}_id() Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 13/27] perf env: Adopt perf_header__set_cmdline Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 24/27] perf tools: Introduce new sort type "socket" for the processor socket Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 08/27] perf tools: Switch to tracing_path interface on appropriate places Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 23/27] perf tools: Add processor socket info to hist_entry and addr_location Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 25/27] perf report: Introduce --socket-filter option Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 26/27] perf hists browser: Zoom in/out for processor socket Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 18/27] tools lib api fs: Introduce sysfs__read_{int,ull}() Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 17/27] perf env: Read msr pmu type from header Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 09/27] perf test: Add entry to test cpu topology Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 18:50 +0200
  [PATCH 07/27] tools lib api fs: Remove debugfs, tracefs and findfs objects Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 19:00 +0200
  [PATCH 06/27] tools lib api fs: Replace debugfs/tracefs objects interface with fs.c Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 19:00 +0200
  [PATCH 05/27] tools lib api fs: Make tracing_path_strerror_open message generic Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-09-14 19:00 +0200
  Re: [GIT PULL 00/27] perf/core2 improvements and fixes Ingo Molnar <mingo@kernel.org> - 2015-09-15 09:00 +0200

csiph-web