-1

我有一个套接字程序,它既像客户端又像服务器。

它在输入端口上启动连接并从中读取数据。在实时场景中,它读取输入端口上的数据并将数据(逐个记录)发送到输出端口。

这里的问题是,在向输出端口发送数据时,CPU 使用率会增加到 50%,而这是不允许的。

while(1)
{    
        if(IsInputDataAvail==1)//check if data is available on input port
        { 
           //condition to avoid duplications while sending
        if( LastRecordSent < LastRecordRecvd )   
        {     
           record_time temprt;
           list<record_time> BufferList;
           list<record_time>::iterator j;
           list<record_time>::iterator i;

           // Storing into a temp list
            for(i=L.begin(); i != L.end(); ++i)
             {
               if((i->recordId > LastRecordSent) && (i->recordId <= LastRecordRecvd))
               {     
                temprt.listrec = i->listrec;
                temprt.recordId = i->recordId;
                temprt.timestamp = i->timestamp;
                BufferList.push_back(temprt);
               }
             }

                //Sending to output port
             for(j=BufferList.begin(); j != BufferList.end(); ++j)
             {
               LastRecordSent = j->recordId;

              std::string newlistrecord = j->listrec;
              newlistrecord.append("\n");
              char* newrecord= new char [newlistrecord.size()+1];
              strcpy (newrecord, newlistrecord.c_str());

             if ( s.OutputClientAvail() == 1) //check  if output client is available
             { 
              int ret = s.SendBytes(newrecord,strlen(newrecord));                        
              if ( ret < 0)
               { 
                log1.AddLogFormatFatal("Nice Send Thread : Nice Client Disconnected");    
                 --connected;
                  return;
               }                                              
            }
            else
            {
             log1.AddLogFormatFatal("Nice Send Thread : Nice Client Timedout..connection closed");    
             --connected; //if output client not available disconnect after a timeout
             return;
            }                                        
          }    

       }  
      }

    //  Sleep(100); if we include sleep here CPU usage is less..but to send data real time I need to remove this sleep.
If I remove Sleep()...CPU usage goes very high while sending data to out put port.          
  }//End of while loop

任何可能的方法来保持实时数据传输并减少 CPU 使用率..请提出建议。

4

2 回答 2

0

列出的代码中有两个潜在的 CPU 接收器。一、外循环:

while (1)
{
    if (IsInputDataAvail == 1)
    {
        // Not run most of the time
    }

    // Sleep(100);
}

鉴于 Sleep 调用显着降低了 CPU 使用率,这个自旋循环很可能是罪魁祸首。它看起来像是IsInputDataAvail由另一个线程设置的变量(尽管它可能是预处理器宏),这意味着几乎所有 CPU 都用于运行这条比较指令和几次跳转。

回收浪费的功率的方法是阻塞直到输入可用。您的阅读线程可能已经这样做了,因此您只需要某种信号量来在两者之间进行通信,并使用系统调用来阻止输出线程。在可用的情况下,理想的选项是sem_wait()在输出线程中,就在循环的顶部,sem_post()在输入线程中,它当前设置IsInputDataAvail. 如果这是不可能的,那么自管技巧可能会起作用。

第二个潜在的 CPU 接收器位于s.SendBytes(). 如果肯定结果表明记录已完全发送,则该方法必须使用循环。它可能使用阻塞调用来写入记录;如果没有,那么可以重写它来这样做。

或者,您可以重写一半的应用程序以使用select(),poll()或类似的方法将读取和写入合并到同一个线程中,但如果您的程序已经大部分完成,那么工作量就太大了。

于 2012-10-28T08:51:37.337 回答
0
  if(IsInputDataAvail==1)//check if data is available on input port

摆脱它。只需从输入端口读取。它将阻塞,直到数据可用。这是您大部分 CPU 时间的去向。但是还有其他问题:

          std::string newlistrecord = j->listrec;

在这里,您正在复制数据。

          newlistrecord.append("\n");

          char* newrecord= new char [newlistrecord.size()+1];
          strcpy (newrecord, newlistrecord.c_str());

在这里,您再次复制相同的数据。你也在动态分配内存,你也在泄漏它。

         if ( s.OutputClientAvail() == 1) //check  if output client is available

我不知道这是做什么的,但你应该删除它。以下发送是检查错误的时间。不要试图猜测未来。

          int ret = s.SendBytes(newrecord,strlen(newrecord));

在这里,您正在重新计算您在设置时可能已经知道的字符串的长度j->listrecs.sendBytes()直接调用j->listrec,然后再调用"\n"比执行所有这些操作要高效得多。无论如何,TCP 都会合并数据。

于 2014-10-30T02:14:19.800 回答