Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Benchmark synthesis of perf events such as at the start of a 'perf
0004  * record'. Synthesis is done on the current process and the 'dummy' event
0005  * handlers are invoked that support dump_trace but otherwise do nothing.
0006  *
0007  * Copyright 2019 Google LLC.
0008  */
0009 #include <stdio.h>
0010 #include "bench.h"
0011 #include "../util/debug.h"
0012 #include "../util/session.h"
0013 #include "../util/stat.h"
0014 #include "../util/synthetic-events.h"
0015 #include "../util/target.h"
0016 #include "../util/thread_map.h"
0017 #include "../util/tool.h"
0018 #include "../util/util.h"
0019 #include <linux/atomic.h>
0020 #include <linux/err.h>
0021 #include <linux/time64.h>
0022 #include <subcmd/parse-options.h>
0023 
0024 static unsigned int min_threads = 1;
0025 static unsigned int max_threads = UINT_MAX;
0026 static unsigned int single_iterations = 10000;
0027 static unsigned int multi_iterations = 10;
0028 static bool run_st;
0029 static bool run_mt;
0030 
0031 static const struct option options[] = {
0032     OPT_BOOLEAN('s', "st", &run_st, "Run single threaded benchmark"),
0033     OPT_BOOLEAN('t', "mt", &run_mt, "Run multi-threaded benchmark"),
0034     OPT_UINTEGER('m', "min-threads", &min_threads,
0035         "Minimum number of threads in multithreaded bench"),
0036     OPT_UINTEGER('M', "max-threads", &max_threads,
0037         "Maximum number of threads in multithreaded bench"),
0038     OPT_UINTEGER('i', "single-iterations", &single_iterations,
0039         "Number of iterations used to compute single-threaded average"),
0040     OPT_UINTEGER('I', "multi-iterations", &multi_iterations,
0041         "Number of iterations used to compute multi-threaded average"),
0042     OPT_END()
0043 };
0044 
0045 static const char *const bench_usage[] = {
0046     "perf bench internals synthesize <options>",
0047     NULL
0048 };
0049 
0050 static atomic_t event_count;
0051 
0052 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
0053                      union perf_event *event __maybe_unused,
0054                      struct perf_sample *sample __maybe_unused,
0055                      struct machine *machine __maybe_unused)
0056 {
0057     atomic_inc(&event_count);
0058     return 0;
0059 }
0060 
0061 static int do_run_single_threaded(struct perf_session *session,
0062                 struct perf_thread_map *threads,
0063                 struct target *target, bool data_mmap)
0064 {
0065     const unsigned int nr_threads_synthesize = 1;
0066     struct timeval start, end, diff;
0067     u64 runtime_us;
0068     unsigned int i;
0069     double time_average, time_stddev, event_average, event_stddev;
0070     int err;
0071     struct stats time_stats, event_stats;
0072 
0073     init_stats(&time_stats);
0074     init_stats(&event_stats);
0075 
0076     for (i = 0; i < single_iterations; i++) {
0077         atomic_set(&event_count, 0);
0078         gettimeofday(&start, NULL);
0079         err = __machine__synthesize_threads(&session->machines.host,
0080                         NULL,
0081                         target, threads,
0082                         process_synthesized_event,
0083                         true, data_mmap,
0084                         nr_threads_synthesize);
0085         if (err)
0086             return err;
0087 
0088         gettimeofday(&end, NULL);
0089         timersub(&end, &start, &diff);
0090         runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
0091         update_stats(&time_stats, runtime_us);
0092         update_stats(&event_stats, atomic_read(&event_count));
0093     }
0094 
0095     time_average = avg_stats(&time_stats);
0096     time_stddev = stddev_stats(&time_stats);
0097     printf("  Average %ssynthesis took: %.3f usec (+- %.3f usec)\n",
0098         data_mmap ? "data " : "", time_average, time_stddev);
0099 
0100     event_average = avg_stats(&event_stats);
0101     event_stddev = stddev_stats(&event_stats);
0102     printf("  Average num. events: %.3f (+- %.3f)\n",
0103         event_average, event_stddev);
0104 
0105     printf("  Average time per event %.3f usec\n",
0106         time_average / event_average);
0107     return 0;
0108 }
0109 
0110 static int run_single_threaded(void)
0111 {
0112     struct perf_session *session;
0113     struct target target = {
0114         .pid = "self",
0115     };
0116     struct perf_thread_map *threads;
0117     int err;
0118 
0119     perf_set_singlethreaded();
0120     session = perf_session__new(NULL, NULL);
0121     if (IS_ERR(session)) {
0122         pr_err("Session creation failed.\n");
0123         return PTR_ERR(session);
0124     }
0125     threads = thread_map__new_by_pid(getpid());
0126     if (!threads) {
0127         pr_err("Thread map creation failed.\n");
0128         err = -ENOMEM;
0129         goto err_out;
0130     }
0131 
0132     puts(
0133 "Computing performance of single threaded perf event synthesis by\n"
0134 "synthesizing events on the perf process itself:");
0135 
0136     err = do_run_single_threaded(session, threads, &target, false);
0137     if (err)
0138         goto err_out;
0139 
0140     err = do_run_single_threaded(session, threads, &target, true);
0141 
0142 err_out:
0143     if (threads)
0144         perf_thread_map__put(threads);
0145 
0146     perf_session__delete(session);
0147     return err;
0148 }
0149 
0150 static int do_run_multi_threaded(struct target *target,
0151                 unsigned int nr_threads_synthesize)
0152 {
0153     struct timeval start, end, diff;
0154     u64 runtime_us;
0155     unsigned int i;
0156     double time_average, time_stddev, event_average, event_stddev;
0157     int err;
0158     struct stats time_stats, event_stats;
0159     struct perf_session *session;
0160 
0161     init_stats(&time_stats);
0162     init_stats(&event_stats);
0163     for (i = 0; i < multi_iterations; i++) {
0164         session = perf_session__new(NULL, NULL);
0165         if (IS_ERR(session))
0166             return PTR_ERR(session);
0167 
0168         atomic_set(&event_count, 0);
0169         gettimeofday(&start, NULL);
0170         err = __machine__synthesize_threads(&session->machines.host,
0171                         NULL,
0172                         target, NULL,
0173                         process_synthesized_event,
0174                         true, false,
0175                         nr_threads_synthesize);
0176         if (err) {
0177             perf_session__delete(session);
0178             return err;
0179         }
0180 
0181         gettimeofday(&end, NULL);
0182         timersub(&end, &start, &diff);
0183         runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
0184         update_stats(&time_stats, runtime_us);
0185         update_stats(&event_stats, atomic_read(&event_count));
0186         perf_session__delete(session);
0187     }
0188 
0189     time_average = avg_stats(&time_stats);
0190     time_stddev = stddev_stats(&time_stats);
0191     printf("    Average synthesis took: %.3f usec (+- %.3f usec)\n",
0192         time_average, time_stddev);
0193 
0194     event_average = avg_stats(&event_stats);
0195     event_stddev = stddev_stats(&event_stats);
0196     printf("    Average num. events: %.3f (+- %.3f)\n",
0197         event_average, event_stddev);
0198 
0199     printf("    Average time per event %.3f usec\n",
0200         time_average / event_average);
0201     return 0;
0202 }
0203 
0204 static int run_multi_threaded(void)
0205 {
0206     struct target target = {
0207         .cpu_list = "0"
0208     };
0209     unsigned int nr_threads_synthesize;
0210     int err;
0211 
0212     if (max_threads == UINT_MAX)
0213         max_threads = sysconf(_SC_NPROCESSORS_ONLN);
0214 
0215     puts(
0216 "Computing performance of multi threaded perf event synthesis by\n"
0217 "synthesizing events on CPU 0:");
0218 
0219     for (nr_threads_synthesize = min_threads;
0220          nr_threads_synthesize <= max_threads;
0221          nr_threads_synthesize++) {
0222         if (nr_threads_synthesize == 1)
0223             perf_set_singlethreaded();
0224         else
0225             perf_set_multithreaded();
0226 
0227         printf("  Number of synthesis threads: %u\n",
0228             nr_threads_synthesize);
0229 
0230         err = do_run_multi_threaded(&target, nr_threads_synthesize);
0231         if (err)
0232             return err;
0233     }
0234     perf_set_singlethreaded();
0235     return 0;
0236 }
0237 
0238 int bench_synthesize(int argc, const char **argv)
0239 {
0240     int err = 0;
0241 
0242     argc = parse_options(argc, argv, options, bench_usage, 0);
0243     if (argc) {
0244         usage_with_options(bench_usage, options);
0245         exit(EXIT_FAILURE);
0246     }
0247 
0248     /*
0249      * If neither single threaded or multi-threaded are specified, default
0250      * to running just single threaded.
0251      */
0252     if (!run_st && !run_mt)
0253         run_st = true;
0254 
0255     if (run_st)
0256         err = run_single_threaded();
0257 
0258     if (!err && run_mt)
0259         err = run_multi_threaded();
0260 
0261     return err;
0262 }