xpcom/tests/TestDeadlockDetectorScalability.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: sw=4 ts=4 et :
     3  * This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "TestHarness.h"
     9 //#define OLD_API
    11 #define PASS()                                  \
    12     do {                                        \
    13         passed(__FUNCTION__);                   \
    14         return NS_OK;                           \
    15     } while (0)
    17 #define FAIL(why)                               \
    18     do {                                        \
    19         fail("%s | %s - %s", __FILE__, __FUNCTION__, why); \
    20         return NS_ERROR_FAILURE;                \
    21     } while (0)
    23 #ifdef OLD_API
    24 #  include "nsAutoLock.h"
    25    typedef PRLock* moz_lock_t;
    26 #  define NEWLOCK(n) nsAutoLock::NewLock(n)
    27 #  define DELETELOCK(v) nsAutoLock::DestroyLock(v)
    28 #  define AUTOLOCK(v, l) nsAutoLock v(l)
    29 #else
    30 #  include "mozilla/Mutex.h"
    31    typedef mozilla::Mutex* moz_lock_t;
    32 #  define NEWLOCK(n) new mozilla::Mutex(n)
    33 #  define DELETELOCK(v) delete (v)
    34 #  define AUTOLOCK(v, l) mozilla::MutexAutoLock v(*l)
    35 #endif
    37 // def/undef these to run particular tests.
    38 #undef DD_TEST1
    39 #undef DD_TEST2
    40 #undef DD_TEST3
    42 //-----------------------------------------------------------------------------
    44 #ifdef DD_TEST1
    46 static void
    47 AllocLockRecurseUnlockFree(int i)
    48 {
    49     if (0 == i)
    50         return;
    52     moz_lock_t lock = NEWLOCK("deadlockDetector.scalability.t1");
    53     {
    54         AUTOLOCK(_, lock);
    55         AllocLockRecurseUnlockFree(i - 1);
    56     }
    57     DELETELOCK(lock);
    58 }
    60 // This test creates a resource dependency chain N elements long, then
    61 // frees all the resources in the chain.
    62 static nsresult
    63 LengthNDepChain(int N)
    64 {
    65     AllocLockRecurseUnlockFree(N);
    66     PASS();
    67 }
    69 #endif
    71 //-----------------------------------------------------------------------------
    73 #ifdef DD_TEST2
    75 // This test creates a single lock that is ordered < N resources, then
    76 // repeatedly exercises this order k times.
    77 static nsresult
    78 OneLockNDeps(const int N, const int K)
    79 {
    80     moz_lock_t lock = NEWLOCK("deadlockDetector.scalability.t2.master");
    81     moz_lock_t* locks = new moz_lock_t[N];
    82     if (!locks)
    83         NS_RUNTIMEABORT("couldn't allocate lock array");
    85     for (int i = 0; i < N; ++i)
    86         locks[i] =
    87             NEWLOCK("deadlockDetector.scalability.t2.dep");
    89     // establish orders
    90     {AUTOLOCK(m, lock);
    91         for (int i = 0; i < N; ++i)
    92             AUTOLOCK(s, locks[i]);
    93     }
    95     // exercise order check
    96     {AUTOLOCK(m, lock);
    97         for (int i = 0; i < K; ++i)
    98             for (int j = 0; j < N; ++j)
    99                 AUTOLOCK(s, locks[i]);
   100     }
   102     for (int i = 0; i < N; ++i)
   103         DELETELOCK(locks[i]);
   104     delete[] locks;
   106     PASS();
   107 }
   109 #endif
   111 //-----------------------------------------------------------------------------
   113 #ifdef DD_TEST3
   115 // This test creates N resources and adds the theoretical maximum number
   116 // of dependencies, O(N^2).  It then repeats that sequence of
   117 // acquisitions k times.  Finally, all resources are freed.
   118 //
   119 // It's very difficult to perform well on this test.  It's put forth as a
   120 // challenge problem.
   122 static nsresult
   123 MaxDepsNsq(const int N, const int K)
   124 {
   125     moz_lock_t* locks = new moz_lock_t[N];
   126     if (!locks)
   127         NS_RUNTIMEABORT("couldn't allocate lock array");
   129     for (int i = 0; i < N; ++i)
   130         locks[i] = NEWLOCK("deadlockDetector.scalability.t3");
   132     for (int i = 0; i < N; ++i) {
   133         AUTOLOCK(al1, locks[i]);
   134         for (int j = i+1; j < N; ++j)
   135             AUTOLOCK(al2, locks[j]);
   136     }
   138     for (int i = 0; i < K; ++i) {
   139         for (int j = 0; j < N; ++j) {
   140             AUTOLOCK(al1, locks[j]);
   141             for (int k = j+1; k < N; ++k)
   142                 AUTOLOCK(al2, locks[k]);
   143         }
   144     }
   146     for (int i = 0; i < N; ++i)
   147         DELETELOCK(locks[i]);
   148     delete[] locks;
   150     PASS();
   151 }
   153 #endif
   155 //-----------------------------------------------------------------------------
   157 int
   158 main(int argc, char** argv)
   159 {
   160     ScopedXPCOM xpcom("Deadlock detector scalability (" __FILE__ ")");
   161     if (xpcom.failed())
   162         return 1;
   164     int rv = 0;
   166     // Uncomment these tests to run them.  Not expected to be common.
   168 #ifndef DD_TEST1
   169     puts("Skipping not-requested LengthNDepChain() test");
   170 #else
   171     if (NS_FAILED(LengthNDepChain(1 << 14))) // 16K
   172         rv = 1;
   173 #endif
   175 #ifndef DD_TEST2
   176     puts("Skipping not-requested OneLockNDeps() test");
   177 #else
   178     if (NS_FAILED(OneLockNDeps(1 << 14, 100))) // 16k
   179         rv = 1;
   180 #endif
   182 #ifndef DD_TEST3
   183     puts("Skipping not-requested MaxDepsNsq() test");
   184 #else
   185     if (NS_FAILED(MaxDepsNsq(1 << 10, 10))) // 1k
   186         rv = 1;
   187 #endif
   189     return rv;
   190 }

mercurial