TCPServer::TCPServer( Board *b, int call_port){ assert(b); board = b; clients_n = 0; port = call_port; if((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) perror("create socket"), exit(1); bzero(&server_addr, sizeof(sockaddr_in)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if((bind(server_socket, (struct sockaddr *)&server_addr, sizeof(sockaddr))) < 0) perror("bind to port"), exit(1); if((listen(server_socket, DEFAULT_QUEUE_SIZE)) < 0) perror("listern to port"), exit(1); while(!stop){ socklen_t client_addr_length = sizeof(client_addr); client_socket = accept(server_socket, (struct sockaddr *)&client_addr,\ &client_addr_length); //may block here if(client_socket < 0) perror("accept from client"), exit(1); time_limit(client_socket, RECV_TIMEWAIT); clients_n++; if( pthread_create( &client_thread_id, NULL,\ client_thread_start_routine, (void *)this) != 0) perror("Initialize thread"), exit(2); pthread_detach(client_thread_id); usleep(200000); //to enable the newest client-server-comunication be well ready } }
bool wait(Locker<Mutex> &_lock, const TimeSpec &_ts, ERROR_NS::system_error &_rerr){ std::chrono::milliseconds ms(std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::seconds(_ts.seconds()) + std::chrono::nanoseconds(_ts.nanoSeconds()) )); std::chrono::system_clock::time_point time_limit(ms); const std::cv_status stat = wait_until(_lock, time_limit); return stat == std::cv_status::no_timeout; }
int main(int argc, char** argv) { namespace po = boost::program_options; po::options_description desc("Options"); desc.add_options() ("help", "Print help messages") ("handystats-config", po::value<std::string>(), "Handystats configuration (in JSON format)" ) ("threads", po::value<uint64_t>(&threads)->default_value(threads), "Number of worker threads" ) ("step", po::value<std::vector<uint64_t>>(&steps)->multitoken()->required(), "Load step pairs (rate, seconds), e.g. --step 100 10 --step 200 10" ) ("timers", po::value<uint64_t>(&timers)->default_value(timers), "Number of different timers (0 for no timers)" ) ("counters", po::value<uint64_t>(&counters)->default_value(counters), "Number of different counters (0 for no counters)" ) ("gauges", po::value<uint64_t>(&gauges)->default_value(gauges), "Number of different gauges (0 for no gauges)" ) ("output-interval", po::value<uint64_t>()->default_value(output_interval.count()), "Stats output interval (in milliseconds)" ) ; po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } po::notify(vm); } catch(po::error& e) { std::cerr << "ERROR: " << e.what() << std::endl << std::endl; std::cerr << desc << std::endl; return 1; } if (vm["threads"].as<uint64_t>() == 0) { std::cerr << "ERROR: number of threads must be greater than 0" << std::endl; return 1; } if (!vm["timers"].as<uint64_t>() && !vm["counters"].as<uint64_t>() && !vm["gauges"].as<uint64_t>()) { std::cerr << "ERROR: at least one metric type must be specified (timers, counters, gauges)" << std::endl; return 1; } if (vm.count("handystats-config")) { HANDY_CONFIG_JSON(vm["handystats-config"].as<std::string>().c_str()); } output_interval = std::chrono::milliseconds(vm["output-interval"].as<uint64_t>()); HANDY_INIT(); std::atomic<bool> stop_flag(false); std::thread stats_printer( [&stop_flag] () { while (!stop_flag.load()) { const auto& start_time = handystats::chrono::tsc_clock::now(); print_stats(); const auto& end_time = handystats::chrono::tsc_clock::now(); const auto& call_time = handystats::chrono::duration::convert_to( handystats::chrono::time_unit::NSEC, end_time - start_time ); std::this_thread::sleep_for(output_interval - std::chrono::nanoseconds(call_time.count())); } } ); std::vector<std::thread> workers(threads); for (size_t step_index = 0; step_index < steps.size(); step_index += 2) { uint64_t step_rate = steps[step_index]; uint64_t step_time_limit = steps[step_index + 1]; if (step_time_limit == 0) { continue; } rate.store(step_rate); end_time.store( handystats::chrono::duration::convert_to(handystats::chrono::time_unit::USEC, ( handystats::chrono::tsc_clock::now() + handystats::chrono::duration(step_time_limit, handystats::chrono::time_unit::SEC) ).time_since_epoch() ).count() ); if (step_rate == 0) { std::this_thread::sleep_for(std::chrono::seconds(step_time_limit)); continue; } handystats::chrono::duration command_time_limit = handystats::chrono::duration::convert_to( handystats::chrono::time_unit::NSEC, handystats::chrono::duration(1, handystats::chrono::time_unit::SEC) ) * threads / step_rate; handystats::chrono::duration time_limit(step_time_limit, handystats::chrono::time_unit::SEC); for (auto& worker : workers) { worker = std::thread( [command_time_limit, time_limit] () { handystats::benchmarks::command_executor::run_for( [] () { command(); }, command_time_limit, time_limit ); } ); } for (auto& worker : workers) { worker.join(); } } stop_flag.store(true); stats_printer.join(); HANDY_FINALIZE(); }
int run_main (int, ACE_TCHAR *[]) { ACE_START_TEST (ACE_TEXT ("MT_Reactor_Timer_Test")); int status = 0; int test_result = 0; ACE_Reactor *r = ACE_Reactor::instance (); Dispatch_Count_Handler callback; for (int i = ACE_MAX_TIMERS; i > 0; i--) // Schedule a timeout to expire immediately. if (r->schedule_timer (&callback, reinterpret_cast <const void *> (static_cast <size_t> (i)), ACE_Time_Value (0)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")), 1); ACE_Time_Value no_waiting (0); size_t events = 0; while (1) { int result = r->handle_events (no_waiting); // Timeout. if (result == 0) break; // Make sure there were no errors. ACE_TEST_ASSERT (result != -1); events += result; } // All <ACE_MAX_TIMERS> + 2 I/O dispatches (one for <handle_input> // and the other for <handle_exception>) should be counted in // events. if (events < ACE_MAX_TIMERS + 2) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("expected %d events, got %d instead\n"), ACE_MAX_TIMERS + 2, events)); } status = callback.verify_results (); if (status != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Dispatch counting test failed.\n"))); test_result = 1; } #if defined (ACE_HAS_THREADS) Time_Handler other_thread; ACE_Time_Value time_limit (30); // Set up initial set of timers. other_thread.setup (); other_thread.activate (THR_NEW_LWP | THR_JOINABLE); status = ACE_Reactor::instance()->run_reactor_event_loop (time_limit); // Should have returned only because the time limit is up... ACE_TEST_ASSERT (status != -1); ACE_TEST_ASSERT (time_limit.sec () == 0); status = other_thread.wait (); if (status == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p, errno is %d\n"), "wait ()", ACE_ERRNO_GET)); ACE_TEST_ASSERT (status != -1); } status = other_thread.verify_results (); if (status != 0) test_result = 1; #else ACE_ERROR ((LM_INFO, ACE_TEXT ("threads not supported on this platform\n"))); #endif /* ACE_HAS_THREADS */ ACE_END_TEST; return test_result; }