Skip to content

Commit

Permalink
Fix Proc.Limits limit name matching
Browse files Browse the repository at this point in the history
I was working on improving this algorithm to reduce the number of
allocations when I found out that with the addition of the additional
test cases, `Max processes` was failing to match the `switch`
statement as for some reason the limit name has a trailing
whitespace. By trimming the spaces it now matches all cases.

Signed-off-by: Leandro López (inkel) <[email protected]>
  • Loading branch information
inkel committed Sep 25, 2024
1 parent a49c6d2 commit 289d012
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion proc_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"regexp"
"strconv"
"strings"
)

// ProcLimits represents the soft limits for each of the process's resource
Expand Down Expand Up @@ -106,7 +107,7 @@ func (p Proc) Limits() (ProcLimits, error) {
return ProcLimits{}, fmt.Errorf("%w: couldn't parse %q line %q", ErrFileParse, f.Name(), s.Text())
}

switch fields[1] {
switch strings.TrimSpace(fields[1]) {
case "Max cpu time":
l.CPUTime, err = parseUint(fields[2])
case "Max file size":
Expand Down
13 changes: 12 additions & 1 deletion proc_limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ func TestLimits(t *testing.T) {
have uint64
}{
{name: "cpu time", want: 18446744073709551615, have: l.CPUTime},
{name: "file size", want: 18446744073709551615, have: l.FileSize},
{name: "data size", want: 18446744073709551615, have: l.DataSize},
{name: "stack size", want: 8388608, have: l.StackSize},
{name: "core file size", want: 0, have: l.CoreFileSize},
{name: "resident set", want: 18446744073709551615, have: l.ResidentSet},
{name: "processes", want: 62898, have: l.Processes},
{name: "open files", want: 2048, have: l.OpenFiles},
{name: "locked memory", want: 18446744073708503040, have: l.LockedMemory},
{name: "address space", want: 8589934592, have: l.AddressSpace},
{name: "file locks", want: 18446744073709551615, have: l.FileLocks},
{name: "pending signals", want: 62898, have: l.PendingSignals},
{name: "msgqueue size", want: 819200, have: l.MsqqueueSize},
{name: "nice priority", want: 0, have: l.NicePriority},
{name: "address space", want: 8589934592, have: l.AddressSpace},
{name: "realtime priority", want: 0, have: l.RealtimePriority},
{name: "realtime timeout", want: 18446744073709551615, have: l.RealtimeTimeout},
} {
if test.want != test.have {
t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
Expand Down

0 comments on commit 289d012

Please sign in to comment.