Skip to content

Fix nagle, other minor optimizations #8222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions common/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ bool daemon_developer_mode(char *argv[])
kill(getpid(), SIGSTOP);
}

/* This checks for any tal_steal loops! */
add_steal_notifiers(NULL);
/* This checks for any tal_steal loops, but it's not free:
* only use if we're already using the fairly heavy memleak
* detection. */
if (getenv("LIGHTNINGD_DEV_MEMLEAK"))
add_steal_notifiers(NULL);

return true;
}
18 changes: 3 additions & 15 deletions connectd/multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ void setup_peer_gossip_store(struct peer *peer,
static void set_urgent_flag(struct peer *peer, bool urgent)
{
int val;
int opt;
const char *optname;

if (urgent == peer->urgent)
return;
Expand All @@ -318,23 +316,13 @@ static void set_urgent_flag(struct peer *peer, bool urgent)
if (peer->is_websocket != NORMAL_SOCKET)
return;

#ifdef TCP_CORK
opt = TCP_CORK;
optname = "TCP_CORK";
#elif defined(TCP_NODELAY)
opt = TCP_NODELAY;
optname = "TCP_NODELAY";
#else
#error "Please report platform with neither TCP_CORK nor TCP_NODELAY?"
#endif

val = urgent;
if (setsockopt(io_conn_fd(peer->to_peer),
IPPROTO_TCP, opt, &val, sizeof(val)) != 0
IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) != 0
/* This actually happens in testing, where we blackhole the fd */
&& peer->daemon->dev_disconnect_fd == -1) {
status_broken("setsockopt %s=1 fd=%u: %s",
optname, io_conn_fd(peer->to_peer),
status_broken("setsockopt TCP_NODELAY=1 fd=%u: %s",
io_conn_fd(peer->to_peer),
strerror(errno));
}
peer->urgent = urgent;
Expand Down
29 changes: 20 additions & 9 deletions lightningd/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,14 @@ static void log_to_files(const char *log_prefix,
struct log_file **log_files)
{
char tstamp[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ ")];
char *entry, *nodestr;
char *entry, nodestr[hex_str_size(PUBKEY_CMPR_LEN)];
char buf[sizeof("%s%s%s %s-%s: %s\n")
+ strlen(log_prefix)
+ sizeof(tstamp)
+ strlen(level_prefix(level))
+ sizeof(nodestr)
+ strlen(entry_prefix)
+ strlen(str)];
bool filtered;

if (print_timestamps) {
Expand All @@ -218,9 +225,10 @@ static void log_to_files(const char *log_prefix,
tstamp[0] = '\0';

if (node_id)
nodestr = fmt_node_id(tmpctx, node_id);
hex_encode(node_id->k, sizeof(node_id->k),
nodestr, sizeof(nodestr));
else
nodestr = "";
nodestr[0] = '\0';
if (level == LOG_IO_IN || level == LOG_IO_OUT) {
const char *dir = level == LOG_IO_IN ? "[IN]" : "[OUT]";
char *hex = tal_hexstr(NULL, io, io_len);
Expand All @@ -234,14 +242,17 @@ static void log_to_files(const char *log_prefix,
entry_prefix, str, dir, hex);
tal_free(hex);
} else {
size_t len;
entry = buf;
if (!node_id)
entry = tal_fmt(tmpctx, "%s%s%s %s: %s\n",
log_prefix, tstamp, level_prefix(level), entry_prefix, str);
len = snprintf(buf, "%s%s%s %s: %s\n",
log_prefix, tstamp, level_prefix(level), entry_prefix, str);
else
entry = tal_fmt(tmpctx, "%s%s%s %s-%s: %s\n",
log_prefix, tstamp, level_prefix(level),
nodestr,
entry_prefix, str);
len = snprintf(buf, "%s%s%s %s-%s: %s\n",
log_prefix, tstamp, level_prefix(level),
nodestr,
entry_prefix, str);
assert(len < sizeof(buf));
}

/* In complex configurations, we tell loggers to overshare: then we
Expand Down
97 changes: 74 additions & 23 deletions lightningd/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ bool notifications_have_topic(const struct plugins *plugins, const char *topic)
}

/* Modern notifications X contain an object X */
static struct jsonrpc_notification *notify_start(const char *name)
static struct jsonrpc_notification *notify_start(struct lightningd *ld,
const char *name)
{
struct jsonrpc_notification *n;

/* Optimization: does anyone care? */
if (!plugins_anyone_cares(ld->plugins, name))
return NULL;

n = jsonrpc_notification_start(NULL, name);
json_object_start(n->stream, name);
return n;
Expand Down Expand Up @@ -71,7 +76,9 @@ void notify_connect(struct lightningd *ld,
bool incoming,
const struct wireaddr_internal *addr)
{
struct jsonrpc_notification *n = notify_start("connect");
struct jsonrpc_notification *n = notify_start(ld, "connect");
if (!n)
return;
connect_notification_serialize(n->stream, nodeid, incoming, addr);
notify_send(ld, n);
}
Expand All @@ -85,7 +92,9 @@ REGISTER_NOTIFICATION(disconnect);

void notify_disconnect(struct lightningd *ld, struct node_id *nodeid)
{
struct jsonrpc_notification *n = notify_start("disconnect");
struct jsonrpc_notification *n = notify_start(ld, "disconnect");
if (!n)
return;
disconnect_notification_serialize(n->stream, nodeid);
notify_send(ld, n);
}
Expand Down Expand Up @@ -114,7 +123,9 @@ REGISTER_NOTIFICATION(warning);

void notify_warning(struct lightningd *ld, struct log_entry *l)
{
struct jsonrpc_notification *n = notify_start("warning");
struct jsonrpc_notification *n = notify_start(ld, "warning");
if (!n)
return;
warning_notification_serialize(n->stream, l);
notify_send(ld, n);
}
Expand All @@ -133,7 +144,9 @@ void notify_custommsg(struct lightningd *ld,
const struct node_id *peer_id,
const u8 *msg)
{
struct jsonrpc_notification *n = notify_start("custommsg");
struct jsonrpc_notification *n = notify_start(ld, "custommsg");
if (!n)
return;
custommsg_notification_serialize(n->stream, peer_id, msg);
notify_send(ld, n);
}
Expand Down Expand Up @@ -167,7 +180,9 @@ void notify_onionmessage_forward_fail(struct lightningd *ld,
const u8 *outgoing,
const struct sciddir_or_pubkey *next_node)
{
struct jsonrpc_notification *n = notify_start("onionmessage_forward_fail");
struct jsonrpc_notification *n = notify_start(ld, "onionmessage_forward_fail");
if (!n)
return;
onionmessage_forward_fail_serialize(n->stream,
source,
incoming,
Expand Down Expand Up @@ -198,7 +213,9 @@ void notify_invoice_payment(struct lightningd *ld,
const struct json_escape *label,
const struct bitcoin_outpoint *outpoint)
{
struct jsonrpc_notification *n = notify_start("invoice_payment");
struct jsonrpc_notification *n = notify_start(ld, "invoice_payment");
if (!n)
return;
invoice_payment_notification_serialize(n->stream, amount, preimage, label, outpoint);
notify_send(ld, n);
}
Expand All @@ -222,7 +239,9 @@ void notify_invoice_creation(struct lightningd *ld,
const struct preimage *preimage,
const struct json_escape *label)
{
struct jsonrpc_notification *n = notify_start("invoice_creation");
struct jsonrpc_notification *n = notify_start(ld, "invoice_creation");
if (!n)
return;
invoice_creation_notification_serialize(n->stream, amount, preimage, label);
notify_send(ld, n);
}
Expand All @@ -249,7 +268,9 @@ void notify_channel_opened(struct lightningd *ld,
const struct bitcoin_txid *funding_txid,
bool channel_ready)
{
struct jsonrpc_notification *n = notify_start("channel_opened");
struct jsonrpc_notification *n = notify_start(ld, "channel_opened");
if (!n)
return;
channel_opened_notification_serialize(n->stream, ld, node_id, funding_sat, funding_txid, channel_ready);
notify_send(ld, n);
}
Expand Down Expand Up @@ -292,7 +313,9 @@ void notify_channel_state_changed(struct lightningd *ld,
enum state_change cause,
const char *message)
{
struct jsonrpc_notification *n = notify_start("channel_state_changed");
struct jsonrpc_notification *n = notify_start(ld, "channel_state_changed");
if (!n)
return;
channel_state_changed_notification_serialize(n->stream, peer_id, cid, scid, timestamp, old_state, new_state, cause, message);
notify_send(ld, n);
}
Expand Down Expand Up @@ -361,7 +384,9 @@ void notify_forward_event(struct lightningd *ld,
u64 created_index,
u64 updated_index)
{
struct jsonrpc_notification *n = notify_start("forward_event");
struct jsonrpc_notification *n = notify_start(ld, "forward_event");
if (!n)
return;
forward_event_notification_serialize(n->stream, in, scid_out, amount_out, state, failcode, resolved_time, forward_style, created_index, updated_index);
notify_send(ld, n);
}
Expand All @@ -371,7 +396,9 @@ REGISTER_NOTIFICATION(sendpay_success);
void notify_sendpay_success(struct lightningd *ld,
const struct wallet_payment *payment)
{
struct jsonrpc_notification *n = notify_start("sendpay_success");
struct jsonrpc_notification *n = notify_start(ld, "sendpay_success");
if (!n)
return;
json_add_payment_fields(n->stream, payment);
notify_send(ld, n);
}
Expand Down Expand Up @@ -407,7 +434,9 @@ void notify_sendpay_failure(struct lightningd *ld,
const struct routing_failure *fail,
const char *errmsg)
{
struct jsonrpc_notification *n = notify_start("sendpay_failure");
struct jsonrpc_notification *n = notify_start(ld, "sendpay_failure");
if (!n)
return;
sendpay_failure_notification_serialize(n->stream, payment, pay_errcode, onionreply, fail, errmsg);
notify_send(ld, n);
}
Expand Down Expand Up @@ -492,7 +521,9 @@ REGISTER_NOTIFICATION(coin_movement);
void notify_coin_mvt(struct lightningd *ld,
const struct coin_mvt *mvt)
{
struct jsonrpc_notification *n = notify_start("coin_movement");
struct jsonrpc_notification *n = notify_start(ld, "coin_movement");
if (!n)
return;
coin_movement_notification_serialize(n->stream, mvt);
notify_send(ld, n);
}
Expand Down Expand Up @@ -522,7 +553,9 @@ REGISTER_NOTIFICATION(balance_snapshot);
void notify_balance_snapshot(struct lightningd *ld,
const struct balance_snapshot *snap)
{
struct jsonrpc_notification *n = notify_start("balance_snapshot");
struct jsonrpc_notification *n = notify_start(ld, "balance_snapshot");
if (!n)
return;
balance_snapshot_serialize(n->stream, snap);
notify_send(ld, n);
}
Expand All @@ -539,7 +572,9 @@ REGISTER_NOTIFICATION(block_added);
void notify_block_added(struct lightningd *ld,
const struct block *block)
{
struct jsonrpc_notification *n = notify_start("block_added");
struct jsonrpc_notification *n = notify_start(ld, "block_added");
if (!n)
return;
block_added_notification_serialize(n->stream, block);
notify_send(ld, n);
}
Expand All @@ -558,7 +593,9 @@ void notify_openchannel_peer_sigs(struct lightningd *ld,
const struct channel_id *cid,
const struct wally_psbt *psbt)
{
struct jsonrpc_notification *n = notify_start("openchannel_peer_sigs");
struct jsonrpc_notification *n = notify_start(ld, "openchannel_peer_sigs");
if (!n)
return;
openchannel_peer_sigs_serialize(n->stream, cid, psbt);
notify_send(ld, n);
}
Expand All @@ -574,7 +611,9 @@ REGISTER_NOTIFICATION(channel_open_failed);
void notify_channel_open_failed(struct lightningd *ld,
const struct channel_id *cid)
{
struct jsonrpc_notification *n = notify_start("channel_open_failed");
struct jsonrpc_notification *n = notify_start(ld, "channel_open_failed");
if (!n)
return;
channel_open_failed_serialize(n->stream, cid);
notify_send(ld, n);
}
Expand All @@ -583,7 +622,9 @@ REGISTER_NOTIFICATION(shutdown);

bool notify_plugin_shutdown(struct lightningd *ld, struct plugin *p)
{
struct jsonrpc_notification *n = notify_start("shutdown");
struct jsonrpc_notification *n = notify_start(ld, "shutdown");
if (!n)
return false;
json_object_end(n->stream);
jsonrpc_notification_end(n);
return plugin_single_notify(p, take(n));
Expand All @@ -593,7 +634,9 @@ bool notify_deprecated_oneshot(struct lightningd *ld,
struct plugin *p,
bool deprecated_ok)
{
struct jsonrpc_notification *n = notify_start("deprecated_oneshot");
struct jsonrpc_notification *n = notify_start(ld, "deprecated_oneshot");
if (!n)
return false;
json_add_bool(n->stream, "deprecated_ok", deprecated_ok);
json_object_end(n->stream);
jsonrpc_notification_end(n);
Expand All @@ -616,7 +659,11 @@ REGISTER_NOTIFICATION(log);

void notify_log(struct lightningd *ld, const struct log_entry *l)
{
struct jsonrpc_notification *n = notify_start("log");
struct jsonrpc_notification *n;

n = notify_start(ld, "log");
if (!n)
return;
log_notification_serialize(n->stream, l);
notify_send(ld, n);
}
Expand All @@ -637,7 +684,9 @@ REGISTER_NOTIFICATION(plugin_started);

void notify_plugin_started(struct lightningd *ld, struct plugin *plugin)
{
struct jsonrpc_notification *n = notify_start("plugin_started");
struct jsonrpc_notification *n = notify_start(ld, "plugin_started");
if (!n)
return;
plugin_notification_serialize(n->stream, plugin);
notify_send(ld, n);
}
Expand All @@ -646,7 +695,9 @@ REGISTER_NOTIFICATION(plugin_stopped);

void notify_plugin_stopped(struct lightningd *ld, struct plugin *plugin)
{
struct jsonrpc_notification *n = notify_start("plugin_stopped");
struct jsonrpc_notification *n = notify_start(ld, "plugin_stopped");
if (!n)
return;
plugin_notification_serialize(n->stream, plugin);
notify_send(ld, n);
}
Loading
Loading