perf tests: Fix task exit test setting maps
[firefly-linux-kernel-4.4.55.git] / tools / perf / builtin-buildid-cache.c
index 50e6b66aea1ff9c68a1dd02074e98c0b83d481d8..7b8450cd33c23ec7884a526fcb4fd3bec6ba6e8c 100644 (file)
@@ -25,8 +25,6 @@
 static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
 {
        char root_dir[PATH_MAX];
-       char notes[PATH_MAX];
-       u8 build_id[BUILD_ID_SIZE];
        char *p;
 
        strlcpy(root_dir, proc_dir, sizeof(root_dir));
@@ -35,15 +33,7 @@ static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
        if (!p)
                return -1;
        *p = '\0';
-
-       scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
-
-       if (sysfs__read_build_id(notes, build_id, sizeof(build_id)))
-               return -1;
-
-       build_id__sprintf(build_id, sizeof(build_id), sbuildid);
-
-       return 0;
+       return sysfs__sprintf_build_id(root_dir, sbuildid);
 }
 
 static int build_id_cache__kcore_dir(char *dir, size_t sz)
@@ -125,10 +115,9 @@ static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
        return ret;
 }
 
-static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
-                                    bool force)
+static int build_id_cache__add_kcore(const char *filename, bool force)
 {
-       char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1];
+       char dir[32], sbuildid[SBUILD_ID_SIZE];
        char from_dir[PATH_MAX], to_dir[PATH_MAX];
        char *p;
 
@@ -139,11 +128,11 @@ static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
                return -1;
        *p = '\0';
 
-       if (build_id_cache__kcore_buildid(from_dir, sbuildid))
+       if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
                return -1;
 
        scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s",
-                 debugdir, sbuildid);
+                 buildid_dir, sbuildid);
 
        if (!force &&
            !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
@@ -155,7 +144,7 @@ static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
                return -1;
 
        scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s",
-                 debugdir, sbuildid, dir);
+                 buildid_dir, sbuildid, dir);
 
        if (mkdir_p(to_dir, 0755))
                return -1;
@@ -183,9 +172,9 @@ static int build_id_cache__add_kcore(const char *filename, const char *debugdir,
        return 0;
 }
 
-static int build_id_cache__add_file(const char *filename, const char *debugdir)
+static int build_id_cache__add_file(const char *filename)
 {
-       char sbuild_id[BUILD_ID_SIZE * 2 + 1];
+       char sbuild_id[SBUILD_ID_SIZE];
        u8 build_id[BUILD_ID_SIZE];
        int err;
 
@@ -195,19 +184,17 @@ static int build_id_cache__add_file(const char *filename, const char *debugdir)
        }
 
        build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
-       err = build_id_cache__add_s(sbuild_id, debugdir, filename,
+       err = build_id_cache__add_s(sbuild_id, filename,
                                    false, false);
-       if (verbose)
-               pr_info("Adding %s %s: %s\n", sbuild_id, filename,
-                       err ? "FAIL" : "Ok");
+       pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
+                err ? "FAIL" : "Ok");
        return err;
 }
 
-static int build_id_cache__remove_file(const char *filename,
-                                      const char *debugdir)
+static int build_id_cache__remove_file(const char *filename)
 {
        u8 build_id[BUILD_ID_SIZE];
-       char sbuild_id[BUILD_ID_SIZE * 2 + 1];
+       char sbuild_id[SBUILD_ID_SIZE];
 
        int err;
 
@@ -217,10 +204,34 @@ static int build_id_cache__remove_file(const char *filename,
        }
 
        build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
-       err = build_id_cache__remove_s(sbuild_id, debugdir);
-       if (verbose)
-               pr_info("Removing %s %s: %s\n", sbuild_id, filename,
-                       err ? "FAIL" : "Ok");
+       err = build_id_cache__remove_s(sbuild_id);
+       pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
+                err ? "FAIL" : "Ok");
+
+       return err;
+}
+
+static int build_id_cache__purge_path(const char *pathname)
+{
+       struct strlist *list;
+       struct str_node *pos;
+       int err;
+
+       err = build_id_cache__list_build_ids(pathname, &list);
+       if (err)
+               goto out;
+
+       strlist__for_each(pos, list) {
+               err = build_id_cache__remove_s(pos->s);
+               pr_debug("Removing %s %s: %s\n", pos->s, pathname,
+                        err ? "FAIL" : "Ok");
+               if (err)
+                       break;
+       }
+       strlist__delete(list);
+
+out:
+       pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
 
        return err;
 }
@@ -252,13 +263,12 @@ static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *f
        return 0;
 }
 
-static int build_id_cache__update_file(const char *filename,
-                                      const char *debugdir)
+static int build_id_cache__update_file(const char *filename)
 {
        u8 build_id[BUILD_ID_SIZE];
-       char sbuild_id[BUILD_ID_SIZE * 2 + 1];
+       char sbuild_id[SBUILD_ID_SIZE];
 
-       int err;
+       int err = 0;
 
        if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
                pr_debug("Couldn't read a build-id in %s\n", filename);
@@ -266,14 +276,14 @@ static int build_id_cache__update_file(const char *filename,
        }
 
        build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
-       err = build_id_cache__remove_s(sbuild_id, debugdir);
-       if (!err) {
-               err = build_id_cache__add_s(sbuild_id, debugdir, filename,
-                                           false, false);
-       }
-       if (verbose)
-               pr_info("Updating %s %s: %s\n", sbuild_id, filename,
-                       err ? "FAIL" : "Ok");
+       if (build_id_cache__cached(sbuild_id))
+               err = build_id_cache__remove_s(sbuild_id);
+
+       if (!err)
+               err = build_id_cache__add_s(sbuild_id, filename, false, false);
+
+       pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
+                err ? "FAIL" : "Ok");
 
        return err;
 }
@@ -287,6 +297,7 @@ int cmd_buildid_cache(int argc, const char **argv,
        bool force = false;
        char const *add_name_list_str = NULL,
                   *remove_name_list_str = NULL,
+                  *purge_name_list_str = NULL,
                   *missing_filename = NULL,
                   *update_name_list_str = NULL,
                   *kcore_filename = NULL;
@@ -304,6 +315,8 @@ int cmd_buildid_cache(int argc, const char **argv,
                   "file", "kcore file to add"),
        OPT_STRING('r', "remove", &remove_name_list_str, "file list",
                    "file(s) to remove"),
+       OPT_STRING('p', "purge", &purge_name_list_str, "path list",
+                   "path(s) to remove (remove old caches too)"),
        OPT_STRING('M', "missing", &missing_filename, "file",
                   "to find missing build ids in the cache"),
        OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
@@ -320,6 +333,11 @@ int cmd_buildid_cache(int argc, const char **argv,
        argc = parse_options(argc, argv, buildid_cache_options,
                             buildid_cache_usage, 0);
 
+       if (argc || (!add_name_list_str && !kcore_filename &&
+                    !remove_name_list_str && !purge_name_list_str &&
+                    !missing_filename && !update_name_list_str))
+               usage_with_options(buildid_cache_usage, buildid_cache_options);
+
        if (missing_filename) {
                file.path = missing_filename;
                file.force = force;
@@ -335,10 +353,10 @@ int cmd_buildid_cache(int argc, const char **argv,
        setup_pager();
 
        if (add_name_list_str) {
-               list = strlist__new(true, add_name_list_str);
+               list = strlist__new(add_name_list_str, NULL);
                if (list) {
                        strlist__for_each(pos, list)
-                               if (build_id_cache__add_file(pos->s, buildid_dir)) {
+                               if (build_id_cache__add_file(pos->s)) {
                                        if (errno == EEXIST) {
                                                pr_debug("%s already in the cache\n",
                                                         pos->s);
@@ -353,10 +371,28 @@ int cmd_buildid_cache(int argc, const char **argv,
        }
 
        if (remove_name_list_str) {
-               list = strlist__new(true, remove_name_list_str);
+               list = strlist__new(remove_name_list_str, NULL);
+               if (list) {
+                       strlist__for_each(pos, list)
+                               if (build_id_cache__remove_file(pos->s)) {
+                                       if (errno == ENOENT) {
+                                               pr_debug("%s wasn't in the cache\n",
+                                                        pos->s);
+                                               continue;
+                                       }
+                                       pr_warning("Couldn't remove %s: %s\n",
+                                                  pos->s, strerror_r(errno, sbuf, sizeof(sbuf)));
+                               }
+
+                       strlist__delete(list);
+               }
+       }
+
+       if (purge_name_list_str) {
+               list = strlist__new(purge_name_list_str, NULL);
                if (list) {
                        strlist__for_each(pos, list)
-                               if (build_id_cache__remove_file(pos->s, buildid_dir)) {
+                               if (build_id_cache__purge_path(pos->s)) {
                                        if (errno == ENOENT) {
                                                pr_debug("%s wasn't in the cache\n",
                                                         pos->s);
@@ -374,10 +410,10 @@ int cmd_buildid_cache(int argc, const char **argv,
                ret = build_id_cache__fprintf_missing(session, stdout);
 
        if (update_name_list_str) {
-               list = strlist__new(true, update_name_list_str);
+               list = strlist__new(update_name_list_str, NULL);
                if (list) {
                        strlist__for_each(pos, list)
-                               if (build_id_cache__update_file(pos->s, buildid_dir)) {
+                               if (build_id_cache__update_file(pos->s)) {
                                        if (errno == ENOENT) {
                                                pr_debug("%s wasn't in the cache\n",
                                                         pos->s);
@@ -391,8 +427,7 @@ int cmd_buildid_cache(int argc, const char **argv,
                }
        }
 
-       if (kcore_filename &&
-           build_id_cache__add_kcore(kcore_filename, buildid_dir, force))
+       if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
                pr_warning("Couldn't add %s\n", kcore_filename);
 
 out: