cpp: TSocket: call a second gettimeofday only for error checking
authorDavid Reiss <dreiss@apache.org>
Tue, 9 Mar 2010 05:19:54 +0000 (05:19 +0000)
committerDavid Reiss <dreiss@apache.org>
Tue, 9 Mar 2010 05:19:54 +0000 (05:19 +0000)
Previously, we called gettimeofday twice for every send, which is
costly.  Now, we only make the second call if send fails with EAGAIN.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920677 13f79535-47bb-0310-9956-ffa450edef68

lib/cpp/src/transport/TSocket.cpp

index 55e8dc3..d27c868 100644 (file)
@@ -308,16 +308,17 @@ uint32_t TSocket::read(uint8_t* buf, uint32_t len) {
   gettimeofday(&begin, NULL);
   int got = recv(socket_, buf, len, 0);
   int errno_copy = errno; //gettimeofday can change errno
-  struct timeval end;
-  gettimeofday(&end, NULL);
-  uint32_t readElapsedMicros =  (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
-                                 + (((uint64_t)(end.tv_usec - begin.tv_usec))));
   ++g_socket_syscalls;
 
   // Check for error on read
   if (got < 0) {
     if (errno_copy == EAGAIN) {
       // check if this is the lack of resources or timeout case
+      struct timeval end;
+      gettimeofday(&end, NULL);
+      uint32_t readElapsedMicros =  (((end.tv_sec - begin.tv_sec) * 1000 * 1000)
+                                     + (((uint64_t)(end.tv_usec - begin.tv_usec))));
+
       if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) {
         if (retries++ < maxRecvRetries_) {
           usleep(50);