- 論壇徽章:
- 0
|
sonicling 發(fā)表于 2011-12-21 17:31 ![]()
CPU間緩存同步會(huì)影響效率,但是不至于數(shù)量級(jí)的差別。我以前也做過無鎖循環(huán)隊(duì)列,不過是內(nèi)核級(jí)的,用atomic_ ...
其實(shí)我覺得不是隊(duì)列慢了導(dǎo)致整體性能慢,
我覺得可能是增加流水線的長(zhǎng)度會(huì)導(dǎo)致處理數(shù)據(jù)的速度變慢。
我給大家貼一段處理的代碼吧。- string GroupPE::getEventKey(eventPtr msg) {
- vector<string> keys;
- keys.push_back("UchAPN"); // apn
- keys.push_back("Wlac"); // 位置區(qū)編號(hào):lacid
- keys.push_back("Cell"); // 小區(qū)信息:sac
- keys.push_back("InterfaceType"); // 接口類型:interfacetype
- keys.push_back("Protocoltype"); // 協(xié)議類型:protocolid
- keys.push_back("Eventtype"); // 事件類型:eventtype
- string key = Utils::encoding(*msg, keys.begin(), keys.end(), 2000);
- return key;
- }
- /*每個(gè)線程有一個(gè)統(tǒng)計(jì)map 用來存放統(tǒng)計(jì)數(shù)據(jù) 每次來一個(gè)event就根據(jù)getEventKey這個(gè)方法得到key,然后查看key是不是在統(tǒng)計(jì)map中,如果在統(tǒng)計(jì)map中,那么更新這個(gè)map的數(shù)據(jù),如果沒有,那么new一個(gè)event,設(shè)置一些值,然后放到統(tǒng)計(jì)map中*/
- void GroupPE::process(eventPtr msg) {//統(tǒng)計(jì)
-
- string key = this->getEventKey(msg);
- map<string, eventPtr>::iterator iter4map = this->groupMap.find(key);
-
-
- if (iter4map != groupMap.end()) {
- int times = iter4map->second->getValueByName("times", 0);
- ++times;
- iter4map->second->setValueByName("times", times);
- if (msg->getValueByName("cause", -1) == 255) { // response success
- times = iter4map->second->getValueByName("succtimes", 0);
- ++times;
- iter4map->second->setValueByName("succtimes", times);
- }
- string str = msg->getValueByName("Btime", "1970-01-01 00:00:00.000");
- long btime = Utils::millisecs(str.c_str());
- str = msg->getValueByName("RspTime", "1970-01-01 00:00:00.000");
- long rsptime = Utils::millisecs(str.c_str());
- double delays = ((double) rsptime - (double) btime) / 1000;
- double d = iter4map->second->getValueByName("delays", (double) 0);
- iter4map->second->setValueByName("delays", delays + d);
- d = iter4map->second->getValueByName("maxdelays", (double) 0);
- if (d < delays)
- iter4map->second->setValueByName("maxdelays", delays);
- d = iter4map->second->getValueByName("mindelays", (double) 0);
- if (d > delays)
- iter4map->second->setValueByName("mindelays", delays);
- if (btime != rsptime || msg->getValueByName("Tdrtype", (char) 0) != 1) {
- times = iter4map->second->getValueByName("rsptimes", 0);
- ++times;
- iter4map->second->setValueByName("rsptimes", times);
- } else {
- }
- } else { // 進(jìn)行新統(tǒng)計(jì)結(jié)果記錄
- // 如果單個(gè)map數(shù)量太大超過閥值,需要進(jìn)行清理
- //begin use pointer add by lxy 7-19
- #ifndef NEW_ALLOC
- CEventMessage *groupMapEvent = this->event_alloc.allocate(1);
- groupMapEvent->metaData = NULL;
- char * body = this->mt_allocator.allocate(150);
- groupMapEvent->setBody(body);
- EVENT_HEAD_MESSAGE * head = this->head_alloc.allocate(1); //new EVENT_HEAD_MESSAGE;
- #else
- CEventMessage *groupMapEvent = new CEventMessage();
- char * body = new char[150];
- groupMapEvent->setBody(body);
- EVENT_HEAD_MESSAGE * head = new EVENT_HEAD_MESSAGE(); //new EVENT_HEAD_MESSAGE;
- #endif
- groupMapEvent->reset(head);
- groupMapEvent->setHead(head);
- groupMapEvent->getHead()->type = 2005; // 修改事件的類型為“統(tǒng)計(jì)事件對(duì)象”
- groupMapEvent->getHead()->data_len = 150;
- // 進(jìn)行業(yè)務(wù)信息初始化
- groupMapEvent->setValueByName("times", 1);
- // groupMapEvent->setValueByName("cause", msg->getValueByName("cause", -1));
- if (msg->getValueByName("Cause", -1) == 255)
- groupMapEvent->setValueByName("succtimes", 1);
- else
- groupMapEvent->setValueByName("succtimes", 0);
- string Btime_str = msg->getValueByName("Btime", "1970-01-01 00:00:00.000");
- long btime = Utils::millisecs(Btime_str.c_str());
- groupMapEvent->setValueByName("sttime", msg->getValueByName("Btime", "1970-01-01 00:00:00.000"));
- string RspTime_str = msg->getValueByName("RspTime", "1970-01-01 00:00:00.000");
- long rsptime = Utils::millisecs(RspTime_str.c_str());
- double delays = ((double) rsptime - (double) btime) / 1000;
- groupMapEvent->setValueByName("delays", delays);
- groupMapEvent->setValueByName("maxdelays", delays);
- groupMapEvent->setValueByName("mindelays", delays);
- // groupMapEvent->setValueByName("tdrtype", msg->getValueByName("tdrtype", 0));
- if (btime != rsptime || msg->getValueByName("Tdrtype", (char) 0) != 1) {
- groupMapEvent->setValueByName("rsptimes", 1);
- } else {
- groupMapEvent->setValueByName("rsptimes", 0);
- }
- //統(tǒng)計(jì)維度的設(shè)置 add by lxy 7-21
- groupMapEvent->setValueByName("apn", msg->getValueByName("UchAPN", "*"));
- groupMapEvent->setValueByName("lacid", msg->getValueByName("Wlac", 0));
- groupMapEvent->setValueByName("sac", msg->getValueByName("Cell", 0));
- groupMapEvent->setValueByName("interfacetype", msg->getValueByName("InterfaceType", "*"));
- groupMapEvent->setValueByName("protocolid", msg->getValueByName("Protocoltype", "*"));
- groupMapEvent->setValueByName("eventtype", msg->getValueByName("Eventtype", 0));
- this->groupMap.insert(map<string, eventPtr, std::less<string>, __gnu_cxx::__mt_alloc<string> >::value_type(key, groupMapEvent));
- }
- }
復(fù)制代碼 下面是Event的代碼 不好意思 有點(diǎn)長(zhǎng) 我截取一點(diǎn)給大家 CEventMessage.h- class CEventMessage {
- public:
- /*下面這些函數(shù)主要是根據(jù)類型找到配置文件然后在body的指定位置設(shè)置值*/
- int getValueByName(const string &targetname, const int &defval) const; //
- unsigned int getValueByName(const string &targetname, const unsigned int &defval) const;
- long getValueByName(const string &targetname, const long &defval) const;
- unsigned long getValueByName(const string &targetname, const unsigned long &defval) const;
- char getValueByName(const string &targetname, const char &defval) const;
- unsigned char getValueByName(const string &targetname, const unsigned char &defval) const;
- short getValueByName(const string &targetname, const short &defval) const;
- unsigned short getValueByName(const string &targetname, const unsigned short &defval) const;
- string getValueByName(const string &targetname, const string &defval) const;
- double getValueByName(const string &targetname, const double &defval) const;
- float getValueByName(const string &targetname, const float &defval) const;
- void setValueByName(const string &targetname, const int &val);
- void setValueByName(const string &targetname, const unsigned int &val);
- void setValueByName(const string &targetname, const long &val);
- void setValueByName(const string &targetname, const unsigned long &val);
- void setValueByName(const string &targetname, const short &val);
- void setValueByName(const string &targetname, const unsigned short &val);
- void setValueByName(const string &targetname, const char &val);
- void setValueByName(const string &targetname, const unsigned char &val);
- void setValueByName(const string &targetname, const float &val);
- void setValueByName(const string &targetname, const string &val);
- void setValueByName(const string &targetname, const double &val);
- public:
- CEventMessage();
- CEventMessage(const unsigned short& type);
- CEventMessage(const CEventMessage& event);
- CEventMessage& operator=(const CEventMessage& event);
- virtual ~CEventMessage();
- char * getBytes(int &length);
- void setBody(char* body);
- char* getBody() const;
- void setHead(EVENT_HEAD_MESSAGE* head);
- EVENT_HEAD_MESSAGE* getHead() const;
- static void reset(EVENT_HEAD_MESSAGE *event_head_message) {
- event_head_message->pkg_len = 1;
- event_head_message->flag = 0;
- event_head_message->format = 0;
- event_head_message->ver_info = 1;
- event_head_message->peid = 0;
- }
- unsigned int getPrevPEId() const {
- if (head != NULL)
- return head->peid;
- return 0;
- }
- /**
- * ??socket?жs???????
- * @param conn
- * @return
- */
- private:
- template<class T>
- const EVENT_TYPE_ENTY * getCheckedEventTypeEntity(const string&name)const;
- template<class T>
- const EVENT_TYPE_ENTY * getCheckedEventTypeEntityAllocBody(const string&name);
-
- void resetFields(const CEventMessage& event);
- string getStringForNum(EVENT_TYPE_ENTY *eventType)const;
- void setStringFormNum(EVENT_TYPE_ENTY *eventType, const string&str_val) const;
- CMetaData * getMetaData()const; //元數(shù)據(jù) 就是配置文件 指示body中的數(shù)據(jù)是如何組成的
- typedef hash_map<string, string> TempValueMap;
- TempValueMap mytempvalmap;//用來存放臨時(shí)值 用于豐富維度用
- public:
- CMetaData * metaData;
- private:
- EVENT_HEAD_MESSAGE *head; //用于網(wǎng)絡(luò)傳輸 head
- char *body; //實(shí)際數(shù)據(jù)
- };
復(fù)制代碼 CEventMessage.cpp太長(zhǎng)了就不放上來了 其實(shí)很簡(jiǎn)單 根據(jù)metaData找到配置,根據(jù)配置在body的指定位置設(shè)置值。
|
|