void Slic::build_pyramid(const vector<unsigned char>& img, int levels) { #ifdef SLIC_DEBUG unsigned long t1, t2; t1 = now_us(); printf("Slic::build_pyramid "); #endif // build the pyramid for the image data pyramid = vector<vector<unsigned char> >(levels); int n = rows[0] * cols[0] * chan; pyramid[0] = vector<unsigned char>(n); copy(img.begin(), img.begin() + n, pyramid[0].begin()); for (int i = 1; i < levels; ++i) { imresize(pyramid[i], pyramid[i - 1], rows[i - 1], cols[i - 1], rows[i], cols[i]); } // build the pyramid for assignments: assignments_pyramid = vector<vector<int> >(levels); for (int i = 0; i < levels; ++i) { assignments_pyramid[i] = vector<int>(rows[i] * cols[i], 0); } #ifdef SLIC_DEBUG t2 = now_us(); printf("elapsed: %f\n", (t2 - t1) / 1000.0); #endif }
uint16_t sim_tick_counter(void) { if (time_scale) { // microseconds to 16-bit clock ticks return (now_us() / time_scale) US; } return (uint16_t)(ticks % 0xFFFF); }
replication_app_client_base::request_context* replication_app_client_base::create_read_context( uint64_t key_hash, dsn_task_code_t code, dsn_message_t request, ::dsn::task_ptr& callback, read_semantic_t read_semantic, decree snapshot_decree, // only used when ReadSnapshot int reply_hash ) { dsn_msg_options_t opts; dsn_msg_get_options(request, &opts); auto rc = new request_context; rc->request = request; rc->callback_task = callback; rc->is_read = true; rc->partition_index = -1; rc->key_hash = key_hash; rc->read_header.gpid.app_id = _app_id; rc->read_header.gpid.pidx = -1; rc->read_header.code = dsn_task_code_to_string(code); rc->read_header.semantic = read_semantic; rc->read_header.version_decree = snapshot_decree; rc->timeout_timer = nullptr; rc->timeout_ms = opts.timeout_ms; rc->timeout_ts_us = now_us() + opts.timeout_ms * 1000; rc->completed = false; size_t offset = dsn_msg_body_size(request); ::marshall(request, rc->read_header); rc->header_pos = (char*)dsn_msg_rw_ptr(request, offset); return rc; }
uint64_t zmq::clock_t::now_ms () { uint64_t tsc = rdtsc (); // If TSC is not supported, get precise time and chop off the microseconds. if (!tsc) return now_us () / 1000; // If TSC haven't jumped back (in case of migration to a different // CPU core) and if not too much time elapsed since last measurement, // we can return cached time value. if (likely (tsc - last_tsc <= (clock_precision / 2) && tsc >= last_tsc)) return last_time; last_tsc = tsc; last_time = now_us (); return last_time; }
static void print_counts (void) { unsigned long long tmp = now_us(); unsigned time = (unsigned) (tmp / 1000 / 1000); fseek(log_file, 0, SEEK_SET); if (log_fd) ftruncate(log_fd, 0); last_gc = tmp; g_hash_table_foreach(asites, print_counts_cb, (gpointer) time); fflush(log_file); }
/* * Add a new future to the queue */ int push_future(ncm_future_queue_t* queue, u32 jmp, u64 wait_us) { int index; if(queue->length == MAX_FUTURES) { return FUTURE_Q_FULL; } index = (queue->first + queue->length) % MAX_FUTURES; queue->at[index].jmp_address = jmp; queue->at[index].wait_time = wait_us; queue->at[index].expiry = now_us() + wait_us; queue->length++; sort_future_queue(queue); return FUTURE_Q_OK; }
JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *vm, char *options, void *reserved) { int res; char *log_file_path = "/tmp/heapprof.log"; jvm = vm; if (options) { fprintf(stderr, "options=\"%s\"\n", options); if ((strlen(options) > 4) && (strncmp(options, "log=", 4) == 0)) { log_file_path = options + 4; } } last_gc = now_us(); class_filter = (regex_t*) calloc(1, sizeof(regex_t)); res = regcomp(class_filter, "^(bamboo|ostore|org\\.apache|com\\.sleepycat)\\..*", REG_EXTENDED | REG_NOSUB); assert(!res); if (log_file_path) { log_fd = open(log_file_path, O_WRONLY | O_CREAT, 0664); if (!log_fd) { fprintf(stderr, "Could not open log file: %s\n", log_file_path); fflush(stderr); exit(1); } log_file = fdopen(log_fd, "w"); } else { log_file = stderr; log_fd = 0; } classes = g_hash_table_new(g_direct_hash, g_direct_equal); init_classes(); asites = g_hash_table_new(asite_key_hash, asite_key_equals); methods = g_hash_table_new(g_direct_hash, g_direct_equal); objects = g_hash_table_new(g_direct_hash, g_direct_equal); arenas = g_hash_table_new(g_direct_hash, g_direct_equal); res = (*jvm)->GetEnv(jvm, (void**)&jvmpi, JVMPI_VERSION_1); if (res < 0) return JNI_ERR; jvmpi->NotifyEvent = notify_event; lock = jvmpi->RawMonitorCreate("_lock"); jvmpi->RawMonitorEnter(lock); if (jvmpi->EnableEvent(JVMPI_EVENT_GC_START, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_GC_FINISH, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_OBJ_ALLOC, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_OBJ_MOVE, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_OBJ_FREE, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_DELETE_ARENA, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_CLASS_LOAD, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_CLASS_UNLOAD, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_METHOD_ENTRY, NULL) != JVMPI_SUCCESS) return JNI_ERR; if (jvmpi->EnableEvent(JVMPI_EVENT_METHOD_EXIT, NULL) != JVMPI_SUCCESS) return JNI_ERR; jvmpi->RawMonitorExit(lock); return JNI_OK; }
//! returns time in milliseconds double now_ms() { return double(now_us())/1000.0f; }
void Slic::adjust_centers() { #ifdef SLIC_DEBUG printf("Slic::adjust_centers "); unsigned long t1, t2; t1 = now_us(); #endif vector<unsigned char>* data = &pyramid[num_levels - 1]; int cols_ = cols[num_levels - 1]; //int rows_ = rows[num_levels-1]; for (int k = 0; k < K; ++k) { int x = centers[k].x; int y = centers[k].y; int i = 0; int min_val = INT_MAX; int best_x = x; int best_y = y; // boundary checks are not implemented because centers should be // initialized away from the image boundary in the first place!! if (chan == 3) { // for rgb images for (int yy = y - 1; yy <= (y + 1); yy++) { for (int xx = x - 1; xx <= (x + 1); xx++) { float grad = 0.0; for (int u = 0; u<3; ++u) { int i_bot = linear_index(yy + 1, xx, u, cols_, 3); int i_top = linear_index(yy - 1, xx, u, cols_, 3); int i_lef = linear_index(yy, xx - 1, u, cols_, 3); int i_rig = linear_index(yy, xx + 1, u, cols_, 3); float v = (int)((*data)[i_bot] - (int)(*data)[i_top]); float h = (int)((*data)[i_lef] - (int)(*data)[i_rig]); grad += v*v + h*h; } if (grad < min_val) { min_val = grad; best_x = xx; best_y = yy; } i++; } } centers[k].x = best_x; centers[k].y = best_y; } else { // for grayscale images for (int yy = y - 1; yy <= (y + 1); yy++) { for (int xx = x - 1; xx <= (x + 1); xx++) { float grad = 0.0; int i_bot = linear_index(yy + 1, xx, cols_); int i_top = linear_index(yy - 1, xx, cols_); int i_lef = linear_index(yy, xx - 1, cols_); int i_rig = linear_index(yy, xx + 1, cols_); float v = (int)((*data)[i_bot] - (int)(*data)[i_top]); float h = (int)((*data)[i_lef] - (int)(*data)[i_rig]); grad += v*v + h*h; if (grad < min_val) { min_val = grad; best_x = xx; best_y = yy; } i++; } } centers[k].x = best_x; centers[k].y = best_y; } } #ifdef SLIC_DEBUG t2 = now_us(); printf("elapsed: %f\n", double(t2 - t1) / 1000.0); #endif }
double now_ms() { return (now_us()/1000.0); }
void Slic::upsample_assignments(int level) { #ifdef SLIC_DEBUG unsigned long t1, t2; t1 = now_us(); printf("Slic::upsample_assignments "); #endif // level is the current level. if (level == 0) { printf("Should not be called with this argument!\n"); } vector<int> *assignments = &assignments_pyramid[level]; vector<int> *assignments_out = &assignments_pyramid[level - 1]; vector<unsigned char> *data = &pyramid[level - 1]; int rows_ = rows[level]; int cols_ = cols[level]; int rows_big = rows[level - 1]; int cols_big = cols[level - 1]; float EPS = 1e-4; float xx, yy; // location and flow directions int nx0, nx1, ny0, ny1; // neighboring location int i1, i2, i3, i4, i_out, i_; int ass1, ass2, ass3, ass4, temp; int d1, d2, d3, d4; float scale = (float(rows_big) / float(rows_) + float(cols_big) / float(cols_)) / 2.0; float antiscale = 1.0 / scale; int Mp = get_distance_weight(level - 1); for (size_t r = 0; r < (size_t)rows_big; ++r) { for (size_t c = 0; c < (size_t)cols_big; ++c) { xx = (float)c*antiscale; yy = (float)r*antiscale; nx0 = floor(xx + EPS); nx1 = ceil(xx + EPS); ny0 = floor(yy + EPS); ny1 = ceil(yy + EPS); nx0 = min(max(nx0, 0), cols_ - 1); nx1 = min(max(nx1, 0), cols_ - 1); ny0 = min(max(ny0, 0), rows_ - 1); ny1 = min(max(ny1, 0), rows_ - 1); i1 = linear_index(ny0, nx0, cols_); i2 = linear_index(ny1, nx0, cols_); i3 = linear_index(ny0, nx1, cols_); i4 = linear_index(ny1, nx1, cols_); i_out = linear_index(r, c, cols_big); ass1 = (*assignments)[i1]; ass2 = (*assignments)[i2]; ass3 = (*assignments)[i3]; ass4 = (*assignments)[i4]; if ((ass1 == ass2) && (ass2 == ass3) && (ass3 == ass4)) { // we are on the interior of the superpixel (whew) (*assignments_out)[i_out] = ass1; } else { d1 = d2 = d3 = d4 = 0; for (int k = 0; k < chan; ++k) { i_ = linear_index(r, c, k, cols_big, chan); temp = (*data)[i_] - centers[ass1].rgb[k]; d1 += temp*temp; temp = (*data)[i_] - centers[ass2].rgb[k]; d2 += temp*temp; temp = (*data)[i_] - centers[ass3].rgb[k]; d3 += temp*temp; temp = (*data)[i_] - centers[ass4].rgb[k]; d4 += temp*temp; } temp = (r - centers[ass1].y)*(r - centers[ass1].y) + (c - centers[ass1].x)*(c - centers[ass1].x); d1 += Mp*temp; temp = (r - centers[ass2].y)*(r - centers[ass2].y) + (c - centers[ass2].x)*(c - centers[ass2].x); d2 += Mp*temp; temp = (r - centers[ass3].y)*(r - centers[ass3].y) + (c - centers[ass3].x)*(c - centers[ass3].x); d3 += Mp*temp; temp = (r - centers[ass4].y)*(r - centers[ass4].y) + (c - centers[ass4].x)*(c - centers[ass4].x); d4 += Mp*temp; if ((d1 <= d2) && (d1 <= d2) && (d1 <= d3)) { (*assignments_out)[i_out] = ass1; } else if ((d2 <= d1) && (d2 <= d3) && (d2 <= d4)) { (*assignments_out)[i_out] = ass2; } else if ((d3 <= d1) && (d3 <= d2) && (d3 <= d4)) { (*assignments_out)[i_out] = ass3; } else { (*assignments_out)[i_out] = ass4; } } } } #ifdef SLIC_DEBUG t2 = now_us(); printf("elapsed: %f\n", (t2 - t1) / 1000.0); #endif }
void Slic::ensure_contiguity(int level) { #ifdef SLIC_DEBUG unsigned long t1, t2; t1 = now_us(); printf("Slic::ensure_contiguity "); #endif int rows_ = rows[level]; int cols_ = cols[level]; int search_region_x_ = search_region_x[level]; int search_region_y_ = search_region_y[level]; int n = rows_*cols_; vector<int> *assignments = &assignments_pyramid[level]; vector<bool> mask = vector<bool>(n); vector<bool> visited = vector<bool>(n); for (int k = 0; k<K; ++k) { int xc = centers[k].x; int yc = centers[k].y; if ((xc < 0) || (yc < 0)) { continue; } int xlo = max(0, xc - search_region_x_); int xhi = min(cols_ - 1, xc + search_region_x_); int ylo = max(0, yc - search_region_y_); int yhi = min(rows_ - 1, yc + search_region_y_); // the actual extents of the superpixel. int xlosp = xc; int xhisp = xc; int ylosp = yc; int yhisp = yc; // by definition, no need to check outside search region. int nums = 0; int origin_x = -1; int origin_y = -1; int dist_to_origin = INT_MAX; // first check if the centroid (or its 4 nearest neighbors belong to // this superpixel. if yes, great -- we will start at this location. // otherwise we need to find a point that belongs to this superpixel // while being close to the center (this would be necessary if the // superpixel is U-shaped) int i = linear_index(yc, xc, cols_); if ((*assignments)[i] == k) { // yay! origin_x = xc; origin_y = yc; nums = 1; } else { for (int y = ylo; y <= yhi; ++y) { int ddy = (y - yc)*(y - yc); if (ddy > dist_to_origin) { continue; } for (int x = xlo; x <= xhi; ++x) { int i = linear_index(y, x, cols_); if ((*assignments)[i] == k) { int d = ddy + (x - xc)*(x - xc); if (d < dist_to_origin) { dist_to_origin = d; origin_x = x; origin_y = y; } } } } } for (int y = ylo; y <= yhi; ++y) { for (int x = xlo; x <= xhi; ++x) { int i = linear_index(y, x, cols_); visited[i] = false; mask[i] = ((*assignments)[i] == k); if (mask[i]) { nums++; xlosp = min(xlosp, x); ylosp = min(ylosp, y); xhisp = max(xhisp, x); yhisp = max(yhisp, y); } } } if (nums == 0) { //printf("No pixels associated with this superpixel"); continue; // this is a can of worms, but it is handled later } // we know the point closest to center that belongs to this cluster, // so start here and try to 'unset' all the 'set' pixels in the mask. try_fill(origin_x, origin_y, linear_index(origin_y, origin_x, cols_), mask, visited, xlosp, xhisp, ylosp, yhisp, cols_); int num_left = 0; for (int y = ylosp; y <= yhisp; ++y) { for (int x = xlosp; x <= xhisp; ++x) { int i = linear_index(y, x, cols_); if (mask[i]) { num_left += 1; break; } } if (num_left > 0) { break; } } if (num_left > 0) { // assign the remaining pixels to neighboring clusters reassign_neighbors(mask, k, xlosp, xhisp, ylosp, yhisp, level); } } #ifdef SLIC_DEBUG t2 = now_us(); printf("elapsed: %f\n", double(t2 - t1) / 1000.0); #endif }
// SEEMS OK. void Slic::update(int level) { #ifdef SLIC_DEBUG printf("Slic::update "); unsigned long t1, t2; t1 = now_us(); #endif int rows_ = rows[level]; int cols_ = cols[level]; int search_region_x_ = search_region_x[level]; int search_region_y_ = search_region_y[level]; //int n = rows_*cols_; vector<unsigned char> *data = &pyramid[level]; vector<int> *assignments = &assignments_pyramid[level]; for (int k = 0; k<K; ++k) { int xc = centers[k].x; int yc = centers[k].y; int xlo = max(0, xc - search_region_x_); int xhi = min(cols_ - 1, xc + search_region_x_); int ylo = max(0, yc - search_region_y_); int yhi = min(rows_ - 1, yc + search_region_y_); if (chan == 3) { int r = 0; int g = 0; int b = 0; int px = 0; int py = 0; int nums = 0; for (int y = ylo; y <= yhi; ++y) { for (int x = xlo; x <= xhi; ++x) { int i = linear_index(y, x, cols_); if ((*assignments)[i] == k) { int q = linear_index(y, x, 0, cols_, 3); r += (int)(*data)[q + 0]; g += (int)(*data)[q + 1]; b += (int)(*data)[q + 2]; px += x; py += y; nums++; } } } if (nums>0) { centers[k].x = px / nums; centers[k].y = py / nums; centers[k].rgb[0] = (unsigned char)(r / nums); centers[k].rgb[1] = (unsigned char)(g / nums); centers[k].rgb[2] = (unsigned char)(b / nums); centers[k].valid = 1; } else { centers[k].valid = 0; } } else { int r = 0; int px = 0; int py = 0; int nums = 0; for (int y = ylo; y <= yhi; ++y) { for (int x = xlo; x <= xhi; ++x) { int i = linear_index(y, x, cols_); if ((*assignments)[i] == k) { nums++; r += (int)(*data)[i]; px += x; py += y; } } } if (nums>0) { centers[k].x = px / nums; centers[k].y = py / nums; centers[k].gray = (unsigned char)(r / nums); centers[k].valid = 1; } else { centers[k].valid = 0; } } } #ifdef SLIC_DEBUG t2 = now_us(); printf("elapsed: %f\n", double(t2 - t1) / 1000.0); #endif }
void Slic::assign(int level) { #ifdef SLIC_DEBUG printf("Slic::assign "); unsigned long t1, t2; t1 = now_us(); #endif int rows_ = rows[level]; int cols_ = cols[level]; int search_region_x_ = search_region_x[level]; int search_region_y_ = search_region_y[level]; int n = rows_*cols_; vector<unsigned char> *data = &pyramid[level]; vector<int> *assignments = &assignments_pyramid[level]; int Mp = get_distance_weight(level); for (int i = 0; i < n; ++i) { (*assignments)[i] = 0; } for (int i = 0; i<n; ++i) d[i] = INT_MAX; for (int k = 0; k<K; ++k) { int xc = centers[k].x; int yc = centers[k].y; int xlo = max(0, xc - search_region_x_); int xhi = min(cols_ - 1, xc + search_region_x_); int ylo = max(0, yc - search_region_y_); int yhi = min(rows_ - 1, yc + search_region_y_); if (chan == 3) { int r = centers[k].rgb[0]; int g = centers[k].rgb[1]; int b = centers[k].rgb[2]; for (int y = ylo; y <= yhi; ++y) { int ddy = Mp*(y - yc)*(y - yc); for (int x = xlo; x <= xhi; ++x) { int i = linear_index(y, x, cols_); if (ddy >= d[i]) continue; int dist = Mp*(x - xc)*(x - xc) + ddy; if (dist >= d[i]) continue; int q = linear_index(y, x, 0, cols_, 3); int temp1 = ((int)(*data)[q + 0] - r); int temp2 = ((int)(*data)[q + 1] - g); int temp3 = ((int)(*data)[q + 2] - b); dist += (temp1*temp1 + temp2*temp2 + temp3*temp3); if (dist < d[i]) { d[i] = dist; (*assignments)[i] = k; } } } } else { int g = centers[k].gray; for (int y = ylo; y <= yhi; ++y) { int ddy = Mp*(y - yc)*(y - yc); for (int x = xlo; x <= xhi; ++x) { int i = linear_index(y, x, cols_); if (ddy >= d[i]) continue; int dist = Mp*(x - xc)*(x - xc) + ddy; if (dist >= d[i]) continue; int temp = ((int)(*data)[i] - g); dist += temp*temp; if (dist < d[i]) { d[i] = dist; (*assignments)[i] = k; } } } } } #ifdef SLIC_DEBUG t2 = now_us(); printf("elapsed: %f\n", double(t2 - t1) / 1000.0); #endif }
struct worker * worker_start(EV_P_ struct session *session) { struct worker *w; pid_t pid; int stdin_fds [2] = {-1, -1}; int stdout_fds[2] = {-1, -1}; int stderr_fds[2] = {-1, -1}; int msgin_fds [2] = {-1, -1}; int msgout_fds[2] = {-1, -1}; #if WORKER_TIMINGS uint64_t _start = now_us(); #endif if ((w = calloc(1, sizeof(*w))) == NULL) { goto fail; } w->session = session; writeq_init(&w->stdin_writeq); writeq_init(&w->msgin_writeq); if (pipe(stdin_fds ) < 0 || pipe(stdout_fds) < 0 || pipe(stderr_fds) < 0 || pipe(msgin_fds ) < 0 || pipe(msgout_fds) < 0) { LOG_ERRNO("pipe()"); goto fail; } maxfd_update(stdin_fds [0]); maxfd_update(stdin_fds [1]); maxfd_update(stdout_fds[0]); maxfd_update(stdout_fds[1]); maxfd_update(stderr_fds[0]); maxfd_update(stderr_fds[1]); maxfd_update(msgin_fds [0]); maxfd_update(msgin_fds [1]); maxfd_update(msgout_fds[0]); maxfd_update(msgout_fds[1]); pid = fork(); if (pid < 0) { LOG_ERRNO("fork()"); goto fail; } if (pid == 0) { /* child. */ if (dup2(stdin_fds [0], 0) < 0 || dup2(stdout_fds[1], 1) < 0 || dup2(stderr_fds[1], 2) < 0 || dup2(msgin_fds [0], 3) < 0 || dup2(msgout_fds[1], 4) < 0) { exit(EXIT_FAILURE); } maxfd_closeall(5); pyenv_child_after_fork(); exit(EXIT_SUCCESS); } else { /* parent. */ close(stdin_fds [0]); close(stdout_fds[1]); close(stderr_fds[1]); close(msgin_fds [0]); close(msgout_fds[1]); set_fd_nonblocking(stdin_fds [1]); set_fd_nonblocking(stdout_fds[0]); set_fd_nonblocking(stderr_fds[0]); set_fd_nonblocking(msgin_fds [1]); set_fd_nonblocking(msgout_fds[0]); ev_child_init(&w->child_watcher, worker_exited_cb, pid, 0); ev_child_start(EV_A_ &w->child_watcher); ev_io_init(&w->stdin_w , worker_write_stdin_cb, stdin_fds [1], EV_WRITE); ev_io_init(&w->stdout_w, worker_read_stdout_cb, stdout_fds[0], EV_READ); ev_io_init(&w->stderr_w, worker_read_stderr_cb, stderr_fds[0], EV_READ); ev_io_init(&w->msgin_w , worker_write_msgin_cb, msgin_fds [1], EV_WRITE); ev_io_init(&w->msgout_w, worker_read_msgout_cb, msgout_fds[0], EV_READ); ev_io_start(EV_A_ &w->stdout_w); ev_io_start(EV_A_ &w->stderr_w); ev_io_start(EV_A_ &w->msgout_w); LOGF(3, "=== %d: worker started, fds=[%d, %d, %d, %d, %d]\n", worker_pid(w), stdin_fds[1], stdout_fds[0], stderr_fds[0], msgin_fds[1], msgout_fds[0]); w->f_alive = true; } #if WORKER_TIMINGS worker_start_time += now_us() - _start; worker_start_calls++; #endif return w; fail: close(stdin_fds [0]); close(stdin_fds [1]); close(stdout_fds[0]); close(stdout_fds[1]); close(stderr_fds[0]); close(stderr_fds[1]); close(msgin_fds [0]); close(msgin_fds [1]); close(msgout_fds[0]); close(msgout_fds[1]); free(w); return NULL; }
int main(int argc, const char * argv[]) { #ifdef ICMP // get the ping socket int ping = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP); { // Drop privelege immediately errno_t ping_errno = errno; setuid(getuid()); if (0 > ping) { errno = ping_errno; DIE(EX_OSERR, "open ping socket"); } LOG(2, "ping socket: %d", ping); } #endif // ICMP #ifdef DNS // get the dns socket int dns = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); { if (0 > dns) { DIE(EX_OSERR, "open dns socket"); } LOG(2, "dns socket: %d", dns); } #endif // DNS struct pollfd fds[2] = { #ifdef ICMP { ping, POLLIN, 0 } #endif // ICMP #ifdef BOTH , #endif // BOTH #ifdef DNS { dns, POLLIN, 0 } #endif // DNS }; int fd_count = 0; #ifdef ICMP int ping_index = fd_count++; #endif // ICMP #ifdef DNS int dns_index = fd_count++; #endif // DNS // process arguments struct opts_t opts; get_opts(argc, argv, &opts); const struct addrinfo * addr = get_one_host(opts.target); #ifdef ICMP int sequence = -1; struct icmp icmp_template; construct_icmp_template(&icmp_template); #endif // ICMP #ifdef DNS void * dns_template; size_t template_size = construct_dns_template(&dns_template, opts.query); LOG(3, "template: "); if (verbose() >= 3) { fputbuf(stderr, dns_template, template_size);fputc('\n', stderr); } #endif // DNS // initialize the prng srandomdev(); int count = -1; while (1) { ++count; #ifdef ICMP struct icmp icmp_message; size_t icmp_message_size; icmp_message_size = construct_icmp(&icmp_template, &icmp_message, ++sequence); ssize_t icmp_sent = sendto(ping, (const void *)&icmp_message, icmp_message_size, 0, addr->ai_addr, addr->ai_addrlen); if (0 > icmp_sent) DIE(EX_OSERR, "sendto ping"); LOG(1, "ping sent %d bytes", icmp_sent); long icmp_send_time = now_us(); long icmp_recv_time = -1; #endif // ICMP #ifdef DNS void * dns_message; size_t dns_message_size; short dns_id = (short)random(); dns_message_size = construct_dns(dns_template, template_size, &dns_message, dns_id); ssize_t dns_sent = sendto(dns, (const void *)dns_message, dns_message_size, 0, addr->ai_addr, addr->ai_addrlen); LOG(1, "dns sent %d bytes", dns_sent); if (verbose() >= 3) { fputbuf(stderr, dns_message, dns_message_size);fputc('\n', stderr); } if (0 > dns_sent) DIE(EX_OSERR, "sendto dns"); long dns_send_time = now_us(); long dns_recv_time = -1; #endif // DNS long ttd_ms = now_ms() + opts.period_ms; int poll_time = (int)opts.period_ms; int ret; while ((ret = poll(fds, fd_count, poll_time))) { if (0 > ret) DIE(EX_OSERR, "poll"); #ifdef ICMP if (fds[ping_index].revents & POLLERR) { int error = 0; socklen_t errlen = sizeof(error); if (0 < getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen)) DIE(EX_OSERR, "getsockopt on ping while handling POLLERR"); errno = error; DIE(EX_OSERR, "POLLERR on ping"); } if (fds[ping_index].revents & POLLIN) { icmp_recv_time = process_ping(fds[ping_index].fd, sequence); } #endif // ICMP #ifdef DNS if (fds[dns_index].revents & POLLERR) { int error = 0; socklen_t errlen = sizeof(error); if (0 < getsockopt(fds[1].fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen)) DIE(EX_OSERR, "getsockopt on dns while handling POLLERR"); errno = error; DIE(EX_OSERR, "POLLERR on dns"); } if (fds[dns_index].revents & POLLIN) { dns_recv_time = process_dns(fds[dns_index].fd, dns_id); } #endif // DNS poll_time = (int)(ttd_ms - now_ms()); if (poll_time < 0) break; } LOG(1, "poll period %d ended", count); #ifdef ICMP REPORT("icmp", icmp_send_time, icmp_recv_time, sequence); #endif // ICMP #ifdef DNS REPORT("dns", dns_send_time, dns_recv_time, dns_id); #endif // DNS } return 0; }
zmq::clock_t::clock_t () : last_tsc (rdtsc ()), last_time (now_us ()) { }