struct timespec wait_for(struct timespec time_in) { struct timespec now; double now_db, time_db; time_db = timespec_to_double(time_in); clock_gettime(CLOCK_REALTIME, &now); now_db = timespec_to_double(now); while (now_db < time_db) { clock_gettime(CLOCK_REALTIME, &now); now_db = timespec_to_double(now); } return now; }
static void stopwatch_stop(struct stopwatch *watch, long long bytes_read) { double elapsed, rate; if (clock_gettime(CLOCK_MONOTONIC, &watch->stop)) { int err = errno; fprintf(stderr, "clock_gettime(CLOCK_MONOTONIC) failed with " "error %d (%s)\n", err, strerror(err)); goto done; } elapsed = timespec_to_double(&watch->stop) - timespec_to_double(&watch->start); rate = (bytes_read / elapsed) / (1024 * 1024 * 1024); printf("stopwatch: took %.5g seconds to read %lld bytes, " "for %.5g GB/s\n", elapsed, bytes_read, rate); printf("stopwatch: %.5g seconds\n", elapsed); done: free(watch); }
CAMLprim value core_kernel_time_ns_nanosleep(value v_seconds) { struct timespec req = timespec_of_double(Double_val(v_seconds)); struct timespec rem; int retval; caml_enter_blocking_section(); retval = nanosleep(&req, &rem); caml_leave_blocking_section(); if (retval == 0) return caml_copy_double(0.0); else if (retval == -1) { if (errno == EINTR) return caml_copy_double(timespec_to_double(rem)); else uerror("nanosleep", Nothing); } else caml_failwith("core_kernel_time_ns_nanosleep: impossible return value from nanosleep(2)"); }
int arm_walsh_cmd(struct katcp_dispatch *d, int argc){ int s, hb_offset; time_t timeStamp; uint32_t value, mask; struct tbs_raw *tr; struct tbs_entry *te; double start_db, now_db, arm_at_db; struct timespec start, now; /* Grab the mode pointer */ tr = get_mode_katcp(d, TBS_MODE_RAW); if(tr == NULL){ log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "unable to acquire raw mode state"); return KATCP_RESULT_FAIL; } /* Make sure we're programmed */ if(tr->r_fpga != TBS_FPGA_MAPPED){ log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "fpga not programmed"); return KATCP_RESULT_FAIL; } /* Get the register pointer */ te = find_data_avltree(tr->r_registers, WALSH_ARM_REG); if(te == NULL){ log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "register %s not defined", WALSH_ARM_REG); return KATCP_RESULT_FAIL; } /* Get current value of WALSH_ARM_REG */ value = *((uint32_t *)(tr->r_map + te->e_pos_base)); log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "arm ctrl value = %d", (int)value); /* Set MSB=0 to prepare to arm */ *((uint32_t *)(tr->r_map + te->e_pos_base)) = value & 0x7fffffff; msync(tr->r_map, tr->r_map_size, MS_SYNC); /* Grab the first argument, offset in heartbeats */ hb_offset = arg_signed_long_katcp(d, 1); log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "hb_offset=%d", hb_offset); /* Grab the sync time over DSM */ s = dsm_read(DDS_HOST, DSM_ARMAT_VAR, &arm_at_db, &timeStamp); if (s != DSM_SUCCESS) { dsm_error_message(s, "dsm_read()"); log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "dsm_read('%s', '%s'): failed with %d", DDS_HOST, DSM_ARMAT_VAR, s); return KATCP_RESULT_FAIL; } /* Make sure arm_at is not in the past */ clock_gettime(CLOCK_REALTIME, &now); if ((int)arm_at_db <= now.tv_sec){ log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "%0.6f is in the past! It's %d.%06d now", arm_at_db, now.tv_sec, now.tv_nsec); return KATCP_RESULT_FAIL; } /* Print out requested arm time */ log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "arm reqst=%.9f", arm_at_db); /* Add in the HB offset and print */ arm_at_db += ((double)hb_offset) * HB_PERIOD; log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "arming at=%.9f", arm_at_db); /* ... and now we wait, timeout=60 seconds */ clock_gettime(CLOCK_REALTIME, &start); start_db = timespec_to_double(start); now_db = timespec_to_double(now); while (now_db < arm_at_db) { clock_gettime(CLOCK_REALTIME, &now); now_db = timespec_to_double(now); if (now.tv_sec > start.tv_sec+60) { log_message_katcp(d, KATCP_LEVEL_ERROR, NULL, "arm timed out after 60 seconds"); return KATCP_RESULT_FAIL; } } /* Twiddle bits 31 and 29 finally to arm swof and mcnt */ mask = 0xa0000000; *((uint32_t *)(tr->r_map + te->e_pos_base)) = value & ~mask; msync(tr->r_map, tr->r_map_size, MS_SYNC); *((uint32_t *)(tr->r_map + te->e_pos_base)) = value | mask; msync(tr->r_map, tr->r_map_size, MS_SYNC); *((uint32_t *)(tr->r_map + te->e_pos_base)) = value & ~mask; msync(tr->r_map, tr->r_map_size, MS_SYNC); log_message_katcp(d, KATCP_LEVEL_INFO, NULL, "armed at==%.9f", now_db); return KATCP_RESULT_OK; }