void cos_init(void) { cbuf_t cbs[MAX_CBUFS]; int szs[MAX_CBUFS]; char *bufs[MAX_CBUFS]; int i; printc("UNIT TEST Unit tests for cbufs...\n"); for (i = 0 ; i < MAX_CBUFS ; i++) { int sz = (rand() % MAX_CBUF_SZ) + 1; bufs[i] = cbuf_alloc(sz, &cbs[i]); printv("UNIT TEST alloc %d -> %p\n", sz, bufs[i]); assert(bufs[i]); cbuf_free(bufs[i]); printv("UNIT TEST free %p\n", bufs[i]); } printc("UNIT TEST PASSED: alloc->dealloc\n"); for (i = 0 ; i < MAX_CBUFS ; i++) { int sz = (rand() % MAX_CBUF_SZ) + 1; bufs[i] = cbuf_alloc(sz, &cbs[i]); printv("UNIT TEST alloc %d -> %p\n", sz, bufs[i]); assert(bufs[i]); } for (i = 0 ; i < MAX_CBUFS ; i++) { cbuf_free(bufs[i]); printv("UNIT TEST free %p\n", bufs[i]); } printc("UNIT TEST PASSED: N alloc -> N dealloc\n"); for (i = 0 ; i < MAX_CBUFS ; i++) { int sz = (rand() % MAX_CBUF_SZ) + 1; bufs[i] = cbuf_alloc(sz, &cbs[i]); printv("UNIT TEST alloc %d -> %p\n", sz, bufs[i]); szs[i] = sz; assert(bufs[i]); bufs[i][0] = '_'; unit_cbuf(cbs[i], sz); assert(bufs[i][0] == '*'); printv("UNIT TEST cbuf2buf %d\n", sz); } printc("UNIT TEST PASSED: N alloc + cbuf2buf\n"); for (i = 0 ; i < MAX_CBUFS ; i++) { bufs[i][0] = '_'; unit_cbuf(cbs[i], szs[i]); printv("UNIT TEST cbuf2buf %d\n", szs[i]); } printc("UNIT TEST PASSED: N cached cbuf2buf\n"); for (i = 0 ; i < MAX_CBUFS ; i++) { cbuf_free(bufs[i]); printv("UNIT TEST free %p\n", bufs[i]); } printc("UNIT TEST PASSED: N deallocs\n"); printc("UNIT TEST ALL PASSED\n"); return; }
int main(int argc, char** argv) { cbuf *cb1 = cbuf_alloc(); // make sure the cbuf grows and shrinks appropriately int capacity = cbuf_capacity(cb1); check(capacity > 0, "Initial capacity > 0"); check(cbuf_size(cb1) == 0, "Initial size == 0"); for(int i = 0; i < capacity; i++) { cbuf_update(cb1, 60, 1.291); } check(cbuf_size(cb1) == cbuf_capacity(cb1), "Size is allowed to grow to capacity"); cbuf_update(cb1, 60, 1.291); // inserting one more than what the original structure could contain int new_capacity = cbuf_capacity(cb1); check(new_capacity > capacity, "Capacity grows when necessary"); // inserting an update that's > 5 minutes older than all of the data // in the structure cbuf_update(cb1, 60 + 6 * 60, 1.291); check(cbuf_capacity(cb1) < new_capacity, "Capacity shrinks when able"); check(cbuf_size(cb1) == 1, "Aged out records are removed correctly"); cbuf_free(cb1); return 0; }
void cbuf_call(char c) { printc("\n****** MID: thread %d in spd %ld ******\n",cos_get_thd_id(), cos_spd_id()); /* cbuf_t cb = cbuf_null(); */ u32_t id, idx; /* u64_t start, end; */ int i; cbuf_t cbt[iter]; void *mt[iter]; for (i = 0; i < iter ; i++){ cbt[i] = cbuf_null(); mt[i] = cbuf_alloc(SZ, &cbt[i]); cbuf_unpack(cbt[i], &id, &idx); printc("thread %d Now @ %p, memid %x, idx %x\n", cos_get_thd_id(), mt[i], id, idx); assert(mt[i]); memset(mt[i], c, SZ); } delay(DELAY); delay(DELAY); for (i = 0; i < iter ; i++){ f(cbt[i],SZ); } /* delay(DELAY); */ /* delay(DELAY); */ printc("\n****** MID free: thread %d in spd %ld ******\n",cos_get_thd_id(), cos_spd_id()); for (i = 0; i < iter ; i++){ cbuf_free(mt[i]); } delay(DELAY); /* check_val(); */ /* check_val1(); */ /* m = cbuf_alloc(SZ, &cb); */ /* cbuf_unpack(cb, &id, &idx); */ /* printc("....Now @ %p, memid %x, idx %x\n", m, id, idx); */ /* /\* printc("cb is %d\n",cb); *\/ */ /* memset(m, c, SZ); */ /* rdtscll(start); */ /* for (i = 0 ; i < ITER ; i++) { */ /* f(cb, SZ); */ /* } */ /* rdtscll(end); */ /* printc("AVG: %lld\n", (end-start)/ITER); */ /* printc("initial %c, after %c\n", c, ((char *)m)[0]); */ /* cbuf_free(m); */ return; }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { td_t ntd; struct torrent *t; char *buf, *nbuf; int ret = -1; cbuf_t ncbid; if (tor_isnull(td)) return -EINVAL; t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done); assert(t->data); ntd = (td_t)t->data; buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); nbuf = cbuf_alloc(sz, &ncbid); assert(nbuf); /* printc("tip_tif_tread (thd %d)\n", cos_get_thd_id()); */ ret = server_tread(cos_spd_id(), ntd, ncbid, sz); if (ret < 0) goto free; /* ip_tread_cnt++; */ memcpy(buf, nbuf, ret); free: /* cbufp_deref(ncbid); */ // should keep this cbufp alive in netif for FT purpose? Jiguo cbuf_free(ncbid); done: return ret; }
int twrite(spdid_t spdid, td_t td, int cbid, int sz) { td_t ntd; struct torrent *t; char *buf, *nbuf; int ret = -1; cbuf_t ncbid; if (tor_isnull(td)) return -EINVAL; t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done); assert(t->data); ntd = (td_t)t->data; buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); nbuf = cbuf_alloc(sz, &ncbid); assert(nbuf); memcpy(nbuf, buf, sz); ret = parent_twrite(cos_spd_id(), ntd, ncbid, sz); cbuf_free(ncbid); done: return ret; }
static inline int __cbufp_alloc_slow(int cbid, int size, int *len, int *error) { int amnt = 0, i; cbuf_t cb; int *cbs; assert(cbid <= 0); if (cbid == 0) { struct cbuf_meta *cm; cbs = cbuf_alloc(PAGE_SIZE, &cb); assert(cbs); cbs[0] = 0; /* Do a garbage collection */ amnt = cbufp_collect(cos_spd_id(), size, cb); if (amnt < 0) { *error = 1; return -1; } CBUF_TAKE(); cbid = cbs[0]; /* own the cbuf we just collected */ if (amnt > 0) { cm = cbuf_vect_lookup_addr(cbid_to_meta_idx(cbid), 0); assert(cm); /* (should be atomic) */ cm->nfo.c.flags |= CBUFM_IN_USE | CBUFM_TOUCHED; } /* ...add the rest back into freelists */ for (i = 1 ; i < amnt ; i++) { struct cbuf_alloc_desc *d, *fl; struct cbuf_meta *meta; int idx = cbid_to_meta_idx(cbs[i]); u32_t page; void *data; assert(idx > 0); meta = cbuf_vect_lookup_addr(idx, 0); d = __cbuf_alloc_lookup(meta->nfo.c.ptr); assert(d && d->cbid == cbs[i]); fl = d->flhead; assert(fl); ADD_LIST(fl, d, next, prev); } CBUF_RELEASE(); cbuf_free(cbs); } /* Nothing collected...allocate a new cbufp! */ if (amnt == 0) { cbid = cbufp_create(cos_spd_id(), size, cbid*-1); if (cbid == 0) assert(0); } /* TODO update correctly */ *len = 1; return cbid; }
CSTUB_FN(int, tmerge)(struct usr_inv_cap *uc, spdid_t spdid, td_t td, td_t td_into, char *param, int len) { int ret; long fault = 0; struct __sg_tmerge_data *d; cbuf_t cb; int sz = len + sizeof(struct __sg_tmerge_data); assert(param && len > 0); assert(param[len-1] == '\0'); d = cbuf_alloc(sz, &cb); if (!d) return -1; d->td = td; d->td_into = td_into; d->len[0] = 0; d->len[1] = len; memcpy(&d->data[0], param, len); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); cbuf_free(cb); return ret; }
CSTUB_FN(int, twmeta)(struct usr_inv_cap *uc, spdid_t spdid, td_t td, const char *key, unsigned int klen, const char *val, unsigned int vlen) { int ret; long fault = 0; cbuf_t cb; int sz = sizeof(struct __sg_twmeta_data) + klen + vlen + 1; struct __sg_twmeta_data *d; assert(key && val && klen > 0 && vlen > 0); assert(key[klen] == '\0' && val[vlen] == '\0' && sz <= PAGE_SIZE); d = cbuf_alloc(sz, &cb); if (!d) assert(0); //return -1; d->td = td; d->klen = klen; d->vlen = vlen; memcpy(&d->data[0], key, klen + 1); memcpy(&d->data[klen + 1], val, vlen + 1); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); cbuf_free(cb); return ret; }
CSTUB_FN(td_t, tsplit)(struct usr_inv_cap *uc, spdid_t spdid, td_t tid, char * param, int len, tor_flags_t tflags, long evtid) { long fault = 0; td_t ret; struct __sg_tsplit_data *d; cbuf_t cb; int sz = len + sizeof(struct __sg_tsplit_data); assert(param && len >= 0); assert(param[len] == '\0'); d = cbuf_alloc(sz, &cb); if (!d) return -6; d->tid = tid; d->tflags = tflags; d->evtid = evtid; d->len[0] = 0; d->len[1] = len; memcpy(&d->data[0], param, len + 1); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); cbuf_free(cb); return ret; }
CSTUB_FN(int, trmeta)(struct usr_inv_cap *uc, spdid_t spdid, td_t td, const char *key, unsigned int klen, char *retval, unsigned int max_rval_len) { int ret; long fault = 0; cbuf_t cb; int sz = sizeof(struct __sg_trmeta_data) + klen + max_rval_len + 1; struct __sg_trmeta_data *d; assert(key && retval && klen > 0 && max_rval_len > 0); assert(key[klen] == '\0' && sz <= PAGE_SIZE); d = cbuf_alloc(sz, &cb); if (!d) return -1; d->td = td; d->klen = klen; d->retval_len = max_rval_len; memcpy(&d->data[0], key, klen + 1); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); if (ret >= 0) { if ((unsigned int)ret > max_rval_len) { // as ret >= 0, cast it to unsigned int to omit compiler warning cbuf_free(cb); return -EIO; } memcpy(retval, &d->data[klen + 1], ret + 1); } cbuf_free(cb); return ret; }
static int cos_net_evt_loop(void) { int alloc_sz = MTU; char *data; cbuf_t cb; assert(event_thd > 0); ip_td = parent_tsplit(cos_spd_id(), td_root, "", 0, TOR_ALL, -1); assert(ip_td > 0); printc("network uc %d starting...\n", cos_get_thd_id()); alloc_sz = sizeof(struct cos_array) + MTU; while (1) { int sz; data = cbuf_alloc(alloc_sz, &cb); assert(data); /* printc("tnet_tip_tread (thd %d)\n", cos_get_thd_id()); */ sz = parent_tread(cos_spd_id(), ip_td, cb, alloc_sz); tcp_tread_cnt++; assert(sz > 0); cos_net_interrupt(data, sz); assert(lock_contested(&net_lock) != cos_get_thd_id()); cbuf_free(data); } return 0; }
cbuf_t call_cbuf_alloc(unsigned long sz) { cbuf_t cbuf; char *addr; addr = cbuf_alloc(sz, &cbuf); assert(addr && cbuf); addr[0] = '$'; cbuf_send(cbuf); cbuf_send_free(cbuf); return cbuf; }
cbuf_t unit_cbufp_alloc(int sz) { cbuf_t cbuf; char *addr; addr = cbuf_alloc(sz, &cbuf); assert(addr); assert(cbuf); addr[0] = '_'; cbuf_send(cbuf); return cbuf; }
/* * Default allocation and deallocation functions: assume header is * internal to the slab's memory */ struct ps_slab * ps_slab_defalloc(struct ps_mem *m, size_t sz, coreid_t coreid) { int id; struct ps_slab *s = (struct ps_slab *)cbuf_alloc(sz, &id); (void)coreid; (void)m; if (!s) return NULL; memset(s, 0, sz); s->memory = s; s->cbid = id; return s; }
void unit_cbuf(cbuf_t cbuf, int sz) { char *c = cbuf2buf(cbuf, sz); cbuf_t cb; char *addr; assert(c); assert(c[0] == '_'); c[0] = '*'; addr = cbuf_alloc(sz, &cb); cbuf_free(addr); }
CSTUB_FN(int, tmerge)(struct usr_inv_cap *uc, spdid_t spdid, td_t td, td_t td_into, char * param, int len) { long fault = 0; td_t ret; struct __sg_tmerge_data *d; struct rec_data_tor *rd; cbuf_t cb; int sz = len + sizeof(struct __sg_tmerge_data); /* printc("<<< rtorrent In: call tmerge (thread %d) >>>\n", cos_get_thd_id()); */ assert(param && len > 0); assert(param[len] == '\0'); redo: /* printc("<<< In: call tmerge (thread %d) >>>\n", cos_get_thd_id()); */ rd = rd_update(td, STATE_TMERGE); assert(rd); d = cbuf_alloc(sz, &cb); if (!d) return -1; /* printc("c: tmerge td %d (server td %d) len %d param %s\n", td, rd->s_tid, len, param); */ d->td = rd->s_tid; /* actual server side torrent id */ d->td_into = td_into; d->len[0] = 0; d->len[1] = len; memcpy(&d->data[0], param, len); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); if (unlikely(fault)) { CSTUB_FAULT_UPDATE(); cbuf_free(cb); goto redo; } cbuf_free(cb); if (!ret) map_rd_delete(rd->c_tid); return ret; }
CSTUB_FN(int, trmeta)(struct usr_inv_cap *uc, spdid_t spdid, td_t td, const char *key, unsigned int klen, char *retval, unsigned int max_rval_len) { int ret; long fault = 0; cbuf_t cb; int sz = sizeof(struct __sg_trmeta_data) + klen + max_rval_len + 1; struct __sg_trmeta_data *d; struct rec_data_tor *rd; assert(key && retval && klen > 0 && max_rval_len > 0); assert(key[klen] == '\0' && sz <= PAGE_SIZE); redo: printc("<<< In: call trmeta (thread %d) >>>\n", cos_get_thd_id()); rd = rd_update(td, STATE_TRMETA); assert(rd); d = cbuf_alloc(sz, &cb); if (!d) return -1; d->td = rd->s_tid; d->klen = klen; d->retval_len = max_rval_len; memcpy(&d->data[0], key, klen + 1); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); if (unlikely(fault)) { CSTUB_FAULT_UPDATE(); goto redo; } if (ret >= 0) { if ((unsigned int)ret > max_rval_len) { // as ret >= 0, cast it to unsigned int to omit compiler warning cbuf_free(cb); return -EIO; } memcpy(retval, &d->data[klen + 1], ret + 1); } cbuf_free(cb); return ret; }
static err_t cos_net_stack_send(struct netif *ni, struct pbuf *p, struct ip_addr *ip) { int tot_len = 0, sz; char *buff; cbuf_t cb; /* assuming the net lock is taken here */ assert(p && p->ref == 1); assert(p->type == PBUF_RAM); buff = cbuf_alloc(MTU, &cb); assert(buff); while (p) { if (p->len + tot_len > MTU) BUG(); memcpy(buff + tot_len, p->payload, p->len); tot_len += p->len; #ifdef TCP_SEND_COPY #ifdef TEST_TIMING if ((p->type == PBUF_REF || p->type == PBUF_ROM)) { struct packet_queue *pq; pq = net_packet_pq(p->payload); timing_record(SEND, pq->ts_start); } #endif #endif assert(p->type != PBUF_POOL); assert(p->ref == 1); p = p->next; } sz = parent_twrite(cos_spd_id(), ip_td, cb, tot_len); if (sz <= 0) { printc("<<transmit returns %d -> %d>>\n", sz, tot_len); } tcp_twrite_cnt++; assert(sz > 0); cbuf_free(buff); /* cannot deallocate packets here as we might need to * retransmit them. */ return ERR_OK; }
static void from_data_new(struct tor_conn *tc) { int from, to, amnt; char *buf; from = tc->from; to = tc->to; while (1) { int ret; cbuf_t cb; buf = cbuf_alloc(BUFF_SZ, &cb); assert(buf); amnt = from_tread(cos_spd_id(), from, cb, BUFF_SZ-1); if (0 == amnt) break; else if (-EPIPE == amnt) { goto close; } else if (amnt < 0) { printc("read from fd %d produced %d.\n", from, amnt); BUG(); } assert(amnt <= BUFF_SZ); if (amnt != (ret = twrite(cos_spd_id(), to, cb, amnt))) { printc("conn_mgr: write failed w/ %d on fd %d\n", ret, to); goto close; } cbuf_free(buf); } done: cbuf_free(buf); return; close: mapping_remove(from, to, tc->feid, tc->teid); from_trelease(cos_spd_id(), from); trelease(cos_spd_id(), to); assert(tc->feid && tc->teid); evt_put(tc->feid); evt_put(tc->teid); goto done; }
int __attribute__((format(printf,1,2))) printc(char *fmt, ...) { static td_t tor = 0; char *s; va_list arg_ptr; int ret; cbuf_t cb = cbuf_null(); if (!tor) tor = printt_init(); s = cbuf_alloc(4096, &cb); assert(s); va_start(arg_ptr, fmt); ret = vsnprintf(s, 4096, fmt, arg_ptr); va_end(arg_ptr); print_twrite(cos_spd_id(), tor, cb, ret); cbuf_free(s); return ret; }
/* pid ==> simulator */ static void to_data_new(ap_data *out_data) { int amnt, ret; char *buf; cbuf_t cb; /* printc("to_data_new\n"); */ if (!(buf = cbuf_alloc(BUFF_SZ, &cb))) BUG(); static int pid2out = 20; /* prepare the information to be sent simulator here */ // TODO: /* char tmpstr[1024]; */ /* char *test_str = tmpstr; */ /* sprintf(test_str, "%d", pid2out++); */ char *test_str = "fake msg from PID controller\n"; memcpy(buf, test_str, strlen(test_str)+1); amnt = strlen(test_str); if (amnt != (ret = from_twrite(cos_spd_id(), pid_torrent, cb, amnt))) { printc("write failed w/ %d of %d\n", ret, amnt); goto close; } printc("pid ==> simulator:: %s\n", buf); memset(buf, 0, strlen(test_str)+1); done: cbuf_free(buf); return; close: from_trelease(cos_spd_id(), pid_torrent); goto done; }
/* simulator ==> pid */ static int from_data_new(ap_data *in_data) { char *buf; cbuf_t cb; int amnt; int ret = 0; /* printc("from_data_new\n"); */ buf = cbuf_alloc(BUFF_SZ, &cb); assert(buf); amnt = from_tread(cos_spd_id(), pid_torrent, cb, BUFF_SZ-1); if (0 == amnt) { /* printc("0 amnt\n"); */ goto done; } else if (-EPIPE == amnt) { printc("EPIPE close connection\n"); goto close; } else if (amnt < 0) { /* printc("read from pid_torrent %d produced %d.\n", pid_torrent, amnt); */ goto done; } /* copy the external information here*/ // TODO printc("simulator ==> pid:: %s\n", buf); if (buf) ret = 1; done: cbuf_free(buf); return ret; close: from_trelease(cos_spd_id(), pid_torrent); goto done; }
CSTUB_FN(int, twmeta)(struct usr_inv_cap *uc, spdid_t spdid, td_t td, const char *key, unsigned int klen, const char *val, unsigned int vlen) { int ret; long fault = 0; cbuf_t cb; int sz = sizeof(struct __sg_twmeta_data) + klen + vlen + 1; struct __sg_twmeta_data *d; struct rec_data_tor *rd; assert(key && val && klen > 0 && vlen > 0); assert(key[klen] == '\0' && val[vlen] == '\0' && sz <= PAGE_SIZE); redo: printc("<<< In: call twmeta (thread %d) >>>\n", cos_get_thd_id()); rd = rd_update(td, STATE_TWMETA); assert(rd); d = cbuf_alloc(sz, &cb); if (!d) assert(0); //return -1; d->td = td; // do not pass rd->s_tid since this is only for recovery d->klen = klen; d->vlen = vlen; memcpy(&d->data[0], key, klen + 1); memcpy(&d->data[klen + 1], val, vlen + 1); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); if (unlikely(fault)) { CSTUB_FAULT_UPDATE(); goto redo; } cbuf_free(cb); return ret; }
static unsigned long do_action(unsigned long exe_time_left, const unsigned long initial_exe_t, cbuf_t cbt_map, int len_map) { unsigned long i, j, val; unsigned long long t; int mark = 0; int len = SZ; static int first = 1; unsigned long has_run; /* thread has run cycles in this inv */ u32_t id, idx; cbuf_t cbt[NCBUF]; memset(cbt, 0 , NCBUF*sizeof(cbuf_t)); void *mt[NCBUF] = {}; int get[NCBUF]; memset(get, 0 , NCBUF*sizeof(cbuf_t)); parse_initstr(); /* DOUTs("thd %d enter comp %ld!\n", cos_get_thd_id(), cos_spd_id()); */ if (first) { unsigned long temp = 0; temp = measure_loop_costs(spin); first = 0; /*if (exe_time_left < temp) return 0; exe_time_left -= temp;*/ } if (AVG_INVC_CYCS > exe_time_left) return 0; exe_time_left -= AVG_INVC_CYCS; #ifdef CBUF2BUF u64_t start,end; char *b; if(cbt_map && len_map){ rdtscll(start); b = cbuf2buf(cbt_map,len_map); rdtscll(end); DOUTs("---- cost Bf2Bf :: %llu in spd %ld\n", end-start, cos_spd_id()); if (!b) { DOUTs("Can not map into this spd %ld\n", cos_spd_id()); return cbuf_null(); } memset(b, 's', len_map); /* DOUTs("after buf2buf write sth...\n"); */ } #endif for (j = 0 ; j < num_invs ; j++) { if (exe_time_left == 0) return 0; kkk = 0; unsigned long ss = initial_exe_t / (100 / PERCENT_EXE) / 15 * 2; for (i=0; i<ss; i++) kkk++; has_run = ss * 15 / 2;//loop_cost;// if (has_run > exe_time_left) { return 0; } exe_time_left -= has_run; rdtscll(t); val = (int)(t & (TOTAL_AMNT-1)); if (ss_attached && (val < prop_call_ss)) { //exe_time_left = ss_action(exe_time_left, initial_exe_t); SYNTH_TAKE(); for (i = 0; i < NCBUF ; i++){ rdtscll(t); cbt[i] = cbuf_null(); mt[i] = cbuf_alloc(len, &cbt[i]); } SYNTH_RELEASE(); printc("I am suspended :(\n"); timed_event_block(cos_spd_id(), 2); printc("I am back :)\n"); for (i = 0; i < NCBUF ; i++){ cbuf_free(mt[i]); } } if (exe_time_left == 0) return 0; #ifdef ALLOC_CBUF SYNTH_TAKE(); for (i = 0; i < NCBUF ; i++){ rdtscll(t); val = (int)(t & (TOTAL_AMNT-1)); if (val >= cbuf_l_to_r) { cbt[i] = cbuf_null(); rdtscll(start); mt[i] = cbuf_alloc(len, &cbt[i]); rdtscll(end); cbuf_unpack(cbt[i], &id, &idx); memset(mt[i], 'a', len); get[i] = 1; mark = 1; } } SYNTH_RELEASE(); #endif rdtscll(t); val = (int)(t & (TOTAL_AMNT-1)); if(mark == 1){ if (val >= l_to_r) { exe_time_left = calll_left(exe_time_left, initial_exe_t , cbt[0], len); } else { exe_time_left = callr_right(exe_time_left, initial_exe_t, cbt[0], len); } } else{ if (val >= l_to_r) { exe_time_left = calll_left(exe_time_left, initial_exe_t , 0, 0); } else { exe_time_left = callr_right(exe_time_left, initial_exe_t, 0, 0); } } #ifdef ALLOC_CBUF for (i = 0; i < NCBUF ; i++){ if (get[i] == 1){ get[i] = 0; rdtscll(start); cbuf_free(mt[i]); rdtscll(end); } } #endif } return exe_time_left; }
void cos_init(void) { u64_t start, end, start_tmp, end_tmp; int i, k, prev_sz = 1; cbuf_t cbt[NCBUF]; memset(cbt, 0 , NCBUF*sizeof(cbuf_t)); void *mt[NCBUF]; unsigned int sz[NCBUF]; for (i = 0; i < NCBUF ; i++){ cbt[i] = cbuf_null(); sz[i] = 0; } printc("\nMICRO BENCHMARK TEST (PINGPONG WITH CBUF)\n"); /* RDTSCLL */ printc("\n<<< RDTSCLL MICRO-BENCHMARK TEST >>>\n"); rdtscll(start_tmp); for (i = 0 ; i < ITER ; i++) { rdtscll(start); } rdtscll(end_tmp); printc("%d rdtscll avg %lld cycs\n", ITER, (end_tmp-start_tmp)/ITER); /* PINGPONG */ printc("\n<<< PINGPONG MICRO-BENCHMARK TEST >>>\n"); call(); for (k = 0; k <10 ;k++){ rdtscll(start); for (i = 0 ; i < ITER ; i++) { call(); } rdtscll(end); printc("%d invs avg %lld cycs\n", ITER, (end-start)/ITER); } printc("<<< PINGPONG BENCHMARK TEST DONE >>>\n"); /* CACHING */ printc("\n<<< WARM UP CBUF CACHE......."); for (i = 0; i < NCBUF ; i++){ prev_sz += 4; prev_sz &= PAGE_SIZE-1; sz[i] = prev_sz; mt[i] = cbuf_alloc(sz[i], &cbt[i]); } for (i = 0; i < NCBUF ; i++){ simple_call_buf2buf(cbt[i], sz[i]); } for (i = 0; i < NCBUF ; i++){ cbuf_free(mt[i]); } printc(" Done! >>>\n"); /* CBUF_ALLOC */ printc("\n<<< CBUF_ALLOC MICRO-BENCHMARK TEST >>>\n"); rdtscll(start); for (i = 0; i < NCBUF ; i++){ prev_sz += 4; prev_sz &= PAGE_SIZE-1; sz[i] = prev_sz; mt[i] = cbuf_alloc(sz[i], &cbt[i]); } rdtscll(end); printc("%d alloc_cbuf %llu cycs\n", NCBUF, (end-start)/NCBUF); printc("<<< CBUF_ALLOC MICRO-BENCHMARK TEST DONE >>>\n"); /* CBUF2BUF */ printc("\n<<< CBUF2BUF MICRO-BENCHMARK TEST >>>\n"); for (i = 0; i < NCBUF ; i++){ call_buf2buf(cbt[i], sz[i]); } printc("<<< CBUF2BUF MICRO-BENCHMARK TEST DONE >>>\n"); /* CBUF_FREE */ printc("\n<<< CBUF_FREE MICRO-BENCHMARK TEST >>>\n"); rdtscll(start); for (i = 0; i < NCBUF ; i++){ cbuf_free(mt[i]); } rdtscll(end); printc("%d free_cbuf %llu cycs avg\n", NCBUF, (end-start)/NCBUF); printc("<<< CBUF_FREE MICRO-BENCHMARK TEST DONE >>>\n"); /* CBUF_ALLOC-CBUF2BUF-CBUF_FREE */ printc("\n<<< CBUF_ALLOC-CBUF2BUF-CBUF_FREE MICRO-BENCHMARK TEST >>>\n"); prev_sz += 4; prev_sz &= PAGE_SIZE-1; sz[0] = prev_sz; rdtscll(start); for (i = 0; i < ITER ; i++){ mt[0] = cbuf_alloc(sz[0], &cbt[0]); simple_call_buf2buf(cbt[0], sz[0]); cbuf_free(mt[0]); } rdtscll(end); printc("%d alloc-cbuf2buf-free %llu cycles avg\n", ITER, (end-start)/ITER); printc("<<< CBUF_ALLOC-CBUF2BUF-CBUF_FREE MICRO-BENCHMARK TEST DONE >>>\n"); printc("\nMICRO BENCHMARK TEST (PINGPONG WITH CBUF) DONE!\n\n"); return; }
CSTUB_FN(td_t, tsplit)(struct usr_inv_cap *uc, spdid_t spdid, td_t parent_tid, char * param, int len, tor_flags_t tflags, long evtid) { long fault = 0; td_t ret; struct __sg_tsplit_data *d = NULL; cbuf_t cb = 0; struct rec_data_tor *rd = NULL; int sz = len + sizeof(struct __sg_tsplit_data); td_t curr_ptid = 0; td_t cli_tid = 0; td_t ser_tid = 0; /* printc("cli: tsplit passed in param %s\n", param); */ assert(parent_tid >= 1); assert(param && len >= 0); assert(param[len] == '\0'); if (first == 0) { cos_map_init_static(&uniq_tids); first = 1; } redo: /* printc("<<< cli rtorrent: call tsplit (thread %d, spd %ld and parent tid %d) >>>\n", */ /* cos_get_thd_id(), cos_spd_id(), parent_tid); */ rd = rd_update(parent_tid, STATE_TSPLIT_PARENT); if (rd) { curr_ptid = rd->s_tid; } else { curr_ptid = parent_tid; } /* printc("<<< In: call tsplit (thread %d, , spd %ld and curr_parent tid %d) >>>\n", */ /* cos_get_thd_id(), cos_spd_id(), curr_ptid); */ d = cbuf_alloc(sz, &cb); assert(d); if (!d) return -1; d->parent_tid = curr_ptid; d->tflags = tflags; d->evtid = evtid; d->len[0] = 0; d->len[1] = len; memcpy(&d->data[0], param, len + 1); CSTUB_INVOKE(ret, fault, uc, 3, spdid, cb, sz); if (unlikely(fault)) { CSTUB_FAULT_UPDATE(); memset(&d->data[0], 0, len); cbuf_free(cb); printc("tsplit found a fault and ready to go to redo\n"); goto redo; } cbuf_free(cb); ser_tid = ret; /* printc("passed in param %s (ser_tid %d)\n", param, ser_tid); */ assert(ser_tid >= 1); char *l_param = ""; if (len > 0) { l_param = param_save(param, len); assert(l_param); } cli_tid = map_rd_create(); assert(cli_tid >= 2); rd = map_rd_lookup(cli_tid); assert(rd); rd_cons(rd, curr_ptid, cli_tid, ser_tid, l_param, len, tflags, evtid); /* printc("tsplit done!!! return new client tid %d\n\n", cli_tid); */ return cli_tid; }
void call_pingpong_prepare(int num, int sz) { global_addr = cbuf_alloc(sz, &global_id); }
static int connection_get_reply(struct connection *c, char *resp, int resp_sz) { struct http_request *r; int used = 0; /* * Currently, this doesn't do anything interesting. In the * future it will call the content provider and get the * (ready) response. */ r = c->pending_reqs; if (NULL == r) return 0; while (r) { struct http_request *next; char *local_resp; cbuf_t cb; int consumed, ret, local_resp_sz; assert(r->c == c); if (r->flags & HTTP_REQ_PENDING) break; assert(r->flags & HTTP_REQ_PROCESSED); assert(r->content_id >= 0); /* Previously saved response? */ if (NULL != r->resp.resp) { local_resp = r->resp.resp; local_resp_sz = r->resp.resp_len; } else { int sz; /* Make the request to the content * component */ sz = resp_sz - used; local_resp = cbuf_alloc(sz, &cb); if (!local_resp) BUG(); ret = server_tread(cos_spd_id(), r->content_id, cb, sz); if (ret < 0) { cbuf_free(local_resp); printc("https get reply returning %d.\n", ret); return ret; } local_resp_sz = ret; } /* no more data */ if (local_resp_sz == 0) { cbuf_free(local_resp); break; } /* If the header and data couldn't fit into the * provided buffer, then we need to save the response, * so that we can send it out later... */ if (http_get_header(resp+used, resp_sz-used, local_resp_sz, &consumed)) { if (NULL == r->resp.resp) { char *save; save = malloc(local_resp_sz); assert(save); assert(local_resp); memcpy(save, local_resp, local_resp_sz); cbuf_free(local_resp); local_resp = NULL; r->resp.resp = save; r->resp.resp_len = local_resp_sz; } if (0 == used) { printc("https: could not allocate either header or response of sz %d:%s\n", local_resp_sz, local_resp); if (local_resp) cbuf_free(local_resp); return -ENOMEM; } break; } memcpy(resp+used+consumed, local_resp, local_resp_sz); assert(local_resp); cbuf_free(local_resp); local_resp = NULL; used += local_resp_sz + consumed; next = r->next; /* bookkeeping */ http_req_cnt++; http_free_request(r); r = c->pending_reqs; assert(r == next || NULL == r); } return used; }
void cos_init(void *arg) { td_t t1, serv; long evt; char *params1 = "foo", *params2 = "", *d; int period, num, ret, sz, i, j; u64_t start = 0, end = 0, re_cbuf; cbuf_t cb1; union sched_param sp; static int first = 1; if (first) { first = 0; sp.c.type = SCHEDP_PRIO; sp.c.value = 9; if (sched_create_thd(cos_spd_id(), sp.v, 0, 0) == 0) BUG(); return ; } evt = evt_split(cos_spd_id(), 0, 0); assert(evt > 0); serv = tsplit(cos_spd_id(), td_root, params1, strlen(params1), TOR_RW, evt); if (serv < 1) { printc("UNIT TEST FAILED: split1 failed %d\n", serv); } evt_wait(cos_spd_id(), evt); printc("client split successfully\n"); sz = 4096; j = 1000*ITER; rdtscll(start); for (i=1; i<=j; i++) { if (i == j) rdtscll(end); d = cbuf_alloc(sz, &cb1); if (!d) goto done; cbuf_send(cb1); rdtscll(end); ((u64_t *)d)[0] = end; ret = twritep(cos_spd_id(), serv, cb1, sz); cbuf_free(cb1); } printc("Client snd %d times %llu\n", j-1, (end-start)/(j-1)); /* * insert evt_grp_wait(...) into the code below where it makes * sense to. Simulate if the code were executing in separate * threads. */ parse_args(&period, &num); periodic_wake_create(cos_spd_id(), period); re_cbuf = 0; for (i=1; i<=ITER; i++) { for (j=0; j<num; j++) { rdtscll(start); d = cbuf_alloc(i*sz, &cb1); if (!d) goto done; cbuf_send_free(cb1); rdtscll(end); re_cbuf = re_cbuf+(end-start); rdtscll(end); ((u64_t *)d)[0] = end; ret = twritep(cos_spd_id(), serv, cb1, i*sz); } periodic_wake_wait(cos_spd_id()); } printc("Client: Period %d Num %d Cbuf %llu\n", period, num, re_cbuf/(num*ITER)); done: trelease(cos_spd_id(), serv); printc("client UNIT TEST PASSED: split->release\n"); printc("client UNIT TEST ALL PASSED\n"); return; }
int main(int argc, char** argv) { cbuf *eur_usd, *eur_jpy, *usd_jpy; FILE *ifile; char* fname; char pair[10]; unsigned int hour, min, isec, time; double sec, rate; int r; eur_usd = cbuf_alloc(); eur_jpy = cbuf_alloc(); usd_jpy = cbuf_alloc(); if(argc != 2) { fprintf(stderr, "usage: %s filename\n", argv[0]); exit(1); } fname = argv[1]; // Try to open file ifile = fopen (fname, "r"); if (ifile == NULL) { fprintf(stderr, "Could not open file: %s\n", fname); exit(1); } // keep reading file until no more ticks // while(1) { r = fscanf(ifile, "%7s %d:%d:%lf %lf", pair, &hour, &min, &sec, &rate); isec = sec; // fprintf(stderr, "matched %d items\n", r); // fprintf(stderr, "%s, %02d:%02d:%02d %f\n", pair, hour, min, isec, rate); time = isec + 60 * min + 3600 * hour; if (r <= 0) break; // EOF? if (strcmp(pair, "EUR-USD") == 0) { cbuf_update(eur_usd, time, rate); } else if (strcmp(pair, "EUR-JPY") == 0) { cbuf_update(eur_jpy, time, rate); } else if (strcmp(pair, "USD-JPY") == 0) { cbuf_update(usd_jpy, time, rate); } else { fprintf(stderr, "Not a recognized currency pair!\n"); } } printf("\n\n*** Data for EUR-USD ***\n"); print_data(eur_usd); printf("\n\n*** Data for EUR-JPY ***\n"); print_data(eur_jpy); printf("\n\n*** Data for USD-JPY ***\n"); print_data(usd_jpy); // Be nice, clean up cbuf_free(eur_usd); cbuf_free(eur_jpy); cbuf_free(usd_jpy); fclose(ifile) ; return 0; }