tools lib api cpu: Introduce cpu.[ch] to obtain cpu related information
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 10 Sep 2015 15:20:14 +0000 (12:20 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 14 Sep 2015 15:50:26 +0000 (12:50 -0300)
E.g.:

 $ ./cpu__get_max_freq
 3200000

It does that, as Kan's patch does, by looking at these files:

  $ cat /sys/devices/system/cpu/online
  0-3
  $ ./sysfs__read_ull
  devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
  /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq=3200000
  $

I.e. find out the first online CPU, then read its cpufreq info.

But do it in tools/lib/api/, so that other tools/ living code can use
it, not just perf.

Based-on-a-patch-by: Kan Liang <kan.liang@intel.com>
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-915v4cvxqplaub8qco66b9mv@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/api/Build
tools/lib/api/cpu.c [new file with mode: 0644]
tools/lib/api/cpu.h [new file with mode: 0644]

index 3653965cf48185a9950bb81aafb12660b306cba6..e8b8a23b9bf4cdac8ccfcf95690f8dfd93e1d6e6 100644 (file)
@@ -1,2 +1,3 @@
 libapi-y += fd/
 libapi-y += fs/
+libapi-y += cpu.o
diff --git a/tools/lib/api/cpu.c b/tools/lib/api/cpu.c
new file mode 100644 (file)
index 0000000..8c64893
--- /dev/null
@@ -0,0 +1,18 @@
+#include <stdio.h>
+
+#include "cpu.h"
+#include "fs/fs.h"
+
+int cpu__get_max_freq(unsigned long long *freq)
+{
+       char entry[PATH_MAX];
+       int cpu;
+
+       if (sysfs__read_int("devices/system/cpu/online", &cpu) < 0)
+               return -1;
+
+       snprintf(entry, sizeof(entry),
+                "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
+
+       return sysfs__read_ull(entry, freq);
+}
diff --git a/tools/lib/api/cpu.h b/tools/lib/api/cpu.h
new file mode 100644 (file)
index 0000000..81e9d39
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef __API_CPU__
+#define __API_CPU__
+
+int cpu__get_max_freq(unsigned long long *freq);
+
+#endif /* __API_CPU__ */