1 package runtime
2 3 // Per-arena scope attribution.
4 //
5 // The question this answers, concretely and without guessing: WHICH function's
6 // arena is holding the bytes that N requests left behind?
7 //
8 // An arena is tagged with the scope that was current when it was pushed, and
9 // every allocation into it and every reclaim of it is charged to that tag. A
10 // scope normally opens and closes with a function: the shell sets one around a
11 // phase of request handling so a retained byte is charged to the phase that
12 // allocated it. The report then shows, per scope, allocated vs reclaimed - and
13 // the difference is exactly the retention, with a name attached.
14 //
15 // Cost: one int32 store on push, one 64-bit add per allocation, one add plus a
16 // reset on reclaim. Nothing here allocates, so the instrument cannot perturb
17 // what it measures.
18 19 const arenaScopeMax = int32(64)
20 21 // Scopes the runtime itself names. Everything else is the shell's: leaving 0
22 // unnamed is deliberate, so that an allocation nobody accounted for is visible
23 // as scope 0 instead of hiding inside the root arena's row (which is exactly how
24 // the group leak was misread as "root retention" for several rounds).
25 const ArenaScopeRoot = int32(5)
26 27 type arenaScopeRec struct {
28 alloc uint64
29 freed uint64
30 resets int32
31 arenas int32
32 }
33 34 var arenaScopes [arenaScopeMax]arenaScopeRec
35 var arenaScopeCurrent int32
36 37 // --- allocation-size histogram (phase 0b) ---
38 //
39 // The phase 4 header size (D8), the slab decision (D9) and the frame budget
40 // question are all read from this, so it is always on. The cost is one bin
41 // computation and two adds per allocation; the bin is floor(log2(size)), so a
42 // question about "objects at or below 32 bytes" is bins 0..5 and "just above
43 // 64 KiB" is bin 16.
44 //
45 // The two slices are carved from the DOMAIN segment by initDomain and are
46 // therefore per-domain TLS, like every other runtime table: a worker's
47 // histogram is its own.
48 const allocHistBins = int32(64)
49 50 var allocHistCount []uint64
51 var allocHistBytes []uint64
52 53 // sizeBin is floor(log2(size)) without a library call or a loop. TargetBits
54 // gates the 64-bit step because a shift of a 32-bit uintptr by 32 is not that
55 // comparison on wasm.
56 func sizeBin(size uintptr) (b int32) {
57 if TargetBits == 64 && size > 0xFFFFFFFF {
58 b = b + 32
59 size = size >> 32
60 }
61 if size >= 1<<16 {
62 b = b + 16
63 size = size >> 16
64 }
65 if size >= 1<<8 {
66 b = b + 8
67 size = size >> 8
68 }
69 if size >= 1<<4 {
70 b = b + 4
71 size = size >> 4
72 }
73 if size >= 1<<2 {
74 b = b + 2
75 size = size >> 2
76 }
77 if size >= 1<<1 {
78 b = b + 1
79 }
80 return b
81 }
82 83 // recordAllocSize charges one allocation to its size bin. A zero-length table
84 // is the pre-initDomain state: initDomain itself allocates nothing through the
85 // allocator, so nothing is ever dropped.
86 func recordAllocSize(size uintptr) {
87 if len(allocHistCount) == 0 {
88 return
89 }
90 b := sizeBin(size)
91 if b >= allocHistBins {
92 b = allocHistBins - 1
93 }
94 allocHistCount[b]++
95 allocHistBytes[b] += uint64(size)
96 }
97 98 // AllocHistogram returns the size histogram (floor(log2) bins, count and
99 // bytes). It is the measurement D8 and D9 are decided from.
100 func AllocHistogram() (counts []uint64, bytes []uint64) {
101 return allocHistCount, allocHistBytes
102 }
103 104 // SegmentStats is the per-kind ledger: live segments, bytes reserved and
105 // bytes allocated. A segment is a mapping; the arena's region table is its
106 // descriptor, so these counters and that table must agree at every quiescent
107 // point.
108 type SegmentStats struct {
109 FnCount uint64
110 FnBytes uint64
111 RootCount uint64
112 RootBytes uint64
113 LargeCount uint64
114 LargeBytes uint64
115 DomainCount uint64
116 DomainBytes uint64
117 ChanCount uint64
118 ChanBytes uint64
119 }
120 121 var segFnCount uint64
122 var segFnBytes uint64
123 var segRootCount uint64
124 var segRootBytes uint64
125 var segLargeCount uint64
126 var segLargeBytes uint64
127 var segDomainCount uint64
128 var segDomainBytes uint64
129 var segChanCount uint64
130 var segChanBytes uint64
131 132 // RecordSegmentAdd charges one mapping to its kind.
133 func RecordSegmentAdd(kind int32, bytes uintptr) {
134 switch kind {
135 case segKindFn:
136 segFnCount++
137 segFnBytes += uint64(bytes)
138 case segKindRoot:
139 segRootCount++
140 segRootBytes += uint64(bytes)
141 case segKindLarge:
142 segLargeCount++
143 segLargeBytes += uint64(bytes)
144 case segKindDomain:
145 segDomainCount++
146 segDomainBytes += uint64(bytes)
147 case segKindChannel:
148 segChanCount++
149 segChanBytes += uint64(bytes)
150 }
151 }
152 153 // RecordSegmentFree retires one mapping from its kind's ledger.
154 func RecordSegmentFree(kind int32, bytes uintptr) {
155 switch kind {
156 case segKindFn:
157 segFnCount--
158 segFnBytes -= uint64(bytes)
159 case segKindRoot:
160 segRootCount--
161 segRootBytes -= uint64(bytes)
162 case segKindLarge:
163 segLargeCount--
164 segLargeBytes -= uint64(bytes)
165 case segKindDomain:
166 segDomainCount--
167 segDomainBytes -= uint64(bytes)
168 case segKindChannel:
169 segChanCount--
170 segChanBytes -= uint64(bytes)
171 }
172 }
173 174 // ReadSegmentStats copies the ledger into s.
175 func ReadSegmentStats(s *SegmentStats) {
176 s.FnCount = segFnCount
177 s.FnBytes = segFnBytes
178 s.RootCount = segRootCount
179 s.RootBytes = segRootBytes
180 s.LargeCount = segLargeCount
181 s.LargeBytes = segLargeBytes
182 s.DomainCount = segDomainCount
183 s.DomainBytes = segDomainBytes
184 s.ChanCount = segChanCount
185 s.ChanBytes = segChanBytes
186 }
187 188 // ArenaLedger reports the function-tier book balance: pushes, releases, the
189 // live frame depth and the pooled arenas. At quiescence pushed == released and
190 // depth == 0; anything else is a release path that did not run, and the two
191 // numbers name it.
192 func ArenaLedger() (pushed, released, depth, pooled uint64) {
193 pushed = fnPushCount
194 released = fnReleaseCount
195 if domainRoot != nil {
196 depth = uint64(uint32(domainRoot.fnTop))
197 }
198 pooled = uint64(uint32(arenaPoolN))
199 return
200 }
201 202 // SysMapStats reports the process-level mapping ledger. Phase 2's exit
203 // criterion is that a warm allocating loop stops mapping: two reads around the
204 // loop with equal counts is that evidence, and it is the one measurement the
205 // segment pool exists to make true.
206 func SysMapStats() (maps, unmaps, mapBytes, unmapBytes uint64) {
207 maps = sysMapCount
208 unmaps = sysUnmapCount
209 mapBytes = sysMapBytes
210 unmapBytes = sysUnmapBytes
211 return
212 }
213 214 // ArenaDomainExitCheck runs at the end of a spawned domain, where the thread is
215 // about to end and every frame it owns should already be gone. It reports only
216 // when the ledger does not balance and only under the trace gate, because a
217 // correct program's exit must stay silent.
218 func ArenaDomainExitCheck() {
219 if domainRoot == nil || !arenaTraceWanted() {
220 return
221 }
222 pushed, released, depth, pooled := ArenaLedger()
223 if pushed == released && depth == 0 {
224 return
225 }
226 printstring("arena domain-exit pushed=")
227 printuint64(pushed)
228 printstring(" released=")
229 printuint64(released)
230 printstring(" depth=")
231 printuint64(depth)
232 printstring(" pooled=")
233 printuint64(pooled)
234 printstring("\n")
235 }
236 237 // SetArenaScope names the scope that arenas pushed from now on belong to, and
238 // returns the previous one so a caller can restore it.
239 func SetArenaScope(id int32) (prev int32) {
240 prev = arenaScopeCurrent
241 if id >= 0 && id < arenaScopeMax {
242 arenaScopeCurrent = id
243 }
244 return prev
245 }
246 247 // ArenaScope reports the current scope.
248 func ArenaScope() (id int32) {
249 return arenaScopeCurrent
250 }
251 252 // --- region accounting ---
253 //
254 // A region is the unit an arena grows by. One that is added and never unmapped
255 // costs its whole mapping whether or not its bytes are reported as "used", which
256 // is why the byte counters can look clean while RSS climbs.
257 var regionAddCount [arenaScopeMax]uint64
258 var regionAddBytes [arenaScopeMax]uint64
259 var regionFreeCount [arenaScopeMax]uint64
260 var regionFreeBytes [arenaScopeMax]uint64
261 var sysMapCount uint64
262 var sysMapBytes uint64
263 var sysUnmapCount uint64
264 var sysUnmapBytes uint64
265 266 // RecordRegionAdd charges one region acquisition to a scope.
267 func RecordRegionAdd(id int32, bytes uintptr) {
268 if id >= 0 && id < arenaScopeMax {
269 regionAddCount[id]++
270 regionAddBytes[id] += uint64(bytes)
271 }
272 }
273 274 // RecordRegionFree charges region releases to a scope.
275 func RecordRegionFree(id int32, count uintptr, bytes uintptr) {
276 if id >= 0 && id < arenaScopeMax {
277 regionFreeCount[id] += uint64(count)
278 regionFreeBytes[id] += uint64(bytes)
279 }
280 }
281 282 // RecordSysMap / RecordSysUnmap count the process-level mappings.
283 func RecordSysMap(bytes uintptr) {
284 sysMapCount++
285 sysMapBytes += uint64(bytes)
286 }
287 288 func RecordSysUnmap(bytes uintptr) {
289 sysUnmapCount++
290 sysUnmapBytes += uint64(bytes)
291 }
292 293 // --- sovereign group accounting ---
294 //
295 // A sovereign group is the arena set that a self-mutating type's receiver owns.
296 // Its arenas are the only ones that double (growDouble), so their region adds
297 // are counted apart from the ordinary 64MB ones, and created vs released is
298 // the direct test of the binding rule: a group lives exactly as long as the
299 // arena that holds its receiver. If created climbs and released does not, the
300 // groups outlive their receivers, and `regionadd` says how fast the arenas they
301 // keep alive are growing.
302 var sovCreated uint64
303 var sovReleased uint64
304 var sovRegionAdd uint64
305 var sovRegionAddBytes uint64
306 var sovRegionFree uint64
307 var sovRegionFreeBytes uint64
308 309 func RecordSovCreate() {
310 sovCreated++
311 }
312 313 func RecordSovRelease() {
314 sovReleased++
315 }
316 317 // The three ways a group can escape release, counted separately so a residual
318 // leak names its own path instead of being inferred:
319 //
320 // untracked - created for a receiver that was in no live arena, so no arena
321 // list ever names it (the documented ctxArena == nil case);
322 // miss - its receiver was not in the receiver map at release time;
323 // refused - its receiver was nil or attributed to another arena.
324 var sovUntracked uint64
325 var sovReleaseMiss uint64
326 var sovReleaseRefused uint64
327 328 func RecordSovUntracked() {
329 sovUntracked++
330 }
331 332 func RecordSovReleaseMiss() {
333 sovReleaseMiss++
334 }
335 336 func RecordSovReleaseRefused() {
337 sovReleaseRefused++
338 }
339 340 // sovMetaBytes is the group bookkeeping (group struct, record slice, batch
341 // stack, slot table) charged to the arena that owns the receiver. It is not a
342 // retention number: it is the proof that the bookkeeping follows the receiver
343 // instead of the process.
344 var sovMetaBytes uint64
345 346 func RecordSovMeta(size uintptr) {
347 sovMetaBytes += uint64(size)
348 }
349 350 // RecordSovRegionAdd charges a doubling arena's new region to the group
351 // accounting.
352 func RecordSovRegionAdd(bytes uintptr) {
353 sovRegionAdd++
354 sovRegionAddBytes += uint64(bytes)
355 }
356 357 // RecordSovRegionFree charges a doubling arena's released regions.
358 func RecordSovRegionFree(count uintptr, bytes uintptr) {
359 sovRegionFree += uint64(count)
360 sovRegionFreeBytes += uint64(bytes)
361 }
362 363 func arenaScopeAlloc(id int32, size uintptr) {
364 if id >= 0 && id < arenaScopeMax {
365 arenaScopes[id].alloc += uint64(size)
366 }
367 }
368 369 func arenaScopeFreed(id int32, size uintptr) {
370 if id >= 0 && id < arenaScopeMax {
371 arenaScopes[id].freed += uint64(size)
372 arenaScopes[id].resets++
373 }
374 }
375 376 // --- trace gate ---
377 //
378 // SetArenaTrace turns the DIRECT-allocation size recorder on. Kept because a
379 // size histogram is the one thing the scope counters cannot show: a single
380 // large allocation (a codec buffer, a bucket array) is one event, not a pattern.
381 382 var arenaTraceOn bool
383 var rootTraceBusy bool
384 385 // SetArenaTrace turns allocation-size capture on or off.
386 func SetArenaTrace(on bool) {
387 arenaTraceOn = on
388 }
389 390 // RecordRootSize charges one DIRECT allocation into the root arena, keyed by
391 // size. Powers of two are the hashmap bucket signature; variable sizes are codec
392 // buffers. It answers "is the root taking bytes", not "from where": the scope
393 // rows answer that, and the group counters answer "for whom".
394 const rootSizeMax = int32(24)
395 396 type rootSizeRec struct {
397 size uintptr
398 count uint64
399 bytes uint64
400 }
401 402 var rootSizes [rootSizeMax]rootSizeRec
403 var rootSizeN int32
404 405 // RecordRootSize records one direct allocation into the root arena.
406 func RecordRootSize(size uintptr) {
407 if rootTraceBusy {
408 return
409 }
410 rootTraceBusy = true
411 var i int32 = 0
412 for i = 0; i < rootSizeN; i++ {
413 if rootSizes[i].size == size {
414 rootSizes[i].count++
415 rootSizes[i].bytes += uint64(size)
416 rootTraceBusy = false
417 return
418 }
419 }
420 if rootSizeN < rootSizeMax {
421 rootSizes[rootSizeN].size = size
422 rootSizes[rootSizeN].count = 1
423 rootSizes[rootSizeN].bytes = uint64(size)
424 rootSizeN++
425 }
426 rootTraceBusy = false
427 }
428 429 // arenaTraceWanted reports whether site capture is on.
430 func arenaTraceWanted() (on bool) {
431 return arenaTraceOn
432 }
433 434 func arenaScopeArena(id int32) {
435 if id >= 0 && id < arenaScopeMax {
436 arenaScopes[id].arenas++
437 }
438 }
439 440 // resetArenaScopeStats clears the counters so a measurement can start from zero.
441 func ResetArenaScopeStats() {
442 for i := int32(0); i < arenaScopeMax; i++ {
443 arenaScopes[i].alloc = 0
444 arenaScopes[i].freed = 0
445 arenaScopes[i].resets = 0
446 arenaScopes[i].arenas = 0
447 }
448 }
449 450 // ArenaScopeReport writes one line per scope that has seen any traffic:
451 //
452 // scope=<id> allocated=<bytes> reclaimed=<bytes> retained=<bytes> resets=<n> arenas=<n>
453 //
454 // retained is the number the whole instrument exists for: bytes this scope's
455 // arenas have allocated and no reset or free has reclaimed. A scope that
456 // retains across many requests is the leak, and its id says which phase of the
457 // port it belongs to (net/http's chanpath.mx assigns the ids).
458 func ArenaScopeReport() (out []byte) {
459 for i := int32(0); i < arenaScopeMax; i++ {
460 r := arenaScopes[i]
461 if r.alloc == 0 && r.freed == 0 && r.arenas == 0 {
462 continue
463 }
464 out = appendScopeStr(out, "scope=")
465 out = appendScopeUint(out, uint64(i))
466 out = appendScopeStr(out, " allocated=")
467 out = appendScopeUint(out, r.alloc)
468 out = appendScopeStr(out, " reclaimed=")
469 out = appendScopeUint(out, r.freed)
470 out = appendScopeStr(out, " retained=")
471 if r.alloc > r.freed {
472 out = appendScopeUint(out, r.alloc-r.freed)
473 } else {
474 out = appendScopeUint(out, 0)
475 }
476 out = appendScopeStr(out, " resets=")
477 out = appendScopeUint(out, uint64(uint32(r.resets)))
478 out = appendScopeStr(out, " arenas=")
479 out = appendScopeUint(out, uint64(uint32(r.arenas)))
480 out = appendScopeStr(out, "\n")
481 }
482 if domainRoot != nil {
483 for i := int32(0); i <= domainRoot.fnTop; i++ {
484 out = emitLiveArena(out, domainRoot.fnStack[i], "live")
485 }
486 out = emitLiveArena(out, trueRootArena(), "root")
487 }
488 for i := int32(0); i < arenaPoolN; i++ {
489 out = emitLiveArena(out, arenaPool[i], "pooled")
490 }
491 out = appendScopeStr(out, "fnarena pushed=")
492 out = appendScopeUint(out, fnPushCount)
493 out = appendScopeStr(out, " released=")
494 out = appendScopeUint(out, fnReleaseCount)
495 out = appendScopeStr(out, " leaked=")
496 out = appendScopeUint(out, fnPushCount-fnReleaseCount)
497 out = appendScopeStr(out, "\n")
498 out = appendScopeStr(out, "sovhome root=")
499 out = appendScopeUint(out, sovHomeRoot)
500 out = appendScopeStr(out, " sov=")
501 out = appendScopeUint(out, sovHomeSov)
502 out = appendScopeStr(out, " frame=")
503 out = appendScopeUint(out, sovHomeFrame)
504 out = appendScopeStr(out, "\n")
505 out = appendScopeStr(out, "sysmap count=")
506 out = appendScopeUint(out, sysMapCount)
507 out = appendScopeStr(out, " bytes=")
508 out = appendScopeUint(out, sysMapBytes)
509 out = appendScopeStr(out, "\n")
510 out = appendScopeStr(out, "sysunmap count=")
511 out = appendScopeUint(out, sysUnmapCount)
512 out = appendScopeStr(out, " bytes=")
513 out = appendScopeUint(out, sysUnmapBytes)
514 out = appendScopeStr(out, "\n")
515 out = appendScopeStr(out, "mapped sysmap_bytes=")
516 out = appendScopeUint(out, sysMapBytes)
517 out = appendScopeStr(out, " sysunmap_bytes=")
518 out = appendScopeUint(out, sysUnmapBytes)
519 out = appendScopeStr(out, " net=")
520 if sysMapBytes > sysUnmapBytes {
521 out = appendScopeUint(out, sysMapBytes-sysUnmapBytes)
522 } else {
523 out = appendScopeUint(out, 0)
524 }
525 out = appendScopeStr(out, " livecap=")
526 out = appendScopeUint(out, uint64(liveArenaCapBytes()))
527 out = appendScopeStr(out, "\n")
528 out = appendScopeStr(out, "sov created=")
529 out = appendScopeUint(out, sovCreated)
530 out = appendScopeStr(out, " released=")
531 out = appendScopeUint(out, sovReleased)
532 out = appendScopeStr(out, " live=")
533 if sovCreated > sovReleased {
534 out = appendScopeUint(out, sovCreated-sovReleased)
535 } else {
536 out = appendScopeUint(out, 0)
537 }
538 out = appendScopeStr(out, " regionadd=")
539 out = appendScopeUint(out, sovRegionAdd)
540 out = appendScopeStr(out, "/")
541 out = appendScopeUint(out, sovRegionAddBytes)
542 out = appendScopeStr(out, " untracked=")
543 out = appendScopeUint(out, sovUntracked)
544 out = appendScopeStr(out, " miss=")
545 out = appendScopeUint(out, sovReleaseMiss)
546 out = appendScopeStr(out, " refused=")
547 out = appendScopeUint(out, sovReleaseRefused)
548 out = appendScopeStr(out, " meta=")
549 out = appendScopeUint(out, sovMetaBytes)
550 out = appendScopeStr(out, " recycled=")
551 out = appendScopeUint(out, sovGroupRecycleCount())
552 out = appendScopeStr(out, " regionfree=")
553 out = appendScopeUint(out, sovRegionFree)
554 out = appendScopeStr(out, "/")
555 out = appendScopeUint(out, sovRegionFreeBytes)
556 out = appendScopeStr(out, "\n")
557 for i := int32(0); i < arenaScopeMax; i++ {
558 if regionAddCount[i] == 0 && regionFreeCount[i] == 0 {
559 continue
560 }
561 out = appendScopeStr(out, "region scope=")
562 out = appendScopeUint(out, uint64(i))
563 out = appendScopeStr(out, " added=")
564 out = appendScopeUint(out, regionAddCount[i])
565 out = appendScopeStr(out, "/")
566 out = appendScopeUint(out, regionAddBytes[i])
567 out = appendScopeStr(out, " freed=")
568 out = appendScopeUint(out, regionFreeCount[i])
569 out = appendScopeStr(out, "/")
570 out = appendScopeUint(out, regionFreeBytes[i])
571 out = appendScopeStr(out, "\n")
572 }
573 for i := int32(0); i < rootSizeN; i++ {
574 out = appendScopeStr(out, "rootsize=")
575 out = appendScopeUint(out, uint64(rootSizes[i].size))
576 out = appendScopeStr(out, " count=")
577 out = appendScopeUint(out, rootSizes[i].count)
578 out = appendScopeStr(out, " bytes=")
579 out = appendScopeUint(out, rootSizes[i].bytes)
580 out = appendScopeStr(out, "\n")
581 }
582 return out
583 }
584 585 // emitLiveArena writes one arena that is currently holding memory: its scope,
586 // what kind of arena it is, how many regions it holds and how many bytes it has
587 // used, and how much it has reserved.
588 func emitLiveArena(out []byte, a *Arena, tag []byte) (res []byte) {
589 res = out
590 if a == nil {
591 return res
592 }
593 used := uintptr(0)
594 for i := int32(0); i < a.nregions; i++ {
595 used += a.regions[i].off
596 }
597 if used == 0 {
598 return res
599 }
600 res = appendScopeStr(res, tag)
601 res = appendScopeStr(res, " scope=")
602 res = appendScopeUint(res, uint64(uint32(a.scope)))
603 res = appendScopeStr(res, " sovereign=")
604 res = appendScopeUint(res, uint64(uint32(a.sovereign)))
605 res = appendScopeStr(res, " n=")
606 res = appendScopeUint(res, uint64(uint32(a.nregions)))
607 res = appendScopeStr(res, " groups=")
608 res = appendScopeUint(res, uint64(uint32(a.sovGroups)))
609 res = appendScopeStr(res, " used=")
610 res = appendScopeUint(res, uint64(used))
611 reserved := uintptr(0)
612 for i := int32(0); i < a.nregions; i++ {
613 reserved += a.regions[i].cap
614 }
615 res = appendScopeStr(res, " cap=")
616 res = appendScopeUint(res, uint64(reserved))
617 res = appendScopeStr(res, "\n")
618 return res
619 }
620 621 // liveArenaCapBytes is every byte of arena memory currently reserved: the root,
622 // the function stack, and the pool. It is the number that must stay flat when a
623 // scope boundary is doing its job - a scope that frees regions but leaves them
624 // mapped shows nothing here, and neither does one that leaves a child group
625 // alive.
626 func liveArenaCapBytes() (n uintptr) {
627 if domainRoot == nil {
628 return 0
629 }
630 for i := int32(0); i <= domainRoot.fnTop; i++ {
631 a := domainRoot.fnStack[i]
632 if a == nil {
633 continue
634 }
635 for j := int32(0); j < a.nregions; j++ {
636 n += a.regions[j].cap
637 }
638 }
639 for i := int32(0); i < arenaPoolN; i++ {
640 a := arenaPool[i]
641 if a == nil {
642 continue
643 }
644 for j := int32(0); j < a.nregions; j++ {
645 n += a.regions[j].cap
646 }
647 }
648 return n
649 }
650 651 func appendScopeStr(b []byte, s []byte) (out []byte) {
652 out = b
653 for i := 0; i < len(s); i++ {
654 out = push(out, s[i])
655 }
656 return out
657 }
658 659 func appendScopeUint(b []byte, v uint64) (out []byte) {
660 out = b
661 if v == 0 {
662 return push(out, '0')
663 }
664 d := []byte{:24}
665 n := int32(0)
666 for v > 0 {
667 d[n] = byte('0' + v%10)
668 n = n + 1
669 v = v / 10
670 }
671 for n > 0 {
672 n = n - 1
673 out = push(out, d[n])
674 }
675 return out
676 }
677