void Request::process( int attempt ) { init(); int op = _m.operation(); assert( op > dbMsg ); if ( op == dbKillCursors ) { cursorCache.gotKillCursors( _m ); return; } log(3) << "Request::process ns: " << getns() << " msg id:" << (int)(_m.header()->id) << " attempt: " << attempt << endl; Strategy * s = SINGLE; _counter = &opsNonSharded; _d.markSet(); if ( _chunkManager ) { s = SHARDED; _counter = &opsSharded; } bool iscmd = false; if ( op == dbQuery ) { iscmd = isCommand(); try { s->queryOp( *this ); } catch ( StaleConfigException& staleConfig ) { log() << staleConfig.what() << " attempt: " << attempt << endl; uassert( 10195 , "too many attempts to update config, failing" , attempt < 5 ); ShardConnection::checkMyConnectionVersions( getns() ); if (!staleConfig.justConnection() ) sleepsecs( attempt ); reset( ! staleConfig.justConnection() ); _d.markReset(); process( attempt + 1 ); return; } } else if ( op == dbGetMore ) { s->getMore( *this ); } else { char cl[256]; nsToDatabase(getns(), cl); uassert(15845, "unauthorized", _clientInfo->getAuthenticationInfo()->isAuthorized(cl)); s->writeOp( op, *this ); } globalOpCounters.gotOp( op , iscmd ); _counter->gotOp( op , iscmd ); }
void Request::process( int attempt ) { init(); int op = _m.operation(); verify( op > dbMsg ); int msgId = (int)(_m.header()->id); Timer t; LOG(3) << "Request::process begin ns: " << getns() << " msg id: " << msgId << " op: " << op << " attempt: " << attempt << endl; _d.markSet(); bool iscmd = false; if ( op == dbKillCursors ) { cursorCache.gotKillCursors( _m ); globalOpCounters.gotOp( op , iscmd ); } else if ( op == dbQuery ) { NamespaceString nss(getns()); iscmd = nss.isCommand() || nss.isSpecialCommand(); if (iscmd) { int n = _d.getQueryNToReturn(); uassert( 16978, str::stream() << "bad numberToReturn (" << n << ") for $cmd type ns - can only be 1 or -1", n == 1 || n == -1 ); STRATEGY->clientCommandOp(*this); } else { STRATEGY->queryOp( *this ); } globalOpCounters.gotOp( op , iscmd ); } else if ( op == dbGetMore ) { STRATEGY->getMore( *this ); globalOpCounters.gotOp( op , iscmd ); } else { STRATEGY->writeOp( op, *this ); // globalOpCounters are handled by write commands. } LOG(3) << "Request::process end ns: " << getns() << " msg id: " << msgId << " op: " << op << " attempt: " << attempt << " " << t.millis() << "ms" << endl; }
void Request::process( int attempt ) { init(); int op = _m.operation(); verify( op > dbMsg ); if ( op == dbKillCursors ) { cursorCache.gotKillCursors( _m ); return; } int msgId = (int)(_m.header()->id); Timer t; LOG(3) << "Request::process begin ns: " << getns() << " msg id: " << msgId << " op: " << op << " attempt: " << attempt << endl; Strategy * s = SHARDED; _counter = &opsNonSharded; _d.markSet(); bool iscmd = false; if ( op == dbQuery ) { iscmd = isCommand(); if (iscmd) { SINGLE->queryOp(*this); } else { s->queryOp( *this ); } } else if ( op == dbGetMore ) { s->getMore( *this ); } else { s->writeOp( op, *this ); } LOG(3) << "Request::process end ns: " << getns() << " msg id: " << msgId << " op: " << op << " attempt: " << attempt << " " << t.millis() << "ms" << endl; globalOpCounters.gotOp( op , iscmd ); _counter->gotOp( op , iscmd ); }
// ---------------------------------------------------------------------------- // the callback used by G-WAN to generate the data pushed to clients // ---------------------------------------------------------------------------- static int make_fn(char *argv[]) { // of course, instead of using pseudo-random data, one could agregate data // from a database back-end server, or from another application server, or // use other client requests that bring new data (publishers) but we could // also calculate complex things here (scoring, simulations, etc.) // // the point is to do things once (here) for all the clients we feed // instead of doing it repeatedly each time a client gets data // if(!s_data) { s_data = (float*)calloc(1, sizeof(float) * CELLS); if(!s_data) return 0; } static int once = 0; if(!once) { once = 1; sw_init(&rnd, (u32)getns()); // (period: 1 << 158) } int i = 0; while(i < CELLS) s_data[i++] = (sw_rand(&rnd) % 100000) / 100.; return 1; }
void Request::checkAuth( Auth::Level levelNeeded ) const { char cl[256]; nsToDatabase(getns(), cl); uassert( 15845 , str::stream() << "unauthorized for db:" << cl << " level: " << levelNeeded , _clientInfo->getAuthenticationInfo()->isAuthorizedForLevel(cl,levelNeeded) ); }
shared_ptr<ChunkManager> ChunkManager::reload(bool force) const { const NamespaceString nss(_ns); auto status = grid.catalogCache()->getDatabase(nss.db().toString()); shared_ptr<DBConfig> config = uassertStatusOK(status); return config->getChunkManager(getns(), force); }
int dialfactotum(void) { int fd; struct sockaddr_un su; char *name; name = smprint("%s/factotum", getns()); if(name == nil || access(name, 0) < 0) return -1; memset(&su, 0, sizeof su); su.sun_family = AF_UNIX; if(strlen(name)+1 > sizeof su.sun_path){ werrstr("socket name too long"); return -1; } strcpy(su.sun_path, name); if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){ werrstr("socket: %r"); return -1; } if(connect(fd, (struct sockaddr*)&su, sizeof su) < 0){ werrstr("connect %s: %r", name); close(fd); return -1; } return lfdfd(fd); }
void remoteside(void *v) { int srv_to_net[2]; int net_to_srv[2]; char *addr; int srvfd; if(ns == nil) ns = getns(); addr = smprint("unix!%s/%s", ns, srv); if(addr == nil) fatal("%r"); if(debug) fprint(dfd, "remoteside starting %s\n", addr); srvfd = dial(addr, 0, 0, 0); if(srvfd < 0) fatal("dial %s: %r", addr); if(debug) fprint(dfd, "remoteside dial %s succeeded\n", addr); fcntl(srvfd, F_SETFL, FD_CLOEXEC); /* threads to shuffle messages each way */ srv_to_net[0] = srvfd; srv_to_net[1] = netfd[1]; proccreate(shuffle, srv_to_net, Stack); net_to_srv[0] = netfd[0]; net_to_srv[1] = srvfd; shuffle(net_to_srv); threadexitsall(0); }
inline double get_worktime_percentage(){ #if MC_DPI_TICKS_WAIT == 1 ticks totalticks = getticks() - startticks; return (double) workticks / (double) totalticks * 100.0; #else unsigned long totalns = getns() - startns; return (double) workns / (double) totalns * 100.0; #endif }
void Request::reset( bool reload ) { if ( _m.operation() == dbKillCursors ) { return; } _config = grid.getDBConfig( getns() ); if ( reload ) uassert( 10192 , "db config reload failed!" , _config->reload() ); if ( _config->isSharded( getns() ) ) { _chunkManager = _config->getChunkManager( getns() , reload ); uassert( 10193 , (string)"no shard info for: " + getns() , _chunkManager ); } else { _chunkManager.reset(); } _m.header()->id = _id; }
void ChunkManager::calcInitSplitsAndShards(OperationContext* txn, const ShardId& primaryShardId, const vector<BSONObj>* initPoints, const set<ShardId>* initShardIds, vector<BSONObj>* splitPoints, vector<ShardId>* shardIds) const { verify(_chunkMap.size() == 0); Chunk c(this, _keyPattern.getKeyPattern().globalMin(), _keyPattern.getKeyPattern().globalMax(), primaryShardId); if (!initPoints || !initPoints->size()) { // discover split points const auto primaryShard = grid.shardRegistry()->getShard(txn, primaryShardId); auto targetStatus = primaryShard->getTargeter()->findHost({ReadPreference::PrimaryPreferred, TagSet{}}); uassertStatusOK(targetStatus); NamespaceString nss(getns()); auto result = grid.shardRegistry()->runCommand( txn, targetStatus.getValue(), nss.db().toString(), BSON("count" << nss.coll())); long long numObjects = 0; uassertStatusOK(result.getStatus()); uassertStatusOK(Command::getStatusFromCommandResult(result.getValue())); uassertStatusOK(bsonExtractIntegerField(result.getValue(), "n", &numObjects)); if (numObjects > 0) c.pickSplitVector(txn, *splitPoints, Chunk::MaxChunkSize); // since docs already exists, must use primary shard shardIds->push_back(primaryShardId); } else { // make sure points are unique and ordered set<BSONObj> orderedPts; for (unsigned i = 0; i < initPoints->size(); ++i) { BSONObj pt = (*initPoints)[i]; orderedPts.insert(pt); } for (set<BSONObj>::iterator it = orderedPts.begin(); it != orderedPts.end(); ++it) { splitPoints->push_back(*it); } if (!initShardIds || !initShardIds->size()) { // If not specified, only use the primary shard (note that it's not safe for mongos // to put initial chunks on other shards without the primary mongod knowing). shardIds->push_back(primaryShardId); } else { std::copy(initShardIds->begin(), initShardIds->end(), std::back_inserter(*shardIds)); } } }
inline void reset_worktime_percentage_real(bool force = false){ if(reset || force){ #if MC_DPI_TICKS_WAIT == 1 workticks = 0; startticks = getticks(); #else workns = 0; startns = getns(); #endif reset = 0; } }
// Deprecated, will move to the strategy itself Shard Request::primaryShard() const { assert( _didInit ); if ( _chunkManager ) { if ( _chunkManager->numChunks() > 1 ) throw UserException( 8060 , "can't call primaryShard on a sharded collection" ); return _chunkManager->findChunk( _chunkManager->getShardKey().globalMin() )->getShard(); } Shard s = _config->getShard( getns() ); uassert( 10194 , "can't call primaryShard on a sharded collection!" , s.ok() ); return s; }
// Deprecated, will move to the strategy itself void Request::reset() { _m.header()->id = _id; _clientInfo->clearRequestInfo(); if ( !_d.messageShouldHaveNs()) { return; } uassert( 13644 , "can't use 'local' database through mongos" , ! str::startsWith( getns() , "local." ) ); grid.getDBConfig( getns() ); }
void Request::reset( bool reload ) { if ( _m.operation() == dbKillCursors ) { return; } uassert( 13644 , "can't use 'local' database through mongos" , ! str::startsWith( getns() , "local." ) ); _config = grid.getDBConfig( getns() ); if ( reload ) uassert( 10192 , "db config reload failed!" , _config->reload() ); if ( _config->isSharded( getns() ) ) { _chunkManager = _config->getChunkManager( getns() , reload ); uassert( 10193 , (string)"no shard info for: " + getns() , _chunkManager ); } else { _chunkManager.reset(); } _m.header()->id = _id; }
void ChunkManager::calcInitSplitsAndShards( const ShardId& primaryShardId, const vector<BSONObj>* initPoints, const set<ShardId>* initShardIds, vector<BSONObj>* splitPoints, vector<ShardId>* shardIds ) const { verify( _chunkMap.size() == 0 ); unsigned long long numObjects = 0; Chunk c(this, _keyPattern.getKeyPattern().globalMin(), _keyPattern.getKeyPattern().globalMax(), primaryShardId); if ( !initPoints || !initPoints->size() ) { // discover split points { const auto& primaryShard = grid.shardRegistry()->findIfExists(primaryShardId); // get stats to see if there is any data ScopedDbConnection shardConn(primaryShard->getConnString()); numObjects = shardConn->count( getns() ); shardConn.done(); } if ( numObjects > 0 ) c.pickSplitVector( *splitPoints , Chunk::MaxChunkSize ); // since docs alread exists, must use primary shard shardIds->push_back(primaryShardId); } else { // make sure points are unique and ordered set<BSONObj> orderedPts; for ( unsigned i = 0; i < initPoints->size(); ++i ) { BSONObj pt = (*initPoints)[i]; orderedPts.insert( pt ); } for ( set<BSONObj>::iterator it = orderedPts.begin(); it != orderedPts.end(); ++it ) { splitPoints->push_back( *it ); } if ( !initShardIds || !initShardIds->size() ) { // If not specified, only use the primary shard (note that it's not safe for mongos // to put initial chunks on other shards without the primary mongod knowing). shardIds->push_back(primaryShardId); } else { std::copy(initShardIds->begin() , initShardIds->end() , std::back_inserter(*shardIds)); } } }
void Request::reply( Message & response , const string& fromServer ) { verify( _didInit ); long long cursor =response.header()->getCursor(); if ( cursor ) { if ( fromServer.size() ) { cursorCache.storeRef(fromServer, cursor, getns()); } else { // probably a getMore // make sure we have a ref for this verify( cursorCache.getRef( cursor ).size() ); } } _p->reply( _m , response , _id ); }
void Request::process( int attempt ) { init(); int op = _m.operation(); assert( op > dbMsg ); if ( op == dbKillCursors ) { cursorCache.gotKillCursors( _m ); return; } MONGO_LOG(3) << "Request::process ns: " << getns() << " msg id:" << (int)(_m.header()->id) << " attempt: " << attempt << endl; Strategy * s = SHARDED; _counter = &opsNonSharded; _d.markSet(); bool iscmd = false; if ( op == dbQuery ) { try { iscmd = isCommand(); s->queryOp( *this ); } catch ( RecvStaleConfigException& stale ) { _d.markReset(); log( attempt == 0 ) << "got RecvStaleConfigException at top level: " << stale.toString() << " attempt: " << attempt << endl; massert( 16062 , "too many attemps to handle RecvStaleConfigException at top level" , attempt <= 5 ); process( attempt + 1 ); return; } } else if ( op == dbGetMore ) { checkAuth( Auth::READ ); // this is important so someone can't steal a cursor s->getMore( *this ); } else { checkAuth( Auth::WRITE ); s->writeOp( op, *this ); } globalOpCounters.gotOp( op , iscmd ); _counter->gotOp( op , iscmd ); }
static Stab *findstab(Stab *st, char *pkg) { Stab *s; if (!pkg) { if (!st->name) return st; else return NULL; } s = getns(file, pkg); if (!s) { s = mkstab(0); s->name = strdup(pkg); putns(file, s); } return s; }
// ---------------------------------------------------------------------------- // imported functions: // get_reply(): get a pointer on the 'reply' dynamic buffer from the server // get_env(): get connection's 'environment' variables from the server // xbuf_cat(): like strcat(), but it works in the specified dynamic buffer // gif_build(): build an in-memory GIF image from a bitmap and palette // ---------------------------------------------------------------------------- int main(int argc, char *argv[]) { // ------------------------------------------------------------------------- // build the top of our HTML page // ------------------------------------------------------------------------- static char top[]= "<!DOCTYPE HTML>" "<html lang=\"en\"><head><title>Captcha</title><meta http-equiv" "=\"Content-Type\" content=\"text/html; charset=utf-8\">" "<link href=\"/imgs/style.css\" rel=\"stylesheet\" type=\"text/css\">" "</head><body style=\"margin:0 16px;\"><br><h2>Captcha for Humans</h2>" "<p>Please enter the SUM of all the GREEN FIGURES (not letters) below " "(that's twice the same image, just with a different HTML background " "- the Data-URI-inlined GIF background is transparent):</p><br>\r\n"; xbuf_t *reply = get_reply(argv); xbuf_ncat(reply, top, sizeof(top) - 1); // ------------------------------------------------------------------------- // allocate memory for a raw bitmap // ------------------------------------------------------------------------- const int w = BMP_WIDTH, h = BMP_HEIGHT, wXh = w * h; u8 *bmp = (u8*)calloc(CHAR_WIDTH * w, h); if(!bmp) return 503; // service unavailable // ------------------------------------------------------------------------- // render the captcha in our bitmap // ------------------------------------------------------------------------- u32 seed = (u32)getns(); prnd_t rnd; // pseudo-random generator (period: 1 << 158) sw_init(&rnd, seed); // EPOCH time in nano-seconds // structure needed by G-WAN's frame buffer routines like dr_text() bmp_t img ={ .bmp = bmp, .p = bmp, .bbp = 8, .pen = 1, .bgd = 0, .rect = {0,0, w,h}, .flags = 0, .w = w, .h = h, .x = 0, .y = 0 }; u32 sum = captcha(&img, &rnd); // ------------------------------------------------------------------------- // build the GIF image, gif_build(0:transparent color index, 0: no comment) // ------------------------------------------------------------------------- u8 pal[] = { 255, 255, 255, 223, 255, 191, 132, 164, 100, 0, 0, 0 }; const int nbcolors = (sizeof(pal) / sizeof(u8)) / 3; // RGB values u8 *gif = (u8*)malloc(CHAR_WIDTH * wXh); if(!gif) { free(bmp); return 503; } // service unavailable int gln = gif_build(gif, bmp, w, h, pal, nbcolors, 0, 0); // ------------------------------------------------------------------------- // store the base64 encoded GIF in the 'reply' buffer // ------------------------------------------------------------------------- if(gln > 0) // (gln == -1) if gif_build() failed { // a real captcha test would only display the first of those two views: // (they are shown side-by-side to visualize the background trick) xbuf_cat(reply, "<table><tr>\r\n" "<td style=\"background:#dfffbf;\">\r\n"); u32 img_pos = reply->len; xbuf_xcat(reply, "<img src=\"data:image/gif;base64,%*B\" alt=\"A tree\" " "width=\"%d\" height=\"%d\" /></td>\r\n", gln, gif, w + w, h + h); // scale picture xbuf_xcat(reply, "<td style=\"background:#84a464;\">%.*s</tr>\r\n</table>\n\r", reply->len - img_pos, reply->ptr + img_pos); } free(gif); free(bmp); // ------------------------------------------------------------------------- // close our HTML page // ------------------------------------------------------------------------- xbuf_xcat(reply, "<br>The two sums are: <b>%u</b> and <b>%u</b>... " "for the same Captcha image!<br><br>" "By just changing the <b>HTML background color</b> (mouse cursor " "hovering, previous state or input or shared secret) used for " "the transparent GIF Captcha image we can make something simple " "for humans become difficult or even completely impossible " "for robots.<br><br>" "HTML and GIF are served with one single request: the picture" " is generated on-the-fly and embedded into the HTML code by " " using the base64 encoding (look at the HTML source code)." "<br></body></html>", (sum & 0xffff0000) >> 16, sum & 0x0000ffff); return 200; // return an HTTP code (200:'OK') }
// Deprecated, will move to the strategy itself void Request::reset() { if ( _m.operation() == dbKillCursors ) { return; } uassert( 13644 , "can't use 'local' database through mongos" , ! str::startsWith( getns() , "local." ) ); // TODO: Deprecated, keeping to preserve codepath for now const string nsStr (getns()); // use in functions taking string rather than char* _config = grid.getDBConfig( nsStr ); // TODO: In general, throwing an exception when the cm doesn't exist is really annoying if ( _config->isSharded( nsStr ) ) { _chunkManager = _config->getChunkManagerIfExists( nsStr ); } else { _chunkManager.reset(); } _m.header()->id = _id; _clientInfo->clearCurrentShards(); }
int main(int argc,char * argv[]){ if(argc<3){ printf("Usage: ./wordcnt0 [target plaintext file] [keyword file]\n"); return 0; } int line, i; char word[MAXLEN]; struct output * result; FILE * keyFile; struct timespec start,end; //set the target file name target = strdup(argv[1]); clock_gettime(CLOCK_REALTIME,&start);//record the starting time keyFile = fopen(argv[2],"r"); fscanf(keyFile,"%d",&line); // read in the number of keywords result = (struct output *) malloc(line * sizeof(struct output)); //create the pipe to do multiprocessing int task_pfd[2]; //pipe descriptor int data_pfd[2]; //pipe descriptor pipe(task_pfd); pipe(data_pfd); int word_count = 0; int numOfChild = std::min(line,10); pid_t pids[numOfChild]; //child processes with number of line signal(SIGINT,sigint_handler); signal(SIGUSR1,sigusr1_handler); for (i=0;i<numOfChild;i++){ pids[i] = fork(); if(pids[i]>0){//this is the parent process // printf("Here is the parent process\n"); fscanf(keyFile,"%s",word); write(task_pfd[1],word,sizeof(word)); kill(pids[i],SIGUSR1); continue; }else if(pids[i]==0){ printf("Child %d will start to work\n",getpid()); while(true){ while(!has_arrived && !finished){ sleep(1); } if(finished){ free(result); exit(word_count); } has_arrived = false; read(task_pfd[0],word,sizeof(word)); word_count++; // printf(".............Here read in another task to search with %d word_count\n",word_count); result[0].cid = (int)getpid(); result[0].count = search(word); strcpy(result[0].keyword,word); write(data_pfd[1],&result[0],sizeof(struct output)); } }else{ printf("fork: error no = %s\n",strerror(errno)); exit(-1); } } int remaining = line-numOfChild; for(i=0;i<line;i++){ read(data_pfd[0],&result[i],sizeof(result[i])); if(remaining>0){ fscanf(keyFile,"%s",word); write(task_pfd[1],word,sizeof(word)); kill(result[i].cid,SIGUSR1); remaining--; } } fclose(keyFile); int status=0; for (i=0;i<numOfChild;i++){ kill(pids[i],SIGINT); waitpid(pids[i],&status,0); printf("Child process (%d) exited and it searched %d words \n",pids[i],WEXITSTATUS(status)); } clock_gettime(CLOCK_REALTIME,&end);//record ending time printf("\n\n"); for(i=0;i<line;++i){ printf("%s : %d\n",result[i].keyword,result[i].count); } printf("\nTotal elapsed time: %.2lf ms\n",getns(start,end)/1000000.0); free(target); free(result); return 0; }
bool Request::isCommand() const { int x = _d.getQueryNToReturn(); return ( x == 1 || x == -1 ) && strstr( getns() , ".$cmd" ); }
void convert(FILE *D_in, FILE *D_out) { /* Buffers utilizados por la rutina */ float *fvertbuf; // Valores en coma flotante de los v‚rtices float *nbuf; // Buffer de normales (averaged) long *vertbuf; // Vertices en modo fixed WORD *facebuf, *list; // Lista de caras... ; Lista de normales que converge... char *filebuf; // Buffer del fichero cargado en memoria... /* Datos varios */ float ftemp; long ltemp; short itemp; long len; char number[255]; long i=0, j=0, k=0, np=0, p=0; BYTE col=0; float scalar; char *inmem = { "\n\rInsuficient memory!!!\n" }; char *errorv = { "\n\rError in Vertex %d:%c" }; char *errorf = { "\n\rError in Face %d:%c ; Value: %s" }; char *errord = { "\n\n\r Hey men, where are the %ss definitions?" }; char *abort = { "\n\n\r Too many errors!\n\r Aborted.\n" }; long error = 0; dot3d normal; float max, opt; len = filesize(D_in); // Read the file into memory if((filebuf = (char *) malloc(len)) == NULL) { cprintf("\r\nERROR: Insuficient memory for file buffer (¨¨Comorllll??)\n"); exit(0); } fread(filebuf, 1, (size_t)len, D_in); // Initalize the struct shape.ident = 0x3DEF; shape.atrib = 0; // Extract the number of vertx if((pos = finddata(filebuf, data_table[0])) == NULL) { cprintf("\n\r ERROR: Vertices not defined!"); exit(0); } getns(filebuf, strtemp); shape.numverts = atoi(strtemp); cprintf("\n\r Number of vertices: %d", shape.numverts); // Now with the faces... if((pos = finddata(filebuf, data_table[1])) == NULL) { cprintf("\n\rERROR: Faces not defined!\n"); exit(0); } getns(filebuf, strtemp); shape.numfaces = atoi(strtemp); cprintf("\n\r Number of faces: %d", shape.numfaces); // ¨Maped? getns(filebuf, strtemp); if((strcmp(strtemp, data_table[16])) == NULL) { cprintf("\n\r Mesh Mapped."); shape.atrib |= 1; SizeVertex = 32 / 4; } else { SizeVertex = (32-8) / 4; } // Here is the memory allocation if((vertbuf = (long *) malloc(shape.numverts * (SizeVertex*4))) == NULL) { cprintf(inmem); exit(0); } if((fvertbuf = (float *) malloc(shape.numverts * sizeof(dot3d))) == NULL) { cprintf(inmem); exit(0); } if((facebuf = (WORD *) malloc(shape.numfaces * 14)) == NULL) { cprintf(inmem); exit(0); } if((nbuf = (float *) malloc(shape.numfaces * sizeof(dot3d))) == NULL) { cprintf(inmem); exit(0); } if((list = (WORD *) malloc(shape.numfaces * 2)) == NULL) { cprintf(inmem); exit(0); } /* Proceso de extracci¢n de v‚rtices */ cprintf("\n\r Converting Vertices."); pos = finddata(filebuf, data_table[5]); if(pos == -1) { cprintf(errord, data_table[5]); exit(-1); } for(i = 0; i < shape.numverts; i++) { if(error > MAX_ERRORS) { cprintf(abort); exit(-1); } if((i % 32) == 0) cprintf("."); do { getns(filebuf, strtemp); } while((strcmp(strtemp, data_table[13])) != NULL); getns(filebuf, strtemp); getns(filebuf, strtemp); if((strtemp[0] == 'X')) { getns(filebuf, strtemp); scalar = atof(strtemp); fvertbuf[k++] = scalar * SCALE; vertbuf[j++] = (long)(scalar * 65536 * SCALE); } else { error++; cprintf(errorv, i, 'X'); } getns(filebuf, strtemp); if((strtemp[0] == 'Y')) { getns(filebuf, strtemp); scalar = atof(strtemp); fvertbuf[k++] = scalar * SCALE; vertbuf[j++] = (long)(scalar * 65536 * SCALE); } else { error++; cprintf(errorv, i, 'Y'); } getns(filebuf, strtemp); if((strtemp[0] == 'Z')) { getns(filebuf, strtemp); scalar = atof(strtemp); fvertbuf[k++] = scalar * SCALE; vertbuf[j++] = (long)(scalar * 65536 * SCALE); } else { error++; cprintf(errorv, i, 'Z'); } /* Xchg order of coordinates */ ltemp = -vertbuf[j-2]; vertbuf[j-2] = -vertbuf[j-1]; vertbuf[j-1] = ltemp; ftemp = -fvertbuf[k-2]; fvertbuf[k-2] = -fvertbuf[k-1]; fvertbuf[k-1] = ftemp; /* Take space for the normal's calculation */ j += 3; /* Read texture-map coordinates, if exists... */ if((shape.atrib & 1) != 0) { getns(filebuf, strtemp); if((strtemp[0] == 'U')) { getns(filebuf, strtemp); scalar = atof(strtemp); vertbuf[j++] = (long)(scalar * 65536 * U_TEXT); } else { error++; cprintf(errorv, i, 'U'); } getns(filebuf, strtemp); if((strtemp[0] == 'V')) { getns(filebuf, strtemp); scalar = atof(strtemp); vertbuf[j++] = (long)(scalar * 65536 * V_TEXT); } else { error++; cprintf(errorv, i, 'V'); } } /* In total, every [mapped] vertex is : 3 ³ Dwords for the point 3 ³ Dwords for the VERTEX normal [2 ³ Dwords for the mapping coordinates] ÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ 8 ³ DWORDS (32 bytes) [No map = 24] */ } // Here we start with the polys... cprintf("\n\r Extracting Faces."); pos = finddata(filebuf, data_table[6]); if(pos == -1) { cprintf(errord, data_table[6]); exit(-1); } for(i = 0, np=0; i < shape.numfaces; i++) { if(error > MAX_ERRORS) { cprintf(abort); exit(-1); } j = i * 4; if((i % 32) == 0) cprintf("."); do { getns(filebuf, strtemp); } while((strcmp(strtemp, data_table[11])) != NULL); getns(filebuf, strtemp); getns(filebuf, strtemp); if(strtemp[0] == 'A') { getns(filebuf, strtemp); facebuf[j++] = atoi(strtemp); } else { error++; cprintf(errorf, i, 'A', strtemp); } getns(filebuf, strtemp); if(strtemp[0] == 'B') { getns(filebuf, strtemp); facebuf[j++] = atoi(strtemp); } else { error++; cprintf(errorf, i, 'B', strtemp); } getns(filebuf, strtemp); if(strtemp[0] == 'C') { getns(filebuf, strtemp); facebuf[j++] = atoi(strtemp); } else { error++; cprintf(errorf, i, 'C', strtemp); } itemp = facebuf[j-1]; facebuf[j-1] = facebuf[j-3]; facebuf[j-3] = itemp; GetNormal(facebuf[j-3], facebuf[j-2], facebuf[j-1], fvertbuf, &nbuf[np]); np += 3; /* do { getns(filebuf, strtemp); } while((strcmp(strtemp, data_table[17])) != NULL); getns(filebuf, strtemp); for(col=0;strtemp[col]!= NULL;col++); strtemp[col-1] = NULL; for(col=0;col<=8;col++) { if((strcmp(&strtemp[1], mat_table[col])) == NULL) break; } facebuf[j++] = col; cprintf("\n\r Material: %s; %d", mat_table[col], col); */ facebuf[j++] = 1; } // Average the normal dots that converge cprintf("\n\r Averaging Normals."); for(i = 0; i < shape.numverts; i++) { if((i % 32) == 0) cprintf("."); Average_Dot(facebuf, i, vertbuf, nbuf, list); } // Save the results: cprintf("\n\r Written to disk..."); fwrite((void *)&shape, 1, sizeof(SHPfile), D_out); fwrite(vertbuf, 1, shape.numverts*(SizeVertex*4), D_out); fwrite(facebuf, 1, shape.numfaces*4*2, D_out); cprintf("\n\rConversion finished.\n"); }
int main(int argc, char **argv) { FILE *ifile = stdin; FILE *ofile = stdout; FILE *ffile = NULL; int ret; float alpha, ilines_rate, ilines_rate_avg; int64_t raw_lines = -1; uint64_t report_mask = 0; uint64_t time_last, time_curr, time_delta; uint64_t time_start, time_elapsed; uint64_t ilines_last, ilines_curr, ilines_delta; uint64_t olines; int skipping = 0, tty = 0; char *line = NULL; size_t line_sz = 0; int line_read; int c, spok = 0, aopt = 0, vopt = 0, wopt = 16, Lopt = 0; int nopt_mod = 0, nopt_rem = 0; uint64_t kopt = 0; unsigned char *bopt = NULL, *iopt = NULL, *oopt = NULL; unsigned char *topt = NULL, *sopt = NULL, *popt = NULL; unsigned char *mopt = NULL, *fopt = NULL, *ropt = NULL; while ((c = getopt(argc, argv, "avb:hi:k:f:m:n:o:p:s:r:t:w:L")) != -1) { switch (c) { case 'a': aopt = 1; // open output file in append mode break; case 'k': kopt = strtoull(optarg, NULL, 10); // skip first k lines of input skipping = 1; break; case 'n': // only try the rem'th of every mod lines (one indexed) nopt_rem = atoi(optarg) - 1; optarg = strchr(optarg, '/'); if (optarg != NULL) { nopt_mod = atoi(optarg+1); } skipping = 1; break; case 'w': if (wopt > 1) wopt = atoi(optarg); break; case 'm': mopt = optarg; // table file wopt = 1; // auto break; case 'v': vopt = 1; // verbose break; case 'b': bopt = optarg; // bloom filter file break; case 'f': fopt = optarg; // full filter file break; case 'i': iopt = optarg; // input file break; case 'o': oopt = optarg; // output file break; case 's': sopt = optarg; // salt break; case 'p': popt = optarg; // passphrase break; case 'r': ropt = optarg; // rushwallet break; case 't': topt = optarg; // type of input break; case 'L': Lopt = 1; // lookup output break; case 'h': // show help usage(argv[0]); return 0; case '?': // show error return 1; default: // should never be reached... printf("got option '%c' (%d)\n", c, c); return 1; } } if (optind < argc) { if (optind == 1 && argc == 2) { // older versions of brainflayer had the bloom filter file as a // single optional argument, this keeps compatibility with that bopt = argv[1]; } else { fprintf(stderr, "Invalid arguments:\n"); while (optind < argc) { fprintf(stderr, " '%s'\n", argv[optind++]); } exit(1); } } if (nopt_rem != 0 || nopt_mod != 0) { // note that nopt_rem has had one subtracted at option parsing if (nopt_rem >= nopt_mod) { bail(1, "Invalid '-n' argument, remainder '%d' must be <= modulus '%d'\n", nopt_rem+1, nopt_mod); } else if (nopt_rem < 0) { bail(1, "Invalid '-n' argument, remainder '%d' must be > 0\n", nopt_rem+1); } else if (nopt_mod < 1) { bail(1, "Invalid '-n' argument, modulus '%d' must be > 0\n", nopt_mod); } } if (wopt < 1 || wopt > 28) { bail(1, "Invalid window size '%d' - must be >= 1 and <= 28\n", wopt); } else { // very rough sanity check of window size struct sysinfo info; sysinfo(&info); uint64_t sysram = info.mem_unit * info.totalram; if (3584LLU*(1<<wopt) > sysram) { bail(1, "Not enough ram for requested window size '%d'\n", wopt); } } if (topt != NULL) { if (strcmp(topt, "str") == 0) { input2hash160 = &pass2hash160; } else if (strcmp(topt, "hex") == 0) { input2hash160 = &hexpass2hash160; } else if (strcmp(topt, "priv") == 0) { input2hash160 = &hexpriv2hash160; } else if (strcmp(topt, "warp") == 0) { spok = 1; input2hash160 = popt ? &warpsalt2hash160 : &warppass2hash160; } else if (strcmp(topt, "bwio") == 0) { spok = 1; input2hash160 = popt ? &bwiosalt2hash160 : &bwiopass2hash160; } else if (strcmp(topt, "bv2") == 0) { spok = 1; input2hash160 = popt ? &brainv2salt2hash160 : &brainv2pass2hash160; } else if (strcmp(topt, "rush") == 0) { input2hash160 = &rush2hash160; } else { bail(1, "Unknown input type '%s'.\n", topt); } } else { topt = "str"; input2hash160 = &pass2hash160; } if (spok) { if (sopt && popt) { bail(1, "Cannot specify both a salt and a passphrase\n"); } if (popt) { kdfpass = popt; kdfpass_sz = strlen(popt); } else { if (sopt) { kdfsalt = sopt; kdfsalt_sz = strlen(kdfsalt); } else { kdfsalt = chkmalloc(0); kdfsalt_sz = 0; } } } else { if (popt) { bail(1, "Specifying a passphrase not supported with input type '%s'\n", topt); } else if (sopt) { bail(1, "Specifying a salt not supported with this input type '%s'\n", topt); } } if (ropt) { if (input2hash160 != &rush2hash160) { bail(1, "Specifying a url fragment only supported with input type 'rush'\n"); } kdfsalt = ropt; kdfsalt_sz = strlen(kdfsalt) - sizeof(rushchk)*2; if (kdfsalt[kdfsalt_sz-1] != '!') { bail(1, "Invalid rushwallet url fragment '%s'\n", kdfsalt); } unhex(kdfsalt+kdfsalt_sz, sizeof(rushchk)*2, rushchk, sizeof(rushchk)); kdfsalt[kdfsalt_sz] = '\0'; } else if (input2hash160 == &rush2hash160) { bail(1, "The '-r' option is required for rushwallet.\n"); } if (bopt) { if (Lopt) { bail(1, "The '-L' option cannot be used with a bloom filter\n"); } if ((ret = mmapf(&bloom_mmapf, bopt, BLOOM_SIZE, MMAPF_RNDRD)) != MMAPF_OKAY) { bail(1, "failed to open bloom filter '%s': %s\n", bopt, mmapf_strerror(ret)); } else if (bloom_mmapf.mem == NULL) { bail(1, "got NULL pointer trying to set up bloom filter\n"); } bloom = bloom_mmapf.mem; } if (fopt) { if (!bopt) { bail(1, "The '-f' option must be used with a bloom filter\n"); } if ((ffile = fopen(fopt, "r")) == NULL) { bail(1, "failed to open '%s' for reading: %s\n", fopt, strerror(errno)); } } if (iopt) { if ((ifile = fopen(iopt, "r")) == NULL) { bail(1, "failed to open '%s' for reading: %s\n", iopt, strerror(errno)); } // increases readahead window, don't really care if it fails posix_fadvise(fileno(ifile), 0, 0, POSIX_FADV_SEQUENTIAL); } if (oopt && (ofile = fopen(oopt, (aopt ? "a" : "w"))) == NULL) { bail(1, "failed to open '%s' for writing: %s\n", oopt, strerror(errno)); } /* line buffer output */ setvbuf(ofile, NULL, _IOLBF, 0); /* line buffer stderr */ setvbuf(stderr, NULL, _IOLBF, 0); if (vopt && ofile == stdout && isatty(fileno(stdout))) { tty = 1; } brainflayer_init_globals(); if (secp256k1_ec_pubkey_precomp_table(wopt, mopt) != 0) { bail(1, "failed to initialize precomputed table\n"); } if (vopt) { /* initialize timing data */ time_start = time_last = getns(); olines = ilines_last = ilines_curr = 0; ilines_rate_avg = -1; alpha = 0.500; } else { time_start = time_last = 0; // prevent compiler warning about uninitialized use } for (;;) { if ((line_read = getline(&line, &line_sz, ifile)-1) > -1) { if (skipping) { ++raw_lines; if (kopt && raw_lines < kopt) { continue; } if (nopt_mod && raw_lines % nopt_mod != nopt_rem) { continue; } } line[line_read] = 0; if (input2hash160(line, line_read) == 0) { if (bloom) { if (bloom_chk_hash160(bloom, hash160_uncmp.ul)) { if (!fopt || hsearchf(ffile, &hash160_uncmp)) { if (tty) { fprintf(ofile, "\033[0K"); } fprintresult(ofile, &hash160_uncmp, 'u', topt, line); ++olines; } } if (bloom_chk_hash160(bloom, hash160_compr.ul)) { if (!fopt || hsearchf(ffile, &hash160_compr)) { if (tty) { fprintf(ofile, "\033[0K"); } fprintresult(ofile, &hash160_compr, 'c', topt, line); ++olines; } } } else if (Lopt) { fprintlookup(ofile, &hash160_uncmp, &hash160_compr, priv256, topt, line); } else { fprintresult(ofile, &hash160_uncmp, 'u', topt, line); fprintresult(ofile, &hash160_compr, 'c', topt, line); } } } else { if (!vopt) break; } if (vopt) { ++ilines_curr; if (line_read < 0 || (ilines_curr & report_mask) == 0) { time_curr = getns(); time_delta = time_curr - time_last; time_elapsed = time_curr - time_start; time_last = time_curr; ilines_delta = ilines_curr - ilines_last; ilines_last = ilines_curr; ilines_rate = (ilines_delta * 1.0e9) / (time_delta * 1.0); if (line_read < 0) { /* report overall average on last status update */ ilines_rate_avg = (--ilines_curr * 1.0e9) / (time_elapsed * 1.0); } else if (ilines_rate_avg < 0) { ilines_rate_avg = ilines_rate; /* target reporting frequency to about once every five seconds */ } else if (time_delta < 2500000000) { report_mask = (report_mask << 1) | 1; ilines_rate_avg = ilines_rate; /* reset EMA */ } else if (time_delta > 10000000000) { report_mask >>= 1; ilines_rate_avg = ilines_rate; /* reset EMA */ } else { /* exponetial moving average */ ilines_rate_avg = alpha * ilines_rate + (1 - alpha) * ilines_rate_avg; } fprintf(stderr, "\033[0G\033[2K" " rate: %9.2f p/s" " found: %5zu/%-10zu" " elapsed: %8.3f s" "\033[0G", ilines_rate_avg, olines, ilines_curr, time_elapsed / 1.0e9 ); if (line_read < 0) { fprintf(stderr, "\n"); break; } else { fflush(stderr); } }
shared_ptr<ChunkManager> ChunkManager::reload(OperationContext* txn, bool force) const { const NamespaceString nss(_ns); auto config = uassertStatusOK(grid.catalogCache()->getDatabase(txn, nss.db().toString())); return config->getChunkManagerIfExists(txn, getns(), force); }