costtracker.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum 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
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "encoding/binary"
  19. "math"
  20. "sync"
  21. "sync/atomic"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common/mclock"
  24. "github.com/ethereum/go-ethereum/eth"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/les/flowcontrol"
  27. "github.com/ethereum/go-ethereum/log"
  28. )
  29. const makeCostStats = false // make request cost statistics during operation
  30. var (
  31. // average request cost estimates based on serving time
  32. reqAvgTimeCost = requestCostTable{
  33. GetBlockHeadersMsg: {150000, 30000},
  34. GetBlockBodiesMsg: {0, 700000},
  35. GetReceiptsMsg: {0, 1000000},
  36. GetCodeMsg: {0, 450000},
  37. GetProofsV2Msg: {0, 600000},
  38. GetHelperTrieProofsMsg: {0, 1000000},
  39. SendTxV2Msg: {0, 450000},
  40. GetTxStatusMsg: {0, 250000},
  41. }
  42. // maximum incoming message size estimates
  43. reqMaxInSize = requestCostTable{
  44. GetBlockHeadersMsg: {40, 0},
  45. GetBlockBodiesMsg: {0, 40},
  46. GetReceiptsMsg: {0, 40},
  47. GetCodeMsg: {0, 80},
  48. GetProofsV2Msg: {0, 80},
  49. GetHelperTrieProofsMsg: {0, 20},
  50. SendTxV2Msg: {0, 16500},
  51. GetTxStatusMsg: {0, 50},
  52. }
  53. // maximum outgoing message size estimates
  54. reqMaxOutSize = requestCostTable{
  55. GetBlockHeadersMsg: {0, 556},
  56. GetBlockBodiesMsg: {0, 100000},
  57. GetReceiptsMsg: {0, 200000},
  58. GetCodeMsg: {0, 50000},
  59. GetProofsV2Msg: {0, 4000},
  60. GetHelperTrieProofsMsg: {0, 4000},
  61. SendTxV2Msg: {0, 100},
  62. GetTxStatusMsg: {0, 100},
  63. }
  64. // request amounts that have to fit into the minimum buffer size minBufferMultiplier times
  65. minBufferReqAmount = map[uint64]uint64{
  66. GetBlockHeadersMsg: 192,
  67. GetBlockBodiesMsg: 1,
  68. GetReceiptsMsg: 1,
  69. GetCodeMsg: 1,
  70. GetProofsV2Msg: 1,
  71. GetHelperTrieProofsMsg: 16,
  72. SendTxV2Msg: 8,
  73. GetTxStatusMsg: 64,
  74. }
  75. minBufferMultiplier = 3
  76. )
  77. const (
  78. maxCostFactor = 2 // ratio of maximum and average cost estimates
  79. gfUsageThreshold = 0.5
  80. gfUsageTC = time.Second
  81. gfRaiseTC = time.Second * 200
  82. gfDropTC = time.Second * 50
  83. gfDbKey = "_globalCostFactorV3"
  84. )
  85. // costTracker is responsible for calculating costs and cost estimates on the
  86. // server side. It continuously updates the global cost factor which is defined
  87. // as the number of cost units per nanosecond of serving time in a single thread.
  88. // It is based on statistics collected during serving requests in high-load periods
  89. // and practically acts as a one-dimension request price scaling factor over the
  90. // pre-defined cost estimate table.
  91. //
  92. // The reason for dynamically maintaining the global factor on the server side is:
  93. // the estimated time cost of the request is fixed(hardcoded) but the configuration
  94. // of the machine running the server is really different. Therefore, the request serving
  95. // time in different machine will vary greatly. And also, the request serving time
  96. // in same machine may vary greatly with different request pressure.
  97. //
  98. // In order to more effectively limit resources, we apply the global factor to serving
  99. // time to make the result as close as possible to the estimated time cost no matter
  100. // the server is slow or fast. And also we scale the totalRecharge with global factor
  101. // so that fast server can serve more requests than estimation and slow server can
  102. // reduce request pressure.
  103. //
  104. // Instead of scaling the cost values, the real value of cost units is changed by
  105. // applying the factor to the serving times. This is more convenient because the
  106. // changes in the cost factor can be applied immediately without always notifying
  107. // the clients about the changed cost tables.
  108. type costTracker struct {
  109. db ethdb.Database
  110. stopCh chan chan struct{}
  111. inSizeFactor float64
  112. outSizeFactor float64
  113. factor float64
  114. utilTarget float64
  115. minBufLimit uint64
  116. gfLock sync.RWMutex
  117. reqInfoCh chan reqInfo
  118. totalRechargeCh chan uint64
  119. stats map[uint64][]uint64 // Used for testing purpose.
  120. }
  121. // newCostTracker creates a cost tracker and loads the cost factor statistics from the database.
  122. // It also returns the minimum capacity that can be assigned to any peer.
  123. func newCostTracker(db ethdb.Database, config *eth.Config) (*costTracker, uint64) {
  124. utilTarget := float64(config.LightServ) * flowcontrol.FixedPointMultiplier / 100
  125. ct := &costTracker{
  126. db: db,
  127. stopCh: make(chan chan struct{}),
  128. reqInfoCh: make(chan reqInfo, 100),
  129. utilTarget: utilTarget,
  130. }
  131. if config.LightIngress > 0 {
  132. ct.inSizeFactor = utilTarget / float64(config.LightIngress)
  133. }
  134. if config.LightEgress > 0 {
  135. ct.outSizeFactor = utilTarget / float64(config.LightEgress)
  136. }
  137. if makeCostStats {
  138. ct.stats = make(map[uint64][]uint64)
  139. for code := range reqAvgTimeCost {
  140. ct.stats[code] = make([]uint64, 10)
  141. }
  142. }
  143. ct.gfLoop()
  144. costList := ct.makeCostList(ct.globalFactor() * 1.25)
  145. for _, c := range costList {
  146. amount := minBufferReqAmount[c.MsgCode]
  147. cost := c.BaseCost + amount*c.ReqCost
  148. if cost > ct.minBufLimit {
  149. ct.minBufLimit = cost
  150. }
  151. }
  152. ct.minBufLimit *= uint64(minBufferMultiplier)
  153. return ct, (ct.minBufLimit-1)/bufLimitRatio + 1
  154. }
  155. // stop stops the cost tracker and saves the cost factor statistics to the database
  156. func (ct *costTracker) stop() {
  157. stopCh := make(chan struct{})
  158. ct.stopCh <- stopCh
  159. <-stopCh
  160. if makeCostStats {
  161. ct.printStats()
  162. }
  163. }
  164. // makeCostList returns upper cost estimates based on the hardcoded cost estimate
  165. // tables and the optionally specified incoming/outgoing bandwidth limits
  166. func (ct *costTracker) makeCostList(globalFactor float64) RequestCostList {
  167. maxCost := func(avgTimeCost, inSize, outSize uint64) uint64 {
  168. cost := avgTimeCost * maxCostFactor
  169. inSizeCost := uint64(float64(inSize) * ct.inSizeFactor * globalFactor)
  170. if inSizeCost > cost {
  171. cost = inSizeCost
  172. }
  173. outSizeCost := uint64(float64(outSize) * ct.outSizeFactor * globalFactor)
  174. if outSizeCost > cost {
  175. cost = outSizeCost
  176. }
  177. return cost
  178. }
  179. var list RequestCostList
  180. for code, data := range reqAvgTimeCost {
  181. baseCost := maxCost(data.baseCost, reqMaxInSize[code].baseCost, reqMaxOutSize[code].baseCost)
  182. reqCost := maxCost(data.reqCost, reqMaxInSize[code].reqCost, reqMaxOutSize[code].reqCost)
  183. if ct.minBufLimit != 0 {
  184. // if minBufLimit is set then always enforce maximum request cost <= minBufLimit
  185. maxCost := baseCost + reqCost*minBufferReqAmount[code]
  186. if maxCost > ct.minBufLimit {
  187. mul := 0.999 * float64(ct.minBufLimit) / float64(maxCost)
  188. baseCost = uint64(float64(baseCost) * mul)
  189. reqCost = uint64(float64(reqCost) * mul)
  190. }
  191. }
  192. list = append(list, requestCostListItem{
  193. MsgCode: code,
  194. BaseCost: baseCost,
  195. ReqCost: reqCost,
  196. })
  197. }
  198. return list
  199. }
  200. // reqInfo contains the estimated time cost and the actual request serving time
  201. // which acts as a feed source to update factor maintained by costTracker.
  202. type reqInfo struct {
  203. // avgTimeCost is the estimated time cost corresponding to maxCostTable.
  204. avgTimeCost float64
  205. // servingTime is the CPU time corresponding to the actual processing of
  206. // the request.
  207. servingTime float64
  208. }
  209. // gfLoop starts an event loop which updates the global cost factor which is
  210. // calculated as a weighted average of the average estimate / serving time ratio.
  211. // The applied weight equals the serving time if gfUsage is over a threshold,
  212. // zero otherwise. gfUsage is the recent average serving time per time unit in
  213. // an exponential moving window. This ensures that statistics are collected only
  214. // under high-load circumstances where the measured serving times are relevant.
  215. // The total recharge parameter of the flow control system which controls the
  216. // total allowed serving time per second but nominated in cost units, should
  217. // also be scaled with the cost factor and is also updated by this loop.
  218. func (ct *costTracker) gfLoop() {
  219. var (
  220. factor, totalRecharge float64
  221. gfLog, recentTime, recentAvg float64
  222. lastUpdate, expUpdate = mclock.Now(), mclock.Now()
  223. )
  224. // Load historical cost factor statistics from the database.
  225. data, _ := ct.db.Get([]byte(gfDbKey))
  226. if len(data) == 8 {
  227. gfLog = math.Float64frombits(binary.BigEndian.Uint64(data[:]))
  228. }
  229. ct.factor = math.Exp(gfLog)
  230. factor, totalRecharge = ct.factor, ct.utilTarget*ct.factor
  231. // In order to perform factor data statistics under the high request pressure,
  232. // we only adjust factor when recent factor usage beyond the threshold.
  233. threshold := gfUsageThreshold * float64(gfUsageTC) * ct.utilTarget / flowcontrol.FixedPointMultiplier
  234. go func() {
  235. saveCostFactor := func() {
  236. var data [8]byte
  237. binary.BigEndian.PutUint64(data[:], math.Float64bits(gfLog))
  238. ct.db.Put([]byte(gfDbKey), data[:])
  239. log.Debug("global cost factor saved", "value", factor)
  240. }
  241. saveTicker := time.NewTicker(time.Minute * 10)
  242. for {
  243. select {
  244. case r := <-ct.reqInfoCh:
  245. requestServedMeter.Mark(int64(r.servingTime))
  246. requestEstimatedMeter.Mark(int64(r.avgTimeCost / factor))
  247. requestServedTimer.Update(time.Duration(r.servingTime))
  248. relativeCostHistogram.Update(int64(r.avgTimeCost / factor / r.servingTime))
  249. now := mclock.Now()
  250. dt := float64(now - expUpdate)
  251. expUpdate = now
  252. exp := math.Exp(-dt / float64(gfUsageTC))
  253. // calculate factor correction until now, based on previous values
  254. var gfCorr float64
  255. max := recentTime
  256. if recentAvg > max {
  257. max = recentAvg
  258. }
  259. // we apply continuous correction when MAX(recentTime, recentAvg) > threshold
  260. if max > threshold {
  261. // calculate correction time between last expUpdate and now
  262. if max*exp >= threshold {
  263. gfCorr = dt
  264. } else {
  265. gfCorr = math.Log(max/threshold) * float64(gfUsageTC)
  266. }
  267. // calculate log(factor) correction with the right direction and time constant
  268. if recentTime > recentAvg {
  269. // drop factor if actual serving times are larger than average estimates
  270. gfCorr /= -float64(gfDropTC)
  271. } else {
  272. // raise factor if actual serving times are smaller than average estimates
  273. gfCorr /= float64(gfRaiseTC)
  274. }
  275. }
  276. // update recent cost values with current request
  277. recentTime = recentTime*exp + r.servingTime
  278. recentAvg = recentAvg*exp + r.avgTimeCost/factor
  279. if gfCorr != 0 {
  280. // Apply the correction to factor
  281. gfLog += gfCorr
  282. factor = math.Exp(gfLog)
  283. // Notify outside modules the new factor and totalRecharge.
  284. if time.Duration(now-lastUpdate) > time.Second {
  285. totalRecharge, lastUpdate = ct.utilTarget*factor, now
  286. ct.gfLock.Lock()
  287. ct.factor = factor
  288. ch := ct.totalRechargeCh
  289. ct.gfLock.Unlock()
  290. if ch != nil {
  291. select {
  292. case ct.totalRechargeCh <- uint64(totalRecharge):
  293. default:
  294. }
  295. }
  296. log.Debug("global cost factor updated", "factor", factor)
  297. }
  298. }
  299. recentServedGauge.Update(int64(recentTime))
  300. recentEstimatedGauge.Update(int64(recentAvg))
  301. totalRechargeGauge.Update(int64(totalRecharge))
  302. case <-saveTicker.C:
  303. saveCostFactor()
  304. case stopCh := <-ct.stopCh:
  305. saveCostFactor()
  306. close(stopCh)
  307. return
  308. }
  309. }
  310. }()
  311. }
  312. // globalFactor returns the current value of the global cost factor
  313. func (ct *costTracker) globalFactor() float64 {
  314. ct.gfLock.RLock()
  315. defer ct.gfLock.RUnlock()
  316. return ct.factor
  317. }
  318. // totalRecharge returns the current total recharge parameter which is used by
  319. // flowcontrol.ClientManager and is scaled by the global cost factor
  320. func (ct *costTracker) totalRecharge() uint64 {
  321. ct.gfLock.RLock()
  322. defer ct.gfLock.RUnlock()
  323. return uint64(ct.factor * ct.utilTarget)
  324. }
  325. // subscribeTotalRecharge returns all future updates to the total recharge value
  326. // through a channel and also returns the current value
  327. func (ct *costTracker) subscribeTotalRecharge(ch chan uint64) uint64 {
  328. ct.gfLock.Lock()
  329. defer ct.gfLock.Unlock()
  330. ct.totalRechargeCh = ch
  331. return uint64(ct.factor * ct.utilTarget)
  332. }
  333. // updateStats updates the global cost factor and (if enabled) the real cost vs.
  334. // average estimate statistics
  335. func (ct *costTracker) updateStats(code, amount, servingTime, realCost uint64) {
  336. avg := reqAvgTimeCost[code]
  337. avgTimeCost := avg.baseCost + amount*avg.reqCost
  338. select {
  339. case ct.reqInfoCh <- reqInfo{float64(avgTimeCost), float64(servingTime)}:
  340. default:
  341. }
  342. if makeCostStats {
  343. realCost <<= 4
  344. l := 0
  345. for l < 9 && realCost > avgTimeCost {
  346. l++
  347. realCost >>= 1
  348. }
  349. atomic.AddUint64(&ct.stats[code][l], 1)
  350. }
  351. }
  352. // realCost calculates the final cost of a request based on actual serving time,
  353. // incoming and outgoing message size
  354. //
  355. // Note: message size is only taken into account if bandwidth limitation is applied
  356. // and the cost based on either message size is greater than the cost based on
  357. // serving time. A maximum of the three costs is applied instead of their sum
  358. // because the three limited resources (serving thread time and i/o bandwidth) can
  359. // also be maxed out simultaneously.
  360. func (ct *costTracker) realCost(servingTime uint64, inSize, outSize uint32) uint64 {
  361. cost := float64(servingTime)
  362. inSizeCost := float64(inSize) * ct.inSizeFactor
  363. if inSizeCost > cost {
  364. cost = inSizeCost
  365. }
  366. outSizeCost := float64(outSize) * ct.outSizeFactor
  367. if outSizeCost > cost {
  368. cost = outSizeCost
  369. }
  370. return uint64(cost * ct.globalFactor())
  371. }
  372. // printStats prints the distribution of real request cost relative to the average estimates
  373. func (ct *costTracker) printStats() {
  374. if ct.stats == nil {
  375. return
  376. }
  377. for code, arr := range ct.stats {
  378. log.Info("Request cost statistics", "code", code, "1/16", arr[0], "1/8", arr[1], "1/4", arr[2], "1/2", arr[3], "1", arr[4], "2", arr[5], "4", arr[6], "8", arr[7], "16", arr[8], ">16", arr[9])
  379. }
  380. }
  381. type (
  382. // requestCostTable assigns a cost estimate function to each request type
  383. // which is a linear function of the requested amount
  384. // (cost = baseCost + reqCost * amount)
  385. requestCostTable map[uint64]*requestCosts
  386. requestCosts struct {
  387. baseCost, reqCost uint64
  388. }
  389. // RequestCostList is a list representation of request costs which is used for
  390. // database storage and communication through the network
  391. RequestCostList []requestCostListItem
  392. requestCostListItem struct {
  393. MsgCode, BaseCost, ReqCost uint64
  394. }
  395. )
  396. // getMaxCost calculates the estimated cost for a given request type and amount
  397. func (table requestCostTable) getMaxCost(code, amount uint64) uint64 {
  398. costs := table[code]
  399. return costs.baseCost + amount*costs.reqCost
  400. }
  401. // decode converts a cost list to a cost table
  402. func (list RequestCostList) decode(protocolLength uint64) requestCostTable {
  403. table := make(requestCostTable)
  404. for _, e := range list {
  405. if e.MsgCode < protocolLength {
  406. table[e.MsgCode] = &requestCosts{
  407. baseCost: e.BaseCost,
  408. reqCost: e.ReqCost,
  409. }
  410. }
  411. }
  412. return table
  413. }
  414. // testCostList returns a dummy request cost list used by tests
  415. func testCostList(testCost uint64) RequestCostList {
  416. cl := make(RequestCostList, len(reqAvgTimeCost))
  417. var max uint64
  418. for code := range reqAvgTimeCost {
  419. if code > max {
  420. max = code
  421. }
  422. }
  423. i := 0
  424. for code := uint64(0); code <= max; code++ {
  425. if _, ok := reqAvgTimeCost[code]; ok {
  426. cl[i].MsgCode = code
  427. cl[i].BaseCost = testCost
  428. cl[i].ReqCost = 0
  429. i++
  430. }
  431. }
  432. return cl
  433. }