当前位置: 首页>>代码示例>>Golang>>正文


Golang os.Getpagesize函数代码示例

本文整理汇总了Golang中os.Getpagesize函数的典型用法代码示例。如果您正苦于以下问题:Golang Getpagesize函数的具体用法?Golang Getpagesize怎么用?Golang Getpagesize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Getpagesize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewReader

// create a new zim reader
func NewReader(path string, mmap bool) (*ZimReader, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	z := ZimReader{f: f, mainPage: 0xffffffff, layoutPage: 0xffffffff}

	fi, err := f.Stat()
	if err != nil {
		return nil, err
	}

	size := fi.Size()

	if mmap {
		// we need a multiple of page size bigger than the file
		pc := size / int64(os.Getpagesize())
		totalMmap := pc*int64(os.Getpagesize()) + int64(os.Getpagesize())
		if (size % int64(os.Getpagesize())) == 0 {
			totalMmap = size
		}

		mmap, err := syscall.Mmap(int(f.Fd()), 0, int(totalMmap), syscall.PROT_READ, syscall.MAP_PRIVATE)
		if err != nil {
			return nil, err
		}
		z.mmap = mmap
	}

	err = z.readFileHeaders()
	return &z, err
}
开发者ID:postfix,项目名称:gozim,代码行数:33,代码来源:zim.go

示例2: EdfAnonMap

// EdfAnonMap maps the EdfFile structure into RAM
// IMPORTANT: everything's lost if unmapped
func EdfAnonMap() (*EdfFile, error) {

	var err error

	// Allocate return structure
	ret := new(EdfFile)

	// Create mapping references
	ret.m = make([]mmap.Mmap, 0)

	// Get the page size
	pageSize := int64(os.Getpagesize())

	// Segment size is the size of each mapped region
	ret.pageSize = uint64(pageSize)
	ret.segmentSize = uint64(EDF_LENGTH) * uint64(os.Getpagesize())

	// Set the mode
	ret.mode = edfAnonMode

	// Allocate 4 pages initially
	ret.truncate(4)

	// Generate the header
	ret.createHeader()
	err = ret.writeInitialData()

	// Make sure this gets unmapped on garbage collection
	runtime.SetFinalizer(ret, edfCallFree)

	return ret, err
}
开发者ID:GeekFreaker,项目名称:golearn,代码行数:34,代码来源:map.go

示例3: edfMap

// edfMap takes an os.File and returns an EdfMappedFile
// structure, which represents the mmap'd underlying file
//
// The `mode` parameter takes the following values
//      EDF_CREATE: edfMap will truncate the file to the right length and write the correct header information
//      EDF_READ_WRITE: edfMap will verify header information
//      EDF_READ_ONLY:  edfMap will verify header information
// IMPORTANT: EDF_LENGTH (edf.go) controls the size of the address
// space mapping. This means that the file can be truncated to the
// correct size without remapping. On 32-bit systems, this
// is set to 2GiB.
func edfMap(f *os.File, mode int) (*EdfFile, error) {
	var err error

	// Set up various things
	ret := new(EdfFile)
	ret.f = f
	ret.m = make([]mmap.Mmap, 0)

	// Figure out the flags
	protFlags := mmap.PROT_READ
	if mode == EDF_READ_WRITE || mode == EDF_CREATE {
		protFlags |= mmap.PROT_WRITE
	}
	mapFlags := mmap.MAP_FILE | mmap.MAP_SHARED
	// Get the page size
	pageSize := int64(os.Getpagesize())
	// Segment size is the size of each mapped region
	ret.pageSize = uint64(pageSize)
	ret.segmentSize = uint64(EDF_LENGTH) * uint64(os.Getpagesize())

	// Map the file
	for i := int64(0); i < EDF_SIZE; i += int64(EDF_LENGTH) * pageSize {
		thisMapping, err := mmap.Map(f, i*pageSize, int(int64(EDF_LENGTH)*pageSize), protFlags, mapFlags)
		if err != nil {
			// TODO: cleanup
			return nil, err
		}
		ret.m = append(ret.m, thisMapping)
	}

	// Verify or generate the header
	if mode == EDF_READ_WRITE || mode == EDF_READ_ONLY {
		err = ret.verifyHeader()
		if err != nil {
			return nil, err
		}
	} else if mode == EDF_CREATE {
		err = ret.truncate(4)
		if err != nil {
			return nil, err
		}
		ret.createHeader()
		err = ret.writeInitialData()
	} else {
		err = fmt.Errorf("Unrecognised flags")
	}

	// Make sure this gets unmapped on garbage collection
	runtime.SetFinalizer(ret, edfCallFree)

	// Set the mode
	ret.mode = edfFileMode

	return ret, err

}
开发者ID:GeekFreaker,项目名称:golearn,代码行数:67,代码来源:map.go

示例4: TestFileCreate

func TestFileCreate(t *testing.T) {
	Convey("Creating a non-existent file should succeed", t, func() {
		tempFile, err := ioutil.TempFile(os.TempDir(), "TestFileCreate")
		So(err, ShouldEqual, nil)
		Convey("Mapping the file should succeed", func() {
			mapping, err := edfMap(tempFile, EDF_CREATE)
			So(err, ShouldEqual, nil)
			Convey("Unmapping the file should succeed", func() {
				err = mapping.unmap(EDF_UNMAP_SYNC)
				So(err, ShouldEqual, nil)
			})

			// Read the magic bytes
			magic := make([]byte, 4)
			read, err := tempFile.ReadAt(magic, 0)
			Convey("Magic bytes should be correct", func() {
				So(err, ShouldEqual, nil)
				So(read, ShouldEqual, 4)
				So(magic[0], ShouldEqual, byte('G'))
				So(magic[1], ShouldEqual, byte('O'))
				So(magic[2], ShouldEqual, byte('L'))
				So(magic[3], ShouldEqual, byte('N'))
			})
			// Read the file version
			versionBytes := make([]byte, 4)
			read, err = tempFile.ReadAt(versionBytes, 4)
			Convey("Version should be correct", func() {
				So(err, ShouldEqual, nil)
				So(read, ShouldEqual, 4)
				version := uint32FromBytes(versionBytes)
				So(version, ShouldEqual, EDF_VERSION)
			})
			// Read the block size
			blockBytes := make([]byte, 4)
			read, err = tempFile.ReadAt(blockBytes, 8)
			Convey("Page size should be correct", func() {
				So(err, ShouldEqual, nil)
				So(read, ShouldEqual, 4)
				pageSize := uint32FromBytes(blockBytes)
				So(pageSize, ShouldEqual, os.Getpagesize())
			})
			// Check the file size is at least four * page size
			info, err := tempFile.Stat()
			Convey("File should be the right size", func() {
				So(err, ShouldEqual, nil)
				So(info.Size(), ShouldBeGreaterThanOrEqualTo, 4*os.Getpagesize())
			})
		})
	})
}
开发者ID:GeekFreaker,项目名称:golearn,代码行数:50,代码来源:map_test.go

示例5: FileMincore

// mmap the given file, get the mincore vector, then
// return it as an []bool
func FileMincore(f *os.File, size int64) ([]bool, error) {
	//skip could not mmap error when the file size is 0
	if int(size) == 0 {
		return nil, nil
	}
	// mmap is a []byte
	mmap, err := unix.Mmap(int(f.Fd()), 0, int(size), unix.PROT_NONE, unix.MAP_SHARED)
	if err != nil {
		return nil, fmt.Errorf("could not mmap: %v", err)
	}
	// TODO: check for MAP_FAILED which is ((void *) -1)
	// but maybe unnecessary since it looks like errno is always set when MAP_FAILED

	// one byte per page, only LSB is used, remainder is reserved and clear
	vecsz := (size + int64(os.Getpagesize()) - 1) / int64(os.Getpagesize())
	vec := make([]byte, vecsz)

	// get all of the arguments to the mincore syscall converted to uintptr
	mmap_ptr := uintptr(unsafe.Pointer(&mmap[0]))
	size_ptr := uintptr(size)
	vec_ptr := uintptr(unsafe.Pointer(&vec[0]))

	// use Go's ASM to submit directly to the kernel, no C wrapper needed
	// mincore(2): int mincore(void *addr, size_t length, unsigned char *vec);
	// 0 on success, takes the pointer to the mmap, a size, which is the
	// size that came from f.Stat(), and the vector, which is a pointer
	// to the memory behind an []byte
	// this writes a snapshot of the data into vec which a list of 8-bit flags
	// with the LSB set if the page in that position is currently in VFS cache
	ret, _, err := unix.Syscall(unix.SYS_MINCORE, mmap_ptr, size_ptr, vec_ptr)
	if ret != 0 {
		return nil, fmt.Errorf("syscall SYS_MINCORE failed: %v", err)
	}
	defer unix.Munmap(mmap)

	mc := make([]bool, vecsz)

	// there is no bitshift only bool
	for i, b := range vec {
		if b%2 == 1 {
			mc[i] = true
		} else {
			mc[i] = false
		}
	}

	return mc, nil
}
开发者ID:cholcombe973,项目名称:pcstat,代码行数:50,代码来源:mincore.go

示例6: TestWalkMemoryDoesntOverlapTheBuffer

func TestWalkMemoryDoesntOverlapTheBuffer(t *testing.T) {
	cmd, err := test.LaunchTestCaseAndWaitForInitialization()
	if err != nil {
		t.Fatal(err)
	}
	defer cmd.Process.Kill()

	pid := uint(cmd.Process.Pid)
	proc, err, softerrors := process.OpenFromPid(pid)
	test.PrintSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	}

	pageSize := uint(os.Getpagesize())
	bufferSizes := []uint{1024, pageSize, pageSize + 100, pageSize * 2, pageSize*2 + 123}
	for _, size := range bufferSizes {

		lastRegion := MemoryRegion{}
		err, softerrors = WalkMemory(proc, 0, size, func(address uintptr, buffer []byte) (keepSearching bool) {
			currentRegion := MemoryRegion{Address: address, Size: uint(len(buffer))}
			if memoryRegionsOverlap(lastRegion, currentRegion) {
				t.Errorf("Regions overlap while reading %d at a time: %v %v", size, lastRegion, currentRegion)
				return false
			}

			lastRegion = currentRegion
			return true
		})
		test.PrintSoftErrors(softerrors)
		if err != nil {
			t.Fatal(err)
		}
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:35,代码来源:memaccess_test.go

示例7: TestBucket_Stats_Small

// Ensure a bucket can calculate stats.
func TestBucket_Stats_Small(t *testing.T) {

	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			// Add a bucket that fits on a single root leaf.
			b, err := tx.CreateBucket([]byte("whozawhats"))
			assert.NoError(t, err)
			b.Put([]byte("foo"), []byte("bar"))

			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("whozawhats"))
			stats := b.Stats()
			assert.Equal(t, stats.BranchPageN, 0)
			assert.Equal(t, stats.BranchOverflowN, 0)
			assert.Equal(t, stats.LeafPageN, 1)
			assert.Equal(t, stats.LeafOverflowN, 0)
			assert.Equal(t, stats.KeyN, 1)
			assert.Equal(t, stats.Depth, 1)
			if os.Getpagesize() != 4096 {
				// Incompatible page size
				assert.Equal(t, stats.BranchInuse, 0)
				assert.Equal(t, stats.BranchAlloc, 0)
				assert.Equal(t, stats.LeafInuse, 38)
				assert.Equal(t, stats.LeafAlloc, 4096)
			}
			return nil
		})
	})
}
开发者ID:jebjerg,项目名称:bolt,代码行数:33,代码来源:bucket_test.go

示例8: newInputPreloader

func newInputPreloader(filePath string) (*inputPreloader, error) {
	if filePath == "/dev/null" {
		return nil, nil
	}
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	info, err := file.Stat()
	if err != nil {
		return nil, err
	}
	preloader := &inputPreloader{
		file:     file,
		fileSize: info.Size(),
	}
	mapping, err := syscall.Mmap(
		int(preloader.file.Fd()),
		0,
		int(preloader.fileSize),
		syscall.PROT_READ,
		syscall.MAP_SHARED,
	)
	if err == nil {
		pageSize := os.Getpagesize()
		preloader.mapping = mapping
		for i := 0; i < int(preloader.fileSize); i += pageSize {
			preloader.checksum += preloader.mapping[i]
		}
	} else {
		// mmap failed, so just read all the file.
		io.Copy(ioutil.Discard, preloader.file)
	}
	return preloader, nil
}
开发者ID:lhchavez,项目名称:quark,代码行数:35,代码来源:sandbox.go

示例9: TestBucket_Stats_EmptyBucket

func TestBucket_Stats_EmptyBucket(t *testing.T) {

	withOpenDB(func(db *DB, path string) {
		db.Update(func(tx *Tx) error {
			// Add a bucket that fits on a single root leaf.
			_, err := tx.CreateBucket([]byte("whozawhats"))
			assert.NoError(t, err)
			return nil
		})
		mustCheck(db)
		db.View(func(tx *Tx) error {
			b := tx.Bucket([]byte("whozawhats"))
			stats := b.Stats()
			assert.Equal(t, 0, stats.BranchPageN, "BranchPageN")
			assert.Equal(t, 0, stats.BranchOverflowN, "BranchOverflowN")
			assert.Equal(t, 0, stats.LeafPageN, "LeafPageN")
			assert.Equal(t, 0, stats.LeafOverflowN, "LeafOverflowN")
			assert.Equal(t, 0, stats.KeyN, "KeyN")
			assert.Equal(t, 1, stats.Depth, "Depth")
			assert.Equal(t, 0, stats.BranchInuse, "BranchInuse")
			assert.Equal(t, 0, stats.LeafInuse, "LeafInuse")
			if os.Getpagesize() == 4096 {
				// Incompatible page size
				assert.Equal(t, 0, stats.BranchAlloc, "BranchAlloc")
				assert.Equal(t, 0, stats.LeafAlloc, "LeafAlloc")
			}
			assert.Equal(t, 1, stats.BucketN, "BucketN")
			assert.Equal(t, 1, stats.InlineBucketN, "InlineBucketN")
			assert.Equal(t, pageHeaderSize, stats.InlineBucketInuse, "InlineBucketInuse")
			return nil
		})
	})
}
开发者ID:udnay,项目名称:bolt,代码行数:33,代码来源:bucket_test.go

示例10: TestMonitorAndParseRIB

func TestMonitorAndParseRIB(t *testing.T) {
	if testing.Short() || os.Getuid() != 0 {
		t.Skip("must be root")
	}

	// We suppose that using an IPv4 link-local address and the
	// dot1Q ID for Token Ring and FDDI doesn't harm anyone.
	pv := &propVirtual{addr: "169.254.0.1", mask: "255.255.255.0"}
	if err := pv.configure(1002); err != nil {
		t.Skip(err)
	}
	if err := pv.setup(); err != nil {
		t.Skip(err)
	}
	pv.teardown()

	s, err := syscall.Socket(syscall.AF_ROUTE, syscall.SOCK_RAW, syscall.AF_UNSPEC)
	if err != nil {
		t.Fatal(err)
	}
	defer syscall.Close(s)

	go func() {
		b := make([]byte, os.Getpagesize())
		for {
			n, err := syscall.Read(s, b)
			if err != nil {
				return
			}
			ms, err := ParseRIB(0, b[:n])
			if err != nil {
				t.Error(err)
				return
			}
			ss, err := msgs(ms).validate()
			if err != nil {
				t.Error(err)
				return
			}
			for _, s := range ss {
				t.Log(s)
			}
		}
	}()

	for _, vid := range []int{1002, 1003, 1004, 1005} {
		pv := &propVirtual{addr: "169.254.0.1", mask: "255.255.255.0"}
		if err := pv.configure(vid); err != nil {
			t.Fatal(err)
		}
		if err := pv.setup(); err != nil {
			t.Fatal(err)
		}
		time.Sleep(200 * time.Millisecond)
		if err := pv.teardown(); err != nil {
			t.Fatal(err)
		}
		time.Sleep(200 * time.Millisecond)
	}
}
开发者ID:XLabWang,项目名称:net,代码行数:60,代码来源:message_test.go

示例11: Open

func (s *AfpacketSniffer) Open(config *Config) error {
	// Capture settings
	const (
		// MMap buffer size
		buffer_mb int = 24
		// Max packet length
		snaplen int = 65536
		// Set the interface in promiscuous mode
		promisc bool = true
	)

	frame_size, block_size, num_blocks, err := afpacketComputeSize(
		buffer_mb,
		snaplen,
		os.Getpagesize())
	if err != nil {
		return fmt.Errorf("Error calculating afpacket size: %s", err)
	}

	// Configure the afpacket ring and bind it to the interface
	var tPacket *afpacket.TPacket
	tPacket, err = afpacket.NewTPacket(
		afpacket.OptInterface(*iface),
		afpacket.OptFrameSize(frame_size),
		afpacket.OptBlockSize(block_size),
		afpacket.OptNumBlocks(num_blocks))
	if err != nil {
		fmt.Errorf("Error opening afpacket interface: %s", err)
	}
	s.handle = tPacket

	return nil
}
开发者ID:postfix,项目名称:sniffer,代码行数:33,代码来源:afpacket.go

示例12: TestAllocFixed

func TestAllocFixed(t *testing.T) {
	Convey("Creating a non-existent file should succeed", t, func() {
		tempFile, err := ioutil.TempFile(os.TempDir(), "TestFileCreate")
		So(err, ShouldEqual, nil)
		Convey("Mapping the file should succeed", func() {
			mapping, err := edfMap(tempFile, EDF_CREATE)
			So(err, ShouldEqual, nil)
			Convey("Allocation should succeed", func() {
				r, err := mapping.AllocPages(1, 2)
				So(err, ShouldEqual, nil)
				So(r.Start.Byte, ShouldEqual, 4*os.Getpagesize())
				So(r.Start.Segment, ShouldEqual, 0)
				Convey("Unmapping the file should succeed", func() {
					err = mapping.unmap(EDF_UNMAP_SYNC)
					So(err, ShouldEqual, nil)
					Convey("Remapping the file should succeed", func() {
						mapping, err = edfMap(tempFile, EDF_READ_ONLY)
						Convey("Should get the same allocations back", func() {
							rr, err := mapping.getThreadBlocks(2)
							So(err, ShouldEqual, nil)
							So(len(rr), ShouldEqual, 1)
							So(rr[0], ShouldResemble, r)
						})
					})
				})
			})
		})
	})
}
开发者ID:GeekFreaker,项目名称:golearn,代码行数:29,代码来源:alloc_test.go

示例13: getSize

// use large buffer size. heuristic multiple of os size.
func getSize() int {
	size := os.Getpagesize()
	if size < 65536 {
		return size * 16
	}
	return size
}
开发者ID:pombredanne,项目名称:xopen,代码行数:8,代码来源:xopen.go

示例14: TestPeek

func TestPeek(t *testing.T) {
	pagesize := os.Getpagesize()

	peeks := []int{3, pagesize * 2 / 3, 3, pagesize - 1, 3, pagesize * 13 / 5, 3, 0, 3}
	counts := []int{1, 0, 0, 0, 0, 1, 0, 0, 1}
	sizes := []int{0, 1, 0, pagesize * 2 / 3, 0, pagesize * 11 / 7, 0, pagesize * 13 / 5, 0}
	lastAttempt := 511
	incomplete := 255
	lastCount := 0

	total := incomplete
	for _, size := range sizes {
		total += size
	}

	rndr := newrndreader(23, 0, total, io.EOF, nil)
	r := bufio.NewReader(rndr)

	offset := 0
	for i, size := range sizes {
		verifypeek(t, r, peeks[i], []error{nil}, rndr, 23, offset, peeks[i], counts[i])
		offset = verifyread(t, r, size, []error{nil}, rndr, 23, offset, size)
	}

	verifypeek(t, r, lastAttempt, []error{io.EOF}, rndr, 23, offset, incomplete, lastCount)
}
开发者ID:blubberdiblub,项目名称:bufio,代码行数:26,代码来源:reader_test.go

示例15: OtherSystemInfo

// OtherSystemInfo retrieves information from the system like hostname, IPv4 address, pagesize,
// target architecture and target operating system.
func OtherSystemInfo() map[string]string {
	otherInfoMap := make(map[string]string)

	// Hostname
	hostname, err := os.Hostname()
	if err != nil {
		otherInfoMap["hostname"] = "Error: The hostname for the current system could not be retrieved."
	} else {
		otherInfoMap["hostname"] = hostname
	}

	// IP address
	addresses, err := net.LookupHost(hostname)
	if err != nil {
		otherInfoMap["ipv4_address"] = "Error: The IPv4 address for the current system could not be retrieved."
	} else {
		for _, address := range addresses {
			ipv4_address := net.ParseIP(address).To4()
			if ipv4_address != nil {
				otherInfoMap["ipv4_address"] = address
			}
		}
	}

	otherInfoMap["os_pagesize"] = strconv.Itoa(os.Getpagesize())
	otherInfoMap["target_architecture"] = runtime.GOARCH
	otherInfoMap["target_os"] = runtime.GOOS
	return otherInfoMap
}
开发者ID:tstatler,项目名称:continuum-sample-apps,代码行数:31,代码来源:system_info.go


注:本文中的os.Getpagesize函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。