static void
list_assembly(const SqlDatabase::TransactionPtr &tx, int func_id)
{
    Events events;
    gather_events(tx, func_id);
    load_events(tx, func_id, events);

    SqlDatabase::StatementPtr stmt = tx->statement("select address, assembly from semantic_instructions where func_id = ?"
                                                   " order by position")->bind(0, func_id);
    for (SqlDatabase::Statement::iterator insn=stmt->begin(); insn!=stmt->end(); ++insn) {
        rose_addr_t addr = insn.get<rose_addr_t>(0);
        std::string assembly = insn.get<std::string>(1);
        Events::const_iterator ei=events.find(addr);

        // Assembly line prefix
        if (ei!=events.end() && ei->second.nexecuted>0) {
            std::cout <<std::setw(9) <<std::right <<ei->second.nexecuted <<"x ";
        } else {
            std::cout <<std::string(11, ' ');
        }

        // Assembly instruction
        std::cout <<"| " <<StringUtility::addrToString(addr) <<":  " <<assembly <<"\n";

        if (ei!=events.end())
            show_events(ei->second);
    }
}
int main() {
	std::string line;

	Events events;

	while (std::getline(std::cin, line)) {
		std::vector<std::string> elements;
		boost::split(elements, line, boost::is_any_of("/"));
		if (elements.size() != 4) {
			std::cerr << "Warning: elements.size() != 4" << std::endl;
			continue;
		}
		Event e;
		e.player = elements[0];
		e.map = elements[1];
		e.lapTime = parseLapTime(elements[2]);
		e.date = parseDate(elements[3]);

		events.push_back(e);
	}

	std::cout << "Number of events: " << events.size() << std::endl;

	std::sort(events.begin(), events.end());

	std::cout << "Last event: " << events.back() << std::endl;

	Ranking ranking = getRankings(events, boost::posix_time::time_from_string("2014-01-03 22:00:00.000"));
	for ( unsigned i = 0; i < 20 && i < ranking.size(); ++i ) {
		std::cout << i+1 << ".: " << ranking[i].getPlayer() << ", Time: " << ranking[i].getTotalLapTime() << std::endl;
	}

	std::cout << "Current leader = " << ranking[0].getTotalLapTime() << std::endl;
}
// Create the tmp_insns table to hold all the instructions for the function-to-be-listed and all the instructions of all
// the functions that are mentioned in events.
static void
gather_instructions(const SqlDatabase::TransactionPtr tx, int func_id, const Events &events)
{
    std::set<std::string> func_ids;
    func_ids.insert(StringUtility::numberToString(func_id));
    for (Events::const_iterator ei=events.begin(); ei!=events.end(); ++ei)
        func_ids.insert(StringUtility::numberToString(ei->second.func_id));
    std::string sql = "create temporary table tmp_insns as"
                      " select * from semantic_instructions"
                      " where func_id in ("+StringUtility::join_range(", ", func_ids.begin(), func_ids.end())+")";
    tx->execute(sql);
}
bool EventQueue::takeEvents(Events& events)
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
    if (!_eventQueue.empty())
    {
        events.splice(events.end(), _eventQueue);
        return true;
    }
    else
    {
        return false;
    }
}
bool EventQueue::copyEvents(Events& events) const
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
    if (!_eventQueue.empty())
    {
        events.insert(events.end(),_eventQueue.begin(),_eventQueue.end());
        return true;
    }
    else
    {
        return false;
    }
}
Beispiel #6
0
//-------------------------------------------------------------------------------------------------
void TimerActor::delete_event(const acto::actor_ref& actor) {
	for (Events::iterator i = m_events.begin(); i != m_events.end(); i++) {
		if ((*i)->actor == actor) {
			// 1. Удалить системный таймер
			//
			// NOTE: Так как последний параметр INVALID_HANDLE_VALUE, то
			//       функция возвратит управление только тогда, когда завершится
			//       выполнение соответствующей TimerProc.
			::DeleteTimerQueueTimer( m_timers, (*i)->timer, INVALID_HANDLE_VALUE );
			// 2. Удалить экземпляр события
			delete (*i);
			// 3. Удалить элемент из списка
			m_events.erase( i );
			// -
			break;
		}
	}
}
void EventQueue::appendEvents(Events& events)
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
    _eventQueue.insert(_eventQueue.end(), events.begin(), events.end());
}
static void
list_combined(const SqlDatabase::TransactionPtr &tx, int func_id, bool show_assembly)
{
    CloneDetection::FilesTable files(tx);

    Events events;
    gather_events(tx, func_id);
    load_events(tx, func_id, events/*out*/);
    gather_instructions(tx, func_id, events);

    Listing listing;
    gather_source_code(tx);
    load_source_code(tx, listing/*out*/);

    // Get lines of assembly code and insert them into the correct place in the Listing.
    if (show_assembly) {
        SqlDatabase::StatementPtr stmt = tx->statement("select"
                                                       // 0                1              2              3
                                                       " insn.src_file_id, insn.src_line, insn.position, insn.address,"
                                                       // 4             5        6
                                                       " insn.assembly, func.id, func.name"
                                                       " from tmp_insns as insn"
                                                       " join semantic_functions as func on insn.func_id = func.id"
                                                       " order by position");
        for (SqlDatabase::Statement::iterator row=stmt->begin(); row!=stmt->end(); ++row) {
            int src_file_id = row.get<int>(0);
            int src_line_num = row.get<int>(1);
            SourcePosition srcpos(src_file_id, src_line_num);
            int pos = row.get<int>(2);
            rose_addr_t addr = row.get<rose_addr_t>(3);
            std::string assembly = row.get<std::string>(4);
            int func_id = row.get<int>(5);
            std::string func_name = row.get<std::string>(6);
            listing[srcpos].assembly_code.insert(std::make_pair(addr, AssemblyCode(pos, addr, assembly, func_id, func_name)));
        }

        // Listing header
        std::cout <<"WARNING: This listing should be read cautiously. It is ordered according to the\n"
                  <<"         source code with assembly lines following the source code line from which\n"
                  <<"         they came.  However, the compiler does not always generate machine\n"
                  <<"         instructions in the same order as source code.  When a discontinuity\n"
                  <<"         occurs in the assembly instruction listing, it will be marked by a \"#\"\n"
                  <<"         character.  The assembly instructions are also numbered according to\n"
                  <<"         their relative positions in the binary function.\n"
                  <<"\n"
                  <<"         The prefix area contains either source location information or test trace\n"
                  <<"         information.  Note that trace information might be incomplete because\n"
                  <<"         tracing was disabled or only partially enabled, or the trace includes\n"
                  <<"         instructions that are not present in this function listing (e.g., when\n"
                  <<"         execution follows a CALL instruction). The following notes are possible:\n"
                  <<"           * \"Nx\" where N is an integer indicates that this instruction\n"
                  <<"             was reached N times during testing.  These notes are typically\n"
                  <<"             only attached to the first instruction of a basic block and only\n"
                  <<"             if the trace contains EV_REACHED events.  Lack of an Nx notation\n"
                  <<"             doesn't necessarily mean that the basic block was not reached, it\n"
                  <<"             only means that there is no EV_REACHED event for that block.\n"
                  <<"           * \"N<\" where N is an integer indicates that the instruction\n"
                  <<"             on the previous line consumed N inputs. Information about the\n"
                  <<"             inputs is listed on the right side of this line.\n"
                  <<"           * \"N>\" where N is an integer indicates that the instruction\n"
                  <<"             on the previous line produced N memory outputs. Information about the\n"
                  <<"             outputs is listed on the right side of this line. Only the final\n"
                  <<"             write to a memory address is considered a true output, and such\n"
                  <<"             writes will be marked with the string \"final\".\n"
                  <<"           * \"BR\" indicates that the instruction on the previous line is a\n"
                  <<"             control flow branch point. The right side of the line shows more\n"
                  <<"             detailed information about how many times the branch was taken.\n"
                  <<"           * \"FAULT\" indicates that the test was terminated at the previous\n"
                  <<"             instruction. The right side of the line shows the distribution of\n"
                  <<"             faults that occurred here.\n"
                  <<"\n"
                  <<"                /------------- Prefix area\n"
                  <<" /-------------/-------------- Source file ID or assembly function ID\n"
                  <<" |     /------/--------------- Source line number or assembly instruction index\n"
                  <<" |     |   /-/---------------- Instruction out-of-order indicator\n"
                  <<" |     |   |/     /----------- Instruction virtual address\n"
                  <<" |     |   |      |\n"
                  <<"vvvv vvvvv/|      |\n"
                  <<"vvvvvvvvvv v vvvvvvvvvv\n";
    }

    // Show the listing
    int prev_func_id = -1, prev_position = -1;
    std::set<int> seen_files;
    for (Listing::iterator li=listing.begin(); li!=listing.end(); ++li) {
        int file_id = li->first.file_id;
        if (seen_files.insert(file_id).second) {
            if (file_id>=0) {
                std::cout <<"\n" <<std::setw(4) <<std::right <<file_id <<".file  |"
                          <<(opt.colorize?"\033[33;4m":"") <<files.name(file_id) <<(opt.colorize?"\033[m":"") <<"\n";
            } else {
                std::cout <<"\n" <<std::string(11, ' ') <<"|"
                          <<(opt.colorize?"\033[33;4m":"") <<"instructions not associated with a source file"
                          <<(opt.colorize?"\033[m":"") <<"\n";
            }
        }
        if (file_id>=0) {
            std::cout <<std::setw(4) <<std::right <<file_id <<"." <<std::setw(6) <<std::left <<li->first.line_num
                      <<"|"
                      <<(opt.colorize?"\033[34m":"")
                      <<StringUtility::untab(li->second.source_code)
                      <<(opt.colorize?"\033[m":"") <<"\n";
        }

        for (Instructions::iterator ii=li->second.assembly_code.begin(); ii!=li->second.assembly_code.end(); ++ii) {
            const AssemblyCode assm = ii->second;
            if (assm.func_id!=prev_func_id) {
                std::cout <<std::string(11, ' ') <<"# "
                          <<(opt.colorize?"\033[33;4m":"") <<"function " <<StringUtility::numberToString(assm.func_id);
                if (!assm.func_name.empty())
                    std::cout <<" <" <<assm.func_name <<">";
                std::cout <<(opt.colorize?"\033[m":"") <<"\n";
            }

            Events::const_iterator ei=events.find(assm.addr);
            std::cout <<std::setw(4) <<std::right <<assm.func_id <<"." <<std::setw(6) <<std::left <<assm.pos
                      <<(prev_func_id==assm.func_id && prev_position+1==assm.pos ? "|" : "#");

            if (ei!=events.end() && ei->second.nexecuted>0) {
                std::cout <<std::setw(9) <<std::right <<ei->second.nexecuted <<"x ";
            } else {
                std::cout <<std::string(11, ' ');
            }

            std::cout <<StringUtility::addrToString(assm.addr) <<":  "
                      <<(opt.colorize?"\033[32m":"") <<assm.assembly <<(opt.colorize?"\033[m":"") <<"\n";

            if (ei!=events.end())
                show_events(ei->second);

            prev_func_id = assm.func_id;
            prev_position = assm.pos;
        }
    }
}
Beispiel #9
0
 const_iterator end () const
 {
     return m_events.end();
 }
Beispiel #10
0
 iterator end ()
 {
     return m_events.end();
 }
int main(int argc, char *argv[]) {
	int c;
	bool use_cors = false;
	std::string remote_str, filename;
	sockaddr_in local, remote;
	timeval tm_start, tm_end;
	uint32_t delay = DEFAULT_DELAY;
	uint8_t relaynum = DEFAULT_RLNUM;

	// default arguments
	memset(&local, 0, sizeof(local));
	local.sin_family = AF_INET;
	local.sin_port = htons(DEFAULT_PORT);
	local.sin_addr.s_addr = htonl(INADDR_ANY);

	replay = 1;

	while((c = getopt(argc, argv, "cd:l:n:p:r:")) != -1) {
		switch(c) {
		case 'c':
			use_cors = true;
			break;
		case 'd':
			delay = atoi(optarg);
			break;
		case 'l':
			local.sin_port = htons(atoi(optarg));
			break;
		case 'n':
			replay = atoi(optarg);
			break;
		case 'p':
			relaynum = atoi(optarg);
			break;
		case 'r':
			remote_str = optarg;
			break;
		case '?':
			PrintUsage();
			break;
		}
	}
	if (optind != argc - 1) {
		cout << "must specify an input file" << endl;
		PrintUsage();
		return -1;
	}  
	filename = argv[optind];

	if (remote_str.empty()) {
		cout << "must specify remote addr -- ip:port" << endl;
		PrintUsage();
		return -1;
	}
	size_t pos = remote_str.find(":");
	memset(&remote, 0, sizeof(remote));
	remote.sin_family = AF_INET;
	remote.sin_port = htons(atoi(remote_str.substr(pos+1).c_str()));
	remote.sin_addr.s_addr = inet_addr(remote_str.substr(0, pos).c_str());

	cout << "use_cors: " << (use_cors ? "yes" : "no") << endl;
	cout << "local addr: " << sockaddr2str(local) << endl;
	cout << "remote addr: " << sockaddr2str(remote) << endl;
	cout << "replay time: " << replay << endl;
	cout << "delay requirement: " << delay << "ms" << endl;
	cout << "relay path number: " << (int)relaynum << endl;
	cout << "input file: " << filename << endl; 

	Events events;
	if (ReadRtpData(filename, &events) < 0) {
		return -1;
	}

	int sock;
	if ((sock = InitSock(local)) < 0) {
		return -1;
	}

  int flags;
  if ((flags = fcntl(sock, F_GETFL, 0)) < 0) {
    cerr << "fcntl(GETFL) error" << endl;
    return -1;
  }

  if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
    cerr << "fcntl(SETFL) error" << endl;
    return -1;
  }

	Requirement req;
	memset(&req, 0, sizeof(req));
	req.delay = delay;
	req.relaynum = relaynum;

	Feature feature;
	memset(&feature, 0, sizeof(feature));
	feature.delay = delay;
	feature.significance = 0;
	feature.seculevel = 0;
	feature.channelnum = relaynum;

	vector<string> rsparams, mmparams;
	rsparams.push_back("SimpleRlysltr");
	rsparams.push_back("rsltr.conf");
	rsparams.push_back("300000");	// probe interval
	rsparams.push_back("300");	// timeout seconds

	mmparams.push_back("SimpleMpathManager");

	CorsSocket *cors_sk = NULL;
  if (use_cors) {
    cors_sk = new CorsSocket(sock, rsparams, mmparams);
	  cors_sk->init(req, remote.sin_addr.s_addr, remote.sin_port);
  }

	pthread_t recv_tid, timer_tid;
	if (use_cors) {
		pthread_create(&recv_tid, NULL, recv_thread, cors_sk);
	}
	// pthread_create(&timer_tid, NULL, timer_thread, &events);

	int index = 0;
  int sended = 0;
  int target = 0;
  timeval tm_used, tm_begin;
  // gettimeofday(&tm_begin, NULL);
	Events::iterator it;
	for (; replay > 0; replay--) {
		for (it = events.begin(); it != events.end(); ++it) {
			timeval timeout = it->delta;
			select(0, NULL, NULL, NULL, &timeout);
      if (use_cors) {
        cors_sk->sendto(it->pkt, 
                       it->len,
                       remote.sin_addr.s_addr,
                       remote.sin_port,
                       feature);
      } else {
        sendto(sock, it->pkt, it->len, 0, 
               (sockaddr *)&remote, sizeof(remote));
      }
      sended++;
		}
	}
#if 0
	while (true) {
		pthread_mutex_lock(&timer_mutex);
		while (timer_sig == -1) {
			pthread_cond_wait(&timer_cond, &timer_mutex);
		}
		index = timer_sig;
		timer_sig = -1;
		pthread_mutex_unlock(&timer_mutex);
		
    if (index == -2) {
      target = events.size();
    } else {
      target = index + 1;
    }
    for (int i = sended; i < target; i++) {
		  //gettimeofday(&tm_start, NULL);
      // tm_used = CalcDelta(tm_start, tm_begin);
      //cout << "#### main() send " << i << " at " << tm_start.tv_sec
      //     << "." << setw(6) << setfill('0') << tm_start.tv_usec << endl;
      if (use_cors) {
		    // gettimeofday(&tm_start, NULL);
        // tm_used = CalcDelta(tm_start, tm_begin);
        // cout << "#### main() send " << i << " at " << tm_start.tv_sec
        //     << "." << setw(6) << setfill('0') << tm_start.tv_usec << endl;
        // Arguments *arg = new Arguments;
        // arg->cors = &cors_sk;
        // arg->buf = events[i].pkt;
        // arg->len = events[i].len;
        // arg->addr = &remote;
        // arg->feature = &feature;
        // pthread_t send_tid;
        // pthread_create(&send_tid, NULL, send_thread, arg);
        cors_sk.sendto(events[i].pkt,
                       events[i].len,
                       remote.sin_addr.s_addr,
                       remote.sin_port,
                       feature);
        
		    // gettimeofday(&tm_end, NULL);
        // cout << "#### main() finish send at " << tm_end.tv_sec
        //      << "." << setw(6) << setfill('0') << tm_end.tv_usec << endl;
        // tm_used = CalcDelta(tm_end, tm_start);
        // cout << "#### main() time_used = " << tm_used.tv_sec
        //      << "." << setw(6) << setfill('0') << tm_used.tv_usec << endl;
      } else {
		    // gettimeofday(&tm_start, NULL);
        // tm_used = CalcDelta(tm_start, tm_begin);
        // cout << "#### main() send " << i << " at " << tm_start.tv_sec
        //     << "." << setw(6) << setfill('0') << tm_start.tv_usec << endl;
        sendto(sock, events[i].pkt,
               events[i].len, 0,
               (sockaddr *)&remote, sizeof(remote));
		    // gettimeofday(&tm_end, NULL);
        // cout << "#### main() finish send at " << tm_end.tv_sec
        //      << "." << setw(6) << setfill('0') << tm_end.tv_usec << endl;
        // tm_used = CalcDelta(tm_end, tm_start);
        // cout << "#### main() time_used = " << tm_used.tv_sec
        //      << "." << setw(6) << setfill('0') << tm_used.tv_usec << endl;
      }
      sended++;
    }
    if (index == -2) 
      break;

		// if (index == -2) {
    //   for (int i = sended; i < events.size)
    //   break;
    // } else {
    //   for (int i = sended; i <= index; i++) {
		// 	  if (use_cors)
		// 		  cors_sk.sendto(events[i].pkt,
		// 					           events[i].len,
		// 					           remote.sin_addr.s_addr,
		// 					           remote.sin_port,
		// 					           feature);
		// 	  else
		// 		  sendto(sock, events[i].pkt,
		// 			       events[i].len, 0,
		// 			       (sockaddr *)&remote, sizeof(remote));
    //     sended++;
    //   }
		// }
		// cout << "time_used main = " 
		//	    << tm_end.tv_usec - tm_start.tv_usec << endl;
	}
#endif
  cout << "total send: " << sended << " packets" << endl;
	if (use_cors)
    cors_sk->close();
	close(sock);

	return 0;
}