strlist__delete(metriclist);
 }
 
+static void metricgroup__add_metric_weak_group(struct strbuf *events,
+                                              const char **ids,
+                                              int idnum)
+{
+       bool no_group = false;
+       int i;
+
+       for (i = 0; i < idnum; i++) {
+               pr_debug("found event %s\n", ids[i]);
+               /*
+                * Duration time maps to a software event and can make
+                * groups not count. Always use it outside a
+                * group.
+                */
+               if (!strcmp(ids[i], "duration_time")) {
+                       if (i > 0)
+                               strbuf_addf(events, "}:W,");
+                       strbuf_addf(events, "duration_time");
+                       no_group = true;
+                       continue;
+               }
+               strbuf_addf(events, "%s%s",
+                       i == 0 || no_group ? "{" : ",",
+                       ids[i]);
+               no_group = false;
+       }
+       if (!no_group)
+               strbuf_addf(events, "}:W");
+}
+
 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
                                   struct list_head *group_list)
 {
        struct pmu_events_map *map = perf_pmu__find_map(NULL);
        struct pmu_event *pe;
-       int ret = -EINVAL;
-       int i, j;
+       int i, ret = -EINVAL;
 
        if (!map)
                return 0;
                        const char **ids;
                        int idnum;
                        struct egroup *eg;
-                       bool no_group = false;
 
                        pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
 
                                continue;
                        if (events->len > 0)
                                strbuf_addf(events, ",");
-                       for (j = 0; j < idnum; j++) {
-                               pr_debug("found event %s\n", ids[j]);
-                               /*
-                                * Duration time maps to a software event and can make
-                                * groups not count. Always use it outside a
-                                * group.
-                                */
-                               if (!strcmp(ids[j], "duration_time")) {
-                                       if (j > 0)
-                                               strbuf_addf(events, "}:W,");
-                                       strbuf_addf(events, "duration_time");
-                                       no_group = true;
-                                       continue;
-                               }
-                               strbuf_addf(events, "%s%s",
-                                       j == 0 || no_group ? "{" : ",",
-                                       ids[j]);
-                               no_group = false;
-                       }
-                       if (!no_group)
-                               strbuf_addf(events, "}:W");
+
+                       metricgroup__add_metric_weak_group(events, ids, idnum);
 
                        eg = malloc(sizeof(struct egroup));
                        if (!eg) {