void dft(struct graph *g, struct vnode *v, int id) { struct enode *e; int first; first = 1; v->visited = 1; e = v->edge; while(e != NULL) { if( !g->vn[e->vndx].visited) { if(first) { first = 0; }else{ indent(id); } printf("-->%d", g->vn[e->vndx].item); id += 3 + INCR(g->vn[e->vndx].item); dft(g,&(g->vn[e->vndx]),id); id -= 3 + INCR(g->vn[e->vndx].item); } e = e->next; } if(first) printf("\n"); }
static void _server_event(void *arg, uint32_t events) { struct buf_sock *s = arg; log_verb("server event %06"PRIX32" on buf_sock %p", events, s); if (events & EVENT_ERR) { INCR(server_metrics, server_event_error); _server_close(s); return; } if (events & EVENT_READ) { log_verb("processing server read event on buf_sock %p", s); INCR(server_metrics, server_event_read); _server_event_read(s); } if (events & EVENT_WRITE) { /* the only server write event is write on pipe */ log_verb("processing server write event"); _server_pipe_write(); INCR(server_metrics, server_event_write); } }
static inline void _klog_write_get(struct request *req, struct response *rsp, char *buf, int len) { struct response *nr = rsp; int suffix_len; uint32_t i; struct bstring *key; for (i = 0; i < array_nelem(req->keys); ++i) { key = array_get(req->keys, i); if (nr->type != RSP_END && bstring_compare(key, &nr->key) == 0) { /* key was found, rsp at nr */ suffix_len = cc_scnprintf(buf + len, KLOG_MAX_LEN - len, KLOG_GET_FMT, req_strings[req->type].len, req_strings[req->type].data, key->len, key->data, rsp->type, _get_val_rsp_len(nr, key)); nr = STAILQ_NEXT(nr, next); } else { /* key not found */ suffix_len = cc_scnprintf(buf + len, KLOG_MAX_LEN - len, KLOG_GET_FMT, req_strings[req->type].len, req_strings[req->type].data, key->len, key->data, RSP_UNKNOWN, 0); } ASSERT(len + suffix_len <= KLOG_MAX_LEN); if (log_write(klogger, buf, len + suffix_len)) { INCR(klog_metrics, klog_logged); } else { INCR(klog_metrics, klog_discard); } } ASSERT(nr ->type == RSP_END); }
ref * dstack_find_name_by_index(dict_stack_t * pds, uint nidx) { ref *pvalue = real_dstack_find_name_by_index(pds, nidx); dict *pdict = pds->stack.p->value.pdict; INCR(lookups); if (dict_is_packed(pdict)) { uint hash = dict_hash_mod(dict_name_index_hash(nidx), npairs(pdict)) + 1; if (pdict->keys.value.packed[hash] == pt_tag(pt_literal_name) + nidx ) INCR(probes[0]); else if (pdict->keys.value.packed[hash - 1] == pt_tag(pt_literal_name) + nidx ) INCR(probes[1]); } if (gs_debug_c('d') && !(stats_dstack.lookups % 1000)) dlprintf3("[d]lookups=%ld probe1=%ld probe2=%ld\n", stats_dstack.lookups, stats_dstack.probes[0], stats_dstack.probes[1]); return pvalue; }
static void _process_get(struct response *rsp, struct request *req) { struct bstring *key; struct response *r = rsp; uint32_t i; INCR(process_metrics, get); /* use chained responses, move to the next response if key is found. */ for (i = 0; i < array_nelem(req->keys); ++i) { INCR(process_metrics, get_key); key = array_get(req->keys, i); if (_get_key(r, key)) { req->nfound++; r->cas = false; r = STAILQ_NEXT(r, next); if (r == NULL) { INCR(process_metrics, get_ex); log_warn("get response incomplete due to lack of rsp objects"); return; } INCR(process_metrics, get_key_hit); } else { INCR(process_metrics, get_key_miss); } } r->type = RSP_END; log_verb("get req %p processed, %d out of %d keys found", req, req->nfound, i); }
MATRIZ crearMatriz() { MATRIZ m; int i, j, ind; int il, ir, ju, jd; // left of i, right of i, up of j, down of j for (j=0; j < L; j++) { for (i=0; i < L; i++) { ind = POS(i, j); il = DECR(i); ir = INCR(i); ju = DECR(j); jd = INCR(j); m.u[ind] = POS(i, ju); m.d[ind] = POS(i, jd); m.l[ind] = POS(il, j); m.r[ind] = POS(ir, j); m.ul[ind] = POS(il, ju); m.ur[ind] = POS(ir, ju); m.dl[ind] = POS(il, jd); m.dr[ind] = POS(ir, jd); } } return m; }
static void wUCol(uchar **row, uchar sflag, ulong icol, bool isi) { if (!sflag) return; if (sflag == COL_1BYTE_INT) { **row = (char)icol; INCR(*row); } else if (sflag == COL_2BYTE_INT) { memcpy(*row, &icol, 2); INCRBY(*row, 2); } else if (sflag == COL_4BYTE_INT) { memcpy(*row, &icol, 4); INCRBY(*row, 4); } else { if (isi) { /* INT */ **row = COL_5BYTE_INT; INCR(*row); memcpy(*row, &icol, 4); INCRBY(*row, 4); } else if (sflag == COL_6BYTE_INT) { /* LONG */ memcpy(*row, &icol, 6); INCRBY(*row, 6); } else if (sflag == COL_8BYTE_INT) { memcpy(*row, &icol, 8); INCRBY(*row, 8); } else { **row = COL_9BYTE_INT; INCR(*row); memcpy(*row, &icol, 8); INCRBY(*row, 8); } } }
int main(int argc, char **argv) { segments_init(); buttons_init(); uint8_t counters[4] = { 0, 0, 0, 0 }; #define INCR(i) \ if (++counters[i] >= 10) { \ counters[i] = 0; \ } while (1) { buttons_update(); if (button_is_down(0)) { INCR(0) } if (button_pressed(1)) { INCR(1) } if (button_pressed(2)) { INCR(2) } for (size_t i = 0; i < sizeof(counters) / sizeof(counters[0]); i++) { segments_set_digit(i, counters[i]); } segments_draw(); _delay_ms(1); } }
/* * Get a slab. * id is the slabclass the new slab will be linked into. * * We return a slab either from the: * 1. slab pool, if not empty. or, * 2. evict an active slab and return that instead. */ static rstatus_i _slab_get(uint8_t id) { rstatus_i status; struct slab *slab; ASSERT(slabclass[id].next_item_in_slab == NULL); ASSERT(SLIST_EMPTY(&slabclass[id].free_itemq)); slab = _slab_get_new(); if (slab == NULL && (evict_opt & EVICT_CS)) { slab = _slab_evict_lru(id); } if (slab == NULL && (evict_opt & EVICT_RS)) { slab = _slab_evict_rand(); } if (slab != NULL) { _slab_init(slab, id); status = CC_OK; } else { status = CC_ENOMEM; INCR(slab_metrics, slab_req_ex); } INCR(slab_metrics, slab_req); return status; }
static inline struct item * _add_key(struct response *rsp, struct bstring *key) { struct element *reply = (struct element *)array_get(rsp->token, 0); struct item *it; item_rstatus_e istatus; it = item_get(key); if (it != NULL) { rsp->type = reply->type = ELEM_ERR; reply->bstr = str2bstr(RSP_EXIST); INCR(process_metrics, list_create_exist); return NULL; } else { /* TODO: figure out a TTL story here */ istatus = item_reserve(&it, key, NULL, ZIPLIST_HEADER_SIZE, 0, INT32_MAX); if (istatus != ITEM_OK) { rsp->type = reply->type = ELEM_ERR; reply->bstr = str2bstr(RSP_ERR_STORAGE); INCR(process_metrics, list_create_ex); INCR(process_metrics, process_ex); } else { INCR(process_metrics, list_create_stored); } return it; } }
/* * Allocate an item. We allocate an item by consuming the next free item * from slab of the item's slab class. * * On success we return the pointer to the allocated item. */ static item_rstatus_e _item_alloc(struct item **it_p, uint8_t klen, uint32_t vlen, uint8_t olen) { uint8_t id = slab_id(item_ntotal(klen, vlen, olen)); struct item *it; log_verb("allocate item with klen %u vlen %u", klen, vlen); *it_p = NULL; if (id == SLABCLASS_INVALID_ID) { return ITEM_EOVERSIZED; } it = slab_get_item(id); *it_p = it; if (it != NULL) { _item_reset(it); slab_ref(item_to_slab(it)); /* slab to be deref'ed in _item_link */ INCR(slab_metrics, item_curr); INCR(slab_metrics, item_alloc); PERSLAB_INCR(id, item_curr); log_verb("alloc it %p of id %"PRIu8" at offset %"PRIu32, it, it->id, it->offset); return ITEM_OK; } else { INCR(slab_metrics, item_alloc_ex); log_warn("server error on allocating item in slab %"PRIu8, id); return ITEM_ENOMEM; } }
void ProductType::move_assign_impl(void *dst, void *src) { for (int i = 0; i<n; ++i) { auto vt = cp[i]; auto align = vt->object_alignment(); src = round_up(src,align); dst = round_up(dst,align); vt->move_assign(dst,src); auto z = vt->object_size(); INCR(src, z); INCR(dst, z); } }
static int walkAtomicVar(NCD4meta* compiler, NCD4node* topvar, NCD4node* var, void** offsetp) { int ret = NC_NOERR; void* offset; d4size_t i; nc_type subsort; d4size_t dimproduct; NCD4node* basetype; basetype = (var->sort == NCD4_TYPE ? var : var->basetype); subsort = basetype->subsort; dimproduct = (var->sort == NCD4_TYPE ? 1 : NCD4_dimproduct(var)); offset = *offsetp; if(subsort == NC_ENUM) subsort = var->basetype->basetype->subsort; /* Only need to swap multi-byte integers and floats */ if(subsort != NC_STRING) { int typesize = NCD4_typesize(subsort); d4size_t totalsize = typesize*dimproduct; if(typesize == 1) { offset = INCR(offset,totalsize); } else { /*(typesize > 1)*/ for(i=0;i<dimproduct;i++) { char* sp = (char*)offset; if(compiler->swap) { switch (typesize) { case 2: swapinline16(sp); break; case 4: swapinline32(sp); break; case 8: swapinline64(sp); break; default: break; } } offset = INCR(offset,typesize); } } } else if(subsort == NC_STRING) { /* remaining case; just convert the counts */ COUNTERTYPE count; for(i=0;i<dimproduct;i++) { /* Get string count */ if(compiler->swap) swapinline64(offset); count = GETCOUNTER(offset); SKIPCOUNTER(offset); /* skip count bytes */ offset = INCR(offset,count); } } *offsetp = offset; return THROW(ret); }
void MessageChannel::send(const void* message, int len) { status = 0; BLOCK_DECR(SHM_MUTEX); // grab access to the read // I have access now so I can go ahead and set the message // write data // printf("send: writing %i to len\n",len); memcpy(data,&len,sizeof(int)); memcpy(((unsigned char*)data) + WORDALIGN,message,len); INCR(0); // flag a message available INCR(SHM_MUTEX); // release the access }
void Player::PlayerThread::run(){ while(true){ switch(player->threadstate){ case Thread_Cancelled: //threads should exit return; case Thread_Wait_Start: //threads are waiting to start case Thread_Wait_Start_Cancelled: player->runbarrier.wait(); CAS(player->threadstate, Thread_Wait_Start, Thread_Running); CAS(player->threadstate, Thread_Wait_Start_Cancelled, Thread_Cancelled); break; case Thread_Wait_End: //threads are waiting to end player->runbarrier.wait(); CAS(player->threadstate, Thread_Wait_End, Thread_Wait_Start); break; case Thread_Running: //threads are running if(player->rootboard.won() >= 0 || player->root.outcome >= 0 || (player->maxruns > 0 && player->runs >= player->maxruns)){ //solved or finished runs if(CAS(player->threadstate, Thread_Running, Thread_Wait_End) && player->root.outcome >= 0) logerr("Solved as " + to_str((int)player->root.outcome) + "\n"); break; } if(player->ctmem.memalloced() >= player->maxmem){ //out of memory, start garbage collection CAS(player->threadstate, Thread_Running, Thread_GC); break; } INCR(player->runs); iterate(); break; case Thread_GC: //one thread is running garbage collection, the rest are waiting case Thread_GC_End: //once done garbage collecting, go to wait_end instead of back to running if(player->gcbarrier.wait()){ Time starttime; logerr("Starting player GC with limit " + to_str(player->gclimit) + " ... "); uint64_t nodesbefore = player->nodes; Board copy = player->rootboard; player->garbage_collect(copy, & player->root); Time gctime; player->ctmem.compact(1.0, 0.75); Time compacttime; logerr(to_str(100.0*player->nodes/nodesbefore, 1) + " % of tree remains - " + to_str((gctime - starttime)*1000, 0) + " msec gc, " + to_str((compacttime - gctime)*1000, 0) + " msec compact\n"); if(player->ctmem.meminuse() >= player->maxmem/2) player->gclimit = (int)(player->gclimit*1.3); else if(player->gclimit > player->rollouts*5) player->gclimit = (int)(player->gclimit*0.9); //slowly decay to a minimum of 5 CAS(player->threadstate, Thread_GC, Thread_Running); CAS(player->threadstate, Thread_GC_End, Thread_Wait_End); } player->gcbarrier.wait(); break; } } }
static void deallocate_spine(qt_hash h, size_t id) { qt_free((void *)(h->spines[id])); // XXX should be to a memory pool h->spines[id] = NULL; INCR(&h->numspines, -1); }
void cmd_list_create(struct response *rsp, struct request *req, struct command *cmd) { struct item *it; struct bstring *key = _get_key(req); struct element *reply = (struct element *)array_push(rsp->token); INCR(process_metrics, list_create); it = _add_key(rsp, key); if (it == NULL) { log_debug("command '%.*s' '%.*s' failed: cannot store", cmd->bstr.len, cmd->bstr.data, key->len, key->data); return; } /* initialize data structure */ ziplist_reset((ziplist_p)item_data(it)); it->vlen = ZIPLIST_HEADER_SIZE; /* link into index */ item_insert(it, key); rsp->type = reply->type = ELEM_STR; reply->bstr = str2bstr(RSP_OK); log_verb("command '%.*s' '%.*s' succeeded", cmd->bstr.len, cmd->bstr.data, key->len, key->data); }
/* Assumes that compiler->swap is true; does necessary byte swapping. */ int NCD4_swapdata(NCD4meta* compiler, NClist* topvars) { int ret = NC_NOERR; int i; void* offset; offset = compiler->serial.dap; for(i=0;i<nclistlength(topvars);i++) { NCD4node* var = (NCD4node*)nclistget(topvars,i); var->data.dap4data.memory = offset; switch (var->subsort) { default: if((ret=walkAtomicVar(compiler,var,var,&offset))) goto done; break; case NC_OPAQUE: /* The only thing we need to do is swap the counts */ if((ret=walkOpaqueVar(compiler,var,var,&offset))) goto done; break; case NC_STRUCT: if((ret=walkStructArray(compiler,var,var,&offset))) goto done; break; case NC_SEQ: if((ret=walkSeqArray(compiler,var,var,&offset))) goto done; break; } var->data.dap4data.size = DELTA(offset,var->data.dap4data.memory); /* skip checksum, if there is one */ if(compiler->serial.remotechecksumming) offset = INCR(offset,CHECKSUMSIZE); } done: return THROW(ret); }
void* lf_table_put_if_absent(LF_HashTable* table, void* key, void* value) { marked_ptr_t* prev; marked_ptr_t cur; marked_ptr_t new_node; uint32_t index; index = HASH(key); while(1) { if(lf_table_find(&table->buckets[index], CONSTRUCT(0, key), &prev, &cur) != NULL) { return PTR_OF(cur)->value; } new_node = CONSTRUCT(0, malloc(sizeof(Node))); PTR_OF(new_node)->value = value; PTR_OF(new_node)->key = CONSTRUCT(0, key); PTR_OF(new_node)->next = *prev; if(CAS(prev, cur, CONSTRUCT(0, new_node)) == cur) { break; } } INCR(&table->size, 1); return NULL; }
struct response * response_create(void) { struct response *rsp = cc_alloc(sizeof(struct response)); if (rsp == NULL) { return NULL; } response_reset(rsp); INCR(response_metrics, response_create); INCR(response_metrics, response_free); return rsp; }
void ProductType::destroy_impl (void *p) { for (int i = 0; i<n; ++i) { auto vt = cp[i]; p = round_up(p,vt->object_alignment()); vt->destroy(p); INCR(p,vt->object_size()); } }
static void _admin_version(struct response *rsp, struct request *req) { INCR(admin_metrics, version); rsp->type = RSP_GENERIC; cc_snprintf(version_buf, VERSION_PRINT_LEN, VERSION_PRINT_FMT, VERSION_STRING); rsp->data = str2bstr(version_buf); }
void request_return(struct request **request) { struct request *req = *request; if (req == NULL) { return; } INCR(request_metrics, request_free); INCR(request_metrics, request_return); log_vverb("return req %p", req); req->free = true; FREEPOOL_RETURN(req, &reqp, next); *request = NULL; }
int lf_table_remove(LF_HashTable* table, void* key) { if(remove_node(&table->buckets[HASH(key)], CONSTRUCT(0,key))) { INCR(&table->size, -1); return 1; } return 0; }
void response_destroy(struct response **response) { struct response *rsp = *response; ASSERT(rsp != NULL); INCR(response_metrics, response_destroy); cc_free(rsp); *response = NULL; }
void request_destroy(struct request **request) { struct request *req = *request; ASSERT(req != NULL); INCR(request_metrics, request_destroy); array_destroy(&req->keys); cc_free(req); *request = NULL; }
/* * Return a single response object */ void response_return(struct response **response) { ASSERT(response != NULL); struct response *rsp = *response; if (rsp == NULL) { return; } INCR(response_metrics, response_free); INCR(response_metrics, response_return); log_vverb("return rsp %p", rsp); rsp->free = true; FREEPOOL_RETURN(rsp, &rspp, next); *response = NULL; }
void cmd_list_delete(struct response *rsp, struct request *req, struct command *cmd) { struct bstring *key = _get_key(req); struct element *reply = (struct element *)array_push(rsp->token); INCR(process_metrics, list_delete); if (item_delete(key)) { reply->bstr = str2bstr(RSP_OK); INCR(process_metrics, list_delete_deleted); log_verb("command '%.*s' '%.*s' succeeded", cmd->bstr.len, cmd->bstr.data, key->len, key->data); } else { reply->bstr = str2bstr(RSP_NOTFOUND); INCR(process_metrics, list_delete_notfound); log_verb("command '%.*s' '%.*s' completed as no-op, key not found", cmd->bstr.len, cmd->bstr.data, key->len, key->data); } }
static inline void _item_dealloc(struct item **it_p) { uint8_t id = (*it_p)->id; DECR(slab_metrics, item_curr); INCR(slab_metrics, item_dealloc); PERSLAB_DECR(id, item_curr); slab_put_item(*it_p, id); *it_p = NULL; }
static size_t allocate_spine(qt_hash h, spine_t **realspine) { size_t newspine_ct = INCR(&h->numspines, 1) + 1; size_t maxspines = h->maxspines; spine_t *newspine; size_t id; if (newspine_ct >= maxspines) { // Need to make more room in the spine-idx array spine_t **spines = qt_calloc(maxspines * 2, sizeof(spine_t *)); spine_t **oldspines = h->spines; spine_t **tmp; assert(spines); memcpy(spines, h->spines, sizeof(spine_t *) * maxspines); tmp = CAS(&h->spines, oldspines, spines); if (tmp != oldspines) { // someone else added more spines FREE(spines, maxspines * 2 * sizeof(spine_t *)); } else { INCR(&h->maxspines, maxspines); } maxspines *= 2; } newspine = qt_calloc(1, sizeof(spine_t)); // XXX should be from a memory pool assert(newspine); for(id = 0; id < maxspines; ++id) { if (h->spines[id] == NULL) { spine_t *tmp = CAS(&h->spines[id], NULL, newspine); if (tmp == NULL) { break; // success! } } } assert(id < maxspines); if (realspine != NULL) { *realspine = (spine_t *)newspine; } return id; }