Commit Graph

44 Commits

Author SHA1 Message Date
Etienne Perot 064faf80a4 runsc metric-server: Optimize memory usage and allocation-heavy functions.
This is an effort to reduce it to be a well-behaved background process.

With 110 sandboxes running, at rest, this goes from

```
VmRSS:   72376 kB
RssAnon: 51944 kB
```

to:

```
VmRSS:   45864 kB
RssAnon: 25788 kB
```

This GCs much more aggressively, including after every single request, which
means we do spend disproportionately more CPU in order to get that low memory
usage. From my testing, serving requests takes about 12% more CPU, and it's
all spent in GC.

The optimizations that went into this are:

- Add a method in `state` to discard the global type maps.
- Add a custom "packed" number type in `prometheus` library that encodes small
  integers and floating-point numbers in 32 bits whenever possible without
  loss of precision, otherwise they are encoded in their full 64-bit glory and
  the 32-bit representation is used as a pointer to the 64-bit representation.
  These are stored either per-sandbox (for static-after-sandbox-creation
  numbers like distribution bucket boundaries), or per-metric-retrieval
  attempt otherwise.
- Use string interning for commonly-seen strings across sandboxes, like metric
  names and label names. Label values are also interned, but only at a
  per-sandbox granularity.
- Reworked allocation-heavy functions like `OrderedLabels` and some string
  rendering functions to be (almost) allocation-free. This doesn't reduce
  memory usage at rest, and does increase their CPU cost, but in return it
  significantly cuts down on the percentage of CPU time spent in GC
  (>50% -> 25%) enough to justify spending the extra CPU in these functions.

PiperOrigin-RevId: 515181387
2023-03-08 17:15:52 -08:00
Adin Scannell 1ceb814544 Add default_applicable_licenses rules to packages.
PiperOrigin-RevId: 513581243
2023-03-02 10:50:04 -08:00
Adin Scannell 12a930a63e Move goid to dynamic facts render.
This removes the need for ongoing tags.

This change requires some minor updates to remove dependency cycles, since
the goid package is a base library used by many internals (log, sync, etc.).

PiperOrigin-RevId: 504066914
2023-01-23 13:29:37 -08:00
Rahat Mahmood a17ad261d6 stateify: Handle multi-name fields in struct declarations.
PiperOrigin-RevId: 500268939
2023-01-06 15:32:10 -08:00
Kevin Krakauer d8aa09e04c convert uses of interface{} to any
Done via:
  find . -name "*.go" | xargs sed -i -E 's/interface\{\}/any/g'

PiperOrigin-RevId: 487033228
2022-11-08 13:14:06 -08:00
Ayush Ranjan 5ebf3246df Make syncableDentries and specialFileFDs linked lists in gofer client.
fs.syncableDentries saves all non-synthetic dentries. This requires a map insert
operation every time a new dentry is created and map removal operation when a
dentry is destroyed. This can be expensive there can be a very large number of
non-synthetic dentries.

Using a map does not provide any additional benefits. We do not require lookup.
Instead use a linked list, as insert and remove are really fast and it allows us
to iterate on the list. It also saves the heap allocations to maintain the map.

Also simplify pkg/state to not use a custom ElementMapper. There is no need to.

PiperOrigin-RevId: 476145150
2022-09-22 11:10:26 -07:00
Kevin Krakauer f78c23ffd6 state: don't print the encoded object twice upon error
Sufficiently junky data can cause printing to take a *long* time. OSS-fuzz times
out entirely [1]. On my desktop it takes ~20s to run that test.

1 - https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=45414&q=gvisor&can=2

PiperOrigin-RevId: 453774647
2022-06-08 15:03:27 -07:00
Ayush Ranjan f6ed4523dc Reformat codebase.
PiperOrigin-RevId: 449358041
2022-05-17 17:48:35 -07:00
Jamie Liu 1ad3822200 Add go:build directives as required by Go 1.17's gofmt.
PiperOrigin-RevId: 385894869
2021-07-20 16:28:45 -07:00
Rahat Mahmood e00bd82816 Remove uses of the binary package from the rest of the sentry.
PiperOrigin-RevId: 372020696
2021-05-04 16:41:08 -07:00
Adin Scannell ab32fa2481 Make type sanity checking happen only in race builds.
This adds significant costs to startup, since it is done for
every type in the system. Since the state package already saves
sanity checks for race builds, use this for type registration.

PiperOrigin-RevId: 350259336
2021-01-05 18:15:27 -08:00
Adin Scannell 4cba3904f4 Remove existing nogo exceptions.
PiperOrigin-RevId: 347047550
2020-12-11 12:06:49 -08:00
Jamie Liu 227fd9f1b0 //pkg/state fixes for VFS2.
- When encodeState.resolve() determines that the resolved reflect.Value is
  contained by a previously-resolved object, set wire.Ref.Type to the
  containing object's type (existing.obj.Type()) rather than the contained
  value's type (obj.Type()).

- When encodeState.resolve() determines that the resolved reflect.Value
  contains a previously-resolved object, handle cases where the new object
  contains *multiple* previously-resolved objects. (This may cause
  previously-allocated object IDs to become unused; to facilitate this, change
  encodeState.pending to a map, and change the wire format to prefix each
  object with its object ID.)

- Add encodeState.encodedStructs to avoid redundant encoding of structs, since
  deduplication of objects via encodeState.resolve() doesn't work for objects
  instantiated by StateSave() and passed to SaveValue() (i.e. fields tagged
  `state:".(whatever)"`).

- Make unexported array fields deserializable via slices that refer to them by
  casting away their unexportedness in decodeState.decodeObject().

Updates #1663

PiperOrigin-RevId: 338727687
2020-10-23 12:53:20 -07:00
Adin Scannell 0a7075f38a Add basic stateify annotations.
Updates #1663

PiperOrigin-RevId: 333539293
2020-09-24 10:13:04 -07:00
Ting-Yu Wang ba25485d96 stateify: Bring back struct field and type names in pretty print
PiperOrigin-RevId: 329349158
2020-08-31 12:06:00 -07:00
Ting-Yu Wang 9607515aed stateify: Fix pretty print not printing odd numbered fields.
PiperOrigin-RevId: 327902182
2020-08-21 17:34:26 -07:00
Ting-Yu Wang df48227099 stateify: Fix afterLoad not being called for root object
PiperOrigin-RevId: 327711264
2020-08-20 15:40:15 -07:00
Adin Scannell 364ac92baf Support for saving pointers to fields in the state package.
Previously, it was not possible to encode/decode an object graph which
contained a pointer to a field within another type. This was because the
encoder was previously unable to disambiguate a pointer to an object and a
pointer within the object.

This CL remedies this by constructing an address map tracking the full memory
range object occupy. The encoded Refvalue message has been extended to allow
references to children objects within another object. Because the encoding
process may learn about object structure over time, we cannot encode any
objects under the entire graph has been generated.

This CL also updates the state package to use standard interfaces intead of
reflection-based dispatch in order to improve performance overall. This
includes a custom wire protocol to significantly reduce the number of
allocations and take advantage of structure packing.

As part of these changes, there are a small number of minor changes in other
places of the code base:

* The lists used during encoding are changed to use intrusive lists with the
  objectEncodeState directly, which required that the ilist Len() method is
  updated to work properly with the ElementMapper mechanism.

* A bug is fixed in the list code wherein Remove() called on an element that is
  already removed can corrupt the list (removing the element if there's only a
  single element). Now the behavior is correct.

* Standard error wrapping is introduced.

* Compressio was updated to implement the new wire.Reader and wire.Writer
  inteface methods directly. The lack of a ReadByte and WriteByte caused issues
  not due to interface dispatch, but because underlying slices for a Read or
  Write call through an interface would always escape to the heap!

* Statify has been updated to support the new APIs.

See README.md for a description of how the new mechanism works.

PiperOrigin-RevId: 318010298
2020-06-23 23:34:06 -07:00
Rahat Mahmood 49d2cf287d Remove implicit dependencies for leaf packages.
These packages don't actually use go_stateify or go_marshal, but end
up implicitly dependent on the respective packages due to our build
rules.

These unnecessary dependencies make them unusuable in certain contexts
due to circular dependency.

PiperOrigin-RevId: 312595738
2020-05-20 18:35:32 -07:00
Adin Scannell 94b793262d Fix all copy locks violations.
This required minor restructuring of how system call tables were saved
and restored, but it makes way more sense this way.

Updates #2243
2020-04-08 10:00:14 -07:00
Adin Scannell d29e59af9f Standardize on tools directory.
PiperOrigin-RevId: 291745021
2020-01-27 12:21:00 -08:00
Adin Scannell c0f89eba6e Import and structure cleanup.
PiperOrigin-RevId: 281795269
2019-11-21 11:41:30 -08:00
Adin Scannell a99d3479a8 Add context to state.
PiperOrigin-RevId: 277840416
2019-10-31 18:03:24 -07:00
Kevin Krakauer 2a82d5ad68 Reorder BUILD license and load functions in gvisor.
PiperOrigin-RevId: 275139066
2019-10-16 16:40:30 -07:00
Michael Pratt df5d377521 Remove go_test from go_stateify and go_marshal
They are no-ops, so the standard rule works fine.

PiperOrigin-RevId: 268776264
2019-09-12 15:10:17 -07:00