mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-21 14:24:09 +01:00
gossipd/test/run-bench-find_route.c: add perfme support.
Compile this, and link from perfme-start and perfme-stop in your path: /* Simple wrapper to allow a program to perf itself. * Copyright Rusty Russell, Blockstream 2015. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * See <http://www.gnu.org/licenses/>. */ #include <ccan/err/err.h> #include <ccan/str/str.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <errno.h> #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #define PERFME_PREFIX "/tmp/perfme." #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, "", 1) != 1) /* Complain about warn_unused_result fascist bullshit */ ; errno = e; } /* Child. Setup pid, run perf. */ static void exec_perf(int pfd[2], const char *perfpid, const char *perfout, pid_t parent) { char pid[STR_MAX_CHARS(pid_t)]; int i, fd; char *cmd, *args[MAX_ENV_ARGS + 5]; fd = open(perfpid, O_CREAT|O_EXCL|O_WRONLY, 0400); if (fd < 0) { write_noerr(pfd[1]); err(1, "opening %s", perfpid); } sprintf(pid, "%u", getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, "writing to %s", perfpid); } close(fd); sprintf(pid, "%u", parent); cmd = getenv("PERFME"); if (!cmd) cmd = "perf record --call-graph dwarf -q"; cmd = strdup(cmd); for (i = 0; i < MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, " "); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, "Too %s args in $PERFME: '%s'", i ? "many" : "few", getenv("PERFME")); args[i++] = "-p"; args[i++] = pid; args[i++] = "-o"; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, "Execing %s", args[0]); } int main(int argc, char *argv[]) { pid_t parent = argv[1] ? atoi(argv[1]) : getppid(); char perfout[sizeof(PERFME_PREFIX) + STR_MAX_CHARS(parent)]; char perfpid[sizeof(perfout) + sizeof(".pid")]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX "%u.pid", parent); if (strends(argv[0], "perfme-stop")) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd < 0) err(1, "Opening %s", perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r < 0) err(1, "Reading %s", perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn("Unlinking %s", perfpid); if (atoi(pid) <= 0) errx(1, "Invalid pid '%s' from %s", pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, "Stopping %s", pid); exit(0); } else if (strends(argv[0], "perfme-start")) { int pfd[2]; sprintf(perfout, PERFME_PREFIX "%u", parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, "Creating pipe"); switch (fork()) { case 0: close(pfd[0]); fcntl(pfd[1], F_SETFD, fcntl(pfd[1], F_GETFD)|FD_CLOEXEC); exec_perf(pfd, perfpid, perfout, parent); case -1: err(1, "Forking"); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, "Perf recording into %s\n", perfout); sleep(1); exit(0); } } errx(1, "Unknown name: am I perfme-start or perfme-stop?"); } Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
ee06c71efc
commit
21cc904b03
1 changed files with 37 additions and 1 deletions
|
@ -1,10 +1,15 @@
|
|||
#include <assert.h>
|
||||
#include <bitcoin/pubkey.h>
|
||||
#include <ccan/err/err.h>
|
||||
#include <ccan/opt/opt.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <ccan/time/time.h>
|
||||
#include <common/pseudorand.h>
|
||||
#include <common/status.h>
|
||||
#include <common/type_to_string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#define status_trace(fmt, ...) \
|
||||
|
@ -126,6 +131,23 @@ static void populate_random_node(struct routing_state *rstate, u64 n)
|
|||
}
|
||||
}
|
||||
|
||||
static void run(const char *name)
|
||||
{
|
||||
int status;
|
||||
|
||||
switch (fork()) {
|
||||
case 0:
|
||||
execlp(name, name, NULL);
|
||||
exit(127);
|
||||
case -1:
|
||||
err(1, "forking %s", name);
|
||||
default:
|
||||
wait(&status);
|
||||
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
||||
errx(1, "%s failed", name);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
static const struct sha256_double zerohash;
|
||||
|
@ -135,21 +157,31 @@ int main(int argc, char *argv[])
|
|||
struct timemono start, end;
|
||||
size_t num_success;
|
||||
struct pubkey me = nodeid(0);
|
||||
bool perfme = false;
|
||||
|
||||
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
||||
| SECP256K1_CONTEXT_SIGN);
|
||||
|
||||
rstate = new_routing_state(ctx, &zerohash, &me);
|
||||
opt_register_noarg("--perfme", opt_set_bool, &perfme,
|
||||
"Run perfme-start and perfme-stop around benchmark");
|
||||
|
||||
opt_parse(&argc, argv, opt_log_stderr_exit);
|
||||
|
||||
if (argc > 1)
|
||||
num_nodes = atoi(argv[1]);
|
||||
if (argc > 2)
|
||||
num_runs = atoi(argv[2]);
|
||||
|
||||
if (argc > 3)
|
||||
opt_usage_and_exit("[num_nodes [num_runs]]");
|
||||
|
||||
for (size_t i = 0; i < num_nodes; i++)
|
||||
populate_random_node(rstate, i);
|
||||
|
||||
in_bench = true;
|
||||
if (perfme)
|
||||
run("perfme-start");
|
||||
|
||||
start = time_mono();
|
||||
num_success = 0;
|
||||
for (size_t i = 0; i < num_runs; i++) {
|
||||
|
@ -167,6 +199,9 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
end = time_mono();
|
||||
|
||||
if (perfme)
|
||||
run("perfme-stop");
|
||||
|
||||
printf("%zu (%zu succeeded) routes in %zu nodes in %"PRIu64" msec (%"PRIu64" nanoseconds per route)",
|
||||
num_runs, num_success, num_nodes,
|
||||
time_to_msec(timemono_between(end, start)),
|
||||
|
@ -174,5 +209,6 @@ int main(int argc, char *argv[])
|
|||
|
||||
tal_free(ctx);
|
||||
secp256k1_context_destroy(secp256k1_ctx);
|
||||
opt_free_table();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue