/** Creates a list of transactions from parsed data. The parsed data * will first be validated. If any errors are found in lines that are marked * for processing (ie not marked to skip) this function will * throw an error. * @param skip_errors true skip over lines with errors * @exception throws std::invalid_argument if data validation or processing fails. */ void GncTxImport::create_transactions () { /* Start with verifying the current data. */ auto verify_result = verify(); if (!verify_result.empty()) throw std::invalid_argument (verify_result); /* Drop all existing draft transactions */ m_transactions.clear(); m_parent = nullptr; /* Iterate over all parsed lines */ for (auto parsed_lines_it = m_parsed_lines.begin(); parsed_lines_it != m_parsed_lines.end(); ++parsed_lines_it) { /* Skip current line if the user specified so */ if ((std::get<PL_SKIP>(*parsed_lines_it))) continue; /* Should not throw anymore, otherwise verify needs revision */ create_transaction (parsed_lines_it); } }
int PDQ_CreateOpen_p(char *name, double *lambda) { extern char s1[]; extern int streams; extern char tUnit[]; extern char wUnit[]; extern int PDQ_DEBUG; FILE *out_fd; if (PDQ_DEBUG) { out_fd = fopen("PDQ.out", "a"); fprintf(out_fd, "name : %s lambda : %f\n", name, *lambda); //PJP This should really be fclose // close(out_fd); fclose(out_fd); } if (strlen(name) > MAXCHARS) { sprintf(s1, "Nodename \"%s\" is longer than %d characters", name, MAXCHARS); errmsg("PDQ_CreateOpen()", s1); } create_transaction(OPEN, name, *lambda); // Set default units strcpy(wUnit, "Trans"); strcpy(tUnit, "Sec"); c = ++streams; return(0); // silence gcc warnings } // PDQ_CreateOpen
int lookup_service(int fd, uint16_t *name, int len, void **binder, void **cookie) { unsigned char buf[1024], *p; obj_t *obj; bcmd_txn_t *txn; tdata_t *tdata; int r; p = buf; // strict_policy *(uint32_t *)p = 0; p += 4; // svcmgr_id *(uint32_t *)p = sizeof(svcmgr_id) / 2; p += 4; memcpy(p, svcmgr_id, sizeof(svcmgr_id)); p += sizeof(svcmgr_id); *(uint16_t *)p = 0; p += 2; p = (unsigned char *)ALIGN((unsigned long)p); // name *(uint32_t *)p = len; p += 4; memcpy(p, name, len * 2); p += len * 2; *(uint16_t *)p = 0; p += 2; p = (unsigned char *)ALIGN((unsigned long)p); txn = create_transaction(0, NULL, NULL, 1, buf, p - buf, NULL, 0); if (!txn) return -1; r = simple_transact(fd, txn, &tdata, buf, sizeof(buf)); if (r < 0) return r; if (tdata->data_size == 4 && !*(unsigned int *)tdata->data.ptr.buffer) return 0; // server not ready if (tdata->data_size != sizeof(*obj) || tdata->offsets_size != 4) { fprintf(stderr, "client %d invalid reply data received\n", id); return -1; } obj = (obj_t *)((unsigned char *)tdata->data.ptr.buffer + *(unsigned int *)tdata->data.ptr.offsets); if (obj->type != BINDER_TYPE_HANDLE) { fprintf(stderr, "client %d invalid object type received\n", id); return -1; } *binder = obj->binder; *cookie = obj->cookie; free(txn); return 1; }
/* send the msg according to the routing table * returns 0 on success, <0 on error */ int do_route(AAAMessage *msg, struct peer *in_p) { struct peer_entry_list* pl; struct trans* tr; pl=route_dest(&msg->dest_realm->data); if (pl==0) goto noroute; /* transaction stuff */ tr=create_transaction(&(msg->buf), in_p, 0); if (tr==0) goto error_transaction; update_forward_transaction_from_msg(tr, msg); /* try to send it to the first peer in the route */ for(;pl;pl=pl->next){ DBG("do_route: pl=%p <%.*s>, peer=%p\n", pl, pl->pe->full_uri.len, pl->pe->full_uri.s, pl->pe->peer); if (pl->pe->peer){ if (send_req_to_peer(tr, pl->pe->peer)<0){ LOG(L_WARN, "WARNING: do_route: unable to send to %.*s\n", pl->pe->full_uri.len, pl->pe->full_uri.s); continue; /*try next peer*/ }else{ DBG("do_route: sending msg to %.*s\n", pl->pe->full_uri.len, pl->pe->full_uri.s); goto end; } } } /* if we are here => all sends failed */ /* cleanup */ destroy_transaction(tr); noroute: LOG(L_ERR, "ERROR: do_route: dropping message, no peers/route found\n"); return -1; end: return 1; error_transaction: LOG(L_ERR, "ERROR: do_route: unable to create transaction\n"); return -1; }
int client_main(void) { int fd, r, n, m, wait = 0, retries,pid; void *binder, *cookie; bcmd_txn_t *txn; bwr_t bwr; inst_buf_t *inst, *inst_reply; inst_entry_t *entry, copy; unsigned char rbuf[RBUF_SIZE], *ibuf, *p; struct timeval ref, delta; char labels[INST_MAX_ENTRIES][8]; unsigned long long total_usecs[INST_MAX_ENTRIES]; unsigned long long min[INST_MAX_ENTRIES],max[INST_MAX_ENTRIES],record[INST_MAX_ENTRIES]; FILE *fp; if (!share_cpus) { cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(id + 1, &cpuset); r = sched_setaffinity(0, sizeof(cpuset), &cpuset); if (!r) printf("client %d is bound to CPU %d\n", id, id + 1); else fprintf(stderr, "client %d failed to be bound to CPU %d\n", id, id + 1); } fd = open("/dev/binder", O_RDWR); if (fd < 0) { fprintf(stderr, "client %d failed to open binder device\n", id); return -1; } #if (!defined(INLINE_TRANSACTION_DATA)) if (mmap(NULL, 128 * 1024, PROT_READ, MAP_PRIVATE, fd, 0) == MAP_FAILED) { fprintf(stderr, "server failed to mmap shared buffer\n"); return -1; } #endif while (1) { r = lookup_service(fd, service, sizeof(service) / 2, &binder, &cookie); if (r < 0) { fprintf(stderr, "client %d failed to find the instrumentation service\n", id); return -1; } else if (r > 0) break; if (wait++ > 1) fprintf(stderr, "client %d still waiting on instrumentation service to be ready\n", id); sleep(1); } printf("client %d found instrumentation service\n", id); txn = create_transaction(0, binder, cookie, 0, NULL, sizeof(inst_buf_t) + data_SZ, NULL, 0); if (!txn) { fprintf(stderr, "client %d failed to prepare transaction buffer\n", id); return -1; } bwr.write_buffer = (unsigned long)txn; bwr.read_buffer = (unsigned long)rbuf; inst = (inst_buf_t *)txn->tdata.data.ptr.buffer; INST_INIT(inst); ibuf = malloc((iterations + 1) * sizeof(inst_entry_t) * INST_MAX_ENTRIES); if (!ibuf) fprintf(stderr, "client %d failed to allocate instrumentation buffer\n", id); p = ibuf; n = iterations + 1; while (n-- > 0) { INST_BEGIN(inst); retries = 2; bwr.write_size = sizeof(*txn); bwr.write_consumed = 0; bwr.read_size = sizeof(rbuf); bwr.read_consumed = 0; INST_ENTRY(inst, "C_SEND"); ioctl_write++; wait_reply: ioctl_read++; r = ioctl(fd, BINDER_WRITE_READ, &bwr); if (r < 0) { fprintf(stderr, "client %d failed ioctl\n", id); return r; } INST_RECORD(©); r = client_parse_command(id, rbuf, bwr.read_consumed, &inst_reply); if (r < 0) return r; if (!inst_reply) { //hexdump(rbuf, bwr.read_consumed); if (retries-- > 0) { bwr.write_size = 0; bwr.read_consumed = 0; goto wait_reply; } else { fprintf(stderr, "client %d failed to receive reply\n", id); return -1; } } memcpy(inst, inst_reply, sizeof(*inst)+data_SZ); //acsiidump(inst_reply,sizeof(*inst)+data_SZ); INST_ENTRY_COPY(inst, "C_RECV", ©); INST_END(inst, &p); #if (defined(SIMULATE_FREE_BUFFER) || !defined(INLINE_TRANSACTION_DATA)) if (FREE_BUFFER(fd, inst_reply) < 0) { fprintf(stderr, "client %d: failed to free shared buffer\n", id); return -1; } #endif } if (output_file) { if (clients > 1) { char *p = malloc(strlen(output_file) + 16); if (!p) { fprintf(stderr, "client %d failed to alloc memory for filename\n", id); return -1; } sprintf(p, "%s-%d", output_file, id); output_file = p; } fp = fopen(output_file, "w"); if (!fp) { fprintf(stderr, "client %d failed to open dump file\n", id); return -1; } } else fp = stdout; memset(total_usecs, 0, sizeof(total_usecs)); memset(max,0,sizeof(max)); memset(min,255,sizeof(min)); entry = (inst_entry_t *)ibuf; int i; //acsiidump(ibuf,80); for (n = 0; n < inst->seq; n++) { for (m = 0; m < inst->next_entry; m++) { if (n > 0) { if (m == 0) { if (time_ref == 0) // absolute time ref.tv_sec = ref.tv_usec = 0; else ref = entry->tv; } delta.tv_sec = entry->tv.tv_sec - ref.tv_sec; delta.tv_usec = entry->tv.tv_usec - ref.tv_usec; if (delta.tv_usec < 0) { delta.tv_sec--; delta.tv_usec += 1000000; } record[m] = delta.tv_sec * 1000000 + delta.tv_usec; //fprintf(fp, "%ld.%06ld\t", delta.tv_sec, delta.tv_usec); if (time_ref > 0) { total_usecs[m] += delta.tv_sec * 1000000 + delta.tv_usec; if( m == inst->next_entry -1) { if (min[m] > record[m] ) { for( i = 0; i < inst->next_entry; i++ ) { min[i] = record[i]; } } if (max[m] < record[m] ) { for( i = 0; i < inst->next_entry; i++ ) { max[i] = record[i]; } } } } if (time_ref > 1) // relative to the previous entry ref = entry->tv; } else { //fprintf(fp, "%8s\t", entry->label); if (time_ref > 0) strcpy(labels[m], entry->label); } entry++; } //fprintf(fp, "\n"); } if (fp != stdout) fclose(fp); free(txn); pid =getpid(); printf("client(%d) %d: ioctl read: %u\n",pid,id, ioctl_read); printf("client(%d) %d: ioctl write: %u\n",pid,id, ioctl_write); printf("client(%d) %d: ioctl buffer: %u\n",pid,id, ioctl_buffer); if (time_ref > 0 && iterations > 0) { int pos = 0,minpos=0,maxpos=0; char *buf = malloc(64 * m); char *minbuf = malloc(64 * m); char *maxbuf = malloc(64 * m); if (!buf||!minbuf||!maxbuf) return 1; for (n = 0; n < m; n++) { pos += sprintf(buf + pos, "\t%s: %lld.%02lldus\n", labels[n], (total_usecs[n] / iterations), (total_usecs[n] % iterations) * 100 / iterations); minpos += sprintf(minbuf + minpos, "\t%s: %lldus\n", labels[n], min[n]); maxpos += sprintf(maxbuf + maxpos, "\t%s: %lldus\n", labels[n], max[n]); } printf("client %d: average results:\n%s\n", id, buf); printf("client %d: min results:\n%s\n", id, minbuf); printf("client %d: max results:\n%s\n", id, maxbuf); free(buf); free(minbuf); free(maxbuf); } return 0; }
int server_parse_command(unsigned char *buf, long size, tdata_t **tdata_out, bcmd_txn_t **txn_out) { unsigned char *p, *ep; unsigned int cmd; tdata_t *tdata = NULL; bcmd_txn_t *txn = NULL; #ifdef INLINE_TRANSACTION_DATA unsigned long buffer_size; #endif p = buf; ep = p + size; while (p < ep) { cmd = *(unsigned int *)p; p += sizeof(cmd); switch (cmd) { case BR_NOOP: case BR_TRANSACTION_COMPLETE: break; case BR_INCREFS: case BR_ACQUIRE: case BR_RELEASE: case BR_DECREFS: fprintf(stderr, "server received unexpected ref_cmd command\n"); if (p + 2 * sizeof(cmd) > ep) { fprintf(stderr, "server not enough ref_cmd data\n"); return -1; } p += 2 * sizeof(cmd); break; case BR_TRANSACTION: if (p + sizeof(*tdata) > ep) { fprintf(stderr, "server not enough transaction data\n"); return -1; } tdata = (tdata_t *)p; p += sizeof(*tdata); //if (tdata->data_size != sizeof(inst_buf_t)) { // fprintf(stderr, "server data size in transaction is incorrect\n"); // return -1; //} #ifdef INLINE_TRANSACTION_DATA buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size); if (p + buffer_size > ep) { fprintf(stderr, "server not enough transaction data buffer\n"); return -1; } txn = (bcmd_txn_t *)(p - sizeof(*tdata) - sizeof(cmd)); txn->cmd = BC_REPLY; p += buffer_size; #else txn = create_transaction(1, NULL, NULL, 0, (unsigned char *)tdata->data.ptr.buffer, tdata->data_size, (unsigned char *)tdata->data.ptr.offsets, tdata->offsets_size); if (!txn) { fprintf(stderr, "server failed to create reply buffer\n"); return -1; } #endif goto expected_out; case BR_REPLY: fprintf(stderr, "server received unexpected reply command\n"); if (p + sizeof(*tdata) > ep) { fprintf(stderr, "server not enough transaction data\n"); return -1; } tdata = (tdata_t *)p; p += sizeof(*tdata); #ifdef INLINE_TRANSACTION_DATA buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size); if (p + buffer_size > ep) { fprintf(stderr, "server not enough transaction data buffer\n"); return -1; } p += buffer_size; #endif break; case BR_DEAD_BINDER: fprintf(stderr, "server received unexpected dead_binder command\n"); if (p + sizeof(cmd) > ep) { fprintf(stderr, "server not enough dead binder data\n"); return -1; } p += sizeof(cmd); break; case BR_FAILED_REPLY: fprintf(stderr, "server received unexpected failed_reply command\n"); return -1; case BR_DEAD_REPLY: fprintf(stderr, "server received unexpected dead_reply command\n"); return -1; default: fprintf(stderr, "server received unknown command\n"); return -1; } } expected_out: *tdata_out = tdata; *txn_out = txn; return (p - buf); }
int add_service(int fd, void *binder, void *cookie, uint16_t *name, int len) { unsigned char buf[1024], *p; obj_t *obj; size_t *offsets; bcmd_txn_t *txn; tdata_t *tdata; int r; p = buf; // strict_policy *(uint32_t *)p = 0; p += 4; // svcmgr_id *(uint32_t *)p = sizeof(svcmgr_id) / 2; p += 4; memcpy(p, svcmgr_id, sizeof(svcmgr_id)); p += sizeof(svcmgr_id); *(uint16_t *)p = 0; p += 2; p = (unsigned char *)ALIGN((unsigned long)p); // name *(uint32_t *)p = len; p += 4; memcpy(p, name, len * 2); p += len * 2; *(uint16_t *)p = 0; p += 2; p = (unsigned char *)ALIGN((unsigned long)p); // flat_binder_obj obj = (obj_t *)p; obj->type = BINDER_TYPE_BINDER; obj->flags = 0; obj->binder = binder; obj->cookie = cookie; p = (unsigned char *)(obj + 1); // offsets offsets = (size_t *)p; *offsets = (unsigned char *)obj - buf; txn = create_transaction(0, NULL, NULL, 3, buf, p - buf, (unsigned char *)offsets, 4); if (!txn) return -1; r = simple_transact(fd, txn, &tdata, buf, sizeof(buf)); if (r < 0) return r; if (tdata->data_size != 4 || *(unsigned int *)tdata->data.ptr.buffer) { fprintf(stderr, "server invalid reply data received\n"); return -1; } #if (defined(SIMULATE_FREE_BUFFER) || !defined(INLINE_TRANSACTION_DATA)) if (FREE_BUFFER(fd, (void *)tdata->data.ptr.buffer) < 0) { fprintf(stderr, "failed to free shared buffer\n"); return -1; } #endif free(txn); return 0; }
session::transaction_type session::start_transaction() { auto tx = create_transaction(); tx.start(); return tx; }