...

Source file src/runtime/testdata/testprog/syscall_windows.go

Documentation: runtime/testdata/testprog

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"internal/syscall/windows"
     9  	"runtime"
    10  	"sync"
    11  	"syscall"
    12  	"unsafe"
    13  )
    14  
    15  func init() {
    16  	register("RaiseException", RaiseException)
    17  	register("ZeroDivisionException", ZeroDivisionException)
    18  	register("StackMemory", StackMemory)
    19  }
    20  
    21  func RaiseException() {
    22  	const EXCEPTION_NONCONTINUABLE = 1
    23  	mod := syscall.MustLoadDLL("kernel32.dll")
    24  	proc := mod.MustFindProc("RaiseException")
    25  	proc.Call(0xbad, EXCEPTION_NONCONTINUABLE, 0, 0)
    26  	println("RaiseException should not return")
    27  }
    28  
    29  func ZeroDivisionException() {
    30  	x := 1
    31  	y := 0
    32  	z := x / y
    33  	println(z)
    34  }
    35  
    36  func getPagefileUsage() (uintptr, error) {
    37  	p, err := syscall.GetCurrentProcess()
    38  	if err != nil {
    39  		return 0, err
    40  	}
    41  	var m windows.PROCESS_MEMORY_COUNTERS
    42  	err = windows.GetProcessMemoryInfo(p, &m, uint32(unsafe.Sizeof(m)))
    43  	if err != nil {
    44  		return 0, err
    45  	}
    46  	return m.PagefileUsage, nil
    47  }
    48  
    49  func StackMemory() {
    50  	mem1, err := getPagefileUsage()
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	const threadCount = 100
    55  	var wg sync.WaitGroup
    56  	for i := 0; i < threadCount; i++ {
    57  		wg.Add(1)
    58  		go func() {
    59  			runtime.LockOSThread()
    60  			wg.Done()
    61  			select {}
    62  		}()
    63  	}
    64  	wg.Wait()
    65  	mem2, err := getPagefileUsage()
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  	// assumes that this process creates 1 thread for each
    70  	// thread locked goroutine plus extra 10 threads
    71  	// like sysmon and others
    72  	print((mem2 - mem1) / (threadCount + 10))
    73  }
    74  

View as plain text