Merge branch 'akpm' (patches from Andrew)
[deliverable/linux.git] / tools / perf / builtin-record.c
index d15517e849a3ab7b1c3b06507668b6e6f345819e..8f2c16d9275f303aca4c8ba3d7f148fb71a4df00 100644 (file)
@@ -509,10 +509,13 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
        if (!evlist)
                return 0;
 
-       maps = evlist->mmap;
+       maps = backward ? evlist->backward_mmap : evlist->mmap;
        if (!maps)
                return 0;
 
+       if (backward && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
+               return 0;
+
        for (i = 0; i < evlist->nr_mmaps; i++) {
                struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap;
 
@@ -538,6 +541,8 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
        if (bytes_written != rec->bytes_written)
                rc = record__write(rec, &finished_round_event, sizeof(finished_round_event));
 
+       if (backward)
+               perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
 out:
        return rc;
 }
@@ -550,7 +555,7 @@ static int record__mmap_read_all(struct record *rec)
        if (err)
                return err;
 
-       return err;
+       return record__mmap_read_evlist(rec, rec->evlist, true);
 }
 
 static void record__init_features(struct record *rec)
@@ -599,13 +604,16 @@ record__finish_output(struct record *rec)
        return;
 }
 
-static int record__synthesize_workload(struct record *rec)
+static int record__synthesize_workload(struct record *rec, bool tail)
 {
        struct {
                struct thread_map map;
                struct thread_map_data map_data;
        } thread_map;
 
+       if (rec->opts.tail_synthesize != tail)
+               return 0;
+
        thread_map.map.nr = 1;
        thread_map.map.map[0].pid = rec->evlist->workload.pid;
        thread_map.map.map[0].comm = NULL;
@@ -616,7 +624,7 @@ static int record__synthesize_workload(struct record *rec)
                                                 rec->opts.proc_map_timeout);
 }
 
-static int record__synthesize(struct record *rec);
+static int record__synthesize(struct record *rec, bool tail);
 
 static int
 record__switch_output(struct record *rec, bool at_exit)
@@ -627,6 +635,10 @@ record__switch_output(struct record *rec, bool at_exit)
        /* Same Size:      "2015122520103046"*/
        char timestamp[] = "InvalidTimestamp";
 
+       record__synthesize(rec, true);
+       if (target__none(&rec->opts.target))
+               record__synthesize_workload(rec, true);
+
        rec->samples = 0;
        record__finish_output(rec);
        err = fetch_current_timestamp(timestamp, sizeof(timestamp));
@@ -649,7 +661,7 @@ record__switch_output(struct record *rec, bool at_exit)
 
        /* Output tracking events */
        if (!at_exit) {
-               record__synthesize(rec);
+               record__synthesize(rec, false);
 
                /*
                 * In 'perf record --switch-output' without -a,
@@ -661,7 +673,7 @@ record__switch_output(struct record *rec, bool at_exit)
                 * perf_event__synthesize_thread_map() for those events.
                 */
                if (target__none(&rec->opts.target))
-                       record__synthesize_workload(rec);
+                       record__synthesize_workload(rec, false);
        }
        return fd;
 }
@@ -696,8 +708,12 @@ perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused
 static const struct perf_event_mmap_page *
 perf_evlist__pick_pc(struct perf_evlist *evlist)
 {
-       if (evlist && evlist->mmap && evlist->mmap[0].base)
-               return evlist->mmap[0].base;
+       if (evlist) {
+               if (evlist->mmap && evlist->mmap[0].base)
+                       return evlist->mmap[0].base;
+               if (evlist->backward_mmap && evlist->backward_mmap[0].base)
+                       return evlist->backward_mmap[0].base;
+       }
        return NULL;
 }
 
@@ -711,7 +727,7 @@ static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
        return NULL;
 }
 
-static int record__synthesize(struct record *rec)
+static int record__synthesize(struct record *rec, bool tail)
 {
        struct perf_session *session = rec->session;
        struct machine *machine = &session->machines.host;
@@ -721,6 +737,9 @@ static int record__synthesize(struct record *rec)
        int fd = perf_data_file__fd(file);
        int err = 0;
 
+       if (rec->opts.tail_synthesize != tail)
+               return 0;
+
        if (file->is_pipe) {
                err = perf_event__synthesize_attrs(tool, session,
                                                   process_synthesized_event);
@@ -884,7 +903,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
        machine = &session->machines.host;
 
-       err = record__synthesize(rec);
+       err = record__synthesize(rec, false);
        if (err < 0)
                goto out_child;
 
@@ -944,6 +963,17 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
        for (;;) {
                unsigned long long hits = rec->samples;
 
+               /*
+                * rec->evlist->bkw_mmap_state is possible to be
+                * BKW_MMAP_EMPTY here: when done == true and
+                * hits != rec->samples in previous round.
+                *
+                * perf_evlist__toggle_bkw_mmap ensure we never
+                * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
+                */
+               if (trigger_is_hit(&switch_output_trigger) || done || draining)
+                       perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
+
                if (record__mmap_read_all(rec) < 0) {
                        trigger_error(&auxtrace_snapshot_trigger);
                        trigger_error(&switch_output_trigger);
@@ -963,8 +993,26 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
                }
 
                if (trigger_is_hit(&switch_output_trigger)) {
+                       /*
+                        * If switch_output_trigger is hit, the data in
+                        * overwritable ring buffer should have been collected,
+                        * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
+                        *
+                        * If SIGUSR2 raise after or during record__mmap_read_all(),
+                        * record__mmap_read_all() didn't collect data from
+                        * overwritable ring buffer. Read again.
+                        */
+                       if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
+                               continue;
                        trigger_ready(&switch_output_trigger);
 
+                       /*
+                        * Reenable events in overwrite ring buffer after
+                        * record__mmap_read_all(): we should have collected
+                        * data from it.
+                        */
+                       perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
+
                        if (!quiet)
                                fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
                                        waking);
@@ -1019,6 +1067,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
        if (!quiet)
                fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
 
+       if (target__none(&rec->opts.target))
+               record__synthesize_workload(rec, true);
+
 out_child:
        if (forks) {
                int exit_status;
@@ -1037,6 +1088,7 @@ out_child:
        } else
                status = err;
 
+       record__synthesize(rec, true);
        /* this will be recalculated during process_buildids() */
        rec->samples = 0;
 
@@ -1361,6 +1413,9 @@ struct option __record_options[] = {
        OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
                        &record.opts.no_inherit_set,
                        "child tasks do not inherit counters"),
+       OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
+                   "synthesize non-sample events at the end of output"),
+       OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
        OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
        OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
                     "number of mmap data pages and AUX area tracing mmap pages",
@@ -1571,6 +1626,9 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
                }
        }
 
+       if (record.opts.overwrite)
+               record.opts.tail_synthesize = true;
+
        if (rec->evlist->nr_entries == 0 &&
            perf_evlist__add_default(rec->evlist) < 0) {
                pr_err("Not enough memory for event selector list\n");
This page took 0.037086 seconds and 5 git commands to generate.