lineReader reads line-by-line using only a single fixed scratch buffer.
When a single line is too long for the scratch buffer, the remainder of the
line will be skipped. // read reached EOF.fdint // bytes of scratch in use. // index of the first newline in scratch.readfunc(fd int, b []byte) (int, uintptr)scratch[]byte line returns a view of the current line, excluding the trailing newline.
If [r.next] returned errIncompleteLine, then this returns only the beginning
of the line.
Preconditions: [r.next] is called prior to the first call to line.
Postconditions: The caller must not keep a reference to the returned slice. next advances to the next line.
May return errIncompleteLine if the scratch buffer is too small to hold the
entire line, in which case [r.line] will return the beginning of the line. A
subsequent call to next will skip the remainder of the incomplete line.
N.B. this behavior is important for /proc/self/mountinfo. Some lines
(mounts), such as overlayfs, may be extremely long due to long super-block
options, but we don't care about those. The mount type will appear early in
the line.
Returns errEOF when there are no more lines.
func newLineReader(fd int, scratch []byte, read func(fd int, b []byte) (n int, errno uintptr)) *lineReader
stringError is a trival implementation of error, equivalent to errors.New,
which cannot be imported from a runtime package.( stringError) Error() string
stringError : error
Package-Level Functions (total 16, in which 5 are exported)
FindCPU finds the path to the CPU cgroup that this process is a member of
and places it in out. scratch is a scratch buffer for internal use.
out must have length PathSize. scratch must have length ParseSize.
Returns the number of bytes written to out and the cgroup version (1 or 2).
Returns ErrNoCgroup if the process is not in a CPU cgroup.
FindCPUMountPoint finds the root of the CPU cgroup mount places it in out.
scratch is a scratch buffer for internal use.
out must have length PathSize. scratch must have length ParseSize.
Returns the number of bytes written to out.
Returns ErrNoCgroup if the process is not in a CPU cgroup.
FindCPURelativePath finds the path to the CPU cgroup that this process is a member of
relative to the root of the cgroup mount and places it in out. scratch is a
scratch buffer for internal use.
out must have length PathSize minus the size of the cgroup mount root (if
known). scratch must have length ParseSize.
Returns the number of bytes written to out and the cgroup version (1 or 2).
Returns ErrNoCgroup if the process is not in a CPU cgroup.
OpenCPU returns a CPU for the CPU cgroup containing the current process, or
ErrNoCgroup if the process is not in a CPU cgroup.
scratch must have length ScratchSize.
Returns average CPU throughput limit from the cgroup, or ok false if there
is no limit.
Returns true if comma-separated list b contains "cpu".
newLineReader returns a lineReader which reads lines from fd.
fd is the file descriptor to read from.
scratch is the scratch buffer to read into. Note that len(scratch) is the
longest line that can be read. Lines longer than len(scratch) will have the
remainder of the line skipped. See next for more details.
read is the function used to read more bytes from fd. This is usually
internal/runtime/syscall.Read. Note that this follows syscall semantics (not
io.Reader), so EOF is indicated with n=0, errno=0.
Returns the mount point for the cpu cgroup controller (v1 or v2) from
/proc/self/mountinfo.
Finds the path of the current process's CPU cgroup relative to the cgroup
mount and writes it to out.
Returns the number of bytes written and the cgroup version (1 or 2).
unescapePath copies in to out, unescaping escape sequences generated by
Linux's show_path.
That is, '\', ' ', '\t', and '\n' are converted to octal escape sequences,
like '\040' for space.
out must be at least as large as in.
Returns the number of bytes written to out.
Also see escapePath in cgroup_linux_test.go.
Package-Level Variables (total 6, in which 1 is exported)
Package-Level Constants (total 11, in which 6 are exported)
Required space to parse /proc/self/mountinfo and /proc/self/cgroup.
See findCPUMount and findCPURelativePath.
Required space to store a path of the cgroup in the filesystem.
Required amount of scratch space for CPULimit.
TODO(prattmic): This is shockingly large (~70KiB) due to the (very
unlikely) combination of extremely long paths consisting mostly
escaped characters. The scratch buffer ends up in .bss in package
runtime, so it doesn't contribute to binary size and generally won't
be faulted in, but it would still be nice to shrink this. A more
complex parser that did not need to keep entire lines in memory
could get away with much less. Alternatively, we could do a one-off
mmap allocation for this buffer, which is only mapped larger if we
actually need the extra space.