int restore_handle_progress_msg(restored_client_t client, plist_t msg) { plist_t node = NULL; uint64_t progress = 0; uint64_t operation = 0; node = plist_dict_get_item(msg, "Operation"); if (!node || plist_get_node_type(node) != PLIST_UINT) { debug("Failed to parse operation from ProgressMsg plist\n"); return -1; } plist_get_uint_val(node, &operation); node = plist_dict_get_item(msg, "Progress"); if (!node || plist_get_node_type(node) != PLIST_UINT) { debug("Failed to parse progress from ProgressMsg plist \n"); return -1; } plist_get_uint_val(node, &progress); if ((progress > 0) && (progress < 100)) { print_progress_bar(restore_progress_string(operation), (double) progress); } else { info("%s\n", restore_progress_string(operation)); } return 0; }
int progress_cb(irecv_client_t client, const irecv_event_t* event) { if (event->type == IRECV_PROGRESS) { print_progress_bar(event->progress); } return 0; }
static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum, const char *data, uint16_t length) { if (self->num != num) return; FILE *fp = friends[num].file_receiver.files[filenum]; if (fp) { if (fwrite(data, length, 1, fp) != 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Error writing to file."); tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); chat_close_file_receiver(num, filenum); } } friends[num].file_receiver.bps[filenum] += length; double remain = (double) tox_file_data_remaining(m, num, filenum, 1); uint64_t curtime = get_unix_time(); /* refresh line with percentage complete and transfer speed (must be called once per second) */ if (!remain || timed_out(friends[num].file_receiver.last_progress[filenum], curtime, 1)) { friends[num].file_receiver.last_progress[filenum] = curtime; uint64_t size = friends[num].file_receiver.size[filenum]; double pct_remain = remain > 0 ? (1 - (remain / size)) * 100 : 100; print_progress_bar(self, filenum, num, pct_remain); friends[num].file_receiver.bps[filenum] = 0; } }
static void send_file_data(ToxWindow *self, Tox *m, int i, int32_t friendnum, int filenum, const char *filename) { FILE *fp = file_senders[i].file; while (true) { if (tox_file_send_data(m, friendnum, filenum, (uint8_t *) file_senders[i].nextpiece, file_senders[i].piecelen) == -1) return; uint64_t curtime = get_unix_time(); file_senders[i].timestamp = curtime; file_senders[i].bps += file_senders[i].piecelen; file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, friendnum), fp); double remain = (double) tox_file_data_remaining(m, friendnum, filenum, 0); /* refresh line with percentage complete and transfer speed (must be called once per second) */ if (timed_out(file_senders[i].last_progress, curtime, 1) || (!remain && !file_senders[i].finished)) { file_senders[i].last_progress = curtime; double pct_done = remain > 0 ? (1 - (remain / file_senders[i].size)) * 100 : 100; print_progress_bar(self, i, -1, pct_done); file_senders[i].bps = 0; } /* file sender is closed in chat_onFileControl callback after receiving reply */ if (file_senders[i].piecelen == 0 && !file_senders[i].finished) { tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); file_senders[i].finished = true; } } }
int write_output(void) { int fd; struct filter_op *fop; struct filter_header fh; size_t ninst, i, data_len; u_char pad = 0, *data = NULL; /* conver the tree to an array of filter_op */ ninst = compile_tree(&fop); if (fop == NULL) return -E_NOTHANDLED; if (ninst == 0) return -E_INVALID; /* create the file */ fd = open(GBL_OPTIONS->output_file, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0644); ON_ERROR(fd, -1, "Can't create file %s", GBL_OPTIONS->output_file); /* display the message */ fprintf(stdout, " Writing output to \'%s\' ", GBL_OPTIONS->output_file); fflush(stdout); /* compute the header */ fh.magic = htons(EC_FILTER_MAGIC); strncpy(fh.version, EC_VERSION, sizeof(fh.version)); fh.data = sizeof(fh); data_len = create_data_segment(&data, &fh, fop, ninst); /* write the header */ write(fd, &fh, sizeof(struct filter_header)); /* write the data segment */ write(fd, data, data_len); /* write padding to next 8-byte boundary */ for (i = 0; i < fh.code - (fh.data + data_len); i++) write(fd, &pad, 1); /* write the instructions */ for (i = 0; i < ninst; i++) { print_progress_bar(&fop[i]); write(fd, &fop[i], sizeof(struct filter_op)); } close(fd); fprintf(stdout, " done.\n\n"); fprintf(stdout, " -> Script encoded into %d instructions.\n\n", (int)(i - 1)); return E_SUCCESS; }
void progress_bar(sfile_t *f) { time_t tick; unsigned long speed; int percent; human_unit_t h; off_t filesize = 0; if (g_quiet) return; if (f->fd) filesize = f->st.st_size ? f->st.st_size : f->h->content_length; tick = time(NULL); if (f->fh_cur_pos == filesize) { print_progress_bar(100, f->fh_cur_pos, 0, 0); } else if (f->speedometer == 0 || tick >= f->pb_timer + 1) { f->pb_timer = time(NULL); speed = f->fh_cur_pos - f->speedometer; f->speedometer = f->fh_cur_pos; if (filesize) { percent = (int)(f->fh_cur_pos * 100 / filesize); print_progress_bar(percent, f->fh_cur_pos, speed, (filesize - f->fh_cur_pos) / speed); } else { bytes_to_unit(&h, speed); #if defined(__i386__) || defined(__arm__) || defined(__APPLE__) printf("\033[2K\r%llu bytes - %.0f %s/s", f->fh_cur_pos, h.b, h.unit); #else printf("\033[2K\r%lu bytes - %.0f %s/s", f->fh_cur_pos, h.b, h.unit); #endif } } }
void idevicerestore_progress(struct idevicerestore_client_t* client, int step, double progress) { if(client && client->progress_cb) { client->progress_cb(step, progress, client->progress_cb_data); } else { // we don't want to be too verbose in regular idevicerestore. if ((step == RESTORE_STEP_UPLOAD_FS) || (step == RESTORE_STEP_VERIFY_FS) || (step == RESTORE_STEP_FLASH_FW)) { print_progress_bar(100.0f * progress); } } }
static void refresh_progress_helper(ToxWindow *self, Tox *m, struct FileTransfer *ft) { if (ft->state == FILE_TRANSFER_INACTIVE) return; /* Timeout must be set to 1 second to show correct bytes per second */ if (!timed_out(ft->last_line_progress, 1)) return; double remain = ft->file_size - ft->position; double pct_done = remain > 0 ? (1 - (remain / ft->file_size)) * 100 : 100; print_progress_bar(self, ft->bps, pct_done, ft->line_id); ft->bps = 0; ft->last_line_progress = get_unix_time(); }
int asr_send_payload(idevice_connection_t asr, const char* filesystem) { int i = 0; char data[1450]; FILE* file = NULL; uint32_t bytes = 0; uint32_t count = 0; uint32_t length = 0; double progress = 0; file = fopen(filesystem, "rb"); if (file == NULL) { return -1; } fseek(file, 0, SEEK_END); length = ftell(file); fseek(file, 0, SEEK_SET); for(i = length; i > 0; i -= 1450) { int size = 1450; if (i < 1450) { size = i; } if (fread(data, 1, size, file) != (unsigned int) size) { error("Error reading filesystem\n"); fclose(file); return -1; } if (asr_send_buffer(asr, data, size) < 0) { error("ERROR: Unable to send filesystem payload\n"); fclose(file); return -1; } bytes += size; progress = ((double) bytes/ (double) length) * 100.0; print_progress_bar(progress); } fclose(file); return 0; }
int main (int argc, char **argv) { pthread_t *threads; long i; int ret; struct timespec intv; struct sched_param param; parse_options(argc, argv); signal(SIGINT, stop_log); if (argc >= (optind + 1)) nr_tasks = atoi(argv[optind]); else nr_tasks = count_cpus() + 1; threads = malloc(sizeof(*threads) * nr_tasks); if (!threads) perr("malloc"); memset(threads, 0, sizeof(*threads) * nr_tasks); ret = pthread_barrier_init(&start_barrier, NULL, nr_tasks + 1); ret = pthread_barrier_init(&end_barrier, NULL, nr_tasks + 1); if (ret < 0) perr("pthread_barrier_init"); intervals = malloc(sizeof(void*) * nr_runs); if (!intervals) perr("malloc intervals array"); intervals_length = malloc(sizeof(void*) * nr_runs); if (!intervals_length) perr("malloc intervals length array"); intervals_loops = malloc(sizeof(void*) * nr_runs); if (!intervals_loops) perr("malloc intervals loops array"); thread_pids = malloc(sizeof(long) * nr_tasks); if (!thread_pids) perr("malloc thread_pids"); for (i=0; i < nr_runs; i++) { intervals[i] = malloc(sizeof(unsigned long long)*nr_tasks); if (!intervals[i]) perr("malloc intervals"); memset(intervals[i], 0, sizeof(unsigned long long)*nr_tasks); intervals_length[i] = malloc(sizeof(unsigned long long)*nr_tasks); if (!intervals_length[i]) perr("malloc length intervals"); memset(intervals_length[i], 0, sizeof(unsigned long long)*nr_tasks); intervals_loops[i] = malloc(sizeof(unsigned long)*nr_tasks); if (!intervals_loops[i]) perr("malloc loops intervals"); memset(intervals_loops[i], 0, sizeof(unsigned long)*nr_tasks); } for (i=0; i < nr_tasks; i++) { if (pthread_create(&threads[i], NULL, start_task, (void *)i)) perr("pthread_create"); } /* * Progress bar uses stderr to let users see it when * redirecting output. So we convert stderr to use line * buffering so the progress bar doesn't flicker. */ setlinebuf(stderr); /* up our prio above all tasks */ memset(¶m, 0, sizeof(param)); param.sched_priority = nr_tasks + prio_start; if (sched_setscheduler(0, SCHED_FIFO, ¶m)) fprintf(stderr, "Warning, can't set priority of main thread!\n"); intv.tv_sec = nano2sec(INTERVAL); intv.tv_nsec = INTERVAL % sec2nano(1); print_progress_bar(0); lfd = do_logdev_open(); logdev_print_set(1); logdev_switch_set(1); for (loop=0; loop < nr_runs; loop++) { unsigned long long end; now = get_time(); lgprint(lfd, "Loop %d now=%lld\n", loop, now); pthread_barrier_wait(&start_barrier); lgprint(lfd, "All running!!!\n"); nanosleep(&intv, NULL); print_progress_bar((loop * 100)/ nr_runs); end = get_time(); lgprint(lfd, "Loop %d end now=%lld diff=%lld\n", loop, end, end - now); pthread_barrier_wait(&end_barrier); if (stop || (check && check_times(loop))) { loop++; nr_runs = loop; break; } } do_logdev_close(lfd); putc('\n', stderr); pthread_barrier_wait(&start_barrier); done = 1; pthread_barrier_wait(&end_barrier); for (i=0; i < nr_tasks; i++) pthread_join(threads[i], (void*)&thread_pids[i]); logdev_print_set(0); logdev_switch_set(0); print_results(); if (stop) { /* * We use this test in bash while loops * So if we hit Ctrl-C then let the while * loop know to break. */ if (check < 0) exit(-1); else exit(1); } if (check < 0) exit(-1); else exit(0); return 0; }
int ipsw_extract_to_file(const char* ipsw, const char* infile, const char* outfile) { ipsw_archive* archive = ipsw_open(ipsw); if (archive == NULL || archive->zip == NULL) { error("ERROR: Invalid archive\n"); return -1; } int zindex = zip_name_locate(archive->zip, infile, 0); if (zindex < 0) { error("ERROR: zip_name_locate: %s\n", infile); return -1; } struct zip_stat zstat; zip_stat_init(&zstat); if (zip_stat_index(archive->zip, zindex, 0, &zstat) != 0) { error("ERROR: zip_stat_index: %s\n", infile); return -1; } char* buffer = (char*) malloc(BUFSIZE); if (buffer == NULL) { error("ERROR: Unable to allocate memory\n"); return -1; } struct zip_file* zfile = zip_fopen_index(archive->zip, zindex, 0); if (zfile == NULL) { error("ERROR: zip_fopen_index: %s\n", infile); return -1; } FILE* fd = fopen(outfile, "wb"); if (fd == NULL) { error("ERROR: Unable to open output file: %s\n", outfile); zip_fclose(zfile); return -1; } int i = 0; int size = 0; int bytes = 0; int count = 0; double progress = 0; for(i = zstat.size; i > 0; i -= count) { if (i < BUFSIZE) size = i; else size = BUFSIZE; count = zip_fread(zfile, buffer, size); if (count < 0) { error("ERROR: zip_fread: %s\n", infile); zip_fclose(zfile); free(buffer); return -1; } fwrite(buffer, 1, count, fd); bytes += size; progress = ((double) bytes/ (double) zstat.size) * 100.0; print_progress_bar(progress); } fclose(fd); zip_fclose(zfile); ipsw_close(archive); free(buffer); return 0; }
int recovery_progress_callback(irecv_client_t client, const irecv_event_t* event) { if (event->type == IRECV_PROGRESS) { print_progress_bar(event->data, event->progress); } return 0; }
int main(int argc, char **argv) { pthread_t *threads; long i; int ret; struct timespec intv; struct sched_param param; rt_init("a:r:t:e:l:h:", parse_args, argc, argv); signal(SIGINT, stop_log); if (argc >= (optind + 1)) nr_tasks = atoi(argv[optind]); else { numcpus = sysconf(_SC_NPROCESSORS_ONLN); nr_tasks = numcpus + 1; } intervals = malloc(sizeof(stats_container_t) * nr_tasks); if (!intervals) debug(DBG_ERR, "malloc failed\n"); memset(intervals, 0, sizeof(stats_container_t) * nr_tasks); intervals_length = malloc(sizeof(stats_container_t) * nr_tasks); if (!intervals_length) debug(DBG_ERR, "malloc failed\n"); memset(intervals_length, 0, sizeof(stats_container_t) * nr_tasks); if (!intervals_loops) debug(DBG_ERR, "malloc failed\n"); intervals_loops = malloc(sizeof(stats_container_t) * nr_tasks); memset(intervals_loops, 0, sizeof(stats_container_t) * nr_tasks); threads = malloc(sizeof(*threads) * nr_tasks); if (!threads) debug(DBG_ERR, "malloc failed\n"); memset(threads, 0, sizeof(*threads) * nr_tasks); ret = pthread_barrier_init(&start_barrier, NULL, nr_tasks + 1); ret = pthread_barrier_init(&end_barrier, NULL, nr_tasks + 1); if (ret < 0) debug(DBG_ERR, "pthread_barrier_init failed: %s\n", strerror(ret)); for (i = 0; i < nr_tasks; i++) { stats_container_init(&intervals[i], nr_runs); stats_container_init(&intervals_length[i], nr_runs); stats_container_init(&intervals_loops[i], nr_runs); } thread_pids = malloc(sizeof(long) * nr_tasks); if (!thread_pids) debug(DBG_ERR, "malloc thread_pids failed\n"); for (i = 0; i < nr_tasks; i++) { threads[i] = create_fifo_thread(start_task, (void *)i, prio_start + i); } /* * Progress bar uses stderr to let users see it when * redirecting output. So we convert stderr to use line * buffering so the progress bar doesn't flicker. */ setlinebuf(stderr); /* up our prio above all tasks */ memset(¶m, 0, sizeof(param)); param.sched_priority = nr_tasks + prio_start; if (sched_setscheduler(0, SCHED_FIFO, ¶m)) debug(DBG_WARN, "Warning, can't set priority of" "main thread !\n"); intv.tv_sec = INTERVAL / NS_PER_SEC; intv.tv_nsec = INTERVAL % (1 * NS_PER_SEC); print_progress_bar(0); setup_ftrace_marker(); for (loop = 0; loop < nr_runs; loop++) { unsigned long long end; now = rt_gettime() / NS_PER_US; ftrace_write("Loop %d now=%lld\n", loop, now); pthread_barrier_wait(&start_barrier); ftrace_write("All running!!!\n"); rt_nanosleep(intv.tv_nsec); print_progress_bar((loop * 100) / nr_runs); end = rt_gettime() / NS_PER_US; ftrace_write("Loop %d end now=%lld diff=%lld\n", loop, end, end - now); ret = pthread_barrier_wait(&end_barrier); if (stop || (check && check_times(loop))) { loop++; nr_runs = loop; break; } } putc('\n', stderr); pthread_barrier_wait(&start_barrier); done = 1; pthread_barrier_wait(&end_barrier); join_threads(); print_results(); if (stop) { /* * We use this test in bash while loops * So if we hit Ctrl-C then let the while * loop know to break. */ if (check < 0) exit(-1); else exit(1); } if (check < 0) exit(-1); else exit(0); return 0; }