int object_stream::read(msgpack::object* obj, std::auto_ptr<msgpack::zone>* zone, double timeout_sec) { clock_time start = get_clock_time(); while(true) { if(unpacker.execute()) { *obj = unpacker.data(); zone->reset( unpacker.release_zone() ); unpacker.reset(); return 1; } unpacker.reserve_buffer(1024); ssize_t rl; while(true) { rl = ::read(iofd, unpacker.buffer(), unpacker.buffer_capacity()); if(rl > 0) break; if(rl == 0) { return -1; } if(errno == EINTR) { continue; } if(timeout_sec > 0.0 && timeout_sec < (double)(get_clock_time() - start)){ throw rpc_timeout_error("timeout"); } if(errno == EAGAIN) { continue; } return -1; } unpacker.buffer_consumed(rl); } }
int main(int argc, char* argv[]) { int array1[LENGTH] = {0}; int array2[LENGTH] = {0}; produce_array(array1, LENGTH); produce_array(array2, LENGTH); printf("Bubble time:"); get_clock_time(bubble_sort, array1, LENGTH); printf("Quick time :") ; get_clock_time(quick_sort, array2, LENGTH); return 0; }
//@broadcast int graph_serv::update_index(){ if(not a_.is_standalone()){ throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("manual mix is available only in standalone mode.")); } clock_time start = get_clock_time(); g_.get_model()->update_index(); std::string diff; g_.get_model()->get_diff(diff); g_.get_model()->set_mixed_and_clear_diff(diff); clock_time end = get_clock_time(); LOG(WARNING) << "mix done manually and locally; in " << (double)(end - start) << " secs."; return 0; }
bool rw_mutex::impl::write_lock(double sec) { #if defined(__linux__) || defined(__sparcv8) || defined(__sparcv9) if (!valid) return false; #ifdef JUBATUS_UTIL_CONCURRENT_RWMUTEX_ERRORCHECK assert(holders_lk.lock()==true); assert(holders.count(thread::id())==0); #endif bool result; if (sec<1e-9) { result = pthread_rwlock_trywrlock(&lk)==0; } else { timespec end=to_timespec(get_clock_time()+sec); result = pthread_rwlock_timedwrlock(&lk, &end)==0; } #ifdef JUBATUS_UTIL_CONCURRENT_RWMUTEX_ERRORCHECK if (result) holders.insert(thread::id()); assert(holders_lk.unlock()==true); #endif return result; #else return false; #endif }
proxy_common::status_type proxy_common::get_status() { update_request_counter(); status_type status; string_map& data = status[get_proxy_identifier(a_)]; const clock_time ct = get_clock_time(); data["clock_time"] = jubatus::util::lang::lexical_cast<std::string>(ct.sec); data["start_time"] = jubatus::util::lang::lexical_cast<std::string>(start_time_.sec); data["uptime"] = jubatus::util::lang::lexical_cast<std::string>((ct - start_time_).sec); common::machine_status_t mt; common::get_machine_status(mt); data["VIRT"] = jubatus::util::lang::lexical_cast<std::string>(mt.vm_size); data["RSS"] = jubatus::util::lang::lexical_cast<std::string>(mt.vm_resident); data["SHR"] = jubatus::util::lang::lexical_cast<std::string>(mt.vm_share); data["VERSION"] = JUBATUS_VERSION; data["PROGNAME"] = a_.program_name; data["pid"] = jubatus::util::lang::lexical_cast<std::string>(getpid()); data["user"] = jubatus::server::common::get_user_name(); data["threadnum"] = jubatus::util::lang::lexical_cast<std::string>(a_.threadnum); data["timeout"] = jubatus::util::lang::lexical_cast<std::string>(a_.timeout); data["logdir"] = a_.logdir; data["log_config"] = a_.log_config; data["zookeeper"] = a_.z; data["connected_zookeeper"] = zk_->get_connected_host_and_port(); data["zookeeper_timeout"] = jubatus::util::lang::lexical_cast<std::string>(a_.zookeeper_timeout); data["interconnect_timeout"] = jubatus::util::lang::lexical_cast<std::string> (a_.interconnect_timeout); data["session_pool_expire"] = jubatus::util::lang::lexical_cast<std::string>(a_.session_pool_expire); data["session_pool_size"] = jubatus::util::lang::lexical_cast<std::string>(a_.session_pool_size); data["request_count"] = jubatus::util::lang::lexical_cast<std::string>(request_counter_); data["forward_count"] = jubatus::util::lang::lexical_cast<std::string>(forward_counter_); return status; }
static scalar_t calc_time_step() { scalar_t cur_time, time_step; cur_time = get_clock_time( ); time_step = cur_time - clock_time; if (time_step < EPS ) { time_step = EPS; } clock_time = cur_time; return time_step; }
rpc_mclient::rpc_mclient(const vector<std::pair<string, uint16_t> >& hosts, int timeout_sec): hosts_(hosts), timeout_sec_(timeout_sec), start_(get_clock_time()), evbase_(::event_base_new()) { if (!evbase_) throw std::bad_alloc(); connect_async_(); create_fd_event_(); }
int object_stream::write(const void* data, size_t size, double timeout_sec) { const char* p = static_cast<const char*>(data); const char* const pend = p + size; clock_time start = get_clock_time(); while(p < pend) { ssize_t rl = ::write(iofd, p, pend-p); if(rl <= 0) { if(rl == 0) { return -1; } if(errno == EINTR) { continue; } if(timeout_sec < (double)(get_clock_time() - start)){ throw rpc_timeout_error("timeout"); } if(errno == EAGAIN) { continue; } return -1; } p += rl; } return size; }
rpc_mclient::rpc_mclient(const vector<std::pair<string, int> >& hosts, int timeout_sec): timeout_sec_(timeout_sec), start_(get_clock_time()), evbase_(::event_base_new()) { if (!evbase_) throw std::bad_alloc(); for(size_t i=0; i<hosts.size(); ++i){ hosts_.push_back(hosts[i]); } connect_async_(); create_fd_event_(); }
proxy_common::proxy_common(const proxy_argv& a) : a_(a), request_counter_(0), forward_counter_(0), start_time_(get_clock_time()) { common::prepare_signal_handling(); zk_.reset(common::create_lock_service( "cached_zk", a.z, a.zookeeper_timeout, make_logfile_name(a))); register_lock_service(zk_); jubatus::server::common::prepare_jubatus(*zk_, a_.type, ""); if (a.daemon) { daemonize_process(a.logdir); } }
void stat::push(const std::string& key, double val) { { clock_time ct = get_clock_time(); window_.push_back(make_pair((uint64_t) ct, make_pair(key, val))); stats_[key].add(val); } while (window_.size() > window_size_) { string key = window_.front().second.first; double val = window_.front().second.second; stat_val& st = stats_[key]; window_.pop_front(); st.rem(val, key, *this); if (st.n_ == 0) { stats_.erase(key); } } }
explicit server_helper(const server_argv& a, bool use_cht = false) : impl_(a), start_time_(get_clock_time()), use_cht_(use_cht) { impl_.prepare_for_start(a, use_cht); server_.reset(new Server(a, impl_.zk())); impl_.get_config_lock(a, 3); try { server_->set_config(get_conf(a)); } catch (const core::common::jsonconfig::cast_check_error& e) { core::common::config_exception config_error; const core::common::jsonconfig::config_error_list& errors = e.errors(); for (core::common::jsonconfig::config_error_list::const_iterator it = errors.begin(), end = errors.end(); it != end; ++it) { config_error << core::common::exception::error_message((*it)->what()); } // send error message to caller throw JUBATUS_EXCEPTION(config_error); } catch (const jubatus::util::lang::parse_error& e) { // exit immediately on JSON parse error with exit-code 1 std::string msg = std::string("syntax error in configuration: ") + (a.is_standalone() ? a.configpath : std::string("<zookeeper>")) + ":" + jubatus::util::lang::lexical_cast<std::string>(e.lineno()) + ":" + jubatus::util::lang::lexical_cast<std::string>(e.pos()) + " " + e.msg(); LOG(ERROR) << msg; exit(1); } try { // standalone only, is it desirable? if (a.is_standalone() && !a.modelpath.empty()) { server_->load_file(a.modelpath); } } catch (const std::runtime_error& e) { exit(1); } }
int start(common::mprpc::rpc_server& serv) { const server_argv& a = server_->argv(); try { serv.listen(a.port, a.bind_address); LOG(INFO) << "start listening at port " << a.port; start_time_ = get_clock_time(); serv.start(a.threadnum, true); // RPC server started, then register group membership impl_.prepare_for_run(a, use_cht_); LOG(INFO) << common::get_program_name() << " RPC server startup"; // Stop RPC server when terminate signal is sent common::set_action_on_term( jubatus::util::lang::bind( &server_helper::stop, this, jubatus::util::lang::ref(serv))); if (!a.is_standalone()) { // Start mixer and register active membership server_->get_mixer()->start(); } // wait for termination serv.join(); return 0; } catch (const mp::system_error& e) { if (e.code == EADDRINUSE) { LOG(FATAL) << "server failed to start: any process using port " << a.port << "?"; } else { LOG(FATAL) << "server failed to start: " << e.what(); } } catch (jubatus::core::common::exception::jubatus_exception&) { throw; } catch (const std::exception& e) { LOG(FATAL) << "server failed to start: " << e.what(); } return -1; }
void update_ui_snow( scalar_t time_step, bool_t windy ) { vector2d_t *v, f; point2d_t *pt; scalar_t size; scalar_t dist_from_push, p_dist; vector2d_t push_vector; int i; scalar_t push_timestep, time; float grav_x, grav_y; time = get_clock_time(); push_vector.x = 0; push_vector.y = 0; push_timestep = 0; #ifdef TARGET_OS_IPHONE_ winsys_get_gravity(&grav_x, &grav_y); #else // No gravity (yet) on Windows/Android grav_x = 0.0; grav_y = -1.0; #endif if ( push_position_initialized ) { push_vector.x = push_position.x - last_push_position.x; push_vector.y = push_position.y - last_push_position.y; push_timestep = time - last_update_time; } last_push_position = push_position; last_update_time = time; for ( i=0; i<num_particles; i++) { pt = &particles[i].pt; v = &particles[i].vel; size = particles[i].size; f.x = 0; f.y = 0; /* Mouse push and gravity */ dist_from_push = (pow((pt->x - push_position.x), 2) + pow((pt->y - push_position.y), 2)); if ( push_timestep > 0 ) { f.x = PUSH_FACTOR * push_vector.x / push_timestep; f.y = PUSH_FACTOR * push_vector.y / push_timestep; f.x = min( MAX_PUSH_FORCE, f.x ); f.x = max( -MAX_PUSH_FORCE, f.x ); f.y = min( MAX_PUSH_FORCE, f.y ); f.y = max( -MAX_PUSH_FORCE, f.y ); f.x *= 1.0/(PUSH_DIST_DECAY*dist_from_push + 1) * size/PARTICLE_SIZE_RANGE; f.y *= 1.0/(PUSH_DIST_DECAY*dist_from_push + 1) * size/PARTICLE_SIZE_RANGE; } /* Update velocity */ v->x += ( f.x + GRAVITY_FACTOR * grav_x + ( windy ? WIND_FORCE : 0.0 ) - v->x * AIR_DRAG ) * time_step; v->y += ( f.y + GRAVITY_FACTOR * grav_y - v->y * AIR_DRAG ) * time_step; /* Update position */ pt->x += v->x * time_step * ( size / PARTICLE_SIZE_RANGE ); pt->y += v->y * time_step * ( size / PARTICLE_SIZE_RANGE ); if ( pt->x < 0 ) { pt->x = 1; } else if ( pt->x > 1 ) { pt->x = 0.0; } } /* Kill off & regenerate particles */ for (i=0; i<num_particles; i++) { particle_t *p = &particles[i]; if (p->pt.y < -0.05) { /* If we have an excess of particles, kill off with 50% probability */ if ( num_particles > orig_num_particles && frand() > 0.5 ) { /* Delete the particle */ *p = particles[num_particles-1]; num_particles -= 1; } else { p->pt.x = frand(); p->pt.y = 1+frand()*BASE_VELOCITY; p_dist = frand(); p->size = PARTICLE_MIN_SIZE + ( 1.0 - p_dist ) * PARTICLE_SIZE_RANGE; p->vel.x = 0; p->vel.y = -BASE_VELOCITY-p_dist*VELOCITY_RANGE; } } } if ( time_step < PUSH_DECAY_TIME_CONSTANT ) { push_vector.x *= 1.0 - time_step/PUSH_DECAY_TIME_CONSTANT; push_vector.y *= 1.0 - time_step/PUSH_DECAY_TIME_CONSTANT; } else { push_vector.x = 0.0; push_vector.y = 0.0; } }
std::map<std::string, status_t> get_status() const { std::map<std::string, status_t> status; const server_argv& a = server_->argv(); status_t& data = status[get_server_identifier(a)]; const clock_time ct = get_clock_time(); data["clock_time"] = jubatus::util::lang::lexical_cast<std::string>(ct.sec); data["start_time"] = jubatus::util::lang::lexical_cast<std::string>(start_time_.sec); data["uptime"] = jubatus::util::lang::lexical_cast<std::string>((ct - start_time_).sec); common::machine_status_t mt; common::get_machine_status(mt); data["VIRT"] = jubatus::util::lang::lexical_cast<std::string>(mt.vm_size); data["RSS"] = jubatus::util::lang::lexical_cast<std::string>(mt.vm_resident); data["SHR"] = jubatus::util::lang::lexical_cast<std::string>(mt.vm_share); data["timeout"] = jubatus::util::lang::lexical_cast<std::string>(a.timeout); data["threadnum"] = jubatus::util::lang::lexical_cast<std::string>(a.threadnum); data["datadir"] = a.datadir; data["is_standalone"] = jubatus::util::lang::lexical_cast<std::string>( a.is_standalone()); data["VERSION"] = JUBATUS_VERSION; data["PROGNAME"] = a.program_name; data["type"] = a.type; data["logdir"] = a.logdir; data["log_config"] = a.log_config; std::string configpath; if (a.is_standalone()) { configpath = a.configpath; } else { #ifdef HAVE_ZOOKEEPER_H // return zookeeper node name jubatus::server::common::build_config_path(configpath, a.type, a.name); #endif } data["configpath"] = configpath; data["pid"] = jubatus::util::lang::lexical_cast<std::string>(getpid()); data["user"] = jubatus::server::common::get_user_name(); data["update_count"] = jubatus::util::lang::lexical_cast<std::string>( server_->update_count()); data["last_saved"] = jubatus::util::lang::lexical_cast<std::string> (server_->last_saved_sec()); data["last_saved_path"] = server_->last_saved_path(); data["last_loaded"] = jubatus::util::lang::lexical_cast<std::string> (server_->last_loaded_sec()); data["last_loaded_path"] = server_->last_loaded_path(); server_->get_status(data); // distributed mode only if (!a.is_standalone()) { data["zk"] = a.z; data["name"] = a.name; data["interval_sec"] = jubatus::util::lang::lexical_cast<std::string>(a.interval_sec); data["interval_count"] = jubatus::util::lang::lexical_cast<std::string>( a.interval_count); data["zookeeper_timeout"] = jubatus::util::lang::lexical_cast<std::string>(a.zookeeper_timeout); data["interconnect_timeout"] = jubatus::util::lang::lexical_cast<std::string> (a.interconnect_timeout); data["connected_zookeeper"] = impl_.zk()->get_connected_host_and_port(); data["use_cht"] = jubatus::util::lang::lexical_cast<std::string>( use_cht_); data["mixer"] = a.mixer; server_->get_mixer()->get_status(data); } return status; }
void reset_time_step_clock() { clock_time = get_clock_time( ); }