1 | /* |
2 | Copyright (C) 2005 Apple Inc. All rights reserved. |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include <wtf/HashTable.h> |
22 | |
23 | #include <mutex> |
24 | #include <wtf/DataLog.h> |
25 | |
26 | namespace WTF { |
27 | |
28 | #if DUMP_HASHTABLE_STATS |
29 | |
30 | std::atomic<unsigned> HashTableStats::numAccesses; |
31 | std::atomic<unsigned> HashTableStats::numRehashes; |
32 | std::atomic<unsigned> HashTableStats::numRemoves; |
33 | std::atomic<unsigned> HashTableStats::numReinserts; |
34 | |
35 | unsigned HashTableStats::numCollisions; |
36 | unsigned HashTableStats::collisionGraph[4096]; |
37 | unsigned HashTableStats::maxCollisions; |
38 | |
39 | static Lock hashTableStatsMutex; |
40 | |
41 | void HashTableStats::recordCollisionAtCount(unsigned count) |
42 | { |
43 | std::lock_guard<Lock> lock(hashTableStatsMutex); |
44 | |
45 | if (count > maxCollisions) |
46 | maxCollisions = count; |
47 | numCollisions++; |
48 | collisionGraph[count]++; |
49 | } |
50 | |
51 | void HashTableStats::dumpStats() |
52 | { |
53 | std::lock_guard<Lock> lock(hashTableStatsMutex); |
54 | |
55 | dataLogF("\nWTF::HashTable statistics\n\n" ); |
56 | dataLogF("%u accesses\n" , numAccesses.load()); |
57 | dataLogF("%d total collisions, average %.2f probes per access\n" , numCollisions, 1.0 * (numAccesses + numCollisions) / numAccesses); |
58 | dataLogF("longest collision chain: %d\n" , maxCollisions); |
59 | for (unsigned i = 1; i <= maxCollisions; i++) { |
60 | dataLogF(" %u lookups with exactly %u collisions (%.2f%% , %.2f%% with this many or more)\n" , collisionGraph[i], i, 100.0 * (collisionGraph[i] - collisionGraph[i+1]) / numAccesses, 100.0 * collisionGraph[i] / numAccesses); |
61 | } |
62 | dataLogF("%d rehashes\n" , numRehashes.load()); |
63 | dataLogF("%d reinserts\n" , numReinserts.load()); |
64 | } |
65 | |
66 | #endif |
67 | |
68 | } // namespace WTF |
69 | |