Пример #1
0
static void ascii_write_to_client(void *arg) {
    struct connection *c = arg;
    struct iovec *vecs = c->vecs;
    if (c->key_prealloc) {
        vecs[c->iov_used].iov_base = c->keys[*c->cur_key].key;
        vecs[c->iov_used].iov_len  = c->keys[*c->cur_key].key_len;
    } else {
        vecs[c->iov_used].iov_base = c->wbuf_pos;
        vecs[c->iov_used].iov_len = c->ascii_format(c);
        c->wbuf_pos += vecs[c->iov_used].iov_len;
    }
    c->iov_used++;
    if (c->value_size) {
        if (c->use_shared_value) {
            vecs[c->iov_used].iov_base = c->t->shared_value;
        } else {
            vecs[c->iov_used].iov_base = c->value;
        }
        vecs[c->iov_used].iov_len = c->value_size;
        c->iov_used++;
        vecs[c->iov_used].iov_base = "\r\n";
        vecs[c->iov_used].iov_len = 2;
        c->iov_used++;
    }
    run_counter(c);
}
Пример #2
0
/* Unhappy with this, but it's still shorter/better than the old code.
 * Binprot is just unwieldy in C, or I haven't figured out how to use it
 * simply yet.
 */
static void bin_write_to_client(void *arg) {
    struct connection *c = arg;
    struct iovec *vecs = c->vecs;
    protocol_binary_request_header *pkt = (protocol_binary_request_header *)c->wbuf_pos;
    memset(pkt, 0, sizeof(protocol_binary_request_header));
    pkt->request.magic = PROTOCOL_BINARY_REQ;
    c->bin_prep_cmd(c);
    if (c->key_prealloc) {
        vecs[c->iov_used].iov_base = c->keys[*c->cur_key].key;
        vecs[c->iov_used].iov_len  = c->keys[*c->cur_key].key_len;
    } else {
        vecs[c->iov_used].iov_base = c->wbuf_pos;
        vecs[c->iov_used].iov_len = c->bin_format(c);
        c->wbuf_pos += vecs[c->iov_used].iov_len;
    }
    int bodylen = vecs[c->iov_used].iov_len + pkt->request.extlen;
    pkt->request.keylen = htons(vecs[c->iov_used].iov_len);
    c->iov_used++;
    if (c->value_size) {
        bodylen += c->value_size;
        if (c->use_shared_value) {
            vecs[c->iov_used].iov_base = c->t->shared_value;
        } else {
            vecs[c->iov_used].iov_base = c->value;
        }
        vecs[c->iov_used].iov_len = c->value_size;
        c->iov_used++;
    }
    pkt->request.bodylen = htonl(bodylen);
    run_counter(c);
}
Пример #3
0
static void write_bin_getq_to_client(void *arg) {
    struct connection *c = arg;
    int wbytes = 0;
    uint32_t keylen = 0;
    int towrite = 0;
    size_t psize = sizeof(c->bin_get_pkt);

    /* existing buffer we need to finish flushing */
    if (c->buf_towrite) {
        wbytes = send(c->fd, c->wbuf + c->buf_written, c->buf_towrite, 0);
        if (wbytes == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                c->state = conn_reading;
                return;
            } else {
                perror("Early write error to client");
                return;
            }
        } else if (wbytes < c->buf_towrite) {
            c->state = conn_reading;
            c->buf_towrite -= wbytes;
            c->buf_written += wbytes;
            return;
        }
        c->buf_towrite = 0;
        c->buf_written = 0;
    }

    for(;;) {
        towrite = 0;
        while (towrite < (4096 - (psize + 250))) {
            keylen = sprintf(c->wbuf + towrite + psize, "%s%llu",
                c->key_prefix, (unsigned long long)*c->cur_key);
            c->bin_get_pkt.message.header.request.keylen = htons(keylen);
            c->bin_get_pkt.message.header.request.bodylen = htonl(keylen);
            memcpy(c->wbuf + towrite, (char *)&c->bin_get_pkt.bytes, psize);
            towrite += keylen + psize;
            run_counter(c);
        }
        wbytes = send(c->fd, c->wbuf, towrite, 0);
        if (wbytes == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                c->state = conn_reading;
                c->buf_towrite = towrite;
                return;
            } else {
                perror("Late write error to client");
                return;
            }
        } else if (wbytes < towrite) {
            c->state = conn_reading;
            c->buf_towrite = towrite - wbytes;
            c->buf_written = wbytes;
            return;
        }
    }

    return;
}
Пример #4
0
static void write_ascii_get_to_client(void *arg) {
    struct connection *c = arg;
    int wbytes = 0;
    sprintf(c->wbuf, "get %s%llu\r\n", c->key_prefix,
        (unsigned long long)*c->cur_key);
    wbytes = send(c->fd, &c->wbuf, strlen(c->wbuf), 0);
    c->state = conn_reading;
    run_counter(c);
}
Пример #5
0
void main(){
	WDTCTL=WDTPW+WDTHOLD; // disable WDT for now
	index=0; // initial index for storage of data
	_bis_SR_register(GIE); // enable global interrupts
	while(1){
		count[index++]=run_counter();
		if (index>=N_samples){
			index=0;
		}
	}
}
Пример #6
0
static void prealloc_write_ascii_decr_to_client(void *arg) {
    struct connection *c = arg;
    struct iovec *vecs = c->vecs;
    vecs[0].iov_base = "decr ";
    vecs[0].iov_len = 5;
    vecs[1].iov_base = c->keys[*c->cur_key].key;
    vecs[1].iov_len  = c->keys[*c->cur_key].key_len;
    vecs[2].iov_base = " 1\r\n";
    vecs[2].iov_len = 4;

    c->iov_towrite = sum_iovecs(vecs, c->iov_count);
    write_iovecs(c, conn_reading);
    run_counter(c);
}
Пример #7
0
static void write_bin_get_to_client(void *arg) {
    struct connection *c = arg;
    int wbytes = 0;
    uint32_t keylen = 0;

    keylen = sprintf(c->wbuf + sizeof(c->bin_get_pkt), "%s%llu", c->key_prefix,
        (unsigned long long)*c->cur_key);
    c->bin_get_pkt.message.header.request.keylen = htons(keylen);
    c->bin_get_pkt.message.header.request.bodylen = htonl(keylen);
    memcpy(c->wbuf, (char *)&c->bin_get_pkt.bytes, sizeof(c->bin_get_pkt));
    wbytes = send(c->fd, c->wbuf, sizeof(c->bin_get_pkt) + keylen, 0);
    c->state = conn_reading;
    run_counter(c);
}
Пример #8
0
static void prealloc_write_ascii_mget_to_client(void *arg) {
    struct connection *c = arg;
    int i;
    struct iovec *vecs = c->vecs;
    vecs[0].iov_base = "get ";
    vecs[0].iov_len  = 4;
    for (i = 1; i < c->mget_count + 1; i++) {
        vecs[i].iov_base = c->keys[*c->cur_key].key;
        vecs[i].iov_len  = c->keys[*c->cur_key].key_len;
        run_counter(c);
    }
    vecs[i].iov_base = "\r\n";
    vecs[i].iov_len  = 2;
    c->iov_towrite = sum_iovecs(vecs, c->iov_count);
    write_iovecs(c, conn_reading);
}
Пример #9
0
static void write_ascii_mget_to_client(void *arg) {
    struct connection *c = arg;
    int wbytes = 0;
    int written = 0;
    int i;
    strcpy(c->wbuf, "get ");
    written += 4;
    for (i = 0; i < c->mget_count; i++) {
        written += sprintf(c->wbuf + written, "%s%llu ",
            c->key_prefix, (unsigned long long)*c->cur_key);
        run_counter(c);
    }
    strcpy(c->wbuf + (written), "\r\n");
    wbytes = send(c->fd, &c->wbuf, written + 2, 0);
    c->state = conn_reading;
}
Пример #10
0
static void write_ascii_set_to_client(void *arg) {
    struct connection *c = arg;
    struct iovec *vecs = c->vecs;
    vecs[0].iov_base = c->wbuf;
    vecs[0].iov_len  = sprintf(c->wbuf, "set %s%llu 0 0 %d\r\n", c->key_prefix,
        (unsigned long long)*c->cur_key, c->value_size);
    if (c->value[0] == '\0') {
        vecs[1].iov_base = shared_value;
    } else {
        vecs[1].iov_base = c->value;
    }
    vecs[1].iov_len  = c->value_size;
    vecs[2].iov_base = "\r\n";
    vecs[2].iov_len  = 2;
    c->iov_towrite = sum_iovecs(vecs, c->iov_count);
    write_iovecs(c, conn_reading);

    run_counter(c);
}
Пример #11
0
/* Multigets have a weird/specific format. */
static void ascii_write_mget_to_client(void *arg) {
    struct connection *c = arg;
    int i;
    struct iovec *vecs = c->vecs;
    vecs[0].iov_base = "get ";
    vecs[0].iov_len  = 4;
    for (i = 1; i < c->mget_count + 1; i++) {
        if (c->key_prealloc) {
            vecs[i].iov_base = c->keys[*c->cur_key].key;
            vecs[i].iov_len  = c->keys[*c->cur_key].key_len;
        } else {
            vecs[i].iov_base = c->wbuf_pos;
            vecs[i].iov_len  = ascii_mget_format(c);
        }
        run_counter(c);
    }
    vecs[i].iov_base = "\r\n";
    vecs[i].iov_len  = 2;
}