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/app/rdb/backend/SQLiteDatabase.h>
26 #include <ripple/basics/UptimeClock.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  if (!app.config().reporting() && app.config().useTxTables())
77  {
78  auto const db =
79  dynamic_cast<SQLiteDatabase*>(&app.getRelationalDatabase());
80 
81  if (!db)
82  Throw<std::runtime_error>("Failed to get relational database");
83 
84  auto dbKB = db->getKBUsedAll();
85 
86  if (dbKB > 0)
87  ret[jss::dbKBTotal] = dbKB;
88 
89  dbKB = db->getKBUsedLedger();
90 
91  if (dbKB > 0)
92  ret[jss::dbKBLedger] = dbKB;
93 
94  dbKB = db->getKBUsedTransaction();
95 
96  if (dbKB > 0)
97  ret[jss::dbKBTransaction] = dbKB;
98 
99  {
100  std::size_t c = app.getOPs().getLocalTxCount();
101  if (c > 0)
102  ret[jss::local_txs] = static_cast<Json::UInt>(c);
103  }
104  }
105 
106  ret[jss::write_load] = app.getNodeStore().getWriteLoad();
107 
108  ret[jss::historical_perminute] =
109  static_cast<int>(app.getInboundLedgers().fetchRate());
110  ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
111  ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
112  ret[jss::AL_size] = Json::UInt(app.getAcceptedLedgerCache().size());
113  ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
114 
115  ret[jss::fullbelow_size] =
116  static_cast<int>(app.getNodeFamily().getFullBelowCache(0)->size());
117  ret[jss::treenode_cache_size] =
118  app.getNodeFamily().getTreeNodeCache(0)->getCacheSize();
119  ret[jss::treenode_track_size] =
120  app.getNodeFamily().getTreeNodeCache(0)->getTrackSize();
121 
122  std::string uptime;
123  auto s = UptimeClock::now();
124  using namespace std::chrono_literals;
125  textTime(uptime, s, "year", 365 * 24h);
126  textTime(uptime, s, "day", 24h);
127  textTime(uptime, s, "hour", 1h);
128  textTime(uptime, s, "minute", 1min);
129  textTime(uptime, s, "second", 1s);
130  ret[jss::uptime] = uptime;
131 
132  if (auto shardStore = app.getShardStore())
133  {
134  auto shardFamily{dynamic_cast<ShardFamily*>(app.getShardFamily())};
135  auto const [cacheSz, trackSz] = shardFamily->getTreeNodeCacheSize();
136  Json::Value& jv = (ret[jss::shards] = Json::objectValue);
137 
138  jv[jss::fullbelow_size] = shardFamily->getFullBelowCacheSize();
139  jv[jss::treenode_cache_size] = cacheSz;
140  jv[jss::treenode_track_size] = trackSz;
141  ret[jss::write_load] = shardStore->getWriteLoad();
142  jv[jss::node_writes] = std::to_string(shardStore->getStoreCount());
143  jv[jss::node_reads_total] = shardStore->getFetchTotalCount();
144  jv[jss::node_reads_hit] = shardStore->getFetchHitCount();
145  jv[jss::node_written_bytes] =
146  std::to_string(shardStore->getStoreSize());
147  jv[jss::node_read_bytes] = shardStore->getFetchSize();
148  }
149  else
150  {
151  app.getNodeStore().getCountsJson(ret);
152  }
153 
154  return ret;
155 }
156 
157 // {
158 // min_count: <number> // optional, defaults to 10
159 // }
162 {
163  int minCount = 10;
164 
165  if (context.params.isMember(jss::min_count))
166  minCount = context.params[jss::min_count].asUInt();
167 
168  return getCountsJson(context.app, minCount);
169 }
170 
171 } // namespace ripple
ripple::SQLiteDatabase
Definition: SQLiteDatabase.h:27
ripple::Application
Definition: Application.h:115
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:161
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::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
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::Config::reporting
bool reporting() const
Definition: Config.h:337
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
ripple::Application::config
virtual Config & config()=0
ripple::Application::getRelationalDatabase
virtual RelationalDatabase & getRelationalDatabase()=0
ripple::Config::useTxTables
bool useTxTables() const
Definition: Config.h:343
std::to_string
T to_string(T... args)
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::TaggedCache::rate
double rate() const
Returns the fraction of cache hits.
Definition: TaggedCache.h:475
ripple::NetworkOPs::getLocalTxCount
virtual std::size_t getLocalTxCount()=0
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:95
ripple::Application::getShardFamily
virtual Family * getShardFamily()=0
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
ripple::NodeStore::Database::getCountsJson
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:378
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)
std::size_t
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::LedgerMaster::getCacheHitRate
float getCacheHitRate()
Definition: LedgerMaster.cpp:1881
Json::Value
Represents a JSON value.
Definition: json_value.h:145