diff --git a/core/fiber.m b/core/fiber.m index c388fed07bd2ea8652874d3f9a6b2bcbeca05ceb..a525e31d1a36a2f010a6e896cf842db60de7ef8f 100644 --- a/core/fiber.m +++ b/core/fiber.m @@ -694,11 +694,25 @@ fiber_bread(struct tbuf *buf, size_t at_least) } void -add_iov_dup(const void *buf, size_t len) +iov_add(const void *buf, size_t len) +{ + iov_ensure(1); + iov_add_unsafe(buf, len); +} + +void +iov_dup(const void *buf, size_t len) { void *copy = palloc(fiber->gc_pool, len); memcpy(copy, buf, len); - add_iov(copy, len); + iov_add(copy, len); +} + +void +iov_reset() +{ + fiber->iov_cnt = 0; /* discard anything unwritten */ + tbuf_reset(fiber->iov); } /** @@ -706,7 +720,7 @@ add_iov_dup(const void *buf, size_t len) */ ssize_t -fiber_flush_output(void) +iov_flush(void) { ssize_t result, r = 0, bytes = 0; struct iovec *iov = iovec(fiber->iov); @@ -747,7 +761,7 @@ fiber_flush_output(void) } else result = bytes; - fiber_iov_reset(); + iov_reset(); return result; } diff --git a/core/iproto.m b/core/iproto.m index 7fcb86ba18bf5aaaf464d1ff7bc945135430e6a3..1dfa406f3f06e60a8be8a54dc0582e0c80264ec5 100644 --- a/core/iproto.m +++ b/core/iproto.m @@ -65,7 +65,7 @@ iproto_interact(iproto_callback *callback) * next header. */ if (to_read > 0) { - if (fiber_flush_output() < 0) { + if (iov_flush() < 0) { say_warn("io_error: %s", strerror(errno)); break; } @@ -88,12 +88,12 @@ static void iproto_reply(iproto_callback callback, struct tbuf *request) if (unlikely(reply->msg_code == msg_ping)) { reply->len = 0; - add_iov(reply, sizeof(struct iproto_header)); + iov_add(reply, sizeof(struct iproto_header)); return; } reply->len = sizeof(uint32_t); /* ret_code */ - add_iov(reply, sizeof(struct iproto_header_retcode)); + iov_add(reply, sizeof(struct iproto_header_retcode)); size_t saved_iov_cnt = fiber->iov_cnt; /* make request point to iproto data */ request->len = iproto(request)->len; @@ -107,7 +107,7 @@ static void iproto_reply(iproto_callback callback, struct tbuf *request) fiber->iov->len -= (fiber->iov_cnt - saved_iov_cnt) * sizeof(struct iovec); fiber->iov_cnt = saved_iov_cnt; reply->ret_code = tnt_errcode_val(e->errcode); - add_iov_dup(e->errmsg, strlen(e->errmsg)+1); + iov_dup(e->errmsg, strlen(e->errmsg)+1); } for (; saved_iov_cnt < fiber->iov_cnt; saved_iov_cnt++) reply->len += iovec(fiber->iov)[saved_iov_cnt].iov_len; diff --git a/core/tarantool_lua.m b/core/tarantool_lua.m index abd47cd647bcc221711c60e181964bc08c14e90d..d345bc8be904f11b9401189c3965e9e11e12bd18 100644 --- a/core/tarantool_lua.m +++ b/core/tarantool_lua.m @@ -164,5 +164,5 @@ tarantool_lua(struct lua_State *L, else { mod_convert_iov_to_yaml(out); } - fiber_iov_reset(); + iov_reset(); } diff --git a/include/fiber.h b/include/fiber.h index 686317c65b83b7f4b421f04b59c3ed77ff91b1c3..d3a2fe780487fc9a42b3f97d99982dab95ff8e3c 100644 --- a/include/fiber.h +++ b/include/fiber.h @@ -144,7 +144,7 @@ void fiber_destroy_all(); struct msg *read_inbox(void); int fiber_bread(struct tbuf *, size_t v); -inline static void add_iov_unsafe(const void *buf, size_t len) +inline static void iov_add_unsafe(const void *buf, size_t len) { struct iovec *v; assert(fiber->iov->size - fiber->iov->len >= sizeof(*v)); @@ -160,19 +160,14 @@ inline static void iov_ensure(size_t count) tbuf_ensure(fiber->iov, sizeof(struct iovec) * count); } -inline static void add_iov(const void *buf, size_t len) -{ - iov_ensure(1); - add_iov_unsafe(buf, len); -} - -inline static void fiber_iov_reset() -{ - fiber->iov_cnt = 0; /* discard anything unwritten */ - tbuf_reset(fiber->iov); -} +/* Add to fiber's iov vector. */ +void iov_add(const void *buf, size_t len); +void iov_dup(const void *buf, size_t len); +/* Reset the fiber's iov vector. */ +ssize_t iov_flush(void); +/* Write everything in the fiber's iov vector to fiber socket. */ +void iov_reset(); -void add_iov_dup(const void *buf, size_t len); bool write_inbox(struct fiber *recipient, struct tbuf *msg); int inbox_size(struct fiber *recipient); void wait_inbox(struct fiber *recipient); diff --git a/mod/box/box.m b/mod/box/box.m index 8c9710741b2d2835a78436b39565c1e276c1440e..f752b74fb8a45e00bc87da12192032164733e18b 100644 --- a/mod/box/box.m +++ b/mod/box/box.m @@ -92,7 +92,7 @@ box_snap_row(const struct tbuf *t) return (struct box_snap_row *)t->data; } -static void tuple_add_iov(struct box_txn *txn, struct box_tuple *tuple); +static void tuple_iov_add(struct box_txn *txn, struct box_tuple *tuple); void * @@ -290,10 +290,10 @@ prepare_replace(struct box_txn *txn, size_t cardinality, struct tbuf *data) if (!(txn->flags & BOX_QUIET)) { u32 tuples_affected = 1; - add_iov_dup(&tuples_affected, sizeof(uint32_t)); + iov_dup(&tuples_affected, sizeof(uint32_t)); if (txn->flags & BOX_RETURN_TUPLE) - tuple_add_iov(txn, txn->tuple); + tuple_iov_add(txn, txn->tuple); } } @@ -538,15 +538,15 @@ prepare_update_fields(struct box_txn *txn, struct tbuf *data) out: if (!(txn->flags & BOX_QUIET)) { - add_iov_dup(&tuples_affected, sizeof(uint32_t)); + iov_dup(&tuples_affected, sizeof(uint32_t)); if (txn->flags & BOX_RETURN_TUPLE) - tuple_add_iov(txn, txn->tuple); + tuple_iov_add(txn, txn->tuple); } } static void -tuple_add_iov(struct box_txn *txn, struct box_tuple *tuple) +tuple_iov_add(struct box_txn *txn, struct box_tuple *tuple) { size_t len; @@ -556,9 +556,9 @@ tuple_add_iov(struct box_txn *txn, struct box_tuple *tuple) if (len > BOX_REF_THRESHOLD) { tuple_txn_ref(txn, tuple); - add_iov(&tuple->bsize, len); + iov_add(&tuple->bsize, len); } else { - add_iov_dup(&tuple->bsize, len); + iov_dup(&tuple->bsize, len); } } @@ -572,7 +572,7 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data) tnt_raise(IllegalParams, :"tuple count must be positive"); found = palloc(fiber->gc_pool, sizeof(*found)); - add_iov(found, sizeof(*found)); + iov_add(found, sizeof(*found)); *found = 0; if (txn->index->type == TREE) { @@ -605,7 +605,7 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data) continue; } - tuple_add_iov(txn, tuple); + tuple_iov_add(txn, tuple); if (limit == ++(*found)) break; @@ -632,7 +632,7 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data) continue; } - tuple_add_iov(txn, tuple); + tuple_iov_add(txn, tuple); (*found)++; } } @@ -663,10 +663,10 @@ prepare_delete(struct box_txn *txn, void *key) } if (!(txn->flags & BOX_QUIET)) { - add_iov_dup(&tuples_affected, sizeof(tuples_affected)); + iov_dup(&tuples_affected, sizeof(tuples_affected)); if (txn->old_tuple && (txn->flags & BOX_RETURN_TUPLE)) - tuple_add_iov(txn, txn->old_tuple); + tuple_iov_add(txn, txn->old_tuple); } } @@ -1582,7 +1582,7 @@ void mod_convert_iov_to_yaml(struct tbuf *out) /* * Sic, we can't access tuple->flags or * tuple->refs since they may point - * to nowhere, @sa tuple_add_iov(). + * to nowhere, @sa tuple_iov_add(). */ struct box_tuple *tuple = iov->iov_base - offsetof(struct box_tuple, bsize); diff --git a/mod/box/memcached-grammar.m b/mod/box/memcached-grammar.m index 43da17368b99086a7468729484d24b431f57f88c..886d2d9e24549b7cbb71a3bdd3180fccc51e3384 100644 --- a/mod/box/memcached-grammar.m +++ b/mod/box/memcached-grammar.m @@ -271,7 +271,7 @@ tr26: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple != NULL && !expired(tuple)) - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); else STORE; } @@ -311,7 +311,7 @@ tr30: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple != NULL && !expired(tuple)) - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); else STORE; } @@ -353,7 +353,7 @@ tr39: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple != NULL && !expired(tuple)) - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); else STORE; } @@ -399,7 +399,7 @@ tr58: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST) { - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); } else { value = tuple_field(tuple, 3); value_len = load_varint32(&value); @@ -457,7 +457,7 @@ tr62: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST) { - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); } else { value = tuple_field(tuple, 3); value_len = load_varint32(&value); @@ -517,7 +517,7 @@ tr71: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST) { - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); } else { value = tuple_field(tuple, 3); value_len = load_varint32(&value); @@ -573,9 +573,9 @@ tr91: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || expired(tuple)) - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); else if (meta(tuple)->cas != cas) - add_iov("EXISTS\r\n", 8); + iov_add("EXISTS\r\n", 8); else STORE; } @@ -615,9 +615,9 @@ tr95: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || expired(tuple)) - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); else if (meta(tuple)->cas != cas) - add_iov("EXISTS\r\n", 8); + iov_add("EXISTS\r\n", 8); else STORE; } @@ -659,9 +659,9 @@ tr105: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || expired(tuple)) - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); else if (meta(tuple)->cas != cas) - add_iov("EXISTS\r\n", 8); + iov_add("EXISTS\r\n", 8); else STORE; } @@ -688,7 +688,7 @@ tr118: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST || expired(tuple)) { - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); } else { m = meta(tuple); field = tuple_field(tuple, 3); @@ -718,16 +718,16 @@ tr118: @try { store(key, exptime, flags, bytes, data); stats.total_items++; - add_iov(b->data, b->len); - add_iov("\r\n", 2); + iov_add(b->data, b->len); + iov_add("\r\n", 2); } @catch (ClientError *e) { - add_iov("SERVER_ERROR ", 13); - add_iov(e->errmsg, strlen(e->errmsg)); - add_iov("\r\n", 2); + iov_add("SERVER_ERROR ", 13); + iov_add(e->errmsg, strlen(e->errmsg)); + iov_add("\r\n", 2); } } else { - add_iov("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n", 62); + iov_add("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n", 62); } } @@ -753,7 +753,7 @@ tr122: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST || expired(tuple)) { - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); } else { m = meta(tuple); field = tuple_field(tuple, 3); @@ -783,16 +783,16 @@ tr122: @try { store(key, exptime, flags, bytes, data); stats.total_items++; - add_iov(b->data, b->len); - add_iov("\r\n", 2); + iov_add(b->data, b->len); + iov_add("\r\n", 2); } @catch (ClientError *e) { - add_iov("SERVER_ERROR ", 13); - add_iov(e->errmsg, strlen(e->errmsg)); - add_iov("\r\n", 2); + iov_add("SERVER_ERROR ", 13); + iov_add(e->errmsg, strlen(e->errmsg)); + iov_add("\r\n", 2); } } else { - add_iov("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n", 62); + iov_add("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n", 62); } } @@ -820,7 +820,7 @@ tr132: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST || expired(tuple)) { - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); } else { m = meta(tuple); field = tuple_field(tuple, 3); @@ -850,16 +850,16 @@ tr132: @try { store(key, exptime, flags, bytes, data); stats.total_items++; - add_iov(b->data, b->len); - add_iov("\r\n", 2); + iov_add(b->data, b->len); + iov_add("\r\n", 2); } @catch (ClientError *e) { - add_iov("SERVER_ERROR ", 13); - add_iov(e->errmsg, strlen(e->errmsg)); - add_iov("\r\n", 2); + iov_add("SERVER_ERROR ", 13); + iov_add(e->errmsg, strlen(e->errmsg)); + iov_add("\r\n", 2); } } else { - add_iov("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n", 62); + iov_add("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n", 62); } } @@ -879,16 +879,16 @@ tr141: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST || expired(tuple)) { - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); } else { @try { delete(key); - add_iov("DELETED\r\n", 9); + iov_add("DELETED\r\n", 9); } @catch (ClientError *e) { - add_iov("SERVER_ERROR ", 13); - add_iov(e->errmsg, strlen(e->errmsg)); - add_iov("\r\n", 2); + iov_add("SERVER_ERROR ", 13); + iov_add(e->errmsg, strlen(e->errmsg)); + iov_add("\r\n", 2); } } } @@ -913,16 +913,16 @@ tr146: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST || expired(tuple)) { - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); } else { @try { delete(key); - add_iov("DELETED\r\n", 9); + iov_add("DELETED\r\n", 9); } @catch (ClientError *e) { - add_iov("SERVER_ERROR ", 13); - add_iov(e->errmsg, strlen(e->errmsg)); - add_iov("\r\n", 2); + iov_add("SERVER_ERROR ", 13); + iov_add(e->errmsg, strlen(e->errmsg)); + iov_add("\r\n", 2); } } } @@ -943,16 +943,16 @@ tr157: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || tuple->flags & GHOST || expired(tuple)) { - add_iov("NOT_FOUND\r\n", 11); + iov_add("NOT_FOUND\r\n", 11); } else { @try { delete(key); - add_iov("DELETED\r\n", 9); + iov_add("DELETED\r\n", 9); } @catch (ClientError *e) { - add_iov("SERVER_ERROR ", 13); - add_iov(e->errmsg, strlen(e->errmsg)); - add_iov("\r\n", 2); + iov_add("SERVER_ERROR ", 13); + iov_add(e->errmsg, strlen(e->errmsg)); + iov_add("\r\n", 2); } } } @@ -974,7 +974,7 @@ tr169: fiber_call(f); } else flush_all((void *)0); - add_iov("OK\r\n", 4); + iov_add("OK\r\n", 4); } goto st197; tr174: @@ -996,7 +996,7 @@ tr174: fiber_call(f); } else flush_all((void *)0); - add_iov("OK\r\n", 4); + iov_add("OK\r\n", 4); } goto st197; tr185: @@ -1018,7 +1018,7 @@ tr185: fiber_call(f); } else flush_all((void *)0); - add_iov("OK\r\n", 4); + iov_add("OK\r\n", 4); } goto st197; tr195: @@ -1094,18 +1094,18 @@ tr195: if (show_cas) { struct tbuf *b = tbuf_alloc(fiber->gc_pool); tbuf_printf(b, "VALUE %.*s %"PRIu32" %"PRIu32" %"PRIu64"\r\n", key_len, (u8 *)key, m->flags, value_len, m->cas); - add_iov_unsafe(b->data, b->len); + iov_add_unsafe(b->data, b->len); stats.bytes_written += b->len; } else { - add_iov_unsafe("VALUE ", 6); - add_iov_unsafe(key, key_len); - add_iov_unsafe(suffix, suffix_len); + iov_add_unsafe("VALUE ", 6); + iov_add_unsafe(key, key_len); + iov_add_unsafe(suffix, suffix_len); } - add_iov_unsafe(value, value_len); - add_iov_unsafe("\r\n", 2); + iov_add_unsafe(value, value_len); + iov_add_unsafe("\r\n", 2); stats.bytes_written += value_len + 2; } - add_iov_unsafe("END\r\n", 5); + iov_add_unsafe("END\r\n", 5); stats.bytes_written += 5; } goto st197; @@ -1160,7 +1160,7 @@ tr233: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || expired(tuple)) - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); else STORE; } @@ -1200,7 +1200,7 @@ tr237: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || expired(tuple)) - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); else STORE; } @@ -1242,7 +1242,7 @@ tr246: key = read_field(keys); struct box_tuple *tuple = find(key); if (tuple == NULL || expired(tuple)) - add_iov("NOT_STORED\r\n", 12); + iov_add("NOT_STORED\r\n", 12); else STORE; } @@ -3490,14 +3490,14 @@ case 196: if (pe - p > (1 << 20)) { exit: say_warn("memcached proto error"); - add_iov("ERROR\r\n", 7); + iov_add("ERROR\r\n", 7); stats.bytes_written += 7; return -1; } char *r; if ((r = memmem(p, pe - p, "\r\n", 2)) != NULL) { tbuf_peek(fiber->rbuf, r + 2 - (char *)fiber->rbuf->data); - add_iov("CLIENT_ERROR bad command line format\r\n", 38); + iov_add("CLIENT_ERROR bad command line format\r\n", 38); return 1; } return 0; diff --git a/mod/box/memcached-grammar.rl b/mod/box/memcached-grammar.rl index dabee4d8c22c85c0189cb29fcf65233aa6bf6562..1b7e455c5cf01e3c18805770b6ca7d79c16e5405 100644 --- a/mod/box/memcached-grammar.rl +++ b/mod/box/memcached-grammar.rl @@ -252,18 +252,18 @@ memcached_dispatch() if (show_cas) { struct tbuf *b = tbuf_alloc(fiber->gc_pool); tbuf_printf(b, "VALUE %.*s %"PRIu32" %"PRIu32" %"PRIu64"\r\n", key_len, (u8 *)key, m->flags, value_len, m->cas); - add_iov_unsafe(b->data, b->len); + iov_add_unsafe(b->data, b->len); stats.bytes_written += b->len; } else { - add_iov_unsafe("VALUE ", 6); - add_iov_unsafe(key, key_len); - add_iov_unsafe(suffix, suffix_len); + iov_add_unsafe("VALUE ", 6); + iov_add_unsafe(key, key_len); + iov_add_unsafe(suffix, suffix_len); } - add_iov_unsafe(value, value_len); - add_iov_unsafe("\r\n", 2); + iov_add_unsafe(value, value_len); + iov_add_unsafe("\r\n", 2); stats.bytes_written += value_len + 2; } - add_iov_unsafe("END\r\n", 5); + iov_add_unsafe("END\r\n", 5); stats.bytes_written += 5; } diff --git a/mod/box/memcached.m b/mod/box/memcached.m index cbaef78a93357c6ba7c62e10c98bb44d03b2ebf6..5d664a769f704e04061155e4e29a2b5d5061d568 100644 --- a/mod/box/memcached.m +++ b/mod/box/memcached.m @@ -188,7 +188,7 @@ print_stats() tbuf_printf(out, "STAT limit_maxbytes %"PRIu64"\r\n", (u64)(cfg.slab_alloc_arena * (1 << 30))); tbuf_printf(out, "STAT threads 1\r\n"); tbuf_printf(out, "END\r\n"); - add_iov(out->data, out->len); + iov_add(out->data, out->len); } static void @@ -209,17 +209,17 @@ flush_all(void *data) do { \ stats.cmd_set++; \ if (bytes > (1<<20)) { \ - add_iov("SERVER_ERROR object too large for cache\r\n", 41); \ + iov_add("SERVER_ERROR object too large for cache\r\n", 41); \ } else { \ @try { \ store(key, exptime, flags, bytes, data); \ stats.total_items++; \ - add_iov("STORED\r\n", 8); \ + iov_add("STORED\r\n", 8); \ } \ @catch (ClientError *e) { \ - add_iov("SERVER_ERROR ", 13); \ - add_iov(e->errmsg, strlen(e->errmsg)); \ - add_iov("\r\n", 2); \ + iov_add("SERVER_ERROR ", 13); \ + iov_add(e->errmsg, strlen(e->errmsg)); \ + iov_add("\r\n", 2); \ } \ } \ } while (0) @@ -258,7 +258,7 @@ memcached_handler(void *_data __attribute__((unused))) goto dispatch; } - r = fiber_flush_output(); + r = iov_flush(); if (r < 0) { say_debug("flush_output failed, closing connection"); goto exit; @@ -273,7 +273,7 @@ memcached_handler(void *_data __attribute__((unused))) } } exit: - fiber_flush_output(); + iov_flush(); fiber_sleep(0.01); say_debug("exit"); stats.curr_connections--; /* FIXME: nonlocal exit via exception will leak this counter */