Skip to content

Commit

Permalink
tools lib api cpu: Introduce cpu.[ch] to obtain cpu related information
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Arnaldo Carvalho de Melo committed Sep 14, 2015
1 parent 2d729f6 commit 09f6acf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/lib/api/Build
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
libapi-y += fd/
libapi-y += fs/
libapi-y += cpu.o
18 changes: 18 additions & 0 deletions tools/lib/api/cpu.c
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 6 additions & 0 deletions tools/lib/api/cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __API_CPU__
#define __API_CPU__

int cpu__get_max_freq(unsigned long long *freq);

#endif /* __API_CPU__ */

0 comments on commit 09f6acf

Please sign in to comment.