...

Source file src/github.com/prometheus/procfs/proc_maps64_test.go

Documentation: github.com/prometheus/procfs

     1  // Copyright 2019 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:build (aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris) && !386 && !arm && !mips && !mipsle
    15  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
    16  // +build !386
    17  // +build !arm
    18  // +build !mips
    19  // +build !mipsle
    20  
    21  package procfs
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/google/go-cmp/cmp"
    27  	"golang.org/x/sys/unix"
    28  )
    29  
    30  func TestProcMaps(t *testing.T) {
    31  	tsts64 := []*ProcMap{
    32  		{
    33  			StartAddr: 0x55680ae1e000,
    34  			EndAddr:   0x55680ae20000,
    35  			Perms:     &ProcMapPermissions{true, false, false, false, true},
    36  			Offset:    0,
    37  			Dev:       unix.Mkdev(0xfd, 0x01),
    38  			Inode:     47316994,
    39  			Pathname:  "/bin/cat",
    40  		},
    41  		{
    42  			StartAddr: 0x55680ae29000,
    43  			EndAddr:   0x55680ae2a000,
    44  			Perms:     &ProcMapPermissions{true, true, true, true, false},
    45  			Offset:    40960,
    46  			Dev:       unix.Mkdev(0xfd, 0x01),
    47  			Inode:     47316994,
    48  			Pathname:  "/bin/cat",
    49  		},
    50  		{
    51  			StartAddr: 0x55680bed6000,
    52  			EndAddr:   0x55680bef7000,
    53  			Perms:     &ProcMapPermissions{true, true, false, false, true},
    54  			Offset:    0,
    55  			Dev:       unix.Mkdev(0, 0),
    56  			Inode:     0,
    57  			Pathname:  "[heap]",
    58  		},
    59  		{
    60  			StartAddr: 0x7fdf964fc000,
    61  			EndAddr:   0x7fdf973f2000,
    62  			Perms:     &ProcMapPermissions{true, false, false, false, true},
    63  			Offset:    0,
    64  			Dev:       unix.Mkdev(0xfd, 0x01),
    65  			Inode:     17432624,
    66  			Pathname:  "/usr/lib/locale/locale-archive",
    67  		},
    68  		{
    69  			StartAddr: 0x7fdf973f2000,
    70  			EndAddr:   0x7fdf97417000,
    71  			Perms:     &ProcMapPermissions{true, false, false, false, true},
    72  			Offset:    0,
    73  			Dev:       unix.Mkdev(0xfd, 0x01),
    74  			Inode:     60571062,
    75  			Pathname:  "/lib/x86_64-linux-gnu/libc-2.29.so",
    76  		},
    77  		{
    78  			StartAddr: 0x7ffe9215c000,
    79  			EndAddr:   0x7ffe9217f000,
    80  			Perms:     &ProcMapPermissions{true, true, false, false, true},
    81  			Offset:    0,
    82  			Dev:       0,
    83  			Inode:     0,
    84  			Pathname:  "[stack]",
    85  		},
    86  		{
    87  			StartAddr: 0x7ffe921da000,
    88  			EndAddr:   0x7ffe921dd000,
    89  			Perms:     &ProcMapPermissions{true, false, false, false, true},
    90  			Offset:    0,
    91  			Dev:       0,
    92  			Inode:     0,
    93  			Pathname:  "[vvar]",
    94  		},
    95  		{
    96  			StartAddr: 0x7ffe921dd000,
    97  			EndAddr:   0x7ffe921de000,
    98  			Perms:     &ProcMapPermissions{true, false, true, false, true},
    99  			Offset:    0,
   100  			Dev:       0,
   101  			Inode:     0,
   102  			Pathname:  "[vdso]",
   103  		},
   104  		{
   105  			StartAddr: 0xffffffffff600000,
   106  			EndAddr:   0xffffffffff601000,
   107  			Perms:     &ProcMapPermissions{false, false, true, false, true},
   108  			Offset:    0,
   109  			Dev:       0,
   110  			Inode:     0,
   111  			Pathname:  "[vsyscall]",
   112  		},
   113  	}
   114  
   115  	// 64-bit test pid and fixtures
   116  	tpid := 26232
   117  	tsts := tsts64
   118  
   119  	p, err := getProcFixtures(t).Proc(tpid)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	maps, err := p.ProcMaps()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	if want, have := len(maps), len(tsts); want > have {
   130  		t.Errorf("want at least %d parsed proc/map entries, have %d", want, have)
   131  	}
   132  
   133  	for idx, tst := range tsts {
   134  		want, got := tst, maps[idx]
   135  		if diff := cmp.Diff(want, got); diff != "" {
   136  			t.Fatalf("unexpected proc/map entry (-want +got):\n%s", diff)
   137  		}
   138  	}
   139  
   140  }
   141  
   142  var start, end uintptr
   143  
   144  func BenchmarkParseAddress(b *testing.B) {
   145  	b.ReportAllocs()
   146  	var (
   147  		s, e uintptr
   148  		err  error
   149  	)
   150  	for i := 0; i < b.N; i++ {
   151  		s, e, err = parseAddresses("7f7d7469e000-7f7d746a0000")
   152  		if err != nil {
   153  			b.Fatal(err)
   154  		}
   155  	}
   156  	// Prevent the compiler from optimizing away benchmark code.
   157  	start = s
   158  	end = e
   159  }
   160  
   161  var device uint64
   162  
   163  func BenchmarkParseDevice(b *testing.B) {
   164  	b.ReportAllocs()
   165  	var (
   166  		d   uint64
   167  		err error
   168  	)
   169  	for i := 0; i < b.N; i++ {
   170  		d, err = parseDevice("00:22")
   171  		if err != nil {
   172  			b.Fatal(err)
   173  		}
   174  	}
   175  	// Prevent the compiler from optimizing away benchmark code.
   176  	device = d
   177  }
   178  

View as plain text