[tip:perf/core] tools lib api fs: Introduce sysfs__read_{int,ull} ()

2015-09-15 Thread tip-bot for Arnaldo Carvalho de Melo
Commit-ID:  2d729f6a8ac3edbf68de7239fab96c9736946af5
Gitweb: http://git.kernel.org/tip/2d729f6a8ac3edbf68de7239fab96c9736946af5
Author: Arnaldo Carvalho de Melo 
AuthorDate: Thu, 10 Sep 2015 11:58:50 -0300
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Mon, 14 Sep 2015 12:50:25 -0300

tools lib api fs: Introduce sysfs__read_{int,ull}()

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
  320
  $ ./sysfs__read_ull
  devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
  /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq=320
  $

Cc: Adrian Hunter 
Cc: Borislav Petkov 
Cc: David Ahern 
Cc: Frederic Weisbecker 
Cc: Jiri Olsa 
Cc: Kan Liang 
Cc: Namhyung Kim 
Cc: Stephane Eranian 
Cc: Wang Nan 
Link: http://lkml.kernel.org/n/tip-4a12m4d5k8m4qgc1vguoc...@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo 
---
 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 7915093..732dbef5 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -1,5 +1,6 @@
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -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 f654bcb..d024a7f 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__ */
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[tip:perf/core] tools lib api fs: Introduce sysfs__read_{int,ull} ()

2015-09-15 Thread tip-bot for Arnaldo Carvalho de Melo
Commit-ID:  2d729f6a8ac3edbf68de7239fab96c9736946af5
Gitweb: http://git.kernel.org/tip/2d729f6a8ac3edbf68de7239fab96c9736946af5
Author: Arnaldo Carvalho de Melo 
AuthorDate: Thu, 10 Sep 2015 11:58:50 -0300
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Mon, 14 Sep 2015 12:50:25 -0300

tools lib api fs: Introduce sysfs__read_{int,ull}()

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
  320
  $ ./sysfs__read_ull
  devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
  /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq=320
  $

Cc: Adrian Hunter 
Cc: Borislav Petkov 
Cc: David Ahern 
Cc: Frederic Weisbecker 
Cc: Jiri Olsa 
Cc: Kan Liang 
Cc: Namhyung Kim 
Cc: Stephane Eranian 
Cc: Wang Nan 
Link: http://lkml.kernel.org/n/tip-4a12m4d5k8m4qgc1vguoc...@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo 
---
 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 7915093..732dbef5 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -1,5 +1,6 @@
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -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 f654bcb..d024a7f 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__ */
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/