Bug 1325088 - Part 2: Expose the index-update to cache entry. r=michal
authorJunior Hsu <juhsu@mozilla.com>
Tue, 28 Mar 2017 12:02:45 +0800
changeset 350486 e7af85a4fd7b918bb4806418d72187c496262d48
parent 350485 913b6b05d4c1ddbdea5e593bd72d41bbdf39896b
child 350487 7bd673cb48e49ba40c28818bec1a3318a2a2a15c
push id88652
push userryanvm@gmail.com
push dateThu, 30 Mar 2017 17:38:50 +0000
treeherdermozilla-inbound@e0f0467f930f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs1325088
milestone55.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 1325088 - Part 2: Expose the index-update to cache entry. r=michal
netwerk/cache2/CacheEntry.cpp
netwerk/cache2/CacheFile.cpp
netwerk/cache2/CacheFile.h
netwerk/cache2/OldWrappers.h
netwerk/cache2/nsICacheEntry.idl
netwerk/protocol/http/nsHttpChannel.cpp
--- a/netwerk/cache2/CacheEntry.cpp
+++ b/netwerk/cache2/CacheEntry.cpp
@@ -1070,16 +1070,36 @@ NS_IMETHODIMP CacheEntry::GetLastModifie
 
 NS_IMETHODIMP CacheEntry::GetExpirationTime(uint32_t *aExpirationTime)
 {
   NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
 
   return mFile->GetExpirationTime(aExpirationTime);
 }
 
+nsresult CacheEntry::GetOnStartTime(uint64_t *aTime)
+{
+  NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
+  return mFile->GetOnStartTime(aTime);
+}
+
+nsresult CacheEntry::GetOnStopTime(uint64_t *aTime)
+{
+  NS_ENSURE_SUCCESS(mFileStatus, NS_ERROR_NOT_AVAILABLE);
+  return mFile->GetOnStopTime(aTime);
+}
+
+nsresult CacheEntry::SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime)
+{
+  if (NS_SUCCEEDED(mFileStatus)) {
+    return mFile->SetNetworkTimes(aOnStartTime, aOnStopTime);
+  }
+  return NS_ERROR_NOT_AVAILABLE;
+}
+
 NS_IMETHODIMP CacheEntry::GetIsForcedValid(bool *aIsForcedValid)
 {
   NS_ENSURE_ARG(aIsForcedValid);
 
   MOZ_ASSERT(mState > LOADING);
 
   if (mPinned) {
     *aIsForcedValid = true;
--- a/netwerk/cache2/CacheFile.cpp
+++ b/netwerk/cache2/CacheFile.cpp
@@ -1193,19 +1193,88 @@ CacheFile::GetFrecency(uint32_t *_retval
 {
   CacheFileAutoLock lock(this);
   MOZ_ASSERT(mMetadata);
   NS_ENSURE_TRUE(mMetadata, NS_ERROR_UNEXPECTED);
 
   return mMetadata->GetFrecency(_retval);
 }
 
+nsresult CacheFile::SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime)
+{
+  CacheFileAutoLock lock(this);
+
+  LOG(("CacheFile::SetNetworkTimes() this=%p, aOnStartTime=%" PRIu64
+       ", aOnStopTime=%" PRIu64 "", this, aOnStartTime, aOnStopTime));
+
+  MOZ_ASSERT(mMetadata);
+  NS_ENSURE_TRUE(mMetadata, NS_ERROR_UNEXPECTED);
+  MOZ_ASSERT(aOnStartTime != kIndexTimeNotAvailable);
+  MOZ_ASSERT(aOnStopTime != kIndexTimeNotAvailable);
+
+  PostWriteTimer();
+
+  nsAutoCString onStartTime;
+  onStartTime.AppendInt(aOnStartTime);
+  nsresult rv = mMetadata->SetElement("net-response-time-onstart", onStartTime.get());
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  nsAutoCString onStopTime;
+  onStopTime.AppendInt(aOnStopTime);
+  rv = mMetadata->SetElement("net-response-time-onstop", onStopTime.get());
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  uint16_t onStartTime16 = aOnStartTime <= kIndexTimeOutOfBound ? aOnStartTime : kIndexTimeOutOfBound;
+  uint16_t onStopTime16 = aOnStopTime <= kIndexTimeOutOfBound ? aOnStopTime : kIndexTimeOutOfBound;
+
+  if (mHandle && !mHandle->IsDoomed()) {
+    CacheFileIOManager::UpdateIndexEntry(mHandle, nullptr, nullptr, nullptr,
+                                         &onStartTime16, &onStopTime16);
+  }
+  return NS_OK;
+}
+
+nsresult CacheFile::GetOnStartTime(uint64_t *_retval)
+{
+  CacheFileAutoLock lock(this);
+
+  MOZ_ASSERT(mMetadata);
+  const char *onStartTimeStr = mMetadata->GetElement("net-response-time-onstart");
+  if (!onStartTimeStr) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  nsresult rv;
+  *_retval = nsCString(onStartTimeStr).ToInteger64(&rv);
+  MOZ_ASSERT(NS_SUCCEEDED(rv));
+  return NS_OK;
+}
+
+nsresult CacheFile::GetOnStopTime(uint64_t *_retval)
+{
+  CacheFileAutoLock lock(this);
+
+  MOZ_ASSERT(mMetadata);
+  const char *onStopTimeStr = mMetadata->GetElement("net-response-time-onstop");
+  if (!onStopTimeStr) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  nsresult rv;
+  *_retval = nsCString(onStopTimeStr).ToInteger64(&rv);
+  MOZ_ASSERT(NS_SUCCEEDED(rv));
+  return NS_OK;
+}
+
 nsresult
 CacheFile::SetAltMetadata(const char* aAltMetadata)
 {
+  AssertOwnsLock();
   LOG(("CacheFile::SetAltMetadata() this=%p, aAltMetadata=%s",
        this, aAltMetadata ? aAltMetadata : ""));
 
   MOZ_ASSERT(mMetadata);
   NS_ENSURE_TRUE(mMetadata, NS_ERROR_UNEXPECTED);
 
   PostWriteTimer();
 
--- a/netwerk/cache2/CacheFile.h
+++ b/netwerk/cache2/CacheFile.h
@@ -97,16 +97,19 @@ public:
   nsresult GetElement(const char *aKey, char **_retval);
   nsresult SetElement(const char *aKey, const char *aValue);
   nsresult VisitMetaData(nsICacheEntryMetaDataVisitor *aVisitor);
   nsresult ElementsSize(uint32_t *_retval);
   nsresult SetExpirationTime(uint32_t aExpirationTime);
   nsresult GetExpirationTime(uint32_t *_retval);
   nsresult SetFrecency(uint32_t aFrecency);
   nsresult GetFrecency(uint32_t *_retval);
+  nsresult SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime);
+  nsresult GetOnStartTime(uint64_t *_retval);
+  nsresult GetOnStopTime(uint64_t *_retval);
   nsresult GetLastModified(uint32_t *_retval);
   nsresult GetLastFetched(uint32_t *_retval);
   nsresult GetFetchCount(uint32_t *_retval);
   nsresult GetDiskStorageSizeInKB(uint32_t *aDiskStorageSize);
   // Called by upper layers to indicated the entry has been fetched,
   // i.e. delivered to the consumer.
   nsresult OnFetched();
 
--- a/netwerk/cache2/OldWrappers.h
+++ b/netwerk/cache2/OldWrappers.h
@@ -130,16 +130,28 @@ public:
   NS_IMETHOD GetExpirationTime(uint32_t *aExpirationTime) override
   {
     return mOldInfo->GetExpirationTime(aExpirationTime);
   }
   nsresult GetDataSize(uint32_t *aDataSize)
   {
     return mOldInfo->GetDataSize(aDataSize);
   }
+  NS_IMETHOD GetOnStartTime(uint64_t *aTime) override
+  {
+    return NS_ERROR_NOT_IMPLEMENTED;
+  }
+  NS_IMETHOD GetOnStopTime(uint64_t *aTime) override
+  {
+    return NS_ERROR_NOT_IMPLEMENTED;
+  }
+  NS_IMETHOD SetNetworkTimes(uint64_t aOnStartTime, uint64_t aOnStopTime) override
+  {
+    return NS_ERROR_NOT_IMPLEMENTED;
+  }
   NS_IMETHOD GetLoadContextInfo(nsILoadContextInfo** aInfo) override
   {
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
   NS_IMETHOD AsyncDoom(nsICacheEntryDoomCallback* listener) override;
   NS_IMETHOD GetPersistent(bool *aPersistToDisk) override;
   NS_IMETHOD GetIsForcedValid(bool *aIsForcedValid) override;
--- a/netwerk/cache2/nsICacheEntry.idl
+++ b/netwerk/cache2/nsICacheEntry.idl
@@ -56,16 +56,29 @@ interface nsICacheEntry : nsISupports
 
   /**
    * Set the time at which the cache entry should be considered invalid (in
    * seconds since the Epoch).
    */
   void setExpirationTime(in uint32_t expirationTime);
 
   /**
+   * Get the last network response times for onStartReqeust/onStopRequest (in ms).
+   * @throws
+   *    - NS_ERROR_NOT_AVAILABLE if onStartTime/onStopTime does not exist.
+   */
+  readonly attribute uint64_t  onStartTime;
+  readonly attribute uint64_t  onStopTime;
+
+  /**
+   * Set the network response times for onStartReqeust/onStopRequest (in ms).
+   */
+  void setNetworkTimes(in uint64_t onStartTime, in uint64_t onStopTime);
+
+  /**
    * This method is intended to override the per-spec cache validation
    * decisions for a duration specified in seconds. The current state can
    * be examined with isForcedValid (see below). This value is not persisted,
    * so it will not survive session restart. Cache entries that are forced valid
    * will not be evicted from the cache for the duration of forced validity.
    * This means that there is a potential problem if the number of forced valid
    * entries grows to take up more space than the cache size allows.
    *
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -6910,23 +6910,19 @@ nsHttpChannel::OnStopRequest(nsIRequest 
         // time since async open in the cache entry so we can compare telemetry
         // between cache and net response.
         // Do not store the time of conditional requests because even if we
         // fetch the data from the server, the time includes loading of the old
         // cache entry which would skew the network load time.
         if (request == mTransactionPump && mCacheEntry && !mDidReval &&
             !mCustomConditionalRequest &&
             !mAsyncOpenTime.IsNull() && !mOnStartRequestTimestamp.IsNull()) {
-            nsAutoCString onStartTime;
-            onStartTime.AppendInt( (uint64_t) (mOnStartRequestTimestamp - mAsyncOpenTime).ToMilliseconds());
-            mCacheEntry->SetMetaDataElement("net-response-time-onstart", onStartTime.get());
-
-            nsAutoCString responseTime;
-            responseTime.AppendInt( (uint64_t) (TimeStamp::Now() - mAsyncOpenTime).ToMilliseconds());
-            mCacheEntry->SetMetaDataElement("net-response-time-onstop", responseTime.get());
+            uint64_t onStartTime = (mOnStartRequestTimestamp - mAsyncOpenTime).ToMilliseconds();
+            uint64_t onStopTime = (TimeStamp::Now() - mAsyncOpenTime).ToMilliseconds();
+            Unused << mCacheEntry->SetNetworkTimes(onStartTime, onStopTime);
         }
 
         // at this point, we're done with the transaction
         mTransactionTimings = mTransaction->Timings();
         mTransaction = nullptr;
         mTransactionPump = nullptr;
 
         // We no longer need the dns prefetch object
@@ -8604,35 +8600,23 @@ nsHttpChannel::ReportNetVSCacheTelemetry
 
     // We only report telemetry if the entry is persistent (on disk)
     bool persistent;
     rv = mCacheEntry->GetPersistent(&persistent);
     if (NS_FAILED(rv) || !persistent) {
         return;
     }
 
-    nsXPIDLCString tmpStr;
-    rv = mCacheEntry->GetMetaDataElement("net-response-time-onstart",
-                                         getter_Copies(tmpStr));
-    if (NS_FAILED(rv)) {
-        return;
-    }
-    uint64_t onStartNetTime = tmpStr.ToInteger64(&rv);
-    if (NS_FAILED(rv)) {
+    uint64_t onStartNetTime = 0;
+    if (NS_FAILED(mCacheEntry->GetOnStartTime(&onStartNetTime))) {
         return;
     }
 
-    tmpStr.Truncate();
-    rv = mCacheEntry->GetMetaDataElement("net-response-time-onstop",
-                                         getter_Copies(tmpStr));
-    if (NS_FAILED(rv)) {
-        return;
-    }
-    uint64_t onStopNetTime = tmpStr.ToInteger64(&rv);
-    if (NS_FAILED(rv)) {
+    uint64_t onStopNetTime = 0;
+    if (NS_FAILED(mCacheEntry->GetOnStopTime(&onStopNetTime))) {
         return;
     }
 
     uint64_t onStartCacheTime = (mOnStartRequestTimestamp - mAsyncOpenTime).ToMilliseconds();
     int64_t onStartDiff = onStartNetTime - onStartCacheTime;
     onStartDiff = ComputeTelemetryBucketNumber(onStartDiff);
 
     uint64_t onStopCacheTime = (mCacheReadEnd - mAsyncOpenTime).ToMilliseconds();