rippled
GetCounts.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/AcceptedLedger.h>
21 #include <ripple/app/ledger/InboundLedgers.h>
22 #include <ripple/app/ledger/LedgerMaster.h>
23 #include <ripple/app/main/Application.h>
24 #include <ripple/app/misc/NetworkOPs.h>
25 #include <ripple/basics/UptimeClock.h>
26 #include <ripple/core/DatabaseCon.h>
27 #include <ripple/json/json_value.h>
28 #include <ripple/ledger/CachedSLEs.h>
29 #include <ripple/net/RPCErr.h>
30 #include <ripple/nodestore/Database.h>
31 #include <ripple/nodestore/DatabaseShard.h>
32 #include <ripple/protocol/ErrorCodes.h>
33 #include <ripple/protocol/jss.h>
34 #include <ripple/rpc/Context.h>
35 
36 namespace ripple {
37 
38 static
39 void
41  const char* unitName, std::chrono::seconds unitVal)
42 {
43  auto i = seconds.time_since_epoch() / unitVal;
44 
45  if (i == 0)
46  return;
47 
48  seconds -= unitVal * i;
49 
50  if (!text.empty ())
51  text += ", ";
52 
53  text += std::to_string(i);
54  text += " ";
55  text += unitName;
56 
57  if (i > 1)
58  text += "s";
59 }
60 
61 Json::Value getCountsJson(Application& app, int minObjectCount)
62 {
63  auto objectCounts = CountedObjects::getInstance().getCounts(minObjectCount);
64 
66 
67  for (auto const& [k, v] : objectCounts)
68  {
69  ret [k] = v;
70  }
71 
72  int dbKB = getKBUsedAll (app.getLedgerDB ().getSession ());
73 
74  if (dbKB > 0)
75  ret[jss::dbKBTotal] = dbKB;
76 
77  dbKB = getKBUsedDB (app.getLedgerDB ().getSession ());
78 
79  if (dbKB > 0)
80  ret[jss::dbKBLedger] = dbKB;
81 
82  dbKB = getKBUsedDB (app.getTxnDB ().getSession ());
83 
84  if (dbKB > 0)
85  ret[jss::dbKBTransaction] = dbKB;
86 
87  {
88  std::size_t c = app.getOPs().getLocalTxCount ();
89  if (c > 0)
90  ret[jss::local_txs] = static_cast<Json::UInt> (c);
91  }
92 
93  ret[jss::write_load] = app.getNodeStore ().getWriteLoad ();
94 
95  ret[jss::historical_perminute] = static_cast<int>(
97  ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
98  ret[jss::node_hit_rate] = app.getNodeStore ().getCacheHitRate ();
99  ret[jss::ledger_hit_rate] = app.getLedgerMaster ().getCacheHitRate ();
100  ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache ().getHitRate ();
101 
102  ret[jss::fullbelow_size] = static_cast<int>(app.family().fullbelow().size());
103  ret[jss::treenode_cache_size] = app.family().treecache().getCacheSize();
104  ret[jss::treenode_track_size] = app.family().treecache().getTrackSize();
105 
106  std::string uptime;
107  auto s = UptimeClock::now();
108  using namespace std::chrono_literals;
109  textTime (uptime, s, "year", 365 * 24h);
110  textTime (uptime, s, "day", 24h);
111  textTime (uptime, s, "hour", 1h);
112  textTime (uptime, s, "minute", 1min);
113  textTime (uptime, s, "second", 1s);
114  ret[jss::uptime] = uptime;
115 
116  ret[jss::node_writes] = app.getNodeStore().getStoreCount();
117  ret[jss::node_reads_total] = app.getNodeStore().getFetchTotalCount();
118  ret[jss::node_reads_hit] = app.getNodeStore().getFetchHitCount();
119  ret[jss::node_written_bytes] = app.getNodeStore().getStoreSize();
120  ret[jss::node_read_bytes] = app.getNodeStore().getFetchSize();
121 
122  if (auto shardStore = app.getShardStore())
123  {
124  Json::Value& jv = (ret[jss::shards] = Json::objectValue);
125  jv[jss::fullbelow_size] =
126  static_cast<int>(app.shardFamily()->fullbelow().size());
127  jv[jss::treenode_cache_size] =
129  jv[jss::treenode_track_size] =
131  ret[jss::write_load] = shardStore->getWriteLoad();
132  ret[jss::node_hit_rate] = shardStore->getCacheHitRate();
133  jv[jss::node_writes] = shardStore->getStoreCount();
134  jv[jss::node_reads_total] = shardStore->getFetchTotalCount();
135  jv[jss::node_reads_hit] = shardStore->getFetchHitCount();
136  jv[jss::node_written_bytes] = shardStore->getStoreSize();
137  jv[jss::node_read_bytes] = shardStore->getFetchSize();
138  }
139 
140  return ret;
141 }
142 
143 // {
144 // min_count: <number> // optional, defaults to 10
145 // }
147 {
148  int minCount = 10;
149 
150  if (context.params.isMember (jss::min_count))
151  minCount = context.params[jss::min_count].asUInt ();
152 
153  return getCountsJson(context.app, minCount);
154 }
155 
156 } // ripple
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:205
ripple::Application
Definition: Application.h:85
ripple::RPC::JsonContext
Definition: Context.h:52
ripple::doGetCounts
Json::Value doGetCounts(RPC::JsonContext &context)
Definition: GetCounts.cpp:146
ripple::NodeStore::Database::getWriteLoad
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
ripple::Application::getAcceptedLedgerCache
virtual TaggedCache< uint256, AcceptedLedger > & getAcceptedLedgerCache()=0
std::string
STL class.
ripple::getKBUsedDB
size_t getKBUsedDB(soci::session &s)
Definition: SociDB.cpp:136
ripple::getKBUsedAll
size_t getKBUsedAll(soci::session &s)
Definition: SociDB.cpp:129
ripple::InboundLedgers::fetchRate
virtual std::size_t fetchRate()=0
Returns the rate of historical ledger fetches per minute.
Json::UInt
unsigned int UInt
Definition: json_forwards.h:28
std::chrono::seconds
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::NodeStore::Database::getCacheHitRate
virtual float getCacheHitRate()=0
Get the positive cache hits to total attempts ratio.
ripple::Application::getOPs
virtual NetworkOPs & getOPs()=0
ripple::Application::cachedSLEs
virtual CachedSLEs & cachedSLEs()=0
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:25
ripple::Application::getInboundLedgers
virtual InboundLedgers & getInboundLedgers()=0
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::Application::shardFamily
virtual Family * shardFamily()=0
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:64
ripple::NodeStore::Database::getStoreSize
std::uint32_t getStoreSize() const
Definition: Database.h:202
ripple::Application::family
virtual Family & family()=0
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::TaggedCache::getCacheSize
int getCacheSize() const
Definition: TaggedCache.h:123
std::to_string
T to_string(T... args)
ripple::CachedSLEs::rate
double rate() const
Returns the fraction of cache hits.
Definition: CachedSLEs.cpp:50
ripple::NodeStore::Database::getStoreCount
std::uint32_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:193
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
std::chrono::time_point
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:961
ripple::Application::getLedgerDB
virtual DatabaseCon & getLedgerDB()=0
ripple::NetworkOPs::getLocalTxCount
virtual std::size_t getLocalTxCount()=0
ripple::TaggedCache::getTrackSize
int getTrackSize() const
Definition: TaggedCache.h:129
ripple::Family::treecache
virtual TreeNodeCache & treecache()=0
ripple::DatabaseCon::getSession
soci::session & getSession()
Definition: DatabaseCon.h:118
ripple::getCountsJson
Json::Value getCountsJson(Application &app, int minObjectCount)
Definition: GetCounts.cpp:61
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::getNodeStore
virtual NodeStore::Database & getNodeStore()=0
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:555
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:38
ripple::textTime
static void textTime(std::string &text, UptimeClock::time_point &seconds, const char *unitName, std::chrono::seconds unitVal)
Definition: GetCounts.cpp:40
std::string::empty
T empty(T... args)
ripple::detail::BasicFullBelowCache::size
size_type size() const
Return the number of elements in the cache.
Definition: FullBelowCache.h:80
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:199
std::size_t
ripple::Family::fullbelow
virtual FullBelowCache & fullbelow()=0
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:196
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
ripple::LedgerMaster::getCacheHitRate
float getCacheHitRate()
Definition: LedgerMaster.cpp:1716
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::Application::getTxnDB
virtual DatabaseCon & getTxnDB()=0