Adding support to read all formula definition files
under common locations:

  'formula'
    - under perf developement tree
  'PREFIX/PERF_EXEC_PATH/formulas'
    - when installed

Plus adding very basic formula definitions for CPI
and branch related counters.

Signed-off-by: Jiri Olsa <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Corey Ashford <[email protected]>
Cc: Frederic Weisbecker <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Paul Mackerras <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: David Ahern <[email protected]>
Cc: Ulrich Drepper <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Stephane Eranian <[email protected]>
---
 tools/perf/Makefile              |  8 ++++++
 tools/perf/formulas/default.conf | 26 ++++++++++++++++++++
 tools/perf/util/formula.c        | 53 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/formula.h        |  3 +++
 4 files changed, 90 insertions(+)
 create mode 100644 tools/perf/formulas/default.conf

diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index bcbc524..c35bb7f 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -1010,6 +1010,12 @@ $(OUTPUT)util/exec_cmd.o: util/exec_cmd.c 
$(OUTPUT)PERF-CFLAGS
                '-DPREFIX="$(prefix_SQ)"' \
                $<
 
+$(OUTPUT)util/formula.o: util/formula.c $(OUTPUT)PERF-CFLAGS
+       $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) \
+               '-DPERF_EXEC_PATH="$(perfexecdir_SQ)"' \
+               '-DPREFIX="$(prefix_SQ)"' \
+               $<
+
 $(OUTPUT)tests/attr.o: tests/attr.c $(OUTPUT)PERF-CFLAGS
        $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) \
                '-DBINDIR="$(bindir_SQ)"' -DPYTHON='"$(PYTHON_WORD)"' \
@@ -1201,6 +1207,8 @@ install-bin: all
        $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'
        $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'
        $(INSTALL) tests/attr/* '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests/attr'
+       $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/formulas'
+       $(INSTALL) -m 644 formulas/* 
$(DESTDIR_SQ)$(perfexec_instdir_SQ)/formulas
 
 install: install-bin try-install-man
 
diff --git a/tools/perf/formulas/default.conf b/tools/perf/formulas/default.conf
new file mode 100644
index 0000000..56712d6
--- /dev/null
+++ b/tools/perf/formulas/default.conf
@@ -0,0 +1,26 @@
+cpi {
+       events {
+               CY = cycles:u
+               IN = instructions:u
+       }
+
+       cpi = CY / IN
+
+       print cpi
+}
+
+branch {
+       events {
+               IN = instructions:u
+               BI = branch-instructions:u
+               BM = branch-misses:u
+       }
+
+       branch-rate       = BI / IN
+       branch-miss-rate  = BM / IN
+       branch-miss-ratio = BM / BI
+
+       print branch-rate
+       print branch-miss-rate
+       print branch-miss-ratio
+}
diff --git a/tools/perf/util/formula.c b/tools/perf/util/formula.c
index 2edc720..b9d95b2 100644
--- a/tools/perf/util/formula.c
+++ b/tools/perf/util/formula.c
@@ -159,6 +159,35 @@ int perf_formula__load(struct perf_formula *f, char *path)
        return ret;
 }
 
+int perf_formula__load_dir(struct perf_formula *f, char *path)
+{
+       struct dirent *ent;
+       DIR *dir;
+       int ret = 0;
+
+       dir = opendir(path);
+       if (!dir) {
+               pr_err("formula: can't open dir '%s' - %s\n",
+                      path, strerror(errno));
+               return -1;
+       }
+
+       while (!ret && (ent = readdir(dir))) {
+               char file[PATH_MAX];
+
+               if (!strcmp(ent->d_name, ".") ||
+                   !strcmp(ent->d_name, ".."))
+                       continue;
+
+               scnprintf(file, PATH_MAX, "%s/%s", path, ent->d_name);
+
+               ret = perf_formula__load(f, file);
+       }
+
+       closedir(dir);
+       return ret;
+}
+
 int perf_formula__free(struct perf_formula *f)
 {
        struct perf_formula_file *file;
@@ -511,3 +540,27 @@ double perf_formula_expr__resolve(struct perf_formula_expr 
*expr,
        pr_debug2("formula resolve %s = %F\n", name, result);
        return result;
 }
+
+
+__attribute__((weak))
+int perf_formula__preload_arch(struct perf_formula *f __maybe_unused)
+{
+       return 0;
+}
+
+int perf_formula__preload(struct perf_formula *f)
+{
+       struct stat st;
+       int ret = 0;
+
+       if (!lstat("./formulas", &st))
+               ret = perf_formula__load_dir(f, (char *) "./formulas");
+       else {
+               char path[PATH_MAX];
+
+               scnprintf(path, PATH_MAX, "%s/%s/formulas/", PREFIX, 
PERF_EXEC_PATH);
+               ret = perf_formula__load_dir(f, path);
+       }
+
+       return ret ? ret : perf_formula__preload_arch(f);
+}
diff --git a/tools/perf/util/formula.h b/tools/perf/util/formula.h
index 90325ec..b6437d8 100644
--- a/tools/perf/util/formula.h
+++ b/tools/perf/util/formula.h
@@ -106,6 +106,7 @@ struct perf_formula_config {
 void perf_formula__init(struct perf_formula *f);
 
 int perf_formula__load(struct perf_formula *f, char *path);
+int perf_formula__load_dir(struct perf_formula *f, char *path);
 int perf_formula__free(struct perf_formula *f);
 
 struct perf_formula_set*
@@ -134,4 +135,6 @@ perf_formula_set__new(char *name, struct list_head *head);
 double perf_formula_expr__resolve(struct perf_formula_expr *expr,
                                  char *name);
 
+int perf_formula__preload(struct perf_formula *f);
+int perf_formula__preload_arch(struct perf_formula *f);
 #endif /* __PERF_FORMULA */
-- 
1.7.11.7

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

Reply via email to