int main (int argc, char*argv[]) { static int n, /* # de nodos */ token, event, r, i; static char fa_name[5]; if (argc != 2) { printf("Uso incorreto. Sintaxe: '%s <# de nodos>'\n",argv[0]); exit(1); } n=atoi(argv[1]); smpl(0,"um exemplo de simulacao"); reset(); stream(1); /* INICIALIZACAO */ nodo = (tnodo*) malloc(sizeof(tnodo)*n); for (i=0;i<n;i++) { memset(fa_name, 0, 5); sprintf(fa_name,"%d",i); nodo[i].id = facility(fa_name,1); /* Aqui inicializa as variáveis locais */ } /* escalonamento ds eventos */ for (i=0; i<n; i++) schedule(test,30.0,i); schedule(fault,50.0,2); schedule(repair,80.0,2); while(time() < 100) { cause(&event,&token); switch(event) { case test: if(status(nodo[token].id) != 0) break; printf("O nodo %d vai estar no tempo %5.1f\n",token,time()); schedule(test,30.0,token); break; case fault: r=request(nodo[token].id,token,0); if(r!=0) { printf("Nao foi possivel falhar o nodo %d\n",token); exit(1); } printf("O nodo %d falhou no tempo %5.1f\n", token, time()); break; case repair: printf("O nodo %d se recuperou no tempo %5.1f\n", token, time()); release(nodo[token].id,token); schedule(test,30.0,token); break; } } }
bool SkJPEGImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { #ifdef TIME_DECODE AutoTimeMillis atm("JPEG Decode"); #endif SkAutoMalloc srcStorage; JPEGAutoClean autoClean; jpeg_decompress_struct cinfo; skjpeg_error_mgr sk_err; skjpeg_source_mgr sk_stream(stream, this, false); cinfo.err = jpeg_std_error(&sk_err); sk_err.error_exit = skjpeg_error_exit; // All objects need to be instantiated before this setjmp call so that // they will be cleaned up properly if an error occurs. if (setjmp(sk_err.fJmpBuf)) { return return_false(cinfo, *bm, "setjmp"); } jpeg_create_decompress(&cinfo); autoClean.set(&cinfo); #ifdef SK_BUILD_FOR_ANDROID overwrite_mem_buffer_size(&cinfo); #endif //jpeg_stdio_src(&cinfo, file); cinfo.src = &sk_stream; int status = jpeg_read_header(&cinfo, true); if (status != JPEG_HEADER_OK) { return return_false(cinfo, *bm, "read_header"); } /* Try to fulfill the requested sampleSize. Since jpeg can do it (when it can) much faster that we, just use their num/denom api to approximate the size. */ int sampleSize = this->getSampleSize(); if (this->getPreferQualityOverSpeed()) { cinfo.dct_method = JDCT_ISLOW; } else { cinfo.dct_method = JDCT_IFAST; } cinfo.scale_num = 1; cinfo.scale_denom = sampleSize; /* this gives about 30% performance improvement. In theory it may reduce the visual quality, in practice I'm not seeing a difference */ cinfo.do_fancy_upsampling = 0; /* this gives another few percents */ cinfo.do_block_smoothing = 0; /* default format is RGB */ cinfo.out_color_space = JCS_RGB; SkBitmap::Config config = this->getPrefConfig(k32Bit_SrcDepth, false); // only these make sense for jpegs if (config != SkBitmap::kARGB_8888_Config && config != SkBitmap::kARGB_4444_Config && config != SkBitmap::kRGB_565_Config) { config = SkBitmap::kARGB_8888_Config; } #ifdef ANDROID_RGB cinfo.dither_mode = JDITHER_NONE; if (config == SkBitmap::kARGB_8888_Config) { cinfo.out_color_space = JCS_RGBA_8888; } else if (config == SkBitmap::kRGB_565_Config) { cinfo.out_color_space = JCS_RGB_565; if (this->getDitherImage()) { cinfo.dither_mode = JDITHER_ORDERED; } } #endif if (sampleSize == 1 && mode == SkImageDecoder::kDecodeBounds_Mode) { bm->setConfig(config, cinfo.image_width, cinfo.image_height); bm->setIsOpaque(true); return true; } /* image_width and image_height are the original dimensions, available after jpeg_read_header(). To see the scaled dimensions, we have to call jpeg_start_decompress(), and then read output_width and output_height. */ if (!jpeg_start_decompress(&cinfo)) { /* If we failed here, we may still have enough information to return to the caller if they just wanted (subsampled bounds). If sampleSize was 1, then we would have already returned. Thus we just check if we're in kDecodeBounds_Mode, and that we have valid output sizes. One reason to fail here is that we have insufficient stream data to complete the setup. However, output dimensions seem to get computed very early, which is why this special check can pay off. */ if (SkImageDecoder::kDecodeBounds_Mode == mode && valid_output_dimensions(cinfo)) { SkScaledBitmapSampler smpl(cinfo.output_width, cinfo.output_height, recompute_sampleSize(sampleSize, cinfo)); bm->setConfig(config, smpl.scaledWidth(), smpl.scaledHeight()); bm->setIsOpaque(true); return true; } else { return return_false(cinfo, *bm, "start_decompress"); } } sampleSize = recompute_sampleSize(sampleSize, cinfo); // should we allow the Chooser (if present) to pick a config for us??? if (!this->chooseFromOneChoice(config, cinfo.output_width, cinfo.output_height)) { return return_false(cinfo, *bm, "chooseFromOneChoice"); } #ifdef ANDROID_RGB /* short-circuit the SkScaledBitmapSampler when possible, as this gives a significant performance boost. */ if (sampleSize == 1 && ((config == SkBitmap::kARGB_8888_Config && cinfo.out_color_space == JCS_RGBA_8888) || (config == SkBitmap::kRGB_565_Config && cinfo.out_color_space == JCS_RGB_565))) { bm->lockPixels(); JSAMPLE* rowptr = (JSAMPLE*)bm->getPixels(); bm->unlockPixels(); bool reuseBitmap = (rowptr != NULL); if (reuseBitmap && ((int) cinfo.output_width != bm->width() || (int) cinfo.output_height != bm->height())) { // Dimensions must match return false; } if (!reuseBitmap) { bm->setConfig(config, cinfo.output_width, cinfo.output_height); bm->setIsOpaque(true); if (SkImageDecoder::kDecodeBounds_Mode == mode) { return true; } if (!this->allocPixelRef(bm, NULL)) { return return_false(cinfo, *bm, "allocPixelRef"); } } else if (SkImageDecoder::kDecodeBounds_Mode == mode) { return true; } SkAutoLockPixels alp(*bm); rowptr = (JSAMPLE*)bm->getPixels(); INT32 const bpr = bm->rowBytes(); while (cinfo.output_scanline < cinfo.output_height) { int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1); // if row_count == 0, then we didn't get a scanline, so abort. // if we supported partial images, we might return true in this case if (0 == row_count) { return return_false(cinfo, *bm, "read_scanlines"); } if (this->shouldCancelDecode()) { return return_false(cinfo, *bm, "shouldCancelDecode"); } rowptr += bpr; } if (reuseBitmap) { bm->notifyPixelsChanged(); } jpeg_finish_decompress(&cinfo); return true; } #endif // check for supported formats SkScaledBitmapSampler::SrcConfig sc; if (3 == cinfo.out_color_components && JCS_RGB == cinfo.out_color_space) { sc = SkScaledBitmapSampler::kRGB; #ifdef ANDROID_RGB } else if (JCS_RGBA_8888 == cinfo.out_color_space) { sc = SkScaledBitmapSampler::kRGBX; } else if (JCS_RGB_565 == cinfo.out_color_space) { sc = SkScaledBitmapSampler::kRGB_565; #endif } else if (1 == cinfo.out_color_components && JCS_GRAYSCALE == cinfo.out_color_space) { sc = SkScaledBitmapSampler::kGray; } else { return return_false(cinfo, *bm, "jpeg colorspace"); } SkScaledBitmapSampler sampler(cinfo.output_width, cinfo.output_height, sampleSize); bm->lockPixels(); JSAMPLE* rowptr = (JSAMPLE*)bm->getPixels(); bool reuseBitmap = (rowptr != NULL); bm->unlockPixels(); if (reuseBitmap && (sampler.scaledWidth() != bm->width() || sampler.scaledHeight() != bm->height())) { // Dimensions must match return false; } if (!reuseBitmap) { bm->setConfig(config, sampler.scaledWidth(), sampler.scaledHeight()); // jpegs are always opaque (i.e. have no per-pixel alpha) bm->setIsOpaque(true); if (SkImageDecoder::kDecodeBounds_Mode == mode) { return true; } if (!this->allocPixelRef(bm, NULL)) { return return_false(cinfo, *bm, "allocPixelRef"); } } else if (SkImageDecoder::kDecodeBounds_Mode == mode) { return true; } SkAutoLockPixels alp(*bm); if (!sampler.begin(bm, sc, this->getDitherImage())) { return return_false(cinfo, *bm, "sampler.begin"); } uint8_t* srcRow = (uint8_t*)srcStorage.reset(cinfo.output_width * 4); // Possibly skip initial rows [sampler.srcY0] if (!skip_src_rows(&cinfo, srcRow, sampler.srcY0())) { return return_false(cinfo, *bm, "skip rows"); } // now loop through scanlines until y == bm->height() - 1 for (int y = 0;; y++) { JSAMPLE* rowptr = (JSAMPLE*)srcRow; int row_count = jpeg_read_scanlines(&cinfo, &rowptr, 1); if (0 == row_count) { return return_false(cinfo, *bm, "read_scanlines"); } if (this->shouldCancelDecode()) { return return_false(cinfo, *bm, "shouldCancelDecode"); } sampler.next(srcRow); if (bm->height() - 1 == y) { // we're done break; } if (!skip_src_rows(&cinfo, srcRow, sampler.srcDY() - 1)) { return return_false(cinfo, *bm, "skip rows"); } } // we formally skip the rest, so we don't get a complaint from libjpeg if (!skip_src_rows(&cinfo, srcRow, cinfo.output_height - cinfo.output_scanline)) { return return_false(cinfo, *bm, "skip rows"); } if (reuseBitmap) { bm->notifyPixelsChanged(); } jpeg_finish_decompress(&cinfo); // SkDebugf("------------------- bm2 size %d [%d %d] %d\n", bm->getSize(), bm->width(), bm->height(), bm->config()); return true; }
int main(int argc, char *argv[]){ static int N, /* Numero de nodos */ token, event, teste, r, i; static char fa_name[5]; /* Facility Name */ char *fail_code[2] = {"Sem falha", "Falho"}; if(argc != 2){ printf("USO: %s num_nodos\n", argv[0]); exit(1); } N = atoi(argv[1]); smpl(0, "Um Exemplo"); reset(); stream(1); /* Inicializacao */ nodo = (Tnodo*) malloc(sizeof(Tnodo)*N); for(i = 0; i < N; i++){ memset(fa_name, "\0", 5); sprintf(fa_name, "%d", i); nodo[i].id = facility(fa_name, 1); nodo[i].stats = -1; /* Aqui inicializa as variaveis locais */ } /* Escalonamento de eventos */ for(i=0; i<N; i++){ schedule(test, 30.0, i); } schedule(fault, 50.0, 2); /* O nodo 2 vai falhar em 50.0 */ schedule(fault, 50.0, 3); schedule(fault, 50.0, 4); schedule(repair, 100.0, 2); while(time() < 200){ cause(&event, &token); switch(event){ case test: if(status(nodo[token].id) != 0) break; teste = status(nodo[(token+1)%N].id); i = 1; while(teste){ teste = status(nodo[(token+i)%N].id); if(!teste == 0){ nodo[token].stats = (token+i)%N; i++; } } printf("%5.1f - O nodo %d testou o nodo %d. Resultado: %s\n", time(), token, (token+i)%N, fail_code[teste], nodo[token].stats); schedule(test, 30.0, token); break; case fault: r = request(nodo[token].id, token, 0); if(r != 0){ puts("Nao foi possivel falhar o nodo\n"); exit(1); } printf("%5.1f - O nodo %d falhou\n", time(), token); break; case repair: printf("%5.1f - O nodo %d recuperou\n", time(), token); release(nodo[token].id, token); schedule(test, 30.0, token); break; } } return 0; }
/* Program body */ int main(int argc, char *argv[]) { static int N; static int token; static int event; static int sender; static int timestamp; static int r; static int i; static int delay; static char fa_name[5]; int scheduled_events = 0; if (argc < 2) { show_usage(); exit(1); } // Number of nodes N = argc-1; // Sets up SMPL simulation smpl(0, "mutual exclusion"); reset(); stream(1); // 1 execution thread // Node intialization nodes = (Node*) malloc(N*sizeof(Node)); int times[N]; for (i=0; i<N; i++) { memset (fa_name, '\0', 5); sprintf(fa_name, "%d", i); nodes[i].id = facility(fa_name, 1); nodes[i].running = false; nodes[i].clock = 0; nodes[i].pending = (bool *) malloc(N*sizeof(bool)); memset (nodes[i].pending, false, N*sizeof(bool)); nodes[i].waiting_reply = 0; times[i] = atoi(argv[i+1]); // Timestamp -1 means that the node has no active request nodes[i].timestamp = -1; } // Schedules the critical region requests for (i=0; i<N; i++) { schedule (REQUEST, times[i], i, i, 0); scheduled_events++; } printf("=============================================================================\n"); printf(" Ricart-Agrawala mutual exclusion algorithm \n"); printf(" Renan Greca - Distributed Systems, May 2016 \n"); printf("=============================================================================\n"); // The program keeps running while there are still events scheduled while (scheduled_events > 0) { printf("-----\n"); cause(&event, &token, &sender, ×tamp); switch(event) { case REQUEST: // REQUEST critical region access to other nodes scheduled_events--; nodes[token].clock++; printf("(%d, %d) Requested C.R. \n", token, nodes[token].clock); // Stores timestamp of request for priority reference nodes[token].timestamp = nodes[token].clock; // Sends requests to all other nodes for (i=0; i<N; i++) { if (i==token) { continue; } delay = rand_int(0, MAX_MESSAGE_DELAY); // Sends REQUEST message to node i with a random delay schedule (RECEIVE_REQUEST, delay, i, token, nodes[token].clock); scheduled_events++; nodes[token].waiting_reply++; } break; case RECEIVE_REQUEST: // Receive and handle a REQUEST scheduled_events--; // Updates logical clock nodes[token].clock = (nodes[token].clock > timestamp ? nodes[token].clock : timestamp)+1; printf("(%d, %d) Received request from %d\n", token, nodes[token].clock, sender); // Checks whether the sender node has priority over the receiver if (priority(sender, token, timestamp, nodes[token].timestamp) && !nodes[token].running) { // If the sender has priority, send a REPLY delay = rand_int(0, MAX_MESSAGE_DELAY); printf("(%d, %d) Sending reply to %d\n", token, nodes[token].clock, sender); schedule(RECEIVE_REPLY, delay, sender, token, nodes[token].clock); scheduled_events++; } else { // Else, store it in the pending array printf("(%d, %d) Adding %d to pending array\n", token, nodes[token].clock, sender); nodes[token].pending[sender] = true; } break; case RECEIVE_REPLY: // Receive and handle a REPLY scheduled_events--; // Updates logical clock nodes[token].clock = (nodes[token].clock > timestamp ? nodes[token].clock : timestamp)+1; printf("(%d, %d) Received reply from %d\n", token, nodes[token].clock, sender); nodes[token].waiting_reply--; // If node received all replies, it enters the critical region if (nodes[token].waiting_reply == 0) { printf("(%d, %d) Entered the critical region.\n", token, nodes[token].clock); schedule (RELEASE, RUNNING_INTERVAL, token, token, nodes[token].clock); scheduled_events++; nodes[token].running = true; } break; case RELEASE: // Release the critical region and alert other nodes scheduled_events--; nodes[token].clock++; printf("(%d, %d) Left the critical region.\n", token, nodes[token].clock); // Clear the information of the previous request nodes[token].running = false; nodes[token].timestamp = -1; // Send all pending REPLIES for (i=0; i<N; i++) { if (nodes[token].pending[i]) { delay = rand_int(0, MAX_MESSAGE_DELAY); printf("(%d, %d) Sending reply to %d\n", token, nodes[token].clock, i); schedule(RECEIVE_REPLY, delay, i, token, nodes[token].clock); scheduled_events++; nodes[token].pending[i] = false; } } break; } } }
/* Program body */ int main(int argc, char *argv[]) { static int N; // Number of nodes static int token; static int event; static int r; static int i; static char fa_name[5]; if (argc != 2) { puts("Uso correto: tempo [num-nodos]"); exit(1); } N = atoi(argv[1]); smpl(0, "programa tempo"); reset(); stream(1); // 1 execution thread // Node intialization nodes = (Node*) malloc(N*sizeof(Node)); for (i=0; i<N; i++) { memset (fa_name, '\0', 5); sprintf(fa_name, "%d", i); nodes[i].id = facility(fa_name, 1); } for (i=0; i<N; i++) { schedule (TEST, 30.0, i); } schedule (FAULT, 31.0, 2); schedule (REPAIR, 61.0, 2); while (time() < 100.0) { cause(&event, &token); switch(event) { case TEST: if(status(nodes[token].id != 0)) { break; } printf("Sou o nodo %d, vou testar no tempo %5.1f\n", token, time()); printf("\n"); schedule(TEST, 30.0, token); break; case FAULT: r = request(nodes[token].id, token, 0); if (r != 0) { puts("Não consegui falhar o nodo!"); exit(1); } printf("Sou o nodo %d, falhei no tempo %5.1f\n", token, time()); break; case REPAIR: release(nodes[token].id, token); printf("Sou o nodo %d, recuperei no tempo %5.1f\n", token, time()); schedule(TEST, 30.0, token); break; } } }
int main(int argc, char **argv) { const double sim_time = 200; ///< Total simulation time static int show_usage = 0; ///< Flag to indicate if it is necessary to show usage options static int pnum = 0; ///< Number of processes in the system int event, ///< Current event pid, ///< ID of the process executing the event num_requests = 0, ///< Total number of requests in the simulation (used as stop criterion) p0_time = 0, ///< Time at which process 0 will request the critical region p1_time = 0, ///< Time at which process 1 will request the critical region p2_time = 0; ///< Time at which process 2 will request the critical region struct timeval tp; queue_item recvd_msg; print_header(); if (argc < 3) { print_usage(argv[0]); exit(1); } // parse parameters int c; while (1) { static struct option long_options[] = { {"help", no_argument, &show_usage, 1}, {"nproc", required_argument, 0, 'n'}, {"rc0", required_argument, 0, 'p'}, {"rc1", required_argument, 0, 'q'}, {"rc2", required_argument, 0, 'r'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long(argc, argv, "n:p:q:r:", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 0: /* If this option set a flag, do nothing else now. */ if (long_options[option_index].flag != 0) break; printf ("option %s", long_options[option_index].name); if (optarg) printf (" with arg %s", optarg); printf ("\n"); break; case 'n': pnum = (int) strtol(optarg, NULL, 10); break; case 'p': p0_time = (int) strtol(optarg, NULL, 10); ++num_requests; break; case 'q': p1_time = (int) strtol(optarg, NULL, 10); ++num_requests; break; case 'r': p2_time = (int) strtol(optarg, NULL, 10); ++num_requests; break; case '?': /* getopt_long already printed an error message. */ break; default: abort(); } } if (show_usage) { print_usage(argv[0]); exit(0); } if (pnum < 2) { printf("Error: Invalid number of processes! Please, specify at least 2 processes using the --nproc option.\n\n"); print_usage(argv[0]); exit(1); } else if (pnum < 2 && p2_time != 0) { printf("Error: You have specified a time for process 2 (starting from 0) on a simulation with only 2 processes!\n\n"); print_usage(argv[0]); exit(1); } else if (p0_time == 0 && p1_time == 0 && p2_time == 0) { printf("You have not specified any critical region requests in your simulation!\n\n"); print_usage(argv[0]); exit(1); } // initialize PRNG gettimeofday(&tp, NULL); srand(tp.tv_sec + tp.tv_usec / 1000); seed(tp.tv_sec + tp.tv_usec / 1000, 1); smpl(0, "Ricart-Agrawala"); stream(1); process plist = init_processes(pnum); printf("-- BEGIN PARAMETERS --\n"); printf("Number of processes: %d\n", pnum); printf("Number of critical region requests: %d\n", num_requests); printf("Starting events:\n"); if (p0_time != 0) { printf(" - Process 0 will request the critical region at time %d\n", p0_time); schedule(EV_REQUEST, p0_time, 0); } if (p1_time != 0) { printf(" - Process 1 will request the critical region at time %d\n", p1_time); schedule(EV_REQUEST, p1_time, 1); } if (p2_time != 0) { printf(" - Process 2 will request the critical region at time %d\n", p2_time); schedule(EV_REQUEST, p2_time, 2); } printf("-- END PARAMETERS --\n\n"); printf("-- SIMULATION BEGIN --\n"); while(time() < sim_time) { cause(&event, &pid); switch(event) { case EV_REQUEST: printf("Process %d has executed a critical region REQUEST at time %g\n", pid, time()); plist[pid].state = ST_WANTED; // update pid's logical clock plist[pid].timestamp++; plist[pid].request_timestamp = plist[pid].timestamp; // broadcast REQUEST to all processes in the system broadcast(plist, pnum, pid); break; case EV_RECV: // remove next message to be received by pid recvd_msg = remove_max_pqueue(plist[pid].recvd_from); // synchronize pid's logical clock on receive before processing the message plist[pid].timestamp = (plist[pid].timestamp > recvd_msg->timestamp) ? plist[pid].timestamp : recvd_msg->timestamp; plist[pid].timestamp++; printf("Process %d received %s from %d at time %g\n", pid, (recvd_msg->type == MSG_REQUEST) ? "REQUEST" : "REPLY", recvd_msg->pid, time()); recv(plist, pnum, pid, recvd_msg->pid, recvd_msg->timestamp, recvd_msg->type); free(recvd_msg); break; case EV_RELEASE: printf("Process %d released the critical region at time %g\n", pid, time()); releasecr(plist, pnum, pid); num_requests--; break; } // no more requests to simulate, simulation ended if (num_requests == 0) break; } printf("-- SIMULATION END --\n"); destroy_processes(plist, pnum); return 0; }