/* Peels off all the blobs which have been in the input queue for longer * than the spill limit, move them to the spill queue, and enqueue * them for eventual spilling or dropping. * * Note that the "spill queue" is used either for actual spilling (to the disk) * or dropping. * * Returns the number of (eventually) spilled (if spill enabled) or * dropped (if spill disabled) items. */ static stats_count_t spill_by_age(socket_worker_t * self, int spill_enabled, queue_t * private_queue, queue_t * spill_queue, uint64_t spill_microsec, struct timeval *now) { blob_t *cur_blob = private_queue->head; if (!cur_blob) return 0; /* If spill is disabled, this really counts the dropped packets. */ stats_count_t spilled = 0; if (elapsed_usec(&BLOB_RECEIVED_TIME(cur_blob), now) >= spill_microsec) { spill_queue->head = cur_blob; spill_queue->count = 1; while (BLOB_NEXT(cur_blob) && elapsed_usec(&BLOB_RECEIVED_TIME(BLOB_NEXT(cur_blob)), now) >= spill_microsec) { cur_blob = BLOB_NEXT(cur_blob); spill_queue->count++; } spill_queue->tail = cur_blob; private_queue->head = BLOB_NEXT(cur_blob); private_queue->count -= spill_queue->count; BLOB_NEXT_set(cur_blob, NULL); spilled += spill_queue->count; if (spill_enabled) { RELAY_ATOMIC_INCREMENT(self->counters.spilled_count, spill_queue->count); } else { RELAY_ATOMIC_INCREMENT(self->counters.dropped_count, spill_queue->count); } enqueue_queue_for_disk_writing(self, spill_queue); } return spilled; }
void fimg2d_perf_print(struct fimg2d_bltcmd *cmd) { int i; long time; struct fimg2d_perf *perf; if (WARN_ON(!cmd->ctx)) return; for (i = 0; i < MAX_PERF_DESCS; i++) { perf = &cmd->ctx->perf[i]; time = elapsed_usec(cmd->ctx, i); pr_info("[FIMG2D PERF (%8s)] ctx(0x%08x) seq(%d) %8ld usec\n", perfname(i), (unsigned int)cmd->ctx, perf->seq_no, time); } pr_info("[FIMG2D PERF ** seq(%d)]\n", cmd->blt.seq_no); }
long time_parser(void (*parse_doc)(char*,int, bool,char*), char* buf, int len, bool doHash, char *charset, int test_count) { struct timeval tv1, tv2; struct timezone tz1, tz2; long times[test_count]; long long total=0; long max_time=-1; long min_time=999999999; for (int i=0;i<test_count;i++ ){ gettimeofday(&tv1, &tz1); parse_doc(buf,len, doHash,charset); gettimeofday(&tv2, &tz2); times[i] = elapsed_usec(&tv1, &tv2); total += times[i]; if (times[i] < min_time) min_time = times[i]; if (times[i] > max_time) max_time = times[i]; } long avg_time = total/test_count; printf("Hash %s, count: %d, avg: %ld, min: %ld, max: %ld\n", (doHash?"true":"false"), test_count, avg_time, min_time, max_time); return avg_time; }
PJ_DEF(pj_uint32_t) pj_elapsed_usec( const pj_timestamp *start, const pj_timestamp *stop ) { return (pj_uint32_t)elapsed_usec(start, stop); }
static int process_queue(socket_worker_t * self, relay_socket_t * sck, queue_t * private_queue, queue_t * spill_queue, ssize_t * wrote) { if (sck == NULL) { WARN("NULL forwarding socket"); return 0; } blob_t *cur_blob; struct timeval now; struct timeval send_start_time; struct timeval send_end_time; stats_count_t spilled = 0; const config_t *config = self->base.config; const uint64_t spill_microsec = 1000 * config->spill_millisec; const uint64_t grace_microsec = 1000 * config->spill_grace_millisec; const struct sockaddr *dest_addr = (const struct sockaddr *) &sck->sa.in; socklen_t addr_len = sck->addrlen; int in_grace_period = 0; struct timeval grace_period_start; int failed = 0; *wrote = 0; get_time(&send_start_time); cork(sck, 1); while (private_queue->head != NULL) { get_time(&now); /* While not all the socket backends are present, for a configured maximum time, * do not spill/drop. This is a bit crude, better rules/heuristics welcome. */ if (!connected_all()) { if (in_grace_period == 0) { in_grace_period = 1; get_time(&grace_period_start); SAY("Spill/drop grace period of %d millisec started", config->spill_grace_millisec); } if (elapsed_usec(&grace_period_start, &now) >= grace_microsec) { in_grace_period = 0; SAY("Spill/drop grace period of %d millisec expired", config->spill_grace_millisec); } } else { if (in_grace_period) { SAY("Spill/drop grace period of %d millisec canceled", config->spill_grace_millisec); } in_grace_period = 0; } if (in_grace_period == 0) { spilled += spill_by_age(self, config->spill_enabled, private_queue, spill_queue, spill_microsec, &now); } cur_blob = private_queue->head; if (!cur_blob) break; void *blob_data; ssize_t blob_size; if (sck->type == SOCK_DGRAM) { blob_size = BLOB_BUF_SIZE(cur_blob); blob_data = BLOB_BUF_addr(cur_blob); } else { /* sck->type == SOCK_STREAM */ blob_size = BLOB_DATA_MBR_SIZE(cur_blob); blob_data = BLOB_DATA_MBR_addr(cur_blob); } ssize_t blob_left = blob_size; ssize_t blob_sent = 0; int sendto_errno = 0; failed = 0; /* Keep sending while we have data left since a single sendto() * doesn't necessarily send all of it. This may eventually fail * if sendto() returns -1. */ while (!RELAY_ATOMIC_READ(self->base.stopping) && blob_left > 0) { const void *data = (const char *) blob_data + blob_sent; ssize_t sent; sendto_errno = 0; if (sck->type == SOCK_DGRAM) { sent = sendto(sck->socket, data, blob_left, MSG_NOSIGNAL, dest_addr, addr_len); } else { /* sck->type == SOCK_STREAM */ sent = sendto(sck->socket, data, blob_left, MSG_NOSIGNAL, NULL, 0); } sendto_errno = errno; if (0) { /* For debugging. */ peek_send(sck, data, blob_left, sent); } if (sent == -1) { WARN_ERRNO("sendto() tried sending %zd bytes to %s but sent none", blob_left, sck->to_string); RELAY_ATOMIC_INCREMENT(self->counters.error_count, 1); if (sendto_errno == EINTR) { /* sendto() got interrupted by a signal. Wait a while and retry. */ WARN("Interrupted, resuming"); worker_wait_millisec(config->sleep_after_disaster_millisec); continue; } failed = 1; break; /* stop sending from the hijacked queue */ } blob_sent += sent; blob_left -= sent; } if (blob_sent == blob_size) { RELAY_ATOMIC_INCREMENT(self->counters.sent_count, 1); } else if (blob_sent < blob_size) { /* Despite the send-loop above, we failed to send all the bytes. */ WARN("sendto() tried sending %zd bytes to %s but sent only %zd", blob_size, sck->to_string, blob_sent); RELAY_ATOMIC_INCREMENT(self->counters.partial_count, 1); failed = 1; } *wrote += blob_sent; if (failed) { /* We failed to send this packet. Exit the loop, and * right after the loop close the socket, and get out, * letting the main loop to reconnect. */ if ((sendto_errno == EAGAIN || sendto_errno == EWOULDBLOCK)) { /* Traffic jam. Wait a while, but still get out. */ WARN("Traffic jam"); worker_wait_millisec(config->sleep_after_disaster_millisec); } break; } else { queue_shift_nolock(private_queue); blob_destroy(cur_blob); } } cork(sck, 0); get_time(&send_end_time); if (spilled) { if (config->spill_enabled) { WARN("Wrote %lu items which were over spill threshold", (unsigned long) spilled); } else { WARN("Spill disabled: DROPPED %lu items which were over spill threshold", (unsigned long) spilled); } } /* this assumes end_time >= start_time */ uint64_t usec = elapsed_usec(&send_start_time, &send_end_time); RELAY_ATOMIC_INCREMENT(self->counters.send_elapsed_usec, usec); return failed == 0; }
void domainKernelCombine(domainSetup_t * setupDbPtr, domainKernel_t * kernelPtr, long * elapsed) { *elapsed = elapsed_usec(&kernelPtr->startTimer, &kernelPtr->stopTimer); }
int main (int argc, char **argv) { char * filename = argv[1]; fprintf(stderr, "Reading \"%s\"\n", filename); FILE *fp = fopen(filename,"r"); if (!fp){ fprintf(stderr, "Error: could not open file \"%s\"\n", filename); exit(1); } //get charset char *charset = argv[2]; // Get File size size_t file_size; fseek(fp, 0L, SEEK_END); file_size = (size_t)ftell(fp); fseek(fp, 0L, SEEK_SET); char *file_buf = (char*)malloc(file_size+1); size_t nread = fread(file_buf, (size_t)1,file_size, fp); fclose(fp); if (nread != file_size){ fprintf(stderr, "Warning: wanted %d chars, but read %d\n", file_size, nread); } file_buf[nread] = '\0'; int32_t ucBufSize = (int32_t)(nread*2.5); UChar *ucBuf = (UChar*)malloc(ucBufSize); int32_t ucLen = ucToUnicode(ucBuf, ucBufSize, file_buf, nread, "utf-8", NULL); struct timeval tv1, tv2; struct timezone tz1, tz2; int32_t times[test_count]; int64_t total=0; int32_t max_time=-1L; int32_t min_time=999999999L; int32_t avg_time; //int32_t u8size = nread*2; //char *u8buf = (char*)malloc(u8size); int32_t newsize = 0; for (int i=0;i<test_count;i++ ){ gettimeofday(&tv1, &tz1); newsize = ucToUnicode(ucBuf, ucBufSize, file_buf, nread, charset, NULL) << 1; gettimeofday(&tv2, &tz2); times[i] = elapsed_usec(&tv1, &tv2); total += times[i]; if (times[i] < min_time) min_time = times[i]; if (times[i] > max_time) max_time = times[i]; } avg_time = total/test_count; fprintf(stderr,"ICU size: %"INT32", count: %"INT32", avg: %"INT32", min: %"INT32", max: %"INT32"\n", newsize, test_count, avg_time, min_time, max_time); int outfd = open("icu.out", O_CREAT|O_RDWR|O_TRUNC, 00666); if (outfd < 0) {printf("Error creating output file: %s\n", strerror(errno)); exit(1);} write(outfd, ucBuf, newsize); close(outfd); #if 0 total = 0; min_time = 999999999L; max_time = -1L; for (int i=0;i<test_count;i++ ){ gettimeofday(&tv1, &tz1); //newsize = utf16ToUtf8_iconv(u8buf, u8size, ucBuf, ucLen); newsize = ucToUnicode_iconv(ucBuf, ucBufSize, file_buf, nread, charset, NULL) << 1; gettimeofday(&tv2, &tz2); times[i] = elapsed_usec(&tv1, &tv2); total += times[i]; if (times[i] < min_time) min_time = times[i]; if (times[i] > max_time) max_time = times[i]; } avg_time = total/test_count; fprintf(stderr,"iconv size: %"INT32", count: %"INT32", avg: %"INT32", min: %"INT32", max: %"INT32"\n", newsize, test_count, avg_time, min_time, max_time); outfd = open("iconv.out", O_CREAT|O_RDWR|O_TRUNC, 00666); if (outfd < 0) {printf("Error creating output file: %s\n", strerror(errno)); exit(1);} write(outfd, ucBuf, newsize); close(outfd); #endif #if 0 total = 0; min_time = 999999999L; max_time = -1L; for (int i=0;i<test_count;i++ ){ gettimeofday(&tv1, &tz1); newsize = utf16ToUtf8_intern(u8buf, u8size, ucBuf, ucLen); gettimeofday(&tv2, &tz2); times[i] = elapsed_usec(&tv1, &tv2); total += times[i]; if (times[i] < min_time) min_time = times[i]; if (times[i] > max_time) max_time = times[i]; } avg_time = total/test_count; fprintf(stderr,"my size: %"INT32", count: %"INT32", avg: %"INT32", min: %"INT32", max: %"INT32"\n", newsize, test_count, avg_time, min_time, max_time); outfd = open("my.out", O_CREAT|O_RDWR|O_TRUNC, 00666); if (outfd < 0) {printf("Error creating output file: %s\n", strerror(errno)); exit(1);} write(outfd, u8buf, newsize); close(outfd); #endif //printf("%s\n", u8buf); }