You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
8.2 KiB

#include <assert.h>
#include <bitcoin/pubkey.h>
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
#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 <stdio.h>
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void status_fmt(enum log_level level UNUSED, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
static bool in_bench = 0;
/* We use made-up pubkeys, so don't try to format them. */
static char *fake_type_to_string_(const tal_t *ctx, const char *typename,
union printable_types u)
{
/* We *do* call this at end of route setup. */
if (streq(typename, "struct pubkey")) {
size_t n;
memcpy(&n, u.pubkey, sizeof(n));
return tal_fmt(ctx, "pubkey-#%zu", n);
}
return type_to_string_(ctx, typename, u);
}
/* Only used on setup: if it was in benchmark run, we'd need the real one */
static int fake_pubkey_cmp(const struct pubkey *a, const struct pubkey *b)
{
assert(!in_bench);
return memcmp(a, b, sizeof(*a));
}
#define pubkey_cmp fake_pubkey_cmp
#define type_to_string_ fake_type_to_string_
#include "../routing.c"
#undef type_to_string_
struct broadcast_state *new_broadcast_state(tal_t *ctx UNNEEDED)
{
return NULL;
}
/* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire_channel_announcement */
bool fromwire_channel_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, secp256k1_ecdsa_signature *node_signature_1 UNNEEDED, secp256k1_ecdsa_signature *node_signature_2 UNNEEDED, secp256k1_ecdsa_signature *bitcoin_signature_1 UNNEEDED, secp256k1_ecdsa_signature *bitcoin_signature_2 UNNEEDED, u8 **features UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, struct pubkey *node_id_1 UNNEEDED, struct pubkey *node_id_2 UNNEEDED, struct pubkey *bitcoin_key_1 UNNEEDED, struct pubkey *bitcoin_key_2 UNNEEDED)
{ fprintf(stderr, "fromwire_channel_announcement called!\n"); abort(); }
/* Generated stub for fromwire_channel_update */
bool fromwire_channel_update(const void *p UNNEEDED, secp256k1_ecdsa_signature *signature UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, u32 *timestamp UNNEEDED, u16 *flags UNNEEDED, u16 *cltv_expiry_delta UNNEEDED, u64 *htlc_minimum_msat UNNEEDED, u32 *fee_base_msat UNNEEDED, u32 *fee_proportional_millionths UNNEEDED)
{ fprintf(stderr, "fromwire_channel_update called!\n"); abort(); }
/* Generated stub for fromwire_node_announcement */
bool fromwire_node_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, secp256k1_ecdsa_signature *signature UNNEEDED, u8 **features UNNEEDED, u32 *timestamp UNNEEDED, struct pubkey *node_id UNNEEDED, u8 rgb_color[3] UNNEEDED, u8 alias[32] UNNEEDED, u8 **addresses UNNEEDED)
{ fprintf(stderr, "fromwire_node_announcement called!\n"); abort(); }
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
/* Generated stub for onion_type_name */
const char *onion_type_name(int e UNNEEDED)
{ fprintf(stderr, "onion_type_name called!\n"); abort(); }
/* Generated stub for replace_broadcast */
bool replace_broadcast(struct broadcast_state *bstate UNNEEDED,
u64 *index UNNEEDED,
const u8 *tag UNNEEDED,
const u8 *payload UNNEEDED)
{ fprintf(stderr, "replace_broadcast called!\n"); abort(); }
/* Generated stub for sanitize_error */
char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED,
struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "sanitize_error called!\n"); abort(); }
/* Generated stub for status_failed */
void status_failed(enum status_failreason code UNNEEDED,
const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "status_failed called!\n"); abort(); }
/* Generated stub for towire_errorfmt */
u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
const struct channel_id *channel UNNEEDED,
const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "towire_errorfmt called!\n"); abort(); }
/* Generated stub for towire_pubkey */
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
/* Generated stub for towire_short_channel_id */
void towire_short_channel_id(u8 **pptr UNNEEDED,
const struct short_channel_id *short_channel_id UNNEEDED)
{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
const void *trc;
/* Updates existing route if required. */
static struct half_chan *add_connection(struct routing_state *rstate,
const struct pubkey *from,
const struct pubkey *to,
u32 base_fee, s32 proportional_fee,
u32 delay)
{
struct short_channel_id scid;
struct half_chan *c;
struct chan *chan;
memset(&scid, 0, sizeof(scid));
chan = get_channel(rstate, &scid);
if (!chan)
chan = new_chan(rstate, &scid, from, to);
c = &chan->half[pubkey_idx(from, to)];
c->base_fee = base_fee;
c->proportional_fee = proportional_fee;
c->delay = delay;
c->active = true;
c->flags = get_channel_direction(from, to);
return c;
}
static struct pubkey nodeid(size_t n)
{
struct pubkey id;
memset(&id, 0, sizeof(id));
memcpy(&id, &n, sizeof(n));
return id;
}
static void populate_random_node(struct routing_state *rstate, u64 n)
{
struct pubkey id = nodeid(n);
/* Create 2 random channels. */
if (n < 1)
return;
for (size_t i = 0; i < 2; i++) {
struct pubkey randnode = nodeid(pseudorand(n));
add_connection(rstate, &id, &randnode,
pseudorand(100),
pseudorand(100),
pseudorand(144));
add_connection(rstate, &randnode, &id,
pseudorand(100),
pseudorand(100),
pseudorand(144));
}
}
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
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 bitcoin_blkid zerohash;
const tal_t *ctx = trc = tal_tmpctx(NULL);
struct routing_state *rstate;
size_t num_nodes = 100, num_runs = 1;
struct timemono start, end;
size_t num_success;
struct pubkey me = nodeid(0);
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
bool perfme = false;
const double riskfactor = 0.01 / BLOCKS_PER_YEAR / 10000;
struct siphash_seed base_seed;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
rstate = new_routing_state(ctx, &zerohash, &me, 0);
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
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]);
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
if (argc > 3)
opt_usage_and_exit("[num_nodes [num_runs]]");
memset(&base_seed, 0, sizeof(base_seed));
for (size_t i = 0; i < num_nodes; i++)
populate_random_node(rstate, i);
in_bench = true;
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
if (perfme)
run("perfme-start");
start = time_mono();
num_success = 0;
for (size_t i = 0; i < num_runs; i++) {
struct pubkey from = nodeid(pseudorand(num_nodes));
struct pubkey to = nodeid(pseudorand(num_nodes));
u64 fee;
struct chan **route;
route = find_route(ctx, rstate, &from, &to,
pseudorand(100000),
riskfactor,
0.75, &base_seed,
&fee);
num_success += (route != NULL);
tal_free(route);
}
end = time_mono();
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
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)),
time_to_nsec(time_divide(timemono_between(end, start), num_runs)));
tal_free(ctx);
secp256k1_context_destroy(secp256k1_ctx);
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 &lt;http://www.gnu.org/licenses/&gt;. */ #include &lt;ccan/err/err.h&gt; #include &lt;ccan/str/str.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;stdlib.h&gt; #define PERFME_PREFIX &#34;/tmp/perfme.&#34; #define MAX_ENV_ARGS 20 static void write_noerr(int fd) { int e = errno; if (write(fd, &#34;&#34;, 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 &lt; 0) { write_noerr(pfd[1]); err(1, &#34;opening %s&#34;, perfpid); } sprintf(pid, &#34;%u&#34;, getpid()); if (write(fd, pid, strlen(pid)) != strlen(pid)) { write_noerr(pfd[1]); err(1, &#34;writing to %s&#34;, perfpid); } close(fd); sprintf(pid, &#34;%u&#34;, parent); cmd = getenv(&#34;PERFME&#34;); if (!cmd) cmd = &#34;perf record --call-graph dwarf -q&#34;; cmd = strdup(cmd); for (i = 0; i &lt; MAX_ENV_ARGS; i++) { args[i] = strtok(i == 0 ? cmd : NULL, &#34; &#34;); if (!args[i]) break; } if (i == 0 || i == MAX_ENV_ARGS) errx(1, &#34;Too %s args in $PERFME: &#39;%s&#39;&#34;, i ? &#34;many&#34; : &#34;few&#34;, getenv(&#34;PERFME&#34;)); args[i++] = &#34;-p&#34;; args[i++] = pid; args[i++] = &#34;-o&#34;; args[i++] = (char *)perfout; args[i++] = NULL; execvp(args[0], args); write_noerr(pfd[1]); err(1, &#34;Execing %s&#34;, 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(&#34;.pid&#34;)]; err_set_progname(argv[0]); sprintf(perfpid, PERFME_PREFIX &#34;%u.pid&#34;, parent); if (strends(argv[0], &#34;perfme-stop&#34;)) { char pid[STR_MAX_CHARS(pid_t)]; int r, fd = open(perfpid, O_RDONLY); if (fd &lt; 0) err(1, &#34;Opening %s&#34;, perfpid); r = read(fd, pid, sizeof(pid) - 1); if (r &lt; 0) err(1, &#34;Reading %s&#34;, perfpid); pid[r] = 0; if (unlink(perfpid) != 0) warn(&#34;Unlinking %s&#34;, perfpid); if (atoi(pid) &lt;= 0) errx(1, &#34;Invalid pid &#39;%s&#39; from %s&#34;, pid, perfpid); if (kill(atoi(pid), SIGTERM) != 0) err(1, &#34;Stopping %s&#34;, pid); exit(0); } else if (strends(argv[0], &#34;perfme-start&#34;)) { int pfd[2]; sprintf(perfout, PERFME_PREFIX &#34;%u&#34;, parent); /* Use pipe to detect successful exec. */ if (pipe(pfd) != 0) err(1, &#34;Creating pipe&#34;); 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, &#34;Forking&#34;); default: /* Parent. Wait for child. */ close(pfd[1]); if (read(pfd[0], perfpid, 1) == 1) exit(1); fprintf(stderr, &#34;Perf recording into %s\n&#34;, perfout); sleep(1); exit(0); } } errx(1, &#34;Unknown name: am I perfme-start or perfme-stop?&#34;); } Signed-off-by: Rusty Russell &lt;rusty@rustcorp.com.au&gt;
7 years ago
opt_free_table();
return 0;
}