Bug 826238: Convert DNS expiration tracking to TimeStamp/TimeDuration. r=sworkman
authorJosh Aas <joshmoz@gmail.com>
Thu, 14 Feb 2013 09:03:51 -0600
changeset 121914 e524829ba4b7266d9df6a29c50e4313ae85c2d67
parent 121913 e4448a24543bcd556b1ecb14c6578ffac5815de1
child 121915 74828c564a362e19ffc56e6e94d959bcf31df8dc
push id24311
push userryanvm@gmail.com
push dateThu, 14 Feb 2013 22:16:46 +0000
treeherdermozilla-central@a373379d43cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssworkman
bugs826238
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 826238: Convert DNS expiration tracking to TimeStamp/TimeDuration. r=sworkman
netwerk/dns/nsHostResolver.cpp
netwerk/dns/nsHostResolver.h
--- a/netwerk/dns/nsHostResolver.cpp
+++ b/netwerk/dns/nsHostResolver.cpp
@@ -81,22 +81,16 @@ MoveCList(PRCList &from, PRCList &to)
         to.next = from.next;
         to.prev = from.prev;
         to.next->prev = &to;
         to.prev->next = &to;
         PR_INIT_CLIST(&from);
     }             
 }
 
-static uint32_t
-NowInMinutes()
-{
-    return uint32_t(PR_Now() / int64_t(60 * PR_USEC_PER_SEC));
-}
-
 //----------------------------------------------------------------------------
 
 #if defined(RES_RETRY_ON_FAILURE)
 
 // this class represents the resolver state for a given thread.  if we
 // encounter a lookup failure, then we can invoke the Reset method on an
 // instance of this class to reset the resolver (in case /etc/resolv.conf
 // for example changed).  this is mainly an issue on GNU systems since glibc
@@ -153,17 +147,17 @@ nsHostRecord::nsHostRecord(const nsHostK
     , onQueue(false)
     , usingAnyThread(false)
 {
     host = ((char *) this) + sizeof(nsHostRecord);
     memcpy((char *) host, key->host, strlen(key->host) + 1);
     flags = key->flags;
     af = key->af;
 
-    expiration = NowInMinutes();
+    expiration = TimeStamp::NowLoRes();
 
     PR_INIT_CLIST(this);
     PR_INIT_CLIST(&callbacks);
 }
 
 nsresult
 nsHostRecord::Create(const nsHostKey *key, nsHostRecord **result)
 {
@@ -280,19 +274,18 @@ HostDB_ClearEntry(PLDHashTable *table,
 
     LOG(("Clearing cache db entry for host [%s].\n", hr->host));
 #if defined(DEBUG) && defined(PR_LOGGING)
     {
         MutexAutoLock lock(hr->addr_info_lock);
         if (!hr->addr_info) {
             LOG(("No address info for host [%s].\n", hr->host));
         } else {
-            int32_t now = (int32_t) NowInMinutes();
-            int32_t diff = (int32_t) hr->expiration - now;
-            LOG(("Record for [%s] expires in %d minute(s).\n", hr->host, diff));
+            TimeDuration diff = hr->expiration - TimeStamp::NowLoRes();
+            LOG(("Record for [%s] expires in %f seconds.\n", hr->host, diff.ToSeconds()));
 
             NetAddrElement *addrElement = nullptr;
             char buf[kIPv6CStrBufSize];
             do {
                 if (!addrElement) {
                     addrElement = hr->addr_info->mAddresses.getFirst();
                 } else {
                     addrElement = addrElement->getNext();
@@ -342,17 +335,17 @@ HostDB_RemoveEntry(PLDHashTable *table,
 }
 
 //----------------------------------------------------------------------------
 
 nsHostResolver::nsHostResolver(uint32_t maxCacheEntries,
                                uint32_t maxCacheLifetime,
                                uint32_t lifetimeGracePeriod)
     : mMaxCacheEntries(maxCacheEntries)
-    , mMaxCacheLifetime(maxCacheLifetime)
+    , mMaxCacheLifetime(TimeDuration::FromSeconds(maxCacheLifetime * 60))
     , mGracePeriod(lifetimeGracePeriod)
     , mLock("nsHostResolver.mLock")
     , mIdleThreadCV(mLock, "nsHostResolver.mIdleThreadCV")
     , mNumIdleThreads(0)
     , mThreadCount(0)
     , mActiveAnyThreadCount(0)
     , mEvictionQSize(0)
     , mPendingCount(0)
@@ -538,27 +531,26 @@ nsHostResolver::ResolveHost(const char  
                                          (PL_DHashTableOperate(&mDB, &key, PL_DHASH_ADD));
 
             // if the record is null, then HostDB_InitEntry failed.
             if (!he || !he->rec)
                 rv = NS_ERROR_OUT_OF_MEMORY;
             // do we have a cached result that we can reuse?
             else if (!(flags & RES_BYPASS_CACHE) &&
                      he->rec->HasResult() &&
-                     NowInMinutes() <= he->rec->expiration + mGracePeriod) {
-                        
+                     TimeStamp::NowLoRes() <= (he->rec->expiration + TimeDuration::FromSeconds(mGracePeriod * 60))) {
                 LOG(("Using cached record for host [%s].\n", host));
                 // put reference to host record on stack...
                 result = he->rec;
                 Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
 
                 // For entries that are in the grace period with a failed connect,
                 // or all cached negative entries, use the cache but start a new lookup in
                 // the background
-                if ((((NowInMinutes() > he->rec->expiration) &&
+                if ((((TimeStamp::NowLoRes() > he->rec->expiration) &&
                       he->rec->mBlacklistedItems.Length()) ||
                      he->rec->negative) && !he->rec->resolving) {
                     LOG(("Using %s cache entry for host [%s] but starting async renewal.",
                          he->rec->negative ? "negative" :"positive", host));
                     IssueLookup(he->rec);
 
                     if (!he->rec->negative) {
                         // negative entries are constantly being refreshed, only
@@ -850,23 +842,23 @@ nsHostResolver::OnLookupComplete(nsHostR
         {
             MutexAutoLock lock(rec->addr_info_lock);
             old_addr_info = rec->addr_info;
             rec->addr_info = result;
             rec->addr_info_gencnt++;
         }
         delete old_addr_info;
 
-        rec->expiration = NowInMinutes();
+        rec->expiration = TimeStamp::NowLoRes();
         if (result) {
             rec->expiration += mMaxCacheLifetime;
             rec->negative = false;
         }
         else {
-            rec->expiration += 1;                 /* one minute for negative cache */
+            rec->expiration += TimeDuration::FromSeconds(60); /* one minute for negative cache */
             rec->negative = true;
         }
         rec->resolving = false;
         
         if (rec->usingAnyThread) {
             mActiveAnyThreadCount--;
             rec->usingAnyThread = false;
         }
@@ -881,19 +873,20 @@ nsHostResolver::OnLookupComplete(nsHostR
                 // remove first element on mEvictionQ
                 nsHostRecord *head =
                     static_cast<nsHostRecord *>(PR_LIST_HEAD(&mEvictionQ));
                 PR_REMOVE_AND_INIT_LINK(head);
                 PL_DHashTableOperate(&mDB, (nsHostKey *) head, PL_DHASH_REMOVE);
 
                 if (!head->negative) {
                     // record the age of the entry upon eviction.
-                    uint32_t age =
-                        NowInMinutes() - (head->expiration - mMaxCacheLifetime);
-                    Telemetry::Accumulate(Telemetry::DNS_CLEANUP_AGE, age);
+                    TimeDuration age = TimeStamp::NowLoRes() -
+                                         (head->expiration - mMaxCacheLifetime);
+                    Telemetry::Accumulate(Telemetry::DNS_CLEANUP_AGE,
+                                          static_cast<uint32_t>(age.ToSeconds() / 60));
                 }
 
                 // release reference to rec owned by mEvictionQ
                 NS_RELEASE(head);
             }
         }
     }
 
@@ -950,18 +943,16 @@ nsHostResolver::CancelAsyncRequest(const
             if (recPtr->next != recPtr) {
                 PR_REMOVE_LINK(recPtr);
                 NS_RELEASE(recPtr);
             }
         }
     }
 }
 
-//----------------------------------------------------------------------------
-
 void
 nsHostResolver::ThreadFunc(void *arg)
 {
     LOG(("DNS lookup thread starting execution.\n"));
 
     static nsThreadPoolNaming naming;
     naming.SetThreadPoolName(NS_LITERAL_CSTRING("DNS Resolver"));
 
@@ -1020,18 +1011,16 @@ nsHostResolver::ThreadFunc(void *arg)
         // OnLookupComplete may release "rec", log before we lose it.
         LOG(("Lookup completed for host [%s].\n", rec->host));
         resolver->OnLookupComplete(rec, status, ai);
     }
     NS_RELEASE(resolver);
     LOG(("DNS lookup thread ending execution.\n"));
 }
 
-//----------------------------------------------------------------------------
-
 nsresult
 nsHostResolver::Create(uint32_t         maxCacheEntries,
                        uint32_t         maxCacheLifetime,
                        uint32_t         lifetimeGracePeriod,
                        nsHostResolver **result)
 {
 #if defined(PR_LOGGING)
     if (!gHostResolverLog)
@@ -1062,17 +1051,17 @@ CacheEntryEnumerator(PLDHashTable *table
     nsHostRecord *rec = static_cast<nsHostDBEnt*>(entry)->rec;
     if (!rec->addr_info || !rec->host) {
         return PL_DHASH_NEXT;
     }
 
     DNSCacheEntries info;
     info.hostname = rec->host;
     info.family = rec->af;
-    info.expiration = ((int64_t)rec->expiration - NowInMinutes()) * 60;
+    info.expiration = (int64_t)(rec->expiration - TimeStamp::NowLoRes()).ToSeconds();
     if (info.expiration <= 0) {
         // We only need valid DNS cache entries
         return PL_DHASH_NEXT;
     }
 
     {
         MutexAutoLock lock(rec->addr_info_lock);
 
--- a/netwerk/dns/nsHostResolver.h
+++ b/netwerk/dns/nsHostResolver.h
@@ -14,16 +14,17 @@
 #include "mozilla/CondVar.h"
 #include "mozilla/Mutex.h"
 #include "nsISupportsImpl.h"
 #include "nsIDNSListener.h"
 #include "nsString.h"
 #include "nsTArray.h"
 #include "mozilla/net/DNS.h"
 #include "mozilla/net/DashboardTypes.h"
+#include "mozilla/TimeStamp.h"
 
 class nsHostResolver;
 class nsHostRecord;
 class nsResolveHostCallback;
 
 #define MAX_RESOLVER_THREADS_FOR_ANY_PRIORITY  3
 #define MAX_RESOLVER_THREADS_FOR_HIGH_PRIORITY 5
 #define MAX_NON_PRIORITY_REQUESTS 150
@@ -72,17 +73,17 @@ public:
     int          addr_info_gencnt; /* generation count of |addr_info| */
     mozilla::net::AddrInfo *addr_info;
     mozilla::net::NetAddr  *addr;
     bool         negative;   /* True if this record is a cache of a failed lookup.
                                 Negative cache entries are valid just like any other
                                 (though never for more than 60 seconds), but a use
                                 of that negative entry forces an asynchronous refresh. */
 
-    uint32_t     expiration; /* measured in minutes since epoch */
+    mozilla::TimeStamp expiration;
 
     bool HasResult() const { return addr_info || addr || negative; }
 
     // hold addr_info_lock when calling the blacklist functions
     bool   Blacklisted(mozilla::net::NetAddr *query);
     void   ResetBlacklist();
     void   ReportUnusable(mozilla::net::NetAddr *addr);
 
@@ -254,17 +255,17 @@ private:
         METHOD_NEGATIVE_HIT = 3,
         METHOD_LITERAL = 4,
         METHOD_OVERFLOW = 5,
         METHOD_NETWORK_FIRST = 6,
         METHOD_NETWORK_SHARED = 7
     };
 
     uint32_t      mMaxCacheEntries;
-    uint32_t      mMaxCacheLifetime;
+    mozilla::TimeDuration mMaxCacheLifetime;
     uint32_t      mGracePeriod;
     Mutex         mLock;
     CondVar       mIdleThreadCV;
     uint32_t      mNumIdleThreads;
     uint32_t      mThreadCount;
     uint32_t      mActiveAnyThreadCount;
     PLDHashTable  mDB;
     PRCList       mHighQ;