int twrite(spdid_t spdid, td_t td, int cbid, int sz) { int ret = -1; struct channel_info *channel; struct torrent *t; char *buf; if (tor_isnull(td)) return -EINVAL; LOCK(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); assert(t->data); if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, done); buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); channel = (struct channel_info*)t->data; ret = cringbuf_produce(&channels->rb, buf, sz); cos_trans_cntl(COS_TRANS_TRIGGER, 0, 0, 0); t->offset += ret; done: UNLOCK(); return ret; }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { net_connection_t nc; struct torrent *t; char *buf; int ret; buf = cbuf2buf(cbid, sz); if (!buf) return -EINVAL; if (tor_isnull(td)) return -EINVAL; NET_LOCK_TAKE(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, done); assert(t->data); nc = (net_connection_t)t->data; ret = net_recv(spdid, nc, buf, sz); done: NET_LOCK_RELEASE(); assert(lock_contested(&net_lock) != cos_get_thd_id()); return ret; }
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 tread(spdid_t spdid, td_t td, int cbid, int sz) { int ret = -1; struct channel_info *channel; struct torrent *t; char *buf; if (tor_isnull(td)) return -EINVAL; LOCK(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); assert(!tor_is_usrdef(td) || t->data); if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, done); buf = cbuf2buf(cbid, sz); if (!buf) goto done; channel = (struct channel_info*)t->data; ret = cringbuf_consume(&channel->rb, buf, sz); done: UNLOCK(); return ret; }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { struct connection *c; struct torrent *t; char *buf; int ret; if (tor_isnull(td)) return -EINVAL; buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); LOCK(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, unlock); assert(!tor_is_usrdef(td) || t->data); if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, unlock); c = t->data; lock_connection(c); UNLOCK(); ret = connection_get_reply(c, buf, sz); unlock_connection(c); done: return ret; unlock: UNLOCK(); goto done; }
int twrite(spdid_t spdid, td_t td, int cbid, int sz) { struct connection *c = NULL; struct torrent *t; char *buf; int ret = -1; if (tor_isnull(td)) return -EINVAL; buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); LOCK(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, unlock); if (!(t->flags & TOR_WRITE)) ERR_THROW(-EACCES, unlock); c = t->data; assert(c); lock_connection(c); UNLOCK(); if (connection_parse_requests(c, buf, sz)) ERR_THROW(-EINVAL, release); unlock_connection(c); ret = sz; done: return ret; unlock: UNLOCK(); release: unlock_connection(c); goto done; }
int twrite(spdid_t spdid, td_t td, int cbid, int sz) { struct torrent *t; char *buf; int ret = -1; 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); buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); ret = netif_event_xmit(spdid, buf, sz); /* // debug only */ /* cbuf_t debug_cb; */ /* if (debug_first == 0) { */ /* debug_first = 1; */ /* if (!(debug_buf = cbuf_alloc(sz, &debug_cb))) BUG(); */ /* memcpy(debug_buf, buf, sz); */ /* debug_amnt = sz; */ /* } */ 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; }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { int ret = -1, left; struct torrent *t; struct fsobj *fso; char *buf; if (tor_isnull(td)) return -EINVAL; LOCK(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, done); assert(!tor_is_usrdef(td) || t->data); if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, done); fso = t->data; assert(fso->size <= fso->allocated); assert(t->offset <= fso->size); if (!fso->size) ERR_THROW(0, done); buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); left = fso->size - t->offset; ret = left > sz ? sz : left; assert(fso->data); memcpy(buf, fso->data + t->offset, ret); t->offset += ret; cbuf_free(cbid); done: UNLOCK(); return ret; }
int simple_call_buf2buf(cbuf_t cb, int len) { char *b; b = cbuf2buf(cb,len); assert(b); return 0; }
void call_cbuf_send(cbuf_t cb, unsigned long sz, int num) { char *b; b = (char *)cbuf2buf(cb, sz); assert(!num); cbuf_free(cb); }
int call_cbuf2buf(cbuf_t cb, int len) { char *b; b = cbuf2buf(cb, len); assert(b); cbuf_free(cb); return 0; }
cbuf_t call_cbuf_pingpong(cbuf_t cb, unsigned long sz) { char *b; b = (char *)cbuf2buf(cb, sz); assert(b && b[0] == '$'); cbuf_free(cb); global_addr[0] = '&'; cbuf_send(global_id); return global_id; }
void cos_init(void) { int i = 0; int ret; int data; void *buf_read, *buf_write; cbuf_t read_buffer, write_buffer; printc("pong init\n"); if (replica_confirm(cos_spd_id())) BUG(); /* Get our buffers*/ write_buffer = get_write_buf(cos_spd_id()); read_buffer = get_read_buf(cos_spd_id()); buf_read = cbuf2buf(read_buffer, 1024); buf_write = cbuf2buf(write_buffer, 1024); printc("pong confirmed with buffers read (%d) and write(%d)\n", read_buffer, write_buffer); confirm_fork(cos_spd_id()); while (i < N_ROUNDS) { printc("\ni = %d, pong calling read from spdid %d\n", i, cos_spd_id()); ret = nread(cos_spd_id(), 0, 1); assert(ret); data = *((int *) buf_read); printc("Thread %d: read returned %d and now we have data [%d]\n\n", cos_get_thd_id(), ret, data++); printc("\ni = %d, pong calling write\n", i); memcpy(buf_write, (void*)&data, 1); ret = nwrite(cos_spd_id(), 1, 1); assert(ret); printc("Thread %d: write returned %d\n\n", cos_get_thd_id(), ret); i++; } /* * This will actually never execute because this thread was put to sleep and once the last spd returns and exits, nothing is there to wake it up * (minor edge case, voter_monitor would be the ideal place to fix */ printc("Spdid %d finished.\n", cos_spd_id()); }
void unit_cbuf(cbuf_t cbuf, int sz) { char *c = cbuf2buf(cbuf, sz); cbuf_t cb; char *addr; assert(c); assert(c[0] == '_'); addr = cbuf_alloc_ext(sz, &cb, CBUF_TMEM); cbuf_free(cb); cbuf_free(cbuf); }
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); }
int __sg_tmerge(spdid_t spdid, cbuf_t cbid, int len) { struct __sg_tmerge_data *d; d = cbuf2buf(cbid, len); if (unlikely(!d)) return -1; /* mainly to inform the compiler that optimizations are possible */ if (unlikely(d->len[0] != 0)) return -1; if (unlikely(d->len[0] >= d->len[1])) return -1; if (unlikely(((int)(d->len[1] + (sizeof(struct __sg_tmerge_data)))) != len)) return -1; return tmerge(spdid, d->td, d->td_into, &d->data[0], d->len[1] - d->len[0]); }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { struct connection *c; struct torrent *t; char *buf; int ret; /* printc("connmgr reads https thd %d\n", cos_get_thd_id()); */ if (tor_isnull(td)) return -EINVAL; buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); LOCK(); t = tor_lookup(td); if (!t) ERR_THROW(-EINVAL, unlock); assert(!tor_is_usrdef(td) || t->data); if (!(t->flags & TOR_READ)) ERR_THROW(-EACCES, unlock); c = t->data; lock_connection(c); UNLOCK(); /* // debug only */ /* if (debug_buf && debug_amnt > 0) { */ /* printc("use saved cbuf\n"); */ /* memcpy(buf, debug_buf, sz); */ /* ret = debug_amnt; */ /* unlock_connection(c); */ /* goto done; */ /* } */ ret = connection_get_reply(c, buf, sz); /* // debug only */ /* if (!debug_buf && debug_amnt == 0) { */ /* if (!(debug_buf = cbuf_alloc(sz, &debug_cb))) BUG(); */ /* printc("save the response cbuf\n"); */ /* memcpy(debug_buf, buf, sz); */ /* debug_amnt = ret; */ /* } */ unlock_connection(c); done: return ret; unlock: UNLOCK(); goto done; }
cbuf_t f(cbuf_t cb, int len) { char *b; b = cbuf2buf(cb, len); if (!b) { printc("WTF\n"); return cbuf_null(); } // memset(b, 'b', len); return cb; }
int __sg_twmeta(spdid_t spdid, cbuf_t cbid, int len) { struct __sg_twmeta_data *d; d = cbuf2buf(cbid, len); if (unlikely(!d)) return -5; /* mainly to inform the compiler that optimizations are possible */ if (unlikely(d->klen <= 0)) return -2; if (unlikely(d->vlen <= 0)) return -2; // TODO: write "" to td->data? if (unlikely(d->td == 0)) return -EINVAL; return twmeta(spdid, d->td, &d->data[0], d->klen, &d->data[d->klen + 1], d->vlen); }
td_t __sg_tsplit(spdid_t spdid, cbuf_t cbid, int len) { struct __sg_tsplit_data *d; d = cbuf2buf(cbid, len); if (unlikely(!d)) return -5; /* mainly to inform the compiler that optimizations are possible */ if (unlikely(d->len[0] != 0)) return -2; if (unlikely(d->len[0] > d->len[1])) return -3; if (unlikely(((int)(d->len[1] + sizeof(struct __sg_tsplit_data))) != len)) return -4; if (unlikely(d->tid == 0)) return -EINVAL; printc("tsplit ser (torrent): tid %d\n", d->tid); return tsplit(spdid, d->tid, &d->data[0], d->len[1] - d->len[0], d->tflags, d->evtid); }
cbuf_t f(cbuf_t cb, int len) { char *b; printc("\n****** BOT: thread %d in spd %ld ******\n",cos_get_thd_id(), cos_spd_id()); b = cbuf2buf(cb, len); if (!b) { printc("WTF\n"); return cbuf_null(); } /* check_val(); */ memset(b, 'b', len); return cb; }
int call_buf2buf(u32_t cb, int len) { u64_t start = 0, end = 0; char *b; rdtscll(start); b = cbuf2buf(cb,len); rdtscll(end); printc("cbuf2buf %llu cycs\n", end-start); if (!b) { printc("Can not map into this spd %ld\n", cos_spd_id()); return cbuf_null(); } memset(b, 's', len); return 0; }
/* * For a certain principal, collect any unreferenced persistent cbufs * so that they can be reused. This is the garbage-collection * mechanism. * * Collect cbufps and add them onto the component's freelist. */ int cbufp_collect(spdid_t spdid, int size, long cbid) { long *buf; int off = 0; struct cbufp_info *cbi; struct cbufp_comp_info *cci; int ret = -EINVAL; buf = cbuf2buf(cbid, PAGE_SIZE); if (!buf) return -1; CBUFP_TAKE(); cci = cbufp_comp_info_get(spdid); if (!cci) { CBUFP_RELEASE(); return -ENOMEM; } /* Go through all cbufs we own, and save all of them that have * no current references to them. */ cbi = cci->cbufs.c; do { if (!cbi) break; if (!cbufp_referenced(cbi)) { cbufp_references_clear(cbi); buf[off++] = cbi->cbid; if (off == PAGE_SIZE/sizeof(int)) break; } cbi = FIRST_LIST(cbi, next, prev); } while (cbi != cci->cbufs.c); CBUFP_RELEASE(); /* nothing collected...create a new one! */ /* TODO: only allocate when we should, and sleep otherwise */ /* if (off == 0) { */ /* int r = cbufp_create(spdid, size, 0); */ /* if (r) buf[off++] = r; */ /* } */ ret = off; return ret; }
int twrite(spdid_t spdid, td_t td, int cbid, int sz) { LOGD("twrite invoked"); char *buf; char *errMsg = NULL; int ret = 0; buf = cbuf2buf(cbid, sz); LOGD("buf: %s", buf); sqlite3 *db = g_handler[td]; LOGD("db: %p", db); if (SQLITE_OK != sqlite3_exec(db, buf, NULL, NULL, &errMsg)){ LOGD("ERR: %s\n", errMsg); sqlite3_free(errMsg); ret = -1; } else { LOGD("Execute Successfully.\n"); ret = 0; } return ret; }
int tread(spdid_t spdid, td_t td, int cbid, int sz) { struct torrent *t; char *buf; int ret = -1; 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); buf = cbuf2buf(cbid, sz); if (!buf) ERR_THROW(-EINVAL, done); ret = netif_event_wait(spdid, buf, sz); /* // debug ? */ /* if (debug_first == 1) { */ /* ret = netif_event_xmit(spdid, debug_buf, debug_amnt); */ /* } */ done: return ret; }
int simple_call_buf2buf(u32_t cb, int len) { char *b; b = cbuf2buf(cb,len); return 0; }
void unit_cbufp2buf(cbuf_t cbuf, int sz) { char *c = cbuf2buf(cbuf, sz); assert(!c); }
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; }