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 #include <ripple/shamap/ShardFamily.h>
36 
37 namespace ripple {
38 
39 static void
41  std::string& text,
42  UptimeClock::time_point& seconds,
43  const char* unitName,
44  std::chrono::seconds unitVal)
45 {
46  auto i = seconds.time_since_epoch() / unitVal;
47 
48  if (i == 0)
49  return;
50 
51  seconds -= unitVal * i;
52 
53  if (!text.empty())
54  text += ", ";
55 
56  text += std::to_string(i);
57  text += " ";
58  text += unitName;
59 
60  if (i > 1)
61  text += "s";
62 }
63 
65 getCountsJson(Application& app, int minObjectCount)
66 {
67  auto objectCounts = CountedObjects::getInstance().getCounts(minObjectCount);
68 
70 
71  for (auto const& [k, v] : objectCounts)
72  {
73  ret[k] = v;
74  }
75 
76  int dbKB = getKBUsedAll(app.getLedgerDB().getSession());
77 
78  if (dbKB > 0)
79  ret[jss::dbKBTotal] = dbKB;
80 
81  dbKB = getKBUsedDB(app.getLedgerDB().getSession());
82 
83  if (dbKB > 0)
84  ret[jss::dbKBLedger] = dbKB;
85 
86  dbKB = getKBUsedDB(app.getTxnDB().getSession());
87 
88  if (dbKB > 0)
89  ret[jss::dbKBTransaction] = dbKB;
90 
91  {
93  if (c > 0)
94  ret[jss::local_txs] = static_cast<Json::UInt>(c);
95  }
96 
97  ret[jss::write_load] = app.getNodeStore().getWriteLoad();
98 
99  ret[jss::historical_perminute] =
100  static_cast<int>(app.getInboundLedgers().fetchRate());
101  ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
102  ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
103  ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
104 
105  ret[jss::fullbelow_size] =
106  static_cast<int>(app.getNodeFamily().getFullBelowCache(0)->size());
107  ret[jss::treenode_cache_size] =
108  app.getNodeFamily().getTreeNodeCache(0)->getCacheSize();
109  ret[jss::treenode_track_size] =
110  app.getNodeFamily().getTreeNodeCache(0)->getTrackSize();
111 
112  std::string uptime;
113  auto s = UptimeClock::now();
114  using namespace std::chrono_literals;
115  textTime(uptime, s, "year", 365 * 24h);
116  textTime(uptime, s, "day", 24h);
117  textTime(uptime, s, "hour", 1h);
118  textTime(uptime, s, "minute", 1min);
119  textTime(uptime, s, "second", 1s);
120  ret[jss::uptime] = uptime;
121 
122  ret[jss::node_writes] = std::to_string(app.getNodeStore().getStoreCount());
123  ret[jss::node_reads_total] = app.getNodeStore().getFetchTotalCount();
124  ret[jss::node_reads_hit] = app.getNodeStore().getFetchHitCount();
125  ret[jss::node_written_bytes] =
127  ret[jss::node_read_bytes] = app.getNodeStore().getFetchSize();
128 
129  if (auto shardStore = app.getShardStore())
130  {
131  auto shardFamily{dynamic_cast<ShardFamily*>(app.getShardFamily())};
132  auto const [cacheSz, trackSz] = shardFamily->getTreeNodeCacheSize();
133  Json::Value& jv = (ret[jss::shards] = Json::objectValue);
134 
135  jv[jss::fullbelow_size] = shardFamily->getFullBelowCacheSize();
136  jv[jss::treenode_cache_size] = cacheSz;
137  jv[jss::treenode_track_size] = trackSz;
138  ret[jss::write_load] = shardStore->getWriteLoad();
139  jv[jss::node_writes] = std::to_string(shardStore->getStoreCount());
140  jv[jss::node_reads_total] = shardStore->getFetchTotalCount();
141  jv[jss::node_reads_hit] = shardStore->getFetchHitCount();
142  jv[jss::node_written_bytes] =
143  std::to_string(shardStore->getStoreSize());
144  jv[jss::node_read_bytes] = shardStore->getFetchSize();
145  }
146 
147  return ret;
148 }
149 
150 // {
151 // min_count: <number> // optional, defaults to 10
152 // }
155 {
156  int minCount = 10;
157 
158  if (context.params.isMember(jss::min_count))
159  minCount = context.params[jss::min_count].asUInt();
160 
161  return getCountsJson(context.app, minCount);
162 }
163 
164 } // namespace ripple
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:207
ripple::Application
Definition: Application.h:97
ripple::Application::getNodeFamily
virtual Family & getNodeFamily()=0
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::Family::getTreeNodeCache
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache(std::uint32_t ledgerSeq)=0
Return a pointer to the Family Tree Node Cache.
ripple::doGetCounts
Json::Value doGetCounts(RPC::JsonContext &context)
Definition: GetCounts.cpp:154
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
ripple::NodeStore::Database::getStoreCount
std::uint64_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:183
std::string
STL class.
ripple::getKBUsedDB
size_t getKBUsedDB(soci::session &s)
Definition: SociDB.cpp:141
ripple::getKBUsedAll
size_t getKBUsedAll(soci::session &s)
Definition: SociDB.cpp:132
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:27
std::chrono::seconds
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::Family::getFullBelowCache
virtual std::shared_ptr< FullBelowCache > getFullBelowCache(std::uint32_t ledgerSeq)=0
Return a pointer to the Family Full Below Cache.
ripple::Application::getOPs
virtual NetworkOPs & getOPs()=0
ripple::Application::cachedSLEs
virtual CachedSLEs & cachedSLEs()=0
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:27
ripple::Application::getInboundLedgers
virtual InboundLedgers & getInboundLedgers()=0
ripple::NodeStore::Database::getStoreSize
std::uint64_t getStoreSize() const
Definition: Database.h:201
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:63
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
std::to_string
T to_string(T... args)
ripple::CachedSLEs::rate
double rate() const
Returns the fraction of cache hits.
Definition: CachedSLEs.cpp:48
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
std::chrono::time_point
ripple::ShardFamily
Definition: ShardFamily.h:30
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::Application::getLedgerDB
virtual DatabaseCon & getLedgerDB()=0
ripple::NetworkOPs::getLocalTxCount
virtual std::size_t getLocalTxCount()=0
ripple::DatabaseCon::getSession
soci::session & getSession()
Definition: DatabaseCon.h:170
ripple::getCountsJson
Json::Value getCountsJson(Application &app, int minObjectCount)
Definition: GetCounts.cpp:65
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
ripple::ShardFamily::getTreeNodeCacheSize
std::pair< int, int > getTreeNodeCacheSize()
Return a pair where the first item is the number of items cached and the second item is the number of...
Definition: ShardFamily.cpp:93
ripple::Application::getShardFamily
virtual Family * getShardFamily()=0
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:39
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::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:195
std::size_t
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:189
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::LedgerMaster::getCacheHitRate
float getCacheHitRate()
Definition: LedgerMaster.cpp:1743
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::Application::getTxnDB
virtual DatabaseCon & getTxnDB()=0